Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / der / input_unittest.cc
blob12f8c86a7054245c0f49a378b606b920f209600e
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"
9 namespace net {
10 namespace der {
11 namespace test {
13 const uint8_t kInput[] = {'t', 'e', 's', 't'};
15 TEST(InputTest, Equals) {
16 Input test(kInput);
17 Input test2(kInput);
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) {
31 Input input(kInput);
32 EXPECT_EQ(arraysize(kInput), input.Length());
34 Input input2(kInput);
35 EXPECT_TRUE(input.Equals(input2));
38 TEST(ByteReaderTest, NoReadPastEnd) {
39 ByteReader reader(Input(nullptr, 0));
40 uint8_t data;
41 EXPECT_FALSE(reader.ReadByte(&data));
44 TEST(ByteReaderTest, ReadToEnd) {
45 uint8_t out;
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) {
55 Input out;
56 ByteReader reader((Input(kInput)));
57 EXPECT_FALSE(reader.ReadBytes(5, &out));
60 TEST(ByteReaderTest, HasMore) {
61 Input out;
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) {
70 uint8_t out;
71 Input input(kInput);
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);
81 Input marked_data;
82 ASSERT_TRUE(reader.ReadToMark(mark, &marked_data));
85 TEST(ByteReaderTest, CantReadToWrongMark) {
86 Input out;
87 Input in1(kInput);
89 const uint8_t in2_bytes[] = {'t', 'e', 's', 't'};
90 Input in2(in2_bytes);
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) {
106 Input out;
107 Input in1(kInput);
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";
131 Input out;
132 Input in1(data1);
133 Input in2(data2);
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));
148 } // namespace test
149 } // namespace der
150 } // namespace net