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    *      http://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   * Test cases for Base16 class.
39   */
40  public 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 Returns the random.
48       */
49      public Random getRandom() {
50          return this.random;
51      }
52  
53      /**
54       * Test the Base16 implementation
55       */
56      @Test
57      public 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      public void testBase16AtBufferEnd() {
70          testBase16InBuffer(100, 0);
71      }
72  
73      @Test
74      public void testBase16AtBufferMiddle() {
75          testBase16InBuffer(100, 100);
76      }
77  
78      @Test
79      public 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      public 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     public 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     public 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     public 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     public 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     public 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     public 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     public void testDecodeSingleBytesOptimisation() {
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     public 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     public void testEncodeDecodeRandom() {
226         for (int i = 1; i < 5; i++) {
227             final int len = this.getRandom().nextInt(10000) + 1;
228             final byte[] data = new byte[len];
229             this.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     public void testEncodeDecodeSmall() {
239         for (int i = 0; i < 12; i++) {
240             final byte[] data = new byte[i];
241             this.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     public void testIsInAlphabet() {
250         // invalid bounds
251         Base16 b16 = new Base16(true);
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 
260         // lower-case
261         b16 = new Base16(true);
262         for (char c = '0'; c <= '9'; c++) {
263             assertTrue(b16.isInAlphabet((byte) c));
264         }
265         for (char c = 'a'; c <= 'f'; c++) {
266             assertTrue(b16.isInAlphabet((byte) c));
267         }
268         for (char c = 'A'; c <= 'F'; c++) {
269             assertFalse(b16.isInAlphabet((byte) c));
270         }
271         assertFalse(b16.isInAlphabet((byte) ('0' - 1)));
272         assertFalse(b16.isInAlphabet((byte) ('9' + 1)));
273         assertFalse(b16.isInAlphabet((byte) ('a' - 1)));
274         assertFalse(b16.isInAlphabet((byte) ('f' + 1)));
275         assertFalse(b16.isInAlphabet((byte) ('z' + 1)));
276 
277         // upper-case
278         b16 = new Base16(false);
279         for (char c = '0'; c <= '9'; c++) {
280             assertTrue(b16.isInAlphabet((byte) c));
281         }
282         for (char c = 'a'; c <= 'f'; c++) {
283             assertFalse(b16.isInAlphabet((byte) c));
284         }
285         for (char c = 'A'; c <= 'F'; c++) {
286             assertTrue(b16.isInAlphabet((byte) c));
287         }
288         assertFalse(b16.isInAlphabet((byte) ('0' - 1)));
289         assertFalse(b16.isInAlphabet((byte) ('9' + 1)));
290         assertFalse(b16.isInAlphabet((byte) ('A' - 1)));
291         assertFalse(b16.isInAlphabet((byte) ('F' + 1)));
292         assertFalse(b16.isInAlphabet((byte) ('Z' + 1)));
293     }
294 
295     @Test
296     public void testKnownDecodings() {
297         assertEquals("The quick brown fox jumped over the lazy dogs.", new String(new Base16(true)
298                 .decode("54686520717569636b2062726f776e20666f78206a756d706564206f76657220746865206c617a7920646f67732e".getBytes(CHARSET_UTF8))));
299         assertEquals("It was the best of times, it was the worst of times.", new String(new Base16(true)
300                 .decode("497420776173207468652062657374206f662074696d65732c206974207761732074686520776f727374206f662074696d65732e".getBytes(CHARSET_UTF8))));
301         assertEquals("http://jakarta.apache.org/commmons",
302                 new String(new Base16(true).decode("687474703a2f2f6a616b617274612e6170616368652e6f72672f636f6d6d6d6f6e73".getBytes(CHARSET_UTF8))));
303         assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", new String(new Base16(true)
304                 .decode("4161426243634464456546664767486849694a6a4b6b4c6c4d6d4e6e4f6f50705171527253735474557556765777587859795a7a".getBytes(CHARSET_UTF8))));
305         assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",
306                 new String(new Base16(true).decode("7b20302c20312c20322c20332c20342c20352c20362c20372c20382c2039207d".getBytes(CHARSET_UTF8))));
307         assertEquals("xyzzy!", new String(new Base16(true).decode("78797a7a7921".getBytes(CHARSET_UTF8))));
308     }
309 
310     @Test
311     public void testKnownEncodings() {
312         assertEquals("54686520717569636b2062726f776e20666f78206a756d706564206f76657220746865206c617a7920646f67732e",
313                 new String(new Base16(true).encode("The quick brown fox jumped over the lazy dogs.".getBytes(CHARSET_UTF8))));
314         assertEquals("497420776173207468652062657374206f662074696d65732c206974207761732074686520776f727374206f662074696d65732e",
315                 new String(new Base16(true).encode("It was the best of times, it was the worst of times.".getBytes(CHARSET_UTF8))));
316         assertEquals("687474703a2f2f6a616b617274612e6170616368652e6f72672f636f6d6d6d6f6e73",
317                 new String(new Base16(true).encode("http://jakarta.apache.org/commmons".getBytes(CHARSET_UTF8))));
318         assertEquals("4161426243634464456546664767486849694a6a4b6b4c6c4d6d4e6e4f6f50705171527253735474557556765777587859795a7a",
319                 new String(new Base16(true).encode("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes(CHARSET_UTF8))));
320         assertEquals("7b20302c20312c20322c20332c20342c20352c20362c20372c20382c2039207d",
321                 new String(new Base16(true).encode("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }".getBytes(CHARSET_UTF8))));
322         assertEquals("78797a7a7921", new String(new Base16(true).encode("xyzzy!".getBytes(CHARSET_UTF8))));
323     }
324 
325     @Test
326     public void testLenientDecoding() {
327         final String encoded = "aabbccdde"; // Note the trailing `e` which does not make up a hex-pair and so is only 1/2 byte
328 
329         final Base16 b16 = new Base16(true, CodecPolicy.LENIENT);
330         assertEquals(CodecPolicy.LENIENT, b16.getCodecPolicy());
331 
332         final byte[] decoded = b16.decode(StringUtils.getBytesUtf8(encoded));
333         assertArrayEquals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, decoded);
334     }
335 
336     @Test
337     public void testNonBase16Test() {
338         final byte[] invalidEncodedChars = { '/', ':', '@', 'G', '%', '`', 'g' };
339 
340         final byte[] encoded = new byte[1];
341         for (final byte invalidEncodedChar : invalidEncodedChars) {
342             encoded[0] = invalidEncodedChar;
343             assertThrows(IllegalArgumentException.class, () -> new Base16().decode(encoded), "Invalid Base16 char: " + (char) invalidEncodedChar);
344         }
345     }
346 
347     @Test
348     public void testObjectDecodeWithInvalidParameter() {
349         assertThrows(DecoderException.class, () -> new Base16().decode(Integer.valueOf(5)));
350     }
351 
352     @Test
353     public void testObjectDecodeWithValidParameter() throws Exception {
354         final String original = "Hello World!";
355         final Object o = new Base16().encode(original.getBytes(CHARSET_UTF8));
356 
357         final Base16 b16 = new Base16();
358         final Object oDecoded = b16.decode(o);
359         final byte[] baDecoded = (byte[]) oDecoded;
360         final String dest = new String(baDecoded);
361 
362         assertEquals(original, dest, "dest string does not equal original");
363     }
364 
365     @Test
366     public void testObjectEncode() {
367         final Base16 b16 = new Base16();
368         assertEquals(new String(b16.encode("Hello World".getBytes(CHARSET_UTF8))), "48656C6C6F20576F726C64");
369     }
370 
371     @Test
372     public void testObjectEncodeWithInvalidParameter() {
373         assertThrows(EncoderException.class, () -> new Base16().encode("Yadayadayada"));
374     }
375 
376     @Test
377     public void testObjectEncodeWithValidParameter() throws Exception {
378         final String original = "Hello World!";
379         final Object origObj = original.getBytes(CHARSET_UTF8);
380 
381         final Object oEncoded = new Base16().encode(origObj);
382         final byte[] bArray = new Base16().decode((byte[]) oEncoded);
383         final String dest = new String(bArray);
384 
385         assertEquals(original, dest, "dest string does not equal original");
386     }
387 
388     @Test
389     public void testOddEvenDecoding() {
390         final String encoded = "4142434445";
391 
392         final BaseNCodec.Context context = new BaseNCodec.Context();
393         final Base16 base16 = new Base16();
394 
395         final byte[] encodedBytes = StringUtils.getBytesUtf8(encoded);
396 
397         // pass odd, then even, then odd amount of data
398         base16.decode(encodedBytes, 0, 3, context);
399         base16.decode(encodedBytes, 3, 4, context);
400         base16.decode(encodedBytes, 7, 3, context);
401 
402         final byte[] decodedBytes = new byte[context.pos];
403         System.arraycopy(context.buffer, context.readPos, decodedBytes, 0, decodedBytes.length);
404         final String decoded = StringUtils.newStringUtf8(decodedBytes);
405 
406         assertEquals("ABCDE", decoded);
407     }
408 
409     @Test
410     public void testPairs() {
411         assertEquals("0000", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0 })));
412         assertEquals("0001", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 1 })));
413         assertEquals("0002", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 2 })));
414         assertEquals("0003", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 3 })));
415         assertEquals("0004", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 4 })));
416         assertEquals("0005", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 5 })));
417         assertEquals("0006", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 6 })));
418         assertEquals("0007", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 7 })));
419         assertEquals("0008", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 8 })));
420         assertEquals("0009", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 9 })));
421         assertEquals("000A", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 10 })));
422         assertEquals("000B", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 11 })));
423         assertEquals("000C", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 12 })));
424         assertEquals("000D", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 13 })));
425         assertEquals("000E", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 14 })));
426         assertEquals("000F", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 15 })));
427         assertEquals("0010", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 16 })));
428         assertEquals("0011", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 17 })));
429         for (int i = -128; i <= 127; i++) {
430             final byte[] test = { (byte) i, (byte) i };
431             assertArrayEquals(test, new Base16().decode(new Base16().encode(test)));
432         }
433     }
434 
435     @Test
436     public void testSingletons() {
437         assertEquals("00", new String(new Base16().encode(new byte[] { (byte) 0 })));
438         assertEquals("01", new String(new Base16().encode(new byte[] { (byte) 1 })));
439         assertEquals("02", new String(new Base16().encode(new byte[] { (byte) 2 })));
440         assertEquals("03", new String(new Base16().encode(new byte[] { (byte) 3 })));
441         assertEquals("04", new String(new Base16().encode(new byte[] { (byte) 4 })));
442         assertEquals("05", new String(new Base16().encode(new byte[] { (byte) 5 })));
443         assertEquals("06", new String(new Base16().encode(new byte[] { (byte) 6 })));
444         assertEquals("07", new String(new Base16().encode(new byte[] { (byte) 7 })));
445         assertEquals("08", new String(new Base16().encode(new byte[] { (byte) 8 })));
446         assertEquals("09", new String(new Base16().encode(new byte[] { (byte) 9 })));
447         assertEquals("0A", new String(new Base16().encode(new byte[] { (byte) 10 })));
448         assertEquals("0B", new String(new Base16().encode(new byte[] { (byte) 11 })));
449         assertEquals("0C", new String(new Base16().encode(new byte[] { (byte) 12 })));
450         assertEquals("0D", new String(new Base16().encode(new byte[] { (byte) 13 })));
451         assertEquals("0E", new String(new Base16().encode(new byte[] { (byte) 14 })));
452         assertEquals("0F", new String(new Base16().encode(new byte[] { (byte) 15 })));
453         assertEquals("10", new String(new Base16().encode(new byte[] { (byte) 16 })));
454         assertEquals("11", new String(new Base16().encode(new byte[] { (byte) 17 })));
455         assertEquals("12", new String(new Base16().encode(new byte[] { (byte) 18 })));
456         assertEquals("13", new String(new Base16().encode(new byte[] { (byte) 19 })));
457         assertEquals("14", new String(new Base16().encode(new byte[] { (byte) 20 })));
458         assertEquals("15", new String(new Base16().encode(new byte[] { (byte) 21 })));
459         assertEquals("16", new String(new Base16().encode(new byte[] { (byte) 22 })));
460         assertEquals("17", new String(new Base16().encode(new byte[] { (byte) 23 })));
461         assertEquals("18", new String(new Base16().encode(new byte[] { (byte) 24 })));
462         assertEquals("19", new String(new Base16().encode(new byte[] { (byte) 25 })));
463         assertEquals("1A", new String(new Base16().encode(new byte[] { (byte) 26 })));
464         assertEquals("1B", new String(new Base16().encode(new byte[] { (byte) 27 })));
465         assertEquals("1C", new String(new Base16().encode(new byte[] { (byte) 28 })));
466         assertEquals("1D", new String(new Base16().encode(new byte[] { (byte) 29 })));
467         assertEquals("1E", new String(new Base16().encode(new byte[] { (byte) 30 })));
468         assertEquals("1F", new String(new Base16().encode(new byte[] { (byte) 31 })));
469         assertEquals("20", new String(new Base16().encode(new byte[] { (byte) 32 })));
470         assertEquals("21", new String(new Base16().encode(new byte[] { (byte) 33 })));
471         assertEquals("22", new String(new Base16().encode(new byte[] { (byte) 34 })));
472         assertEquals("23", new String(new Base16().encode(new byte[] { (byte) 35 })));
473         assertEquals("24", new String(new Base16().encode(new byte[] { (byte) 36 })));
474         assertEquals("25", new String(new Base16().encode(new byte[] { (byte) 37 })));
475         assertEquals("26", new String(new Base16().encode(new byte[] { (byte) 38 })));
476         assertEquals("27", new String(new Base16().encode(new byte[] { (byte) 39 })));
477         assertEquals("28", new String(new Base16().encode(new byte[] { (byte) 40 })));
478         assertEquals("29", new String(new Base16().encode(new byte[] { (byte) 41 })));
479         assertEquals("2A", new String(new Base16().encode(new byte[] { (byte) 42 })));
480         assertEquals("2B", new String(new Base16().encode(new byte[] { (byte) 43 })));
481         assertEquals("2C", new String(new Base16().encode(new byte[] { (byte) 44 })));
482         assertEquals("2D", new String(new Base16().encode(new byte[] { (byte) 45 })));
483         assertEquals("2E", new String(new Base16().encode(new byte[] { (byte) 46 })));
484         assertEquals("2F", new String(new Base16().encode(new byte[] { (byte) 47 })));
485         assertEquals("30", new String(new Base16().encode(new byte[] { (byte) 48 })));
486         assertEquals("31", new String(new Base16().encode(new byte[] { (byte) 49 })));
487         assertEquals("32", new String(new Base16().encode(new byte[] { (byte) 50 })));
488         assertEquals("33", new String(new Base16().encode(new byte[] { (byte) 51 })));
489         assertEquals("34", new String(new Base16().encode(new byte[] { (byte) 52 })));
490         assertEquals("35", new String(new Base16().encode(new byte[] { (byte) 53 })));
491         assertEquals("36", new String(new Base16().encode(new byte[] { (byte) 54 })));
492         assertEquals("37", new String(new Base16().encode(new byte[] { (byte) 55 })));
493         assertEquals("38", new String(new Base16().encode(new byte[] { (byte) 56 })));
494         assertEquals("39", new String(new Base16().encode(new byte[] { (byte) 57 })));
495         assertEquals("3A", new String(new Base16().encode(new byte[] { (byte) 58 })));
496         assertEquals("3B", new String(new Base16().encode(new byte[] { (byte) 59 })));
497         assertEquals("3C", new String(new Base16().encode(new byte[] { (byte) 60 })));
498         assertEquals("3D", new String(new Base16().encode(new byte[] { (byte) 61 })));
499         assertEquals("3E", new String(new Base16().encode(new byte[] { (byte) 62 })));
500         assertEquals("3F", new String(new Base16().encode(new byte[] { (byte) 63 })));
501         assertEquals("40", new String(new Base16().encode(new byte[] { (byte) 64 })));
502         assertEquals("41", new String(new Base16().encode(new byte[] { (byte) 65 })));
503         assertEquals("42", new String(new Base16().encode(new byte[] { (byte) 66 })));
504         assertEquals("43", new String(new Base16().encode(new byte[] { (byte) 67 })));
505         assertEquals("44", new String(new Base16().encode(new byte[] { (byte) 68 })));
506         assertEquals("45", new String(new Base16().encode(new byte[] { (byte) 69 })));
507         assertEquals("46", new String(new Base16().encode(new byte[] { (byte) 70 })));
508         assertEquals("47", new String(new Base16().encode(new byte[] { (byte) 71 })));
509         assertEquals("48", new String(new Base16().encode(new byte[] { (byte) 72 })));
510         assertEquals("49", new String(new Base16().encode(new byte[] { (byte) 73 })));
511         assertEquals("4A", new String(new Base16().encode(new byte[] { (byte) 74 })));
512         assertEquals("4B", new String(new Base16().encode(new byte[] { (byte) 75 })));
513         assertEquals("4C", new String(new Base16().encode(new byte[] { (byte) 76 })));
514         assertEquals("4D", new String(new Base16().encode(new byte[] { (byte) 77 })));
515         assertEquals("4E", new String(new Base16().encode(new byte[] { (byte) 78 })));
516         assertEquals("4F", new String(new Base16().encode(new byte[] { (byte) 79 })));
517         assertEquals("50", new String(new Base16().encode(new byte[] { (byte) 80 })));
518         assertEquals("51", new String(new Base16().encode(new byte[] { (byte) 81 })));
519         assertEquals("52", new String(new Base16().encode(new byte[] { (byte) 82 })));
520         assertEquals("53", new String(new Base16().encode(new byte[] { (byte) 83 })));
521         assertEquals("54", new String(new Base16().encode(new byte[] { (byte) 84 })));
522         assertEquals("55", new String(new Base16().encode(new byte[] { (byte) 85 })));
523         assertEquals("56", new String(new Base16().encode(new byte[] { (byte) 86 })));
524         assertEquals("57", new String(new Base16().encode(new byte[] { (byte) 87 })));
525         assertEquals("58", new String(new Base16().encode(new byte[] { (byte) 88 })));
526         assertEquals("59", new String(new Base16().encode(new byte[] { (byte) 89 })));
527         assertEquals("5A", new String(new Base16().encode(new byte[] { (byte) 90 })));
528         assertEquals("5B", new String(new Base16().encode(new byte[] { (byte) 91 })));
529         assertEquals("5C", new String(new Base16().encode(new byte[] { (byte) 92 })));
530         assertEquals("5D", new String(new Base16().encode(new byte[] { (byte) 93 })));
531         assertEquals("5E", new String(new Base16().encode(new byte[] { (byte) 94 })));
532         assertEquals("5F", new String(new Base16().encode(new byte[] { (byte) 95 })));
533         assertEquals("60", new String(new Base16().encode(new byte[] { (byte) 96 })));
534         assertEquals("61", new String(new Base16().encode(new byte[] { (byte) 97 })));
535         assertEquals("62", new String(new Base16().encode(new byte[] { (byte) 98 })));
536         assertEquals("63", new String(new Base16().encode(new byte[] { (byte) 99 })));
537         assertEquals("64", new String(new Base16().encode(new byte[] { (byte) 100 })));
538         assertEquals("65", new String(new Base16().encode(new byte[] { (byte) 101 })));
539         assertEquals("66", new String(new Base16().encode(new byte[] { (byte) 102 })));
540         assertEquals("67", new String(new Base16().encode(new byte[] { (byte) 103 })));
541         assertEquals("68", new String(new Base16().encode(new byte[] { (byte) 104 })));
542         for (int i = -128; i <= 127; i++) {
543             final byte[] test = { (byte) i };
544             assertArrayEquals(test, new Base16().decode(new Base16().encode(test)));
545         }
546     }
547 
548     @Test
549     public void testStrictDecoding() {
550         final String encoded = "aabbccdde"; // Note the trailing `e` which does not make up a hex-pair and so is only 1/2 byte
551 
552         final Base16 b16 = new Base16(true, CodecPolicy.STRICT);
553         assertEquals(CodecPolicy.STRICT, b16.getCodecPolicy());
554         assertThrows(IllegalArgumentException.class, () -> b16.decode(StringUtils.getBytesUtf8(encoded)));
555     }
556 
557     @Test
558     public void testStringToByteVariations() throws DecoderException {
559         final Base16 base16 = new Base16();
560         final String s1 = "48656C6C6F20576F726C64";
561         final String s2 = "";
562         final String s3 = null;
563 
564         assertEquals("Hello World", StringUtils.newStringUtf8(base16.decode(s1)), "StringToByte Hello World");
565         assertEquals("Hello World", StringUtils.newStringUtf8((byte[]) new Base16().decode((Object) s1)), "StringToByte Hello World");
566         assertEquals("Hello World", StringUtils.newStringUtf8(new Base16().decode(s1)), "StringToByte static Hello World");
567         assertEquals("", StringUtils.newStringUtf8(new Base16().decode(s2)), "StringToByte \"\"");
568         assertEquals("", StringUtils.newStringUtf8(new Base16().decode(s2)), "StringToByte static \"\"");
569         assertNull(StringUtils.newStringUtf8(new Base16().decode(s3)), "StringToByte null");
570         assertNull(StringUtils.newStringUtf8(new Base16().decode(s3)), "StringToByte static null");
571     }
572 
573     @Test
574     public void testTriplets() {
575         assertEquals("000000", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 0 })));
576         assertEquals("000001", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 1 })));
577         assertEquals("000002", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 2 })));
578         assertEquals("000003", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 3 })));
579         assertEquals("000004", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 4 })));
580         assertEquals("000005", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 5 })));
581         assertEquals("000006", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 6 })));
582         assertEquals("000007", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 7 })));
583         assertEquals("000008", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 8 })));
584         assertEquals("000009", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 9 })));
585         assertEquals("00000A", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 10 })));
586         assertEquals("00000B", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 11 })));
587         assertEquals("00000C", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 12 })));
588         assertEquals("00000D", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 13 })));
589         assertEquals("00000E", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 14 })));
590         assertEquals("00000F", new String(new Base16().encode(new byte[] { (byte) 0, (byte) 0, (byte) 15 })));
591     }
592 
593     private String toString(final byte[] data) {
594         final StringBuilder buf = new StringBuilder();
595         for (int i = 0; i < data.length; i++) {
596             buf.append(data[i]);
597             if (i != data.length - 1) {
598                 buf.append(",");
599             }
600         }
601         return buf.toString();
602     }
603 }