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