1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.trace;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import java.nio.charset.Charset;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 /** Unit tests for {@link io.opencensus.trace.LowerCaseBase16Encoding}. */
29 @RunWith(JUnit4.class)
30 public class LowerCaseBase16EncodingTest {
31   private static final Charset CHARSET = Charset.forName("UTF-8");
32 
33   @Rule public ExpectedException thrown = ExpectedException.none();
34 
35   @Test
valid_EncodeDecode()36   public void valid_EncodeDecode() {
37     testEncoding("", "");
38     testEncoding("f", "66");
39     testEncoding("fo", "666f");
40     testEncoding("foo", "666f6f");
41     testEncoding("foob", "666f6f62");
42     testEncoding("fooba", "666f6f6261");
43     testEncoding("foobar", "666f6f626172");
44   }
45 
46   @Test
invalidDecodings_UnrecongnizedCharacters()47   public void invalidDecodings_UnrecongnizedCharacters() {
48     // These contain bytes not in the decoding.
49     thrown.expect(IllegalArgumentException.class);
50     thrown.expectMessage("Invalid character g");
51     LowerCaseBase16Encoding.decodeToBytes("efhg");
52   }
53 
54   @Test
invalidDecodings_InvalidInputLength()55   public void invalidDecodings_InvalidInputLength() {
56     // Valid base16 strings always have an even length.
57     thrown.expect(IllegalArgumentException.class);
58     thrown.expectMessage("Invalid input length 3");
59     LowerCaseBase16Encoding.decodeToBytes("abc");
60   }
61 
62   @Test
invalidDecodings_InvalidInputLengthAndCharacter()63   public void invalidDecodings_InvalidInputLengthAndCharacter() {
64     // These have a combination of invalid length and unrecognized characters.
65     thrown.expect(IllegalArgumentException.class);
66     thrown.expectMessage("Invalid input length 1");
67     LowerCaseBase16Encoding.decodeToBytes("?");
68   }
69 
testEncoding(String decoded, String encoded)70   private static void testEncoding(String decoded, String encoded) {
71     testEncodes(decoded, encoded);
72     testDecodes(encoded, decoded);
73   }
74 
testEncodes(String decoded, String encoded)75   private static void testEncodes(String decoded, String encoded) {
76     assertThat(LowerCaseBase16Encoding.encodeToString(decoded.getBytes(CHARSET)))
77         .isEqualTo(encoded);
78   }
79 
testDecodes(String encoded, String decoded)80   private static void testDecodes(String encoded, String decoded) {
81     assertThat(LowerCaseBase16Encoding.decodeToBytes(encoded)).isEqualTo(decoded.getBytes(CHARSET));
82   }
83 }
84