1 // Copyright 2015 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.
5 #include "base/logging.h"
6 #include "net/der/input.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 const uint8_t kInput
[] = {'t', 'e', 's', 't'};
15 TEST(InputTest
, Equals
) {
18 EXPECT_TRUE(test
.Equals(test2
));
20 uint8_t input_copy
[arraysize(kInput
)] = {0};
21 memcpy(input_copy
, kInput
, arraysize(kInput
));
22 Input
test_copy(input_copy
);
23 EXPECT_TRUE(test
.Equals(test_copy
));
25 Input
test_truncated(kInput
, arraysize(kInput
) - 1);
26 EXPECT_FALSE(test
.Equals(test_truncated
));
27 EXPECT_FALSE(test_truncated
.Equals(test
));
30 TEST(InputTest
, AsString
) {
32 std::string
expected_string(reinterpret_cast<const char*>(kInput
),
34 EXPECT_EQ(expected_string
, input
.AsString());
37 TEST(InputTest
, StaticArray
) {
39 EXPECT_EQ(arraysize(kInput
), input
.Length());
42 EXPECT_TRUE(input
.Equals(input2
));
45 TEST(ByteReaderTest
, NoReadPastEnd
) {
46 ByteReader
reader(Input(nullptr, 0));
48 EXPECT_FALSE(reader
.ReadByte(&data
));
51 TEST(ByteReaderTest
, ReadToEnd
) {
53 ByteReader
reader((Input(kInput
)));
54 for (size_t i
= 0; i
< arraysize(kInput
); ++i
) {
55 ASSERT_TRUE(reader
.ReadByte(&out
));
56 ASSERT_EQ(kInput
[i
], out
);
58 EXPECT_FALSE(reader
.ReadByte(&out
));
61 TEST(ByteReaderTest
, PartialReadFails
) {
63 ByteReader
reader((Input(kInput
)));
64 EXPECT_FALSE(reader
.ReadBytes(5, &out
));
67 TEST(ByteReaderTest
, HasMore
) {
69 ByteReader
reader((Input(kInput
)));
71 ASSERT_TRUE(reader
.HasMore());
72 ASSERT_TRUE(reader
.ReadBytes(arraysize(kInput
), &out
));
73 ASSERT_FALSE(reader
.HasMore());
76 TEST(ByteReaderTest
, ReadToMark
) {
79 ByteReader
reader(input
);
81 // Read 2 bytes from the reader and then set a mark.
82 ASSERT_TRUE(reader
.ReadByte(&out
));
83 ASSERT_TRUE(reader
.ReadByte(&out
));
84 Mark mark
= reader
.NewMark();
86 // Reset the reader and check that we can read to a mark previously set.
87 reader
= ByteReader(input
);
89 ASSERT_TRUE(reader
.ReadToMark(mark
, &marked_data
));
92 TEST(ByteReaderTest
, CantReadToWrongMark
) {
96 const uint8_t in2_bytes
[] = {'t', 'e', 's', 't'};
98 ByteReader
reader1(in1
);
99 ByteReader
reader2(in2
);
100 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
101 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
102 Mark mark1
= reader1
.NewMark();
103 Mark mark2
= reader2
.NewMark();
104 reader1
= ByteReader(in1
);
105 reader2
= ByteReader(in2
);
107 // It is not possible to advance to a mark outside the underlying input.
108 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
109 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));
112 TEST(ByteReaderTest
, MarksAreSharedBetweenSameInputs
) {
115 Input
in2(kInput
, 1);
116 ByteReader
reader1(in1
);
117 ByteReader
reader2(in2
);
118 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
119 ASSERT_TRUE(reader2
.ReadBytes(1, &out
));
120 Mark mark1
= reader1
.NewMark();
121 Mark mark2
= reader2
.NewMark();
122 reader1
= ByteReader(in1
);
123 reader2
= ByteReader(in2
);
125 // If Marks are created on the same underlying data, they can be shared
126 // across ByteReaders and Inputs. However, they still must be inside the
127 // bounds for the ByteReader they are being used on.
129 // mark1 is past the end of the input for reader2.
130 EXPECT_FALSE(reader2
.AdvanceToMark(mark1
));
131 // mark2 is within the bounds of reader1.
132 EXPECT_TRUE(reader1
.AdvanceToMark(mark2
));
135 TEST(ByteReaderTest
, CantReadToWrongMarkWithInputsOnStack
) {
136 const uint8_t data1
[] = "test";
137 const uint8_t data2
[] = "foo";
142 ByteReader
reader1(in1
);
143 ByteReader
reader2(in2
);
144 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
145 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
146 Mark mark1
= reader1
.NewMark();
147 Mark mark2
= reader2
.NewMark();
148 reader1
= ByteReader(in1
);
149 reader2
= ByteReader(in2
);
151 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
152 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));