blob: b593633ce5b2bc7bb27f267cc2c97681b1818b14 [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
David Benjamin48808e22022-09-21 19:39:127#include "base/numerics/checked_math.h"
8#include "base/test/gtest_util.h"
David Benjamin47bb5ec2022-02-01 23:12:289#include "testing/gmock/include/gmock/gmock.h"
initial.commit586acc5fe2008-07-26 22:42:5210#include "testing/gtest/include/gtest/gtest.h"
David Benjamin48808e22022-09-21 19:39:1211#include "third_party/modp_b64/modp_b64.h"
initial.commit586acc5fe2008-07-26 22:42:5212
[email protected]24f111a2012-09-23 04:51:0413namespace base {
initial.commit586acc5fe2008-07-26 22:42:5214
15TEST(Base64Test, Basic) {
16 const std::string kText = "hello world";
17 const std::string kBase64Text = "aGVsbG8gd29ybGQ=";
18
[email protected]24f111a2012-09-23 04:51:0419 std::string encoded;
20 std::string decoded;
initial.commit586acc5fe2008-07-26 22:42:5221 bool ok;
22
[email protected]33fca122013-12-11 01:48:5023 Base64Encode(kText, &encoded);
initial.commit586acc5fe2008-07-26 22:42:5224 EXPECT_EQ(kBase64Text, encoded);
25
[email protected]24f111a2012-09-23 04:51:0426 ok = Base64Decode(encoded, &decoded);
initial.commit586acc5fe2008-07-26 22:42:5227 EXPECT_TRUE(ok);
28 EXPECT_EQ(kText, decoded);
29}
[email protected]24f111a2012-09-23 04:51:0430
Collin Bakere21f723d2019-09-05 20:05:4131TEST(Base64Test, Binary) {
32 const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF};
33
David Benjamin48808e22022-09-21 19:39:1234 std::string binary_encoded = Base64Encode(kData);
Collin Bakere21f723d2019-09-05 20:05:4135
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 Benjamin47bb5ec2022-02-01 23:12:2843
44 EXPECT_THAT(Base64Decode(binary_encoded),
45 testing::Optional(testing::ElementsAreArray(kData)));
46 EXPECT_FALSE(Base64Decode("invalid base64!"));
David Benjamin48808e22022-09-21 19:39:1247
48 std::string encoded_with_prefix = "PREFIX";
49 Base64EncodeAppend(kData, &encoded_with_prefix);
50 EXPECT_EQ(encoded_with_prefix, "PREFIX" + binary_encoded);
Collin Bakere21f723d2019-09-05 20:05:4151}
52
georgesak85e05a732014-11-11 17:19:4353TEST(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 Benjamin48808e22022-09-21 19:39:1266TEST(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]24f111a2012-09-23 04:51:0488} // namespace base