diff options
Diffstat (limited to 'tests/tests.rs')
-rw-r--r-- | tests/tests.rs | 129 |
1 files changed, 48 insertions, 81 deletions
diff --git a/tests/tests.rs b/tests/tests.rs index 11fed96..eceff40 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,18 +1,15 @@ -extern crate base64; -extern crate rand; - -use rand::{FromEntropy, Rng}; +use rand::{Rng, SeedableRng}; +use base64::engine::{general_purpose::STANDARD, Engine}; use base64::*; -mod helpers; -use self::helpers::*; +use base64::engine::general_purpose::{GeneralPurpose, NO_PAD}; // generate random contents of the specified length and test encode/decode roundtrip -fn roundtrip_random( +fn roundtrip_random<E: Engine>( byte_buf: &mut Vec<u8>, str_buf: &mut String, - config: Config, + engine: &E, byte_len: usize, approx_values_per_byte: u8, max_rounds: u64, @@ -30,8 +27,8 @@ fn roundtrip_random( byte_buf.push(r.gen::<u8>()); } - encode_config_buf(&byte_buf, config, str_buf); - decode_config_buf(&str_buf, config, &mut decode_buf).unwrap(); + engine.encode_string(&byte_buf, str_buf); + engine.decode_vec(&str_buf, &mut decode_buf).unwrap(); assert_eq!(byte_buf, &decode_buf); } @@ -52,17 +49,13 @@ fn calculate_number_of_rounds(byte_len: usize, approx_values_per_byte: u8, max: prod } -fn no_pad_config() -> Config { - Config::new(CharacterSet::Standard, false) -} - #[test] fn roundtrip_random_short_standard() { let mut byte_buf: Vec<u8> = Vec::new(); let mut str_buf = String::new(); for input_len in 0..40 { - roundtrip_random(&mut byte_buf, &mut str_buf, STANDARD, input_len, 4, 10000); + roundtrip_random(&mut byte_buf, &mut str_buf, &STANDARD, input_len, 4, 10000); } } @@ -72,7 +65,7 @@ fn roundtrip_random_with_fast_loop_standard() { let mut str_buf = String::new(); for input_len in 40..100 { - roundtrip_random(&mut byte_buf, &mut str_buf, STANDARD, input_len, 4, 1000); + roundtrip_random(&mut byte_buf, &mut str_buf, &STANDARD, input_len, 4, 1000); } } @@ -81,15 +74,9 @@ fn roundtrip_random_short_no_padding() { let mut byte_buf: Vec<u8> = Vec::new(); let mut str_buf = String::new(); + let engine = GeneralPurpose::new(&alphabet::STANDARD, NO_PAD); for input_len in 0..40 { - roundtrip_random( - &mut byte_buf, - &mut str_buf, - no_pad_config(), - input_len, - 4, - 10000, - ); + roundtrip_random(&mut byte_buf, &mut str_buf, &engine, input_len, 4, 10000); } } @@ -98,15 +85,10 @@ fn roundtrip_random_no_padding() { let mut byte_buf: Vec<u8> = Vec::new(); let mut str_buf = String::new(); + let engine = GeneralPurpose::new(&alphabet::STANDARD, NO_PAD); + for input_len in 40..100 { - roundtrip_random( - &mut byte_buf, - &mut str_buf, - no_pad_config(), - input_len, - 4, - 1000, - ); + roundtrip_random(&mut byte_buf, &mut str_buf, &engine, input_len, 4, 1000); } } @@ -120,13 +102,14 @@ fn roundtrip_decode_trailing_10_bytes() { // to handle that case. for num_quads in 0..25 { - let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect(); + let mut s: String = "ABCD".repeat(num_quads); s.push_str("EFGHIJKLZg"); - let decoded = decode(&s).unwrap(); + let engine = GeneralPurpose::new(&alphabet::STANDARD, NO_PAD); + let decoded = engine.decode(&s).unwrap(); assert_eq!(num_quads * 3 + 7, decoded.len()); - assert_eq!(s, encode_config(&decoded, STANDARD_NO_PAD)); + assert_eq!(s, engine.encode(&decoded)); } } @@ -140,55 +123,39 @@ fn display_wrapper_matches_normal_encode() { bytes.push(255); assert_eq!( - encode(&bytes), - format!( - "{}", - base64::display::Base64Display::with_config(&bytes, STANDARD) - ) + STANDARD.encode(&bytes), + format!("{}", display::Base64Display::new(&bytes, &STANDARD)) ); } #[test] -fn because_we_can() { - compare_decode("alice", "YWxpY2U="); - compare_decode("alice", &encode(b"alice")); - compare_decode("alice", &encode(&decode(&encode(b"alice")).unwrap())); -} - -#[test] -fn encode_config_slice_can_use_inline_buffer() { - let mut buf: [u8; 22] = [0; 22]; - let mut larger_buf: [u8; 24] = [0; 24]; - let mut input: [u8; 16] = [0; 16]; - - let mut rng = rand::rngs::SmallRng::from_entropy(); - for elt in &mut input { - *elt = rng.gen(); - } - - assert_eq!(22, encode_config_slice(&input, STANDARD_NO_PAD, &mut buf)); - let decoded = decode_config(&buf, STANDARD_NO_PAD).unwrap(); - - assert_eq!(decoded, input); - - // let's try it again with padding - - assert_eq!(24, encode_config_slice(&input, STANDARD, &mut larger_buf)); - let decoded = decode_config(&buf, STANDARD).unwrap(); - - assert_eq!(decoded, input); -} - -#[test] -#[should_panic(expected = "index 24 out of range for slice of length 22")] -fn encode_config_slice_panics_when_buffer_too_small() { - let mut buf: [u8; 22] = [0; 22]; - let mut input: [u8; 16] = [0; 16]; - - let mut rng = rand::rngs::SmallRng::from_entropy(); - for elt in &mut input { - *elt = rng.gen(); +fn encode_engine_slice_error_when_buffer_too_small() { + for num_triples in 1..100 { + let input = "AAA".repeat(num_triples); + let mut vec = vec![0; (num_triples - 1) * 4]; + assert_eq!( + EncodeSliceError::OutputSliceTooSmall, + STANDARD.encode_slice(&input, &mut vec).unwrap_err() + ); + vec.push(0); + assert_eq!( + EncodeSliceError::OutputSliceTooSmall, + STANDARD.encode_slice(&input, &mut vec).unwrap_err() + ); + vec.push(0); + assert_eq!( + EncodeSliceError::OutputSliceTooSmall, + STANDARD.encode_slice(&input, &mut vec).unwrap_err() + ); + vec.push(0); + assert_eq!( + EncodeSliceError::OutputSliceTooSmall, + STANDARD.encode_slice(&input, &mut vec).unwrap_err() + ); + vec.push(0); + assert_eq!( + num_triples * 4, + STANDARD.encode_slice(&input, &mut vec).unwrap() + ); } - - encode_config_slice(&input, STANDARD, &mut buf); } |