Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Base64/TestBase64Golden.java
38812 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* @test 423551925* @author Eric Wang <[email protected]>26* @summary tests java.util.Base6427*/2829import java.io.BufferedReader;30import java.io.FileReader;31import java.nio.ByteBuffer;32import java.nio.charset.Charset;33import java.nio.charset.StandardCharsets;34import java.nio.file.Files;35import java.nio.file.Paths;36import java.util.Base64;37import java.util.Base64.Decoder;38import java.util.Base64.Encoder;39import java.util.Objects;40import java.util.Random;4142import sun.misc.BASE64Decoder;43import sun.misc.BASE64Encoder;4445public class TestBase64Golden {4647public static void main(String[] args) throws Exception {48test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),49"plain.txt", "baseEncode.txt");5051test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),52"plain.txt", "urlEncode.txt");5354test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),55"plain.txt", "mimeEncode.txt");56test1();57}5859public static void test0(Base64Type type, Encoder encoder, Decoder decoder,60String srcFile, String encodedFile) throws Exception {6162String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)63.toArray(new String[0]);64String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),65DEF_CHARSET)66.toArray(new String[0]);67int lns = 0;68for (String srcStr : srcLns) {69String encodedStr = null;70if (type != Base64Type.MIME) {71encodedStr = encodedLns[lns++];72} else {73while (lns < encodedLns.length) {74String s = encodedLns[lns++];75if (s.length() == 0)76break;77if (encodedStr != null) {78encodedStr += DEFAULT_CRLF + s;79} else {80encodedStr = s;81}82}83if (encodedStr == null && srcStr.length() == 0) {84encodedStr = "";85}86}87System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr);88System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr);8990byte[] srcArr = srcStr.getBytes(DEF_CHARSET);91byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);9293ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);94ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);95byte[] resArr = new byte[encodedArr.length];9697// test int encode(byte[], byte[])98int len = encoder.encode(srcArr, resArr);99assertEqual(len, encodedArr.length);100assertEqual(resArr, encodedArr);101102// test byte[] encode(byte[])103resArr = encoder.encode(srcArr);104assertEqual(resArr, encodedArr);105106// test ByteBuffer encode(ByteBuffer)107int limit = srcBuf.limit();108ByteBuffer resBuf = encoder.encode(srcBuf);109assertEqual(srcBuf.position(), limit);110assertEqual(srcBuf.limit(), limit);111assertEqual(resBuf, encodedBuf);112srcBuf.rewind(); // reset for next test113114// test String encodeToString(byte[])115String resEncodeStr = encoder.encodeToString(srcArr);116assertEqual(resEncodeStr, encodedStr);117118// test int decode(byte[], byte[])119resArr = new byte[srcArr.length];120len = decoder.decode(encodedArr, resArr);121assertEqual(len, srcArr.length);122assertEqual(resArr, srcArr);123124// test byte[] decode(byte[])125resArr = decoder.decode(encodedArr);126assertEqual(resArr, srcArr);127128// test ByteBuffer decode(ByteBuffer)129limit = encodedBuf.limit();130resBuf = decoder.decode(encodedBuf);131assertEqual(encodedBuf.position(), limit);132assertEqual(encodedBuf.limit(), limit);133assertEqual(resBuf, srcBuf);134encodedBuf.rewind(); // reset for next test135136// test byte[] decode(String)137resArr = decoder.decode(encodedStr);138assertEqual(resArr, srcArr);139140// test compatible with sun.misc.Base64Encoder141if (type == Base64Type.MIME) {142sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder();143sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder();144resArr = decoder.decode(miscEncoder.encode(srcArr));145assertEqual(resArr, srcArr);146147resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr));148assertEqual(new String(resArr, DEF_CHARSET), encodedStr);149}150}151}152153private static void test1() throws Exception {154byte[] src = new byte[] {15546, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,15640, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,157-80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };158Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });159byte[] encoded = encoder.encode(src);160Decoder decoder = Base64.getMimeDecoder();161byte[] decoded = decoder.decode(encoded);162if (!Objects.deepEquals(src, decoded)) {163throw new RuntimeException();164}165}166167// helper168enum Base64Type {169BASIC, URLSAFE, MIME170}171172private static final String SRCDIR = System.getProperty("test.src", ".");173private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;174private static final String DEF_EXCEPTION_MSG =175"Assertion failed! The result is not same as expected";176private static final String DEFAULT_CRLF = "\r\n";177178private static void assertEqual(Object result, Object expect) {179if (!Objects.deepEquals(result, expect)) {180String resultStr = result.toString();181String expectStr = expect.toString();182if (result instanceof byte[]) {183resultStr = new String((byte[]) result, DEF_CHARSET);184}185if (expect instanceof byte[]) {186expectStr = new String((byte[]) expect, DEF_CHARSET);187}188throw new RuntimeException(DEF_EXCEPTION_MSG +189" result: " + resultStr + " expected: " + expectStr);190}191}192}193194195