1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/wtf/text/base64.h"
#include <optional>
#include <string_view>
#include "base/containers/span.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace WTF {
TEST(Base64Test, Encode) {
struct {
std::string_view in;
Vector<char> expected_out;
} kTestCases[] = {{"", {}},
{"i", {'a', 'Q', '=', '='}},
{"i\xB7", {'a', 'b', 'c', '='}},
{"i\xB7\x1D", {'a', 'b', 'c', 'd'}}};
for (const auto& test : kTestCases) {
auto in = base::as_byte_span(test.in);
Vector<char> out_vec;
Base64Encode(in, out_vec);
EXPECT_EQ(out_vec, test.expected_out);
String out_str = Base64Encode(in);
EXPECT_EQ(out_str, String(test.expected_out));
}
}
TEST(Base64Test, DecodeNoPaddingValidation) {
struct {
const char* in;
const char* expected_out;
} kTestCases[] = {
// Inputs that are multiples of 4 always succeed.
{"abcd", "i\xB7\x1D"},
{"abc=", "i\xB7"},
{"abcdefgh", "i\xB7\x1Dy\xF8!"},
// Lack of proper padding (to a multiple of 4) always succeeds if
// len % 4 != 1.
{"abcdef", "i\xB7\x1Dy"},
{"abc", "i\xB7"},
{"ab", "i"},
// Invalid padding is ignored if kNoPaddingValidation is set.
{"abcd=", "i\xB7\x1D"},
{"abcd==", "i\xB7\x1D"},
{"abcd===", "i\xB7\x1D"},
{"abcd==============", "i\xB7\x1D"},
{"=", ""},
// Whitespace should not be allowed if kNoPaddingValidation is set.
{" a bcd", nullptr},
{"ab\t\tc=", nullptr},
{"ab c\ndefgh ", nullptr},
// Failures that should apply in all decoding modes.
{"abc&", nullptr},
{"abcde", nullptr},
{"a", nullptr},
// Empty string should yield an empty result.
{"", ""},
};
for (const auto& test : kTestCases) {
SCOPED_TRACE(::testing::Message() << test.in);
Vector<uint8_t> out;
String in = String(test.in);
bool expected_success = test.expected_out != nullptr;
Vector<uint8_t> expected_out;
if (expected_success) {
expected_out.insert(0, test.expected_out, strlen(test.expected_out));
}
bool success_8bit = Base64Decode(in, out);
EXPECT_EQ(expected_success, success_8bit);
if (expected_success) {
EXPECT_EQ(expected_out, out);
}
out.clear();
in.Ensure16Bit();
bool success_16bit = Base64Decode(in, out);
EXPECT_EQ(expected_success, success_16bit);
if (expected_success) {
EXPECT_EQ(expected_out, out);
}
}
}
TEST(Base64Test, ForgivingBase64Decode) {
struct {
const char* in;
const char* expected_out;
} kTestCases[] = {
// Inputs that are multiples of 4 always succeed.
{"abcd", "i\xB7\x1D"},
{"abc=", "i\xB7"},
{"abcdefgh", "i\xB7\x1Dy\xF8!"},
// Lack of proper padding (to a multiple of 4) always succeeds if
// len % 4 != 1.
{"abcdef", "i\xB7\x1Dy"},
{"abc", "i\xB7"},
{"ab", "i"},
// Invalid padding causes failure if kForgiving is set.
{"abcd=", nullptr},
{"abcd==", nullptr},
{"abcd===", nullptr},
{"abcd==============", nullptr},
{"=", nullptr},
// Whitespace should be allow if kForgiving is set.
{" a bcd", "i\xB7\x1D"},
{"ab\t\tc=", "i\xB7"},
{"ab c\ndefgh", "i\xB7\x1Dy\xF8!"},
// Failures that should apply in all decoding modes.
{"abc&", nullptr},
{"abcde", nullptr},
{"a", nullptr},
// Empty string should yield an empty result.
{"", ""},
};
for (const auto& test : kTestCases) {
SCOPED_TRACE(::testing::Message() << test.in);
Vector<uint8_t> out;
String in = String(test.in);
bool expected_success = test.expected_out != nullptr;
Vector<uint8_t> expected_out;
if (expected_success) {
expected_out.insert(0, test.expected_out, strlen(test.expected_out));
}
bool success_8bit = Base64Decode(in, out, Base64DecodePolicy::kForgiving);
EXPECT_EQ(expected_success, success_8bit);
if (expected_success) {
EXPECT_EQ(expected_out, out);
}
out.clear();
in.Ensure16Bit();
bool success_16bit = Base64Decode(in, out, Base64DecodePolicy::kForgiving);
EXPECT_EQ(expected_success, success_16bit);
if (expected_success) {
EXPECT_EQ(expected_out, out);
}
}
}
} // namespace WTF
|