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.
5 #include "base/base64.h"
7 #include "base/numerics/checked_math.h"
8 #include "base/strings/escape.h"
9 #include "base/test/gtest_util.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/modp_b64/modp_b64.h"
16 TEST(Base64Test, Basic) {
17 const std::string kText = "hello world";
18 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
23 std::string encoded = Base64Encode(kText);
24 EXPECT_EQ(kBase64Text, encoded);
26 ok = Base64Decode(encoded, &decoded);
28 EXPECT_EQ(kText, decoded);
31 TEST(Base64Test, Forgiving) {
35 // nullptr indicates a decode failure.
36 const char* expected_out;
38 // Failures that should apply in all decoding modes:
40 // - Characters not in the base64 alphabet
43 // - input len % 4 == 1
47 // Invalid padding causes failure if kForgiving is set.
51 {"abcd====", nullptr},
52 {"abcd==============", nullptr},
56 // Otherwise, inputs that are multiples of 4 always succeed, this matches
58 {"abcd", "i\xB7\x1D"},
60 {"abcdefgh", "i\xB7\x1Dy\xF8!"},
62 // kForgiving mode allows for omitting padding (to a multiple of 4) if
64 {"abcdef", "i\xB7\x1Dy"},
68 // Whitespace should be allowed if kForgiving is set, matching
69 // https://infra.spec.whatwg.org/#ascii-whitespace:
70 // ASCII whitespace is U+0009 TAB '\t', U+000A LF '\n', U+000C FF '\f',
71 // U+000D CR '\r', or U+0020 SPACE ' '.
72 {" a bcd", "i\xB7\x1D"},
73 {"ab\t\tc=", "i\xB7"},
74 {"ab c\ndefgh", "i\xB7\x1Dy\xF8!"},
75 {"a\tb\nc\f d\r", "i\xB7\x1D"},
77 // U+000B VT '\v' is _not_ valid whitespace to be stripped.
80 // Empty string should yield an empty result.
83 for (const auto& test_case : kTestCases) {
84 SCOPED_TRACE(::testing::Message()
85 << EscapeAllExceptUnreserved(test_case.in));
88 Base64Decode(test_case.in, &output, Base64DecodePolicy::kForgiving);
89 bool expected_success = test_case.expected_out != nullptr;
90 EXPECT_EQ(success, expected_success);
91 if (expected_success) {
92 EXPECT_EQ(output, test_case.expected_out);
97 TEST(Base64Test, Binary) {
98 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
100 std::string binary_encoded = Base64Encode(kData);
102 // Check that encoding the same data through the StringPiece interface gives
104 std::string string_piece_encoded;
105 Base64Encode(StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)),
106 &string_piece_encoded);
108 EXPECT_EQ(binary_encoded, string_piece_encoded);
110 EXPECT_THAT(Base64Decode(binary_encoded),
111 testing::Optional(testing::ElementsAreArray(kData)));
112 EXPECT_FALSE(Base64Decode("invalid base64!"));
114 std::string encoded_with_prefix = "PREFIX";
115 Base64EncodeAppend(kData, &encoded_with_prefix);
116 EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded);
119 TEST(Base64Test, InPlace) {
120 const std::string kText = "hello world";
121 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
123 std::string text = Base64Encode(kText);
124 EXPECT_EQ(kBase64Text, text);
126 bool ok = Base64Decode(text, &text);
128 EXPECT_EQ(text, kText);
131 TEST(Base64Test, Overflow) {
132 // `Base64Encode` makes the input larger, which means inputs whose base64
133 // output overflows `size_t`. Actually allocating a span of this size will
134 // likely fail, but we test it with a fake span and assume a correct
135 // implementation will check for overflow before touching the input.
137 // Note that, with or without an overflow check, the function will still
138 // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for
139 // a `CHECK`-based failure.
141 auto large_span = base::make_span(&b, MODP_B64_MAX_INPUT_LEN + 1);
142 EXPECT_CHECK_DEATH(Base64Encode(large_span));
144 std::string output = "PREFIX";
145 EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output));
147 // `modp_b64_encode_data_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is
148 // correct be verifying the computation doesn't overflow.
149 base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN;
150 EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid());