test/jdk/java/util/Base64/TestBase64.java
author mli
Wed, 23 May 2018 14:21:14 +0800
changeset 50230 cae567ae015d
parent 48689 e704f48d8277
permissions -rw-r--r--
8202771: Migrate Unicode character tests to JDK Repo Reviewed-by: naoto Contributed-by: dan.z.zhou@oracle.com
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
/**
48519
482ede6c4936 8194959: Correct test tag to move bugid from @test to @bug
amlu
parents: 47216
diff changeset
    25
 * @test
482ede6c4936 8194959: Correct test tag to move bugid from @test to @bug
amlu
parents: 47216
diff changeset
    26
 * @bug 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
48689
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    27
 *      8014217 8025003 8026330 8028397 8129544 8165243 8176379
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    28
 * @summary tests java.util.Base64
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 40740
diff changeset
    29
 * @library /test/lib
45466
faf1c55d2046 8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents: 45288
diff changeset
    30
 * @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
    31
 * @run main TestBase64
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 26039
diff changeset
    32
 * @key randomness
14674
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    36
import java.io.ByteArrayOutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    37
import java.io.InputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    38
import java.io.IOException;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    39
import java.io.OutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    40
import java.nio.ByteBuffer;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    41
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
    42
import java.util.ArrayList;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    43
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
    44
import java.util.List;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    45
import java.util.Random;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    46
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 40740
diff changeset
    47
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
    48
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    49
public class TestBase64 {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    50
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
    51
    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
    52
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    53
    public static void main(String args[]) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    54
        int numRuns  = 10;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    55
        int numBytes = 200;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    56
        if (args.length > 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    57
            numRuns  = Integer.parseInt(args[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    58
            numBytes = Integer.parseInt(args[1]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    59
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    60
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    61
        test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    62
        test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
    63
        test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    64
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    65
        byte[] nl_1 = new byte[] {'\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    66
        byte[] nl_2 = new byte[] {'\n', '\r'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    67
        byte[] nl_3 = new byte[] {'\n', '\r', '\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    68
        for (int i = 0; i < 10; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    69
            int len = rnd.nextInt(200) + 4;
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    70
            test(Base64.getMimeEncoder(len, nl_1),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    71
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    72
                 numRuns, numBytes);
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    73
            test(Base64.getMimeEncoder(len, nl_2),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    74
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    75
                 numRuns, numBytes);
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    76
            test(Base64.getMimeEncoder(len, nl_3),
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    77
                 Base64.getMimeDecoder(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    78
                 numRuns, numBytes);
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
48689
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    81
        // test mime case with < 4 length
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    82
        for (int len = 0; len < 4; len++) {
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    83
            test(Base64.getMimeEncoder(len, nl_1),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    84
                 Base64.getMimeDecoder(),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    85
                 numRuns, numBytes);
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    86
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    87
            test(Base64.getMimeEncoder(len, nl_2),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    88
                 Base64.getMimeDecoder(),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    89
                 numRuns, numBytes);
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    90
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    91
            test(Base64.getMimeEncoder(len, nl_3),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    92
                 Base64.getMimeDecoder(),
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    93
                 numRuns, numBytes);
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    94
        }
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 48519
diff changeset
    95
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    96
        testNull(Base64.getEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    97
        testNull(Base64.getUrlEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    98
        testNull(Base64.getMimeEncoder());
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
    99
        testNull(Base64.getMimeEncoder(10, new byte[]{'\n'}));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   100
        testNull(Base64.getDecoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   101
        testNull(Base64.getUrlDecoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   102
        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
   103
        checkNull(() -> Base64.getMimeEncoder(10, null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   104
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   105
        testIOE(Base64.getEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   106
        testIOE(Base64.getUrlEncoder());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   107
        testIOE(Base64.getMimeEncoder());
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   108
        testIOE(Base64.getMimeEncoder(10, new byte[]{'\n'}));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   109
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   110
        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
   111
        rnd.nextBytes(src);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   112
        final byte[] decoded = Base64.getEncoder().encode(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   113
        testIOE(Base64.getDecoder(), decoded);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   114
        testIOE(Base64.getMimeDecoder(), decoded);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   115
        testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   116
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   117
        // 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
   118
        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
   119
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
   120
        // 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
   121
        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
   122
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   123
        // illegal base64 character
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   124
        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
   125
        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
   126
        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
   127
        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
   128
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   129
        // 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
   130
        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
   131
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
   132
        // 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
   133
        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
   134
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
   135
        // 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
   136
        testDecodeIgnoredAfterPadding();
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   137
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   138
        // 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
   139
        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
   140
        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
   141
        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
   142
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   143
        // 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
   144
        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
   145
        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
   146
        testDecoderKeepsAbstinence(Base64.getMimeDecoder());
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   147
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   148
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   149
    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
   150
                             int numRuns, int numBytes) throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   151
        enc.encode(new byte[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   152
        dec.decode(new byte[0]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   153
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   154
        for (boolean withoutPadding : new boolean[] { false, true}) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   155
            if (withoutPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   156
                 enc = enc.withoutPadding();
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
            for (int i=0; i<numRuns; i++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   159
                for (int j=1; j<numBytes; j++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   160
                    byte[] orig = new byte[j];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   161
                    rnd.nextBytes(orig);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   162
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   163
                    // --------testing encode/decode(byte[])--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   164
                    byte[] encoded = enc.encode(orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   165
                    byte[] decoded = dec.decode(encoded);
14674
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
                    checkEqual(orig, decoded,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   168
                               "Base64 array encoding/decoding failed!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   169
                    if (withoutPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   170
                        if (encoded[encoded.length - 1] == '=')
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   171
                            throw new RuntimeException(
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   172
                               "Base64 enc.encode().withoutPadding() has padding!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   173
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   174
                    // --------testing encodetoString(byte[])/decode(String)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   175
                    String str = enc.encodeToString(orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   176
                    if (!Arrays.equals(str.getBytes("ASCII"), encoded)) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   177
                        throw new RuntimeException(
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   178
                            "Base64 encodingToString() failed!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   179
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   180
                    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
   181
                    checkEqual(buf, orig, "Base64 decoding(String) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   182
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   183
                    //-------- testing encode/decode(Buffer)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   184
                    testEncode(enc, ByteBuffer.wrap(orig), encoded);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   185
                    ByteBuffer bin = ByteBuffer.allocateDirect(orig.length);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   186
                    bin.put(orig).flip();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   187
                    testEncode(enc, bin, encoded);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   188
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   189
                    testDecode(dec, ByteBuffer.wrap(encoded), orig);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   190
                    bin = ByteBuffer.allocateDirect(encoded.length);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   191
                    bin.put(encoded).flip();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   192
                    testDecode(dec, bin, orig);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   193
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   194
                    // --------testing decode.wrap(input stream)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   195
                    // 1) random buf length
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   196
                    ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   197
                    InputStream is = dec.wrap(bais);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   198
                    buf = new byte[orig.length + 10];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   199
                    int len = orig.length;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   200
                    int off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   201
                    while (true) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   202
                        int n = rnd.nextInt(len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   203
                        if (n == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   204
                            n = 1;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   205
                        n = is.read(buf, off, n);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   206
                        if (n == -1) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   207
                            checkEqual(off, orig.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   208
                                       "Base64 stream decoding failed");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   209
                            break;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   210
                        }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   211
                        off += n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   212
                        len -= n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   213
                        if (len == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   214
                            break;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   215
                    }
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   216
                    buf = Arrays.copyOf(buf, off);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   217
                    checkEqual(buf, orig, "Base64 stream decoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   218
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   219
                    // 2) read one byte each
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   220
                    bais.reset();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   221
                    is = dec.wrap(bais);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   222
                    buf = new byte[orig.length + 10];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   223
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   224
                    int b;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   225
                    while ((b = is.read()) != -1) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   226
                        buf[off++] = (byte)b;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   227
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   228
                    buf = Arrays.copyOf(buf, off);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   229
                    checkEqual(buf, orig, "Base64 stream decoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   230
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   231
                    // --------testing encode.wrap(output stream)--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   232
                    ByteArrayOutputStream baos = new ByteArrayOutputStream((orig.length + 2) / 3 * 4 + 10);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   233
                    OutputStream os = enc.wrap(baos);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   234
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   235
                    len = orig.length;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   236
                    for (int k = 0; k < 5; k++) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   237
                        if (len == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   238
                            break;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   239
                        int n = rnd.nextInt(len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   240
                        if (n == 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   241
                            n = 1;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   242
                        os.write(orig, off, n);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   243
                        off += n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   244
                        len -= n;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   245
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   246
                    if (len != 0)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   247
                        os.write(orig, off, len);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   248
                    os.close();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   249
                    buf = baos.toByteArray();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   250
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   251
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   252
                    // 2) write one byte each
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   253
                    baos.reset();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   254
                    os = enc.wrap(baos);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   255
                    off = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   256
                    while (off < orig.length) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   257
                        os.write(orig[off++]);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   258
                    }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   259
                    os.close();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   260
                    buf = baos.toByteArray();
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   261
                    checkEqual(buf, encoded, "Base64 stream encoding failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   262
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   263
                    // --------testing encode(in, out); -> bigger buf--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   264
                    buf = new byte[encoded.length + rnd.nextInt(100)];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   265
                    int ret = enc.encode(orig, buf);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   266
                    checkEqual(ret, encoded.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   267
                               "Base64 enc.encode(src, null) returns wrong size!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   268
                    buf = Arrays.copyOf(buf, ret);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   269
                    checkEqual(buf, encoded,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   270
                               "Base64 enc.encode(src, dst) failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   271
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   272
                    // --------testing decode(in, out); -> bigger buf--------
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   273
                    buf = new byte[orig.length + rnd.nextInt(100)];
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   274
                    ret = dec.decode(encoded, buf);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   275
                    checkEqual(ret, orig.length,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   276
                              "Base64 enc.encode(src, null) returns wrong size!");
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   277
                    buf = Arrays.copyOf(buf, ret);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   278
                    checkEqual(buf, orig,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   279
                               "Base64 dec.decode(src, dst) failed!");
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   280
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   281
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   282
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   283
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   284
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   285
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   286
    private static final byte[] ba_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   287
    private static final String str_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   288
    private static final ByteBuffer bb_null = null;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   289
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   290
    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
   291
        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
   292
        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
   293
        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
   294
        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
   295
        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
   296
        checkNull(() -> enc.wrap((OutputStream)null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   297
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   298
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   299
    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
   300
        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
   301
        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
   302
        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
   303
        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
   304
        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
   305
        checkNull(() -> dec.wrap((InputStream)null));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   306
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   307
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   308
    @FunctionalInterface
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   309
    private static interface Testable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   310
        public void test() throws Throwable;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   311
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   312
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   313
    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
   314
        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
   315
        OutputStream os = enc.wrap(baos);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   316
        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
   317
        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
   318
        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
   319
        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
   320
        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
   321
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   322
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   323
    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
   324
        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
   325
        InputStream is = dec.wrap(bais);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   326
        is.read(new byte[10]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   327
        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
   328
        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
   329
        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
   330
        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
   331
        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
   332
        checkIOE(() -> is.skip(20));
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   333
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   334
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   335
    private static final void checkNull(Runnable r) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   336
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   337
            r.run();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   338
            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
   339
        } catch (NullPointerException npe) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   340
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   341
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   342
    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
   343
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   344
            t.test();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   345
            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
   346
        } catch (IOException ioe) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   347
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   348
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   349
    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
   350
        try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   351
            r.run();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   352
            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
   353
        } catch (IllegalArgumentException iae) {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   354
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   355
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
   356
    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
   357
        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
   358
            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
   359
                "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
   360
                "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
   361
                "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
   362
                "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
   363
                "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
   364
            };
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
            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
   366
            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
   367
            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
   368
            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
   369
            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
   370
                // 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
   371
                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
   372
                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
   373
                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
   374
                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
   375
                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
   376
                decM.decode(encoded, decoded);
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   377
                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
   378
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
   379
                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
   380
                    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
   381
                    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
   382
                } 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
   383
            }
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
   384
        }
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
   385
    }
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
   386
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
   387
    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
   388
        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
   389
            "$=#",       "",      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
   390
            "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
   391
            "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
   392
            "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
   393
            "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
   394
            "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
   395
            "QUJDA==",   "ABC",   4,
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   396
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
   397
            "=",         "",      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
   398
            "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   399
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
   400
            "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
   401
            "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
   402
            "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
   403
            "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
   404
            "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
   405
            "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
   406
            "QUJDQQ=?",  "ABC",   4,
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   407
        };
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   408
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
        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
   410
            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
   411
            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
   412
            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
   413
        };
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   414
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
   415
        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
   416
            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
   417
                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
   418
                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
   419
                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
   420
                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
   421
                int pos = (Integer)data[i + 2];
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   422
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
   423
                // 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
   424
                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
   425
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   426
                // 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
   427
                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
   428
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   429
                // 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
   430
                checkIAE(() -> dec.decode(srcBB));
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   431
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
   432
                // 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
   433
                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
   434
                    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
   435
                        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
   436
                            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
   437
                        }
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   438
                }});
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   439
            }
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   440
        }
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   441
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   442
        // anything left after padding is "invalid"/IAE, if
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   443
        // not MIME. In case of MIME, non-base64 character(s)
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   444
        // 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
   445
        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
   446
        checkIAE(() -> Base64.getUrlDecoder().decode("AA==\u00D2"));
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   447
        Base64.getMimeDecoder().decode("AA==\u00D2");
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 30046
diff changeset
   448
     }
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 17471
diff changeset
   449
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
   450
    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
   451
        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
   452
        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
   453
        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
   454
        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
   455
        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
   456
            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
   457
        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
   458
        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
   459
            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
   460
        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
   461
        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
   462
            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
   463
            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
   464
        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
   465
            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
   466
            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
   467
    }
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
   468
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   469
    // 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
   470
    // iae for basic decoding
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   471
    private static void testSingleNonBase64MimeDec() throws Throwable {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   472
        for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   473
            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
   474
                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
   475
            }
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   476
            try {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   477
                Base64.getDecoder().decode(nonBase64);
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   478
                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
   479
            } catch (IllegalArgumentException iae) {}
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   480
        }
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   481
    }
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   482
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   483
    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
   484
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   485
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   486
        ByteBuffer bout = enc.encode(bin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   487
        byte[] buf = new byte[bout.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   488
        bout.get(buf);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   489
        if (bin.hasRemaining()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   490
            throw new RuntimeException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   491
                "Base64 enc.encode(ByteBuffer) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   492
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   493
        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
   494
    }
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
    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
   497
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   498
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   499
        ByteBuffer bout = dec.decode(bin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   500
        byte[] buf = new byte[bout.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   501
        bout.get(buf);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   502
        checkEqual(buf, expected, "Base64 dec.decode(bf) failed!");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   503
    }
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
    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
   506
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   507
       if (v1 != v2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   508
           System.out.printf("    v1=%d%n", v1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   509
           System.out.printf("    v2=%d%n", v2);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   510
           throw new RuntimeException(msg);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   511
       }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   512
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   513
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   514
    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
   515
        throws Throwable {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   516
       if (!Arrays.equals(r1, r2)) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   517
           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
   518
           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
   519
           throw new RuntimeException(msg);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   520
       }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   521
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   522
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   523
    // remove line feeds,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   524
    private static final byte[] normalize(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   525
        int n = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   526
        boolean hasUrl = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   527
        for (int i = 0; i < src.length; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   528
            if (src[i] == '\r' || src[i] == '\n')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   529
                n++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   530
            if (src[i] == '-' || src[i] == '_')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   531
                hasUrl = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   532
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   533
        if (n == 0 && hasUrl == false)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   534
            return src;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   535
        byte[] ret = new byte[src.length - n];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   536
        int j = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   537
        for (int i = 0; i < src.length; i++) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   538
            if (src[i] == '-')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   539
                ret[j++] = '+';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   540
            else if (src[i] == '_')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   541
                ret[j++] = '/';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   542
            else if (src[i] != '\r' && src[i] != '\n')
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   543
                ret[j++] = src[i];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   544
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   545
        return ret;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   546
    }
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   547
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   548
    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
   549
            throws Throwable {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   550
        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
   551
                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
   552
                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
   553
        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
   554
                rnd.nextInt()));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   555
        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
   556
        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
   557
            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
   558
                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
   559
                    // 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
   560
                    continue;
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
                // 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
   563
                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
   564
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   565
                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
   566
                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
   567
                    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
   568
                    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
   569
                } 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
   570
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   571
                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
   572
                    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
   573
                            + 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
   574
            }
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
    }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   577
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   578
    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
   579
            throws Throwable {
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   580
        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
   581
                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
   582
                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
   583
        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
   584
                rnd.nextInt()));
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   585
        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
   586
        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
   587
            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
   588
                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
   589
                    // 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
   590
                    continue;
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
                // 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
   593
                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
   594
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   595
                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
   596
                ByteArrayInputStream bais =
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   597
                        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
   598
                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
   599
                    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
   600
                    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
   601
                } 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
   602
                }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   603
                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
   604
                    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
   605
                            + "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
   606
                            + " bytes");
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   607
            }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   608
        }
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   609
    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   610
}