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