1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/base64.h"
11
12 #include <string_view>
13
14 #include "base/numerics/checked_math.h"
15 #include "base/strings/escape.h"
16 #include "base/test/gtest_util.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/modp_b64/modp_b64.h"
20
21 namespace base {
22
TEST(Base64Test,Basic)23 TEST(Base64Test, Basic) {
24 const std::string kText = "hello world";
25 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
26
27 std::string decoded;
28 bool ok;
29
30 std::string encoded = Base64Encode(kText);
31 EXPECT_EQ(kBase64Text, encoded);
32
33 ok = Base64Decode(encoded, &decoded);
34 EXPECT_TRUE(ok);
35 EXPECT_EQ(kText, decoded);
36 }
37
TEST(Base64Test,ForgivingAndStrictDecode)38 TEST(Base64Test, ForgivingAndStrictDecode) {
39 struct {
40 const char* in;
41
42 // nullptr indicates a decode failure.
43 const char* expected_out_forgiving;
44 const char* expected_out_strict;
45 } kTestCases[] = {
46 // Failures that should apply in all decoding modes:
47 //
48 // - Characters not in the base64 alphabet
49 {"abc&", nullptr, nullptr},
50 {"ab-d", nullptr, nullptr},
51 // - input len % 4 == 1
52 {"abcde", nullptr, nullptr},
53 {"a", nullptr, nullptr},
54
55 // Invalid padding causes failure if kForgiving is set.
56 {"abcd=", nullptr, nullptr},
57 {"abcd==", nullptr, nullptr},
58 {"abcd===", nullptr, nullptr},
59 {"abcd====", nullptr, nullptr},
60 {"abcd==============", nullptr, nullptr},
61 {"abcde===", nullptr, nullptr},
62 {"=", nullptr, nullptr},
63 {"====", nullptr, nullptr},
64
65 // Otherwise, inputs that are multiples of 4 always succeed, this matches
66 // kStrict mode.
67 {"abcd", "i\xB7\x1D", "i\xB7\x1D"},
68 {"abc=", "i\xB7", "i\xB7"},
69 {"abcdefgh", "i\xB7\x1Dy\xF8!", "i\xB7\x1Dy\xF8!"},
70
71 // kForgiving mode allows for omitting padding (to a multiple of 4) if
72 // len % 4 != 1.
73 {"abcdef", "i\xB7\x1Dy", nullptr},
74 {"abc", "i\xB7", nullptr},
75 {"ab", "i", nullptr},
76
77 // Whitespace should be allowed if kForgiving is set, matching
78 // https://infra.spec.whatwg.org/#ascii-whitespace:
79 // ASCII whitespace is U+0009 TAB '\t', U+000A LF '\n', U+000C FF '\f',
80 // U+000D CR '\r', or U+0020 SPACE ' '.
81 {" a bcd", "i\xB7\x1D", nullptr},
82 {"ab\t\tc=", "i\xB7", nullptr},
83 {"ab c\ndefgh", "i\xB7\x1Dy\xF8!", nullptr},
84 {"a\tb\nc\f d\r", "i\xB7\x1D", nullptr},
85 {"this should fail", "\xB6\x18\xAC\xB2\x1A.\x95\xD7\xDA\x8A", nullptr},
86
87 // U+000B VT '\v' is _not_ valid whitespace to be stripped.
88 {"ab\vcd", nullptr, nullptr},
89
90 // Empty string should yield an empty result.
91 {"", "", ""},
92 };
93 for (const auto& test_case : kTestCases) {
94 SCOPED_TRACE(::testing::Message()
95 << "Forgiving: " << EscapeAllExceptUnreserved(test_case.in));
96 std::string output;
97 bool success =
98 Base64Decode(test_case.in, &output, Base64DecodePolicy::kForgiving);
99 bool expected_success = test_case.expected_out_forgiving != nullptr;
100 EXPECT_EQ(success, expected_success);
101 if (expected_success) {
102 EXPECT_EQ(output, test_case.expected_out_forgiving);
103 }
104 }
105 for (const auto& test_case : kTestCases) {
106 SCOPED_TRACE(::testing::Message()
107 << "Strict: " << EscapeAllExceptUnreserved(test_case.in));
108 std::string output;
109 bool success =
110 Base64Decode(test_case.in, &output, Base64DecodePolicy::kStrict);
111 bool expected_success = test_case.expected_out_strict != nullptr;
112 EXPECT_EQ(success, expected_success);
113 if (expected_success) {
114 EXPECT_EQ(output, test_case.expected_out_strict);
115 }
116 }
117 }
118
TEST(Base64Test,Binary)119 TEST(Base64Test, Binary) {
120 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
121
122 std::string binary_encoded = Base64Encode(kData);
123
124 // Check that encoding the same data through the std::string_view interface
125 // gives the same results.
126 std::string string_piece_encoded = Base64Encode(
127 std::string_view(reinterpret_cast<const char*>(kData), sizeof(kData)));
128
129 EXPECT_EQ(binary_encoded, string_piece_encoded);
130
131 EXPECT_THAT(Base64Decode(binary_encoded),
132 testing::Optional(testing::ElementsAreArray(kData)));
133 EXPECT_FALSE(Base64Decode("invalid base64!"));
134
135 std::string encoded_with_prefix = "PREFIX";
136 Base64EncodeAppend(kData, &encoded_with_prefix);
137 EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded);
138 }
139
TEST(Base64Test,InPlace)140 TEST(Base64Test, InPlace) {
141 const std::string kText = "hello world";
142 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
143
144 std::string text = Base64Encode(kText);
145 EXPECT_EQ(kBase64Text, text);
146
147 bool ok = Base64Decode(text, &text);
148 EXPECT_TRUE(ok);
149 EXPECT_EQ(text, kText);
150 }
151
TEST(Base64Test,Overflow)152 TEST(Base64Test, Overflow) {
153 // `Base64Encode` makes the input larger, which means inputs whose base64
154 // output overflows `size_t`. Actually allocating a span of this size will
155 // likely fail, but we test it with a fake span and assume a correct
156 // implementation will check for overflow before touching the input.
157 //
158 // Note that, with or without an overflow check, the function will still
159 // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for
160 // a `CHECK`-based failure.
161 uint8_t b;
162 auto large_span = span(&b, MODP_B64_MAX_INPUT_LEN + 1);
163 EXPECT_CHECK_DEATH(Base64Encode(large_span));
164
165 std::string output = "PREFIX";
166 EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output));
167
168 // `modp_b64_encode_data_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is
169 // correct be verifying the computation doesn't overflow.
170 base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN;
171 EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid());
172 }
173
174 } // namespace base
175