Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 1 | // 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 Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 18 | #include <span> |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 19 | #include <string_view> |
| 20 | |
| 21 | #include "gtest/gtest.h" |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 22 | |
| 23 | namespace pw::tokenizer { |
| 24 | namespace { |
| 25 | |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 26 | using std::byte; |
| 27 | |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 28 | class PrefixedBase64 : public ::testing::Test { |
| 29 | protected: |
| 30 | PrefixedBase64() : binary_{}, base64_{} {} |
| 31 | |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 32 | byte binary_[32]; |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 33 | char base64_[32]; |
| 34 | }; |
| 35 | |
| 36 | const struct TestData { |
| 37 | template <size_t kSize> |
Armando Montanez | 888370d | 2020-05-01 18:29:22 -0700 | [diff] [blame] | 38 | TestData(const char (&binary_data)[kSize], const char* base64_data) |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 39 | : binary{std::as_bytes(std::span(binary_data, kSize - 1))}, |
| 40 | base64(base64_data) {} |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 41 | |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 42 | std::span<const byte> binary; |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 43 | std::string_view base64; |
| 44 | } kTestData[] = { |
| 45 | {"", "$"}, |
| 46 | {"\x00", "$AA=="}, |
| 47 | {"\x71", "$cQ=="}, |
| 48 | {"\xff", "$/w=="}, |
| 49 | {"\x63\xa9", "$Y6k="}, |
| 50 | {"\x69\x89\x03", "$aYkD"}, |
| 51 | {"\x80\xf5\xc8\xd4", "$gPXI1A=="}, |
| 52 | {"\x6e\xb8\x91\x3f\xac", "$briRP6w="}, |
| 53 | {"\x1f\x88\x91\xbb\xd7\x10", "$H4iRu9cQ"}, |
| 54 | {"\xac\xcf\xb2\xd5\xee\xa2\x8e", "$rM+y1e6ijg=="}, |
| 55 | {"\xff\x15\x25\x7e\x7b\xc9\x7b\x60", "$/xUlfnvJe2A="}, |
| 56 | {"\xd5\xab\xd9\xa6\xae\xaa\x33\x9f\x66", "$1avZpq6qM59m"}, |
| 57 | {"\x6b\xfd\x95\xc5\x4a\xc7\xc2\x39\x45\xdc", "$a/2VxUrHwjlF3A=="}, |
| 58 | {"\x4c\xde\xee\xb8\x68\x0d\x9c\x66\x3e\xea\x46", "$TN7uuGgNnGY+6kY="}, |
| 59 | }; |
| 60 | |
| 61 | TEST_F(PrefixedBase64, Encode) { |
Wyatt Hepler | a46bf7d | 2020-01-08 18:10:25 -0800 | [diff] [blame] | 62 | for (auto& [binary, base64] : kTestData) { |
| 63 | EXPECT_EQ(base64.size(), PrefixedBase64Encode(binary, base64_)); |
| 64 | ASSERT_EQ(base64, base64_); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | TEST_F(PrefixedBase64, Encode_EmptyInput_WritesPrefix) { |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 69 | EXPECT_EQ(1u, PrefixedBase64Encode(std::span<byte>(), base64_)); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 70 | EXPECT_EQ('$', base64_[0]); |
| 71 | } |
| 72 | |
| 73 | TEST_F(PrefixedBase64, Encode_EmptyOutput_WritesNothing) { |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 74 | EXPECT_EQ(0u, |
| 75 | PrefixedBase64Encode(kTestData[5].binary, std::span(base64_, 0))); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 76 | EXPECT_EQ('\0', base64_[0]); |
| 77 | } |
| 78 | |
| 79 | TEST_F(PrefixedBase64, Decode) { |
Wyatt Hepler | a46bf7d | 2020-01-08 18:10:25 -0800 | [diff] [blame] | 80 | for (auto& [binary, base64] : kTestData) { |
| 81 | EXPECT_EQ(binary.size(), PrefixedBase64Decode(base64, binary_)); |
| 82 | ASSERT_EQ(0, std::memcmp(binary.data(), binary_, binary.size())); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_F(PrefixedBase64, Decode_EmptyInput_WritesNothing) { |
| 87 | EXPECT_EQ(0u, PrefixedBase64Decode({}, binary_)); |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 88 | EXPECT_EQ(byte{0}, binary_[0]); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST_F(PrefixedBase64, Decode_OnlyPrefix_WritesNothing) { |
| 92 | EXPECT_EQ(0u, PrefixedBase64Decode("$", binary_)); |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 93 | EXPECT_EQ(byte{0}, binary_[0]); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST_F(PrefixedBase64, Decode_EmptyOutput_WritesNothing) { |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 97 | EXPECT_EQ(0u, |
| 98 | PrefixedBase64Decode(kTestData[5].base64, std::span(binary_, 0))); |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 99 | EXPECT_EQ(byte{0}, binary_[0]); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(PrefixedBase64, Decode_OutputTooSmall_WritesNothing) { |
| 103 | auto& item = kTestData[5]; |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 104 | EXPECT_EQ(0u, |
| 105 | PrefixedBase64Decode(item.base64, |
| 106 | std::span(binary_, item.binary.size() - 1))); |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 107 | EXPECT_EQ(byte{0}, binary_[0]); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST(PrefixedBase64, DecodeInPlace) { |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame^] | 111 | byte buffer[32]; |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 112 | |
Wyatt Hepler | a46bf7d | 2020-01-08 18:10:25 -0800 | [diff] [blame] | 113 | for (auto& [binary, base64] : kTestData) { |
| 114 | std::memcpy(buffer, base64.data(), base64.size()); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 115 | |
Wyatt Hepler | a46bf7d | 2020-01-08 18:10:25 -0800 | [diff] [blame] | 116 | EXPECT_EQ(binary.size(), |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 117 | PrefixedBase64DecodeInPlace(std::span(buffer, base64.size()))); |
Wyatt Hepler | a46bf7d | 2020-01-08 18:10:25 -0800 | [diff] [blame] | 118 | ASSERT_EQ(0, std::memcmp(binary.data(), buffer, binary.size())); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | } // namespace |
| 123 | } // namespace pw::tokenizer |