1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 import org.apache.commons.codec.CodecPolicy;
32 import org.junit.jupiter.api.Test;
33
34 public class Base32InputStreamTest {
35
36 private static final String ENCODED_FOO = "MZXW6===";
37
38 private final static byte[] CRLF = { (byte) '\r', (byte) '\n' };
39
40 private final static byte[] LF = { (byte) '\n' };
41
42 private static final String STRING_FIXTURE = "Hello World";
43
44
45
46
47
48
49
50 @Test
51 public void testAvailable() throws Throwable {
52 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
53 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
54 assertEquals(1, b32stream.available());
55 assertEquals(3, b32stream.skip(10));
56
57 assertEquals(0, b32stream.available());
58 assertEquals(-1, b32stream.read());
59 assertEquals(-1, b32stream.read());
60 assertEquals(0, b32stream.available());
61 }
62 }
63
64 private void testBase32EmptyInputStream(final int chuckSize) throws Exception {
65 final byte[] emptyEncoded = {};
66 final byte[] emptyDecoded = {};
67 testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF);
68 testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF);
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 @Test
144 public void testBase32EmptyInputStreamMimeChuckSize() throws Exception {
145 testBase32EmptyInputStream(BaseNCodec.MIME_CHUNK_SIZE);
146 }
147
148
149
150
151
152
153
154 @Test
155 public void testBase32EmptyInputStreamPemChuckSize() throws Exception {
156 testBase32EmptyInputStream(BaseNCodec.PEM_CHUNK_SIZE);
157 }
158
159
160
161
162
163
164
165 @Test
166 public void testBase32InputStreamByChunk() throws Exception {
167
168 byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
169 byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
170 testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
171
172
173 encoded = StringUtils.getBytesUtf8("AA======\r\n");
174 decoded = new byte[] { (byte) 0 };
175 testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
176
177
178
179
180
181
182
183
184
185
186
187
188
189 final BaseNCodec codec = new Base32();
190 for (int i = 0; i <= 150; i++) {
191 final byte[][] randomData = BaseNTestData.randomData(codec, i);
192 encoded = randomData[1];
193 decoded = randomData[0];
194 testByChunk(encoded, decoded, 0, LF);
195 }
196 }
197
198
199
200
201
202
203
204 @Test
205 public void testBase32InputStreamByteByByte() throws Exception {
206
207 byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
208 byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
209 testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
210
211
212 encoded = StringUtils.getBytesUtf8("AA======\r\n");
213 decoded = new byte[] { (byte) 0 };
214 testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
215
216
217
218
219
220
221
222
223 final BaseNCodec codec = new Base32();
224 for (int i = 0; i <= 150; i++) {
225 final byte[][] randomData = BaseNTestData.randomData(codec, i);
226 encoded = randomData[1];
227 decoded = randomData[0];
228 testByteByByte(encoded, decoded, 0, LF);
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
251
252
253 InputStream in;
254
255 in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator);
256 byte[] output = BaseNTestData.streamToBytes(in);
257
258 assertEquals(-1, in.read(), "EOF");
259 assertEquals(-1, in.read(), "Still EOF");
260 assertArrayEquals(encoded, output, "Streaming base32 encode");
261
262
263 in = new Base32InputStream(new ByteArrayInputStream(encoded));
264 output = BaseNTestData.streamToBytes(in);
265
266 assertEquals(-1, in.read(), "EOF");
267 assertEquals(-1, in.read(), "Still EOF");
268 assertArrayEquals(decoded, output, "Streaming base32 decode");
269
270
271 in = new ByteArrayInputStream(decoded);
272 for (int i = 0; i < 10; i++) {
273 in = new Base32InputStream(in, true, chunkSize, separator);
274 in = new Base32InputStream(in, false);
275 }
276 output = BaseNTestData.streamToBytes(in);
277
278 assertEquals(-1, in.read(), "EOF");
279 assertEquals(-1, in.read(), "Still EOF");
280 assertArrayEquals(decoded, output, "Streaming base32 wrap-wrap-wrap!");
281 in.close();
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
303
304
305 InputStream in;
306 in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator);
307 byte[] output = new byte[encoded.length];
308 for (int i = 0; i < output.length; i++) {
309 output[i] = (byte) in.read();
310 }
311
312 assertEquals(-1, in.read(), "EOF");
313 assertEquals(-1, in.read(), "Still EOF");
314 assertArrayEquals(encoded, output, "Streaming base32 encode");
315
316 in.close();
317
318
319 in = new Base32InputStream(new ByteArrayInputStream(encoded));
320 output = new byte[decoded.length];
321 for (int i = 0; i < output.length; i++) {
322 output[i] = (byte) in.read();
323 }
324
325 assertEquals(-1, in.read(), "EOF");
326 assertEquals(-1, in.read(), "Still EOF");
327 assertArrayEquals(decoded, output, "Streaming base32 decode");
328
329 in.close();
330
331
332 in = new ByteArrayInputStream(decoded);
333 for (int i = 0; i < 10; i++) {
334 in = new Base32InputStream(in, true, chunkSize, separator);
335 in = new Base32InputStream(in, false);
336 }
337 output = new byte[decoded.length];
338 for (int i = 0; i < output.length; i++) {
339 output[i] = (byte) in.read();
340 }
341
342 assertEquals(-1, in.read(), "EOF");
343 assertEquals(-1, in.read(), "Still EOF");
344 assertArrayEquals(decoded, output, "Streaming base32 wrap-wrap-wrap!");
345 }
346
347
348
349
350 @Test
351 public void testCodec105() throws IOException {
352 try (final Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null)) {
353 for (int i = 0; i < 5; i++) {
354 in.read();
355 }
356 }
357 }
358
359
360
361
362 @Test
363 public void testCodec130() throws IOException {
364 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
365 try (final Base32OutputStream base32os = new Base32OutputStream(bos)) {
366 base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE));
367 }
368
369 final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
370 final Base32InputStream ins = new Base32InputStream(bis);
371
372
373 ins.skip(1);
374 final byte[] decodedBytes = BaseNTestData.streamToBytes(ins, new byte[64]);
375 final String str = StringUtils.newStringUtf8(decodedBytes);
376
377 assertEquals(STRING_FIXTURE.substring(1), str);
378 }
379
380
381
382
383
384
385
386 @Test
387 public void testMarkSupported() throws Exception {
388 final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
389 final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
390 try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
391
392 assertFalse(in.markSupported(), "Base32InputStream.markSupported() is false");
393 }
394 }
395
396
397
398
399
400
401
402 @Test
403 public void testRead0() throws Exception {
404 final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
405 final byte[] buf = new byte[1024];
406 int bytesRead = 0;
407 final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
408 try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
409 bytesRead = in.read(buf, 0, 0);
410 assertEquals(0, bytesRead, "Base32InputStream.read(buf, 0, 0) returns 0");
411 }
412 }
413
414
415
416
417
418
419
420 @Test
421 public void testReadNull() throws Exception {
422 final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
423 final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
424 try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] {0, 0, 0})) {
425 assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
426 }
427 }
428
429
430
431
432
433
434
435 @Test
436 public void testReadOutOfBounds() throws Exception {
437 final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
438 final byte[] buf = new byte[1024];
439 final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
440 try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
441 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0), "Base32InputStream.read(buf, -1, 0)");
442 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1), "Base32InputStream.read(buf, 0, -1)");
443 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0), "Base32InputStream.read(buf, buf.length + 1, 0)");
444 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2), "Base32InputStream.read(buf, buf.length - 1, 2)");
445 }
446 }
447
448
449
450
451
452
453
454 @Test
455 public void testSkipBig() throws Throwable {
456 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
457 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
458 assertEquals(3, b32stream.skip(1024));
459
460 assertEquals(-1, b32stream.read());
461 assertEquals(-1, b32stream.read());
462 }
463 }
464
465
466
467
468
469
470
471 @Test
472 public void testSkipNone() throws Throwable {
473 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
474 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
475 final byte[] actualBytes = new byte[6];
476 assertEquals(0, b32stream.skip(0));
477 b32stream.read(actualBytes, 0, actualBytes.length);
478 assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 });
479
480 assertEquals(-1, b32stream.read());
481 }
482 }
483
484
485
486
487
488
489
490 @Test
491 public void testSkipPastEnd() throws Throwable {
492 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
493 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
494
495 assertEquals(3, b32stream.skip(10));
496
497 assertEquals(-1, b32stream.read());
498 assertEquals(-1, b32stream.read());
499 }
500 }
501
502
503
504
505
506
507
508 @Test
509 public void testSkipToEnd() throws Throwable {
510 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
511 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
512
513 assertEquals(3, b32stream.skip(3));
514
515 assertEquals(-1, b32stream.read());
516 assertEquals(-1, b32stream.read());
517 }
518 }
519
520
521
522
523
524
525
526 @Test
527 public void testSkipWrongArgument() throws Throwable {
528 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
529 try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
530 assertThrows(IllegalArgumentException.class, () -> b32stream.skip(-10));
531 }
532 }
533
534
535
536
537
538
539
540 @Test
541 public void testStrictDecoding() throws Exception {
542 for (final String s : Base32Test.BASE32_IMPOSSIBLE_CASES) {
543 final byte[] encoded = StringUtils.getBytesUtf8(s);
544 final Base32InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded), false);
545
546 assertFalse(in.isStrictDecoding());
547 BaseNTestData.streamToBytes(in);
548
549
550 final Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
551 assertTrue(in2.isStrictDecoding());
552 assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
553 }
554 }
555 }