1// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <brillo/data_encoding.h>
6
7#include <algorithm>
8#include <numeric>
9
10#include <gtest/gtest.h>
11
12namespace brillo {
13namespace data_encoding {
14
15TEST(data_encoding, UrlEncoding) {
16  std::string test = "\"http://sample/path/0014.html \"";
17  std::string encoded = UrlEncode(test.c_str());
18  EXPECT_EQ("%22http%3A%2F%2Fsample%2Fpath%2F0014.html+%22", encoded);
19  EXPECT_EQ(test, UrlDecode(encoded.c_str()));
20
21  test = "\"http://sample/path/0014.html \"";
22  encoded = UrlEncode(test.c_str(), false);
23  EXPECT_EQ("%22http%3A%2F%2Fsample%2Fpath%2F0014.html%20%22", encoded);
24  EXPECT_EQ(test, UrlDecode(encoded.c_str()));
25}
26
27TEST(data_encoding, WebParamsEncoding) {
28  std::string encoded =
29      WebParamsEncode({{"q", "test"}, {"path", "/usr/bin"}, {"#", "%"}});
30  EXPECT_EQ("q=test&path=%2Fusr%2Fbin&%23=%25", encoded);
31
32  auto params = WebParamsDecode(encoded);
33  EXPECT_EQ(3, params.size());
34  EXPECT_EQ("q", params[0].first);
35  EXPECT_EQ("test", params[0].second);
36  EXPECT_EQ("path", params[1].first);
37  EXPECT_EQ("/usr/bin", params[1].second);
38  EXPECT_EQ("#", params[2].first);
39  EXPECT_EQ("%", params[2].second);
40}
41
42TEST(data_encoding, Base64Encode) {
43  const std::string text1 = "hello world";
44  const std::string encoded1 = "aGVsbG8gd29ybGQ=";
45
46  const std::string text2 =
47      "Lorem ipsum dolor sit amet, facilisis erat nec aliquam, scelerisque "
48      "molestie commodo. Viverra tincidunt integer erat ipsum, integer "
49      "molestie, arcu in, sit mauris ac a sed sit etiam.";
50  const std::string encoded2 =
51      "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGZhY2lsaXNpcyBlcmF0IG5lYyBhbGlxdWF"
52      "tLCBzY2VsZXJpc3F1ZSBtb2xlc3RpZSBjb21tb2RvLiBWaXZlcnJhIHRpbmNpZHVudCBpbn"
53      "RlZ2VyIGVyYXQgaXBzdW0sIGludGVnZXIgbW9sZXN0aWUsIGFyY3UgaW4sIHNpdCBtYXVya"
54      "XMgYWMgYSBzZWQgc2l0IGV0aWFtLg==";
55
56  brillo::Blob data3(256);
57  std::iota(data3.begin(), data3.end(), 0);  // Fills the buffer with 0x00-0xFF.
58  const std::string encoded3 =
59      "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ"
60      "1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaW"
61      "prbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en"
62      "6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU"
63      "1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
64
65  EXPECT_EQ(encoded1, Base64Encode(text1));
66  EXPECT_EQ(encoded2, Base64Encode(text2));
67  EXPECT_EQ(encoded3, Base64Encode(data3));
68}
69
70TEST(data_encoding, Base64EncodeWrapLines) {
71  const std::string text1 = "hello world";
72  const std::string encoded1 = "aGVsbG8gd29ybGQ=\n";
73
74  const std::string text2 =
75      "Lorem ipsum dolor sit amet, facilisis erat nec aliquam, scelerisque "
76      "molestie commodo. Viverra tincidunt integer erat ipsum, integer "
77      "molestie, arcu in, sit mauris ac a sed sit etiam.";
78  const std::string encoded2 =
79      "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGZhY2lsaXNpcyBlcmF0IG5lYyBh\n"
80      "bGlxdWFtLCBzY2VsZXJpc3F1ZSBtb2xlc3RpZSBjb21tb2RvLiBWaXZlcnJhIHRp\n"
81      "bmNpZHVudCBpbnRlZ2VyIGVyYXQgaXBzdW0sIGludGVnZXIgbW9sZXN0aWUsIGFy\n"
82      "Y3UgaW4sIHNpdCBtYXVyaXMgYWMgYSBzZWQgc2l0IGV0aWFtLg==\n";
83
84  brillo::Blob data3(256);
85  std::iota(data3.begin(), data3.end(), 0);  // Fills the buffer with 0x00-0xFF.
86  const std::string encoded3 =
87      "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
88      "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
89      "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
90      "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
91      "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
92      "8PHy8/T19vf4+fr7/P3+/w==\n";
93
94  EXPECT_EQ(encoded1, Base64EncodeWrapLines(text1));
95  EXPECT_EQ(encoded2, Base64EncodeWrapLines(text2));
96  EXPECT_EQ(encoded3, Base64EncodeWrapLines(data3));
97}
98
99TEST(data_encoding, Base64Decode) {
100  const std::string encoded1 = "dGVzdCBzdHJpbmc=";
101
102  const std::string encoded2 =
103      "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGZhY2lsaXNpcyBlcmF0IG5lYyBh\n"
104      "bGlxdWFtLCBzY2VsZXJpc3F1ZSBtb2xlc3RpZSBjb21tb2RvLiBWaXZlcnJhIHRp\r\n"
105      "bmNpZHVudCBpbnRlZ2VyIGVyYXQgaXBzdW0sIGludGVnZXIgbW9sZXN0aWUsIGFy\r"
106      "Y3UgaW4sIHNpdCBtYXVyaXMgYWMgYSBzZWQgc2l0IGV0aWFt\n"
107      "Lg==\n\n\n";
108  const std::string decoded2 =
109      "Lorem ipsum dolor sit amet, facilisis erat nec aliquam, scelerisque "
110      "molestie commodo. Viverra tincidunt integer erat ipsum, integer "
111      "molestie, arcu in, sit mauris ac a sed sit etiam.";
112
113  const std::string encoded3 =
114      "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ"
115      "1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaW"
116      "prbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en"
117      "6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU"
118      "1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
119  brillo::Blob decoded3(256);
120  std::iota(decoded3.begin(), decoded3.end(), 0);  // Fill with 0x00..0xFF.
121
122  std::string decoded;
123  EXPECT_TRUE(Base64Decode(encoded1, &decoded));
124  EXPECT_EQ("test string", decoded);
125
126  EXPECT_TRUE(Base64Decode(encoded2, &decoded));
127  EXPECT_EQ(decoded2, decoded);
128
129  brillo::Blob decoded_blob;
130  EXPECT_TRUE(Base64Decode(encoded3, &decoded_blob));
131  EXPECT_EQ(decoded3, decoded_blob);
132
133  EXPECT_FALSE(Base64Decode("A", &decoded_blob));
134  EXPECT_TRUE(decoded_blob.empty());
135
136  EXPECT_TRUE(Base64Decode("/w==", &decoded_blob));
137  EXPECT_EQ((brillo::Blob{0xFF}), decoded_blob);
138
139  EXPECT_TRUE(Base64Decode("//8=", &decoded_blob));
140  EXPECT_EQ((brillo::Blob{0xFF, 0xFF}), decoded_blob);
141
142  EXPECT_FALSE(Base64Decode("AAECAwQFB,cI", &decoded_blob));
143  EXPECT_TRUE(decoded_blob.empty());
144}
145
146}  // namespace data_encoding
147}  // namespace brillo
148