1 // Copyright (C) 2010-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8
9 #include <stdint.h>
10
11 #include <cctype>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include <exceptions/exceptions.h>
17
18 #include <util/encode/base32hex.h>
19
20 #include <gtest/gtest.h>
21
22 using namespace std;
23 using namespace isc;
24 using namespace isc::util::encode;
25
26 namespace {
27
28 typedef pair<string, string> StringPair;
29
30 class Base32HexTest : public ::testing::Test {
31 protected:
Base32HexTest()32 Base32HexTest() : encoding_chars("0123456789ABCDEFGHIJKLMNOPQRSTUV=") {
33 // test vectors from RFC4648
34 test_sequence.push_back(StringPair("", ""));
35 test_sequence.push_back(StringPair("f", "CO======"));
36 test_sequence.push_back(StringPair("fo", "CPNG===="));
37 test_sequence.push_back(StringPair("foo", "CPNMU==="));
38 test_sequence.push_back(StringPair("foob", "CPNMUOG="));
39 test_sequence.push_back(StringPair("fooba", "CPNMUOJ1"));
40 test_sequence.push_back(StringPair("foobar", "CPNMUOJ1E8======"));
41
42 // the same data, encoded using lower case chars (testable only
43 // for the decode side)
44 test_sequence_lower.push_back(StringPair("f", "co======"));
45 test_sequence_lower.push_back(StringPair("fo", "cpng===="));
46 test_sequence_lower.push_back(StringPair("foo", "cpnmu==="));
47 test_sequence_lower.push_back(StringPair("foob", "cpnmuog="));
48 test_sequence_lower.push_back(StringPair("fooba", "cpnmuoj1"));
49 test_sequence_lower.push_back(StringPair("foobar",
50 "cpnmuoj1e8======"));
51 }
52 vector<StringPair> test_sequence;
53 vector<StringPair> test_sequence_lower;
54 vector<uint8_t> decoded_data;
55 const string encoding_chars;
56 };
57
58 void
decodeCheck(const string & input_string,vector<uint8_t> & output,const string & expected)59 decodeCheck(const string& input_string, vector<uint8_t>& output,
60 const string& expected)
61 {
62 decodeBase32Hex(input_string, output);
63 EXPECT_EQ(expected, string(output.begin(), output.end()));
64 }
65
TEST_F(Base32HexTest,decode)66 TEST_F(Base32HexTest, decode) {
67 for (vector<StringPair>::const_iterator it = test_sequence.begin();
68 it != test_sequence.end();
69 ++it) {
70 decodeCheck((*it).second, decoded_data, (*it).first);
71 }
72
73 // whitespace should be allowed
74 decodeCheck("CP NM\tUOG=", decoded_data, "foob");
75 decodeCheck("CPNMU===\n", decoded_data, "foo");
76 decodeCheck(" CP NM\tUOG=", decoded_data, "foob");
77 decodeCheck(" ", decoded_data, "");
78
79 // Incomplete input
80 EXPECT_THROW(decodeBase32Hex("CPNMUOJ", decoded_data), BadValue);
81
82 // invalid number of padding characters
83 EXPECT_THROW(decodeBase32Hex("CPNMU0==", decoded_data), BadValue);
84 EXPECT_THROW(decodeBase32Hex("CO0=====", decoded_data), BadValue);
85 EXPECT_THROW(decodeBase32Hex("CO=======", decoded_data), // too many ='s
86 BadValue);
87
88 // intermediate padding isn't allowed
89 EXPECT_THROW(decodeBase32Hex("CPNMUOG=CPNMUOG=", decoded_data), BadValue);
90
91 // Non canonical form isn't allowed.
92 // P => 25(11001), so the padding byte would be 01000000
93 EXPECT_THROW(decodeBase32Hex("0P======", decoded_data), BadValue);
94 }
95
TEST_F(Base32HexTest,decodeLower)96 TEST_F(Base32HexTest, decodeLower) {
97 for (vector<StringPair>::const_iterator it = test_sequence_lower.begin();
98 it != test_sequence_lower.end();
99 ++it) {
100 decodeCheck((*it).second, decoded_data, (*it).first);
101 }
102 }
103
TEST_F(Base32HexTest,encode)104 TEST_F(Base32HexTest, encode) {
105 for (vector<StringPair>::const_iterator it = test_sequence.begin();
106 it != test_sequence.end();
107 ++it) {
108 decoded_data.assign((*it).first.begin(), (*it).first.end());
109 EXPECT_EQ((*it).second, encodeBase32Hex(decoded_data));
110 }
111 }
112
113 // For Base32Hex we use handmade mappings, so it's prudent to test the
114 // entire mapping table explicitly.
TEST_F(Base32HexTest,decodeMap)115 TEST_F(Base32HexTest, decodeMap) {
116 string input(8, '0'); // input placeholder
117
118 // We're going to populate an input string with only the last character
119 // not equal to the zero character ('0') for each valid base32hex encoding
120 // character. Decoding that input should result in a data stream with
121 // the last byte equal to the numeric value represented by the that
122 // character. For example, we'll generate and confirm the following:
123 // "00000000" => should be 0 (as a 40bit integer)
124 // "00000001" => should be 1 (as a 40bit integer)
125 // ...
126 // "0000000V" => should be 31 (as a 40bit integer)
127 // We also check the use of an invalid character for the last character
128 // surely fails. '=' should be accepted as a valid padding in this
129 // context; space characters shouldn't be allowed in this context.
130
131 for (int i = 0; i < 256; ++i) {
132 input[7] = i;
133
134 const char ch = toupper(i);
135 const size_t pos = encoding_chars.find(ch);
136 if (pos == string::npos) {
137 EXPECT_THROW(decodeBase32Hex(input, decoded_data), BadValue);
138 } else {
139 decodeBase32Hex(input, decoded_data);
140 if (ch == '=') {
141 EXPECT_EQ(4, decoded_data.size());
142 } else {
143 EXPECT_EQ(5, decoded_data.size());
144 EXPECT_EQ(pos, decoded_data[4]);
145 }
146 }
147 }
148 }
149
TEST_F(Base32HexTest,encodeMap)150 TEST_F(Base32HexTest, encodeMap) {
151 for (uint8_t i = 0; i < 32; ++i) {
152 decoded_data.assign(4, 0);
153 decoded_data.push_back(i);
154 EXPECT_EQ(encoding_chars[i], encodeBase32Hex(decoded_data)[7]);
155 }
156 }
157
158 }
159