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" |
| 8 | #include "base/test/gtest_util.h" |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 9 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame^] | 11 | #include "third_party/modp_b64/modp_b64.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 13 | namespace base { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | |
| 15 | TEST(Base64Test, Basic) { |
| 16 | const std::string kText = "hello world"; |
| 17 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
| 18 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 19 | std::string encoded; |
| 20 | std::string decoded; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | bool ok; |
| 22 | |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 23 | Base64Encode(kText, &encoded); |
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 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 31 | TEST(Base64Test, Binary) { |
| 32 | const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF}; |
| 33 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame^] | 34 | std::string binary_encoded = Base64Encode(kData); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 35 | |
| 36 | // Check that encoding the same data through the StringPiece interface gives |
| 37 | // the same results. |
| 38 | std::string string_piece_encoded; |
| 39 | Base64Encode(StringPiece(reinterpret_cast<const char*>(kData), sizeof(kData)), |
| 40 | &string_piece_encoded); |
| 41 | |
| 42 | EXPECT_EQ(binary_encoded, string_piece_encoded); |
David Benjamin | 47bb5ec | 2022-02-01 23:12:28 | [diff] [blame] | 43 | |
| 44 | EXPECT_THAT(Base64Decode(binary_encoded), |
| 45 | testing::Optional(testing::ElementsAreArray(kData))); |
| 46 | EXPECT_FALSE(Base64Decode("invalid base64!")); |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame^] | 47 | |
| 48 | std::string encoded_with_prefix = "PREFIX"; |
| 49 | Base64EncodeAppend(kData, &encoded_with_prefix); |
| 50 | EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 51 | } |
| 52 | |
georgesak | 85e05a73 | 2014-11-11 17:19:43 | [diff] [blame] | 53 | TEST(Base64Test, InPlace) { |
| 54 | const std::string kText = "hello world"; |
| 55 | const std::string kBase64Text = "aGVsbG8gd29ybGQ="; |
| 56 | std::string text(kText); |
| 57 | |
| 58 | Base64Encode(text, &text); |
| 59 | EXPECT_EQ(kBase64Text, text); |
| 60 | |
| 61 | bool ok = Base64Decode(text, &text); |
| 62 | EXPECT_TRUE(ok); |
| 63 | EXPECT_EQ(text, kText); |
| 64 | } |
| 65 | |
David Benjamin | 48808e2 | 2022-09-21 19:39:12 | [diff] [blame^] | 66 | TEST(Base64Test, Overflow) { |
| 67 | // `Base64Encode` makes the input larger, which means inputs whose base64 |
| 68 | // output overflows `size_t`. Actually allocating a span of this size will |
| 69 | // likely fail, but we test it with a fake span and assume a correct |
| 70 | // implementation will check for overflow before touching the input. |
| 71 | // |
| 72 | // Note that, with or without an overflow check, the function will still |
| 73 | // crash. This test is only meaningful because `EXPECT_CHECK_DEATH` looks for |
| 74 | // a `CHECK`-based failure. |
| 75 | uint8_t b; |
| 76 | auto large_span = base::make_span(&b, MODP_B64_MAX_INPUT_LEN + 1); |
| 77 | EXPECT_CHECK_DEATH(Base64Encode(large_span)); |
| 78 | |
| 79 | std::string output = "PREFIX"; |
| 80 | EXPECT_CHECK_DEATH(Base64EncodeAppend(large_span, &output)); |
| 81 | |
| 82 | // `modp_b64_encode_len` is a macro, so check `MODP_B64_MAX_INPUT_LEN` is |
| 83 | // correct be verifying the computation doesn't overflow. |
| 84 | base::CheckedNumeric<size_t> max_len = MODP_B64_MAX_INPUT_LEN; |
| 85 | EXPECT_TRUE(modp_b64_encode_len(max_len).IsValid()); |
| 86 | } |
| 87 | |
[email protected] | 24f111a | 2012-09-23 04:51:04 | [diff] [blame] | 88 | } // namespace base |