interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / base64ct / tests / proptests.rs

170 lines · 6264 bytes Raw Blame History
1//! Equivalence tests between `base64` crate and `base64ct`.
2
3#![cfg(feature = "std")]
4// TODO(tarcieri): fix `base64` crate deprecations
5// warning: use of deprecated function `base64::encode`: Use Engine::encode
6#![allow(deprecated)]
7
8use base64ct::{Base64 as Base64ct, Base64Unpadded as Base64UnpaddedCt, Encoding};
9use proptest::{prelude::*, string::*};
10
11/// Incremental Base64 decoder.
12type Decoder<'a> = base64ct::Decoder<'a, Base64ct>;
13
14/// Incremental Base64 encoder.
15type Encoder<'a> = base64ct::Encoder<'a, Base64ct>;
16
17proptest! {
18 /// Ensure `base64ct` decodes data encoded by `base64` ref crate
19 #[test]
20 fn decode_equiv(bytes in bytes_regex(".{0,256}").unwrap()) {
21 let encoded = base64::encode(&bytes);
22 let decoded = Base64ct::decode_vec(&encoded);
23 prop_assert_eq!(Ok(bytes), decoded);
24 }
25
26 /// Ensure that `base64ct`'s incremental decoder is able to decode randomly
27 /// generated inputs encoded by the `base64` ref crate
28 #[test]
29 fn decode_incremental(bytes in bytes_regex(".{1,256}").unwrap(), chunk_size in 1..256usize) {
30 let encoded = base64::encode(&bytes);
31 let chunk_size = match chunk_size % bytes.len() {
32 0 => 1,
33 n => n
34 };
35
36 let mut buffer = [0u8; 384];
37 let mut decoder = Decoder::new(encoded.as_bytes()).unwrap();
38 let mut remaining_len = decoder.remaining_len();
39
40 for chunk in bytes.chunks(chunk_size) {
41 prop_assert!(!decoder.is_finished());
42
43 let decoded = decoder.decode(&mut buffer[..chunk.len()]);
44 prop_assert_eq!(Ok(chunk), decoded);
45
46 remaining_len -= decoded.unwrap().len();
47 prop_assert_eq!(remaining_len, decoder.remaining_len());
48 }
49
50 prop_assert!(decoder.is_finished());
51 prop_assert_eq!(decoder.remaining_len(), 0);
52 }
53
54 #[test]
55 fn decode_incremental_wrapped(
56 bytes in bytes_regex(".{1,256}").unwrap(),
57 line_width in 4..128usize,
58 chunk_size in 1..256usize
59 ) {
60 for line_ending in ["\r", "\n", "\r\n"] {
61 let encoded = base64::encode(&bytes);
62
63 let mut encoded_wrapped = Vec::new();
64 let mut lines = encoded.as_bytes().chunks_exact(line_width);
65
66 for line in &mut lines {
67 encoded_wrapped.extend_from_slice(line);
68 encoded_wrapped.extend_from_slice(line_ending.as_bytes());
69 }
70
71 let last = lines.remainder();
72
73 if last.is_empty() {
74 encoded_wrapped.truncate(encoded_wrapped.len() - line_ending.len());
75 } else {
76 encoded_wrapped.extend_from_slice(last);
77 }
78
79 let chunk_size = match chunk_size % bytes.len() {
80 0 => 1,
81 n => n
82 };
83
84 let mut buffer = [0u8; 384];
85 let mut decoder = Decoder::new_wrapped(&encoded_wrapped, line_width).unwrap();
86 let mut remaining_len = decoder.remaining_len();
87
88 for chunk in bytes.chunks(chunk_size) {
89 prop_assert!(!decoder.is_finished());
90
91 let decoded = decoder.decode(&mut buffer[..chunk.len()]);
92 prop_assert_eq!(Ok(chunk), decoded);
93
94 remaining_len -= decoded.unwrap().len();
95 prop_assert_eq!(remaining_len, decoder.remaining_len());
96 }
97
98 prop_assert!(decoder.is_finished());
99 prop_assert_eq!(decoder.remaining_len(), 0);
100 }
101 }
102
103 /// Ensure `base64ct` and `base64` ref crate decode randomly generated
104 /// inputs equivalently.
105 ///
106 /// Inputs are selected to be valid characters in the standard Base64
107 /// padded alphabet, but are not necessarily valid Base64.
108 #[test]
109 fn decode_random(base64ish in string_regex("[A-Za-z0-9+/]{0,256}").unwrap()) {
110 let base64ish_padded = match base64ish.len() % 4 {
111 0 => base64ish,
112 n => {
113 let padding_len = 4 - n;
114 base64ish + &"=".repeat(padding_len)
115 }
116 };
117
118 let decoded_ct = Base64ct::decode_vec(&base64ish_padded).ok();
119 let decoded_ref = base64::decode(&base64ish_padded).ok();
120 prop_assert_eq!(decoded_ct, decoded_ref);
121 }
122
123 /// Ensure `base64ct` and the `base64` ref crate encode randomly generated
124 /// inputs equivalently.
125 #[test]
126 fn encode_equiv(bytes in bytes_regex(".{0,256}").unwrap()) {
127 let encoded_ct = Base64ct::encode_string(&bytes);
128 let encoded_ref = base64::encode(&bytes);
129 prop_assert_eq!(encoded_ct, encoded_ref);
130 }
131
132 /// Ensure that `base64ct`'s incremental encoder is able to encode randomly
133 /// generated inputs which match what's encoded by the `base64` ref crate
134 #[test]
135 fn encode_incremental(bytes in bytes_regex(".{1,256}").unwrap(), chunk_size in 1..256usize) {
136 let expected = base64::encode(&bytes);
137 let chunk_size = match chunk_size % bytes.len() {
138 0 => 1,
139 n => n
140 };
141
142 let mut buffer = [0u8; 1024];
143 let mut encoder = Encoder::new(&mut buffer).unwrap();
144
145 for chunk in bytes.chunks(chunk_size) {
146 encoder.encode(chunk).unwrap();
147 }
148
149 prop_assert_eq!(expected, encoder.finish().unwrap());
150 }
151
152 /// Make sure that base64ct and base64 both decode the same values
153 /// when expecting padded inputs, and produce the same outputs for those values.
154 #[test]
155 fn decode_arbitrary_padded(string in string_regex("[a-zA-Z0-9/+=?]{0,256}").unwrap()) {
156 let actual = Base64ct::decode_vec(&string);
157 let expected = base64::decode( &string);
158 assert_eq!(actual.ok(), expected.ok());
159 }
160
161 /// Make sure that base64ct and base64 both decode the same values
162 /// when expecting unpadded inputs, and produce the same outputs for those values.
163 #[test]
164 fn decode_arbitrary_unpadded(string in string_regex("[a-zA-Z0-9/+=?]{0,256}").unwrap()) {
165 use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _};
166 let actual = Base64UnpaddedCt::decode_vec(&string);
167 let expected = STANDARD_NO_PAD.decode(&string);
168 assert_eq!(actual.ok(), expected.ok());
169 }
170}