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