test/jdk/java/util/Base64/TestBase64.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45466 jdk/test/java/util/Base64/TestBase64.java@faf1c55d2046
child 48519 482ede6c4936
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     1
/*
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 40740
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     4
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     8
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    13
 * accompanied this code).
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    14
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    18
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    21
 * questions.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    22
 */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    23
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    24
/**
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
    25
 * @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    26
 *       8014217 8025003 8026330 8028397 8129544 8165243
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    27
 * @summary tests java.util.Base64
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 40740
diff changeset
    28
 * @library /test/lib
45466
faf1c55d2046 8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents: 45288
diff changeset
    29
 * @build jdk.test.lib.RandomFactory
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    30
 * @run main TestBase64
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 26039
diff changeset
    31
 * @key randomness
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    32
 */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    33
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    34
import java.io.ByteArrayInputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    35
import java.io.ByteArrayOutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    36
import java.io.InputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    37
import java.io.IOException;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    38
import java.io.OutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    39
import java.nio.ByteBuffer;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    40
import java.util.Arrays;
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    41
import java.util.ArrayList;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    42
import java.util.Base64;
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    43
import java.util.List;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    44
import java.util.Random;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    45
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 40740
diff changeset
    46
import jdk.test.lib.RandomFactory;
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    47
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    48
public class TestBase64 {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    49
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    50
    private static final Random rnd = RandomFactory.getRandom();
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    51
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    52
    public static void main(String args[]) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    53
        int numRuns  = 10;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    54
        int numBytes = 200;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    55
        if (args.length > 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    56
            numRuns  = Integer.parseInt(args[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    57
            numBytes = Integer.parseInt(args[1]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    58
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    59
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    60
        test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    61
        test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    62
        test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    63
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    64
        byte[] nl_1 = new byte[] {'\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    65
        byte[] nl_2 = new byte[] {'\n', '\r'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    66
        byte[] nl_3 = new byte[] {'\n', '\r', '\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    67
        for (int i = 0; i < 10; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    68
            int len = rnd.nextInt(200) + 4;
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    69
            test(Base64.getMimeEncoder(len, nl_1),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    70
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    71
                 numRuns, numBytes);
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    72
            test(Base64.getMimeEncoder(len, nl_2),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    73
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    74
                 numRuns, numBytes);
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    75
            test(Base64.getMimeEncoder(len, nl_3),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    76
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    77
                 numRuns, numBytes);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    78
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    79
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    80
        testNull(Base64.getEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    81
        testNull(Base64.getUrlEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    82
        testNull(Base64.getMimeEncoder());
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    83
        testNull(Base64.getMimeEncoder(10, new byte[]{'\n'}));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    84
        testNull(Base64.getDecoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    85
        testNull(Base64.getUrlDecoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    86
        testNull(Base64.getMimeDecoder());
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    87
        checkNull(() -> Base64.getMimeEncoder(10, null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    88
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    89
        testIOE(Base64.getEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    90
        testIOE(Base64.getUrlEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    91
        testIOE(Base64.getMimeEncoder());
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    92
        testIOE(Base64.getMimeEncoder(10, new byte[]{'\n'}));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    93
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    94
        byte[] src = new byte[1024];
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    95
        rnd.nextBytes(src);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    96
        final byte[] decoded = Base64.getEncoder().encode(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    97
        testIOE(Base64.getDecoder(), decoded);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    98
        testIOE(Base64.getMimeDecoder(), decoded);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    99
        testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   100
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   101
        // illegal line separator
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   102
        checkIAE(() -> Base64.getMimeEncoder(10, new byte[]{'\r', 'N'}));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   103
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   104
        // malformed padding/ending
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   105
        testMalformedPadding();
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   106
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   107
        // illegal base64 character
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   108
        decoded[2] = (byte)0xe0;
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   109
        checkIAE(() -> Base64.getDecoder().decode(decoded));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   110
        checkIAE(() -> Base64.getDecoder().decode(decoded, new byte[1024]));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   111
        checkIAE(() -> Base64.getDecoder().decode(ByteBuffer.wrap(decoded)));
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   112
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   113
        // test single-non-base64 character for mime decoding
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   114
        testSingleNonBase64MimeDec();
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   115
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   116
        // test decoding of unpadded data
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   117
        testDecodeUnpadded();
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   118
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   119
        // test mime decoding with ignored character after padding
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   120
        testDecodeIgnoredAfterPadding();
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   121
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   122
        // given invalid args, encoder should not produce output
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   123
        testEncoderKeepsSilence(Base64.getEncoder());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   124
        testEncoderKeepsSilence(Base64.getUrlEncoder());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   125
        testEncoderKeepsSilence(Base64.getMimeEncoder());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   126
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   127
        // given invalid args, decoder should not consume input
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   128
        testDecoderKeepsAbstinence(Base64.getDecoder());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   129
        testDecoderKeepsAbstinence(Base64.getUrlDecoder());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   130
        testDecoderKeepsAbstinence(Base64.getMimeDecoder());
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   131
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   132
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   133
    private static void test(Base64.Encoder enc, Base64.Decoder dec,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   134
                             int numRuns, int numBytes) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   135
        enc.encode(new byte[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   136
        dec.decode(new byte[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   137
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   138
        for (boolean withoutPadding : new boolean[] { false, true}) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   139
            if (withoutPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   140
                 enc = enc.withoutPadding();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   141
            }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   142
            for (int i=0; i<numRuns; i++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   143
                for (int j=1; j<numBytes; j++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   144
                    byte[] orig = new byte[j];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   145
                    rnd.nextBytes(orig);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   146
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   147
                    // --------testing encode/decode(byte[])--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   148
                    byte[] encoded = enc.encode(orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   149
                    byte[] decoded = dec.decode(encoded);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   150
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   151
                    checkEqual(orig, decoded,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   152
                               "Base64 array encoding/decoding failed!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   153
                    if (withoutPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   154
                        if (encoded[encoded.length - 1] == '=')
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   155
                            throw new RuntimeException(
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   156
                               "Base64 enc.encode().withoutPadding() has padding!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   157
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   158
                    // --------testing encodetoString(byte[])/decode(String)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   159
                    String str = enc.encodeToString(orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   160
                    if (!Arrays.equals(str.getBytes("ASCII"), encoded)) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   161
                        throw new RuntimeException(
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   162
                            "Base64 encodingToString() failed!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   163
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   164
                    byte[] buf = dec.decode(new String(encoded, "ASCII"));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   165
                    checkEqual(buf, orig, "Base64 decoding(String) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   166
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   167
                    //-------- testing encode/decode(Buffer)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   168
                    testEncode(enc, ByteBuffer.wrap(orig), encoded);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   169
                    ByteBuffer bin = ByteBuffer.allocateDirect(orig.length);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   170
                    bin.put(orig).flip();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   171
                    testEncode(enc, bin, encoded);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   172
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   173
                    testDecode(dec, ByteBuffer.wrap(encoded), orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   174
                    bin = ByteBuffer.allocateDirect(encoded.length);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   175
                    bin.put(encoded).flip();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   176
                    testDecode(dec, bin, orig);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   177
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   178
                    // --------testing decode.wrap(input stream)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   179
                    // 1) random buf length
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   180
                    ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   181
                    InputStream is = dec.wrap(bais);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   182
                    buf = new byte[orig.length + 10];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   183
                    int len = orig.length;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   184
                    int off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   185
                    while (true) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   186
                        int n = rnd.nextInt(len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   187
                        if (n == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   188
                            n = 1;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   189
                        n = is.read(buf, off, n);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   190
                        if (n == -1) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   191
                            checkEqual(off, orig.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   192
                                       "Base64 stream decoding failed");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   193
                            break;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   194
                        }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   195
                        off += n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   196
                        len -= n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   197
                        if (len == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   198
                            break;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   199
                    }
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   200
                    buf = Arrays.copyOf(buf, off);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   201
                    checkEqual(buf, orig, "Base64 stream decoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   202
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   203
                    // 2) read one byte each
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   204
                    bais.reset();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   205
                    is = dec.wrap(bais);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   206
                    buf = new byte[orig.length + 10];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   207
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   208
                    int b;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   209
                    while ((b = is.read()) != -1) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   210
                        buf[off++] = (byte)b;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   211
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   212
                    buf = Arrays.copyOf(buf, off);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   213
                    checkEqual(buf, orig, "Base64 stream decoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   214
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   215
                    // --------testing encode.wrap(output stream)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   216
                    ByteArrayOutputStream baos = new ByteArrayOutputStream((orig.length + 2) / 3 * 4 + 10);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   217
                    OutputStream os = enc.wrap(baos);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   218
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   219
                    len = orig.length;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   220
                    for (int k = 0; k < 5; k++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   221
                        if (len == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   222
                            break;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   223
                        int n = rnd.nextInt(len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   224
                        if (n == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   225
                            n = 1;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   226
                        os.write(orig, off, n);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   227
                        off += n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   228
                        len -= n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   229
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   230
                    if (len != 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   231
                        os.write(orig, off, len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   232
                    os.close();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   233
                    buf = baos.toByteArray();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   234
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   235
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   236
                    // 2) write one byte each
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   237
                    baos.reset();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   238
                    os = enc.wrap(baos);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   239
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   240
                    while (off < orig.length) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   241
                        os.write(orig[off++]);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   242
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   243
                    os.close();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   244
                    buf = baos.toByteArray();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   245
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   246
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   247
                    // --------testing encode(in, out); -> bigger buf--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   248
                    buf = new byte[encoded.length + rnd.nextInt(100)];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   249
                    int ret = enc.encode(orig, buf);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   250
                    checkEqual(ret, encoded.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   251
                               "Base64 enc.encode(src, null) returns wrong size!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   252
                    buf = Arrays.copyOf(buf, ret);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   253
                    checkEqual(buf, encoded,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   254
                               "Base64 enc.encode(src, dst) failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   255
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   256
                    // --------testing decode(in, out); -> bigger buf--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   257
                    buf = new byte[orig.length + rnd.nextInt(100)];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   258
                    ret = dec.decode(encoded, buf);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   259
                    checkEqual(ret, orig.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   260
                              "Base64 enc.encode(src, null) returns wrong size!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   261
                    buf = Arrays.copyOf(buf, ret);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   262
                    checkEqual(buf, orig,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   263
                               "Base64 dec.decode(src, dst) failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   264
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   265
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   266
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   267
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   268
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   269
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   270
    private static final byte[] ba_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   271
    private static final String str_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   272
    private static final ByteBuffer bb_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   273
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   274
    private static void testNull(Base64.Encoder enc) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   275
        checkNull(() -> enc.encode(ba_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   276
        checkNull(() -> enc.encodeToString(ba_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   277
        checkNull(() -> enc.encode(ba_null, new byte[10]));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   278
        checkNull(() -> enc.encode(new byte[10], ba_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   279
        checkNull(() -> enc.encode(bb_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   280
        checkNull(() -> enc.wrap((OutputStream)null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   281
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   282
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   283
    private static void testNull(Base64.Decoder dec) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   284
        checkNull(() -> dec.decode(ba_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   285
        checkNull(() -> dec.decode(str_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   286
        checkNull(() -> dec.decode(ba_null, new byte[10]));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   287
        checkNull(() -> dec.decode(new byte[10], ba_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   288
        checkNull(() -> dec.decode(bb_null));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   289
        checkNull(() -> dec.wrap((InputStream)null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   290
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   291
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   292
    @FunctionalInterface
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   293
    private static interface Testable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   294
        public void test() throws Throwable;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   295
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   296
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   297
    private static void testIOE(Base64.Encoder enc) throws Throwable {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   298
        ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   299
        OutputStream os = enc.wrap(baos);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   300
        os.write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9});
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   301
        os.close();
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   302
        checkIOE(() -> os.write(10));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   303
        checkIOE(() -> os.write(new byte[] {10}));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   304
        checkIOE(() -> os.write(new byte[] {10}, 1, 4));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   305
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   306
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   307
    private static void testIOE(Base64.Decoder dec, byte[] decoded) throws Throwable {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   308
        ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   309
        InputStream is = dec.wrap(bais);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   310
        is.read(new byte[10]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   311
        is.close();
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   312
        checkIOE(() -> is.read());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   313
        checkIOE(() -> is.read(new byte[] {10}));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   314
        checkIOE(() -> is.read(new byte[] {10}, 1, 4));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   315
        checkIOE(() -> is.available());
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   316
        checkIOE(() -> is.skip(20));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   317
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   318
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   319
    private static final void checkNull(Runnable r) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   320
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   321
            r.run();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   322
            throw new RuntimeException("NPE is not thrown as expected");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   323
        } catch (NullPointerException npe) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   324
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   325
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   326
    private static final void checkIOE(Testable t) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   327
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   328
            t.test();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   329
            throw new RuntimeException("IOE is not thrown as expected");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   330
        } catch (IOException ioe) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   331
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   332
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   333
    private static final void checkIAE(Runnable r) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   334
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   335
            r.run();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   336
            throw new RuntimeException("IAE is not thrown as expected");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   337
        } catch (IllegalArgumentException iae) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   338
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   339
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   340
    private static void testDecodeIgnoredAfterPadding() throws Throwable {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   341
        for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   342
            byte[][] src = new byte[][] {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   343
                "A".getBytes("ascii"),
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   344
                "AB".getBytes("ascii"),
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   345
                "ABC".getBytes("ascii"),
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   346
                "ABCD".getBytes("ascii"),
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   347
                "ABCDE".getBytes("ascii")
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   348
            };
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   349
            Base64.Encoder encM = Base64.getMimeEncoder();
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   350
            Base64.Decoder decM = Base64.getMimeDecoder();
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   351
            Base64.Encoder enc = Base64.getEncoder();
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   352
            Base64.Decoder dec = Base64.getDecoder();
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   353
            for (int i = 0; i < src.length; i++) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   354
                // decode(byte[])
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   355
                byte[] encoded = encM.encode(src[i]);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   356
                encoded = Arrays.copyOf(encoded, encoded.length + 1);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   357
                encoded[encoded.length - 1] = nonBase64;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   358
                checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   359
                byte[] decoded = new byte[src[i].length];
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   360
                decM.decode(encoded, decoded);
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   361
                checkEqual(decoded, src[i], "Non-base64 char is not ignored");
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   362
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   363
                try {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   364
                    dec.decode(encoded);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   365
                    throw new RuntimeException("No IAE for non-base64 char");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   366
                } catch (IllegalArgumentException iae) {}
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   367
            }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   368
        }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   369
    }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   370
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   371
    private static void testMalformedPadding() throws Throwable {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   372
        Object[] data = new Object[] {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   373
            "$=#",       "",      0,      // illegal ending unit
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   374
            "A",         "",      0,      // dangling single byte
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   375
            "A=",        "",      0,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   376
            "A==",       "",      0,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   377
            "QUJDA",     "ABC",   4,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   378
            "QUJDA=",    "ABC",   4,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   379
            "QUJDA==",   "ABC",   4,
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   380
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   381
            "=",         "",      0,      // unnecessary padding
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   382
            "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   383
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   384
            "AA=",       "",      0,      // incomplete padding
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   385
            "QQ=",       "",      0,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   386
            "QQ=N",      "",      0,      // incorrect padding
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   387
            "QQ=?",      "",      0,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   388
            "QUJDQQ=",   "ABC",   4,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   389
            "QUJDQQ=N",  "ABC",   4,
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   390
            "QUJDQQ=?",  "ABC",   4,
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   391
        };
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   392
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   393
        Base64.Decoder[] decs = new Base64.Decoder[] {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   394
            Base64.getDecoder(),
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   395
            Base64.getUrlDecoder(),
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   396
            Base64.getMimeDecoder()
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   397
        };
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   398
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   399
        for (Base64.Decoder dec : decs) {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   400
            for (int i = 0; i < data.length; i += 3) {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   401
                final String srcStr = (String)data[i];
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   402
                final byte[] srcBytes = srcStr.getBytes("ASCII");
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   403
                final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   404
                byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   405
                int pos = (Integer)data[i + 2];
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   406
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   407
                // decode(byte[])
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   408
                checkIAE(() -> dec.decode(srcBytes));
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   409
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   410
                // decode(String)
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   411
                checkIAE(() -> dec.decode(srcStr));
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   412
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   413
                // decode(ByteBuffer)
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   414
                checkIAE(() -> dec.decode(srcBB));
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   415
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   416
                // wrap stream
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   417
                checkIOE(new Testable() {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   418
                    public void test() throws IOException {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   419
                        try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   420
                            while (is.read() != -1);
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   421
                        }
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   422
                }});
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   423
            }
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   424
        }
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   425
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   426
        // anything left after padding is "invalid"/IAE, if
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   427
        // not MIME. In case of MIME, non-base64 character(s)
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   428
        // is ignored.
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   429
        checkIAE(() -> Base64.getDecoder().decode("AA==\u00D2"));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   430
        checkIAE(() -> Base64.getUrlDecoder().decode("AA==\u00D2"));
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   431
        Base64.getMimeDecoder().decode("AA==\u00D2");
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   432
     }
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   433
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   434
    private static void  testDecodeUnpadded() throws Throwable {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   435
        byte[] srcA = new byte[] { 'Q', 'Q' };
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   436
        byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   437
        Base64.Decoder dec = Base64.getDecoder();
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   438
        byte[] ret = dec.decode(srcA);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   439
        if (ret[0] != 'A')
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   440
            throw new RuntimeException("Decoding unpadding input A failed");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   441
        ret = dec.decode(srcAA);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   442
        if (ret[0] != 'A' && ret[1] != 'A')
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   443
            throw new RuntimeException("Decoding unpadding input AA failed");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   444
        ret = new byte[10];
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   445
        if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   446
            ret[0] != 'A')
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   447
            throw new RuntimeException("Decoding unpadding input A from stream failed");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   448
        if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   449
            ret[0] != 'A' && ret[1] != 'A')
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   450
            throw new RuntimeException("Decoding unpadding input AA from stream failed");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   451
    }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   452
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   453
    // single-non-base64-char should be ignored for mime decoding, but
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   454
    // iae for basic decoding
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   455
    private static void testSingleNonBase64MimeDec() throws Throwable {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   456
        for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   457
            if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   458
                throw new RuntimeException("non-base64 char is not ignored");
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   459
            }
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   460
            try {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   461
                Base64.getDecoder().decode(nonBase64);
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   462
                throw new RuntimeException("No IAE for single non-base64 char");
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   463
            } catch (IllegalArgumentException iae) {}
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   464
        }
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   465
    }
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   466
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   467
    private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   468
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   469
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   470
        ByteBuffer bout = enc.encode(bin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   471
        byte[] buf = new byte[bout.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   472
        bout.get(buf);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   473
        if (bin.hasRemaining()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   474
            throw new RuntimeException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   475
                "Base64 enc.encode(ByteBuffer) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   476
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   477
        checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   478
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   479
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   480
    private static final void testDecode(Base64.Decoder dec, ByteBuffer bin, byte[] expected)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   481
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   482
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   483
        ByteBuffer bout = dec.decode(bin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   484
        byte[] buf = new byte[bout.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   485
        bout.get(buf);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   486
        checkEqual(buf, expected, "Base64 dec.decode(bf) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   487
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   488
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   489
    private static final void checkEqual(int v1, int v2, String msg)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   490
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   491
       if (v1 != v2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   492
           System.out.printf("    v1=%d%n", v1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   493
           System.out.printf("    v2=%d%n", v2);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   494
           throw new RuntimeException(msg);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   495
       }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   496
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   497
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   498
    private static final void checkEqual(byte[] r1, byte[] r2, String msg)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   499
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   500
       if (!Arrays.equals(r1, r2)) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   501
           System.out.printf("    r1[%d]=[%s]%n", r1.length, new String(r1));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   502
           System.out.printf("    r2[%d]=[%s]%n", r2.length, new String(r2));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   503
           throw new RuntimeException(msg);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   504
       }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   505
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   506
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   507
    // remove line feeds,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   508
    private static final byte[] normalize(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   509
        int n = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   510
        boolean hasUrl = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   511
        for (int i = 0; i < src.length; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   512
            if (src[i] == '\r' || src[i] == '\n')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   513
                n++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   514
            if (src[i] == '-' || src[i] == '_')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   515
                hasUrl = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   516
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   517
        if (n == 0 && hasUrl == false)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   518
            return src;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   519
        byte[] ret = new byte[src.length - n];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   520
        int j = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   521
        for (int i = 0; i < src.length; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   522
            if (src[i] == '-')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   523
                ret[j++] = '+';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   524
            else if (src[i] == '_')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   525
                ret[j++] = '/';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   526
            else if (src[i] != '\r' && src[i] != '\n')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   527
                ret[j++] = src[i];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   528
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   529
        return ret;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   530
    }
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   531
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   532
    private static void testEncoderKeepsSilence(Base64.Encoder enc)
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   533
            throws Throwable {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   534
        List<Integer> vals = new ArrayList<>(List.of(Integer.MIN_VALUE,
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   535
                Integer.MIN_VALUE + 1, -1111, -2, -1, 0, 1, 2, 3, 1111,
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   536
                Integer.MAX_VALUE - 1, Integer.MAX_VALUE));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   537
        vals.addAll(List.of(rnd.nextInt(), rnd.nextInt(), rnd.nextInt(),
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   538
                rnd.nextInt()));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   539
        byte[] buf = new byte[] {1, 0, 91};
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   540
        for (int off : vals) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   541
            for (int len : vals) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   542
                if (off >= 0 && len >= 0 && off <= buf.length - len) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   543
                    // valid args, skip them
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   544
                    continue;
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   545
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   546
                // invalid args, test them
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   547
                System.out.println("testing off=" + off + ", len=" + len);
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   548
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   549
                ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   550
                try (OutputStream os = enc.wrap(baos)) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   551
                    os.write(buf, off, len);
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   552
                    throw new RuntimeException("Expected IOOBEx was not thrown");
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   553
                } catch (IndexOutOfBoundsException expected) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   554
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   555
                if (baos.size() > 0)
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   556
                    throw new RuntimeException("No output was expected, but got "
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   557
                            + baos.size() + " bytes");
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   558
            }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   559
        }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   560
    }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   561
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   562
    private static void testDecoderKeepsAbstinence(Base64.Decoder dec)
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   563
            throws Throwable {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   564
        List<Integer> vals = new ArrayList<>(List.of(Integer.MIN_VALUE,
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   565
                Integer.MIN_VALUE + 1, -1111, -2, -1, 0, 1, 2, 3, 1111,
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   566
                Integer.MAX_VALUE - 1, Integer.MAX_VALUE));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   567
        vals.addAll(List.of(rnd.nextInt(), rnd.nextInt(), rnd.nextInt(),
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   568
                rnd.nextInt()));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   569
        byte[] buf = new byte[3];
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   570
        for (int off : vals) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   571
            for (int len : vals) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   572
                if (off >= 0 && len >= 0 && off <= buf.length - len) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   573
                    // valid args, skip them
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   574
                    continue;
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   575
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   576
                // invalid args, test them
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   577
                System.out.println("testing off=" + off + ", len=" + len);
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   578
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   579
                String input = "AAAAAAAAAAAAAAAAAAAAAA";
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   580
                ByteArrayInputStream bais =
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   581
                        new ByteArrayInputStream(input.getBytes("Latin1"));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   582
                try (InputStream is = dec.wrap(bais)) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   583
                    is.read(buf, off, len);
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   584
                    throw new RuntimeException("Expected IOOBEx was not thrown");
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   585
                } catch (IndexOutOfBoundsException expected) {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   586
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   587
                if (bais.available() != input.length())
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   588
                    throw new RuntimeException("No input should be consumed, "
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   589
                            + "but consumed " + (input.length() - bais.available())
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   590
                            + " bytes");
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   591
            }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   592
        }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   593
    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   594
}