Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / der / parse_values_unittest.cc
blobb76c4c99c8d9805bd6c2fcc079d71be00bae7160
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 that ParseUTCTimeRelaxed calls ValidateGeneralizedTime.
79 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("1402181662Z"), &out));
81 // Check format of GeneralizedTime.
83 // Years 0 and 9999 are allowed.
84 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("00000101000000Z"), &out));
85 EXPECT_EQ(0, out.year);
86 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("99991231235960Z"), &out));
87 EXPECT_EQ(9999, out.year);
89 // Leap seconds are allowed.
90 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140218161260Z"), &out));
92 // But nothing larger than a leap second.
93 EXPECT_FALSE(
94 ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out));
96 // Minutes only go up to 59.
97 EXPECT_FALSE(
98 ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out));
100 // Hours only go up to 23.
101 EXPECT_FALSE(
102 ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out));
103 // The 0th day of a month is invalid.
104 EXPECT_FALSE(
105 ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out));
106 // The 0th month is invalid.
107 EXPECT_FALSE(
108 ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out));
109 // Months greater than 12 are invalid.
110 EXPECT_FALSE(
111 ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out));
113 // Some months have 31 days.
114 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out));
116 // September has only 30 days.
117 EXPECT_FALSE(
118 ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out));
120 // February has only 28 days...
121 EXPECT_FALSE(
122 ParseGeneralizedTime(FromStringLiteral("20140229000000Z"), &out));
124 // ... unless it's a leap year.
125 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20160229000000Z"), &out));
127 // There aren't any leap days in years divisible by 100...
128 EXPECT_FALSE(
129 ParseGeneralizedTime(FromStringLiteral("21000229000000Z"), &out));
131 // ...unless it's also divisible by 400.
132 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20000229000000Z"), &out));
134 // Check more perverse invalid inputs.
136 // Check that trailing null bytes are not ignored.
137 EXPECT_FALSE(
138 ParseGeneralizedTime(FromStringLiteral("20001231010203Z\0"), &out));
140 // Check what happens when a null byte is in the middle of the input.
141 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral(
142 "200\0"
143 "1231010203Z"),
144 &out));
146 // The year can't be in hex.
147 EXPECT_FALSE(
148 ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out));
150 // The last byte must be 'Z'.
151 EXPECT_FALSE(
152 ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out));
154 // Check that the length is validated.
155 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out));
156 EXPECT_FALSE(
157 ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out));
160 TEST(ParseValuesTest, TimesCompare) {
161 GeneralizedTime time1;
162 GeneralizedTime time2;
163 GeneralizedTime time3;
164 GeneralizedTime time4;
166 ASSERT_TRUE(
167 ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time1));
168 // Test that ParseUTCTime correctly normalizes the year.
169 ASSERT_TRUE(ParseUTCTime(FromStringLiteral("150218161200Z"), &time2));
170 ASSERT_TRUE(ParseUTCTimeRelaxed(FromStringLiteral("1503070000Z"), &time3));
171 ASSERT_TRUE(
172 ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time4));
173 EXPECT_TRUE(time1 < time2);
174 EXPECT_TRUE(time2 < time3);
175 EXPECT_TRUE(time3 < time4);
178 struct Uint64TestData {
179 bool should_pass;
180 const uint8_t input[9];
181 size_t length;
182 uint64_t expected_value;
185 const Uint64TestData kUint64TestData[] = {
186 {true, {0x00}, 1, 0},
187 // This number fails because it is not a minimal representation.
188 {false, {0x00, 0x00}, 2},
189 {true, {0x01}, 1, 1},
190 {false, {0xFF}, 1},
191 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
192 {true,
193 {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
195 UINT64_MAX},
196 // This number fails because it is negative.
197 {false, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8},
198 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8},
199 {false, {0x00, 0x01}, 2},
200 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9},
201 {false, {0}, 0},
204 TEST(ParseValuesTest, ParseUint64) {
205 for (size_t i = 0; i < arraysize(kUint64TestData); i++) {
206 const Uint64TestData& test_case = kUint64TestData[i];
207 SCOPED_TRACE(i);
209 uint64_t result;
210 EXPECT_EQ(test_case.should_pass,
211 ParseUint64(Input(test_case.input, test_case.length), &result));
212 if (test_case.should_pass)
213 EXPECT_EQ(test_case.expected_value, result);
217 struct IsValidIntegerTestData {
218 bool should_pass;
219 const uint8_t input[2];
220 size_t length;
221 bool negative;
224 const IsValidIntegerTestData kIsValidIntegerTestData[] = {
225 // Empty input (invalid DER).
226 {false, {0x00}, 0},
228 // The correct encoding for zero.
229 {true, {0x00}, 1, false},
231 // Invalid representation of zero (not minimal)
232 {false, {0x00, 0x00}, 2},
234 // Valid single byte negative numbers.
235 {true, {0x80}, 1, true},
236 {true, {0xFF}, 1, true},
238 // Non-minimal negative number.
239 {false, {0xFF, 0x80}, 2},
241 // Positive number with a legitimate leading zero.
242 {true, {0x00, 0x80}, 2, false},
244 // A legitimate negative number that starts with FF (MSB of second byte is
245 // 0 so OK).
246 {true, {0xFF, 0x7F}, 2, true},
249 TEST(ParseValuesTest, IsValidInteger) {
250 for (size_t i = 0; i < arraysize(kIsValidIntegerTestData); i++) {
251 const auto& test_case = kIsValidIntegerTestData[i];
252 SCOPED_TRACE(i);
254 bool negative;
255 EXPECT_EQ(
256 test_case.should_pass,
257 IsValidInteger(Input(test_case.input, test_case.length), &negative));
258 if (test_case.should_pass)
259 EXPECT_EQ(test_case.negative, negative);
263 // Tests parsing an empty BIT STRING.
264 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) {
265 const uint8_t kData[] = {0x00};
267 BitString bit_string;
268 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string));
270 EXPECT_EQ(0u, bit_string.unused_bits());
271 EXPECT_EQ(0u, bit_string.bytes().Length());
274 // Tests parsing an empty BIT STRING that incorrectly claims one unused bit.
275 TEST(ParseValuesTest, ParseBitStringEmptyOneUnusedBit) {
276 const uint8_t kData[] = {0x01};
278 BitString bit_string;
279 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string));
282 // Tests parsing an empty BIT STRING that is not minmally encoded (the entire
283 // last byte is comprised of unused bits).
284 TEST(ParseValuesTest, ParseBitStringNonEmptyTooManyUnusedBits) {
285 const uint8_t kData[] = {0x08, 0x00};
287 BitString bit_string;
288 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string));
291 // Tests parsing a BIT STRING of 7 bits each of which are 1.
292 TEST(ParseValuesTest, ParseBitStringSevenOneBits) {
293 const uint8_t kData[] = {0x01, 0xFE};
295 BitString bit_string;
296 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string));
298 EXPECT_EQ(1u, bit_string.unused_bits());
299 EXPECT_EQ(1u, bit_string.bytes().Length());
300 EXPECT_EQ(0xFE, bit_string.bytes().UnsafeData()[0]);
303 // Tests parsing a BIT STRING of 7 bits each of which are 1. The unused bit
304 // however is set to 1, which is an invalid encoding.
305 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) {
306 const uint8_t kData[] = {0x01, 0xFF};
308 BitString bit_string;
309 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string));
312 } // namespace test
313 } // namespace der
314 } // namespace net