2024-08-23 18:18:51 +02:00
|
|
|
use data_encoding::HEXUPPER;
|
|
|
|
use rand::prelude::*;
|
|
|
|
use rand_chacha::ChaCha8Rng;
|
|
|
|
|
2024-08-24 16:02:02 +02:00
|
|
|
use nu_test_support::nu;
|
|
|
|
|
2024-08-23 18:18:51 +02:00
|
|
|
mod base32;
|
|
|
|
mod base32hex;
|
|
|
|
mod base64;
|
|
|
|
mod hex;
|
|
|
|
|
|
|
|
/// Generate a few random binaries.
|
2024-08-24 16:02:02 +02:00
|
|
|
fn random_bytes() -> Vec<String> {
|
2024-08-23 18:18:51 +02:00
|
|
|
const NUM: usize = 32;
|
|
|
|
let mut rng = ChaCha8Rng::seed_from_u64(4);
|
|
|
|
|
|
|
|
(0..NUM)
|
|
|
|
.map(|_| {
|
|
|
|
let length = rng.gen_range(0..512);
|
|
|
|
let mut bytes = vec![0u8; length];
|
|
|
|
rng.fill_bytes(&mut bytes);
|
2024-08-24 16:02:02 +02:00
|
|
|
HEXUPPER.encode(&bytes)
|
2024-08-23 18:18:51 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2024-08-24 16:02:02 +02:00
|
|
|
|
|
|
|
pub fn test_canonical(cmd: &str) {
|
|
|
|
for value in random_bytes() {
|
|
|
|
let outcome = nu!("0x[{}] | encode {1} | decode {1} | to nuon", value, cmd);
|
|
|
|
let nuon_value = format!("0x[{value}]");
|
|
|
|
assert_eq!(outcome.out, nuon_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_const(cmd: &str) {
|
|
|
|
for value in random_bytes() {
|
|
|
|
let outcome = nu!(
|
|
|
|
r#"const out = (0x[{}] | encode {1} | decode {1} | encode hex); $out"#,
|
|
|
|
value,
|
|
|
|
cmd
|
|
|
|
);
|
|
|
|
assert_eq!(outcome.out, value);
|
|
|
|
}
|
|
|
|
}
|