Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "util/base64.h" |
| 6 | |
Jordan Bayles | 6f3efdb | 2021-04-06 09:43:46 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | namespace openscreen { |
| 13 | namespace base64 { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | constexpr char kText[] = "hello world"; |
| 18 | constexpr char kBase64Text[] = "aGVsbG8gd29ybGQ="; |
| 19 | |
Jordan Bayles | 34fd664 | 2021-04-06 21:37:26 -0700 | [diff] [blame] | 20 | // More sophisticated comparisons here, such as EXPECT_STREQ, may |
| 21 | // cause memory failures on some platforms (e.g. ASAN) due to mismatched |
| 22 | // lengths. |
| 23 | void CheckEquals(const char* expected, const std::vector<uint8_t>& actual) { |
| 24 | EXPECT_EQ(0, std::memcmp(actual.data(), expected, actual.size())); |
| 25 | } |
| 26 | |
Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 27 | void CheckEncodeDecode(const char* to_encode, const char* encode_expected) { |
| 28 | std::string encoded = Encode(to_encode); |
| 29 | EXPECT_EQ(encode_expected, encoded); |
| 30 | |
Jordan Bayles | 6f3efdb | 2021-04-06 09:43:46 -0700 | [diff] [blame] | 31 | std::vector<uint8_t> decoded; |
Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 32 | EXPECT_TRUE(Decode(encoded, &decoded)); |
Jordan Bayles | 7e0a4ca | 2021-04-06 15:42:11 -0700 | [diff] [blame] | 33 | |
Jordan Bayles | 34fd664 | 2021-04-06 21:37:26 -0700 | [diff] [blame] | 34 | CheckEquals(to_encode, decoded); |
Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | TEST(Base64Test, ZeroSize) { |
| 40 | CheckEncodeDecode("", ""); |
| 41 | } |
| 42 | |
| 43 | TEST(Base64Test, Basic) { |
| 44 | CheckEncodeDecode(kText, kBase64Text); |
| 45 | } |
| 46 | |
| 47 | TEST(Base64Test, Binary) { |
| 48 | const uint8_t kData[] = {0x00, 0x01, 0xFE, 0xFF}; |
| 49 | |
| 50 | std::string binary_encoded = Encode(absl::MakeConstSpan(kData)); |
| 51 | |
| 52 | // Check that encoding the same data through the StringPiece interface gives |
| 53 | // the same results. |
| 54 | std::string string_piece_encoded = Encode( |
| 55 | absl::string_view(reinterpret_cast<const char*>(kData), sizeof(kData))); |
| 56 | |
| 57 | EXPECT_EQ(binary_encoded, string_piece_encoded); |
| 58 | } |
| 59 | |
| 60 | TEST(Base64Test, InPlace) { |
| 61 | std::string text(kText); |
| 62 | |
| 63 | text = Encode(text); |
| 64 | EXPECT_EQ(kBase64Text, text); |
| 65 | |
Jordan Bayles | 6f3efdb | 2021-04-06 09:43:46 -0700 | [diff] [blame] | 66 | std::vector<uint8_t> out; |
| 67 | EXPECT_TRUE(Decode(text, &out)); |
Jordan Bayles | 34fd664 | 2021-04-06 21:37:26 -0700 | [diff] [blame] | 68 | CheckEquals(kText, out); |
Jordan Bayles | 79c6ea2 | 2020-12-10 11:12:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace base64 |
| 72 | } // namespace openscreen |