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.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.nio.charset.Charset;
28  import java.nio.charset.StandardCharsets;
29  import java.util.Random;
30  
31  import org.apache.commons.codec.CodecPolicy;
32  import org.apache.commons.codec.DecoderException;
33  import org.apache.commons.codec.EncoderException;
34  import org.apache.commons.lang3.ArrayUtils;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Tests {@link Base16}.
39   */
40  class Base16Test {
41  
42      private static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
43  
44      private final Random random = new Random();
45  
46      /**
47       * @return the random.
48       */
49      public Random getRandom() {
50          return this.random;
51      }
52  
53      /**
54       * Test the Base16 implementation
55       */
56      @Test
57      void testBase16() {
58          final String content = "Hello World";
59          final byte[] encodedBytes = new Base16().encode(StringUtils.getBytesUtf8(content));
60          final String encodedContent = StringUtils.newStringUtf8(encodedBytes);
61          assertEquals("48656C6C6F20576F726C64", encodedContent, "encoding hello world");
62  
63          final byte[] decodedBytes = new Base16().decode(encodedBytes);
64          final String decodedContent = StringUtils.newStringUtf8(decodedBytes);
65          assertEquals(content, decodedContent, "decoding hello world");
66      }
67  
68      @Test
69      void testBase16AtBufferEnd() {
70          testBase16InBuffer(100, 0);
71      }
72  
73      @Test
74      void testBase16AtBufferMiddle() {
75          testBase16InBuffer(100, 100);
76      }
77  
78      @Test
79      void testBase16AtBufferStart() {
80          testBase16InBuffer(0, 100);
81      }
82  
83      private void testBase16InBuffer(final int startPasSize, final int endPadSize) {
84          final String content = "Hello World";
85          final String encodedContent;
86          final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content);
87          byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]);
88          buffer = ArrayUtils.addAll(new byte[startPasSize], buffer);
89          final byte[] encodedBytes = new Base16().encode(buffer, startPasSize, bytesUtf8.length);
90          encodedContent = StringUtils.newStringUtf8(encodedBytes);
91          assertEquals("48656C6C6F20576F726C64", encodedContent, "encoding hello world");
92      }
93  
94      @Test
95      void testByteToStringVariations() {
96          final Base16 base16 = new Base16();
97          final byte[] b1 = StringUtils.getBytesUtf8("Hello World");
98          final byte[] b2 = {};
99          final byte[] b3 = null;
100 
101         assertEquals("48656C6C6F20576F726C64", base16.encodeToString(b1), "byteToString Hello World");
102         assertEquals("48656C6C6F20576F726C64", StringUtils.newStringUtf8(new Base16().encode(b1)), "byteToString static Hello World");
103         assertEquals("", base16.encodeToString(b2), "byteToString \"\"");
104         assertEquals("", StringUtils.newStringUtf8(new Base16().encode(b2)), "byteToString static \"\"");
105         assertNull(base16.encodeToString(b3), "byteToString null");
106         assertNull(StringUtils.newStringUtf8(new Base16().encode(b3)), "byteToString static null");
107     }
108 
109     @Test
110     void testCheckEncodeLengthBounds() {
111         final Base16 base16 = new Base16();
112         assertThrows(IllegalArgumentException.class, () -> base16.encode(new byte[10], 0, 1 << 30));
113     }
114 
115     /**
116      * isBase16 throws RuntimeException on some non-Base16 bytes
117      */
118     @Test
119     void testCodec68() {
120         final byte[] x = { 'n', 'H', '=', '=', (byte) 0x9c };
121         final Base16 b16 = new Base16();
122         assertThrows(RuntimeException.class, () -> b16.decode(x));
123     }
124 
125     @Test
126     void testConstructor_LowerCase() {
127         final Base16 base16 = new Base16(true);
128         final byte[] encoded = base16.encode(BaseNTestData.DECODED);
129         final String expectedResult = Base16TestData.ENCODED_UTF8_LOWERCASE;
130         final String result = StringUtils.newStringUtf8(encoded);
131         assertEquals(expectedResult, result, "new Base16(true)");
132     }
133 
134     @Test
135     void testConstructor_LowerCase_DecodingPolicy() {
136         final Base16 base16 = new Base16(false, CodecPolicy.STRICT);
137         final byte[] encoded = base16.encode(BaseNTestData.DECODED);
138         final String expectedResult = Base16TestData.ENCODED_UTF8_UPPERCASE;
139         final String result = StringUtils.newStringUtf8(encoded);
140         assertEquals(result, expectedResult, "new base16(false, CodecPolicy.STRICT)");
141     }
142 
143     @Test
144     void testConstructors() {
145         new Base16();
146         new Base16(false);
147         new Base16(true);
148         new Base16(false, CodecPolicy.LENIENT);
149         new Base16(false, CodecPolicy.STRICT);
150     }
151 
152     @Test
153     void testDecodeSingleBytes() {
154         final String encoded = "556E74696C206E6578742074696D6521";
155 
156         final BaseNCodec.Context context = new BaseNCodec.Context();
157         final Base16 b16 = new Base16();
158 
159         final byte[] encocdedBytes = StringUtils.getBytesUtf8(encoded);
160 
161         // decode byte-by-byte
162         b16.decode(encocdedBytes, 0, 1, context);
163         b16.decode(encocdedBytes, 1, 1, context); // yields "U"
164         b16.decode(encocdedBytes, 2, 1, context);
165         b16.decode(encocdedBytes, 3, 1, context); // yields "n"
166 
167         // decode split hex-pairs
168         b16.decode(encocdedBytes, 4, 3, context); // yields "t"
169         b16.decode(encocdedBytes, 7, 3, context); // yields "il"
170         b16.decode(encocdedBytes, 10, 3, context); // yields " "
171 
172         // decode remaining
173         b16.decode(encocdedBytes, 13, 19, context); // yields "next time!"
174 
175         final byte[] decodedBytes = new byte[context.pos];
176         System.arraycopy(context.buffer, context.readPos, decodedBytes, 0, decodedBytes.length);
177         final String decoded = StringUtils.newStringUtf8(decodedBytes);
178 
179         assertEquals("Until next time!", decoded);
180     }
181 
182     @Test
183     void testDecodeSingleBytesOptimization() {
184         final BaseNCodec.Context context = new BaseNCodec.Context();
185         assertEquals(0, context.ibitWorkArea);
186         assertNull(context.buffer);
187 
188         final byte[] data = new byte[1];
189 
190         final Base16 b16 = new Base16();
191 
192         data[0] = (byte) 'E';
193         b16.decode(data, 0, 1, context);
194         assertEquals(15, context.ibitWorkArea);
195         assertNull(context.buffer);
196 
197         data[0] = (byte) 'F';
198         b16.decode(data, 0, 1, context);
199         assertEquals(0, context.ibitWorkArea);
200 
201         assertEquals((byte) 0xEF, context.buffer[0]);
202     }
203 
204     /**
205      * Test encode and decode of empty byte array.
206      */
207     @Test
208     void testEmptyBase16() {
209         byte[] empty = {};
210         byte[] result = new Base16().encode(empty);
211         assertEquals(0, result.length, "empty Base16 encode");
212         assertNull(new Base16().encode(null), "empty Base16 encode");
213         result = new Base16().encode(empty, 0, 1);
214         assertEquals(0, result.length, "empty Base16 encode with offset");
215         assertNull(new Base16().encode(null), "empty Base16 encode with offset");
216 
217         empty = new byte[0];
218         result = new Base16().decode(empty);
219         assertEquals(0, result.length, "empty Base16 decode");
220         assertNull(new Base16().decode((byte[]) null), "empty Base16 encode");
221     }
222 
223     // encode/decode a large random array
224     @Test
225     void testEncodeDecodeRandom() {
226         for (int i = 1; i < 5; i++) {
227             final int len = getRandom().nextInt(10000) + 1;
228             final byte[] data = new byte[len];
229             getRandom().nextBytes(data);
230             final byte[] enc = new Base16().encode(data);
231             final byte[] data2 = new Base16().decode(enc);
232             assertArrayEquals(data, data2);
233         }
234     }
235 
236     // encode/decode random arrays from size 0 to size 11
237     @Test
238     void testEncodeDecodeSmall() {
239         for (int i = 0; i < 12; i++) {
240             final byte[] data = new byte[i];
241             getRandom().nextBytes(data);
242             final byte[] enc = new Base16().encode(data);
243             final byte[] data2 = new Base16().decode(enc);
244             assertArrayEquals(data, data2, toString(data) + " equals " + toString(data2));
245         }
246     }
247 
248     @Test
249     void testIsInAlphabet() {
250         // invalid bounds
251         Base16 b16 = Base16.builder().setLowerCase(true).get();
252         assertFalse(b16.isInAlphabet((byte) 0));
253         assertFalse(b16.isInAlphabet((byte) 1));
254         assertFalse(b16.isInAlphabet((byte) -1));
255         assertFalse(b16.isInAlphabet((byte) -15));
256         assertFalse(b16.isInAlphabet((byte) -16));
257         assertFalse(b16.isInAlphabet((byte) 128));
258         assertFalse(b16.isInAlphabet((byte) 255));
259         // lower-case
260         b16 = new Base16(true);
261         for (char c = '0'; c <= '9'; c++) {
262             assertTrue(b16.isInAlphabet((byte) c));
263         }
264         for (char c = 'a'; c <= 'f'; c++) {
265             assertTrue(b16.isInAlphabet((byte) c));
266         }
267         for (char c = 'A'; c <= 'F'; c++) {
268             assertFalse(b16.isInAlphabet((byte) c));
269         }
270         assertFalse(b16.isInAlphabet((byte) ('0' - 1)));
271         assertFalse(b16.isInAlphabet((byte) ('9' + 1)));
272         assertFalse(b16.isInAlphabet((byte) ('a' - 1)));
273         assertFalse(b16.isInAlphabet((byte) ('f' + 1)));
274         assertFalse(b16.isInAlphabet((byte) ('z' + 1)));
275         // upper-case
276         b16 = new Base16(false);
277         for (char c = '0'; c <= '9'; c++) {
278             assertTrue(b16.isInAlphabet((byte) c));
279         }
280         for (char c = 'a'; c <= 'f'; c++) {
281             assertFalse(b16.isInAlphabet((byte) c));
282         }
283         for (char c = 'A'; c <= 'F'; c++) {
284             assertTrue(b16.isInAlphabet((byte) c));
285         }
286         assertFalse(b16.isInAlphabet((byte) ('0' - 1)));
287         assertFalse(b16.isInAlphabet((byte) ('9' + 1)));
288         assertFalse(b16.isInAlphabet((byte) ('A' - 1)));
289         assertFalse(b16.isInAlphabet((byte) ('F' + 1)));
290         assertFalse(b16.isInAlphabet((byte) ('Z' + 1)));
291     }
292 
293     @Test
294     void testKnownDecodings() {
295         assertEquals("The quick brown fox jumped over the lazy dogs.", new String(new Base16(true)
296                 .decode("54686520717569636b2062726f776e20666f78206a756d706564206f76657220746865206c617a7920646f67732e".getBytes(CHARSET_UTF8))));
297         assertEquals("It was the best of times, it was the worst of times.", new String(new Base16(true)
298                 .decode("497420776173207468652062657374206f662074696d65732c206974207761732074686520776f727374206f662074696d65732e".getBytes(CHARSET_UTF8))));
299         assertEquals("http://jakarta.apache.org/commmons",
300                 new String(new Base16(true).decode("687474703a2f2f6a616b617274612e6170616368652e6f72672f636f6d6d6d6f6e73".getBytes(CHARSET_UTF8))));
301         assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", new String(new Base16(true)
302                 .decode("4161426243634464456546664767486849694a6a4b6b4c6c4d6d4e6e4f6f50705171527253735474557556765777587859795a7a".getBytes(CHARSET_UTF8))));
303         assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",
304                 new String(new Base16(true).decode("7b20302c20312c20322c20332c20342c20352c20362c20372c20382c2039207d".getBytes(CHARSET_UTF8))));
305         assertEquals("xyzzy!", new String(new Base16(true).decode("78797a7a7921".getBytes(CHARSET_UTF8))));
306     }
307 
308     @Test
309     void testKnownEncodings() {
310         assertEquals("54686520717569636b2062726f776e20666f78206a756d706564206f76657220746865206c617a7920646f67732e",
311                 new String(new Base16(true).encode("The quick brown fox jumped over the lazy dogs.".getBytes(CHARSET_UTF8))));
312         assertEquals("497420776173207468652062657374206f662074696d65732c206974207761732074686520776f727374206f662074696d65732e",
313                 new String(new Base16(true).encode("It was the best of times, it was the worst of times.".getBytes(CHARSET_UTF8))));
314         assertEquals("687474703a2f2f6a616b617274612e6170616368652e6f72672f636f6d6d6d6f6e73",
315                 new String(new Base16(true).encode("http://jakarta.apache.org/commmons".getBytes(CHARSET_UTF8))));
316         assertEquals("4161426243634464456546664767486849694a6a4b6b4c6c4d6d4e6e4f6f50705171527253735474557556765777587859795a7a",
317                 new String(new Base16(true).encode("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes(CHARSET_UTF8))));
318         assertEquals("7b20302c20312c20322c20332c20342c20352c20362c20372c20382c2039207d",
319                 new String(new Base16(true).encode("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }".getBytes(CHARSET_UTF8))));
320         assertEquals("78797a7a7921", new String(new Base16(true).encode("xyzzy!".getBytes(CHARSET_UTF8))));
321     }
322 
323     @Test
324     void testLenientDecoding() {
325         final String encoded = "aabbccdde"; // Note the trailing `e` which does not make up a hex-pair and so is only 1/2 byte
326 
327         final Base16 b16 = new Base16(true, CodecPolicy.LENIENT);
328         assertEquals(CodecPolicy.LENIENT, b16.getCodecPolicy());
329 
330         final byte[] decoded = b16.decode(StringUtils.getBytesUtf8(encoded));
331         assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, decoded);
332     }
333 
334     @Test
335     void testNonBase16Test() {
336         final byte[] invalidEncodedChars = { '/', ':', '@', 'G', '%', '`', 'g' };
337 
338         final byte[] encoded = new byte[1];
339         for (final byte invalidEncodedChar : invalidEncodedChars) {
340             encoded[0] = invalidEncodedChar;
341             assertThrows(IllegalArgumentException.class, () -> new Base16().decode(encoded), "Invalid Base16 char: " + (char) invalidEncodedChar);
342         }
343     }
344 
345     @Test
346     void testObjectDecodeWithInvalidParameter() {
347         assertThrows(DecoderException.class, () -> new Base16().decode(Integer.valueOf(5)));
348     }
349 
350     @Test
351     void testObjectDecodeWithValidParameter() throws Exception {
352         final String original = "Hello World!";
353         final Object o = new Base16().encode(original.getBytes(CHARSET_UTF8));
354 
355         final Base16 b16 = new Base16();
356         final Object oDecoded = b16.decode(o);
357         final byte[] baDecoded = (byte[]) oDecoded;
358         final String dest = new String(baDecoded);
359 
360         assertEquals(original, dest, "dest string does not equal original");
361     }
362 
363     @Test
364     void testObjectEncode() {
365         final Base16 b16 = new Base16();
366         assertEquals(new String(b16.encode("Hello World".getBytes(CHARSET_UTF8))), "48656C6C6F20576F726C64");
367     }
368 
369     @Test
370     void testObjectEncodeWithInvalidParameter() {
371         assertThrows(EncoderException.class, () -> new Base16().encode("Yadayadayada"));
372     }
373 
374     @Test
375     void testObjectEncodeWithValidParameter() throws Exception {
376         final String original = "Hello World!";
377         final Object origObj = original.getBytes(CHARSET_UTF8);
378 
379         final Object oEncoded = new Base16().encode(origObj);
380         final byte[] bArray = new Base16().decode((byte[]) oEncoded);
381         final String dest = new String(bArray);
382 
383         assertEquals(original, dest, "dest string does not equal original");
384     }
385 
386     @Test
387     void testOddEvenDecoding() {
388         final String encoded = "4142434445";
389 
390         final BaseNCodec.Context context = new BaseNCodec.Context();
391         final Base16 base16 = new Base16();
392 
393         final byte[] encodedBytes = StringUtils.getBytesUtf8(encoded);
394 
395         // pass odd, then even, then odd amount of data
396         base16.decode(encodedBytes, 0, 3, context);
397         base16.decode(encodedBytes, 3, 4, context);
398         base16.decode(encodedBytes, 7, 3, context);
399 
400         final byte[] decodedBytes = new byte[context.pos];
401         System.arraycopy(context.buffer, context.readPos, decodedBytes, 0, decodedBytes.length);
402         final String decoded = StringUtils.newStringUtf8(decodedBytes);
403 
404         assertEquals("ABCDE", decoded);
405     }
406 
407     @Test
408     void testPairs() {
409         assertEquals("0000", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0 })));
410         assertEquals("0001", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 1 })));
411         assertEquals("0002", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 2 })));
412         assertEquals("0003", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 3 })));
413         assertEquals("0004", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 4 })));
414         assertEquals("0005", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 5 })));
415         assertEquals("0006", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 6 })));
416         assertEquals("0007", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 7 })));
417         assertEquals("0008", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 8 })));
418         assertEquals("0009", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 9 })));
419         assertEquals("000A", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 10 })));
420         assertEquals("000B", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 11 })));
421         assertEquals("000C", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 12 })));
422         assertEquals("000D", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 13 })));
423         assertEquals("000E", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 14 })));
424         assertEquals("000F", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 15 })));
425         assertEquals("0010", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 16 })));
426         assertEquals("0011", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 17 })));
427         for (int i = -128; i <= 127; i++) {
428             final byte[] test = { (byte) i, (byte) i };
429             assertArrayEquals(test, new Base16().decode(new Base16().encode(test)));
430         }
431     }
432 
433     @Test
434     void testSingletons() {
435         assertEquals("00", new String(new Base16().encode(new byte[] { (byte) 0 })));
436         assertEquals("01", new String(new Base16().encode(new byte[] { (byte) 1 })));
437         assertEquals("02", new String(new Base16().encode(new byte[] { (byte) 2 })));
438         assertEquals("03", new String(new Base16().encode(new byte[] { (byte) 3 })));
439         assertEquals("04", new String(new Base16().encode(new byte[] { (byte) 4 })));
440         assertEquals("05", new String(new Base16().encode(new byte[] { (byte) 5 })));
441         assertEquals("06", new String(new Base16().encode(new byte[] { (byte) 6 })));
442         assertEquals("07", new String(new Base16().encode(new byte[] { (byte) 7 })));
443         assertEquals("08", new String(new Base16().encode(new byte[] { (byte) 8 })));
444         assertEquals("09", new String(new Base16().encode(new byte[] { (byte) 9 })));
445         assertEquals("0A", new String(new Base16().encode(new byte[] { (byte) 10 })));
446         assertEquals("0B", new String(new Base16().encode(new byte[] { (byte) 11 })));
447         assertEquals("0C", new String(new Base16().encode(new byte[] { (byte) 12 })));
448         assertEquals("0D", new String(new Base16().encode(new byte[] { (byte) 13 })));
449         assertEquals("0E", new String(new Base16().encode(new byte[] { (byte) 14 })));
450         assertEquals("0F", new String(new Base16().encode(new byte[] { (byte) 15 })));
451         assertEquals("10", new String(new Base16().encode(new byte[] { (byte) 16 })));
452         assertEquals("11", new String(new Base16().encode(new byte[] { (byte) 17 })));
453         assertEquals("12", new String(new Base16().encode(new byte[] { (byte) 18 })));
454         assertEquals("13", new String(new Base16().encode(new byte[] { (byte) 19 })));
455         assertEquals("14", new String(new Base16().encode(new byte[] { (byte) 20 })));
456         assertEquals("15", new String(new Base16().encode(new byte[] { (byte) 21 })));
457         assertEquals("16", new String(new Base16().encode(new byte[] { (byte) 22 })));
458         assertEquals("17", new String(new Base16().encode(new byte[] { (byte) 23 })));
459         assertEquals("18", new String(new Base16().encode(new byte[] { (byte) 24 })));
460         assertEquals("19", new String(new Base16().encode(new byte[] { (byte) 25 })));
461         assertEquals("1A", new String(new Base16().encode(new byte[] { (byte) 26 })));
462         assertEquals("1B", new String(new Base16().encode(new byte[] { (byte) 27 })));
463         assertEquals("1C", new String(new Base16().encode(new byte[] { (byte) 28 })));
464         assertEquals("1D", new String(new Base16().encode(new byte[] { (byte) 29 })));
465         assertEquals("1E", new String(new Base16().encode(new byte[] { (byte) 30 })));
466         assertEquals("1F", new String(new Base16().encode(new byte[] { (byte) 31 })));
467         assertEquals("20", new String(new Base16().encode(new byte[] { (byte) 32 })));
468         assertEquals("21", new String(new Base16().encode(new byte[] { (byte) 33 })));
469         assertEquals("22", new String(new Base16().encode(new byte[] { (byte) 34 })));
470         assertEquals("23", new String(new Base16().encode(new byte[] { (byte) 35 })));
471         assertEquals("24", new String(new Base16().encode(new byte[] { (byte) 36 })));
472         assertEquals("25", new String(new Base16().encode(new byte[] { (byte) 37 })));
473         assertEquals("26", new String(new Base16().encode(new byte[] { (byte) 38 })));
474         assertEquals("27", new String(new Base16().encode(new byte[] { (byte) 39 })));
475         assertEquals("28", new String(new Base16().encode(new byte[] { (byte) 40 })));
476         assertEquals("29", new String(new Base16().encode(new byte[] { (byte) 41 })));
477         assertEquals("2A", new String(new Base16().encode(new byte[] { (byte) 42 })));
478         assertEquals("2B", new String(new Base16().encode(new byte[] { (byte) 43 })));
479         assertEquals("2C", new String(new Base16().encode(new byte[] { (byte) 44 })));
480         assertEquals("2D", new String(new Base16().encode(new byte[] { (byte) 45 })));
481         assertEquals("2E", new String(new Base16().encode(new byte[] { (byte) 46 })));
482         assertEquals("2F", new String(new Base16().encode(new byte[] { (byte) 47 })));
483         assertEquals("30", new String(new Base16().encode(new byte[] { (byte) 48 })));
484         assertEquals("31", new String(new Base16().encode(new byte[] { (byte) 49 })));
485         assertEquals("32", new String(new Base16().encode(new byte[] { (byte) 50 })));
486         assertEquals("33", new String(new Base16().encode(new byte[] { (byte) 51 })));
487         assertEquals("34", new String(new Base16().encode(new byte[] { (byte) 52 })));
488         assertEquals("35", new String(new Base16().encode(new byte[] { (byte) 53 })));
489         assertEquals("36", new String(new Base16().encode(new byte[] { (byte) 54 })));
490         assertEquals("37", new String(new Base16().encode(new byte[] { (byte) 55 })));
491         assertEquals("38", new String(new Base16().encode(new byte[] { (byte) 56 })));
492         assertEquals("39", new String(new Base16().encode(new byte[] { (byte) 57 })));
493         assertEquals("3A", new String(new Base16().encode(new byte[] { (byte) 58 })));
494         assertEquals("3B", new String(new Base16().encode(new byte[] { (byte) 59 })));
495         assertEquals("3C", new String(new Base16().encode(new byte[] { (byte) 60 })));
496         assertEquals("3D", new String(new Base16().encode(new byte[] { (byte) 61 })));
497         assertEquals("3E", new String(new Base16().encode(new byte[] { (byte) 62 })));
498         assertEquals("3F", new String(new Base16().encode(new byte[] { (byte) 63 })));
499         assertEquals("40", new String(new Base16().encode(new byte[] { (byte) 64 })));
500         assertEquals("41", new String(new Base16().encode(new byte[] { (byte) 65 })));
501         assertEquals("42", new String(new Base16().encode(new byte[] { (byte) 66 })));
502         assertEquals("43", new String(new Base16().encode(new byte[] { (byte) 67 })));
503         assertEquals("44", new String(new Base16().encode(new byte[] { (byte) 68 })));
504         assertEquals("45", new String(new Base16().encode(new byte[] { (byte) 69 })));
505         assertEquals("46", new String(new Base16().encode(new byte[] { (byte) 70 })));
506         assertEquals("47", new String(new Base16().encode(new byte[] { (byte) 71 })));
507         assertEquals("48", new String(new Base16().encode(new byte[] { (byte) 72 })));
508         assertEquals("49", new String(new Base16().encode(new byte[] { (byte) 73 })));
509         assertEquals("4A", new String(new Base16().encode(new byte[] { (byte) 74 })));
510         assertEquals("4B", new String(new Base16().encode(new byte[] { (byte) 75 })));
511         assertEquals("4C", new String(new Base16().encode(new byte[] { (byte) 76 })));
512         assertEquals("4D", new String(new Base16().encode(new byte[] { (byte) 77 })));
513         assertEquals("4E", new String(new Base16().encode(new byte[] { (byte) 78 })));
514         assertEquals("4F", new String(new Base16().encode(new byte[] { (byte) 79 })));
515         assertEquals("50", new String(new Base16().encode(new byte[] { (byte) 80 })));
516         assertEquals("51", new String(new Base16().encode(new byte[] { (byte) 81 })));
517         assertEquals("52", new String(new Base16().encode(new byte[] { (byte) 82 })));
518         assertEquals("53", new String(new Base16().encode(new byte[] { (byte) 83 })));
519         assertEquals("54", new String(new Base16().encode(new byte[] { (byte) 84 })));
520         assertEquals("55", new String(new Base16().encode(new byte[] { (byte) 85 })));
521         assertEquals("56", new String(new Base16().encode(new byte[] { (byte) 86 })));
522         assertEquals("57", new String(new Base16().encode(new byte[] { (byte) 87 })));
523         assertEquals("58", new String(new Base16().encode(new byte[] { (byte) 88 })));
524         assertEquals("59", new String(new Base16().encode(new byte[] { (byte) 89 })));
525         assertEquals("5A", new String(new Base16().encode(new byte[] { (byte) 90 })));
526         assertEquals("5B", new String(new Base16().encode(new byte[] { (byte) 91 })));
527         assertEquals("5C", new String(new Base16().encode(new byte[] { (byte) 92 })));
528         assertEquals("5D", new String(new Base16().encode(new byte[] { (byte) 93 })));
529         assertEquals("5E", new String(new Base16().encode(new byte[] { (byte) 94 })));
530         assertEquals("5F", new String(new Base16().encode(new byte[] { (byte) 95 })));
531         assertEquals("60", new String(new Base16().encode(new byte[] { (byte) 96 })));
532         assertEquals("61", new String(new Base16().encode(new byte[] { (byte) 97 })));
533         assertEquals("62", new String(new Base16().encode(new byte[] { (byte) 98 })));
534         assertEquals("63", new String(new Base16().encode(new byte[] { (byte) 99 })));
535         assertEquals("64", new String(new Base16().encode(new byte[] { (byte) 100 })));
536         assertEquals("65", new String(new Base16().encode(new byte[] { (byte) 101 })));
537         assertEquals("66", new String(new Base16().encode(new byte[] { (byte) 102 })));
538         assertEquals("67", new String(new Base16().encode(new byte[] { (byte) 103 })));
539         assertEquals("68", new String(new Base16().encode(new byte[] { (byte) 104 })));
540         for (int i = -128; i <= 127; i++) {
541             final byte[] test = { (byte) i };
542             assertArrayEquals(test, new Base16().decode(new Base16().encode(test)));
543         }
544     }
545 
546     @Test
547     void testStrictDecoding() {
548         final String encoded = "aabbccdde"; // Note the trailing `e` which does not make up a hex-pair and so is only 1/2 byte
549 
550         final Base16 b16 = new Base16(true, CodecPolicy.STRICT);
551         assertEquals(CodecPolicy.STRICT, b16.getCodecPolicy());
552         assertThrows(IllegalArgumentException.class, () -> b16.decode(StringUtils.getBytesUtf8(encoded)));
553     }
554 
555     @Test
556     void testStringToByteVariations() throws DecoderException {
557         final Base16 base16 = new Base16();
558         final String s1 = "48656C6C6F20576F726C64";
559         final String s2 = "";
560         final String s3 = null;
561 
562         assertEquals("Hello World", StringUtils.newStringUtf8(base16.decode(s1)), "StringToByte Hello World");
563         assertEquals("Hello World", StringUtils.newStringUtf8((byte[]) new Base16().decode((Object) s1)), "StringToByte Hello World");
564         assertEquals("Hello World", StringUtils.newStringUtf8(new Base16().decode(s1)), "StringToByte static Hello World");
565         assertEquals("", StringUtils.newStringUtf8(new Base16().decode(s2)), "StringToByte \"\"");
566         assertEquals("", StringUtils.newStringUtf8(new Base16().decode(s2)), "StringToByte static \"\"");
567         assertNull(StringUtils.newStringUtf8(new Base16().decode(s3)), "StringToByte null");
568         assertNull(StringUtils.newStringUtf8(new Base16().decode(s3)), "StringToByte static null");
569     }
570 
571     @Test
572     void testTriplets() {
573         assertEquals("000000", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 0 })));
574         assertEquals("000001", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 1 })));
575         assertEquals("000002", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 2 })));
576         assertEquals("000003", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 3 })));
577         assertEquals("000004", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 4 })));
578         assertEquals("000005", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 5 })));
579         assertEquals("000006", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 6 })));
580         assertEquals("000007", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 7 })));
581         assertEquals("000008", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 8 })));
582         assertEquals("000009", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 9 })));
583         assertEquals("00000A", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 10 })));
584         assertEquals("00000B", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 11 })));
585         assertEquals("00000C", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 12 })));
586         assertEquals("00000D", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 13 })));
587         assertEquals("00000E", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 14 })));
588         assertEquals("00000F", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 15 })));
589     }
590 
591     private String toString(final byte[] data) {
592         final StringBuilder buf = new StringBuilder();
593         for (int i = 0; i < data.length; i++) {
594             buf.append(data[i]);
595             if (i != data.length - 1) {
596                 buf.append(",");
597             }
598         }
599         return buf.toString();
600     }
601 }