summaryrefslogtreecommitdiffstats
path: root/third_party/rust/base64-0.10.1/src/tests.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/base64-0.10.1/src/tests.rs
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/base64-0.10.1/src/tests.rs')
-rw-r--r--third_party/rust/base64-0.10.1/src/tests.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/third_party/rust/base64-0.10.1/src/tests.rs b/third_party/rust/base64-0.10.1/src/tests.rs
new file mode 100644
index 0000000000..a1fb52a227
--- /dev/null
+++ b/third_party/rust/base64-0.10.1/src/tests.rs
@@ -0,0 +1,80 @@
+extern crate rand;
+
+use encode::encoded_size;
+use *;
+
+use std::str;
+
+use self::rand::distributions::{Distribution, Uniform};
+use self::rand::{FromEntropy, Rng};
+use self::rand::seq::SliceRandom;
+
+#[test]
+fn roundtrip_random_config_short() {
+ // exercise the slower encode/decode routines that operate on shorter buffers more vigorously
+ roundtrip_random_config(Uniform::new(0, 50), 10_000);
+}
+
+#[test]
+fn roundtrip_random_config_long() {
+ roundtrip_random_config(Uniform::new(0, 1000), 10_000);
+}
+
+pub fn assert_encode_sanity(encoded: &str, config: Config, input_len: usize) {
+ let input_rem = input_len % 3;
+ let expected_padding_len = if input_rem > 0 {
+ if config.pad {
+ 3 - input_rem
+ } else {
+ 0
+ }
+ } else {
+ 0
+ };
+
+ let expected_encoded_len = encoded_size(input_len, config).unwrap();
+
+ assert_eq!(expected_encoded_len, encoded.len());
+
+ let padding_len = encoded.chars().filter(|&c| c == '=').count();
+
+ assert_eq!(expected_padding_len, padding_len);
+
+ let _ = str::from_utf8(encoded.as_bytes()).expect("Base64 should be valid utf8");
+}
+
+fn roundtrip_random_config(input_len_range: Uniform<usize>, iterations: u32) {
+ let mut input_buf: Vec<u8> = Vec::new();
+ let mut encoded_buf = String::new();
+ let mut rng = rand::rngs::SmallRng::from_entropy();
+
+ for _ in 0..iterations {
+ input_buf.clear();
+ encoded_buf.clear();
+
+ let input_len = input_len_range.sample(&mut rng);
+
+ let config = random_config(&mut rng);
+
+ for _ in 0..input_len {
+ input_buf.push(rng.gen());
+ }
+
+ encode_config_buf(&input_buf, config, &mut encoded_buf);
+
+ assert_encode_sanity(&encoded_buf, config, input_len);
+
+ assert_eq!(input_buf, decode_config(&encoded_buf, config).unwrap());
+ }
+}
+
+pub fn random_config<R: Rng>(rng: &mut R) -> Config {
+ const CHARSETS: &[CharacterSet] = &[
+ CharacterSet::UrlSafe,
+ CharacterSet::Standard,
+ CharacterSet::Crypt,
+ ];
+ let charset = *CHARSETS.choose(rng).unwrap();
+
+ Config::new(charset, rng.gen())
+}