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.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class Base16OutputStreamTest extends AbstractBaseNOutputStreamTest {
34
35 private static final String STRING_FIXTURE = "Hello World";
36
37 @Override
38 OutputStream newOutputStream() {
39 return new Base16OutputStream(new ByteArrayOutputStream());
40 }
41
42
43
44
45
46
47 @Test
48 void testBase16EmptyOutputStream() throws IOException {
49 final byte[] emptyEncoded = {};
50 final byte[] emptyDecoded = {};
51 testByteByByte(emptyEncoded, emptyDecoded);
52 testByChunk(emptyEncoded, emptyDecoded);
53 }
54
55
56
57
58
59
60 @Test
61 void testBase16OutputStreamByChunk() throws IOException {
62
63 byte[] encoded = StringUtils.getBytesUtf8("48656C6C6F20576F726C64");
64 byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
65 testByChunk(encoded, decoded);
66
67
68 encoded = StringUtils.getBytesUtf8("41");
69 decoded = new byte[]{(byte) 0x41};
70 testByChunk(encoded, decoded);
71
72
73 final BaseNCodec codec = Base16.builder().setLowerCase(true).get();
74 for (int i = 0; i <= 150; i++) {
75 final byte[][] randomData = BaseNTestData.randomData(codec, i);
76 encoded = randomData[1];
77 decoded = randomData[0];
78 testByChunk(encoded, decoded, true);
79 }
80 }
81
82
83
84
85
86
87 @Test
88 void testBase16OutputStreamByteByByte() throws IOException {
89
90 byte[] encoded = StringUtils.getBytesUtf8("48656C6C6F20576F726C64");
91 byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
92 testByteByByte(encoded, decoded);
93
94 encoded = StringUtils.getBytesUtf8("41");
95 decoded = new byte[] { (byte) 0x41 };
96 testByteByByte(encoded, decoded);
97
98 final BaseNCodec codec = Base16.builder().setLowerCase(true).get();
99 for (int i = 0; i <= 150; i++) {
100 final byte[][] randomData = BaseNTestData.randomData(codec, i);
101 encoded = randomData[1];
102 decoded = randomData[0];
103 testByteByByte(encoded, decoded, true);
104 }
105 }
106
107 @Test
108 void testBuilder() {
109 assertNotNull(Base16OutputStream.builder().getBaseNCodec());
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 private void testByChunk(final byte[] encoded, final byte[] decoded) throws IOException {
124 testByChunk(encoded, decoded, false);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 private void testByChunk(final byte[] encoded, final byte[] decoded, final boolean lowerCase) throws IOException {
140
141 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
142 OutputStream out = new Base16OutputStream(byteOut, true, lowerCase)) {
143 out.write(decoded);
144 final byte[] output = byteOut.toByteArray();
145 assertArrayEquals(encoded, output, "Streaming chunked base16 encode");
146 }
147
148 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
149 OutputStream out = new Base16OutputStream(byteOut, false, lowerCase)) {
150 out.write(encoded);
151 final byte[] output = byteOut.toByteArray();
152 assertArrayEquals(decoded, output, "Streaming chunked base16 decode");
153 }
154
155 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
156 OutputStream decoderOut = new Base16OutputStream(byteOut, false, lowerCase);
157 OutputStream encoderOut = new Base16OutputStream(decoderOut, true, lowerCase)) {
158
159 encoderOut.write(decoded);
160 final byte[] output = byteOut.toByteArray();
161 assertArrayEquals(decoded, output, "Streaming chunked base16 wrap-wrap!");
162 }
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176 private void testByteByByte(final byte[] encoded, final byte[] decoded) throws IOException {
177 testByteByByte(encoded, decoded, false);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191 private void testByteByByte(final byte[] encoded, final byte[] decoded, final boolean lowerCase) throws IOException {
192
193 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
194 OutputStream out = new Base16OutputStream(byteOut, true, lowerCase)) {
195 for (final byte element : decoded) {
196 out.write(element);
197 }
198 final byte[] output = byteOut.toByteArray();
199 assertArrayEquals(encoded, output, "Streaming byte-by-byte base16 encode");
200 }
201 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
202 OutputStream out = Base16OutputStream.builder()
203 .setOutputStream(byteOut).setEncode(true).setBaseNCodec(Base16.builder().setLowerCase(lowerCase).get())
204 .get()) {
205 for (final byte element : decoded) {
206 out.write(element);
207 }
208 final byte[] output = byteOut.toByteArray();
209 assertArrayEquals(encoded, output, "Streaming byte-by-byte base16 encode");
210 }
211
212 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
213 OutputStream out = new Base16OutputStream(byteOut, false, lowerCase)) {
214 for (final byte element : encoded) {
215 out.write(element);
216 }
217 final byte[] output = byteOut.toByteArray();
218 assertArrayEquals(decoded, output, "Streaming byte-by-byte base16 decode");
219 }
220
221 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
222 OutputStream out = new Base16OutputStream(byteOut, false, lowerCase)) {
223 for (final byte element : encoded) {
224 out.write(element);
225 out.flush();
226 }
227 final byte[] output = byteOut.toByteArray();
228 assertArrayEquals(decoded, output, "Streaming byte-by-byte flush() base16 decode");
229 }
230
231 try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
232 OutputStream decoderOut = new Base16OutputStream(byteOut, false, lowerCase);
233 OutputStream encoderOut = new Base16OutputStream(decoderOut, true, lowerCase)) {
234 for (final byte element : decoded) {
235 encoderOut.write(element);
236 }
237 final byte[] output = byteOut.toByteArray();
238 assertArrayEquals(decoded, output, "Streaming byte-by-byte base16 wrap-wrap!");
239 }
240 }
241
242 @Override
243 @Test
244 void testClose() throws IOException {
245 try (OutputStream out = newOutputStream()) {
246 out.close();
247 out.close();
248 }
249 }
250
251
252
253
254
255
256 @Test
257 void testWriteOutOfBounds() throws IOException {
258 final byte[] buf = new byte[1024];
259 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
260 try (Base16OutputStream out = new Base16OutputStream(bout)) {
261 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1), "Base16InputStream.write(buf, -1, 0)");
262 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1), "Base16InputStream.write(buf, 1, -1)");
263 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0), "Base16InputStream.write(buf, buf.length + 1, 0)");
264 assertThrows(IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2), "Base16InputStream.write(buf, buf.length - 1, 2)");
265 }
266 }
267
268
269
270
271
272
273 @Test
274 void testWriteToNullCoverage() throws IOException {
275 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
276 try (Base16OutputStream out = new Base16OutputStream(bout)) {
277 assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
278 }
279 }
280 }