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.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.OutputStream;
28
29 import org.apache.commons.codec.CodecPolicy;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class Base32OutputStreamTest extends AbstractBaseNOutputStreamTest {
36
37 private static final byte[] CR_LF = {(byte) '\r', (byte) '\n'};
38
39 private static final byte[] LF = {(byte) '\n'};
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 @Override
65 OutputStream newOutputStream() {
66 return new Base32OutputStream(new ByteArrayOutputStream());
67 }
68
69 private void testBase32EmptyOutputStream(final int chunkSize) throws Exception {
70 final byte[] emptyEncoded = {};
71 final byte[] emptyDecoded = {};
72 testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CR_LF);
73 testByChunk(emptyEncoded, emptyDecoded, chunkSize, CR_LF);
74 }
75
76
77
78
79
80
81
82 @Test
83 void testBase32EmptyOutputStreamMimeChunkSize() throws Exception {
84 testBase32EmptyOutputStream(BaseNCodec.MIME_CHUNK_SIZE);
85 }
86
87
88
89
90
91
92
93 @Test
94 void testBase32EmptyOutputStreamPemChunkSize() throws Exception {
95 testBase32EmptyOutputStream(BaseNCodec.PEM_CHUNK_SIZE);
96 }
97
98
99
100
101
102
103
104 @Test
105 void testBase32OutputStreamByChunk() throws Exception {
106
107 byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
108 byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
109 testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CR_LF);
110
111
112
113
114
115
116
117
118
119
120
121
122
123 final BaseNCodec codec = new Base32();
124 for (int i = 0; i <= 150; i++) {
125 final byte[][] randomData = BaseNTestData.randomData(codec, i);
126 encoded = randomData[1];
127 decoded = randomData[0];
128 testByChunk(encoded, decoded, 0, LF);
129 }
130 }
131
132
133
134
135
136
137
138 @Test
139 void testBase32OutputStreamByteByByte() throws Exception {
140
141 byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
142 byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
143 testByteByByte(encoded, decoded, 76, CR_LF);
144
145
146
147
148
149
150
151
152
153
154
155
156
157 final BaseNCodec codec = new Base32();
158 for (int i = 0; i <= 150; i++) {
159 final byte[][] randomData = BaseNTestData.randomData(codec, i);
160 encoded = randomData[1];
161 decoded = randomData[0];
162 testByteByByte(encoded, decoded, 0, LF);
163 }
164 }
165
166 @Test
167 void testBuilder() {
168 assertNotNull(Base32OutputStream.builder().getBaseNCodec());
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
190
191
192 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
193 try (OutputStream out = new Base32OutputStream(byteOut, true, chunkSize, separator)) {
194 out.write(decoded);
195 }
196 byte[] output = byteOut.toByteArray();
197 assertArrayEquals(encoded, output, "Streaming chunked Base32 encode");
198
199
200 byteOut = new ByteArrayOutputStream();
201 try (OutputStream out = new Base32OutputStream(byteOut, false)) {
202 out.write(encoded);
203 }
204 output = byteOut.toByteArray();
205 assertArrayEquals(decoded, output, "Streaming chunked Base32 decode");
206
207
208 byteOut = new ByteArrayOutputStream();
209 OutputStream out = byteOut;
210 for (int i = 0; i < 10; i++) {
211 out = new Base32OutputStream(out, false);
212 out = new Base32OutputStream(out, true, chunkSize, separator);
213 }
214 out.write(decoded);
215 out.close();
216 output = byteOut.toByteArray();
217
218 assertArrayEquals(decoded, byteOut.toByteArray(), "Streaming chunked Base32 wrap-wrap-wrap!");
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
240
241
242 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
243 try (OutputStream out = new Base32OutputStream(byteOut, true, chunkSize, separator)) {
244 for (final byte element : decoded) {
245 out.write(element);
246 }
247 }
248 byte[] output = byteOut.toByteArray();
249 assertArrayEquals(encoded, output, "Streaming byte-by-byte Base32 encode");
250
251
252 byteOut = new ByteArrayOutputStream();
253 try (OutputStream out = new Base32OutputStream(byteOut, false)) {
254 for (final byte element : encoded) {
255 out.write(element);
256 }
257 }
258 output = byteOut.toByteArray();
259 assertArrayEquals(decoded, output, "Streaming byte-by-byte Base32 decode");
260
261
262 byteOut = new ByteArrayOutputStream();
263 try (OutputStream out = new Base32OutputStream(byteOut, false)) {
264 for (final byte element : encoded) {
265 out.write(element);
266 out.flush();
267 }
268 }
269 output = byteOut.toByteArray();
270 assertArrayEquals(decoded, output, "Streaming byte-by-byte flush() Base32 decode");
271
272
273 byteOut = new ByteArrayOutputStream();
274 OutputStream out = byteOut;
275 for (int i = 0; i < 10; i++) {
276 out = new Base32OutputStream(out, false);
277 out = new Base32OutputStream(out, true, chunkSize, separator);
278 }
279 for (final byte element : decoded) {
280 out.write(element);
281 }
282 out.close();
283 output = byteOut.toByteArray();
284
285 assertArrayEquals(decoded, output, "Streaming byte-by-byte Base32 wrap-wrap-wrap!");
286 }
287
288
289
290
291
292
293
294 @Test
295 void testStrictDecoding() throws Exception {
296 for (final String s : Base32Test.BASE32_IMPOSSIBLE_CASES) {
297 final byte[] encoded = StringUtils.getBytesUtf8(s);
298 ByteArrayOutputStream bout = new ByteArrayOutputStream();
299 try (Base32OutputStream out = new Base32OutputStream(bout, false)) {
300
301 assertFalse(out.isStrictDecoding());
302 out.write(encoded);
303 out.close();
304 assertTrue(bout.size() > 0);
305
306
307 bout = new ByteArrayOutputStream();
308 try (Base32OutputStream out2 = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT)) {
309 assertTrue(out2.isStrictDecoding());
310 assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
311 }
312 try (Base32OutputStream out2 = Base32OutputStream.builder()
313 .setOutputStream(bout).setEncode(false)
314 .setBaseNCodec(Base32.builder().setLineLength(0).setLineSeparator(null).setDecodingPolicy(CodecPolicy.STRICT).get())
315 .get()) {
316 assertTrue(out2.isStrictDecoding());
317 assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
318 }
319 try (Base32OutputStream out2 = Base32OutputStream.builder()
320 .setOutputStream(bout).setEncode(false)
321 .setBaseNCodec(Base32.builder().setDecodingPolicy(CodecPolicy.STRICT).get())
322 .get()) {
323 assertTrue(out2.isStrictDecoding());
324 assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
325 }
326 }
327 }
328 }
329
330
331
332
333
334
335
336 @Test
337 void testWriteOutOfBounds() throws Exception {
338 final byte[] buf = new byte[1024];
339 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
340 try (Base32OutputStream out = new Base32OutputStream(bout)) {
341 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1), "Base32OutputStream.write(buf, -1, 1)");
342 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1), "Base32OutputStream.write(buf, 1, -1)");
343 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0), "Base32OutputStream.write(buf, buf, buf.length + 1, 0)");
344 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2), "Base32OutputStream.write(buf, buf, buf.length - 1, 2)");
345 }
346 }
347
348
349
350
351
352
353
354 @Test
355 void testWriteToNullCoverage() throws Exception {
356 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
357 try (Base32OutputStream out = new Base32OutputStream(bout)) {
358 assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
359 }
360 }
361 }