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
, StaticArray
) {
32 EXPECT_EQ(arraysize(kInput
), input
.Length());
35 EXPECT_TRUE(input
.Equals(input2
));
38 TEST(ByteReaderTest
, NoReadPastEnd
) {
39 ByteReader
reader(Input(nullptr, 0));
41 EXPECT_FALSE(reader
.ReadByte(&data
));
44 TEST(ByteReaderTest
, ReadToEnd
) {
46 ByteReader
reader((Input(kInput
)));
47 for (size_t i
= 0; i
< arraysize(kInput
); ++i
) {
48 ASSERT_TRUE(reader
.ReadByte(&out
));
49 ASSERT_EQ(kInput
[i
], out
);
51 EXPECT_FALSE(reader
.ReadByte(&out
));
54 TEST(ByteReaderTest
, PartialReadFails
) {
56 ByteReader
reader((Input(kInput
)));
57 EXPECT_FALSE(reader
.ReadBytes(5, &out
));
60 TEST(ByteReaderTest
, HasMore
) {
62 ByteReader
reader((Input(kInput
)));
64 ASSERT_TRUE(reader
.HasMore());
65 ASSERT_TRUE(reader
.ReadBytes(arraysize(kInput
), &out
));
66 ASSERT_FALSE(reader
.HasMore());
69 TEST(ByteReaderTest
, ReadToMark
) {
72 ByteReader
reader(input
);
74 // Read 2 bytes from the reader and then set a mark.
75 ASSERT_TRUE(reader
.ReadByte(&out
));
76 ASSERT_TRUE(reader
.ReadByte(&out
));
77 Mark mark
= reader
.NewMark();
79 // Reset the reader and check that we can read to a mark previously set.
80 reader
= ByteReader(input
);
82 ASSERT_TRUE(reader
.ReadToMark(mark
, &marked_data
));
85 TEST(ByteReaderTest
, CantReadToWrongMark
) {
89 const uint8_t in2_bytes
[] = {'t', 'e', 's', 't'};
91 ByteReader
reader1(in1
);
92 ByteReader
reader2(in2
);
93 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
94 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
95 Mark mark1
= reader1
.NewMark();
96 Mark mark2
= reader2
.NewMark();
97 reader1
= ByteReader(in1
);
98 reader2
= ByteReader(in2
);
100 // It is not possible to advance to a mark outside the underlying input.
101 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
102 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));
105 TEST(ByteReaderTest
, MarksAreSharedBetweenSameInputs
) {
108 Input
in2(kInput
, 1);
109 ByteReader
reader1(in1
);
110 ByteReader
reader2(in2
);
111 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
112 ASSERT_TRUE(reader2
.ReadBytes(1, &out
));
113 Mark mark1
= reader1
.NewMark();
114 Mark mark2
= reader2
.NewMark();
115 reader1
= ByteReader(in1
);
116 reader2
= ByteReader(in2
);
118 // If Marks are created on the same underlying data, they can be shared
119 // across ByteReaders and Inputs. However, they still must be inside the
120 // bounds for the ByteReader they are being used on.
122 // mark1 is past the end of the input for reader2.
123 EXPECT_FALSE(reader2
.AdvanceToMark(mark1
));
124 // mark2 is within the bounds of reader1.
125 EXPECT_TRUE(reader1
.AdvanceToMark(mark2
));
128 TEST(ByteReaderTest
, CantReadToWrongMarkWithInputsOnStack
) {
129 const uint8_t data1
[] = "test";
130 const uint8_t data2
[] = "foo";
135 ByteReader
reader1(in1
);
136 ByteReader
reader2(in2
);
137 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
138 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
139 Mark mark1
= reader1
.NewMark();
140 Mark mark2
= reader2
.NewMark();
141 reader1
= ByteReader(in1
);
142 reader2
= ByteReader(in2
);
144 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
145 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));