blob: 59f3a05dd7f774b85accd63ebe3a03364214d9dc [file] [log] [blame]
Wyatt Hepler3c2e9522020-01-09 16:08:54 -08001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_tokenizer/base64.h"
16
17#include <cstring>
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070018#include <span>
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080019#include <string_view>
20
21#include "gtest/gtest.h"
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080022
23namespace pw::tokenizer {
24namespace {
25
26class PrefixedBase64 : public ::testing::Test {
27 protected:
28 PrefixedBase64() : binary_{}, base64_{} {}
29
30 std::byte binary_[32];
31 char base64_[32];
32};
33
34const struct TestData {
35 template <size_t kSize>
Armando Montanez888370d2020-05-01 18:29:22 -070036 TestData(const char (&binary_data)[kSize], const char* base64_data)
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070037 : binary{std::as_bytes(std::span(binary_data, kSize - 1))},
38 base64(base64_data) {}
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080039
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070040 std::span<const std::byte> binary;
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080041 std::string_view base64;
42} kTestData[] = {
43 {"", "$"},
44 {"\x00", "$AA=="},
45 {"\x71", "$cQ=="},
46 {"\xff", "$/w=="},
47 {"\x63\xa9", "$Y6k="},
48 {"\x69\x89\x03", "$aYkD"},
49 {"\x80\xf5\xc8\xd4", "$gPXI1A=="},
50 {"\x6e\xb8\x91\x3f\xac", "$briRP6w="},
51 {"\x1f\x88\x91\xbb\xd7\x10", "$H4iRu9cQ"},
52 {"\xac\xcf\xb2\xd5\xee\xa2\x8e", "$rM+y1e6ijg=="},
53 {"\xff\x15\x25\x7e\x7b\xc9\x7b\x60", "$/xUlfnvJe2A="},
54 {"\xd5\xab\xd9\xa6\xae\xaa\x33\x9f\x66", "$1avZpq6qM59m"},
55 {"\x6b\xfd\x95\xc5\x4a\xc7\xc2\x39\x45\xdc", "$a/2VxUrHwjlF3A=="},
56 {"\x4c\xde\xee\xb8\x68\x0d\x9c\x66\x3e\xea\x46", "$TN7uuGgNnGY+6kY="},
57};
58
59TEST_F(PrefixedBase64, Encode) {
Wyatt Heplera46bf7d2020-01-08 18:10:25 -080060 for (auto& [binary, base64] : kTestData) {
61 EXPECT_EQ(base64.size(), PrefixedBase64Encode(binary, base64_));
62 ASSERT_EQ(base64, base64_);
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080063 }
64}
65
66TEST_F(PrefixedBase64, Encode_EmptyInput_WritesPrefix) {
67 EXPECT_EQ(1u, PrefixedBase64Encode({}, base64_));
68 EXPECT_EQ('$', base64_[0]);
69}
70
71TEST_F(PrefixedBase64, Encode_EmptyOutput_WritesNothing) {
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070072 EXPECT_EQ(0u,
73 PrefixedBase64Encode(kTestData[5].binary, std::span(base64_, 0)));
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080074 EXPECT_EQ('\0', base64_[0]);
75}
76
77TEST_F(PrefixedBase64, Decode) {
Wyatt Heplera46bf7d2020-01-08 18:10:25 -080078 for (auto& [binary, base64] : kTestData) {
79 EXPECT_EQ(binary.size(), PrefixedBase64Decode(base64, binary_));
80 ASSERT_EQ(0, std::memcmp(binary.data(), binary_, binary.size()));
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080081 }
82}
83
84TEST_F(PrefixedBase64, Decode_EmptyInput_WritesNothing) {
85 EXPECT_EQ(0u, PrefixedBase64Decode({}, binary_));
86 EXPECT_EQ(std::byte{0}, binary_[0]);
87}
88
89TEST_F(PrefixedBase64, Decode_OnlyPrefix_WritesNothing) {
90 EXPECT_EQ(0u, PrefixedBase64Decode("$", binary_));
91 EXPECT_EQ(std::byte{0}, binary_[0]);
92}
93
94TEST_F(PrefixedBase64, Decode_EmptyOutput_WritesNothing) {
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070095 EXPECT_EQ(0u,
96 PrefixedBase64Decode(kTestData[5].base64, std::span(binary_, 0)));
Wyatt Hepler3c2e9522020-01-09 16:08:54 -080097 EXPECT_EQ(std::byte{0}, binary_[0]);
98}
99
100TEST_F(PrefixedBase64, Decode_OutputTooSmall_WritesNothing) {
101 auto& item = kTestData[5];
Wyatt Heplere2cbadf2020-06-22 11:21:45 -0700102 EXPECT_EQ(0u,
103 PrefixedBase64Decode(item.base64,
104 std::span(binary_, item.binary.size() - 1)));
Wyatt Hepler3c2e9522020-01-09 16:08:54 -0800105 EXPECT_EQ(std::byte{0}, binary_[0]);
106}
107
108TEST(PrefixedBase64, DecodeInPlace) {
109 std::byte buffer[32];
110
Wyatt Heplera46bf7d2020-01-08 18:10:25 -0800111 for (auto& [binary, base64] : kTestData) {
112 std::memcpy(buffer, base64.data(), base64.size());
Wyatt Hepler3c2e9522020-01-09 16:08:54 -0800113
Wyatt Heplera46bf7d2020-01-08 18:10:25 -0800114 EXPECT_EQ(binary.size(),
Wyatt Heplere2cbadf2020-06-22 11:21:45 -0700115 PrefixedBase64DecodeInPlace(std::span(buffer, base64.size())));
Wyatt Heplera46bf7d2020-01-08 18:10:25 -0800116 ASSERT_EQ(0, std::memcmp(binary.data(), buffer, binary.size()));
Wyatt Hepler3c2e9522020-01-09 16:08:54 -0800117 }
118}
119
120} // namespace
121} // namespace pw::tokenizer