[Android] Add base_junit_tests to VALID_TESTS in bb_device_steps.
[chromium-blink-merge.git] / net / der / input_unittest.cc
blobe8145b54dbd85ab1f667527ebba3ee2073b9510a
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, arraysize(kInput));
17 Input test2(kInput, arraysize(kInput));
18 EXPECT_TRUE(test.Equals(test2));
20 std::string input_copy(reinterpret_cast<const char*>(kInput),
21 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(ByteReaderTest, NoReadPastEnd) {
31 ByteReader reader(Input(nullptr, 0));
32 uint8_t data;
33 EXPECT_FALSE(reader.ReadByte(&data));
36 TEST(ByteReaderTest, ReadToEnd) {
37 uint8_t out;
38 ByteReader reader(Input(kInput, arraysize(kInput)));
39 for (size_t i = 0; i < arraysize(kInput); ++i) {
40 ASSERT_TRUE(reader.ReadByte(&out));
41 ASSERT_EQ(kInput[i], out);
43 EXPECT_FALSE(reader.ReadByte(&out));
46 TEST(ByteReaderTest, PartialReadFails) {
47 Input out;
48 ByteReader reader(Input(kInput, arraysize(kInput)));
49 EXPECT_FALSE(reader.ReadBytes(5, &out));
52 TEST(ByteReaderTest, HasMore) {
53 Input out;
54 ByteReader reader(Input(kInput, arraysize(kInput)));
56 ASSERT_TRUE(reader.HasMore());
57 ASSERT_TRUE(reader.ReadBytes(arraysize(kInput), &out));
58 ASSERT_FALSE(reader.HasMore());
61 TEST(ByteReaderTest, ReadToMark) {
62 uint8_t out;
63 Input input(kInput, arraysize(kInput));
64 ByteReader reader(input);
66 // Read 2 bytes from the reader and then set a mark.
67 ASSERT_TRUE(reader.ReadByte(&out));
68 ASSERT_TRUE(reader.ReadByte(&out));
69 Mark mark = reader.NewMark();
71 // Reset the reader and check that we can read to a mark previously set.
72 reader = ByteReader(input);
73 Input marked_data;
74 ASSERT_TRUE(reader.ReadToMark(mark, &marked_data));
77 TEST(ByteReaderTest, CantReadToWrongMark) {
78 Input out;
79 Input in1(kInput, arraysize(kInput));
80 Input in2("test");
81 ByteReader reader1(in1);
82 ByteReader reader2(in2);
83 ASSERT_TRUE(reader1.ReadBytes(2, &out));
84 ASSERT_TRUE(reader2.ReadBytes(2, &out));
85 Mark mark1 = reader1.NewMark();
86 Mark mark2 = reader2.NewMark();
87 reader1 = ByteReader(in1);
88 reader2 = ByteReader(in2);
90 // It is not possible to advance to a mark outside the underlying input.
91 ASSERT_FALSE(reader1.AdvanceToMark(mark2));
92 ASSERT_FALSE(reader2.AdvanceToMark(mark1));
95 TEST(ByteReaderTest, MarksAreSharedBetweenSameInputs) {
96 Input out;
97 Input in1(kInput, arraysize(kInput));
98 Input in2(kInput, 1);
99 ByteReader reader1(in1);
100 ByteReader reader2(in2);
101 ASSERT_TRUE(reader1.ReadBytes(2, &out));
102 ASSERT_TRUE(reader2.ReadBytes(1, &out));
103 Mark mark1 = reader1.NewMark();
104 Mark mark2 = reader2.NewMark();
105 reader1 = ByteReader(in1);
106 reader2 = ByteReader(in2);
108 // If Marks are created on the same underlying data, they can be shared
109 // across ByteReaders and Inputs. However, they still must be inside the
110 // bounds for the ByteReader they are being used on.
112 // mark1 is past the end of the input for reader2.
113 EXPECT_FALSE(reader2.AdvanceToMark(mark1));
114 // mark2 is within the bounds of reader1.
115 EXPECT_TRUE(reader1.AdvanceToMark(mark2));
118 TEST(ByteReaderTest, CantReadToWrongMarkWithInputsOnStack) {
119 const uint8_t data1[] = "test";
120 const uint8_t data2[] = "foo";
121 Input out;
122 Input in1(data1, arraysize(data1));
123 Input in2(data2, arraysize(data2));
125 ByteReader reader1(in1);
126 ByteReader reader2(in2);
127 ASSERT_TRUE(reader1.ReadBytes(2, &out));
128 ASSERT_TRUE(reader2.ReadBytes(2, &out));
129 Mark mark1 = reader1.NewMark();
130 Mark mark2 = reader2.NewMark();
131 reader1 = ByteReader(in1);
132 reader2 = ByteReader(in2);
134 ASSERT_FALSE(reader1.AdvanceToMark(mark2));
135 ASSERT_FALSE(reader2.AdvanceToMark(mark1));
138 } // namespace test
139 } // namespace der
140 } // namespace net