1 // Copyright 2015 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "input.h"
16 
17 #include <gtest/gtest.h>
18 
19 BSSL_NAMESPACE_BEGIN
20 namespace der::test {
21 
22 constexpr uint8_t kInput[] = {'t', 'e', 's', 't'};
23 const uint8_t kInput2[] = {'t', 'e', 'a', 'l'};
24 
TEST(InputTest,Equals)25 TEST(InputTest, Equals) {
26   Input test(kInput);
27   Input test2(kInput);
28   EXPECT_EQ(test, test2);
29 
30   uint8_t input_copy[std::size(kInput)] = {0};
31   memcpy(input_copy, kInput, std::size(kInput));
32   Input test_copy(input_copy);
33   EXPECT_EQ(test, test_copy);
34 
35   Input test_truncated(kInput, std::size(kInput) - 1);
36   EXPECT_NE(test, test_truncated);
37   EXPECT_NE(test_truncated, test);
38 }
39 
TEST(InputTest,LessThan)40 TEST(InputTest, LessThan) {
41   Input test(kInput);
42   EXPECT_FALSE(test < test);
43 
44   Input test2(kInput2);
45   EXPECT_FALSE(test < test2);
46   EXPECT_TRUE(test2 < test);
47 
48   Input test_truncated(kInput, std::size(kInput) - 1);
49   EXPECT_FALSE(test < test_truncated);
50   EXPECT_TRUE(test_truncated < test);
51 }
52 
TEST(InputTest,AsString)53 TEST(InputTest, AsString) {
54   Input input(kInput);
55   EXPECT_EQ(bssl::BytesAsStringView(kInput), input.AsString());
56 }
57 
TEST(InputTest,StaticArray)58 TEST(InputTest, StaticArray) {
59   Input input(kInput);
60   EXPECT_EQ(std::size(kInput), input.size());
61 
62   Input input2(kInput);
63   EXPECT_EQ(input, input2);
64 }
65 
TEST(InputTest,ConstExpr)66 TEST(InputTest, ConstExpr) {
67   constexpr Input default_input;
68   static_assert(default_input.size() == 0);
69   static_assert(default_input.data() == nullptr);
70 
71   constexpr Input const_array_input(kInput);
72   static_assert(const_array_input.size() == 4);
73   static_assert(const_array_input.data() == kInput);
74   static_assert(default_input < const_array_input);
75 
76   constexpr Input ptr_len_input(kInput, 2);
77   static_assert(ptr_len_input.size() == 2);
78   static_assert(ptr_len_input.data() == kInput);
79   static_assert(ptr_len_input < const_array_input);
80 
81   Input runtime_input(kInput2, 2);
82   EXPECT_EQ(runtime_input, ptr_len_input);
83 }
84 
TEST(ByteReaderTest,NoReadPastEnd)85 TEST(ByteReaderTest, NoReadPastEnd) {
86   ByteReader reader(Input(nullptr, 0));
87   uint8_t data;
88   EXPECT_FALSE(reader.ReadByte(&data));
89 }
90 
TEST(ByteReaderTest,ReadToEnd)91 TEST(ByteReaderTest, ReadToEnd) {
92   uint8_t out;
93   ByteReader reader((Input(kInput)));
94   for (uint8_t input : kInput) {
95     ASSERT_TRUE(reader.ReadByte(&out));
96     ASSERT_EQ(input, out);
97   }
98   EXPECT_FALSE(reader.ReadByte(&out));
99 }
100 
TEST(ByteReaderTest,PartialReadFails)101 TEST(ByteReaderTest, PartialReadFails) {
102   Input out;
103   ByteReader reader((Input(kInput)));
104   EXPECT_FALSE(reader.ReadBytes(5, &out));
105 }
106 
TEST(ByteReaderTest,HasMore)107 TEST(ByteReaderTest, HasMore) {
108   Input out;
109   ByteReader reader((Input(kInput)));
110 
111   ASSERT_TRUE(reader.HasMore());
112   ASSERT_TRUE(reader.ReadBytes(std::size(kInput), &out));
113   ASSERT_FALSE(reader.HasMore());
114 }
115 
116 }  // namespace der::test
117 BSSL_NAMESPACE_END
118