blob: 1b110c8ee9452b258a18e5a72c77d170132f8cc6 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]978df342009-11-24 06:21:535#include "base/base64.h"
[email protected]24f111a2012-09-23 04:51:046
Helmut Januschka0fc785b2024-04-17 21:13:367#include <string_view>
8
David Benjamin48808e22022-09-21 19:39:129#include "base/numerics/checked_math.h"
Charlie Harrison7b633d92022-11-29 05:23:5010#include "base/strings/escape.h"
David Benjamin48808e22022-09-21 19:39:1211#include "base/test/gtest_util.h"
David Benjamin47bb5ec2022-02-01 23:12:2812#include "testing/gmock/include/gmock/gmock.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "testing/gtest/include/gtest/gtest.h"
David Benjamin48808e22022-09-21 19:39:1214#include "third_party/modp_b64/modp_b64.h"
initial.commit586acc5fe2008-07-26 22:42:5215
[email protected]24f111a2012-09-23 04:51:0416namespace base {
initial.commit586acc5fe2008-07-26 22:42:5217
18TEST(Base64Test, Basic) {
19 const std::string kText = "hello world";
20 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
21
[email protected]24f111a2012-09-23 04:51:0422 std::string decoded;
initial.commit586acc5fe2008-07-26 22:42:5223 bool ok;
24
wd l4a70b3ff2023-09-26 20:13:3525 std::string encoded = Base64Encode(kText);
initial.commit586acc5fe2008-07-26 22:42:5226 EXPECT_EQ(kBase64Text, encoded);
27
[email protected]24f111a2012-09-23 04:51:0428 ok = Base64Decode(encoded, &decoded);
initial.commit586acc5fe2008-07-26 22:42:5229 EXPECT_TRUE(ok);
30 EXPECT_EQ(kText, decoded);
31}
[email protected]24f111a2012-09-23 04:51:0432
Charlie Harrison7b633d92022-11-29 05:23:5033TEST(Base64Test, Forgiving) {
34 struct {
35 const char* in;
36
37 // nullptr indicates a decode failure.
38 const char* expected_out;
39 } kTestCases[] = {
40 // Failures that should apply in all decoding modes:
41 //
42 // - Characters not in the base64 alphabet
43 {"abc&", nullptr},
44 {"ab-d", nullptr},
45 // - input len % 4 == 1
46 {"abcde", nullptr},
47 {"a", nullptr},
48
49 // Invalid padding causes failure if kForgiving is set.
50 {"abcd=", nullptr},
51 {"abcd==", nullptr},
52 {"abcd===", nullptr},
53 {"abcd====", nullptr},
54 {"abcd==============", nullptr},
55 {"=", nullptr},
56 {"====", nullptr},
57
58 // Otherwise, inputs that are multiples of 4 always succeed, this matches
59 // kStrict mode.
60 {"abcd", "i\xB7\x1D"},
61 {"abc=", "i\xB7"},
62 {"abcdefgh", "i\xB7\x1Dy\xF8!"},
63
64 // kForgiving mode allows for omitting padding (to a multiple of 4) if
65 // len % 4 != 1.
66 {"abcdef", "i\xB7\x1Dy"},
67 {"abc", "i\xB7"},
68 {"ab", "i"},
69
70 // Whitespace should be allowed if kForgiving is set, matching
71 // https://infra.spec.whatwg.org/#ascii-whitespace:
72 // ASCII whitespace is U+0009 TAB '\t', U+000A LF '\n', U+000C FF '\f',
73 // U+000D CR '\r', or U+0020 SPACE ' '.
74 {" a bcd", "i\xB7\x1D"},
75 {"ab\t\tc=", "i\xB7"},
76 {"ab c\ndefgh", "i\xB7\x1Dy\xF8!"},
77 {"a\tb\nc\f d\r", "i\xB7\x1D"},
78
79 // U+000B VT '\v' is _not_ valid whitespace to be stripped.
80 {"ab\vcd", nullptr},
81
82 // Empty string should yield an empty result.
83 {"", ""},
84 };
85 for (const auto& test_case : kTestCases) {
86 SCOPED_TRACE(::testing::Message()
87 << EscapeAllExceptUnreserved(test_case.in));
88 std::string output;
89 bool success =
90 Base64Decode(test_case.in, &output, Base64DecodePolicy::kForgiving);
91 bool expected_success = test_case.expected_out != nullptr;
92 EXPECT_EQ(success, expected_success);
93 if (expected_success) {
94 EXPECT_EQ(output, test_case.expected_out);
95 }
96 }
97}
98
Collin Bakere21f723d2019-09-05 20:05:4199TEST(Base64Test, Binary) {
100 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
101
David Benjamin48808e22022-09-21 19:39:12102 std::string binary_encoded = Base64Encode(kData);
Collin Bakere21f723d2019-09-05 20:05:41103
Helmut Januschka0fc785b2024-04-17 21:13:36104 // Check that encoding the same data through the std::string_view interface
105 // gives the same results.
Tom Sepez2c860fd2024-02-01 21:31:26106 std::string string_piece_encoded = Base64Encode(
Helmut Januschka0fc785b2024-04-17 21:13:36107 std::string_view(reinterpret_cast<const char*>(kData), sizeof(kData)));
Collin Bakere21f723d2019-09-05 20:05:41108
109 EXPECT_EQ(binary_encoded, string_piece_encoded);
David Benjamin47bb5ec2022-02-01 23:12:28110
111 EXPECT_THAT(Base64Decode(binary_encoded),
112 testing::Optional(testing::ElementsAreArray(kData)));
113 EXPECT_FALSE(Base64Decode("invalid base64!"));
David Benjamin48808e22022-09-21 19:39:12114
115 std::string encoded_with_prefix = "PREFIX";
116 Base64EncodeAppend(kData, &encoded_with_prefix);
117 EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded);
Collin Bakere21f723d2019-09-05 20:05:41118}
119
georgesak85e05a732014-11-11 17:19:43120TEST(Base64Test, InPlace) {
121 const std::string kText = "hello world";
122 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
georgesak85e05a732014-11-11 17:19:43123
wd l4a70b3ff2023-09-26 20:13:35124 std::string text = Base64Encode(kText);
georgesak85e05a732014-11-11 17:19:43125 EXPECT_EQ(kBase64Text, text);
126
127 bool ok = Base64Decode(text, &text);
128 EXPECT_TRUE(ok);
129 EXPECT_EQ(text, kText);
130}
131
David Benjamin48808e22022-09-21 19:39:12132TEST(Base64Test, Overflow) {
133 // `Base64Encode` makes the input larger, which means inputs whose base64
134 // output overflows `size_t`. Actually allocating a span of this size will
135 // likely fail, but we test it with a fake span and assume a correct
136 // implementation will check for overflow before touching the input.
137 //
138 // Note that, with or without an overflow check, the function will still
139 // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for
140 // a `CHECK`-based failure.
141 uint8_t b;
142 auto large_span = base::make_span(&b, MODP_B64_MAX_INPUT_LEN + 1);
143 EXPECT_CHECK_DEATH(Base64Encode(large_span));
144
145 std::string output = "PREFIX";
146 EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output));
147
Charlie Harrison39d533222022-11-22 23:49:39148 // `modp_b64_encode_data_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is
David Benjamin48808e22022-09-21 19:39:12149 // correct be verifying the computation doesn't overflow.
150 base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN;
Charlie Harrison39d533222022-11-22 23:49:39151 EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid());
David Benjamin48808e22022-09-21 19:39:12152}
153
[email protected]24f111a2012-09-23 04:51:04154} // namespace base