tor-netdoc: b64 tests: add exhaustive roundtrip test

This commit is contained in:
Ian Jackson 2022-07-05 15:45:03 +01:00
parent 372ff61552
commit 8f63c07b5f
3 changed files with 44 additions and 0 deletions

1
Cargo.lock generated
View File

@ -3830,6 +3830,7 @@ dependencies = [
"educe",
"hex",
"hex-literal",
"itertools",
"once_cell",
"phf",
"rand 0.8.5",

View File

@ -72,4 +72,5 @@ weak-table = "0.3.0"
[dev-dependencies]
hex-literal = "0.3"
itertools = "0.10.1"
serde_json = "1.0.50"

View File

@ -460,6 +460,9 @@ mod nickname {
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use itertools::Itertools;
use std::iter;
use base64ct::Encoding;
use super::*;
@ -533,6 +536,45 @@ mod test {
Ok(())
}
#[test]
fn base64_rev() {
use base64ct::{Base64, Base64Unpadded};
// Check that strings that we accept are precisely ones which
// can be generated by either Base64 or Base64Unpadded
for n in 0..=5 {
for c_vec in iter::repeat("ACEQg/=".chars())
.take(n)
.multi_cartesian_product()
{
let s: String = c_vec.into_iter().collect();
let b = match s.parse::<B64>() {
Ok(b) => {
eprintln!("{:10} {:?}", &s, b.as_bytes());
b
}
Err(_) => {
eprintln!("{:10} Err", &s);
continue;
}
};
let b = b.as_bytes();
let ep = Base64::encode_string(&b);
let eu = Base64Unpadded::encode_string(&b);
assert!(
&s == &ep || &s == &eu,
"{:?} decoded to {:?} giving neither {:?} nor {:?}",
s,
b,
ep,
eu
);
}
}
}
#[test]
fn base16() -> Result<()> {
assert_eq!("332e313432".parse::<B16>()?.as_bytes(), &b"3.142"[..]);