File: TestBase64.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (610 lines) | stat: -rw-r--r-- 24,748 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
 *      8014217 8025003 8026330 8028397 8129544 8165243 8176379
 * @summary tests java.util.Base64
 * @library /test/lib
 * @build jdk.test.lib.RandomFactory
 * @run main TestBase64
 * @key randomness
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Random;

import jdk.test.lib.RandomFactory;

public class TestBase64 {

    private static final Random rnd = RandomFactory.getRandom();

    public static void main(String args[]) throws Throwable {
        int numRuns  = 10;
        int numBytes = 200;
        if (args.length > 1) {
            numRuns  = Integer.parseInt(args[0]);
            numBytes = Integer.parseInt(args[1]);
        }

        test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
        test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
        test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);

        byte[] nl_1 = new byte[] {'\n'};
        byte[] nl_2 = new byte[] {'\n', '\r'};
        byte[] nl_3 = new byte[] {'\n', '\r', '\n'};
        for (int i = 0; i < 10; i++) {
            int len = rnd.nextInt(200) + 4;
            test(Base64.getMimeEncoder(len, nl_1),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);
            test(Base64.getMimeEncoder(len, nl_2),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);
            test(Base64.getMimeEncoder(len, nl_3),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);
        }

        // test mime case with < 4 length
        for (int len = 0; len < 4; len++) {
            test(Base64.getMimeEncoder(len, nl_1),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);

            test(Base64.getMimeEncoder(len, nl_2),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);

            test(Base64.getMimeEncoder(len, nl_3),
                 Base64.getMimeDecoder(),
                 numRuns, numBytes);
        }

        testNull(Base64.getEncoder());
        testNull(Base64.getUrlEncoder());
        testNull(Base64.getMimeEncoder());
        testNull(Base64.getMimeEncoder(10, new byte[]{'\n'}));
        testNull(Base64.getDecoder());
        testNull(Base64.getUrlDecoder());
        testNull(Base64.getMimeDecoder());
        checkNull(() -> Base64.getMimeEncoder(10, null));

        testIOE(Base64.getEncoder());
        testIOE(Base64.getUrlEncoder());
        testIOE(Base64.getMimeEncoder());
        testIOE(Base64.getMimeEncoder(10, new byte[]{'\n'}));

        byte[] src = new byte[1024];
        rnd.nextBytes(src);
        final byte[] decoded = Base64.getEncoder().encode(src);
        testIOE(Base64.getDecoder(), decoded);
        testIOE(Base64.getMimeDecoder(), decoded);
        testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src));

        // illegal line separator
        checkIAE(() -> Base64.getMimeEncoder(10, new byte[]{'\r', 'N'}));

        // malformed padding/ending
        testMalformedPadding();

        // illegal base64 character
        decoded[2] = (byte)0xe0;
        checkIAE(() -> Base64.getDecoder().decode(decoded));
        checkIAE(() -> Base64.getDecoder().decode(decoded, new byte[1024]));
        checkIAE(() -> Base64.getDecoder().decode(ByteBuffer.wrap(decoded)));

        // test single-non-base64 character for mime decoding
        testSingleNonBase64MimeDec();

        // test decoding of unpadded data
        testDecodeUnpadded();

        // test mime decoding with ignored character after padding
        testDecodeIgnoredAfterPadding();

        // given invalid args, encoder should not produce output
        testEncoderKeepsSilence(Base64.getEncoder());
        testEncoderKeepsSilence(Base64.getUrlEncoder());
        testEncoderKeepsSilence(Base64.getMimeEncoder());

        // given invalid args, decoder should not consume input
        testDecoderKeepsAbstinence(Base64.getDecoder());
        testDecoderKeepsAbstinence(Base64.getUrlDecoder());
        testDecoderKeepsAbstinence(Base64.getMimeDecoder());
    }

    private static void test(Base64.Encoder enc, Base64.Decoder dec,
                             int numRuns, int numBytes) throws Throwable {
        enc.encode(new byte[0]);
        dec.decode(new byte[0]);

        for (boolean withoutPadding : new boolean[] { false, true}) {
            if (withoutPadding) {
                 enc = enc.withoutPadding();
            }
            for (int i=0; i<numRuns; i++) {
                for (int j=1; j<numBytes; j++) {
                    byte[] orig = new byte[j];
                    rnd.nextBytes(orig);

                    // --------testing encode/decode(byte[])--------
                    byte[] encoded = enc.encode(orig);
                    byte[] decoded = dec.decode(encoded);

                    checkEqual(orig, decoded,
                               "Base64 array encoding/decoding failed!");
                    if (withoutPadding) {
                        if (encoded[encoded.length - 1] == '=')
                            throw new RuntimeException(
                               "Base64 enc.encode().withoutPadding() has padding!");
                    }
                    // --------testing encodetoString(byte[])/decode(String)--------
                    String str = enc.encodeToString(orig);
                    if (!Arrays.equals(str.getBytes("ASCII"), encoded)) {
                        throw new RuntimeException(
                            "Base64 encodingToString() failed!");
                    }
                    byte[] buf = dec.decode(new String(encoded, "ASCII"));
                    checkEqual(buf, orig, "Base64 decoding(String) failed!");

                    //-------- testing encode/decode(Buffer)--------
                    testEncode(enc, ByteBuffer.wrap(orig), encoded);
                    ByteBuffer bin = ByteBuffer.allocateDirect(orig.length);
                    bin.put(orig).flip();
                    testEncode(enc, bin, encoded);

                    testDecode(dec, ByteBuffer.wrap(encoded), orig);
                    bin = ByteBuffer.allocateDirect(encoded.length);
                    bin.put(encoded).flip();
                    testDecode(dec, bin, orig);

                    // --------testing decode.wrap(input stream)--------
                    // 1) random buf length
                    ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
                    InputStream is = dec.wrap(bais);
                    buf = new byte[orig.length + 10];
                    int len = orig.length;
                    int off = 0;
                    while (true) {
                        int n = rnd.nextInt(len);
                        if (n == 0)
                            n = 1;
                        n = is.read(buf, off, n);
                        if (n == -1) {
                            checkEqual(off, orig.length,
                                       "Base64 stream decoding failed");
                            break;
                        }
                        off += n;
                        len -= n;
                        if (len == 0)
                            break;
                    }
                    buf = Arrays.copyOf(buf, off);
                    checkEqual(buf, orig, "Base64 stream decoding failed!");

                    // 2) read one byte each
                    bais.reset();
                    is = dec.wrap(bais);
                    buf = new byte[orig.length + 10];
                    off = 0;
                    int b;
                    while ((b = is.read()) != -1) {
                        buf[off++] = (byte)b;
                    }
                    buf = Arrays.copyOf(buf, off);
                    checkEqual(buf, orig, "Base64 stream decoding failed!");

                    // --------testing encode.wrap(output stream)--------
                    ByteArrayOutputStream baos = new ByteArrayOutputStream((orig.length + 2) / 3 * 4 + 10);
                    OutputStream os = enc.wrap(baos);
                    off = 0;
                    len = orig.length;
                    for (int k = 0; k < 5; k++) {
                        if (len == 0)
                            break;
                        int n = rnd.nextInt(len);
                        if (n == 0)
                            n = 1;
                        os.write(orig, off, n);
                        off += n;
                        len -= n;
                    }
                    if (len != 0)
                        os.write(orig, off, len);
                    os.close();
                    buf = baos.toByteArray();
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");

                    // 2) write one byte each
                    baos.reset();
                    os = enc.wrap(baos);
                    off = 0;
                    while (off < orig.length) {
                        os.write(orig[off++]);
                    }
                    os.close();
                    buf = baos.toByteArray();
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");

                    // --------testing encode(in, out); -> bigger buf--------
                    buf = new byte[encoded.length + rnd.nextInt(100)];
                    int ret = enc.encode(orig, buf);
                    checkEqual(ret, encoded.length,
                               "Base64 enc.encode(src, null) returns wrong size!");
                    buf = Arrays.copyOf(buf, ret);
                    checkEqual(buf, encoded,
                               "Base64 enc.encode(src, dst) failed!");

                    // --------testing decode(in, out); -> bigger buf--------
                    buf = new byte[orig.length + rnd.nextInt(100)];
                    ret = dec.decode(encoded, buf);
                    checkEqual(ret, orig.length,
                              "Base64 enc.encode(src, null) returns wrong size!");
                    buf = Arrays.copyOf(buf, ret);
                    checkEqual(buf, orig,
                               "Base64 dec.decode(src, dst) failed!");

                }
            }
        }
    }

    private static final byte[] ba_null = null;
    private static final String str_null = null;
    private static final ByteBuffer bb_null = null;

    private static void testNull(Base64.Encoder enc) {
        checkNull(() -> enc.encode(ba_null));
        checkNull(() -> enc.encodeToString(ba_null));
        checkNull(() -> enc.encode(ba_null, new byte[10]));
        checkNull(() -> enc.encode(new byte[10], ba_null));
        checkNull(() -> enc.encode(bb_null));
        checkNull(() -> enc.wrap((OutputStream)null));
    }

    private static void testNull(Base64.Decoder dec) {
        checkNull(() -> dec.decode(ba_null));
        checkNull(() -> dec.decode(str_null));
        checkNull(() -> dec.decode(ba_null, new byte[10]));
        checkNull(() -> dec.decode(new byte[10], ba_null));
        checkNull(() -> dec.decode(bb_null));
        checkNull(() -> dec.wrap((InputStream)null));
    }

    @FunctionalInterface
    private static interface Testable {
        public void test() throws Throwable;
    }

    private static void testIOE(Base64.Encoder enc) throws Throwable {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
        OutputStream os = enc.wrap(baos);
        os.write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9});
        os.close();
        checkIOE(() -> os.write(10));
        checkIOE(() -> os.write(new byte[] {10}));
        checkIOE(() -> os.write(new byte[] {10}, 1, 4));
    }

    private static void testIOE(Base64.Decoder dec, byte[] decoded) throws Throwable {
        ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
        InputStream is = dec.wrap(bais);
        is.read(new byte[10]);
        is.close();
        checkIOE(() -> is.read());
        checkIOE(() -> is.read(new byte[] {10}));
        checkIOE(() -> is.read(new byte[] {10}, 1, 4));
        checkIOE(() -> is.available());
        checkIOE(() -> is.skip(20));
    }

    private static final void checkNull(Runnable r) {
        try {
            r.run();
            throw new RuntimeException("NPE is not thrown as expected");
        } catch (NullPointerException npe) {}
    }

    private static final void checkIOE(Testable t) throws Throwable {
        try {
            t.test();
            throw new RuntimeException("IOE is not thrown as expected");
        } catch (IOException ioe) {}
    }

    private static final void checkIAE(Runnable r) throws Throwable {
        try {
            r.run();
            throw new RuntimeException("IAE is not thrown as expected");
        } catch (IllegalArgumentException iae) {}
    }

    private static void testDecodeIgnoredAfterPadding() throws Throwable {
        for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
            byte[][] src = new byte[][] {
                "A".getBytes("ascii"),
                "AB".getBytes("ascii"),
                "ABC".getBytes("ascii"),
                "ABCD".getBytes("ascii"),
                "ABCDE".getBytes("ascii")
            };
            Base64.Encoder encM = Base64.getMimeEncoder();
            Base64.Decoder decM = Base64.getMimeDecoder();
            Base64.Encoder enc = Base64.getEncoder();
            Base64.Decoder dec = Base64.getDecoder();
            for (int i = 0; i < src.length; i++) {
                // decode(byte[])
                byte[] encoded = encM.encode(src[i]);
                encoded = Arrays.copyOf(encoded, encoded.length + 1);
                encoded[encoded.length - 1] = nonBase64;
                checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
                byte[] decoded = new byte[src[i].length];
                decM.decode(encoded, decoded);
                checkEqual(decoded, src[i], "Non-base64 char is not ignored");

                try {
                    dec.decode(encoded);
                    throw new RuntimeException("No IAE for non-base64 char");
                } catch (IllegalArgumentException iae) {}
            }
        }
    }

    private static void testMalformedPadding() throws Throwable {
        Object[] data = new Object[] {
            "$=#",       "",      0,      // illegal ending unit
            "A",         "",      0,      // dangling single byte
            "A=",        "",      0,
            "A==",       "",      0,
            "QUJDA",     "ABC",   4,
            "QUJDA=",    "ABC",   4,
            "QUJDA==",   "ABC",   4,

            "=",         "",      0,      // unnecessary padding
            "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"

            "AA=",       "",      0,      // incomplete padding
            "QQ=",       "",      0,
            "QQ=N",      "",      0,      // incorrect padding
            "QQ=?",      "",      0,
            "QUJDQQ=",   "ABC",   4,
            "QUJDQQ=N",  "ABC",   4,
            "QUJDQQ=?",  "ABC",   4,
        };

        Base64.Decoder[] decs = new Base64.Decoder[] {
            Base64.getDecoder(),
            Base64.getUrlDecoder(),
            Base64.getMimeDecoder()
        };

        for (Base64.Decoder dec : decs) {
            for (int i = 0; i < data.length; i += 3) {
                final String srcStr = (String)data[i];
                final byte[] srcBytes = srcStr.getBytes("ASCII");
                final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
                byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
                int pos = (Integer)data[i + 2];

                // decode(byte[])
                checkIAE(() -> dec.decode(srcBytes));

                // decode(String)
                checkIAE(() -> dec.decode(srcStr));

                // decode(ByteBuffer)
                checkIAE(() -> dec.decode(srcBB));

                // wrap stream
                checkIOE(new Testable() {
                    public void test() throws IOException {
                        try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                            while (is.read() != -1);
                        }
                }});
            }
        }

        // anything left after padding is "invalid"/IAE, if
        // not MIME. In case of MIME, non-base64 character(s)
        // is ignored.
        checkIAE(() -> Base64.getDecoder().decode("AA==\u00D2"));
        checkIAE(() -> Base64.getUrlDecoder().decode("AA==\u00D2"));
        Base64.getMimeDecoder().decode("AA==\u00D2");
     }

    private static void  testDecodeUnpadded() throws Throwable {
        byte[] srcA = new byte[] { 'Q', 'Q' };
        byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
        Base64.Decoder dec = Base64.getDecoder();
        byte[] ret = dec.decode(srcA);
        if (ret[0] != 'A')
            throw new RuntimeException("Decoding unpadding input A failed");
        ret = dec.decode(srcAA);
        if (ret[0] != 'A' && ret[1] != 'A')
            throw new RuntimeException("Decoding unpadding input AA failed");
        ret = new byte[10];
        if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
            ret[0] != 'A')
            throw new RuntimeException("Decoding unpadding input A from stream failed");
        if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
            ret[0] != 'A' && ret[1] != 'A')
            throw new RuntimeException("Decoding unpadding input AA from stream failed");
    }

    // single-non-base64-char should be ignored for mime decoding, but
    // iae for basic decoding
    private static void testSingleNonBase64MimeDec() throws Throwable {
        for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
            if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
                throw new RuntimeException("non-base64 char is not ignored");
            }
            try {
                Base64.getDecoder().decode(nonBase64);
                throw new RuntimeException("No IAE for single non-base64 char");
            } catch (IllegalArgumentException iae) {}
        }
    }

    private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected)
        throws Throwable {

        ByteBuffer bout = enc.encode(bin);
        byte[] buf = new byte[bout.remaining()];
        bout.get(buf);
        if (bin.hasRemaining()) {
            throw new RuntimeException(
                "Base64 enc.encode(ByteBuffer) failed!");
        }
        checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!");
    }

    private static final void testDecode(Base64.Decoder dec, ByteBuffer bin, byte[] expected)
        throws Throwable {

        ByteBuffer bout = dec.decode(bin);
        byte[] buf = new byte[bout.remaining()];
        bout.get(buf);
        checkEqual(buf, expected, "Base64 dec.decode(bf) failed!");
    }

    private static final void checkEqual(int v1, int v2, String msg)
        throws Throwable {
       if (v1 != v2) {
           System.out.printf("    v1=%d%n", v1);
           System.out.printf("    v2=%d%n", v2);
           throw new RuntimeException(msg);
       }
    }

    private static final void checkEqual(byte[] r1, byte[] r2, String msg)
        throws Throwable {
       if (!Arrays.equals(r1, r2)) {
           System.out.printf("    r1[%d]=[%s]%n", r1.length, new String(r1));
           System.out.printf("    r2[%d]=[%s]%n", r2.length, new String(r2));
           throw new RuntimeException(msg);
       }
    }

    // remove line feeds,
    private static final byte[] normalize(byte[] src) {
        int n = 0;
        boolean hasUrl = false;
        for (int i = 0; i < src.length; i++) {
            if (src[i] == '\r' || src[i] == '\n')
                n++;
            if (src[i] == '-' || src[i] == '_')
                hasUrl = true;
        }
        if (n == 0 && hasUrl == false)
            return src;
        byte[] ret = new byte[src.length - n];
        int j = 0;
        for (int i = 0; i < src.length; i++) {
            if (src[i] == '-')
                ret[j++] = '+';
            else if (src[i] == '_')
                ret[j++] = '/';
            else if (src[i] != '\r' && src[i] != '\n')
                ret[j++] = src[i];
        }
        return ret;
    }

    private static void testEncoderKeepsSilence(Base64.Encoder enc)
            throws Throwable {
        List<Integer> vals = new ArrayList<>(List.of(Integer.MIN_VALUE,
                Integer.MIN_VALUE + 1, -1111, -2, -1, 0, 1, 2, 3, 1111,
                Integer.MAX_VALUE - 1, Integer.MAX_VALUE));
        vals.addAll(List.of(rnd.nextInt(), rnd.nextInt(), rnd.nextInt(),
                rnd.nextInt()));
        byte[] buf = new byte[] {1, 0, 91};
        for (int off : vals) {
            for (int len : vals) {
                if (off >= 0 && len >= 0 && off <= buf.length - len) {
                    // valid args, skip them
                    continue;
                }
                // invalid args, test them
                System.out.println("testing off=" + off + ", len=" + len);

                ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
                try (OutputStream os = enc.wrap(baos)) {
                    os.write(buf, off, len);
                    throw new RuntimeException("Expected IOOBEx was not thrown");
                } catch (IndexOutOfBoundsException expected) {
                }
                if (baos.size() > 0)
                    throw new RuntimeException("No output was expected, but got "
                            + baos.size() + " bytes");
            }
        }
    }

    private static void testDecoderKeepsAbstinence(Base64.Decoder dec)
            throws Throwable {
        List<Integer> vals = new ArrayList<>(List.of(Integer.MIN_VALUE,
                Integer.MIN_VALUE + 1, -1111, -2, -1, 0, 1, 2, 3, 1111,
                Integer.MAX_VALUE - 1, Integer.MAX_VALUE));
        vals.addAll(List.of(rnd.nextInt(), rnd.nextInt(), rnd.nextInt(),
                rnd.nextInt()));
        byte[] buf = new byte[3];
        for (int off : vals) {
            for (int len : vals) {
                if (off >= 0 && len >= 0 && off <= buf.length - len) {
                    // valid args, skip them
                    continue;
                }
                // invalid args, test them
                System.out.println("testing off=" + off + ", len=" + len);

                String input = "AAAAAAAAAAAAAAAAAAAAAA";
                ByteArrayInputStream bais =
                        new ByteArrayInputStream(input.getBytes("Latin1"));
                try (InputStream is = dec.wrap(bais)) {
                    is.read(buf, off, len);
                    throw new RuntimeException("Expected IOOBEx was not thrown");
                } catch (IndexOutOfBoundsException expected) {
                }
                if (bais.available() != input.length())
                    throw new RuntimeException("No input should be consumed, "
                            + "but consumed " + (input.length() - bais.available())
                            + " bytes");
            }
        }
    }
}