1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.codec; |
---|
21 | |
---|
22 | import java.io.ByteArrayOutputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.OutputStream; |
---|
25 | import java.io.UnsupportedEncodingException; |
---|
26 | |
---|
27 | import junit.framework.TestCase; |
---|
28 | |
---|
29 | public class Base64OutputStreamTest extends TestCase { |
---|
30 | |
---|
31 | public void testEncode() throws IOException { |
---|
32 | ByteArrayOutputStream bos = null; |
---|
33 | Base64OutputStream encoder = null; |
---|
34 | |
---|
35 | /* |
---|
36 | * Simple initial test. |
---|
37 | */ |
---|
38 | bos = new ByteArrayOutputStream(); |
---|
39 | encoder = new Base64OutputStream(bos); |
---|
40 | encoder.write(fromString("This is the plain text message!")); |
---|
41 | encoder.close(); |
---|
42 | assertEquals("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==\r\n", toString(bos.toByteArray())); |
---|
43 | } |
---|
44 | |
---|
45 | public void testEncodeUnderlyingStreamStaysOpen() throws IOException { |
---|
46 | ByteArrayOutputStream bos = null; |
---|
47 | Base64OutputStream encoder = null; |
---|
48 | |
---|
49 | bos = new ByteArrayOutputStream(); |
---|
50 | encoder = new Base64OutputStream(bos); |
---|
51 | encoder.write(fromString("This is the plain text message!")); |
---|
52 | encoder.close(); |
---|
53 | |
---|
54 | try { |
---|
55 | encoder.write('b'); |
---|
56 | fail(); |
---|
57 | } catch (IOException expected) { |
---|
58 | } |
---|
59 | |
---|
60 | bos.write('y'); |
---|
61 | bos.write('a'); |
---|
62 | bos.write('d'); |
---|
63 | bos.write('a'); |
---|
64 | assertEquals("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==\r\nyada", toString(bos.toByteArray())); |
---|
65 | } |
---|
66 | |
---|
67 | public void testNoLineSeparators() throws IOException { |
---|
68 | assertEquals("", encodeNoLs("")); |
---|
69 | assertEquals("YQ==", encodeNoLs("a")); |
---|
70 | assertEquals("YWI=", encodeNoLs("ab")); |
---|
71 | assertEquals("YWJj", encodeNoLs("abc")); |
---|
72 | assertEquals("YWJjZA==", encodeNoLs("abcd")); |
---|
73 | assertEquals("YWJjZGU=", encodeNoLs("abcde")); |
---|
74 | assertEquals("YWJjZGVm", encodeNoLs("abcdef")); |
---|
75 | assertEquals("YWJjZGVmZw==", encodeNoLs("abcdefg")); |
---|
76 | assertEquals("YWJjZGVmZ2g=", encodeNoLs("abcdefgh")); |
---|
77 | assertEquals("YWJjZGVmZ2hp", encodeNoLs("abcdefghi")); |
---|
78 | assertEquals("DQoMCQ==", encodeNoLs("\r\n\f\t")); |
---|
79 | assertEquals("LT0/VGhhdCdzIGEgdGVzdD89LQ==", |
---|
80 | encodeNoLs("-=?That's a test?=-")); |
---|
81 | } |
---|
82 | |
---|
83 | public void testLineSeparators() throws IOException { |
---|
84 | assertEquals("", encodeLs("")); |
---|
85 | assertEquals("YQ==\r\n", encodeLs("a")); |
---|
86 | assertEquals("YWJjZA==\r\n", encodeLs("abcd")); |
---|
87 | assertEquals("YWJjZGVmZw==\r\n", encodeLs("abcdefg")); |
---|
88 | assertEquals("YWJjZGVmZ2g=\r\n", encodeLs("abcdefgh")); |
---|
89 | assertEquals("YWJjZGVmZ2hp\r\n", encodeLs("abcdefghi")); |
---|
90 | assertEquals("YWJjZGVmZ2hp\r\nag==\r\n", encodeLs("abcdefghij")); |
---|
91 | assertEquals("YWJjZGVmZ2hp\r\nams=\r\n", encodeLs("abcdefghijk")); |
---|
92 | assertEquals("YWJjZGVmZ2hp\r\namts\r\n", encodeLs("abcdefghijkl")); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * tests {@link OutputStream#write(int)} |
---|
97 | */ |
---|
98 | public void testWriteInt() throws IOException { |
---|
99 | byte[] bytes = fromString("123456789012345678901234567890123456789012" |
---|
100 | + "3456789012345678901234567890123456789012345678901234567890" |
---|
101 | + "123456789012345"); |
---|
102 | |
---|
103 | ByteArrayOutputStream b = new ByteArrayOutputStream(); |
---|
104 | Base64OutputStream out = new Base64OutputStream(b); |
---|
105 | |
---|
106 | for (byte element : bytes) |
---|
107 | out.write(element); |
---|
108 | |
---|
109 | out.close(); |
---|
110 | |
---|
111 | String actual = new String(b.toByteArray(), "US-ASCII"); |
---|
112 | |
---|
113 | String expected = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz" |
---|
114 | + "g5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODkwMTIzNDU2Nzg5MDEyMzQ1Njc4" |
---|
115 | + "OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0\r\nNQ==\r" |
---|
116 | + "\n"; |
---|
117 | |
---|
118 | assertEquals(expected, actual); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * tests {@link OutputStream#write(byte[], int, int)} with various offsets |
---|
123 | */ |
---|
124 | public void testWriteOffset() throws IOException { |
---|
125 | byte[] bytes = fromString("123456789012345678901234567890123456789012" |
---|
126 | + "3456789012345678901234567890123456789012345678901234567890" |
---|
127 | + "123456789012345"); |
---|
128 | |
---|
129 | ByteArrayOutputStream b = new ByteArrayOutputStream(); |
---|
130 | Base64OutputStream out = new Base64OutputStream(b); |
---|
131 | |
---|
132 | for (int offset = 0; offset < bytes.length; offset += 2) { |
---|
133 | int len = Math.min(2, bytes.length - offset); |
---|
134 | out.write(bytes, offset, len); |
---|
135 | } |
---|
136 | |
---|
137 | out.close(); |
---|
138 | |
---|
139 | String actual = new String(b.toByteArray(), "US-ASCII"); |
---|
140 | |
---|
141 | String expected = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz" |
---|
142 | + "g5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODkwMTIzNDU2Nzg5MDEyMzQ1Njc4" |
---|
143 | + "OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0\r\nNQ==\r" |
---|
144 | + "\n"; |
---|
145 | |
---|
146 | assertEquals(expected, actual); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * tests {@link OutputStream#flush()} while writing |
---|
151 | */ |
---|
152 | public void testWriteFlush() throws IOException { |
---|
153 | byte[] bytes = fromString("123456789012345678901234567890123456789012" |
---|
154 | + "3456789012345678901234567890123456789012345678901234567890" |
---|
155 | + "123456789012345"); |
---|
156 | |
---|
157 | ByteArrayOutputStream b = new ByteArrayOutputStream(); |
---|
158 | Base64OutputStream out = new Base64OutputStream(b); |
---|
159 | |
---|
160 | for (int offset = 0; offset < bytes.length; offset += 7) { |
---|
161 | int len = Math.min(7, bytes.length - offset); |
---|
162 | out.write(bytes, offset, len); |
---|
163 | out.flush(); |
---|
164 | } |
---|
165 | |
---|
166 | out.close(); |
---|
167 | |
---|
168 | String actual = new String(b.toByteArray(), "US-ASCII"); |
---|
169 | |
---|
170 | String expected = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nz" |
---|
171 | + "g5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODkwMTIzNDU2Nzg5MDEyMzQ1Njc4" |
---|
172 | + "OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0\r\nNQ==\r" |
---|
173 | + "\n"; |
---|
174 | |
---|
175 | assertEquals(expected, actual); |
---|
176 | } |
---|
177 | |
---|
178 | private String encodeNoLs(String str) throws IOException { |
---|
179 | return encode(str, 0, new byte[] {}); |
---|
180 | } |
---|
181 | |
---|
182 | private String encodeLs(String str) throws IOException { |
---|
183 | return encode(str, 12, new byte[] { '\r', '\n' }); |
---|
184 | } |
---|
185 | |
---|
186 | private String encode(String str, int lineLength, byte[] lineSeparator) |
---|
187 | throws IOException { |
---|
188 | byte[] bytes = fromString(str); |
---|
189 | |
---|
190 | ByteArrayOutputStream b = new ByteArrayOutputStream(); |
---|
191 | Base64OutputStream out = new Base64OutputStream(b, lineLength, |
---|
192 | lineSeparator); |
---|
193 | |
---|
194 | out.write(bytes); |
---|
195 | out.close(); |
---|
196 | |
---|
197 | return toString(b.toByteArray()); |
---|
198 | } |
---|
199 | |
---|
200 | private byte[] fromString(String s) { |
---|
201 | try { |
---|
202 | return s.getBytes("US-ASCII"); |
---|
203 | } catch (UnsupportedEncodingException e) { |
---|
204 | fail(e.getMessage()); |
---|
205 | return null; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | private String toString(byte[] b) { |
---|
210 | try { |
---|
211 | return new String(b, "US-ASCII"); |
---|
212 | } catch (UnsupportedEncodingException e) { |
---|
213 | fail(e.getMessage()); |
---|
214 | return null; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|