1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4235519
  27  * @author Eric Wang <yiming.wang@oracle.com>
  28  * @summary tests java.util.Base64
  29  */
  30 
  31 import java.io.BufferedReader;
  32 import java.io.FileReader;
  33 import java.nio.ByteBuffer;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.StandardCharsets;
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 import java.util.Base64;
  39 import java.util.Base64.Decoder;
  40 import java.util.Base64.Encoder;
  41 import java.util.Objects;
  42 import java.util.Random;
  43 
  44 public class TestBase64Golden {
  45 
  46     public static void main(String[] args) throws Exception {
  47         test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),
  48               "plain.txt", "baseEncode.txt");
  49 
  50         test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),
  51               "plain.txt", "urlEncode.txt");
  52 
  53         test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),
  54               "plain.txt", "mimeEncode.txt");
  55         test1();
  56     }
  57 
  58     public static void test0(Base64Type type, Encoder encoder, Decoder decoder,
  59                              String srcFile, String encodedFile) throws Exception {
  60 
  61         String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)
  62                                .toArray(new String[0]);
  63         String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),
  64                                                  DEF_CHARSET)
  65                                    .toArray(new String[0]);
  66         int lns = 0;
  67         for (String srcStr : srcLns) {
  68             String encodedStr = null;
  69             if (type != Base64Type.MIME) {
  70                 encodedStr = encodedLns[lns++];
  71             } else {
  72                 while (lns < encodedLns.length) {
  73                     String s = encodedLns[lns++];
  74                     if (s.length() == 0)
  75                         break;
  76                     if (encodedStr != null) {
  77                         encodedStr += DEFAULT_CRLF + s;
  78                     } else {
  79                         encodedStr = s;
  80                     }
  81                 }
  82                 if (encodedStr == null && srcStr.length() == 0) {
  83                     encodedStr = "";
  84                 }
  85             }
  86             System.out.printf("%n    src[%d]: %s%n", srcStr.length(), srcStr);
  87             System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr);
  88 
  89             byte[] srcArr = srcStr.getBytes(DEF_CHARSET);
  90             byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);
  91 
  92             ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);
  93             ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
  94             byte[] resArr = new byte[encodedArr.length];
  95 
  96             // test int encode(byte[], byte[])
  97             int len = encoder.encode(srcArr, resArr);
  98             assertEqual(len, encodedArr.length);
  99             assertEqual(resArr, encodedArr);
 100 
 101             // test byte[] encode(byte[])
 102             resArr = encoder.encode(srcArr);
 103             assertEqual(resArr, encodedArr);
 104 
 105             // test ByteBuffer encode(ByteBuffer)
 106             int limit = srcBuf.limit();
 107             ByteBuffer resBuf = encoder.encode(srcBuf);
 108             assertEqual(srcBuf.position(), limit);
 109             assertEqual(srcBuf.limit(), limit);
 110             assertEqual(resBuf, encodedBuf);
 111             srcBuf.rewind(); // reset for next test
 112 
 113             // test String encodeToString(byte[])
 114             String resEncodeStr = encoder.encodeToString(srcArr);
 115             assertEqual(resEncodeStr, encodedStr);
 116 
 117             // test int decode(byte[], byte[])
 118             resArr = new byte[srcArr.length];
 119             len = decoder.decode(encodedArr, resArr);
 120             assertEqual(len, srcArr.length);
 121             assertEqual(resArr, srcArr);
 122 
 123             // test byte[] decode(byte[])
 124             resArr = decoder.decode(encodedArr);
 125             assertEqual(resArr, srcArr);
 126 
 127             // test ByteBuffer decode(ByteBuffer)
 128             limit = encodedBuf.limit();
 129             resBuf = decoder.decode(encodedBuf);
 130             assertEqual(encodedBuf.position(), limit);
 131             assertEqual(encodedBuf.limit(), limit);
 132             assertEqual(resBuf, srcBuf);
 133             encodedBuf.rewind(); // reset for next test
 134 
 135             // test byte[] decode(String)
 136             resArr = decoder.decode(encodedStr);
 137             assertEqual(resArr, srcArr);
 138 
 139         }
 140     }
 141 
 142     private static void test1() throws Exception {
 143         byte[] src = new byte[] {
 144             46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
 145             40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
 146             -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
 147         Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
 148         byte[] encoded = encoder.encode(src);
 149         Decoder decoder = Base64.getMimeDecoder();
 150         byte[] decoded = decoder.decode(encoded);
 151         if (!Objects.deepEquals(src, decoded)) {
 152             throw new RuntimeException();
 153         }
 154     }
 155 
 156     // helper
 157     enum Base64Type {
 158         BASIC, URLSAFE, MIME
 159     }
 160 
 161     private static final String SRCDIR = System.getProperty("test.src", ".");
 162     private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;
 163     private static final String DEF_EXCEPTION_MSG =
 164         "Assertion failed! The result is not same as expected";
 165     private static final String DEFAULT_CRLF = "\r\n";
 166 
 167     private static void assertEqual(Object result, Object expect) {
 168         if (!Objects.deepEquals(result, expect)) {
 169             String resultStr = result.toString();
 170             String expectStr = expect.toString();
 171             if (result instanceof byte[]) {
 172                 resultStr = new String((byte[]) result, DEF_CHARSET);
 173             }
 174             if (expect instanceof byte[]) {
 175                 expectStr = new String((byte[]) expect, DEF_CHARSET);
 176             }
 177             throw new RuntimeException(DEF_EXCEPTION_MSG +
 178                 " result: " + resultStr + " expected: " + expectStr);
 179         }
 180     }
 181 }