Update progress, update, and error icons in chrome://chrome to Material
[chromium-blink-merge.git] / net / der / parse_values_unittest.cc
blob8233042418db3e545c275673b59f170e6446ae4a
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 TEST(ParseValuesTest, ParseBool) {
16 uint8_t buf[] = {0xFF, 0x00};
17 Input value(buf, 1);
18 bool out;
19 EXPECT_TRUE(ParseBool(value, &out));
20 EXPECT_TRUE(out);
22 buf[0] = 0;
23 EXPECT_TRUE(ParseBool(value, &out));
24 EXPECT_FALSE(out);
26 buf[0] = 1;
27 EXPECT_FALSE(ParseBool(value, &out));
28 EXPECT_TRUE(ParseBoolRelaxed(value, &out));
29 EXPECT_TRUE(out);
31 buf[0] = 0xFF;
32 value = Input(buf, 2);
33 EXPECT_FALSE(ParseBool(value, &out));
34 value = Input(buf, 0);
35 EXPECT_FALSE(ParseBool(value, &out));
38 TEST(ParseValuesTest, ParseTimes) {
39 GeneralizedTime out;
41 EXPECT_TRUE(ParseUTCTime(Input("140218161200Z"), &out));
43 // DER-encoded UTCTime must end with 'Z'.
44 EXPECT_FALSE(ParseUTCTime(Input("140218161200X"), &out));
46 // Check that a negative number (-4 in this case) doesn't get parsed as
47 // a 2-digit number.
48 EXPECT_FALSE(ParseUTCTime(Input("-40218161200Z"), &out));
50 // Check that numbers with a leading 0 don't get parsed in octal by making
51 // the second digit an invalid octal digit (e.g. 09).
52 EXPECT_TRUE(ParseUTCTime(Input("090218161200Z"), &out));
54 // Check that the length is validated.
55 EXPECT_FALSE(ParseUTCTime(Input("140218161200"), &out));
56 EXPECT_FALSE(ParseUTCTime(Input("140218161200Z0"), &out));
57 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200"), &out));
58 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200Z0"), &out));
60 // Check strictness of UTCTime parsers.
61 EXPECT_FALSE(ParseUTCTime(Input("1402181612Z"), &out));
62 EXPECT_TRUE(ParseUTCTimeRelaxed(Input("1402181612Z"), &out));
64 // Check that the time ends in Z.
65 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("1402181612Z0"), &out));
67 // Check format of GeneralizedTime.
69 // Leap seconds are allowed.
70 EXPECT_TRUE(ParseGeneralizedTime(Input("20140218161260Z"), &out));
72 // But nothing larger than a leap second.
73 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161261Z"), &out));
75 // Minutes only go up to 59.
76 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218166000Z"), &out));
78 // Hours only go up to 23.
79 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218240000Z"), &out));
80 // The 0th day of a month is invalid.
81 EXPECT_FALSE(ParseGeneralizedTime(Input("20140200161200Z"), &out));
82 // The 0th month is invalid.
83 EXPECT_FALSE(ParseGeneralizedTime(Input("20140018161200Z"), &out));
84 // Months greater than 12 are invalid.
85 EXPECT_FALSE(ParseGeneralizedTime(Input("20141318161200Z"), &out));
87 // Some months have 31 days.
88 EXPECT_TRUE(ParseGeneralizedTime(Input("20140131000000Z"), &out));
90 // September has only 30 days.
91 EXPECT_FALSE(ParseGeneralizedTime(Input("20140931000000Z"), &out));
93 // February has only 28 days...
94 EXPECT_FALSE(ParseGeneralizedTime(Input("20140229000000Z"), &out));
96 // ... unless it's a leap year.
97 EXPECT_TRUE(ParseGeneralizedTime(Input("20160229000000Z"), &out));
99 // There aren't any leap days in years divisible by 100...
100 EXPECT_FALSE(ParseGeneralizedTime(Input("21000229000000Z"), &out));
102 // ...unless it's also divisible by 400.
103 EXPECT_TRUE(ParseGeneralizedTime(Input("20000229000000Z"), &out));
105 // Check more perverse invalid inputs.
107 const uint8_t trailing_null_bytes[] = {'2',
108 '0',
109 '0',
110 '0',
111 '1',
112 '2',
113 '3',
114 '1',
115 '0',
116 '1',
117 '0',
118 '2',
119 '0',
120 '3',
121 'Z',
122 '\0'};
123 Input trailing_null(trailing_null_bytes, sizeof(trailing_null_bytes));
124 EXPECT_FALSE(ParseGeneralizedTime(trailing_null, &out));
125 const uint8_t embedded_null_bytes[] = {'2',
126 '0',
127 '0',
128 '\0',
129 '1',
130 '2',
131 '3',
132 '1',
133 '0',
134 '1',
135 '0',
136 '2',
137 '0',
138 '3',
139 'Z'};
140 Input embedded_null(embedded_null_bytes, sizeof(embedded_null_bytes));
141 EXPECT_FALSE(ParseGeneralizedTime(embedded_null, &out));
143 // The year can't be in hex.
144 EXPECT_FALSE(ParseGeneralizedTime(Input("0x201231000000Z"), &out));
146 // The last byte must be 'Z'.
147 EXPECT_FALSE(ParseGeneralizedTime(Input("20001231000000X"), &out));
149 // Check that the length is validated.
150 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200"), &out));
151 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200Z0"), &out));
154 TEST(ParseValuesTest, TimesCompare) {
155 GeneralizedTime time1;
156 GeneralizedTime time2;
157 GeneralizedTime time3;
159 ASSERT_TRUE(ParseGeneralizedTime(Input("20140218161200Z"), &time1));
160 ASSERT_TRUE(ParseUTCTime(Input("150218161200Z"), &time2));
161 ASSERT_TRUE(ParseGeneralizedTime(Input("20160218161200Z"), &time3));
162 EXPECT_TRUE(time1 < time2);
163 EXPECT_TRUE(time2 < time3);
164 EXPECT_TRUE(time1 < time3);
167 struct Uint64TestData {
168 bool should_pass;
169 const uint8_t input[9];
170 size_t length;
171 uint64_t expected_value;
174 const Uint64TestData kUint64TestData[] = {
175 {true, {0x00}, 1, 0},
176 {true, {0x01}, 1, 1},
177 {false, {0xFF}, 1, 0},
178 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
179 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0},
180 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0},
181 {false, {0x00, 0x01}, 2, 1},
182 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0},
183 {false, {0}, 0, 0},
186 TEST(ParseValuesTest, ParseUint64) {
187 for (size_t i = 0; i < arraysize(kUint64TestData); i++) {
188 Uint64TestData test_case = kUint64TestData[i];
189 SCOPED_TRACE(i);
191 uint64_t result;
192 EXPECT_EQ(test_case.should_pass,
193 ParseUint64(Input(test_case.input, test_case.length), &result));
194 if (test_case.should_pass)
195 EXPECT_EQ(test_case.expected_value, result);
199 } // namespace test
200 } // namespace der
201 } // namespace net