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 <string>
10 #include <utility>
11 #include <vector>
12 
13 #include <exceptions/exceptions.h>
14 
15 #include <util/encode/base64.h>
16 
17 #include <gtest/gtest.h>
18 
19 using namespace std;
20 using namespace isc;
21 using namespace isc::util::encode;
22 
23 namespace {
24 
25 typedef pair<string, string> StringPair;
26 
27 class Base64Test : public ::testing::Test {
28 protected:
Base64Test()29     Base64Test()
30     {
31         // test vectors from RFC4648
32         test_sequence.push_back(StringPair("", ""));
33         test_sequence.push_back(StringPair("f", "Zg=="));
34         test_sequence.push_back(StringPair("fo", "Zm8="));
35         test_sequence.push_back(StringPair("foo", "Zm9v"));
36         test_sequence.push_back(StringPair("foob", "Zm9vYg=="));
37         test_sequence.push_back(StringPair("fooba", "Zm9vYmE="));
38         test_sequence.push_back(StringPair("foobar", "Zm9vYmFy"));
39     }
40     vector<StringPair> test_sequence;
41     vector<uint8_t> decoded_data;
42 };
43 
44 void
decodeCheck(const string & input_string,vector<uint8_t> & output,const string & expected)45 decodeCheck(const string& input_string, vector<uint8_t>& output,
46             const string& expected)
47 {
48     decodeBase64(input_string, output);
49     EXPECT_EQ(expected, string(output.begin(), output.end()));
50 }
51 
TEST_F(Base64Test,decode)52 TEST_F(Base64Test, decode) {
53     for (vector<StringPair>::const_iterator it = test_sequence.begin();
54          it != test_sequence.end();
55          ++it) {
56         decodeCheck((*it).second, decoded_data, (*it).first);
57     }
58 
59     // whitespace should be allowed
60     decodeCheck("Zm 9v\tYmF\ny", decoded_data, "foobar");
61     decodeCheck("Zm9vYg==", decoded_data, "foob");
62     decodeCheck("Zm9vYmE=\n", decoded_data, "fooba");
63     decodeCheck(" Zm9vYmE=\n", decoded_data, "fooba");
64     decodeCheck(" ", decoded_data, "");
65     decodeCheck("\n\t", decoded_data, "");
66 
67     // incomplete input
68     EXPECT_THROW(decodeBase64("Zm9vYmF", decoded_data), BadValue);
69 
70     // only up to 2 padding characters are allowed
71     EXPECT_THROW(decodeBase64("A===", decoded_data), BadValue);
72     EXPECT_THROW(decodeBase64("A= ==", decoded_data), BadValue);
73 
74     // intermediate padding isn't allowed
75     EXPECT_THROW(decodeBase64("YmE=YmE=", decoded_data), BadValue);
76 
77     // Non canonical form isn't allowed.
78     // Z => 25(011001), m => 38(100110), 9 => 60(111101), so the padding
79     // byte would be 0100 0000.
80     EXPECT_THROW(decodeBase64("Zm9=", decoded_data), BadValue);
81     // Same for the 1st padding byte.  This would make it 01100000.
82     EXPECT_THROW(decodeBase64("Zm==", decoded_data), BadValue);
83 }
84 
TEST_F(Base64Test,encode)85 TEST_F(Base64Test, encode) {
86     for (vector<StringPair>::const_iterator it = test_sequence.begin();
87          it != test_sequence.end();
88          ++it) {
89         decoded_data.assign((*it).first.begin(), (*it).first.end());
90         EXPECT_EQ((*it).second, encodeBase64(decoded_data));
91     }
92 }
93 }
94