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