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 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.
94 ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out
));
96 // Minutes only go up to 59.
98 ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out
));
100 // Hours only go up to 23.
102 ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out
));
103 // The 0th day of a month is invalid.
105 ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out
));
106 // The 0th month is invalid.
108 ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out
));
109 // Months greater than 12 are invalid.
111 ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out
));
113 // Some months have 31 days.
114 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out
));
116 // September has only 30 days.
118 ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out
));
120 // February has only 28 days...
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...
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.
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(
146 // The year can't be in hex.
148 ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out
));
150 // The last byte must be 'Z'.
152 ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out
));
154 // Check that the length is validated.
155 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out
));
157 ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out
));
160 TEST(ParseValuesTest
, TimesCompare
) {
161 GeneralizedTime time1
;
162 GeneralizedTime time2
;
163 GeneralizedTime time3
;
164 GeneralizedTime time4
;
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
));
172 ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time4
));
173 EXPECT_TRUE(time1
< time2
);
174 EXPECT_TRUE(time2
< time3
);
175 EXPECT_TRUE(time3
< time4
);
178 struct Uint64TestData
{
180 const uint8_t input
[9];
182 uint64_t expected_value
;
185 const Uint64TestData kUint64TestData
[] = {
186 {true, {0x00}, 1, 0},
187 {true, {0x01}, 1, 1},
188 {false, {0xFF}, 1, 0},
189 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX
},
190 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0},
191 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0},
192 {false, {0x00, 0x01}, 2, 1},
193 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0},
197 TEST(ParseValuesTest
, ParseUint64
) {
198 for (size_t i
= 0; i
< arraysize(kUint64TestData
); i
++) {
199 Uint64TestData test_case
= kUint64TestData
[i
];
203 EXPECT_EQ(test_case
.should_pass
,
204 ParseUint64(Input(test_case
.input
, test_case
.length
), &result
));
205 if (test_case
.should_pass
)
206 EXPECT_EQ(test_case
.expected_value
, result
);
210 // Tests parsing an empty BIT STRING.
211 TEST(ParseValuesTest
, ParseBitStringEmptyNoUnusedBits
) {
212 const uint8_t kData
[] = {0x00};
214 BitString bit_string
;
215 ASSERT_TRUE(ParseBitString(Input(kData
), &bit_string
));
217 EXPECT_EQ(0u, bit_string
.unused_bits());
218 EXPECT_EQ(0u, bit_string
.bytes().Length());
221 // Tests parsing an empty BIT STRING that incorrectly claims one unused bit.
222 TEST(ParseValuesTest
, ParseBitStringEmptyOneUnusedBit
) {
223 const uint8_t kData
[] = {0x01};
225 BitString bit_string
;
226 EXPECT_FALSE(ParseBitString(Input(kData
), &bit_string
));
229 // Tests parsing an empty BIT STRING that is not minmally encoded (the entire
230 // last byte is comprised of unused bits).
231 TEST(ParseValuesTest
, ParseBitStringNonEmptyTooManyUnusedBits
) {
232 const uint8_t kData
[] = {0x08, 0x00};
234 BitString bit_string
;
235 EXPECT_FALSE(ParseBitString(Input(kData
), &bit_string
));
238 // Tests parsing a BIT STRING of 7 bits each of which are 1.
239 TEST(ParseValuesTest
, ParseBitStringSevenOneBits
) {
240 const uint8_t kData
[] = {0x01, 0xFE};
242 BitString bit_string
;
243 ASSERT_TRUE(ParseBitString(Input(kData
), &bit_string
));
245 EXPECT_EQ(1u, bit_string
.unused_bits());
246 EXPECT_EQ(1u, bit_string
.bytes().Length());
247 EXPECT_EQ(0xFE, bit_string
.bytes().UnsafeData()[0]);
250 // Tests parsing a BIT STRING of 7 bits each of which are 1. The unused bit
251 // however is set to 1, which is an invalid encoding.
252 TEST(ParseValuesTest
, ParseBitStringSevenOneBitsUnusedBitIsOne
) {
253 const uint8_t kData
[] = {0x01, 0xFF};
255 BitString bit_string
;
256 EXPECT_FALSE(ParseBitString(Input(kData
), &bit_string
));