• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 package test.java.util.Base64;
25 
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.nio.ByteBuffer;
30 
31 import java.util.Base64;
32 
33 import static java.nio.charset.StandardCharsets.US_ASCII;
34 
35 /**
36  * @test
37  * @bug 8007799 8176379
38  * @summary test Encoder with linemax == 0, line separator should not appear in encoded data
39  */
40 
41 public class Base64GetEncoderTest {
42 
main(String args[])43     public static void main(String args[]) throws Throwable {
44 
45         for (int maxlen = -4; maxlen < 4; maxlen++) {
46 
47             final Base64.Encoder encoder = Base64.getMimeEncoder(maxlen, "$$$".getBytes(US_ASCII));
48 
49             testEncodeToString(encoder);
50             testWrapEncode1(encoder);
51             testEncodeToStringWithLongInputData(encoder);
52             testWrapEncode2(encoder);
53         }
54     }
55 
testWrapEncode2(final Base64.Encoder encoder)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 
testEncodeToStringWithLongInputData( final Base64.Encoder encoder)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 
testWrapEncode1(final Base64.Encoder encoder)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 
testEncodeToString(final Base64.Encoder encoder)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 }
138