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