Update navigator.services API to use the new services.onconnect event [2/3].
[chromium-blink-merge.git] / net / der / parse_values_unittest.cc
blobea1b5c4e8cca4e28bd85f533fe54b39f05577e50
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 <stdint.h>
7 #include "base/macros.h"
8 #include "net/der/parse_values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace net {
12 namespace der {
13 namespace test {
15 namespace {
17 template <size_t N>
18 Input FromStringLiteral(const char(&data)[N]) {
19 // Strings are null-terminated. The null terminating byte shouldn't be
20 // included in the Input, so the size is N - 1 instead of N.
21 return Input(reinterpret_cast<const uint8_t*>(data), N - 1);
24 } // namespace
26 TEST(ParseValuesTest, ParseBool) {
27 uint8_t buf[] = {0xFF, 0x00};
28 Input value(buf, 1);
29 bool out;
30 EXPECT_TRUE(ParseBool(value, &out));
31 EXPECT_TRUE(out);
33 buf[0] = 0;
34 EXPECT_TRUE(ParseBool(value, &out));
35 EXPECT_FALSE(out);
37 buf[0] = 1;
38 EXPECT_FALSE(ParseBool(value, &out));
39 EXPECT_TRUE(ParseBoolRelaxed(value, &out));
40 EXPECT_TRUE(out);
42 buf[0] = 0xFF;
43 value = Input(buf, 2);
44 EXPECT_FALSE(ParseBool(value, &out));
45 value = Input(buf, 0);
46 EXPECT_FALSE(ParseBool(value, &out));
49 TEST(ParseValuesTest, ParseTimes) {
50 GeneralizedTime out;
52 EXPECT_TRUE(ParseUTCTime(FromStringLiteral("140218161200Z"), &out));
54 // DER-encoded UTCTime must end with 'Z'.
55 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200X"), &out));
57 // Check that a negative number (-4 in this case) doesn't get parsed as
58 // a 2-digit number.
59 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("-40218161200Z"), &out));
61 // Check that numbers with a leading 0 don't get parsed in octal by making
62 // the second digit an invalid octal digit (e.g. 09).
63 EXPECT_TRUE(ParseUTCTime(FromStringLiteral("090218161200Z"), &out));
65 // Check that the length is validated.
66 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200"), &out));
67 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200Z0"), &out));
68 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("140218161200"), &out));
69 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("140218161200Z0"), &out));
71 // Check strictness of UTCTime parsers.
72 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("1402181612Z"), &out));
73 EXPECT_TRUE(ParseUTCTimeRelaxed(FromStringLiteral("1402181612Z"), &out));
75 // Check that the time ends in Z.
76 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("1402181612Z0"), &out));
78 // Check format of GeneralizedTime.
80 // Leap seconds are allowed.
81 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140218161260Z"), &out));
83 // But nothing larger than a leap second.
84 EXPECT_FALSE(
85 ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out));
87 // Minutes only go up to 59.
88 EXPECT_FALSE(
89 ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out));
91 // Hours only go up to 23.
92 EXPECT_FALSE(
93 ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out));
94 // The 0th day of a month is invalid.
95 EXPECT_FALSE(
96 ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out));
97 // The 0th month is invalid.
98 EXPECT_FALSE(
99 ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out));
100 // Months greater than 12 are invalid.
101 EXPECT_FALSE(
102 ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out));
104 // Some months have 31 days.
105 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out));
107 // September has only 30 days.
108 EXPECT_FALSE(
109 ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out));
111 // February has only 28 days...
112 EXPECT_FALSE(
113 ParseGeneralizedTime(FromStringLiteral("20140229000000Z"), &out));
115 // ... unless it's a leap year.
116 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20160229000000Z"), &out));
118 // There aren't any leap days in years divisible by 100...
119 EXPECT_FALSE(
120 ParseGeneralizedTime(FromStringLiteral("21000229000000Z"), &out));
122 // ...unless it's also divisible by 400.
123 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20000229000000Z"), &out));
125 // Check more perverse invalid inputs.
127 // Check that trailing null bytes are not ignored.
128 EXPECT_FALSE(
129 ParseGeneralizedTime(FromStringLiteral("20001231010203Z\0"), &out));
131 // Check what happens when a null byte is in the middle of the input.
132 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral(
133 "200\0"
134 "1231010203Z"),
135 &out));
137 // The year can't be in hex.
138 EXPECT_FALSE(
139 ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out));
141 // The last byte must be 'Z'.
142 EXPECT_FALSE(
143 ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out));
145 // Check that the length is validated.
146 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out));
147 EXPECT_FALSE(
148 ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out));
151 TEST(ParseValuesTest, TimesCompare) {
152 GeneralizedTime time1;
153 GeneralizedTime time2;
154 GeneralizedTime time3;
156 ASSERT_TRUE(
157 ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time1));
158 ASSERT_TRUE(ParseUTCTime(FromStringLiteral("150218161200Z"), &time2));
159 ASSERT_TRUE(
160 ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time3));
161 EXPECT_TRUE(time1 < time2);
162 EXPECT_TRUE(time2 < time3);
163 EXPECT_TRUE(time1 < time3);
166 struct Uint64TestData {
167 bool should_pass;
168 const uint8_t input[9];
169 size_t length;
170 uint64_t expected_value;
173 const Uint64TestData kUint64TestData[] = {
174 {true, {0x00}, 1, 0},
175 {true, {0x01}, 1, 1},
176 {false, {0xFF}, 1, 0},
177 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
178 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0},
179 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0},
180 {false, {0x00, 0x01}, 2, 1},
181 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0},
182 {false, {0}, 0, 0},
185 TEST(ParseValuesTest, ParseUint64) {
186 for (size_t i = 0; i < arraysize(kUint64TestData); i++) {
187 Uint64TestData test_case = kUint64TestData[i];
188 SCOPED_TRACE(i);
190 uint64_t result;
191 EXPECT_EQ(test_case.should_pass,
192 ParseUint64(Input(test_case.input, test_case.length), &result));
193 if (test_case.should_pass)
194 EXPECT_EQ(test_case.expected_value, result);
198 } // namespace test
199 } // namespace der
200 } // namespace net