Path: blob/master/test/jdk/java/util/Base64/TestBase64Golden.java
41149 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 423551926* @author Eric Wang <[email protected]>27* @summary tests java.util.Base6428*/2930import java.io.BufferedReader;31import java.io.FileReader;32import java.nio.ByteBuffer;33import java.nio.charset.Charset;34import java.nio.charset.StandardCharsets;35import java.nio.file.Files;36import java.nio.file.Paths;37import java.util.Base64;38import java.util.Base64.Decoder;39import java.util.Base64.Encoder;40import java.util.Objects;41import java.util.Random;4243public class TestBase64Golden {4445public static void main(String[] args) throws Exception {46test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),47"plain.txt", "baseEncode.txt");4849test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),50"plain.txt", "urlEncode.txt");5152test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),53"plain.txt", "mimeEncode.txt");54test1();55}5657public static void test0(Base64Type type, Encoder encoder, Decoder decoder,58String srcFile, String encodedFile) throws Exception {5960String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)61.toArray(new String[0]);62String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),63DEF_CHARSET)64.toArray(new String[0]);65int lns = 0;66for (String srcStr : srcLns) {67String encodedStr = null;68if (type != Base64Type.MIME) {69encodedStr = encodedLns[lns++];70} else {71while (lns < encodedLns.length) {72String s = encodedLns[lns++];73if (s.length() == 0)74break;75if (encodedStr != null) {76encodedStr += DEFAULT_CRLF + s;77} else {78encodedStr = s;79}80}81if (encodedStr == null && srcStr.length() == 0) {82encodedStr = "";83}84}85System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr);86System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr);8788byte[] srcArr = srcStr.getBytes(DEF_CHARSET);89byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);9091ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);92ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);93byte[] resArr = new byte[encodedArr.length];9495// test int encode(byte[], byte[])96int len = encoder.encode(srcArr, resArr);97assertEqual(len, encodedArr.length);98assertEqual(resArr, encodedArr);99100// test byte[] encode(byte[])101resArr = encoder.encode(srcArr);102assertEqual(resArr, encodedArr);103104// test ByteBuffer encode(ByteBuffer)105int limit = srcBuf.limit();106ByteBuffer resBuf = encoder.encode(srcBuf);107assertEqual(srcBuf.position(), limit);108assertEqual(srcBuf.limit(), limit);109assertEqual(resBuf, encodedBuf);110srcBuf.rewind(); // reset for next test111112// test String encodeToString(byte[])113String resEncodeStr = encoder.encodeToString(srcArr);114assertEqual(resEncodeStr, encodedStr);115116// test int decode(byte[], byte[])117resArr = new byte[srcArr.length];118len = decoder.decode(encodedArr, resArr);119assertEqual(len, srcArr.length);120assertEqual(resArr, srcArr);121122// test byte[] decode(byte[])123resArr = decoder.decode(encodedArr);124assertEqual(resArr, srcArr);125126// test ByteBuffer decode(ByteBuffer)127limit = encodedBuf.limit();128resBuf = decoder.decode(encodedBuf);129assertEqual(encodedBuf.position(), limit);130assertEqual(encodedBuf.limit(), limit);131assertEqual(resBuf, srcBuf);132encodedBuf.rewind(); // reset for next test133134// test byte[] decode(String)135resArr = decoder.decode(encodedStr);136assertEqual(resArr, srcArr);137138}139}140141private static void test1() throws Exception {142byte[] src = new byte[] {14346, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,14440, 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 };146Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });147byte[] encoded = encoder.encode(src);148Decoder decoder = Base64.getMimeDecoder();149byte[] decoded = decoder.decode(encoded);150if (!Objects.deepEquals(src, decoded)) {151throw new RuntimeException();152}153}154155// helper156enum Base64Type {157BASIC, URLSAFE, MIME158}159160private static final String SRCDIR = System.getProperty("test.src", ".");161private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;162private static final String DEF_EXCEPTION_MSG =163"Assertion failed! The result is not same as expected";164private static final String DEFAULT_CRLF = "\r\n";165166private static void assertEqual(Object result, Object expect) {167if (!Objects.deepEquals(result, expect)) {168String resultStr = result.toString();169String expectStr = expect.toString();170if (result instanceof byte[]) {171resultStr = new String((byte[]) result, DEF_CHARSET);172}173if (expect instanceof byte[]) {174expectStr = new String((byte[]) expect, DEF_CHARSET);175}176throw new RuntimeException(DEF_EXCEPTION_MSG +177" result: " + resultStr + " expected: " + expectStr);178}179}180}181182183