Implement Tier 2 of Cookie handling for Mirror on WKBling.
[chromium-blink-merge.git] / net / der / input_unittest.cc
blob63fe715c33e770fc736fb08c67c115646a20486f
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, AsString) {
31 Input input(kInput);
32 std::string expected_string(reinterpret_cast<const char*>(kInput),
33 arraysize(kInput));
34 EXPECT_EQ(expected_string, input.AsString());
37 TEST(InputTest, StaticArray) {
38 Input input(kInput);
39 EXPECT_EQ(arraysize(kInput), input.Length());
41 Input input2(kInput);
42 EXPECT_TRUE(input.Equals(input2));
45 TEST(ByteReaderTest, NoReadPastEnd) {
46 ByteReader reader(Input(nullptr, 0));
47 uint8_t data;
48 EXPECT_FALSE(reader.ReadByte(&data));
51 TEST(ByteReaderTest, ReadToEnd) {
52 uint8_t out;
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) {
62 Input out;
63 ByteReader reader((Input(kInput)));
64 EXPECT_FALSE(reader.ReadBytes(5, &out));
67 TEST(ByteReaderTest, HasMore) {
68 Input out;
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) {
77 uint8_t out;
78 Input input(kInput);
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);
88 Input marked_data;
89 ASSERT_TRUE(reader.ReadToMark(mark, &marked_data));
92 TEST(ByteReaderTest, CantReadToWrongMark) {
93 Input out;
94 Input in1(kInput);
96 const uint8_t in2_bytes[] = {'t', 'e', 's', 't'};
97 Input in2(in2_bytes);
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) {
113 Input out;
114 Input in1(kInput);
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";
138 Input out;
139 Input in1(data1);
140 Input in2(data2);
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));
155 } // namespace test
156 } // namespace der
157 } // namespace net