1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
extern crate base64;
use base64::*;
mod helpers;
use self::helpers::*;
#[test]
fn decode_rfc4648_0() {
compare_decode("", "");
}
#[test]
fn decode_rfc4648_1() {
compare_decode("f", "Zg==");
}
#[test]
fn decode_rfc4648_1_just_a_bit_of_padding() {
// allows less padding than required
compare_decode("f", "Zg=");
}
#[test]
fn decode_rfc4648_1_no_padding() {
compare_decode("f", "Zg");
}
#[test]
fn decode_rfc4648_2() {
compare_decode("fo", "Zm8=");
}
#[test]
fn decode_rfc4648_2_no_padding() {
compare_decode("fo", "Zm8");
}
#[test]
fn decode_rfc4648_3() {
compare_decode("foo", "Zm9v");
}
#[test]
fn decode_rfc4648_4() {
compare_decode("foob", "Zm9vYg==");
}
#[test]
fn decode_rfc4648_4_no_padding() {
compare_decode("foob", "Zm9vYg");
}
#[test]
fn decode_rfc4648_5() {
compare_decode("fooba", "Zm9vYmE=");
}
#[test]
fn decode_rfc4648_5_no_padding() {
compare_decode("fooba", "Zm9vYmE");
}
#[test]
fn decode_rfc4648_6() {
compare_decode("foobar", "Zm9vYmFy");
}
#[test]
fn decode_reject_null() {
assert_eq!(
DecodeError::InvalidByte(3, 0x0),
decode_config("YWx\0pY2U==", config_std_pad()).unwrap_err()
);
}
#[test]
fn decode_single_pad_byte_after_2_chars_in_trailing_quad_ok() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("Zg=");
let input_len = num_quads * 3 + 1;
// Since there are 3 bytes in the trailing quad, want to be sure this allows for the fact
// that it could be bad padding rather than assuming that it will decode to 2 bytes and
// therefore allow 1 extra round of fast decode logic (stage 1 / 2).
let mut decoded = Vec::new();
decoded.resize(input_len, 0);
assert_eq!(
input_len,
decode_config_slice(&s, STANDARD, &mut decoded).unwrap()
);
}
}
//this is a MAY in the rfc: https://tools.ietf.org/html/rfc4648#section-3.3
#[test]
fn decode_1_pad_byte_in_fast_loop_then_extra_padding_chunk_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("YWxpY2U=====");
// since the first 8 bytes are handled in stage 1 or 2, the padding is detected as a
// generic invalid byte, not specifcally a padding issue.
// Could argue that the *next* padding byte (in the next quad) is technically the first
// erroneous one, but reporting that accurately is more complex and probably nobody cares
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 7, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_2_pad_bytes_in_leftovers_then_extra_padding_chunk_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("YWxpY2UABB====");
// 6 bytes (4 padding) after last 8-byte chunk, so it's decoded by stage 4.
// First padding byte is invalid.
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 10, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_valid_bytes_after_padding_in_leftovers_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("YWxpY2UABB=B");
// 4 bytes after last 8-byte chunk, so it's decoded by stage 4.
// First (and only) padding byte is invalid.
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 10, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_absurd_pad_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("==Y=Wx===pY=2U=====");
// Plenty of remaining bytes, so handled by stage 1 or 2.
// first padding byte
assert_eq!(
DecodeError::InvalidByte(num_quads * 4, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_extra_padding_after_1_pad_bytes_in_trailing_quad_returns_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("EEE===");
// handled by stage 1, 2, or 4 depending on length
// first padding byte -- which would be legal if it was the only padding
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 3, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_extra_padding_after_2_pad_bytes_in_trailing_quad_2_returns_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("EE====");
// handled by stage 1, 2, or 4 depending on length
// first padding byte -- which would be legal if it was by itself
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 2, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_start_quad_with_padding_returns_error() {
for num_quads in 0..25 {
// add enough padding to ensure that we'll hit all 4 stages at the different lengths
for pad_bytes in 1..32 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
let padding: String = std::iter::repeat("=").take(pad_bytes).collect();
s.push_str(&padding);
if pad_bytes % 4 == 1 {
// detected in early length check
assert_eq!(DecodeError::InvalidLength, decode(&s).unwrap_err());
} else {
// padding lengths 2 - 8 are handled by stage 4
// padding length >= 8 will hit at least one chunk at stages 1, 2, 3 at different
// prefix lengths
assert_eq!(
DecodeError::InvalidByte(num_quads * 4, b'='),
decode(&s).unwrap_err()
);
}
}
}
}
#[test]
fn decode_padding_followed_by_non_padding_returns_error() {
for num_quads in 0..25 {
for pad_bytes in 0..31 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
let padding: String = std::iter::repeat("=").take(pad_bytes).collect();
s.push_str(&padding);
s.push_str("E");
if pad_bytes % 4 == 0 {
assert_eq!(DecodeError::InvalidLength, decode(&s).unwrap_err());
} else {
// pad len 1 - 8 will be handled by stage 4
// pad len 9 (suffix len 10) will have 8 bytes of padding handled by stage 3
// first padding byte
assert_eq!(
DecodeError::InvalidByte(num_quads * 4, b'='),
decode(&s).unwrap_err()
);
}
}
}
}
#[test]
fn decode_one_char_in_quad_with_padding_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push_str("E=");
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 1, b'='),
decode(&s).unwrap_err()
);
// more padding doesn't change the error
s.push_str("=");
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 1, b'='),
decode(&s).unwrap_err()
);
s.push_str("=");
assert_eq!(
DecodeError::InvalidByte(num_quads * 4 + 1, b'='),
decode(&s).unwrap_err()
);
}
}
#[test]
fn decode_one_char_in_quad_without_padding_error() {
for num_quads in 0..25 {
let mut s: String = std::iter::repeat("ABCD").take(num_quads).collect();
s.push('E');
assert_eq!(DecodeError::InvalidLength, decode(&s).unwrap_err());
}
}
#[test]
fn decode_reject_invalid_bytes_with_correct_error() {
for length in 1..100 {
for index in 0_usize..length {
for invalid_byte in " \t\n\r\x0C\x0B\x00%*.".bytes() {
let prefix: String = std::iter::repeat("A").take(index).collect();
let suffix: String = std::iter::repeat("B").take(length - index - 1).collect();
let input = prefix + &String::from_utf8(vec![invalid_byte]).unwrap() + &suffix;
assert_eq!(
length,
input.len(),
"length {} error position {}",
length,
index
);
if length % 4 == 1 && !suffix.is_empty() {
assert_eq!(DecodeError::InvalidLength, decode(&input).unwrap_err());
} else {
assert_eq!(
DecodeError::InvalidByte(index, invalid_byte),
decode(&input).unwrap_err()
);
}
}
}
}
}
#[test]
fn decode_imap() {
assert_eq!(
decode_config(b"+,,+", crate::IMAP_MUTF7),
decode_config(b"+//+", crate::STANDARD_NO_PAD)
);
}
#[test]
fn decode_invalid_trailing_bytes() {
// The case of trailing newlines is common enough to warrant a test for a good error
// message.
assert_eq!(
Err(DecodeError::InvalidByte(8, b'\n')),
decode(b"Zm9vCg==\n")
);
// extra padding, however, is still InvalidLength
assert_eq!(Err(DecodeError::InvalidLength), decode(b"Zm9vCg==="));
}
fn config_std_pad() -> Config {
Config::new(CharacterSet::Standard, true)
}
|