base64 0.5.0

encodes and decodes base64 as bytes or utf8
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
extern crate base64;
extern crate rand;

use rand::Rng;

use base64::*;

fn compare_encode(expected: &str, target: &[u8]) {
    assert_eq!(expected, encode(target));
}

fn compare_decode(expected: &str, target: &str) {
    assert_eq!(expected, String::from_utf8(decode(target).unwrap()).unwrap());
    assert_eq!(expected, String::from_utf8(decode(target.as_bytes()).unwrap()).unwrap());
}

fn compare_decode_mime(expected: &str, target: &str) {
    assert_eq!(expected, String::from_utf8(decode_config(target, MIME).unwrap()).unwrap());
}

fn push_rand(buf: &mut Vec<u8>, len: usize) {
    let mut r = rand::weak_rng();

    for _ in 0..len {
        buf.push(r.gen::<u8>());
    }
}

// generate every possible byte string recursively and test encode/decode roundtrip
fn roundtrip_append_recurse(byte_buf: &mut Vec<u8>, str_buf: &mut String, remaining_bytes: usize) {
    let orig_length = byte_buf.len();
    for b in 0..256 {
        byte_buf.push(b as u8);

        if remaining_bytes > 1 {
            roundtrip_append_recurse(byte_buf, str_buf, remaining_bytes - 1)
        } else {
            encode_config_buf(&byte_buf, STANDARD, str_buf);
            let roundtrip_bytes = decode_config(&str_buf, STANDARD).unwrap();
            assert_eq!(*byte_buf, roundtrip_bytes);

            str_buf.clear();

        }

        byte_buf.truncate(orig_length);
    }
}

// generate every possible byte string recursively and test encode/decode roundtrip with
// padding removed
fn roundtrip_append_recurse_strip_padding(byte_buf: &mut Vec<u8>, str_buf: &mut String,
                                          remaining_bytes: usize) {
    let orig_length = byte_buf.len();
    for b in 0..256 {
        byte_buf.push(b as u8);

        if remaining_bytes > 1 {
            roundtrip_append_recurse_strip_padding(byte_buf, str_buf, remaining_bytes - 1)
        } else {
            encode_config_buf(&byte_buf, STANDARD, str_buf);
            {
                let trimmed = str_buf.trim_right_matches('=');
                let roundtrip_bytes = decode_config(&trimmed, STANDARD).unwrap();
                assert_eq!(*byte_buf, roundtrip_bytes);
            }
            str_buf.clear();
        }

        byte_buf.truncate(orig_length);
    }
}

// generate random contents of the specified length and test encode/decode roundtrip
fn roundtrip_random(byte_buf: &mut Vec<u8>, str_buf: &mut String, byte_len: usize,
                    approx_values_per_byte: u8, max_rounds: u64) {
    let num_rounds = calculate_number_of_rounds(byte_len, approx_values_per_byte, max_rounds);
    let mut r = rand::weak_rng();

    for _ in 0..num_rounds {
        byte_buf.clear();
        str_buf.clear();
        while byte_buf.len() < byte_len {
            byte_buf.push(r.gen::<u8>());
        }

        encode_config_buf(&byte_buf, STANDARD, str_buf);
        let roundtrip_bytes = decode_config(&str_buf, STANDARD).unwrap();

        assert_eq!(*byte_buf, roundtrip_bytes);
    }
}

// generate random contents of the specified length and test encode/decode roundtrip
fn roundtrip_random_strip_padding(byte_buf: &mut Vec<u8>, str_buf: &mut String, byte_len: usize,
                    approx_values_per_byte: u8, max_rounds: u64) {
    // let the short ones be short but don't let it get too crazy large
    let num_rounds = calculate_number_of_rounds(byte_len, approx_values_per_byte, max_rounds);
    let mut r = rand::weak_rng();

    for _ in 0..num_rounds {
        byte_buf.clear();
        str_buf.clear();
        while byte_buf.len() < byte_len {
            byte_buf.push(r.gen::<u8>());
        }

        encode_config_buf(&byte_buf, STANDARD, str_buf);
        let trimmed = str_buf.trim_right_matches('=');
        let roundtrip_bytes = decode_config(&trimmed, STANDARD).unwrap();

        assert_eq!(*byte_buf, roundtrip_bytes);
    }
}

fn calculate_number_of_rounds(byte_len: usize, approx_values_per_byte: u8, max: u64) -> u64 {
    // don't overflow
    let mut prod = approx_values_per_byte as u64;

    for _ in 0..byte_len {
        if prod > max {
            return max;
        }

        prod = prod.saturating_mul(prod);
    }

    return prod;
}

//-------
//decode

#[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");
}

//this is a MAY in the rfc: https://tools.ietf.org/html/rfc4648#section-3.3
#[test]
fn decode_pad_inside_fast_loop_chunk_error() {
    // can't PartialEq Base64Error, so we do this the hard way
    match decode("YWxpY2U=====").unwrap_err() {
        DecodeError::InvalidByte(offset, byte) => {
            // since the first 8 bytes are handled in the fast loop, the
            // padding is an error. Could argue that the *next* padding
            // byte is technically the first erroneous one, but reporting
            // that accurately is more complex and probably nobody cares
            assert_eq!(7, offset);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_extra_pad_after_fast_loop_chunk_error() {
    match decode("YWxpY2UABB===").unwrap_err() {
        DecodeError::InvalidByte(offset, byte) => {
            // extraneous third padding byte
            assert_eq!(12, offset);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    };
}


//same
#[test]
fn decode_absurd_pad_error() {
    match decode("==Y=Wx===pY=2U=====").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            assert_eq!(0, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_starts_with_padding_single_quad_error() {
    match decode("====").unwrap_err() {
        DecodeError::InvalidByte(offset, byte) => {
            // with no real input, first padding byte is bogus
            assert_eq!(0, offset);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_extra_padding_in_trailing_quad_returns_error() {
    match decode("zzz==").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            // first unneeded padding byte
            assert_eq!(4, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_extra_padding_in_trailing_quad_2_returns_error() {
    match decode("zz===").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            // first unneeded padding byte
            assert_eq!(4, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}


#[test]
fn decode_start_second_quad_with_padding_returns_error() {
    match decode("zzzz=").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            // first unneeded padding byte
            assert_eq!(4, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_padding_in_last_quad_followed_by_non_padding_returns_error() {
    match decode("zzzz==z").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            assert_eq!(4, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_too_short_with_padding_error() {
    match decode("z==").unwrap_err() {
        DecodeError::InvalidByte(size, byte) => {
            // first unneeded padding byte
            assert_eq!(1, size);
            assert_eq!(0x3D, byte);
        }
        _ => assert!(false)
    }
}

#[test]
fn decode_too_short_without_padding_error() {
    match decode("z").unwrap_err() {
        DecodeError::InvalidLength => {}
        _ => assert!(false)
    }
}

#[test]
fn decode_too_short_second_quad_without_padding_error() {
    match decode("zzzzX").unwrap_err() {
        DecodeError::InvalidLength => {}
        _ => assert!(false)
    }
}

#[test]
fn decode_error_for_bogus_char_in_right_position() {
    for length in 1..25 {
        for error_position in 0_usize..length {
            let prefix: String = std::iter::repeat("A").take(error_position).collect();
            let suffix: String = std::iter::repeat("B").take(length - error_position - 1).collect();

            let input = prefix + "%" + &suffix;
            assert_eq!(length, input.len(),
                "length {} error position {}", length, error_position);

            match decode(&input).unwrap_err() {
                DecodeError::InvalidByte(size, byte) => {
                    assert_eq!(error_position, size,
                        "length {} error position {}", length, error_position);
                    assert_eq!(0x25, byte);
                }
                _ => assert!(false)
            }
        }
    }
}

#[test]
fn decode_into_nonempty_buffer_doesnt_clobber_existing_contents() {
    let mut orig_data = Vec::new();
    let mut encoded_data = String::new();
    let mut decoded_with_prefix = Vec::new();
    let mut decoded_without_prefix = Vec::new();
    let mut prefix = Vec::new();
    for encoded_length in 0_usize..26 {
        if encoded_length % 4 == 1 {
            // can't have a lone byte in a quad of input
            continue;
        };

        let raw_data_byte_triples = encoded_length / 4;
        // 4 base64 bytes -> 3 input bytes, 3 -> 2, 2 -> 1, 0 -> 0
        let raw_data_byte_leftovers = (encoded_length % 4).saturating_sub(1);

        // we'll borrow buf to make some data to encode
        orig_data.clear();
        push_rand(&mut orig_data, raw_data_byte_triples * 3 + raw_data_byte_leftovers);

        encoded_data.clear();
        encode_config_buf(&orig_data, STANDARD, &mut encoded_data);

        assert_eq!(encoded_length, encoded_data.trim_right_matches('=').len());

        for prefix_length in 1..26 {
            decoded_with_prefix.clear();
            decoded_without_prefix.clear();
            prefix.clear();

            // fill the buf with a prefix
            push_rand(&mut prefix, prefix_length);
            decoded_with_prefix.resize(prefix_length, 0);
            decoded_with_prefix.copy_from_slice(&prefix);

            // decode into the non-empty buf
            decode_config_buf(&encoded_data, STANDARD, &mut decoded_with_prefix).unwrap();
            // also decode into the empty buf
            decode_config_buf(&encoded_data, STANDARD, &mut decoded_without_prefix).unwrap();

            assert_eq!(prefix_length + decoded_without_prefix.len(), decoded_with_prefix.len());

            // append plain decode onto prefix
            prefix.append(&mut decoded_without_prefix);

            assert_eq!(prefix, decoded_with_prefix);
        }
    }
}

#[test]
fn roundtrip_random_no_fast_loop() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();

    for input_len in 0..9 {
        roundtrip_random(&mut byte_buf, &mut str_buf, input_len, 4, 10000);
    }
}

#[test]
fn roundtrip_random_with_fast_loop() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();

    for input_len in 9..26 {
        roundtrip_random(&mut byte_buf, &mut str_buf, input_len, 4, 100000);
    }
}

#[test]
fn roundtrip_random_no_fast_loop_no_padding() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();

    for input_len in 0..9 {
        roundtrip_random_strip_padding(&mut byte_buf, &mut str_buf, input_len, 4, 10000);
    }
}

#[test]
fn roundtrip_random_with_fast_loop_no_padding() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();

    for input_len in 9..26 {
        roundtrip_random_strip_padding(&mut byte_buf, &mut str_buf, input_len, 4, 100000);
    }
}

#[test]
fn roundtrip_all_1_byte() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();
    roundtrip_append_recurse(&mut byte_buf, &mut str_buf, 1);
}

#[test]
fn roundtrip_all_1_byte_no_padding() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();
    roundtrip_append_recurse_strip_padding(&mut byte_buf, &mut str_buf, 1);
}

#[test]
fn roundtrip_all_2_byte() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();
    roundtrip_append_recurse(&mut byte_buf, &mut str_buf, 2);
}

#[test]
fn roundtrip_all_2_byte_no_padding() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();
    roundtrip_append_recurse_strip_padding(&mut byte_buf, &mut str_buf, 2);
}

#[test]
fn roundtrip_all_3_byte() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();
    roundtrip_append_recurse(&mut byte_buf, &mut str_buf, 3);
}

#[test]
fn roundtrip_random_4_byte() {
    let mut byte_buf: Vec<u8> = Vec::new();
    let mut str_buf = String::new();

    roundtrip_random(&mut byte_buf, &mut str_buf, 4, 48, 10000);
}

//TODO like, write a thing to test every ascii val lol
//prolly just yankput the 64 array and a 256 one later
//is there a way to like, not have to write a fn every time
//"hi test harness this should panic 192 times" would be nice
//oh well whatever this is better done by a fuzzer

//strip yr whitespace kids
#[test]
#[should_panic]
fn decode_reject_space() {
    assert!(decode("YWx pY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_tab() {
    assert!(decode("YWx\tpY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_ff() {
    assert!(decode("YWx\x0cpY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_vtab() {
    assert!(decode("YWx\x0bpY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_nl() {
    assert!(decode("YWx\npY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_crnl() {
    assert!(decode("YWx\r\npY2U=").is_ok());
}

#[test]
#[should_panic]
fn decode_reject_null() {
    assert!(decode("YWx\0pY2U=").is_ok());
}

#[test]
fn decode_mime_allow_space() {
    assert!(decode_config("YWx pY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_allow_tab() {
    assert!(decode_config("YWx\tpY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_allow_ff() {
    assert!(decode_config("YWx\x0cpY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_allow_vtab() {
    assert!(decode_config("YWx\x0bpY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_allow_nl() {
    assert!(decode_config("YWx\npY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_allow_crnl() {
    assert!(decode_config("YWx\r\npY2U=", MIME).is_ok());
}

#[test]
#[should_panic]
fn decode_mime_reject_null() {
    assert!(decode_config("YWx\0pY2U=", MIME).is_ok());
}

#[test]
fn decode_mime_absurd_whitespace() {
    compare_decode_mime("how could you let this happen",
        "\n aG93I\n\nG\x0bNvd\r\nWxkI HlvdSB \tsZXQgdGh\rpcyBo\x0cYXBwZW4 =   ");
}

//-------
//encode

#[test]
fn encode_rfc4648_0() {
    compare_encode("", b"");
}

#[test]
fn encode_rfc4648_1() {
    compare_encode("Zg==", b"f");
}

#[test]
fn encode_rfc4648_2() {
    compare_encode("Zm8=", b"fo");
}

#[test]
fn encode_rfc4648_3() {
    compare_encode("Zm9v", b"foo");
}

#[test]
fn encode_rfc4648_4() {
    compare_encode("Zm9vYg==", b"foob");
}

#[test]
fn encode_rfc4648_5() {
    compare_encode("Zm9vYmE=", b"fooba");
}

#[test]
fn encode_rfc4648_6() {
    compare_encode("Zm9vYmFy", b"foobar");
}

#[test]
fn encode_all_ascii() {
    let mut ascii = Vec::<u8>::with_capacity(128);

    for i in 0..128 {
        ascii.push(i);
    }

    compare_encode("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8=", &ascii);
}

#[test]
fn encode_all_bytes() {
    let mut bytes = Vec::<u8>::with_capacity(256);

    for i in 0..255 {
        bytes.push(i);
    }
    bytes.push(255); //bug with "overflowing" ranges?

    compare_encode("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==", &bytes);
}

#[test]
fn encode_all_bytes_url() {
    let mut bytes = Vec::<u8>::with_capacity(256);

    for i in 0..255 {
        bytes.push(i);
    }
    bytes.push(255); //bug with "overflowing" ranges?

    assert_eq!("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w==", encode_config(&bytes, URL_SAFE));
}

#[test]
fn encode_into_nonempty_buffer_doesnt_clobber_existing_contents() {
    let mut orig_data = Vec::new();
    let mut encoded_with_prefix = String::new();
    let mut encoded_without_prefix = String::new();
    let mut prefix = String::new();
    for orig_data_length in 0_usize..26 {
        // we'll borrow buf to make some data to encode
        orig_data.clear();
        push_rand(&mut orig_data, orig_data_length);

        for prefix_length in 1..26 {
            encoded_with_prefix.clear();
            encoded_without_prefix.clear();
            prefix.clear();

            for _ in 0..prefix_length {
                prefix.push('~');
            }

            encoded_with_prefix.push_str(&prefix);

            // encode into the non-empty buf
            encode_config_buf(&orig_data, STANDARD, &mut encoded_with_prefix);
            // also encode into the empty buf
            encode_config_buf(&orig_data, STANDARD, &mut encoded_without_prefix);

            assert_eq!(prefix_length + encoded_without_prefix.len(), encoded_with_prefix.len());

            // append plain decode onto prefix
            prefix.push_str(&mut encoded_without_prefix);

            assert_eq!(prefix, encoded_with_prefix);
        }
    }
}


#[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_url_safe_without_padding() {
    let encoded = encode_config(b"alice", URL_SAFE_NO_PAD);
    assert_eq!(&encoded, "YWxpY2U");
    assert_eq!(String::from_utf8(decode(&encoded).unwrap()).unwrap(), "alice");
}