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"
15 TEST(ParseValuesTest
, ParseBool
) {
16 uint8_t buf
[] = {0xFF, 0x00};
19 EXPECT_TRUE(ParseBool(value
, &out
));
23 EXPECT_TRUE(ParseBool(value
, &out
));
27 EXPECT_FALSE(ParseBool(value
, &out
));
28 EXPECT_TRUE(ParseBoolRelaxed(value
, &out
));
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
) {
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
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',
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',
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
{
169 const uint8_t input
[9];
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},
186 TEST(ParseValuesTest
, ParseUint64
) {
187 for (size_t i
= 0; i
< arraysize(kUint64TestData
); i
++) {
188 Uint64TestData test_case
= kUint64TestData
[i
];
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
);