nushell/crates/nu-command/tests/commands/base/base32hex.rs
Andrej Kolčin 0560826414
encode/decode for multiple alphabets (#13428)
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`.
2024-08-23 11:18:51 -05:00

59 lines
1.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use nu_test_support::nu;
#[test]
fn canonical() {
for value in super::random_bytes() {
let outcome = nu!("{} | encode base32hex | decode base32hex | to nuon", value);
assert_eq!(outcome.out, value);
let outcome = nu!(
"{} | encode base32hex --nopad | decode base32hex --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 = "AF685J4FPIJCP5UDJ5NSR5UCHJ6OLJ5IPIPCP5RDPI5CP4UCG76O5J4TCN6O9J4CPM2CP06CKR6A3J4UPII21J4EPIVSR1ECKN69RJ5ACR6PFJC4PI6SP1MCLB6BNJ57PM4NBJCCPM2CPBMCJN6OARMDGJ68LJ5PPIF6NJCOPI1CPBMDGV69VJCEF76BTJ4LPI3CR1ECMB6AFJ5043685J4GPICSR5MCLN6P8T6CG76PHJ41PIF6BJ5TPIHCR5ECL1SCR2UCKLQCP4ECIJ6PRJCD";
let outcome = nu!("'{}' | encode base32hex --nopad", text);
assert_eq!(outcome.out, encoded);
}
#[test]
fn decode_string() {
let text = "Very important data";
let encoded = "APIN4U90D5MN0RRIEHGMST10CHGN8O8=";
let outcome = nu!("'{}' | decode base32hex | decode", encoded);
assert_eq!(outcome.out, text);
}
#[test]
fn decode_pad_nopad() {
let text = "®lnnE¾ˆë";
let encoded_pad = "OAN6ORJE8N1BTIS6OELG====";
let encoded_nopad = "OAN6ORJE8N1BTIS6OELG";
let outcome = nu!("'{}' | decode base32hex | decode", encoded_pad);
assert_eq!(outcome.out, text);
let outcome = nu!("'{}' | decode base32hex --nopad | decode", encoded_nopad);
assert_eq!(outcome.out, text);
}
#[test]
fn reject_pad_nopad() {
let encoded_nopad = "D1KG";
let encoded_pad = "D1KG====";
let outcome = nu!("'{}' | decode base32hex", encoded_nopad);
assert!(!outcome.err.is_empty());
let outcome = nu!("'{}' | decode base32hex --nopad", encoded_pad);
assert!(!outcome.err.is_empty())
}