blob: 563cb79702116a4742ae0f0998b06ba407ce82ae [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 {
shermanc2273fa2013-10-24 11:12:59 -070044 final Base64.Encoder encoder = Base64.getMimeEncoder(0, "$$$".getBytes(US_ASCII));
msheppar8f5d1aa2013-04-30 16:24:08 +010045
46 testEncodeToString(encoder);
47
48 testWrapEncode1(encoder);
49
50 testEncodeToStringWithLongInputData(encoder);
51
52 testWrapEncode2(encoder);
53
msheppar8f5d1aa2013-04-30 16:24:08 +010054 }
55
56 private static void testWrapEncode2(final Base64.Encoder encoder)
57 throws IOException {
58 System.err.println("\nEncoder.wrap test II ");
59 final byte[] secondTestBuffer =
60 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
61 .getBytes(US_ASCII);
62 String base64EncodedString;
63 ByteArrayOutputStream secondEncodingStream = new ByteArrayOutputStream();
64 OutputStream base64EncodingStream = encoder.wrap(secondEncodingStream);
65 base64EncodingStream.write(secondTestBuffer);
66 base64EncodingStream.close();
67
68 final byte[] encodedByteArray = secondEncodingStream.toByteArray();
69
70 System.err.print("result = " + new String(encodedByteArray, US_ASCII)
71 + " after wrap Base64 encoding of string");
72
73 base64EncodedString = new String(encodedByteArray, US_ASCII);
74
75 if (base64EncodedString.contains("$$$")) {
76 throw new RuntimeException(
77 "Base64 encoding contains line separator after wrap 2 invoked ... \n");
78 }
79 }
80
81 private static void testEncodeToStringWithLongInputData(
82 final Base64.Encoder encoder) {
83 System.err.println("\n\nEncoder.encodeToStringWithLongInputData test ");
84
85 final byte[] secondTestBuffer =
86 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
87 .getBytes(US_ASCII);
88 String base64EncodedString;
89 base64EncodedString = encoder.encodeToString(secondTestBuffer);
90
91 System.err.println("Second Base64 encoded string is "
92 + base64EncodedString);
93
94 if (base64EncodedString.contains("$$$")) {
95 throw new RuntimeException(
96 "Base64 encoding contains line separator after encodeToString invoked ... \n");
97 }
98 }
99
100 private static void testWrapEncode1(final Base64.Encoder encoder)
101 throws IOException {
102 System.err.println("\nEncoder.wrap test I ");
103
104 final byte[] bytesIn = "fo".getBytes(US_ASCII);
105 String base64EncodedString;
106 ByteArrayOutputStream encodingStream = new ByteArrayOutputStream();
107 OutputStream encoding = encoder.wrap(encodingStream);
108 encoding.write(bytesIn);
109 encoding.close();
110
111 final byte[] encodedBytes = encodingStream.toByteArray();
112
113 System.err.print("result = " + new String(encodedBytes, US_ASCII)
114 + " after the Base64 encoding \n");
115
116 base64EncodedString = new String(encodedBytes, US_ASCII);
117
118 if (base64EncodedString.contains("$$$")) {
119 throw new RuntimeException(
120 "Base64 encoding contains line separator after wrap I test ... \n");
121 }
122 }
123
124 private static void testEncodeToString(final Base64.Encoder encoder) {
125 final byte[] bytesIn = "fo".getBytes(US_ASCII);
126
127 System.err.println("\nEncoder.encodeToString test ");
128
129 String base64EncodedString = encoder.encodeToString(bytesIn);
130
131 System.err.println("Base64 encoded string is " + base64EncodedString);
132
133 if (base64EncodedString.contains("$$$")) {
134 throw new RuntimeException("Base64 encoding contains line separator after Encoder.encodeToString invoked ... \n");
135 }
136 }
137}