blob: bff8eeed8e9016ebc3195b60efea2197c1ef1f87 [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
danakj51d26a42024-04-25 14:23:565#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
[email protected]978df342009-11-24 06:21:5310#include "base/base64.h"
[email protected]24f111a2012-09-23 04:51:0411
Helmut Januschka0fc785b2024-04-17 21:13:3612#include <string_view>
13
David Benjamin48808e22022-09-21 19:39:1214#include "base/numerics/checked_math.h"
Charlie Harrison7b633d92022-11-29 05:23:5015#include "base/strings/escape.h"
David Benjamin48808e22022-09-21 19:39:1216#include "base/test/gtest_util.h"
David Benjamin47bb5ec2022-02-01 23:12:2817#include "testing/gmock/include/gmock/gmock.h"
initial.commit586acc5fe2008-07-26 22:42:5218#include "testing/gtest/include/gtest/gtest.h"
David Benjamin48808e22022-09-21 19:39:1219#include "third_party/modp_b64/modp_b64.h"
initial.commit586acc5fe2008-07-26 22:42:5220
[email protected]24f111a2012-09-23 04:51:0421namespace base {
initial.commit586acc5fe2008-07-26 22:42:5222
23TEST(Base64Test, Basic) {
24 const std::string kText = "hello world";
25 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
26
[email protected]24f111a2012-09-23 04:51:0427 std::string decoded;
initial.commit586acc5fe2008-07-26 22:42:5228 bool ok;
29
wd l4a70b3ff2023-09-26 20:13:3530 std::string encoded = Base64Encode(kText);
initial.commit586acc5fe2008-07-26 22:42:5231 EXPECT_EQ(kBase64Text, encoded);
32
[email protected]24f111a2012-09-23 04:51:0433 ok = Base64Decode(encoded, &decoded);
initial.commit586acc5fe2008-07-26 22:42:5234 EXPECT_TRUE(ok);
35 EXPECT_EQ(kText, decoded);
36}
[email protected]24f111a2012-09-23 04:51:0437
Charlie Harrison7b633d92022-11-29 05:23:5038TEST(Base64Test, Forgiving) {
39 struct {
40 const char* in;
41
42 // nullptr indicates a decode failure.
43 const char* expected_out;
44 } kTestCases[] = {
45 // Failures that should apply in all decoding modes:
46 //
47 // - Characters not in the base64 alphabet
48 {"abc&", nullptr},
49 {"ab-d", nullptr},
50 // - input len % 4 == 1
51 {"abcde", nullptr},
52 {"a", nullptr},
53
54 // Invalid padding causes failure if kForgiving is set.
55 {"abcd=", nullptr},
56 {"abcd==", nullptr},
57 {"abcd===", nullptr},
58 {"abcd====", nullptr},
59 {"abcd==============", nullptr},
60 {"=", nullptr},
61 {"====", nullptr},
62
63 // Otherwise, inputs that are multiples of 4 always succeed, this matches
64 // kStrict mode.
65 {"abcd", "i\xB7\x1D"},
66 {"abc=", "i\xB7"},
67 {"abcdefgh", "i\xB7\x1Dy\xF8!"},
68
69 // kForgiving mode allows for omitting padding (to a multiple of 4) if
70 // len % 4 != 1.
71 {"abcdef", "i\xB7\x1Dy"},
72 {"abc", "i\xB7"},
73 {"ab", "i"},
74
75 // Whitespace should be allowed if kForgiving is set, matching
76 // https://infra.spec.whatwg.org/#ascii-whitespace:
77 // ASCII whitespace is U+0009 TAB '\t', U+000A LF '\n', U+000C FF '\f',
78 // U+000D CR '\r', or U+0020 SPACE ' '.
79 {" a bcd", "i\xB7\x1D"},
80 {"ab\t\tc=", "i\xB7"},
81 {"ab c\ndefgh", "i\xB7\x1Dy\xF8!"},
82 {"a\tb\nc\f d\r", "i\xB7\x1D"},
83
84 // U+000B VT '\v' is _not_ valid whitespace to be stripped.
85 {"ab\vcd", nullptr},
86
87 // Empty string should yield an empty result.
88 {"", ""},
89 };
90 for (const auto& test_case : kTestCases) {
91 SCOPED_TRACE(::testing::Message()
92 << EscapeAllExceptUnreserved(test_case.in));
93 std::string output;
94 bool success =
95 Base64Decode(test_case.in, &output, Base64DecodePolicy::kForgiving);
96 bool expected_success = test_case.expected_out != nullptr;
97 EXPECT_EQ(success, expected_success);
98 if (expected_success) {
99 EXPECT_EQ(output, test_case.expected_out);
100 }
101 }
102}
103
Collin Bakere21f723d2019-09-05 20:05:41104TEST(Base64Test, Binary) {
105 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
106
David Benjamin48808e22022-09-21 19:39:12107 std::string binary_encoded = Base64Encode(kData);
Collin Bakere21f723d2019-09-05 20:05:41108
Helmut Januschka0fc785b2024-04-17 21:13:36109 // Check that encoding the same data through the std::string_view interface
110 // gives the same results.
Tom Sepez2c860fd2024-02-01 21:31:26111 std::string string_piece_encoded = Base64Encode(
Helmut Januschka0fc785b2024-04-17 21:13:36112 std::string_view(reinterpret_cast<const char*>(kData), sizeof(kData)));
Collin Bakere21f723d2019-09-05 20:05:41113
114 EXPECT_EQ(binary_encoded, string_piece_encoded);
David Benjamin47bb5ec2022-02-01 23:12:28115
116 EXPECT_THAT(Base64Decode(binary_encoded),
117 testing::Optional(testing::ElementsAreArray(kData)));
118 EXPECT_FALSE(Base64Decode("invalid base64!"));
David Benjamin48808e22022-09-21 19:39:12119
120 std::string encoded_with_prefix = "PREFIX";
121 Base64EncodeAppend(kData, &encoded_with_prefix);
122 EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded);
Collin Bakere21f723d2019-09-05 20:05:41123}
124
georgesak85e05a732014-11-11 17:19:43125TEST(Base64Test, InPlace) {
126 const std::string kText = "hello world";
127 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
georgesak85e05a732014-11-11 17:19:43128
wd l4a70b3ff2023-09-26 20:13:35129 std::string text = Base64Encode(kText);
georgesak85e05a732014-11-11 17:19:43130 EXPECT_EQ(kBase64Text, text);
131
132 bool ok = Base64Decode(text, &text);
133 EXPECT_TRUE(ok);
134 EXPECT_EQ(text, kText);
135}
136
David Benjamin48808e22022-09-21 19:39:12137TEST(Base64Test, Overflow) {
138 // `Base64Encode` makes the input larger, which means inputs whose base64
139 // output overflows `size_t`. Actually allocating a span of this size will
140 // likely fail, but we test it with a fake span and assume a correct
141 // implementation will check for overflow before touching the input.
142 //
143 // Note that, with or without an overflow check, the function will still
144 // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for
145 // a `CHECK`-based failure.
146 uint8_t b;
Peter Kastingae7e314f2024-11-27 18:04:07147 auto large_span = span(&b, MODP_B64_MAX_INPUT_LEN + 1);
David Benjamin48808e22022-09-21 19:39:12148 EXPECT_CHECK_DEATH(Base64Encode(large_span));
149
150 std::string output = "PREFIX";
151 EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output));
152
Charlie Harrison39d533222022-11-22 23:49:39153 // `modp_b64_encode_data_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is
David Benjamin48808e22022-09-21 19:39:12154 // correct be verifying the computation doesn't overflow.
155 base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN;
Charlie Harrison39d533222022-11-22 23:49:39156 EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid());
David Benjamin48808e22022-09-21 19:39:12157}
158
[email protected]24f111a2012-09-23 04:51:04159} // namespace base