Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
| 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 "input.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace bssl::der::test { |
| 10 | |
| 11 | constexpr uint8_t kInput[] = {'t', 'e', 's', 't'}; |
| 12 | const uint8_t kInput2[] = {'t', 'e', 'a', 'l'}; |
| 13 | |
| 14 | TEST(InputTest, Equals) { |
| 15 | Input test(kInput); |
| 16 | Input test2(kInput); |
| 17 | EXPECT_EQ(test, test2); |
| 18 | |
| 19 | uint8_t input_copy[std::size(kInput)] = {0}; |
| 20 | memcpy(input_copy, kInput, std::size(kInput)); |
| 21 | Input test_copy(input_copy); |
| 22 | EXPECT_EQ(test, test_copy); |
| 23 | |
| 24 | Input test_truncated(kInput, std::size(kInput) - 1); |
| 25 | EXPECT_NE(test, test_truncated); |
| 26 | EXPECT_NE(test_truncated, test); |
| 27 | } |
| 28 | |
| 29 | TEST(InputTest, LessThan) { |
| 30 | Input test(kInput); |
| 31 | EXPECT_FALSE(test < test); |
| 32 | |
| 33 | Input test2(kInput2); |
| 34 | EXPECT_FALSE(test < test2); |
| 35 | EXPECT_TRUE(test2 < test); |
| 36 | |
| 37 | Input test_truncated(kInput, std::size(kInput) - 1); |
| 38 | EXPECT_FALSE(test < test_truncated); |
| 39 | EXPECT_TRUE(test_truncated < test); |
| 40 | } |
| 41 | |
| 42 | TEST(InputTest, AsString) { |
| 43 | Input input(kInput); |
| 44 | std::string expected_string(reinterpret_cast<const char*>(kInput), |
| 45 | std::size(kInput)); |
| 46 | EXPECT_EQ(expected_string, input.AsString()); |
| 47 | } |
| 48 | |
| 49 | TEST(InputTest, StaticArray) { |
| 50 | Input input(kInput); |
| 51 | EXPECT_EQ(std::size(kInput), input.Length()); |
| 52 | |
| 53 | Input input2(kInput); |
| 54 | EXPECT_EQ(input, input2); |
| 55 | } |
| 56 | |
| 57 | TEST(InputTest, ConstExpr) { |
| 58 | constexpr Input default_input; |
| 59 | static_assert(default_input.Length() == 0); |
| 60 | static_assert(default_input.UnsafeData() == nullptr); |
| 61 | |
| 62 | constexpr Input const_array_input(kInput); |
| 63 | static_assert(const_array_input.Length() == 4); |
| 64 | static_assert(const_array_input.UnsafeData() == kInput); |
| 65 | static_assert(default_input < const_array_input); |
| 66 | |
| 67 | constexpr Input ptr_len_input(kInput, 2); |
| 68 | static_assert(ptr_len_input.Length() == 2); |
| 69 | static_assert(ptr_len_input.UnsafeData() == kInput); |
| 70 | static_assert(ptr_len_input < const_array_input); |
| 71 | |
| 72 | Input runtime_input(kInput2, 2); |
| 73 | EXPECT_EQ(runtime_input, ptr_len_input); |
| 74 | } |
| 75 | |
| 76 | TEST(ByteReaderTest, NoReadPastEnd) { |
| 77 | ByteReader reader(Input(nullptr, 0)); |
| 78 | uint8_t data; |
| 79 | EXPECT_FALSE(reader.ReadByte(&data)); |
| 80 | } |
| 81 | |
| 82 | TEST(ByteReaderTest, ReadToEnd) { |
| 83 | uint8_t out; |
| 84 | ByteReader reader((Input(kInput))); |
| 85 | for (uint8_t input : kInput) { |
| 86 | ASSERT_TRUE(reader.ReadByte(&out)); |
| 87 | ASSERT_EQ(input, out); |
| 88 | } |
| 89 | EXPECT_FALSE(reader.ReadByte(&out)); |
| 90 | } |
| 91 | |
| 92 | TEST(ByteReaderTest, PartialReadFails) { |
| 93 | Input out; |
| 94 | ByteReader reader((Input(kInput))); |
| 95 | EXPECT_FALSE(reader.ReadBytes(5, &out)); |
| 96 | } |
| 97 | |
| 98 | TEST(ByteReaderTest, HasMore) { |
| 99 | Input out; |
| 100 | ByteReader reader((Input(kInput))); |
| 101 | |
| 102 | ASSERT_TRUE(reader.HasMore()); |
| 103 | ASSERT_TRUE(reader.ReadBytes(std::size(kInput), &out)); |
| 104 | ASSERT_FALSE(reader.HasMore()); |
| 105 | } |
| 106 | |
| 107 | } // namespace bssl::der::test |