Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 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 | |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 10 | #include "base/base64.h" |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 11 | |
Helmut Januschka | 0fc785b | 2024-04-17 21:13:36 | [diff] [blame] | 12 | #include <string_view> |
| 13 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 14 | #include "base/numerics/checked_math.h" |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 15 | #include "base/strings/escape.h" |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 16 | #include "base/test/gtest_util.h" |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 19 | #include "third_party/modp_b64/modp_b64.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 21 | namespace base { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | |
| 23 | TEST(Base64Test, Basic) { |
| 24 | const std::string kText = "hello world"; |
| 25 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
| 26 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 27 | std::string decoded; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | bool ok; |
| 29 | |
wd l | 4a70b3ff | 2023-09-26 20:13:35 | [diff] [blame] | 30 | std::string encoded = Base64Encode(kText); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | EXPECT_EQ(kBase64Text, encoded); |
| 32 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 33 | ok = Base64Decode(encoded, &decoded); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | EXPECT_TRUE(ok); |
| 35 | EXPECT_EQ(kText, decoded); |
| 36 | } |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 37 | |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 38 | TEST(Base64Test, ForgivingAndStrictDecode) { |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 39 | struct { |
| 40 | const char* in; |
| 41 | |
| 42 | // nullptr indicates a decode failure. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 43 | const char* expected_out_forgiving; |
| 44 | const char* expected_out_strict; |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 45 | } kTestCases[] = { |
| 46 | // Failures that should apply in all decoding modes: |
| 47 | // |
| 48 | // - Characters not in the base64 alphabet |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 49 | {"abc&", nullptr, nullptr}, |
| 50 | {"ab-d", nullptr, nullptr}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 51 | // - input len % 4 == 1 |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 52 | {"abcde", nullptr, nullptr}, |
| 53 | {"a", nullptr, nullptr}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 54 | |
| 55 | // Invalid padding causes failure if kForgiving is set. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 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}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 64 | |
| 65 | // Otherwise, inputs that are multiples of 4 always succeed, this matches |
| 66 | // kStrict mode. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 67 | {"abcd", "i\xB7\x1D", "i\xB7\x1D"}, |
| 68 | {"abc=", "i\xB7", "i\xB7"}, |
| 69 | {"abcdefgh", "i\xB7\x1Dy\xF8!", "i\xB7\x1Dy\xF8!"}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 70 | |
| 71 | // kForgiving mode allows for omitting padding (to a multiple of 4) if |
| 72 | // len % 4 != 1. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 73 | {"abcdef", "i\xB7\x1Dy", nullptr}, |
| 74 | {"abc", "i\xB7", nullptr}, |
| 75 | {"ab", "i", nullptr}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 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 ' '. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 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}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 86 | |
| 87 | // U+000B VT '\v' is _not_ valid whitespace to be stripped. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 88 | {"ab\vcd", nullptr, nullptr}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 89 | |
| 90 | // Empty string should yield an empty result. |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 91 | {"", "", ""}, |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 92 | }; |
| 93 | for (const auto& test_case : kTestCases) { |
| 94 | SCOPED_TRACE(::testing::Message() |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 95 | << "Forgiving: " << EscapeAllExceptUnreserved(test_case.in)); |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 96 | std::string output; |
| 97 | bool success = |
| 98 | Base64Decode(test_case.in, &output, Base64DecodePolicy::kForgiving); |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 99 | bool expected_success = test_case.expected_out_forgiving != nullptr; |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 100 | EXPECT_EQ(success, expected_success); |
| 101 | if (expected_success) { |
Nidhi Jaju | 95d13c2 | 2024-11-30 01:58:55 | [diff] [blame] | 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); |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 119 | TEST(Base64Test, Binary) { |
| 120 | const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF}; |
| 121 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 122 | std::string binary_encoded = Base64Encode(kData); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 123 | |
Helmut Januschka | 0fc785b | 2024-04-17 21:13:36 | [diff] [blame] | 124 | // Check that encoding the same data through the std::string_view interface |
| 125 | // gives the same results. |
Tom Sepez | 2c860fd | 2024-02-01 21:31:26 | [diff] [blame] | 126 | std::string string_piece_encoded = Base64Encode( |
Helmut Januschka | 0fc785b | 2024-04-17 21:13:36 | [diff] [blame] | 127 | std::string_view(reinterpret_cast<const char*>(kData), sizeof(kData))); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 128 | |
| 129 | EXPECT_EQ(binary_encoded, string_piece_encoded); |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 130 | |
| 131 | EXPECT_THAT(Base64Decode(binary_encoded), |
| 132 | testing::Optional(testing::ElementsAreArray(kData))); |
| 133 | EXPECT_FALSE(Base64Decode("invalid base64!")); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 134 | |
| 135 | std::string encoded_with_prefix = "PREFIX"; |
| 136 | Base64EncodeAppend(kData, &encoded_with_prefix); |
| 137 | EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 138 | } |
| 139 | |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 140 | TEST(Base64Test, InPlace) { |
| 141 | const std::string kText = "hello world"; |
| 142 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 143 | |
wd l | 4a70b3ff | 2023-09-26 20:13:35 | [diff] [blame] | 144 | std::string text = Base64Encode(kText); |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 145 | EXPECT_EQ(kBase64Text, text); |
| 146 | |
| 147 | bool ok = Base64Decode(text, &text); |
| 148 | EXPECT_TRUE(ok); |
| 149 | EXPECT_EQ(text, kText); |
| 150 | } |
| 151 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 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; |
Peter Kasting | ae7e314f | 2024-11-27 18:04:07 | [diff] [blame] | 162 | auto large_span = span(&b, MODP_B64_MAX_INPUT_LEN + 1); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 163 | EXPECT_CHECK_DEATH(Base64Encode(large_span)); |
| 164 | |
| 165 | std::string output = "PREFIX"; |
| 166 | EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output)); |
| 167 | |
Charlie Harrison | 39d53322 | 2022-11-22 23:49:39 | [diff] [blame] | 168 | // `modp_b64_encode_data_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 169 | // correct be verifying the computation doesn't overflow. |
| 170 | base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN; |
Charlie Harrison | 39d53322 | 2022-11-22 23:49:39 | [diff] [blame] | 171 | EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid()); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 174 | } // namespace base |