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 | |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 5 | #include "base/base64.h" |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 6 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 7 | #include "base/numerics/checked_math.h" |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 8 | #include "base/strings/escape.h" |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 9 | #include "base/test/gtest_util.h" |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 10 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 12 | #include "third_party/modp_b64/modp_b64.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 13 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 14 | namespace base { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | |
| 16 | TEST(Base64Test, Basic) { |
| 17 | const std::string kText = "hello world"; |
| 18 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
| 19 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 20 | std::string decoded; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | bool ok; |
| 22 | |
wd l | 4a70b3ff | 2023-09-26 20:13:35 | [diff] [blame] | 23 | std::string encoded = Base64Encode(kText); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | EXPECT_EQ(kBase64Text, encoded); |
| 25 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 26 | ok = Base64Decode(encoded, &decoded); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | EXPECT_TRUE(ok); |
| 28 | EXPECT_EQ(kText, decoded); |
| 29 | } |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 30 | |
Charlie Harrison | 7b633d9 | 2022-11-29 05:23:50 | [diff] [blame] | 31 | TEST(Base64Test, Forgiving) { |
| 32 | struct { |
| 33 | const char* in; |
| 34 | |
| 35 | // nullptr indicates a decode failure. |
| 36 | const char* expected_out; |
| 37 | } kTestCases[] = { |
| 38 | // Failures that should apply in all decoding modes: |
| 39 | // |
| 40 | // - Characters not in the base64 alphabet |
| 41 | {"abc&", nullptr}, |
| 42 | {"ab-d", nullptr}, |
| 43 | // - input len % 4 == 1 |
| 44 | {"abcde", nullptr}, |
| 45 | {"a", nullptr}, |
| 46 | |
| 47 | // Invalid padding causes failure if kForgiving is set. |
| 48 | {"abcd=", nullptr}, |
| 49 | {"abcd==", nullptr}, |
| 50 | {"abcd===", nullptr}, |
| 51 | {"abcd====", nullptr}, |
| 52 | {"abcd==============", nullptr}, |
| 53 | {"=", nullptr}, |
| 54 | {"====", nullptr}, |
| 55 | |
| 56 | // Otherwise, inputs that are multiples of 4 always succeed, this matches |
| 57 | // kStrict mode. |
| 58 | {"abcd", "i\xB7\x1D"}, |
| 59 | {"abc=", "i\xB7"}, |
| 60 | {"abcdefgh", "i\xB7\x1Dy\xF8!"}, |
| 61 | |
| 62 | // kForgiving mode allows for omitting padding (to a multiple of 4) if |
| 63 | // len % 4 != 1. |
| 64 | {"abcdef", "i\xB7\x1Dy"}, |
| 65 | {"abc", "i\xB7"}, |
| 66 | {"ab", "i"}, |
| 67 | |
| 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"}, |
| 76 | |
| 77 | // U+000B VT '\v' is _not_ valid whitespace to be stripped. |
| 78 | {"ab\vcd", nullptr}, |
| 79 | |
| 80 | // Empty string should yield an empty result. |
| 81 | {"", ""}, |
| 82 | }; |
| 83 | for (const auto& test_case : kTestCases) { |
| 84 | SCOPED_TRACE(::testing::Message() |
| 85 | << EscapeAllExceptUnreserved(test_case.in)); |
| 86 | std::string output; |
| 87 | bool success = |
| 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); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 97 | TEST(Base64Test, Binary) { |
| 98 | const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF}; |
| 99 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 100 | std::string binary_encoded = Base64Encode(kData); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 101 | |
| 102 | // Check that encoding the same data through the StringPiece interface gives |
| 103 | // the same results. |
Tom Sepez | 2c860fd | 2024-02-01 21:31:26 | [diff] [blame^] | 104 | std::string string_piece_encoded = Base64Encode( |
| 105 | StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData))); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 106 | |
| 107 | EXPECT_EQ(binary_encoded, string_piece_encoded); |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 108 | |
| 109 | EXPECT_THAT(Base64Decode(binary_encoded), |
| 110 | testing::Optional(testing::ElementsAreArray(kData))); |
| 111 | EXPECT_FALSE(Base64Decode("invalid base64!")); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 112 | |
| 113 | std::string encoded_with_prefix = "PREFIX"; |
| 114 | Base64EncodeAppend(kData, &encoded_with_prefix); |
| 115 | EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 116 | } |
| 117 | |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 118 | TEST(Base64Test, InPlace) { |
| 119 | const std::string kText = "hello world"; |
| 120 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 121 | |
wd l | 4a70b3ff | 2023-09-26 20:13:35 | [diff] [blame] | 122 | std::string text = Base64Encode(kText); |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 123 | EXPECT_EQ(kBase64Text, text); |
| 124 | |
| 125 | bool ok = Base64Decode(text, &text); |
| 126 | EXPECT_TRUE(ok); |
| 127 | EXPECT_EQ(text, kText); |
| 128 | } |
| 129 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 130 | TEST(Base64Test, Overflow) { |
| 131 | // `Base64Encode` makes the input larger, which means inputs whose base64 |
| 132 | // output overflows `size_t`. Actually allocating a span of this size will |
| 133 | // likely fail, but we test it with a fake span and assume a correct |
| 134 | // implementation will check for overflow before touching the input. |
| 135 | // |
| 136 | // Note that, with or without an overflow check, the function will still |
| 137 | // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for |
| 138 | // a `CHECK`-based failure. |
| 139 | uint8_t b; |
| 140 | auto large_span = base::make_span(&b, MODP_B64_MAX_INPUT_LEN + 1); |
| 141 | EXPECT_CHECK_DEATH(Base64Encode(large_span)); |
| 142 | |
| 143 | std::string output = "PREFIX"; |
| 144 | EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output)); |
| 145 | |
Charlie Harrison | 39d53322 | 2022-11-22 23:49:39 | [diff] [blame] | 146 | // `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] | 147 | // correct be verifying the computation doesn't overflow. |
| 148 | base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN; |
Charlie Harrison | 39d53322 | 2022-11-22 23:49:39 | [diff] [blame] | 149 | EXPECT_TRUE(modp_b64_encode_data_len(max_len).IsValid()); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 152 | } // namespace base |