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(ByteReaderTest
, NoReadPastEnd
) {
16 ByteReader
reader(Input(nullptr, 0));
18 EXPECT_FALSE(reader
.ReadByte(&data
));
21 TEST(ByteReaderTest
, ReadToEnd
) {
23 ByteReader
reader(Input(kInput
, arraysize(kInput
)));
24 for (size_t i
= 0; i
< arraysize(kInput
); ++i
) {
25 ASSERT_TRUE(reader
.ReadByte(&out
));
26 ASSERT_EQ(kInput
[i
], out
);
28 EXPECT_FALSE(reader
.ReadByte(&out
));
31 TEST(ByteReaderTest
, PartialReadFails
) {
33 ByteReader
reader(Input(kInput
, arraysize(kInput
)));
34 EXPECT_FALSE(reader
.ReadBytes(5, &out
));
37 TEST(ByteReaderTest
, HasMore
) {
39 ByteReader
reader(Input(kInput
, arraysize(kInput
)));
41 ASSERT_TRUE(reader
.HasMore());
42 ASSERT_TRUE(reader
.ReadBytes(arraysize(kInput
), &out
));
43 ASSERT_FALSE(reader
.HasMore());
46 TEST(ByteReaderTest
, ReadToMark
) {
48 Input
input(kInput
, arraysize(kInput
));
49 ByteReader
reader(input
);
51 // Read 2 bytes from the reader and then set a mark.
52 ASSERT_TRUE(reader
.ReadByte(&out
));
53 ASSERT_TRUE(reader
.ReadByte(&out
));
54 Mark mark
= reader
.NewMark();
56 // Reset the reader and check that we can read to a mark previously set.
57 reader
= ByteReader(input
);
59 ASSERT_TRUE(reader
.ReadToMark(mark
, &marked_data
));
62 TEST(ByteReaderTest
, CantReadToWrongMark
) {
64 Input
in1(kInput
, arraysize(kInput
));
66 ByteReader
reader1(in1
);
67 ByteReader
reader2(in2
);
68 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
69 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
70 Mark mark1
= reader1
.NewMark();
71 Mark mark2
= reader2
.NewMark();
72 reader1
= ByteReader(in1
);
73 reader2
= ByteReader(in2
);
75 // It is not possible to advance to a mark outside the underlying input.
76 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
77 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));
80 TEST(ByteReaderTest
, MarksAreSharedBetweenSameInputs
) {
82 Input
in1(kInput
, arraysize(kInput
));
84 ByteReader
reader1(in1
);
85 ByteReader
reader2(in2
);
86 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
87 ASSERT_TRUE(reader2
.ReadBytes(1, &out
));
88 Mark mark1
= reader1
.NewMark();
89 Mark mark2
= reader2
.NewMark();
90 reader1
= ByteReader(in1
);
91 reader2
= ByteReader(in2
);
93 // If Marks are created on the same underlying data, they can be shared
94 // across ByteReaders and Inputs. However, they still must be inside the
95 // bounds for the ByteReader they are being used on.
97 // mark1 is past the end of the input for reader2.
98 EXPECT_FALSE(reader2
.AdvanceToMark(mark1
));
99 // mark2 is within the bounds of reader1.
100 EXPECT_TRUE(reader1
.AdvanceToMark(mark2
));
103 TEST(ByteReaderTest
, CantReadToWrongMarkWithInputsOnStack
) {
104 const uint8_t data1
[] = "test";
105 const uint8_t data2
[] = "foo";
107 Input
in1(data1
, arraysize(data1
));
108 Input
in2(data2
, arraysize(data2
));
110 ByteReader
reader1(in1
);
111 ByteReader
reader2(in2
);
112 ASSERT_TRUE(reader1
.ReadBytes(2, &out
));
113 ASSERT_TRUE(reader2
.ReadBytes(2, &out
));
114 Mark mark1
= reader1
.NewMark();
115 Mark mark2
= reader2
.NewMark();
116 reader1
= ByteReader(in1
);
117 reader2
= ByteReader(in2
);
119 ASSERT_FALSE(reader1
.AdvanceToMark(mark2
));
120 ASSERT_FALSE(reader2
.AdvanceToMark(mark1
));