| 1 |
// rstest_reuse template functions have unused variables |
| 2 |
#![allow(unused_variables)] |
| 3 |
|
| 4 |
use rand::{ |
| 5 |
self, |
| 6 |
distributions::{self, Distribution as _}, |
| 7 |
rngs, Rng as _, SeedableRng as _, |
| 8 |
}; |
| 9 |
use rstest::rstest; |
| 10 |
use rstest_reuse::{apply, template}; |
| 11 |
use std::{collections, fmt, io::Read as _}; |
| 12 |
|
| 13 |
use crate::{ |
| 14 |
alphabet::{Alphabet, STANDARD}, |
| 15 |
encode::add_padding, |
| 16 |
encoded_len, |
| 17 |
engine::{ |
| 18 |
general_purpose, naive, Config, DecodeEstimate, DecodeMetadata, DecodePaddingMode, Engine, |
| 19 |
}, |
| 20 |
read::DecoderReader, |
| 21 |
tests::{assert_encode_sanity, random_alphabet, random_config}, |
| 22 |
DecodeError, PAD_BYTE, |
| 23 |
}; |
| 24 |
|
| 25 |
// the case::foo syntax includes the "foo" in the generated test method names |
| 26 |
#[template] |
| 27 |
#[rstest(engine_wrapper, |
| 28 |
case::general_purpose(GeneralPurposeWrapper {}), |
| 29 |
case::naive(NaiveWrapper {}), |
| 30 |
case::decoder_reader(DecoderReaderEngineWrapper {}), |
| 31 |
)] |
| 32 |
fn all_engines<E: EngineWrapper>(engine_wrapper: E) {} |
| 33 |
|
| 34 |
/// Some decode tests don't make sense for use with `DecoderReader` as they are difficult to |
| 35 |
/// reason about or otherwise inapplicable given how DecoderReader slice up its input along |
| 36 |
/// chunk boundaries. |
| 37 |
#[template] |
| 38 |
#[rstest(engine_wrapper, |
| 39 |
case::general_purpose(GeneralPurposeWrapper {}), |
| 40 |
case::naive(NaiveWrapper {}), |
| 41 |
)] |
| 42 |
fn all_engines_except_decoder_reader<E: EngineWrapper>(engine_wrapper: E) {} |
| 43 |
|
| 44 |
#[apply(all_engines)] |
| 45 |
fn rfc_test_vectors_std_alphabet<E: EngineWrapper>(engine_wrapper: E) { |
| 46 |
let data = vec![ |
| 47 |
("", ""), |
| 48 |
("f", "Zg=="), |
| 49 |
("fo", "Zm8="), |
| 50 |
("foo", "Zm9v"), |
| 51 |
("foob", "Zm9vYg=="), |
| 52 |
("fooba", "Zm9vYmE="), |
| 53 |
("foobar", "Zm9vYmFy"), |
| 54 |
]; |
| 55 |
|
| 56 |
let engine = E::standard(); |
| 57 |
let engine_no_padding = E::standard_unpadded(); |
| 58 |
|
| 59 |
for (orig, encoded) in &data { |
| 60 |
let encoded_without_padding = encoded.trim_end_matches('='); |
| 61 |
|
| 62 |
// unpadded |
| 63 |
{ |
| 64 |
let mut encode_buf = [0_u8; 8]; |
| 65 |
let mut decode_buf = [0_u8; 6]; |
| 66 |
|
| 67 |
let encode_len = |
| 68 |
engine_no_padding.internal_encode(orig.as_bytes(), &mut encode_buf[..]); |
| 69 |
assert_eq!( |
| 70 |
&encoded_without_padding, |
| 71 |
&std::str::from_utf8(&encode_buf[0..encode_len]).unwrap() |
| 72 |
); |
| 73 |
let decode_len = engine_no_padding |
| 74 |
.decode_slice_unchecked(encoded_without_padding.as_bytes(), &mut decode_buf[..]) |
| 75 |
.unwrap(); |
| 76 |
assert_eq!(orig.len(), decode_len); |
| 77 |
|
| 78 |
assert_eq!( |
| 79 |
orig, |
| 80 |
&std::str::from_utf8(&decode_buf[0..decode_len]).unwrap() |
| 81 |
); |
| 82 |
|
| 83 |
// if there was any padding originally, the no padding engine won't decode it |
| 84 |
if encoded.as_bytes().contains(&PAD_BYTE) { |
| 85 |
assert_eq!( |
| 86 |
Err(DecodeError::InvalidPadding), |
| 87 |
engine_no_padding.decode(encoded) |
| 88 |
) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// padded |
| 93 |
{ |
| 94 |
let mut encode_buf = [0_u8; 8]; |
| 95 |
let mut decode_buf = [0_u8; 6]; |
| 96 |
|
| 97 |
let encode_len = engine.internal_encode(orig.as_bytes(), &mut encode_buf[..]); |
| 98 |
assert_eq!( |
| 99 |
// doesn't have padding added yet |
| 100 |
&encoded_without_padding, |
| 101 |
&std::str::from_utf8(&encode_buf[0..encode_len]).unwrap() |
| 102 |
); |
| 103 |
let pad_len = add_padding(encode_len, &mut encode_buf[encode_len..]); |
| 104 |
assert_eq!(encoded.as_bytes(), &encode_buf[..encode_len + pad_len]); |
| 105 |
|
| 106 |
let decode_len = engine |
| 107 |
.decode_slice_unchecked(encoded.as_bytes(), &mut decode_buf[..]) |
| 108 |
.unwrap(); |
| 109 |
assert_eq!(orig.len(), decode_len); |
| 110 |
|
| 111 |
assert_eq!( |
| 112 |
orig, |
| 113 |
&std::str::from_utf8(&decode_buf[0..decode_len]).unwrap() |
| 114 |
); |
| 115 |
|
| 116 |
// if there was (canonical) padding, and we remove it, the standard engine won't decode |
| 117 |
if encoded.as_bytes().contains(&PAD_BYTE) { |
| 118 |
assert_eq!( |
| 119 |
Err(DecodeError::InvalidPadding), |
| 120 |
engine.decode(encoded_without_padding) |
| 121 |
) |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[apply(all_engines)] |
| 128 |
fn roundtrip_random<E: EngineWrapper>(engine_wrapper: E) { |
| 129 |
let mut rng = seeded_rng(); |
| 130 |
|
| 131 |
let mut orig_data = Vec::<u8>::new(); |
| 132 |
let mut encode_buf = Vec::<u8>::new(); |
| 133 |
let mut decode_buf = Vec::<u8>::new(); |
| 134 |
|
| 135 |
let len_range = distributions::Uniform::new(1, 1_000); |
| 136 |
|
| 137 |
for _ in 0..10_000 { |
| 138 |
let engine = E::random(&mut rng); |
| 139 |
|
| 140 |
orig_data.clear(); |
| 141 |
encode_buf.clear(); |
| 142 |
decode_buf.clear(); |
| 143 |
|
| 144 |
let (orig_len, _, encoded_len) = generate_random_encoded_data( |
| 145 |
&engine, |
| 146 |
&mut orig_data, |
| 147 |
&mut encode_buf, |
| 148 |
&mut rng, |
| 149 |
&len_range, |
| 150 |
); |
| 151 |
|
| 152 |
// exactly the right size |
| 153 |
decode_buf.resize(orig_len, 0); |
| 154 |
|
| 155 |
let dec_len = engine |
| 156 |
.decode_slice_unchecked(&encode_buf[0..encoded_len], &mut decode_buf[..]) |
| 157 |
.unwrap(); |
| 158 |
|
| 159 |
assert_eq!(orig_len, dec_len); |
| 160 |
assert_eq!(&orig_data[..], &decode_buf[..dec_len]); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
#[apply(all_engines)] |
| 165 |
fn encode_doesnt_write_extra_bytes<E: EngineWrapper>(engine_wrapper: E) { |
| 166 |
let mut rng = seeded_rng(); |
| 167 |
|
| 168 |
let mut orig_data = Vec::<u8>::new(); |
| 169 |
let mut encode_buf = Vec::<u8>::new(); |
| 170 |
let mut encode_buf_backup = Vec::<u8>::new(); |
| 171 |
|
| 172 |
let input_len_range = distributions::Uniform::new(0, 1000); |
| 173 |
|
| 174 |
for _ in 0..10_000 { |
| 175 |
let engine = E::random(&mut rng); |
| 176 |
let padded = engine.config().encode_padding(); |
| 177 |
|
| 178 |
orig_data.clear(); |
| 179 |
encode_buf.clear(); |
| 180 |
encode_buf_backup.clear(); |
| 181 |
|
| 182 |
let orig_len = fill_rand(&mut orig_data, &mut rng, &input_len_range); |
| 183 |
|
| 184 |
let prefix_len = 1024; |
| 185 |
// plenty of prefix and suffix |
| 186 |
fill_rand_len(&mut encode_buf, &mut rng, prefix_len * 2 + orig_len * 2); |
| 187 |
encode_buf_backup.extend_from_slice(&encode_buf[..]); |
| 188 |
|
| 189 |
let expected_encode_len_no_pad = encoded_len(orig_len, false).unwrap(); |
| 190 |
|
| 191 |
let encoded_len_no_pad = |
| 192 |
engine.internal_encode(&orig_data[..], &mut encode_buf[prefix_len..]); |
| 193 |
assert_eq!(expected_encode_len_no_pad, encoded_len_no_pad); |
| 194 |
|
| 195 |
// no writes past what it claimed to write |
| 196 |
assert_eq!(&encode_buf_backup[..prefix_len], &encode_buf[..prefix_len]); |
| 197 |
assert_eq!( |
| 198 |
&encode_buf_backup[(prefix_len + encoded_len_no_pad)..], |
| 199 |
&encode_buf[(prefix_len + encoded_len_no_pad)..] |
| 200 |
); |
| 201 |
|
| 202 |
let encoded_data = &encode_buf[prefix_len..(prefix_len + encoded_len_no_pad)]; |
| 203 |
assert_encode_sanity( |
| 204 |
std::str::from_utf8(encoded_data).unwrap(), |
| 205 |
// engines don't pad |
| 206 |
false, |
| 207 |
orig_len, |
| 208 |
); |
| 209 |
|
| 210 |
// pad so we can decode it in case our random engine requires padding |
| 211 |
let pad_len = if padded { |
| 212 |
add_padding( |
| 213 |
encoded_len_no_pad, |
| 214 |
&mut encode_buf[prefix_len + encoded_len_no_pad..], |
| 215 |
) |
| 216 |
} else { |
| 217 |
0 |
| 218 |
}; |
| 219 |
|
| 220 |
assert_eq!( |
| 221 |
orig_data, |
| 222 |
engine |
| 223 |
.decode(&encode_buf[prefix_len..(prefix_len + encoded_len_no_pad + pad_len)],) |
| 224 |
.unwrap() |
| 225 |
); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
#[apply(all_engines)] |
| 230 |
fn encode_engine_slice_fits_into_precisely_sized_slice<E: EngineWrapper>(engine_wrapper: E) { |
| 231 |
let mut orig_data = Vec::new(); |
| 232 |
let mut encoded_data = Vec::new(); |
| 233 |
let mut decoded = Vec::new(); |
| 234 |
|
| 235 |
let input_len_range = distributions::Uniform::new(0, 1000); |
| 236 |
|
| 237 |
let mut rng = rngs::SmallRng::from_entropy(); |
| 238 |
|
| 239 |
for _ in 0..10_000 { |
| 240 |
orig_data.clear(); |
| 241 |
encoded_data.clear(); |
| 242 |
decoded.clear(); |
| 243 |
|
| 244 |
let input_len = input_len_range.sample(&mut rng); |
| 245 |
|
| 246 |
for _ in 0..input_len { |
| 247 |
orig_data.push(rng.gen()); |
| 248 |
} |
| 249 |
|
| 250 |
let engine = E::random(&mut rng); |
| 251 |
|
| 252 |
let encoded_size = encoded_len(input_len, engine.config().encode_padding()).unwrap(); |
| 253 |
|
| 254 |
encoded_data.resize(encoded_size, 0); |
| 255 |
|
| 256 |
assert_eq!( |
| 257 |
encoded_size, |
| 258 |
engine.encode_slice(&orig_data, &mut encoded_data).unwrap() |
| 259 |
); |
| 260 |
|
| 261 |
assert_encode_sanity( |
| 262 |
std::str::from_utf8(&encoded_data[0..encoded_size]).unwrap(), |
| 263 |
engine.config().encode_padding(), |
| 264 |
input_len, |
| 265 |
); |
| 266 |
|
| 267 |
engine |
| 268 |
.decode_vec(&encoded_data[0..encoded_size], &mut decoded) |
| 269 |
.unwrap(); |
| 270 |
assert_eq!(orig_data, decoded); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
#[apply(all_engines)] |
| 275 |
fn decode_doesnt_write_extra_bytes<E>(engine_wrapper: E) |
| 276 |
where |
| 277 |
E: EngineWrapper, |
| 278 |
<<E as EngineWrapper>::Engine as Engine>::Config: fmt::Debug, |
| 279 |
{ |
| 280 |
let mut rng = seeded_rng(); |
| 281 |
|
| 282 |
let mut orig_data = Vec::<u8>::new(); |
| 283 |
let mut encode_buf = Vec::<u8>::new(); |
| 284 |
let mut decode_buf = Vec::<u8>::new(); |
| 285 |
let mut decode_buf_backup = Vec::<u8>::new(); |
| 286 |
|
| 287 |
let len_range = distributions::Uniform::new(1, 1_000); |
| 288 |
|
| 289 |
for _ in 0..10_000 { |
| 290 |
let engine = E::random(&mut rng); |
| 291 |
|
| 292 |
orig_data.clear(); |
| 293 |
encode_buf.clear(); |
| 294 |
decode_buf.clear(); |
| 295 |
decode_buf_backup.clear(); |
| 296 |
|
| 297 |
let orig_len = fill_rand(&mut orig_data, &mut rng, &len_range); |
| 298 |
encode_buf.resize(orig_len * 2 + 100, 0); |
| 299 |
|
| 300 |
let encoded_len = engine |
| 301 |
.encode_slice(&orig_data[..], &mut encode_buf[..]) |
| 302 |
.unwrap(); |
| 303 |
encode_buf.truncate(encoded_len); |
| 304 |
|
| 305 |
// oversize decode buffer so we can easily tell if it writes anything more than |
| 306 |
// just the decoded data |
| 307 |
let prefix_len = 1024; |
| 308 |
// plenty of prefix and suffix |
| 309 |
fill_rand_len(&mut decode_buf, &mut rng, prefix_len * 2 + orig_len * 2); |
| 310 |
decode_buf_backup.extend_from_slice(&decode_buf[..]); |
| 311 |
|
| 312 |
let dec_len = engine |
| 313 |
.decode_slice_unchecked(&encode_buf, &mut decode_buf[prefix_len..]) |
| 314 |
.unwrap(); |
| 315 |
|
| 316 |
assert_eq!(orig_len, dec_len); |
| 317 |
assert_eq!( |
| 318 |
&orig_data[..], |
| 319 |
&decode_buf[prefix_len..prefix_len + dec_len] |
| 320 |
); |
| 321 |
assert_eq!(&decode_buf_backup[..prefix_len], &decode_buf[..prefix_len]); |
| 322 |
assert_eq!( |
| 323 |
&decode_buf_backup[prefix_len + dec_len..], |
| 324 |
&decode_buf[prefix_len + dec_len..] |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
#[apply(all_engines)] |
| 330 |
fn decode_detect_invalid_last_symbol<E: EngineWrapper>(engine_wrapper: E) { |
| 331 |
// 0xFF -> "/w==", so all letters > w, 0-9, and '+', '/' should get InvalidLastSymbol |
| 332 |
let engine = E::standard(); |
| 333 |
|
| 334 |
assert_eq!(Ok(vec![0x89, 0x85]), engine.decode("iYU=")); |
| 335 |
assert_eq!(Ok(vec![0xFF]), engine.decode("/w==")); |
| 336 |
|
| 337 |
for (suffix, offset) in vec![ |
| 338 |
// suffix, offset of bad byte from start of suffix |
| 339 |
("/x==", 1_usize), |
| 340 |
("/z==", 1_usize), |
| 341 |
("/0==", 1_usize), |
| 342 |
("/9==", 1_usize), |
| 343 |
("/+==", 1_usize), |
| 344 |
("//==", 1_usize), |
| 345 |
// trailing 01 |
| 346 |
("iYV=", 2_usize), |
| 347 |
// trailing 10 |
| 348 |
("iYW=", 2_usize), |
| 349 |
// trailing 11 |
| 350 |
("iYX=", 2_usize), |
| 351 |
] { |
| 352 |
for prefix_quads in 0..256 { |
| 353 |
let mut encoded = "AAAA".repeat(prefix_quads); |
| 354 |
encoded.push_str(suffix); |
| 355 |
|
| 356 |
assert_eq!( |
| 357 |
Err(DecodeError::InvalidLastSymbol( |
| 358 |
encoded.len() - 4 + offset, |
| 359 |
suffix.as_bytes()[offset], |
| 360 |
)), |
| 361 |
engine.decode(encoded.as_str()) |
| 362 |
); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
#[apply(all_engines)] |
| 368 |
fn decode_detect_invalid_last_symbol_when_length_is_also_invalid<E: EngineWrapper>( |
| 369 |
engine_wrapper: E, |
| 370 |
) { |
| 371 |
let mut rng = seeded_rng(); |
| 372 |
|
| 373 |
// check across enough lengths that it would likely cover any implementation's various internal |
| 374 |
// small/large input division |
| 375 |
for len in (0_usize..256).map(|len| len * 4 + 1) { |
| 376 |
let engine = E::random_alphabet(&mut rng, &STANDARD); |
| 377 |
|
| 378 |
let mut input = vec![b'A'; len]; |
| 379 |
|
| 380 |
// with a valid last char, it's InvalidLength |
| 381 |
assert_eq!(Err(DecodeError::InvalidLength), engine.decode(&input)); |
| 382 |
// after mangling the last char, it's InvalidByte |
| 383 |
input[len - 1] = b'"'; |
| 384 |
assert_eq!( |
| 385 |
Err(DecodeError::InvalidByte(len - 1, b'"')), |
| 386 |
engine.decode(&input) |
| 387 |
); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
#[apply(all_engines)] |
| 392 |
fn decode_detect_invalid_last_symbol_every_possible_two_symbols<E: EngineWrapper>( |
| 393 |
engine_wrapper: E, |
| 394 |
) { |
| 395 |
let engine = E::standard(); |
| 396 |
|
| 397 |
let mut base64_to_bytes = collections::HashMap::new(); |
| 398 |
|
| 399 |
for b in 0_u8..=255 { |
| 400 |
let mut b64 = vec![0_u8; 4]; |
| 401 |
assert_eq!(2, engine.internal_encode(&[b], &mut b64[..])); |
| 402 |
let _ = add_padding(2, &mut b64[2..]); |
| 403 |
|
| 404 |
assert!(base64_to_bytes.insert(b64, vec![b]).is_none()); |
| 405 |
} |
| 406 |
|
| 407 |
// every possible combination of trailing symbols must either decode to 1 byte or get InvalidLastSymbol, with or without any leading chunks |
| 408 |
|
| 409 |
let mut prefix = Vec::new(); |
| 410 |
for _ in 0..256 { |
| 411 |
let mut clone = prefix.clone(); |
| 412 |
|
| 413 |
let mut symbols = [0_u8; 4]; |
| 414 |
for &s1 in STANDARD.symbols.iter() { |
| 415 |
symbols[0] = s1; |
| 416 |
for &s2 in STANDARD.symbols.iter() { |
| 417 |
symbols[1] = s2; |
| 418 |
symbols[2] = PAD_BYTE; |
| 419 |
symbols[3] = PAD_BYTE; |
| 420 |
|
| 421 |
// chop off previous symbols |
| 422 |
clone.truncate(prefix.len()); |
| 423 |
clone.extend_from_slice(&symbols[..]); |
| 424 |
let decoded_prefix_len = prefix.len() / 4 * 3; |
| 425 |
|
| 426 |
match base64_to_bytes.get(&symbols[..]) { |
| 427 |
Some(bytes) => { |
| 428 |
let res = engine |
| 429 |
.decode(&clone) |
| 430 |
// remove prefix |
| 431 |
.map(|decoded| decoded[decoded_prefix_len..].to_vec()); |
| 432 |
|
| 433 |
assert_eq!(Ok(bytes.clone()), res); |
| 434 |
} |
| 435 |
None => assert_eq!( |
| 436 |
Err(DecodeError::InvalidLastSymbol(1, s2)), |
| 437 |
engine.decode(&symbols[..]) |
| 438 |
), |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
prefix.extend_from_slice(b"AAAA"); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
#[apply(all_engines)] |
| 448 |
fn decode_detect_invalid_last_symbol_every_possible_three_symbols<E: EngineWrapper>( |
| 449 |
engine_wrapper: E, |
| 450 |
) { |
| 451 |
let engine = E::standard(); |
| 452 |
|
| 453 |
let mut base64_to_bytes = collections::HashMap::new(); |
| 454 |
|
| 455 |
let mut bytes = [0_u8; 2]; |
| 456 |
for b1 in 0_u8..=255 { |
| 457 |
bytes[0] = b1; |
| 458 |
for b2 in 0_u8..=255 { |
| 459 |
bytes[1] = b2; |
| 460 |
let mut b64 = vec![0_u8; 4]; |
| 461 |
assert_eq!(3, engine.internal_encode(&bytes, &mut b64[..])); |
| 462 |
let _ = add_padding(3, &mut b64[3..]); |
| 463 |
|
| 464 |
let mut v = Vec::with_capacity(2); |
| 465 |
v.extend_from_slice(&bytes[..]); |
| 466 |
|
| 467 |
assert!(base64_to_bytes.insert(b64, v).is_none()); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
// every possible combination of symbols must either decode to 2 bytes or get InvalidLastSymbol, with or without any leading chunks |
| 472 |
|
| 473 |
let mut prefix = Vec::new(); |
| 474 |
for _ in 0..256 { |
| 475 |
let mut input = prefix.clone(); |
| 476 |
|
| 477 |
let mut symbols = [0_u8; 4]; |
| 478 |
for &s1 in STANDARD.symbols.iter() { |
| 479 |
symbols[0] = s1; |
| 480 |
for &s2 in STANDARD.symbols.iter() { |
| 481 |
symbols[1] = s2; |
| 482 |
for &s3 in STANDARD.symbols.iter() { |
| 483 |
symbols[2] = s3; |
| 484 |
symbols[3] = PAD_BYTE; |
| 485 |
|
| 486 |
// chop off previous symbols |
| 487 |
input.truncate(prefix.len()); |
| 488 |
input.extend_from_slice(&symbols[..]); |
| 489 |
let decoded_prefix_len = prefix.len() / 4 * 3; |
| 490 |
|
| 491 |
match base64_to_bytes.get(&symbols[..]) { |
| 492 |
Some(bytes) => { |
| 493 |
let res = engine |
| 494 |
.decode(&input) |
| 495 |
// remove prefix |
| 496 |
.map(|decoded| decoded[decoded_prefix_len..].to_vec()); |
| 497 |
|
| 498 |
assert_eq!(Ok(bytes.clone()), res); |
| 499 |
} |
| 500 |
None => assert_eq!( |
| 501 |
Err(DecodeError::InvalidLastSymbol(2, s3)), |
| 502 |
engine.decode(&symbols[..]) |
| 503 |
), |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
prefix.extend_from_slice(b"AAAA"); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
#[apply(all_engines)] |
| 513 |
fn decode_invalid_trailing_bits_ignored_when_configured<E: EngineWrapper>(engine_wrapper: E) { |
| 514 |
let strict = E::standard(); |
| 515 |
let forgiving = E::standard_allow_trailing_bits(); |
| 516 |
|
| 517 |
fn assert_tolerant_decode<E: Engine>( |
| 518 |
engine: &E, |
| 519 |
input: &mut String, |
| 520 |
b64_prefix_len: usize, |
| 521 |
expected_decode_bytes: Vec<u8>, |
| 522 |
data: &str, |
| 523 |
) { |
| 524 |
let prefixed = prefixed_data(input, b64_prefix_len, data); |
| 525 |
let decoded = engine.decode(prefixed); |
| 526 |
// prefix is always complete chunks |
| 527 |
let decoded_prefix_len = b64_prefix_len / 4 * 3; |
| 528 |
assert_eq!( |
| 529 |
Ok(expected_decode_bytes), |
| 530 |
decoded.map(|v| v[decoded_prefix_len..].to_vec()) |
| 531 |
); |
| 532 |
} |
| 533 |
|
| 534 |
let mut prefix = String::new(); |
| 535 |
for _ in 0..256 { |
| 536 |
let mut input = prefix.clone(); |
| 537 |
|
| 538 |
// example from https://github.com/marshallpierce/rust-base64/issues/75 |
| 539 |
assert!(strict |
| 540 |
.decode(prefixed_data(&mut input, prefix.len(), "/w==")) |
| 541 |
.is_ok()); |
| 542 |
assert!(strict |
| 543 |
.decode(prefixed_data(&mut input, prefix.len(), "iYU=")) |
| 544 |
.is_ok()); |
| 545 |
// trailing 01 |
| 546 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![255], "/x=="); |
| 547 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![137, 133], "iYV="); |
| 548 |
// trailing 10 |
| 549 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![255], "/y=="); |
| 550 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![137, 133], "iYW="); |
| 551 |
// trailing 11 |
| 552 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![255], "/z=="); |
| 553 |
assert_tolerant_decode(&forgiving, &mut input, prefix.len(), vec![137, 133], "iYX="); |
| 554 |
|
| 555 |
prefix.push_str("AAAA"); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
#[apply(all_engines)] |
| 560 |
fn decode_invalid_byte_error<E: EngineWrapper>(engine_wrapper: E) { |
| 561 |
let mut rng = seeded_rng(); |
| 562 |
|
| 563 |
let mut orig_data = Vec::<u8>::new(); |
| 564 |
let mut encode_buf = Vec::<u8>::new(); |
| 565 |
let mut decode_buf = Vec::<u8>::new(); |
| 566 |
|
| 567 |
let len_range = distributions::Uniform::new(1, 1_000); |
| 568 |
|
| 569 |
for _ in 0..100_000 { |
| 570 |
let alphabet = random_alphabet(&mut rng); |
| 571 |
let engine = E::random_alphabet(&mut rng, alphabet); |
| 572 |
|
| 573 |
orig_data.clear(); |
| 574 |
encode_buf.clear(); |
| 575 |
decode_buf.clear(); |
| 576 |
|
| 577 |
let (orig_len, encoded_len_just_data, encoded_len_with_padding) = |
| 578 |
generate_random_encoded_data( |
| 579 |
&engine, |
| 580 |
&mut orig_data, |
| 581 |
&mut encode_buf, |
| 582 |
&mut rng, |
| 583 |
&len_range, |
| 584 |
); |
| 585 |
|
| 586 |
// exactly the right size |
| 587 |
decode_buf.resize(orig_len, 0); |
| 588 |
|
| 589 |
// replace one encoded byte with an invalid byte |
| 590 |
let invalid_byte: u8 = loop { |
| 591 |
let byte: u8 = rng.gen(); |
| 592 |
|
| 593 |
if alphabet.symbols.contains(&byte) || byte == PAD_BYTE { |
| 594 |
continue; |
| 595 |
} else { |
| 596 |
break byte; |
| 597 |
} |
| 598 |
}; |
| 599 |
|
| 600 |
let invalid_range = distributions::Uniform::new(0, orig_len); |
| 601 |
let invalid_index = invalid_range.sample(&mut rng); |
| 602 |
encode_buf[invalid_index] = invalid_byte; |
| 603 |
|
| 604 |
assert_eq!( |
| 605 |
Err(DecodeError::InvalidByte(invalid_index, invalid_byte)), |
| 606 |
engine.decode_slice_unchecked( |
| 607 |
&encode_buf[0..encoded_len_with_padding], |
| 608 |
&mut decode_buf[..], |
| 609 |
) |
| 610 |
); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
/// Any amount of padding anywhere before the final non padding character = invalid byte at first |
| 615 |
/// pad byte. |
| 616 |
/// From this, we know padding must extend to the end of the input. |
| 617 |
// DecoderReader pseudo-engine detects InvalidLastSymbol instead of InvalidLength because it |
| 618 |
// can end a decode on the quad that happens to contain the start of the padding |
| 619 |
#[apply(all_engines_except_decoder_reader)] |
| 620 |
fn decode_padding_before_final_non_padding_char_error_invalid_byte<E: EngineWrapper>( |
| 621 |
engine_wrapper: E, |
| 622 |
) { |
| 623 |
let mut rng = seeded_rng(); |
| 624 |
|
| 625 |
// the different amounts of proper padding, w/ offset from end for the last non-padding char |
| 626 |
let suffixes = [("/w==", 2), ("iYu=", 1), ("zzzz", 0)]; |
| 627 |
|
| 628 |
let prefix_quads_range = distributions::Uniform::from(0..=256); |
| 629 |
|
| 630 |
for mode in all_pad_modes() { |
| 631 |
// we don't encode so we don't care about encode padding |
| 632 |
let engine = E::standard_with_pad_mode(true, mode); |
| 633 |
|
| 634 |
for _ in 0..100_000 { |
| 635 |
for (suffix, offset) in suffixes.iter() { |
| 636 |
let mut s = "ABCD".repeat(prefix_quads_range.sample(&mut rng)); |
| 637 |
s.push_str(suffix); |
| 638 |
let mut encoded = s.into_bytes(); |
| 639 |
|
| 640 |
// calculate a range to write padding into that leaves at least one non padding char |
| 641 |
let last_non_padding_offset = encoded.len() - 1 - offset; |
| 642 |
|
| 643 |
// don't include last non padding char as it must stay not padding |
| 644 |
let padding_end = rng.gen_range(0..last_non_padding_offset); |
| 645 |
|
| 646 |
// don't use more than 100 bytes of padding, but also use shorter lengths when |
| 647 |
// padding_end is near the start of the encoded data to avoid biasing to padding |
| 648 |
// the entire prefix on short lengths |
| 649 |
let padding_len = rng.gen_range(1..=usize::min(100, padding_end + 1)); |
| 650 |
let padding_start = padding_end.saturating_sub(padding_len); |
| 651 |
|
| 652 |
encoded[padding_start..=padding_end].fill(PAD_BYTE); |
| 653 |
|
| 654 |
assert_eq!( |
| 655 |
Err(DecodeError::InvalidByte(padding_start, PAD_BYTE)), |
| 656 |
engine.decode(&encoded), |
| 657 |
); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
/// Any amount of padding before final chunk that crosses over into final chunk with 2-4 bytes = |
| 664 |
/// invalid byte at first pad byte. |
| 665 |
/// From this and [decode_padding_starts_before_final_chunk_error_invalid_length] we know the |
| 666 |
/// padding must start in the final chunk. |
| 667 |
// DecoderReader pseudo-engine detects InvalidLastSymbol instead of InvalidLength because it |
| 668 |
// can end a decode on the quad that happens to contain the start of the padding |
| 669 |
#[apply(all_engines_except_decoder_reader)] |
| 670 |
fn decode_padding_starts_before_final_chunk_error_invalid_byte<E: EngineWrapper>( |
| 671 |
engine_wrapper: E, |
| 672 |
) { |
| 673 |
let mut rng = seeded_rng(); |
| 674 |
|
| 675 |
// must have at least one prefix quad |
| 676 |
let prefix_quads_range = distributions::Uniform::from(1..256); |
| 677 |
// excluding 1 since we don't care about invalid length in this test |
| 678 |
let suffix_pad_len_range = distributions::Uniform::from(2..=4); |
| 679 |
for mode in all_pad_modes() { |
| 680 |
// we don't encode so we don't care about encode padding |
| 681 |
let engine = E::standard_with_pad_mode(true, mode); |
| 682 |
for _ in 0..100_000 { |
| 683 |
let suffix_len = suffix_pad_len_range.sample(&mut rng); |
| 684 |
let mut encoded = "ABCD" |
| 685 |
.repeat(prefix_quads_range.sample(&mut rng)) |
| 686 |
.into_bytes(); |
| 687 |
encoded.resize(encoded.len() + suffix_len, PAD_BYTE); |
| 688 |
|
| 689 |
// amount of padding must be long enough to extend back from suffix into previous |
| 690 |
// quads |
| 691 |
let padding_len = rng.gen_range(suffix_len + 1..encoded.len()); |
| 692 |
// no non-padding after padding in this test, so padding goes to the end |
| 693 |
let padding_start = encoded.len() - padding_len; |
| 694 |
encoded[padding_start..].fill(PAD_BYTE); |
| 695 |
|
| 696 |
assert_eq!( |
| 697 |
Err(DecodeError::InvalidByte(padding_start, PAD_BYTE)), |
| 698 |
engine.decode(&encoded), |
| 699 |
"suffix_len: {}, padding_len: {}, b64: {}", |
| 700 |
suffix_len, |
| 701 |
padding_len, |
| 702 |
std::str::from_utf8(&encoded).unwrap() |
| 703 |
); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
/// Any amount of padding before final chunk that crosses over into final chunk with 1 byte = |
| 709 |
/// invalid length. |
| 710 |
/// From this we know the padding must start in the final chunk. |
| 711 |
// DecoderReader pseudo-engine detects InvalidByte instead of InvalidLength because it starts by |
| 712 |
// decoding only the available complete quads |
| 713 |
#[apply(all_engines_except_decoder_reader)] |
| 714 |
fn decode_padding_starts_before_final_chunk_error_invalid_length<E: EngineWrapper>( |
| 715 |
engine_wrapper: E, |
| 716 |
) { |
| 717 |
let mut rng = seeded_rng(); |
| 718 |
|
| 719 |
// must have at least one prefix quad |
| 720 |
let prefix_quads_range = distributions::Uniform::from(1..256); |
| 721 |
for mode in all_pad_modes() { |
| 722 |
// we don't encode so we don't care about encode padding |
| 723 |
let engine = E::standard_with_pad_mode(true, mode); |
| 724 |
for _ in 0..100_000 { |
| 725 |
let mut encoded = "ABCD" |
| 726 |
.repeat(prefix_quads_range.sample(&mut rng)) |
| 727 |
.into_bytes(); |
| 728 |
encoded.resize(encoded.len() + 1, PAD_BYTE); |
| 729 |
|
| 730 |
// amount of padding must be long enough to extend back from suffix into previous |
| 731 |
// quads |
| 732 |
let padding_len = rng.gen_range(1 + 1..encoded.len()); |
| 733 |
// no non-padding after padding in this test, so padding goes to the end |
| 734 |
let padding_start = encoded.len() - padding_len; |
| 735 |
encoded[padding_start..].fill(PAD_BYTE); |
| 736 |
|
| 737 |
assert_eq!(Err(DecodeError::InvalidLength), engine.decode(&encoded),); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
/// 0-1 bytes of data before any amount of padding in final chunk = invalid byte, since padding |
| 743 |
/// is not valid data (consistent with error for pad bytes in earlier chunks). |
| 744 |
/// From this we know there must be 2-3 bytes of data before padding |
| 745 |
#[apply(all_engines)] |
| 746 |
fn decode_too_little_data_before_padding_error_invalid_byte<E: EngineWrapper>(engine_wrapper: E) { |
| 747 |
let mut rng = seeded_rng(); |
| 748 |
|
| 749 |
// want to test no prefix quad case, so start at 0 |
| 750 |
let prefix_quads_range = distributions::Uniform::from(0_usize..256); |
| 751 |
let suffix_data_len_range = distributions::Uniform::from(0_usize..=1); |
| 752 |
for mode in all_pad_modes() { |
| 753 |
// we don't encode so we don't care about encode padding |
| 754 |
let engine = E::standard_with_pad_mode(true, mode); |
| 755 |
for _ in 0..100_000 { |
| 756 |
let suffix_data_len = suffix_data_len_range.sample(&mut rng); |
| 757 |
let prefix_quad_len = prefix_quads_range.sample(&mut rng); |
| 758 |
|
| 759 |
// ensure there is a suffix quad |
| 760 |
let min_padding = usize::from(suffix_data_len == 0); |
| 761 |
|
| 762 |
// for all possible padding lengths |
| 763 |
for padding_len in min_padding..=(4 - suffix_data_len) { |
| 764 |
let mut encoded = "ABCD".repeat(prefix_quad_len).into_bytes(); |
| 765 |
encoded.resize(encoded.len() + suffix_data_len, b'A'); |
| 766 |
encoded.resize(encoded.len() + padding_len, PAD_BYTE); |
| 767 |
|
| 768 |
if suffix_data_len + padding_len == 1 { |
| 769 |
assert_eq!(Err(DecodeError::InvalidLength), engine.decode(&encoded),); |
| 770 |
} else { |
| 771 |
assert_eq!( |
| 772 |
Err(DecodeError::InvalidByte( |
| 773 |
prefix_quad_len * 4 + suffix_data_len, |
| 774 |
PAD_BYTE, |
| 775 |
)), |
| 776 |
engine.decode(&encoded), |
| 777 |
"suffix data len {} pad len {}", |
| 778 |
suffix_data_len, |
| 779 |
padding_len |
| 780 |
); |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 1 |
| 788 |
#[apply(all_engines)] |
| 789 |
fn decode_malleability_test_case_3_byte_suffix_valid<E: EngineWrapper>(engine_wrapper: E) { |
| 790 |
assert_eq!( |
| 791 |
b"Hello".as_slice(), |
| 792 |
&E::standard().decode("SGVsbG8=").unwrap() |
| 793 |
); |
| 794 |
} |
| 795 |
|
| 796 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 2 |
| 797 |
#[apply(all_engines)] |
| 798 |
fn decode_malleability_test_case_3_byte_suffix_invalid_trailing_symbol<E: EngineWrapper>( |
| 799 |
engine_wrapper: E, |
| 800 |
) { |
| 801 |
assert_eq!( |
| 802 |
DecodeError::InvalidLastSymbol(6, 0x39), |
| 803 |
E::standard().decode("SGVsbG9=").unwrap_err() |
| 804 |
); |
| 805 |
} |
| 806 |
|
| 807 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 3 |
| 808 |
#[apply(all_engines)] |
| 809 |
fn decode_malleability_test_case_3_byte_suffix_no_padding<E: EngineWrapper>(engine_wrapper: E) { |
| 810 |
assert_eq!( |
| 811 |
DecodeError::InvalidPadding, |
| 812 |
E::standard().decode("SGVsbG9").unwrap_err() |
| 813 |
); |
| 814 |
} |
| 815 |
|
| 816 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 4 |
| 817 |
#[apply(all_engines)] |
| 818 |
fn decode_malleability_test_case_2_byte_suffix_valid_two_padding_symbols<E: EngineWrapper>( |
| 819 |
engine_wrapper: E, |
| 820 |
) { |
| 821 |
assert_eq!( |
| 822 |
b"Hell".as_slice(), |
| 823 |
&E::standard().decode("SGVsbA==").unwrap() |
| 824 |
); |
| 825 |
} |
| 826 |
|
| 827 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 5 |
| 828 |
#[apply(all_engines)] |
| 829 |
fn decode_malleability_test_case_2_byte_suffix_short_padding<E: EngineWrapper>(engine_wrapper: E) { |
| 830 |
assert_eq!( |
| 831 |
DecodeError::InvalidPadding, |
| 832 |
E::standard().decode("SGVsbA=").unwrap_err() |
| 833 |
); |
| 834 |
} |
| 835 |
|
| 836 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 6 |
| 837 |
#[apply(all_engines)] |
| 838 |
fn decode_malleability_test_case_2_byte_suffix_no_padding<E: EngineWrapper>(engine_wrapper: E) { |
| 839 |
assert_eq!( |
| 840 |
DecodeError::InvalidPadding, |
| 841 |
E::standard().decode("SGVsbA").unwrap_err() |
| 842 |
); |
| 843 |
} |
| 844 |
|
| 845 |
// https://eprint.iacr.org/2022/361.pdf table 2, test 7 |
| 846 |
// DecoderReader pseudo-engine gets InvalidByte at 8 (extra padding) since it decodes the first |
| 847 |
// two complete quads correctly. |
| 848 |
#[apply(all_engines_except_decoder_reader)] |
| 849 |
fn decode_malleability_test_case_2_byte_suffix_too_much_padding<E: EngineWrapper>( |
| 850 |
engine_wrapper: E, |
| 851 |
) { |
| 852 |
assert_eq!( |
| 853 |
DecodeError::InvalidByte(6, PAD_BYTE), |
| 854 |
E::standard().decode("SGVsbA====").unwrap_err() |
| 855 |
); |
| 856 |
} |
| 857 |
|
| 858 |
/// Requires canonical padding -> accepts 2 + 2, 3 + 1, 4 + 0 final quad configurations |
| 859 |
#[apply(all_engines)] |
| 860 |
fn decode_pad_mode_requires_canonical_accepts_canonical<E: EngineWrapper>(engine_wrapper: E) { |
| 861 |
assert_all_suffixes_ok( |
| 862 |
E::standard_with_pad_mode(true, DecodePaddingMode::RequireCanonical), |
| 863 |
vec!["/w==", "iYU=", "AAAA"], |
| 864 |
); |
| 865 |
} |
| 866 |
|
| 867 |
/// Requires canonical padding -> rejects 2 + 0-1, 3 + 0 final chunk configurations |
| 868 |
#[apply(all_engines)] |
| 869 |
fn decode_pad_mode_requires_canonical_rejects_non_canonical<E: EngineWrapper>(engine_wrapper: E) { |
| 870 |
let engine = E::standard_with_pad_mode(true, DecodePaddingMode::RequireCanonical); |
| 871 |
|
| 872 |
let suffixes = ["/w", "/w=", "iYU"]; |
| 873 |
for num_prefix_quads in 0..256 { |
| 874 |
for &suffix in suffixes.iter() { |
| 875 |
let mut encoded = "AAAA".repeat(num_prefix_quads); |
| 876 |
encoded.push_str(suffix); |
| 877 |
|
| 878 |
let res = engine.decode(&encoded); |
| 879 |
|
| 880 |
assert_eq!(Err(DecodeError::InvalidPadding), res); |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
/// Requires no padding -> accepts 2 + 0, 3 + 0, 4 + 0 final chunk configuration |
| 886 |
#[apply(all_engines)] |
| 887 |
fn decode_pad_mode_requires_no_padding_accepts_no_padding<E: EngineWrapper>(engine_wrapper: E) { |
| 888 |
assert_all_suffixes_ok( |
| 889 |
E::standard_with_pad_mode(true, DecodePaddingMode::RequireNone), |
| 890 |
vec!["/w", "iYU", "AAAA"], |
| 891 |
); |
| 892 |
} |
| 893 |
|
| 894 |
/// Requires no padding -> rejects 2 + 1-2, 3 + 1 final chunk configuration |
| 895 |
#[apply(all_engines)] |
| 896 |
fn decode_pad_mode_requires_no_padding_rejects_any_padding<E: EngineWrapper>(engine_wrapper: E) { |
| 897 |
let engine = E::standard_with_pad_mode(true, DecodePaddingMode::RequireNone); |
| 898 |
|
| 899 |
let suffixes = ["/w=", "/w==", "iYU="]; |
| 900 |
for num_prefix_quads in 0..256 { |
| 901 |
for &suffix in suffixes.iter() { |
| 902 |
let mut encoded = "AAAA".repeat(num_prefix_quads); |
| 903 |
encoded.push_str(suffix); |
| 904 |
|
| 905 |
let res = engine.decode(&encoded); |
| 906 |
|
| 907 |
assert_eq!(Err(DecodeError::InvalidPadding), res); |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
/// Indifferent padding accepts 2 + 0-2, 3 + 0-1, 4 + 0 final chunk configuration |
| 913 |
#[apply(all_engines)] |
| 914 |
fn decode_pad_mode_indifferent_padding_accepts_anything<E: EngineWrapper>(engine_wrapper: E) { |
| 915 |
assert_all_suffixes_ok( |
| 916 |
E::standard_with_pad_mode(true, DecodePaddingMode::Indifferent), |
| 917 |
vec!["/w", "/w=", "/w==", "iYU", "iYU=", "AAAA"], |
| 918 |
); |
| 919 |
} |
| 920 |
|
| 921 |
//this is a MAY in the rfc: https://tools.ietf.org/html/rfc4648#section-3.3 |
| 922 |
// DecoderReader pseudo-engine finds the first padding, but doesn't report it as an error, |
| 923 |
// because in the next decode it finds more padding, which is reported as InvalidByte, just |
| 924 |
// with an offset at its position in the second decode, rather than being linked to the start |
| 925 |
// of the padding that was first seen in the previous decode. |
| 926 |
#[apply(all_engines_except_decoder_reader)] |
| 927 |
fn decode_pad_byte_in_penultimate_quad_error<E: EngineWrapper>(engine_wrapper: E) { |
| 928 |
for mode in all_pad_modes() { |
| 929 |
// we don't encode so we don't care about encode padding |
| 930 |
let engine = E::standard_with_pad_mode(true, mode); |
| 931 |
|
| 932 |
for num_prefix_quads in 0..256 { |
| 933 |
// leave room for at least one pad byte in penultimate quad |
| 934 |
for num_valid_bytes_penultimate_quad in 0..4 { |
| 935 |
// can't have 1 or it would be invalid length |
| 936 |
for num_pad_bytes_in_final_quad in 2..=4 { |
| 937 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 938 |
|
| 939 |
// varying amounts of padding in the penultimate quad |
| 940 |
for _ in 0..num_valid_bytes_penultimate_quad { |
| 941 |
s.push('A'); |
| 942 |
} |
| 943 |
// finish penultimate quad with padding |
| 944 |
for _ in num_valid_bytes_penultimate_quad..4 { |
| 945 |
s.push('='); |
| 946 |
} |
| 947 |
// and more padding in the final quad |
| 948 |
for _ in 0..num_pad_bytes_in_final_quad { |
| 949 |
s.push('='); |
| 950 |
} |
| 951 |
|
| 952 |
// padding should be an invalid byte before the final quad. |
| 953 |
// Could argue that the *next* padding byte (in the next quad) is technically the first |
| 954 |
// erroneous one, but reporting that accurately is more complex and probably nobody cares |
| 955 |
assert_eq!( |
| 956 |
DecodeError::InvalidByte( |
| 957 |
num_prefix_quads * 4 + num_valid_bytes_penultimate_quad, |
| 958 |
b'=', |
| 959 |
), |
| 960 |
engine.decode(&s).unwrap_err(), |
| 961 |
); |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
#[apply(all_engines)] |
| 969 |
fn decode_bytes_after_padding_in_final_quad_error<E: EngineWrapper>(engine_wrapper: E) { |
| 970 |
for mode in all_pad_modes() { |
| 971 |
// we don't encode so we don't care about encode padding |
| 972 |
let engine = E::standard_with_pad_mode(true, mode); |
| 973 |
|
| 974 |
for num_prefix_quads in 0..256 { |
| 975 |
// leave at least one byte in the quad for padding |
| 976 |
for bytes_after_padding in 1..4 { |
| 977 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 978 |
|
| 979 |
// every invalid padding position with a 3-byte final quad: 1 to 3 bytes after padding |
| 980 |
for _ in 0..(3 - bytes_after_padding) { |
| 981 |
s.push('A'); |
| 982 |
} |
| 983 |
s.push('='); |
| 984 |
for _ in 0..bytes_after_padding { |
| 985 |
s.push('A'); |
| 986 |
} |
| 987 |
|
| 988 |
// First (and only) padding byte is invalid. |
| 989 |
assert_eq!( |
| 990 |
DecodeError::InvalidByte( |
| 991 |
num_prefix_quads * 4 + (3 - bytes_after_padding), |
| 992 |
b'=' |
| 993 |
), |
| 994 |
engine.decode(&s).unwrap_err() |
| 995 |
); |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
#[apply(all_engines)] |
| 1002 |
fn decode_absurd_pad_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1003 |
for mode in all_pad_modes() { |
| 1004 |
// we don't encode so we don't care about encode padding |
| 1005 |
let engine = E::standard_with_pad_mode(true, mode); |
| 1006 |
|
| 1007 |
for num_prefix_quads in 0..256 { |
| 1008 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1009 |
s.push_str("==Y=Wx===pY=2U====="); |
| 1010 |
|
| 1011 |
// first padding byte |
| 1012 |
assert_eq!( |
| 1013 |
DecodeError::InvalidByte(num_prefix_quads * 4, b'='), |
| 1014 |
engine.decode(&s).unwrap_err() |
| 1015 |
); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
// DecoderReader pseudo-engine detects InvalidByte instead of InvalidLength because it starts by |
| 1021 |
// decoding only the available complete quads |
| 1022 |
#[apply(all_engines_except_decoder_reader)] |
| 1023 |
fn decode_too_much_padding_returns_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1024 |
for mode in all_pad_modes() { |
| 1025 |
// we don't encode so we don't care about encode padding |
| 1026 |
let engine = E::standard_with_pad_mode(true, mode); |
| 1027 |
|
| 1028 |
for num_prefix_quads in 0..256 { |
| 1029 |
// add enough padding to ensure that we'll hit all decode stages at the different lengths |
| 1030 |
for pad_bytes in 1..=64 { |
| 1031 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1032 |
let padding: String = "=".repeat(pad_bytes); |
| 1033 |
s.push_str(&padding); |
| 1034 |
|
| 1035 |
if pad_bytes % 4 == 1 { |
| 1036 |
assert_eq!(DecodeError::InvalidLength, engine.decode(&s).unwrap_err()); |
| 1037 |
} else { |
| 1038 |
assert_eq!( |
| 1039 |
DecodeError::InvalidByte(num_prefix_quads * 4, b'='), |
| 1040 |
engine.decode(&s).unwrap_err() |
| 1041 |
); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
// DecoderReader pseudo-engine detects InvalidByte instead of InvalidLength because it starts by |
| 1049 |
// decoding only the available complete quads |
| 1050 |
#[apply(all_engines_except_decoder_reader)] |
| 1051 |
fn decode_padding_followed_by_non_padding_returns_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1052 |
for mode in all_pad_modes() { |
| 1053 |
// we don't encode so we don't care about encode padding |
| 1054 |
let engine = E::standard_with_pad_mode(true, mode); |
| 1055 |
|
| 1056 |
for num_prefix_quads in 0..256 { |
| 1057 |
for pad_bytes in 0..=32 { |
| 1058 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1059 |
let padding: String = "=".repeat(pad_bytes); |
| 1060 |
s.push_str(&padding); |
| 1061 |
s.push('E'); |
| 1062 |
|
| 1063 |
if pad_bytes % 4 == 0 { |
| 1064 |
assert_eq!(DecodeError::InvalidLength, engine.decode(&s).unwrap_err()); |
| 1065 |
} else { |
| 1066 |
assert_eq!( |
| 1067 |
DecodeError::InvalidByte(num_prefix_quads * 4, b'='), |
| 1068 |
engine.decode(&s).unwrap_err() |
| 1069 |
); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
#[apply(all_engines)] |
| 1077 |
fn decode_one_char_in_final_quad_with_padding_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1078 |
for mode in all_pad_modes() { |
| 1079 |
// we don't encode so we don't care about encode padding |
| 1080 |
let engine = E::standard_with_pad_mode(true, mode); |
| 1081 |
|
| 1082 |
for num_prefix_quads in 0..256 { |
| 1083 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1084 |
s.push_str("E="); |
| 1085 |
|
| 1086 |
assert_eq!( |
| 1087 |
DecodeError::InvalidByte(num_prefix_quads * 4 + 1, b'='), |
| 1088 |
engine.decode(&s).unwrap_err() |
| 1089 |
); |
| 1090 |
|
| 1091 |
// more padding doesn't change the error |
| 1092 |
s.push('='); |
| 1093 |
assert_eq!( |
| 1094 |
DecodeError::InvalidByte(num_prefix_quads * 4 + 1, b'='), |
| 1095 |
engine.decode(&s).unwrap_err() |
| 1096 |
); |
| 1097 |
|
| 1098 |
s.push('='); |
| 1099 |
assert_eq!( |
| 1100 |
DecodeError::InvalidByte(num_prefix_quads * 4 + 1, b'='), |
| 1101 |
engine.decode(&s).unwrap_err() |
| 1102 |
); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
#[apply(all_engines)] |
| 1108 |
fn decode_too_few_symbols_in_final_quad_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1109 |
for mode in all_pad_modes() { |
| 1110 |
// we don't encode so we don't care about encode padding |
| 1111 |
let engine = E::standard_with_pad_mode(true, mode); |
| 1112 |
|
| 1113 |
for num_prefix_quads in 0..256 { |
| 1114 |
// <2 is invalid |
| 1115 |
for final_quad_symbols in 0..2 { |
| 1116 |
for padding_symbols in 0..=(4 - final_quad_symbols) { |
| 1117 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1118 |
|
| 1119 |
for _ in 0..final_quad_symbols { |
| 1120 |
s.push('A'); |
| 1121 |
} |
| 1122 |
for _ in 0..padding_symbols { |
| 1123 |
s.push('='); |
| 1124 |
} |
| 1125 |
|
| 1126 |
match final_quad_symbols + padding_symbols { |
| 1127 |
0 => continue, |
| 1128 |
1 => { |
| 1129 |
assert_eq!(DecodeError::InvalidLength, engine.decode(&s).unwrap_err()); |
| 1130 |
} |
| 1131 |
_ => { |
| 1132 |
// error reported at first padding byte |
| 1133 |
assert_eq!( |
| 1134 |
DecodeError::InvalidByte( |
| 1135 |
num_prefix_quads * 4 + final_quad_symbols, |
| 1136 |
b'=', |
| 1137 |
), |
| 1138 |
engine.decode(&s).unwrap_err() |
| 1139 |
); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
// DecoderReader pseudo-engine can't handle DecodePaddingMode::RequireNone since it will decode |
| 1149 |
// a complete quad with padding in it before encountering the stray byte that makes it an invalid |
| 1150 |
// length |
| 1151 |
#[apply(all_engines_except_decoder_reader)] |
| 1152 |
fn decode_invalid_trailing_bytes<E: EngineWrapper>(engine_wrapper: E) { |
| 1153 |
for mode in all_pad_modes() { |
| 1154 |
do_invalid_trailing_byte(E::standard_with_pad_mode(true, mode), mode); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
#[apply(all_engines)] |
| 1159 |
fn decode_invalid_trailing_bytes_all_modes<E: EngineWrapper>(engine_wrapper: E) { |
| 1160 |
// excluding no padding mode because the DecoderWrapper pseudo-engine will fail with |
| 1161 |
// InvalidPadding because it will decode the last complete quad with padding first |
| 1162 |
for mode in pad_modes_allowing_padding() { |
| 1163 |
do_invalid_trailing_byte(E::standard_with_pad_mode(true, mode), mode); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
#[apply(all_engines)] |
| 1168 |
fn decode_invalid_trailing_padding_as_invalid_length<E: EngineWrapper>(engine_wrapper: E) { |
| 1169 |
// excluding no padding mode because the DecoderWrapper pseudo-engine will fail with |
| 1170 |
// InvalidPadding because it will decode the last complete quad with padding first |
| 1171 |
for mode in pad_modes_allowing_padding() { |
| 1172 |
do_invalid_trailing_padding_as_invalid_length(E::standard_with_pad_mode(true, mode), mode); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
// DecoderReader pseudo-engine can't handle DecodePaddingMode::RequireNone since it will decode |
| 1177 |
// a complete quad with padding in it before encountering the stray byte that makes it an invalid |
| 1178 |
// length |
| 1179 |
#[apply(all_engines_except_decoder_reader)] |
| 1180 |
fn decode_invalid_trailing_padding_as_invalid_length_all_modes<E: EngineWrapper>( |
| 1181 |
engine_wrapper: E, |
| 1182 |
) { |
| 1183 |
for mode in all_pad_modes() { |
| 1184 |
do_invalid_trailing_padding_as_invalid_length(E::standard_with_pad_mode(true, mode), mode); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
#[apply(all_engines)] |
| 1189 |
fn decode_wrong_length_error<E: EngineWrapper>(engine_wrapper: E) { |
| 1190 |
let engine = E::standard_with_pad_mode(true, DecodePaddingMode::Indifferent); |
| 1191 |
|
| 1192 |
for num_prefix_quads in 0..256 { |
| 1193 |
// at least one token, otherwise it wouldn't be a final quad |
| 1194 |
for num_tokens_final_quad in 1..=4 { |
| 1195 |
for num_padding in 0..=(4 - num_tokens_final_quad) { |
| 1196 |
let mut s: String = "IIII".repeat(num_prefix_quads); |
| 1197 |
for _ in 0..num_tokens_final_quad { |
| 1198 |
s.push('g'); |
| 1199 |
} |
| 1200 |
for _ in 0..num_padding { |
| 1201 |
s.push('='); |
| 1202 |
} |
| 1203 |
|
| 1204 |
let res = engine.decode(&s); |
| 1205 |
if num_tokens_final_quad >= 2 { |
| 1206 |
assert!(res.is_ok()); |
| 1207 |
} else if num_tokens_final_quad == 1 && num_padding > 0 { |
| 1208 |
// = is invalid if it's too early |
| 1209 |
assert_eq!( |
| 1210 |
Err(DecodeError::InvalidByte( |
| 1211 |
num_prefix_quads * 4 + num_tokens_final_quad, |
| 1212 |
61 |
| 1213 |
)), |
| 1214 |
res |
| 1215 |
); |
| 1216 |
} else if num_padding > 2 { |
| 1217 |
assert_eq!(Err(DecodeError::InvalidPadding), res); |
| 1218 |
} else { |
| 1219 |
assert_eq!(Err(DecodeError::InvalidLength), res); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
#[apply(all_engines)] |
| 1227 |
fn decode_into_slice_fits_in_precisely_sized_slice<E: EngineWrapper>(engine_wrapper: E) { |
| 1228 |
let mut orig_data = Vec::new(); |
| 1229 |
let mut encoded_data = String::new(); |
| 1230 |
let mut decode_buf = Vec::new(); |
| 1231 |
|
| 1232 |
let input_len_range = distributions::Uniform::new(0, 1000); |
| 1233 |
let mut rng = rngs::SmallRng::from_entropy(); |
| 1234 |
|
| 1235 |
for _ in 0..10_000 { |
| 1236 |
orig_data.clear(); |
| 1237 |
encoded_data.clear(); |
| 1238 |
decode_buf.clear(); |
| 1239 |
|
| 1240 |
let input_len = input_len_range.sample(&mut rng); |
| 1241 |
|
| 1242 |
for _ in 0..input_len { |
| 1243 |
orig_data.push(rng.gen()); |
| 1244 |
} |
| 1245 |
|
| 1246 |
let engine = E::random(&mut rng); |
| 1247 |
engine.encode_string(&orig_data, &mut encoded_data); |
| 1248 |
assert_encode_sanity(&encoded_data, engine.config().encode_padding(), input_len); |
| 1249 |
|
| 1250 |
decode_buf.resize(input_len, 0); |
| 1251 |
|
| 1252 |
// decode into the non-empty buf |
| 1253 |
let decode_bytes_written = engine |
| 1254 |
.decode_slice_unchecked(encoded_data.as_bytes(), &mut decode_buf[..]) |
| 1255 |
.unwrap(); |
| 1256 |
|
| 1257 |
assert_eq!(orig_data.len(), decode_bytes_written); |
| 1258 |
assert_eq!(orig_data, decode_buf); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
#[apply(all_engines)] |
| 1263 |
fn inner_decode_reports_padding_position<E: EngineWrapper>(engine_wrapper: E) { |
| 1264 |
let mut b64 = String::new(); |
| 1265 |
let mut decoded = Vec::new(); |
| 1266 |
let engine = E::standard(); |
| 1267 |
|
| 1268 |
for pad_position in 1..10_000 { |
| 1269 |
b64.clear(); |
| 1270 |
decoded.clear(); |
| 1271 |
// plenty of room for original data |
| 1272 |
decoded.resize(pad_position, 0); |
| 1273 |
|
| 1274 |
for _ in 0..pad_position { |
| 1275 |
b64.push('A'); |
| 1276 |
} |
| 1277 |
// finish the quad with padding |
| 1278 |
for _ in 0..(4 - (pad_position % 4)) { |
| 1279 |
b64.push('='); |
| 1280 |
} |
| 1281 |
|
| 1282 |
let decode_res = engine.internal_decode( |
| 1283 |
b64.as_bytes(), |
| 1284 |
&mut decoded[..], |
| 1285 |
engine.internal_decoded_len_estimate(b64.len()), |
| 1286 |
); |
| 1287 |
if pad_position % 4 < 2 { |
| 1288 |
// impossible padding |
| 1289 |
assert_eq!( |
| 1290 |
Err(DecodeError::InvalidByte(pad_position, PAD_BYTE)), |
| 1291 |
decode_res |
| 1292 |
); |
| 1293 |
} else { |
| 1294 |
let decoded_bytes = pad_position / 4 * 3 |
| 1295 |
+ match pad_position % 4 { |
| 1296 |
0 => 0, |
| 1297 |
2 => 1, |
| 1298 |
3 => 2, |
| 1299 |
_ => unreachable!(), |
| 1300 |
}; |
| 1301 |
assert_eq!( |
| 1302 |
Ok(DecodeMetadata::new(decoded_bytes, Some(pad_position))), |
| 1303 |
decode_res |
| 1304 |
); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
#[apply(all_engines)] |
| 1310 |
fn decode_length_estimate_delta<E: EngineWrapper>(engine_wrapper: E) { |
| 1311 |
for engine in [E::standard(), E::standard_unpadded()] { |
| 1312 |
for &padding in &[true, false] { |
| 1313 |
for orig_len in 0..1000 { |
| 1314 |
let encoded_len = encoded_len(orig_len, padding).unwrap(); |
| 1315 |
|
| 1316 |
let decoded_estimate = engine |
| 1317 |
.internal_decoded_len_estimate(encoded_len) |
| 1318 |
.decoded_len_estimate(); |
| 1319 |
assert!(decoded_estimate >= orig_len); |
| 1320 |
assert!( |
| 1321 |
decoded_estimate - orig_len < 3, |
| 1322 |
"estimate: {}, encoded: {}, orig: {}", |
| 1323 |
decoded_estimate, |
| 1324 |
encoded_len, |
| 1325 |
orig_len |
| 1326 |
); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[apply(all_engines)] |
| 1333 |
fn estimate_via_u128_inflation<E: EngineWrapper>(engine_wrapper: E) { |
| 1334 |
// cover both ends of usize |
| 1335 |
(0..1000) |
| 1336 |
.chain(usize::MAX - 1000..=usize::MAX) |
| 1337 |
.for_each(|encoded_len| { |
| 1338 |
// inflate to 128 bit type to be able to safely use the easy formulas |
| 1339 |
let len_128 = encoded_len as u128; |
| 1340 |
|
| 1341 |
let estimate = E::standard() |
| 1342 |
.internal_decoded_len_estimate(encoded_len) |
| 1343 |
.decoded_len_estimate(); |
| 1344 |
|
| 1345 |
// This check is a little too strict: it requires using the (len + 3) / 4 * 3 formula |
| 1346 |
// or equivalent, but until other engines come along that use a different formula |
| 1347 |
// requiring that we think more carefully about what the allowable criteria are, this |
| 1348 |
// will do. |
| 1349 |
assert_eq!( |
| 1350 |
((len_128 + 3) / 4 * 3) as usize, |
| 1351 |
estimate, |
| 1352 |
"enc len {}", |
| 1353 |
encoded_len |
| 1354 |
); |
| 1355 |
}) |
| 1356 |
} |
| 1357 |
|
| 1358 |
fn do_invalid_trailing_byte(engine: impl Engine, mode: DecodePaddingMode) { |
| 1359 |
for num_prefix_quads in 0..256 { |
| 1360 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1361 |
s.push_str("Cg==\n"); |
| 1362 |
|
| 1363 |
// The case of trailing newlines is common enough to warrant a test for a good error |
| 1364 |
// message. |
| 1365 |
assert_eq!( |
| 1366 |
Err(DecodeError::InvalidByte(num_prefix_quads * 4 + 4, b'\n')), |
| 1367 |
engine.decode(&s), |
| 1368 |
"mode: {:?}, input: {}", |
| 1369 |
mode, |
| 1370 |
s |
| 1371 |
); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
fn do_invalid_trailing_padding_as_invalid_length(engine: impl Engine, mode: DecodePaddingMode) { |
| 1376 |
for num_prefix_quads in 0..256 { |
| 1377 |
let mut s: String = "ABCD".repeat(num_prefix_quads); |
| 1378 |
s.push_str("Cg==="); |
| 1379 |
|
| 1380 |
assert_eq!( |
| 1381 |
Err(DecodeError::InvalidLength), |
| 1382 |
engine.decode(&s), |
| 1383 |
"mode: {:?}, input: {}", |
| 1384 |
mode, |
| 1385 |
s |
| 1386 |
); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
/// Returns a tuple of the original data length, the encoded data length (just data), and the length including padding. |
| 1391 |
/// |
| 1392 |
/// Vecs provided should be empty. |
| 1393 |
fn generate_random_encoded_data<E: Engine, R: rand::Rng, D: distributions::Distribution<usize>>( |
| 1394 |
engine: &E, |
| 1395 |
orig_data: &mut Vec<u8>, |
| 1396 |
encode_buf: &mut Vec<u8>, |
| 1397 |
rng: &mut R, |
| 1398 |
length_distribution: &D, |
| 1399 |
) -> (usize, usize, usize) { |
| 1400 |
let padding: bool = engine.config().encode_padding(); |
| 1401 |
|
| 1402 |
let orig_len = fill_rand(orig_data, rng, length_distribution); |
| 1403 |
let expected_encoded_len = encoded_len(orig_len, padding).unwrap(); |
| 1404 |
encode_buf.resize(expected_encoded_len, 0); |
| 1405 |
|
| 1406 |
let base_encoded_len = engine.internal_encode(&orig_data[..], &mut encode_buf[..]); |
| 1407 |
|
| 1408 |
let enc_len_with_padding = if padding { |
| 1409 |
base_encoded_len + add_padding(base_encoded_len, &mut encode_buf[base_encoded_len..]) |
| 1410 |
} else { |
| 1411 |
base_encoded_len |
| 1412 |
}; |
| 1413 |
|
| 1414 |
assert_eq!(expected_encoded_len, enc_len_with_padding); |
| 1415 |
|
| 1416 |
(orig_len, base_encoded_len, enc_len_with_padding) |
| 1417 |
} |
| 1418 |
|
| 1419 |
// fill to a random length |
| 1420 |
fn fill_rand<R: rand::Rng, D: distributions::Distribution<usize>>( |
| 1421 |
vec: &mut Vec<u8>, |
| 1422 |
rng: &mut R, |
| 1423 |
length_distribution: &D, |
| 1424 |
) -> usize { |
| 1425 |
let len = length_distribution.sample(rng); |
| 1426 |
for _ in 0..len { |
| 1427 |
vec.push(rng.gen()); |
| 1428 |
} |
| 1429 |
|
| 1430 |
len |
| 1431 |
} |
| 1432 |
|
| 1433 |
fn fill_rand_len<R: rand::Rng>(vec: &mut Vec<u8>, rng: &mut R, len: usize) { |
| 1434 |
for _ in 0..len { |
| 1435 |
vec.push(rng.gen()); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
fn prefixed_data<'i>(input_with_prefix: &'i mut String, prefix_len: usize, data: &str) -> &'i str { |
| 1440 |
input_with_prefix.truncate(prefix_len); |
| 1441 |
input_with_prefix.push_str(data); |
| 1442 |
input_with_prefix.as_str() |
| 1443 |
} |
| 1444 |
|
| 1445 |
/// A wrapper to make using engines in rstest fixtures easier. |
| 1446 |
/// The functions don't need to be instance methods, but rstest does seem |
| 1447 |
/// to want an instance, so instances are passed to test functions and then ignored. |
| 1448 |
trait EngineWrapper { |
| 1449 |
type Engine: Engine; |
| 1450 |
|
| 1451 |
/// Return an engine configured for RFC standard base64 |
| 1452 |
fn standard() -> Self::Engine; |
| 1453 |
|
| 1454 |
/// Return an engine configured for RFC standard base64, except with no padding appended on |
| 1455 |
/// encode, and required no padding on decode. |
| 1456 |
fn standard_unpadded() -> Self::Engine; |
| 1457 |
|
| 1458 |
/// Return an engine configured for RFC standard alphabet with the provided encode and decode |
| 1459 |
/// pad settings |
| 1460 |
fn standard_with_pad_mode(encode_pad: bool, decode_pad_mode: DecodePaddingMode) |
| 1461 |
-> Self::Engine; |
| 1462 |
|
| 1463 |
/// Return an engine configured for RFC standard base64 that allows invalid trailing bits |
| 1464 |
fn standard_allow_trailing_bits() -> Self::Engine; |
| 1465 |
|
| 1466 |
/// Return an engine configured with a randomized alphabet and config |
| 1467 |
fn random<R: rand::Rng>(rng: &mut R) -> Self::Engine; |
| 1468 |
|
| 1469 |
/// Return an engine configured with the specified alphabet and randomized config |
| 1470 |
fn random_alphabet<R: rand::Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine; |
| 1471 |
} |
| 1472 |
|
| 1473 |
struct GeneralPurposeWrapper {} |
| 1474 |
|
| 1475 |
impl EngineWrapper for GeneralPurposeWrapper { |
| 1476 |
type Engine = general_purpose::GeneralPurpose; |
| 1477 |
|
| 1478 |
fn standard() -> Self::Engine { |
| 1479 |
general_purpose::GeneralPurpose::new(&STANDARD, general_purpose::PAD) |
| 1480 |
} |
| 1481 |
|
| 1482 |
fn standard_unpadded() -> Self::Engine { |
| 1483 |
general_purpose::GeneralPurpose::new(&STANDARD, general_purpose::NO_PAD) |
| 1484 |
} |
| 1485 |
|
| 1486 |
fn standard_with_pad_mode( |
| 1487 |
encode_pad: bool, |
| 1488 |
decode_pad_mode: DecodePaddingMode, |
| 1489 |
) -> Self::Engine { |
| 1490 |
general_purpose::GeneralPurpose::new( |
| 1491 |
&STANDARD, |
| 1492 |
general_purpose::GeneralPurposeConfig::new() |
| 1493 |
.with_encode_padding(encode_pad) |
| 1494 |
.with_decode_padding_mode(decode_pad_mode), |
| 1495 |
) |
| 1496 |
} |
| 1497 |
|
| 1498 |
fn standard_allow_trailing_bits() -> Self::Engine { |
| 1499 |
general_purpose::GeneralPurpose::new( |
| 1500 |
&STANDARD, |
| 1501 |
general_purpose::GeneralPurposeConfig::new().with_decode_allow_trailing_bits(true), |
| 1502 |
) |
| 1503 |
} |
| 1504 |
|
| 1505 |
fn random<R: rand::Rng>(rng: &mut R) -> Self::Engine { |
| 1506 |
let alphabet = random_alphabet(rng); |
| 1507 |
|
| 1508 |
Self::random_alphabet(rng, alphabet) |
| 1509 |
} |
| 1510 |
|
| 1511 |
fn random_alphabet<R: rand::Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine { |
| 1512 |
general_purpose::GeneralPurpose::new(alphabet, random_config(rng)) |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
struct NaiveWrapper {} |
| 1517 |
|
| 1518 |
impl EngineWrapper for NaiveWrapper { |
| 1519 |
type Engine = naive::Naive; |
| 1520 |
|
| 1521 |
fn standard() -> Self::Engine { |
| 1522 |
naive::Naive::new( |
| 1523 |
&STANDARD, |
| 1524 |
naive::NaiveConfig { |
| 1525 |
encode_padding: true, |
| 1526 |
decode_allow_trailing_bits: false, |
| 1527 |
decode_padding_mode: DecodePaddingMode::RequireCanonical, |
| 1528 |
}, |
| 1529 |
) |
| 1530 |
} |
| 1531 |
|
| 1532 |
fn standard_unpadded() -> Self::Engine { |
| 1533 |
naive::Naive::new( |
| 1534 |
&STANDARD, |
| 1535 |
naive::NaiveConfig { |
| 1536 |
encode_padding: false, |
| 1537 |
decode_allow_trailing_bits: false, |
| 1538 |
decode_padding_mode: DecodePaddingMode::RequireNone, |
| 1539 |
}, |
| 1540 |
) |
| 1541 |
} |
| 1542 |
|
| 1543 |
fn standard_with_pad_mode( |
| 1544 |
encode_pad: bool, |
| 1545 |
decode_pad_mode: DecodePaddingMode, |
| 1546 |
) -> Self::Engine { |
| 1547 |
naive::Naive::new( |
| 1548 |
&STANDARD, |
| 1549 |
naive::NaiveConfig { |
| 1550 |
encode_padding: false, |
| 1551 |
decode_allow_trailing_bits: false, |
| 1552 |
decode_padding_mode: decode_pad_mode, |
| 1553 |
}, |
| 1554 |
) |
| 1555 |
} |
| 1556 |
|
| 1557 |
fn standard_allow_trailing_bits() -> Self::Engine { |
| 1558 |
naive::Naive::new( |
| 1559 |
&STANDARD, |
| 1560 |
naive::NaiveConfig { |
| 1561 |
encode_padding: true, |
| 1562 |
decode_allow_trailing_bits: true, |
| 1563 |
decode_padding_mode: DecodePaddingMode::RequireCanonical, |
| 1564 |
}, |
| 1565 |
) |
| 1566 |
} |
| 1567 |
|
| 1568 |
fn random<R: rand::Rng>(rng: &mut R) -> Self::Engine { |
| 1569 |
let alphabet = random_alphabet(rng); |
| 1570 |
|
| 1571 |
Self::random_alphabet(rng, alphabet) |
| 1572 |
} |
| 1573 |
|
| 1574 |
fn random_alphabet<R: rand::Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine { |
| 1575 |
let mode = rng.gen(); |
| 1576 |
|
| 1577 |
let config = naive::NaiveConfig { |
| 1578 |
encode_padding: match mode { |
| 1579 |
DecodePaddingMode::Indifferent => rng.gen(), |
| 1580 |
DecodePaddingMode::RequireCanonical => true, |
| 1581 |
DecodePaddingMode::RequireNone => false, |
| 1582 |
}, |
| 1583 |
decode_allow_trailing_bits: rng.gen(), |
| 1584 |
decode_padding_mode: mode, |
| 1585 |
}; |
| 1586 |
|
| 1587 |
naive::Naive::new(alphabet, config) |
| 1588 |
} |
| 1589 |
} |
| 1590 |
|
| 1591 |
/// A pseudo-Engine that routes all decoding through [DecoderReader] |
| 1592 |
struct DecoderReaderEngine<E: Engine> { |
| 1593 |
engine: E, |
| 1594 |
} |
| 1595 |
|
| 1596 |
impl<E: Engine> From<E> for DecoderReaderEngine<E> { |
| 1597 |
fn from(value: E) -> Self { |
| 1598 |
Self { engine: value } |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
impl<E: Engine> Engine for DecoderReaderEngine<E> { |
| 1603 |
type Config = E::Config; |
| 1604 |
type DecodeEstimate = E::DecodeEstimate; |
| 1605 |
|
| 1606 |
fn internal_encode(&self, input: &[u8], output: &mut [u8]) -> usize { |
| 1607 |
self.engine.internal_encode(input, output) |
| 1608 |
} |
| 1609 |
|
| 1610 |
fn internal_decoded_len_estimate(&self, input_len: usize) -> Self::DecodeEstimate { |
| 1611 |
self.engine.internal_decoded_len_estimate(input_len) |
| 1612 |
} |
| 1613 |
|
| 1614 |
fn internal_decode( |
| 1615 |
&self, |
| 1616 |
input: &[u8], |
| 1617 |
output: &mut [u8], |
| 1618 |
decode_estimate: Self::DecodeEstimate, |
| 1619 |
) -> Result<DecodeMetadata, DecodeError> { |
| 1620 |
let mut reader = DecoderReader::new(input, &self.engine); |
| 1621 |
let mut buf = vec![0; input.len()]; |
| 1622 |
// to avoid effects like not detecting invalid length due to progressively growing |
| 1623 |
// the output buffer in read_to_end etc, read into a big enough buffer in one go |
| 1624 |
// to make behavior more consistent with normal engines |
| 1625 |
let _ = reader |
| 1626 |
.read(&mut buf) |
| 1627 |
.and_then(|len| { |
| 1628 |
buf.truncate(len); |
| 1629 |
// make sure we got everything |
| 1630 |
reader.read_to_end(&mut buf) |
| 1631 |
}) |
| 1632 |
.map_err(|io_error| { |
| 1633 |
*io_error |
| 1634 |
.into_inner() |
| 1635 |
.and_then(|inner| inner.downcast::<DecodeError>().ok()) |
| 1636 |
.unwrap() |
| 1637 |
})?; |
| 1638 |
output[..buf.len()].copy_from_slice(&buf); |
| 1639 |
Ok(DecodeMetadata::new( |
| 1640 |
buf.len(), |
| 1641 |
input |
| 1642 |
.iter() |
| 1643 |
.enumerate() |
| 1644 |
.filter(|(_offset, byte)| **byte == PAD_BYTE) |
| 1645 |
.map(|(offset, _byte)| offset) |
| 1646 |
.next(), |
| 1647 |
)) |
| 1648 |
} |
| 1649 |
|
| 1650 |
fn config(&self) -> &Self::Config { |
| 1651 |
self.engine.config() |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
struct DecoderReaderEngineWrapper {} |
| 1656 |
|
| 1657 |
impl EngineWrapper for DecoderReaderEngineWrapper { |
| 1658 |
type Engine = DecoderReaderEngine<general_purpose::GeneralPurpose>; |
| 1659 |
|
| 1660 |
fn standard() -> Self::Engine { |
| 1661 |
GeneralPurposeWrapper::standard().into() |
| 1662 |
} |
| 1663 |
|
| 1664 |
fn standard_unpadded() -> Self::Engine { |
| 1665 |
GeneralPurposeWrapper::standard_unpadded().into() |
| 1666 |
} |
| 1667 |
|
| 1668 |
fn standard_with_pad_mode( |
| 1669 |
encode_pad: bool, |
| 1670 |
decode_pad_mode: DecodePaddingMode, |
| 1671 |
) -> Self::Engine { |
| 1672 |
GeneralPurposeWrapper::standard_with_pad_mode(encode_pad, decode_pad_mode).into() |
| 1673 |
} |
| 1674 |
|
| 1675 |
fn standard_allow_trailing_bits() -> Self::Engine { |
| 1676 |
GeneralPurposeWrapper::standard_allow_trailing_bits().into() |
| 1677 |
} |
| 1678 |
|
| 1679 |
fn random<R: rand::Rng>(rng: &mut R) -> Self::Engine { |
| 1680 |
GeneralPurposeWrapper::random(rng).into() |
| 1681 |
} |
| 1682 |
|
| 1683 |
fn random_alphabet<R: rand::Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine { |
| 1684 |
GeneralPurposeWrapper::random_alphabet(rng, alphabet).into() |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
fn seeded_rng() -> impl rand::Rng { |
| 1689 |
rngs::SmallRng::from_entropy() |
| 1690 |
} |
| 1691 |
|
| 1692 |
fn all_pad_modes() -> Vec<DecodePaddingMode> { |
| 1693 |
vec![ |
| 1694 |
DecodePaddingMode::Indifferent, |
| 1695 |
DecodePaddingMode::RequireCanonical, |
| 1696 |
DecodePaddingMode::RequireNone, |
| 1697 |
] |
| 1698 |
} |
| 1699 |
|
| 1700 |
fn pad_modes_allowing_padding() -> Vec<DecodePaddingMode> { |
| 1701 |
vec![ |
| 1702 |
DecodePaddingMode::Indifferent, |
| 1703 |
DecodePaddingMode::RequireCanonical, |
| 1704 |
] |
| 1705 |
} |
| 1706 |
|
| 1707 |
fn assert_all_suffixes_ok<E: Engine>(engine: E, suffixes: Vec<&str>) { |
| 1708 |
for num_prefix_quads in 0..256 { |
| 1709 |
for &suffix in suffixes.iter() { |
| 1710 |
let mut encoded = "AAAA".repeat(num_prefix_quads); |
| 1711 |
encoded.push_str(suffix); |
| 1712 |
|
| 1713 |
let res = &engine.decode(&encoded); |
| 1714 |
assert!(res.is_ok()); |
| 1715 |
} |
| 1716 |
} |
| 1717 |
} |