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.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.InputStream;
29
30 import org.apache.commons.io.IOUtils;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 class Base58InputStreamTest {
37
38 private static final byte[] CRLF = { (byte) '\r', (byte) '\n' };
39
40 private static final byte[] LF = { (byte) '\n' };
41
42 private static final String STRING_FIXTURE = "Hello World";
43
44 @Test
45 void testAvailable() throws Throwable {
46 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
47 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
48 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
49 final int initialAvailable = b58stream.available();
50 assertTrue(initialAvailable > 0, "Initial available should be greater than 0");
51 assertEquals(3, b58stream.skip(10), "Skip should return 3 (decoded bytes)");
52 assertEquals(0, b58stream.available());
53 assertEquals(-1, b58stream.read());
54 assertEquals(-1, b58stream.read());
55 }
56 }
57
58 private void testBase58EmptyInputStream(final int chuckSize) throws Exception {
59 final byte[] emptyEncoded = {};
60 final byte[] emptyDecoded = {};
61 testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF);
62 testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF);
63 }
64
65
66
67
68
69
70 @Test
71 void testBase58EmptyInputStreamMimeChuckSize() throws Exception {
72 testBase58EmptyInputStream(BaseNCodec.MIME_CHUNK_SIZE);
73 }
74
75
76
77
78
79
80 @Test
81 void testBase58EmptyInputStreamPemChuckSize() throws Exception {
82 testBase58EmptyInputStream(BaseNCodec.PEM_CHUNK_SIZE);
83 }
84
85 @Test
86 void testBase58InputStreamByChunk() throws Exception {
87
88 byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
89 byte[] encoded = new Base58().encode(decoded);
90 testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
91
92 final BaseNCodec codec = new Base58();
93 for (int i = 0; i <= 150; i++) {
94 final byte[][] randomData = BaseNTestData.randomData(codec, i);
95 encoded = randomData[1];
96 decoded = randomData[0];
97 testByChunk(encoded, decoded, 0, LF);
98 }
99 }
100
101 @Test
102 void testBase58InputStreamByteByByte() throws Exception {
103
104 byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
105 byte[] encoded = new Base58().encode(decoded);
106 testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
107
108 final BaseNCodec codec = new Base58();
109 for (int i = 0; i <= 150; i++) {
110 final byte[][] randomData = BaseNTestData.randomData(codec, i);
111 encoded = randomData[1];
112 decoded = randomData[0];
113 testByteByByte(encoded, decoded, 0, LF);
114 }
115 }
116
117 @Test
118 void testBuilder() {
119 assertNotNull(Base58InputStream.builder().getBaseNCodec());
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
135 try (InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
136 final byte[] output = IOUtils.toByteArray(in);
137 assertEquals(-1, in.read(), "EOF");
138 assertEquals(-1, in.read(), "Still EOF");
139 assertArrayEquals(encoded, output, "Streaming base58 encode");
140 }
141 try (InputStream in = new Base58InputStream(new ByteArrayInputStream(encoded))) {
142 final byte[] output = IOUtils.toByteArray(in);
143 assertEquals(-1, in.read(), "EOF");
144 assertEquals(-1, in.read(), "Still EOF");
145 assertArrayEquals(decoded, output, "Streaming base58 decode");
146 }
147 InputStream in = new ByteArrayInputStream(decoded);
148 for (int i = 0; i < 10; i++) {
149 in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
150 in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
151 }
152 final InputStream in1 = in;
153 final byte[] output = IOUtils.toByteArray(in1);
154 assertEquals(-1, in.read(), "EOF");
155 assertEquals(-1, in.read(), "Still EOF");
156 assertArrayEquals(decoded, output, "Streaming base58 wrap-wrap-wrap!");
157 in.close();
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172 private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
173 InputStream in;
174 in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get();
175 final InputStream in1 = in;
176 byte[] output = IOUtils.toByteArray(in1);
177 assertEquals(-1, in.read(), "EOF");
178 assertEquals(-1, in.read(), "Still EOF");
179 assertArrayEquals(encoded, output, "Streaming base58 encode");
180 in.close();
181 in = new Base58InputStream(new ByteArrayInputStream(encoded));
182 final InputStream in2 = in;
183 output = IOUtils.toByteArray(in2);
184 assertEquals(-1, in.read(), "EOF");
185 assertEquals(-1, in.read(), "Still EOF");
186 assertArrayEquals(decoded, output, "Streaming base58 decode");
187 in.close();
188 in = new ByteArrayInputStream(decoded);
189 for (int i = 0; i < 10; i++) {
190 in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
191 in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
192 }
193 final InputStream in3 = in;
194 output = IOUtils.toByteArray(in3);
195 assertEquals(-1, in.read(), "EOF");
196 assertEquals(-1, in.read(), "Still EOF");
197 assertArrayEquals(decoded, output, "Streaming base58 wrap-wrap-wrap!");
198 }
199
200
201
202
203
204
205 @Test
206 void testMarkSupported() throws Exception {
207 final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
208 try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
209
210 assertFalse(in.markSupported(), "Base58InputStream.markSupported() is false");
211 }
212 }
213
214
215
216
217
218
219 @Test
220 void testRead0() throws Exception {
221 final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
222 final byte[] buf = new byte[1024];
223 int bytesRead = 0;
224 try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
225 bytesRead = in.read(buf, 0, 0);
226 assertEquals(0, bytesRead, "Base58InputStream.read(buf, 0, 0) returns 0");
227 }
228 }
229
230
231
232
233
234
235 @Test
236 void testReadNull() throws Exception {
237 final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
238 try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
239 assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
240 }
241 }
242
243
244
245
246
247
248 @Test
249 void testReadOutOfBounds() throws Exception {
250 final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
251 final byte[] buf = new byte[1024];
252 try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
253 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0), "Base58InputStream.read(buf, -1, 0)");
254 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1), "Base58InputStream.read(buf, 0, -1)");
255 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0), "Base58InputStream.read(buf, buf.length + 1, 0)");
256 assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2), "Base58InputStream.read(buf, buf.length - 1, 2)");
257 }
258 }
259
260
261
262
263
264
265 @Test
266 void testSkipBig() throws Throwable {
267 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
268 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
269 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
270 assertEquals(3, b58stream.skip(1024));
271
272 assertEquals(-1, b58stream.read());
273 assertEquals(-1, b58stream.read());
274 }
275 }
276
277
278
279
280
281
282 @Test
283 void testSkipNone() throws Throwable {
284 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
285 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
286 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
287 final byte[] actualBytes = new byte[3];
288 assertEquals(0, b58stream.skip(0));
289 b58stream.read(actualBytes, 0, actualBytes.length);
290 assertArrayEquals(actualBytes, new byte[] { 102, 111, 111 });
291
292 assertEquals(-1, b58stream.read());
293 }
294 }
295
296
297
298
299
300
301 @Test
302 void testSkipPastEnd() throws Throwable {
303 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
304 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
305 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
306
307 assertEquals(3, b58stream.skip(10));
308
309 assertEquals(-1, b58stream.read());
310 assertEquals(-1, b58stream.read());
311 }
312 }
313
314
315
316
317
318
319 @Test
320 void testSkipToEnd() throws Throwable {
321 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
322 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
323 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
324
325 assertEquals(3, b58stream.skip(3));
326 assertEquals(-1, b58stream.read());
327 }
328 }
329
330
331
332
333
334
335 @Test
336 void testSkipWrongArgument() throws Throwable {
337 final String encoded = new String(new Base58().encode(StringUtils.getBytesUtf8("foo")));
338 final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(encoded));
339 try (Base58InputStream b58stream = new Base58InputStream(ins)) {
340 assertThrows(IllegalArgumentException.class, () -> b58stream.skip(-1));
341 }
342 }
343 }