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