mirror of
https://github.com/nushell/nushell
synced 2025-01-08 03:09:00 +00:00
0560826414
Based on the discussion in #13419. ## Description Reworks the `decode`/`encode` commands by adding/changing the following bases: - `base32` - `base32hex` - `hex` - `new-base64` The `hex` base is compatible with the previous version of `hex` out of the box (it only adds more flags). `base64` isn't, so the PR adds a new version and deprecates the old one. All commands have `string -> binary` signature for decoding and `string | binary -> string` signature for encoding. A few `base64` encodings, which are not a part of the [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648#section-6), have been dropped. ## Example usage ```Nushell ~/fork/nushell> "string" | encode base32 | decode base32 | decode string ``` ```Nushell ~/fork/nushell> "ORSXG5A=" | decode base32 # `decode` always returns a binary value Length: 4 (0x4) bytes | printable whitespace ascii_other non_ascii 00000000: 74 65 73 74 test ``` ## User-Facing Changes - New commands: `encode/decode base32/base32hex`. - `encode hex` gets a `--lower` flag. - `encode/decode base64` deprecated in favor of `encode/decode new-base64`.
86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use nu_test_support::nu;
|
||
|
||
#[test]
|
||
fn canonical() {
|
||
for value in super::random_bytes() {
|
||
let outcome = nu!(
|
||
"{} | encode new-base64 | decode new-base64 | to nuon",
|
||
value
|
||
);
|
||
assert_eq!(outcome.out, value);
|
||
|
||
let outcome = nu!(
|
||
"{} | encode new-base64 --url | decode new-base64 --url | to nuon",
|
||
value
|
||
);
|
||
assert_eq!(outcome.out, value);
|
||
|
||
let outcome = nu!(
|
||
"{} | encode new-base64 --nopad | decode new-base64 --nopad | to nuon",
|
||
value
|
||
);
|
||
assert_eq!(outcome.out, value);
|
||
|
||
let outcome = nu!(
|
||
"{} | encode new-base64 --url --nopad | decode new-base64 --url --nopad | to nuon",
|
||
value
|
||
);
|
||
assert_eq!(outcome.out, value);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn encode() {
|
||
let text = "Ș̗͙̂̏o̲̲̗͗̌͊m̝̊̓́͂ë̡̦̞̤́̌̈́̀ ̥̝̪̎̿ͅf̧̪̻͉͗̈́̍̆u̮̝͌̈́ͅn̹̞̈́̊k̮͇̟͎̂͘y̧̲̠̾̆̕ͅ ̙͖̭͔̂̐t̞́́͘e̢̨͕̽x̥͋t͍̑̔͝";
|
||
let encoded = "U8yCzI/MpsyXzZlvzZfMjM2KzLLMssyXbcyKzJPMgc2CzJ1lzYTMjM2EzIDMpsyhzJ7MpCDMjsy/zYXMpcydzKpmzZfNhMyNzIbMqsy7zKfNiXXNjM2EzK7Mnc2Fbs2EzIrMucyea82YzILMrs2HzJ/NjnnMvsyVzIbNhcyyzKfMoCDMgsyQzJnNlsytzZR0zIHNmMyBzJ5lzL3Mos2VzKh4zYvMpXTMkcyUzZ3NjQ==";
|
||
|
||
let outcome = nu!("'{}' | encode new-base64", text);
|
||
assert_eq!(outcome.out, encoded);
|
||
}
|
||
|
||
#[test]
|
||
fn decode_string() {
|
||
let text = "Very important data";
|
||
let encoded = "VmVyeSBpbXBvcnRhbnQgZGF0YQ==";
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 | decode", encoded);
|
||
assert_eq!(outcome.out, text);
|
||
}
|
||
|
||
#[test]
|
||
fn decode_pad_nopad() {
|
||
let text = "”¥.ä@°bZö¢";
|
||
let encoded_pad = "4oCdwqUuw6RAwrBiWsO2wqI=";
|
||
let encoded_nopad = "4oCdwqUuw6RAwrBiWsO2wqI";
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 | decode", encoded_pad);
|
||
assert_eq!(outcome.out, text);
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 --nopad | decode", encoded_nopad);
|
||
assert_eq!(outcome.out, text);
|
||
}
|
||
|
||
#[test]
|
||
fn decode_url() {
|
||
let text = "p:gטݾ߫t+?";
|
||
let encoded = "cDpn15jdvt+rdCs/";
|
||
let encoded_url = "cDpn15jdvt-rdCs_";
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 | decode", encoded);
|
||
assert_eq!(outcome.out, text);
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 --url | decode", encoded_url);
|
||
assert_eq!(outcome.out, text);
|
||
}
|
||
|
||
#[test]
|
||
fn reject_pad_nopad() {
|
||
let encoded_nopad = "YQ";
|
||
let encoded_pad = "YQ==";
|
||
|
||
let outcome = nu!("'{}' | decode new-base64", encoded_nopad);
|
||
assert!(!outcome.err.is_empty());
|
||
|
||
let outcome = nu!("'{}' | decode new-base64 --nopad", encoded_pad);
|
||
assert!(!outcome.err.is_empty())
|
||
}
|