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.
7 #include "base/macros.h"
8 #include "net/der/parse_values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
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);
26 TEST(ParseValuesTest
, ParseBool
) {
27 uint8_t buf
[] = {0xFF, 0x00};
30 EXPECT_TRUE(ParseBool(value
, &out
));
34 EXPECT_TRUE(ParseBool(value
, &out
));
38 EXPECT_FALSE(ParseBool(value
, &out
));
39 EXPECT_TRUE(ParseBoolRelaxed(value
, &out
));
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
) {
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
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.
85 ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out
));
87 // Minutes only go up to 59.
89 ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out
));
91 // Hours only go up to 23.
93 ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out
));
94 // The 0th day of a month is invalid.
96 ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out
));
97 // The 0th month is invalid.
99 ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out
));
100 // Months greater than 12 are invalid.
102 ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out
));
104 // Some months have 31 days.
105 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out
));
107 // September has only 30 days.
109 ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out
));
111 // February has only 28 days...
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...
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.
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(
137 // The year can't be in hex.
139 ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out
));
141 // The last byte must be 'Z'.
143 ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out
));
145 // Check that the length is validated.
146 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out
));
148 ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out
));
151 TEST(ParseValuesTest
, TimesCompare
) {
152 GeneralizedTime time1
;
153 GeneralizedTime time2
;
154 GeneralizedTime time3
;
157 ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time1
));
158 ASSERT_TRUE(ParseUTCTime(FromStringLiteral("150218161200Z"), &time2
));
160 ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time3
));
161 EXPECT_TRUE(time1
< time2
);
162 EXPECT_TRUE(time2
< time3
);
163 EXPECT_TRUE(time1
< time3
);
166 struct Uint64TestData
{
168 const uint8_t input
[9];
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},
185 TEST(ParseValuesTest
, ParseUint64
) {
186 for (size_t i
= 0; i
< arraysize(kUint64TestData
); i
++) {
187 Uint64TestData test_case
= kUint64TestData
[i
];
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
);