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 4235519 26 * @author Eric Wang <yiming.wang@oracle.com> 27 * @summary tests java.util.Base64 28 */ 29 30 import java.io.BufferedReader; 31 import java.io.FileReader; 32 import java.nio.ByteBuffer; 33 import java.nio.charset.Charset; 34 import java.nio.charset.StandardCharsets; 35 import java.nio.file.Files; 36 import java.nio.file.Paths; 37 import java.util.Base64; 38 import java.util.Base64.Decoder; 39 import java.util.Base64.Encoder; 40 import java.util.Objects; 41 import java.util.Random; 42 43 public class TestBase64Golden { 44 45 public static void main(String[] args) throws Exception { 46 test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(), 47 "plain.txt", "baseEncode.txt"); 48 49 test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(), 50 "plain.txt", "urlEncode.txt"); 51 52 test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(), 53 "plain.txt", "mimeEncode.txt"); 54 test1(); 55 } 56 57 public static void test0(Base64Type type, Encoder encoder, Decoder decoder, 58 String srcFile, String encodedFile) throws Exception { 59 60 String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) 61 .toArray(new String[0]); 62 String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), 63 DEF_CHARSET) 64 .toArray(new String[0]); 65 int lns = 0; 66 for (String srcStr : srcLns) { 67 String encodedStr = null; 68 if (type != Base64Type.MIME) { 69 encodedStr = encodedLns[lns++]; 70 } else { 71 while (lns < encodedLns.length) { 72 String s = encodedLns[lns++]; 73 if (s.length() == 0) 74 break; 75 if (encodedStr != null) { 76 encodedStr += DEFAULT_CRLF + s; 77 } else { 78 encodedStr = s; 79 } 80 } 81 if (encodedStr == null && srcStr.length() == 0) { 82 encodedStr = ""; 83 } 84 } 85 System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); 86 System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); 87 88 byte[] srcArr = srcStr.getBytes(DEF_CHARSET); 89 byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); 90 91 ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); 92 ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); 93 byte[] resArr = new byte[encodedArr.length]; 94 95 // test int encode(byte[], byte[]) 96 int len = encoder.encode(srcArr, resArr); 97 assertEqual(len, encodedArr.length); 98 assertEqual(resArr, encodedArr); 99 100 // test byte[] encode(byte[]) 101 resArr = encoder.encode(srcArr); 102 assertEqual(resArr, encodedArr); 103 104 // test ByteBuffer encode(ByteBuffer) 105 int limit = srcBuf.limit(); 106 ByteBuffer resBuf = encoder.encode(srcBuf); 107 assertEqual(srcBuf.position(), limit); 108 assertEqual(srcBuf.limit(), limit); 109 assertEqual(resBuf, encodedBuf); 110 srcBuf.rewind(); // reset for next test 111 112 // test String encodeToString(byte[]) 113 String resEncodeStr = encoder.encodeToString(srcArr); 114 assertEqual(resEncodeStr, encodedStr); 115 116 // test int decode(byte[], byte[]) 117 resArr = new byte[srcArr.length]; 118 len = decoder.decode(encodedArr, resArr); 119 assertEqual(len, srcArr.length); 120 assertEqual(resArr, srcArr); 121 122 // test byte[] decode(byte[]) 123 resArr = decoder.decode(encodedArr); 124 assertEqual(resArr, srcArr); 125 126 // test ByteBuffer decode(ByteBuffer) 127 limit = encodedBuf.limit(); 128 resBuf = decoder.decode(encodedBuf); 129 assertEqual(encodedBuf.position(), limit); 130 assertEqual(encodedBuf.limit(), limit); 131 assertEqual(resBuf, srcBuf); 132 encodedBuf.rewind(); // reset for next test 133 134 // test byte[] decode(String) 135 resArr = decoder.decode(encodedStr); 136 assertEqual(resArr, srcArr); 137 138 } 139 } 140 141 private static void test1() throws Exception { 142 byte[] src = new byte[] { 143 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 144 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, 145 -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; 146 Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); 147 byte[] encoded = encoder.encode(src); 148 Decoder decoder = Base64.getMimeDecoder(); 149 byte[] decoded = decoder.decode(encoded); 150 if (!Objects.deepEquals(src, decoded)) { 151 throw new RuntimeException(); 152 } 153 } 154 155 // helper 156 enum Base64Type { 157 BASIC, URLSAFE, MIME 158 } 159 160 private static final String SRCDIR = System.getProperty("test.src", "."); 161 private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII; 162 private static final String DEF_EXCEPTION_MSG = 163 "Assertion failed! The result is not same as expected"; 164 private static final String DEFAULT_CRLF = "\r\n"; 165 166 private static void assertEqual(Object result, Object expect) { 167 if (!Objects.deepEquals(result, expect)) { 168 String resultStr = result.toString(); 169 String expectStr = expect.toString(); 170 if (result instanceof byte[]) { 171 resultStr = new String((byte[]) result, DEF_CHARSET); 172 } 173 if (expect instanceof byte[]) { 174 expectStr = new String((byte[]) expect, DEF_CHARSET); 175 } 176 throw new RuntimeException(DEF_EXCEPTION_MSG + 177 " result: " + resultStr + " expected: " + expectStr); 178 } 179 } 180 }