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