View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link Base16OutputStream}.
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       * Test the Base16OutputStream implementation against empty input.
44       *
45       * @throws IOException for some failure scenarios.
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       * Test the Base16OutputStream implementation
57       *
58       * @throws IOException for some failure scenarios.
59       */
60      @Test
61      void testBase16OutputStreamByChunk() throws IOException {
62          // Hello World test.
63          byte[] encoded = StringUtils.getBytesUtf8("48656C6C6F20576F726C64");
64          byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
65          testByChunk(encoded, decoded);
66  
67          // Single Byte test.
68          encoded = StringUtils.getBytesUtf8("41");
69          decoded = new byte[]{(byte) 0x41};
70          testByChunk(encoded, decoded);
71  
72          // test random data of sizes 0 through 150
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       * Test the Base16OutputStream implementation
84       *
85       * @throws IOException for some failure scenarios.
86       */
87      @Test
88      void testBase16OutputStreamByteByByte() throws IOException {
89          // Hello World test.
90          byte[] encoded = StringUtils.getBytesUtf8("48656C6C6F20576F726C64");
91          byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
92          testByteByByte(encoded, decoded);
93          // Single Byte test.
94          encoded = StringUtils.getBytesUtf8("41");
95          decoded = new byte[] { (byte) 0x41 };
96          testByteByByte(encoded, decoded);
97          // test random data of sizes 0 through 150
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      * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
114      * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
115      * <p/>
116      * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base16OutputStream wraps itself in encode and decode
117      * mode over and over again.
118      *
119      * @param encoded base16 encoded data
120      * @param decoded the data from above, but decoded
121      * @throws IOException Usually signifies a bug in the Base16 commons-codec implementation.
122      */
123     private void testByChunk(final byte[] encoded, final byte[] decoded) throws IOException {
124         testByChunk(encoded, decoded, false);
125     }
126 
127     /**
128      * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
129      * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
130      * <p/>
131      * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base16OutputStream wraps itself in encode and decode
132      * mode over and over again.
133      *
134      * @param encoded base16 encoded data
135      * @param decoded the data from above, but decoded
136      * @param lowerCase if {@code true} then use a lower-case Base16 alphabet
137      * @throws IOException Usually signifies a bug in the Base16 commons-codec implementation.
138      */
139     private void testByChunk(final byte[] encoded, final byte[] decoded, final boolean lowerCase) throws IOException {
140         // Start with encode.
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         // Now let's try to decode.
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         // wrap encoder with decoder
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      * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
167      * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
168      * <p/>
169      * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base16OutputStream wraps itself in encode and decode
170      * mode over and over again.
171      *
172      * @param encoded base16 encoded data
173      * @param decoded the data from above, but decoded
174      * @throws IOException Usually signifies a bug in the Base16 commons-codec implementation.
175      */
176     private void testByteByByte(final byte[] encoded, final byte[] decoded) throws IOException {
177         testByteByByte(encoded, decoded, false);
178     }
179 
180     /**
181      * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
182      * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
183      * <p/>
184      * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base16OutputStream wraps itself in encode and decode
185      * mode over and over again.
186      *
187      * @param encoded base16 encoded data
188      * @param decoded the data from above, but decoded
189      * @throws IOException Usually signifies a bug in the Base16 commons-codec implementation.
190      */
191     private void testByteByByte(final byte[] encoded, final byte[] decoded, final boolean lowerCase) throws IOException {
192         // Start with encode.
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         // Now let's try to decode.
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         // Now let's try to decode with tonnes of flushes.
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         // wrap encoder with decoder
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      * Tests Base16OutputStream.write for expected IndexOutOfBoundsException conditions.
253      *
254      * @throws IOException for some failure scenarios.
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      * Tests Base16OutputStream.write(null).
270      *
271      * @throws IOException for some failure scenarios.
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 }