blob: c00e2efd091a4add6e6b74e15ab2014154e5f772 [file] [log] [blame]
msheppar8f5d1aa2013-04-30 16:24:08 +01001/*
2 * Copyright (c) 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
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.OutputStream;
29import java.nio.ByteBuffer;
30
31import java.util.Base64;
32
33import static java.nio.charset.StandardCharsets.US_ASCII;
34
35/**
36 * @test
37 * @bug 8007799
38 * @summary test Encoder with linemax == 0, line separator should not appear in encoded data
39 */
40
41public class Base64GetEncoderTest {
42
43 public static void main(String args[]) throws Throwable {
44 final Base64.Encoder encoder = Base64.getEncoder(0, "$$$".getBytes(US_ASCII));
45
46 testEncodeToString(encoder);
47
48 testWrapEncode1(encoder);
49
50 testEncodeToStringWithLongInputData(encoder);
51
52 testWrapEncode2(encoder);
53
54 testEncodeWithByteBuffer(encoder);
55
56 }
57
58 private static void testEncodeWithByteBuffer(final Base64.Encoder encoder) {
59 System.err.println("\n\nEncoder.encode with ByteBuffer test ");
60 final byte[] secondTestBuffer =
61 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
62 .getBytes(US_ASCII);
63 String base64EncodedString;
64 ByteBuffer srcData = ByteBuffer.wrap(secondTestBuffer);
65 ByteBuffer dstData = ByteBuffer.allocate(secondTestBuffer.length * 2);
66
67 encoder.encode(srcData, dstData, 0);
68 dstData.flip();
69 if (dstData.hasArray()) {
70 System.err.println("\nByteBuffer test dstData is Base64 encoding = "
71 + new String(dstData.array(), US_ASCII) + "\n");
72 }
73
74 base64EncodedString = new String(dstData.array(), US_ASCII);
75 if (base64EncodedString.contains("$$$")) {
76 throw new RuntimeException("Base64 encoding contains line separator after Encoder.encode ByteBuffer ... \n");
77 }
78 }
79
80 private static void testWrapEncode2(final Base64.Encoder encoder)
81 throws IOException {
82 System.err.println("\nEncoder.wrap test II ");
83 final byte[] secondTestBuffer =
84 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
85 .getBytes(US_ASCII);
86 String base64EncodedString;
87 ByteArrayOutputStream secondEncodingStream = new ByteArrayOutputStream();
88 OutputStream base64EncodingStream = encoder.wrap(secondEncodingStream);
89 base64EncodingStream.write(secondTestBuffer);
90 base64EncodingStream.close();
91
92 final byte[] encodedByteArray = secondEncodingStream.toByteArray();
93
94 System.err.print("result = " + new String(encodedByteArray, US_ASCII)
95 + " after wrap Base64 encoding of string");
96
97 base64EncodedString = new String(encodedByteArray, US_ASCII);
98
99 if (base64EncodedString.contains("$$$")) {
100 throw new RuntimeException(
101 "Base64 encoding contains line separator after wrap 2 invoked ... \n");
102 }
103 }
104
105 private static void testEncodeToStringWithLongInputData(
106 final Base64.Encoder encoder) {
107 System.err.println("\n\nEncoder.encodeToStringWithLongInputData test ");
108
109 final byte[] secondTestBuffer =
110 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
111 .getBytes(US_ASCII);
112 String base64EncodedString;
113 base64EncodedString = encoder.encodeToString(secondTestBuffer);
114
115 System.err.println("Second Base64 encoded string is "
116 + base64EncodedString);
117
118 if (base64EncodedString.contains("$$$")) {
119 throw new RuntimeException(
120 "Base64 encoding contains line separator after encodeToString invoked ... \n");
121 }
122 }
123
124 private static void testWrapEncode1(final Base64.Encoder encoder)
125 throws IOException {
126 System.err.println("\nEncoder.wrap test I ");
127
128 final byte[] bytesIn = "fo".getBytes(US_ASCII);
129 String base64EncodedString;
130 ByteArrayOutputStream encodingStream = new ByteArrayOutputStream();
131 OutputStream encoding = encoder.wrap(encodingStream);
132 encoding.write(bytesIn);
133 encoding.close();
134
135 final byte[] encodedBytes = encodingStream.toByteArray();
136
137 System.err.print("result = " + new String(encodedBytes, US_ASCII)
138 + " after the Base64 encoding \n");
139
140 base64EncodedString = new String(encodedBytes, US_ASCII);
141
142 if (base64EncodedString.contains("$$$")) {
143 throw new RuntimeException(
144 "Base64 encoding contains line separator after wrap I test ... \n");
145 }
146 }
147
148 private static void testEncodeToString(final Base64.Encoder encoder) {
149 final byte[] bytesIn = "fo".getBytes(US_ASCII);
150
151 System.err.println("\nEncoder.encodeToString test ");
152
153 String base64EncodedString = encoder.encodeToString(bytesIn);
154
155 System.err.println("Base64 encoded string is " + base64EncodedString);
156
157 if (base64EncodedString.contains("$$$")) {
158 throw new RuntimeException("Base64 encoding contains line separator after Encoder.encodeToString invoked ... \n");
159 }
160 }
161}