-
Notifications
You must be signed in to change notification settings - Fork 230
/
Base64OutputStreamTest.java
348 lines (310 loc) · 13.8 KB
/
Base64OutputStreamTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.codec.binary;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.apache.commons.codec.CodecPolicy;
import org.junit.jupiter.api.Test;
/**
*/
public class Base64OutputStreamTest {
private final static byte[] CR_LF = {(byte) '\r', (byte) '\n'};
private final static byte[] LF = {(byte) '\n'};
private static final String STRING_FIXTURE = "Hello World";
private void testBase64EmptyOutputStream(final int chunkSize) throws Exception {
final byte[] emptyEncoded = {};
final byte[] emptyDecoded = {};
testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CR_LF);
testByChunk(emptyEncoded, emptyDecoded, chunkSize, CR_LF);
}
/**
* Test the Base64OutputStream implementation against empty input.
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testBase64EmptyOutputStreamMimeChunkSize() throws Exception {
testBase64EmptyOutputStream(BaseNCodec.MIME_CHUNK_SIZE);
}
/**
* Test the Base64OutputStream implementation against empty input.
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testBase64EmptyOutputStreamPemChunkSize() throws Exception {
testBase64EmptyOutputStream(BaseNCodec.PEM_CHUNK_SIZE);
}
/**
* Test the Base64OutputStream implementation
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testBase64OutputStreamByChunk() throws Exception {
// Hello World test.
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CR_LF);
// Single Byte test.
encoded = StringUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CR_LF);
// OpenSSL interop test.
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
decoded = BaseNTestData.DECODED;
testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF);
// Single Line test.
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
encoded = StringUtils.getBytesUtf8(singleLine);
decoded = BaseNTestData.DECODED;
testByChunk(encoded, decoded, 0, LF);
// test random data of sizes 0 through 150
final BaseNCodec codec = new Base64(0, null, false);
for (int i = 0; i <= 150; i++) {
final byte[][] randomData = BaseNTestData.randomData(codec, i);
encoded = randomData[1];
decoded = randomData[0];
testByChunk(encoded, decoded, 0, LF);
}
}
/**
* Test the Base64OutputStream implementation
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testBase64OutputStreamByteByByte() throws Exception {
// Hello World test.
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
testByteByByte(encoded, decoded, 76, CR_LF);
// Single Byte test.
encoded = StringUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByteByByte(encoded, decoded, 76, CR_LF);
// OpenSSL interop test.
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
decoded = BaseNTestData.DECODED;
testByteByByte(encoded, decoded, 64, LF);
// Single Line test.
final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replace("\n", "");
encoded = StringUtils.getBytesUtf8(singleLine);
decoded = BaseNTestData.DECODED;
testByteByByte(encoded, decoded, 0, LF);
// test random data of sizes 0 through 150
final BaseNCodec codec = new Base64(0, null, false);
for (int i = 0; i <= 150; i++) {
final byte[][] randomData = BaseNTestData.randomData(codec, i);
encoded = randomData[1];
decoded = randomData[0];
testByteByByte(encoded, decoded, 0, LF);
}
}
/**
* Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
* encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
* <p/>
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64OutputStream wraps itself in encode and decode
* mode over and over again.
*
* @param encoded
* base64 encoded data
* @param decoded
* the data from above, but decoded
* @param chunkSize
* chunk size (line-length) of the base64 encoded data.
* @param separator
* Line separator in the base64 encoded data.
* @throws Exception
* Usually signifies a bug in the Base64 commons-codec implementation.
*/
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
// Start with encode.
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try (OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator)) {
out.write(decoded);
}
byte[] output = byteOut.toByteArray();
assertArrayEquals(encoded, output, "Streaming chunked base64 encode");
// Now let's try to decode.
byteOut = new ByteArrayOutputStream();
try (OutputStream out = new Base64OutputStream(byteOut, false)) {
out.write(encoded);
}
output = byteOut.toByteArray();
assertArrayEquals(decoded, output, "Streaming chunked base64 decode");
// I always wanted to do this! (wrap encoder with decoder etc.).
byteOut = new ByteArrayOutputStream();
OutputStream out = byteOut;
for (int i = 0; i < 10; i++) {
out = new Base64OutputStream(out, false);
out = new Base64OutputStream(out, true, chunkSize, separator);
}
out.write(decoded);
out.close();
output = byteOut.toByteArray();
assertArrayEquals(decoded, output, "Streaming chunked base64 wrap-wrap-wrap!");
}
/**
* Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
* encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
* <p/>
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64OutputStream wraps itself in encode and decode
* mode over and over again.
*
* @param encoded
* base64 encoded data
* @param decoded
* the data from above, but decoded
* @param chunkSize
* chunk size (line-length) of the base64 encoded data.
* @param separator
* Line separator in the base64 encoded data.
* @throws Exception
* Usually signifies a bug in the Base64 commons-codec implementation.
*/
private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
// Start with encode.
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try (OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator)) {
for (final byte element : decoded) {
out.write(element);
}
}
byte[] output = byteOut.toByteArray();
assertArrayEquals(encoded, output, "Streaming byte-by-byte base64 encode");
// Now let's try to decode.
byteOut = new ByteArrayOutputStream();
try (OutputStream out = new Base64OutputStream(byteOut, false)) {
for (final byte element : encoded) {
out.write(element);
}
}
output = byteOut.toByteArray();
assertArrayEquals(decoded, output, "Streaming byte-by-byte base64 decode");
// Now let's try to decode with tonnes of flushes.
byteOut = new ByteArrayOutputStream();
try (OutputStream out = new Base64OutputStream(byteOut, false)) {
for (final byte element : encoded) {
out.write(element);
out.flush();
}
}
output = byteOut.toByteArray();
assertArrayEquals(decoded, output, "Streaming byte-by-byte flush() base64 decode");
// I always wanted to do this! (wrap encoder with decoder etc.).
byteOut = new ByteArrayOutputStream();
OutputStream out = byteOut;
for (int i = 0; i < 10; i++) {
out = new Base64OutputStream(out, false);
out = new Base64OutputStream(out, true, chunkSize, separator);
}
for (final byte element : decoded) {
out.write(element);
}
out.close();
output = byteOut.toByteArray();
assertArrayEquals(decoded, output, "Streaming byte-by-byte base64 wrap-wrap-wrap!");
}
/**
* Test the Base64OutputStream implementation against the special NPE inducing input
* identified in the CODEC-98 bug.
*
* @throws Exception for some failure scenarios.
*/
@Test
public void testCodec98NPE() throws Exception {
final byte[] codec98 = StringUtils.getBytesUtf8(Base64TestData.CODEC_98_NPE);
final byte[] codec98_1024 = new byte[1024];
System.arraycopy(codec98, 0, codec98_1024, 0, codec98.length);
final ByteArrayOutputStream data = new ByteArrayOutputStream(1024);
try (final Base64OutputStream stream = new Base64OutputStream(data, false)) {
stream.write(codec98_1024, 0, 1024);
}
final byte[] decodedBytes = data.toByteArray();
final String decoded = StringUtils.newStringUtf8(decodedBytes);
assertEquals(Base64TestData.CODEC_98_NPE_DECODED, decoded, "codec-98 NPE Base64OutputStream");
}
/**
* Test strict decoding.
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testStrictDecoding() throws Exception {
for (final String impossibleStr : Base64Test.BASE64_IMPOSSIBLE_CASES) {
final byte[] impossibleEncoded = StringUtils.getBytesUtf8(impossibleStr);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (Base64OutputStream out = new Base64OutputStream(bout, false)) {
// Default is lenient decoding; it should not throw
assertFalse(out.isStrictDecoding());
out.write(impossibleEncoded);
}
assertTrue(bout.size() > 0);
// Strict decoding should throw
bout = new ByteArrayOutputStream();
final Base64OutputStream out = new Base64OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
// May throw on write or on close depending on the position of the
// impossible last character in the output block size
assertThrows(IllegalArgumentException.class, () -> {
out.write(impossibleEncoded);
out.close();
});
}
}
/**
* Tests Base64OutputStream.write for expected IndexOutOfBoundsException conditions.
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testWriteOutOfBounds() throws Exception {
final byte[] buf = new byte[1024];
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base64OutputStream out = new Base64OutputStream(bout)) {
assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1), "Base64OutputStream.write(buf, -1, 1)");
assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1), "Base64OutputStream.write(buf, 1, -1)");
assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0), "Base64OutputStream.write(buf, buf.length + 1, 0)");
assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2), "Base64OutputStream.write(buf, buf.length - 1, 2)");
}
}
/**
* Tests Base64OutputStream.write(null).
*
* @throws Exception
* for some failure scenarios.
*/
@Test
public void testWriteToNullCoverage() throws Exception {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base64OutputStream out = new Base64OutputStream(bout)) {
assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
}
}
}