OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "net/der/parse_values.h" | 8 #include "net/der/parse_values.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 buf[0] = 0xFF; | 31 buf[0] = 0xFF; |
32 value = Input(buf, 2); | 32 value = Input(buf, 2); |
33 EXPECT_FALSE(ParseBool(value, &out)); | 33 EXPECT_FALSE(ParseBool(value, &out)); |
34 value = Input(buf, 0); | 34 value = Input(buf, 0); |
35 EXPECT_FALSE(ParseBool(value, &out)); | 35 EXPECT_FALSE(ParseBool(value, &out)); |
36 } | 36 } |
37 | 37 |
38 TEST(ParseValuesTest, ParseTimes) { | 38 TEST(ParseValuesTest, ParseTimes) { |
39 GeneralizedTime out; | 39 GeneralizedTime out; |
40 | 40 |
41 EXPECT_TRUE(ParseUTCTime(Input("140218161200Z"), &out)); | 41 EXPECT_TRUE(ParseUTCTime(Input::FromCString("140218161200Z"), &out)); |
42 | 42 |
43 // DER-encoded UTCTime must end with 'Z'. | 43 // DER-encoded UTCTime must end with 'Z'. |
44 EXPECT_FALSE(ParseUTCTime(Input("140218161200X"), &out)); | 44 EXPECT_FALSE(ParseUTCTime(Input::FromCString("140218161200X"), &out)); |
45 | 45 |
46 // Check that a negative number (-4 in this case) doesn't get parsed as | 46 // Check that a negative number (-4 in this case) doesn't get parsed as |
47 // a 2-digit number. | 47 // a 2-digit number. |
48 EXPECT_FALSE(ParseUTCTime(Input("-40218161200Z"), &out)); | 48 EXPECT_FALSE(ParseUTCTime(Input::FromCString("-40218161200Z"), &out)); |
49 | 49 |
50 // Check that numbers with a leading 0 don't get parsed in octal by making | 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). | 51 // the second digit an invalid octal digit (e.g. 09). |
52 EXPECT_TRUE(ParseUTCTime(Input("090218161200Z"), &out)); | 52 EXPECT_TRUE(ParseUTCTime(Input::FromCString("090218161200Z"), &out)); |
53 | 53 |
54 // Check that the length is validated. | 54 // Check that the length is validated. |
55 EXPECT_FALSE(ParseUTCTime(Input("140218161200"), &out)); | 55 EXPECT_FALSE(ParseUTCTime(Input::FromCString("140218161200"), &out)); |
56 EXPECT_FALSE(ParseUTCTime(Input("140218161200Z0"), &out)); | 56 EXPECT_FALSE(ParseUTCTime(Input::FromCString("140218161200Z0"), &out)); |
57 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200"), &out)); | 57 EXPECT_FALSE(ParseUTCTimeRelaxed(Input::FromCString("140218161200"), &out)); |
58 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200Z0"), &out)); | 58 EXPECT_FALSE(ParseUTCTimeRelaxed(Input::FromCString("140218161200Z0"), &out)); |
59 | 59 |
60 // Check strictness of UTCTime parsers. | 60 // Check strictness of UTCTime parsers. |
61 EXPECT_FALSE(ParseUTCTime(Input("1402181612Z"), &out)); | 61 EXPECT_FALSE(ParseUTCTime(Input::FromCString("1402181612Z"), &out)); |
62 EXPECT_TRUE(ParseUTCTimeRelaxed(Input("1402181612Z"), &out)); | 62 EXPECT_TRUE(ParseUTCTimeRelaxed(Input::FromCString("1402181612Z"), &out)); |
63 | 63 |
64 // Check that the time ends in Z. | 64 // Check that the time ends in Z. |
65 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("1402181612Z0"), &out)); | 65 EXPECT_FALSE(ParseUTCTimeRelaxed(Input::FromCString("1402181612Z0"), &out)); |
66 | 66 |
67 // Check format of GeneralizedTime. | 67 // Check format of GeneralizedTime. |
68 | 68 |
69 // Leap seconds are allowed. | 69 // Leap seconds are allowed. |
70 EXPECT_TRUE(ParseGeneralizedTime(Input("20140218161260Z"), &out)); | 70 EXPECT_TRUE( |
| 71 ParseGeneralizedTime(Input::FromCString("20140218161260Z"), &out)); |
71 | 72 |
72 // But nothing larger than a leap second. | 73 // But nothing larger than a leap second. |
73 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161261Z"), &out)); | 74 EXPECT_FALSE( |
| 75 ParseGeneralizedTime(Input::FromCString("20140218161261Z"), &out)); |
74 | 76 |
75 // Minutes only go up to 59. | 77 // Minutes only go up to 59. |
76 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218166000Z"), &out)); | 78 EXPECT_FALSE( |
| 79 ParseGeneralizedTime(Input::FromCString("20140218166000Z"), &out)); |
77 | 80 |
78 // Hours only go up to 23. | 81 // Hours only go up to 23. |
79 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218240000Z"), &out)); | 82 EXPECT_FALSE( |
| 83 ParseGeneralizedTime(Input::FromCString("20140218240000Z"), &out)); |
80 // The 0th day of a month is invalid. | 84 // The 0th day of a month is invalid. |
81 EXPECT_FALSE(ParseGeneralizedTime(Input("20140200161200Z"), &out)); | 85 EXPECT_FALSE( |
| 86 ParseGeneralizedTime(Input::FromCString("20140200161200Z"), &out)); |
82 // The 0th month is invalid. | 87 // The 0th month is invalid. |
83 EXPECT_FALSE(ParseGeneralizedTime(Input("20140018161200Z"), &out)); | 88 EXPECT_FALSE( |
| 89 ParseGeneralizedTime(Input::FromCString("20140018161200Z"), &out)); |
84 // Months greater than 12 are invalid. | 90 // Months greater than 12 are invalid. |
85 EXPECT_FALSE(ParseGeneralizedTime(Input("20141318161200Z"), &out)); | 91 EXPECT_FALSE( |
| 92 ParseGeneralizedTime(Input::FromCString("20141318161200Z"), &out)); |
86 | 93 |
87 // Some months have 31 days. | 94 // Some months have 31 days. |
88 EXPECT_TRUE(ParseGeneralizedTime(Input("20140131000000Z"), &out)); | 95 EXPECT_TRUE( |
| 96 ParseGeneralizedTime(Input::FromCString("20140131000000Z"), &out)); |
89 | 97 |
90 // September has only 30 days. | 98 // September has only 30 days. |
91 EXPECT_FALSE(ParseGeneralizedTime(Input("20140931000000Z"), &out)); | 99 EXPECT_FALSE( |
| 100 ParseGeneralizedTime(Input::FromCString("20140931000000Z"), &out)); |
92 | 101 |
93 // February has only 28 days... | 102 // February has only 28 days... |
94 EXPECT_FALSE(ParseGeneralizedTime(Input("20140229000000Z"), &out)); | 103 EXPECT_FALSE( |
| 104 ParseGeneralizedTime(Input::FromCString("20140229000000Z"), &out)); |
95 | 105 |
96 // ... unless it's a leap year. | 106 // ... unless it's a leap year. |
97 EXPECT_TRUE(ParseGeneralizedTime(Input("20160229000000Z"), &out)); | 107 EXPECT_TRUE( |
| 108 ParseGeneralizedTime(Input::FromCString("20160229000000Z"), &out)); |
98 | 109 |
99 // There aren't any leap days in years divisible by 100... | 110 // There aren't any leap days in years divisible by 100... |
100 EXPECT_FALSE(ParseGeneralizedTime(Input("21000229000000Z"), &out)); | 111 EXPECT_FALSE( |
| 112 ParseGeneralizedTime(Input::FromCString("21000229000000Z"), &out)); |
101 | 113 |
102 // ...unless it's also divisible by 400. | 114 // ...unless it's also divisible by 400. |
103 EXPECT_TRUE(ParseGeneralizedTime(Input("20000229000000Z"), &out)); | 115 EXPECT_TRUE( |
| 116 ParseGeneralizedTime(Input::FromCString("20000229000000Z"), &out)); |
104 | 117 |
105 // Check more perverse invalid inputs. | 118 // Check more perverse invalid inputs. |
106 | 119 |
107 const uint8_t trailing_null_bytes[] = {'2', | 120 // Check that trailing null bytes are not ignored. |
108 '0', | 121 EXPECT_FALSE( |
109 '0', | 122 ParseGeneralizedTime(Input::FromCString("20001231010203Z\0"), &out)); |
110 '0', | 123 |
111 '1', | 124 // Check what happens when a null byte is in the middle of the input. |
112 '2', | 125 EXPECT_FALSE(ParseGeneralizedTime(Input::FromCString( |
113 '3', | 126 "200\0" |
114 '1', | 127 "1231010203Z"), |
115 '0', | 128 &out)); |
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)); | |
142 | 129 |
143 // The year can't be in hex. | 130 // The year can't be in hex. |
144 EXPECT_FALSE(ParseGeneralizedTime(Input("0x201231000000Z"), &out)); | 131 EXPECT_FALSE( |
| 132 ParseGeneralizedTime(Input::FromCString("0x201231000000Z"), &out)); |
145 | 133 |
146 // The last byte must be 'Z'. | 134 // The last byte must be 'Z'. |
147 EXPECT_FALSE(ParseGeneralizedTime(Input("20001231000000X"), &out)); | 135 EXPECT_FALSE( |
| 136 ParseGeneralizedTime(Input::FromCString("20001231000000X"), &out)); |
148 | 137 |
149 // Check that the length is validated. | 138 // Check that the length is validated. |
150 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200"), &out)); | 139 EXPECT_FALSE( |
151 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200Z0"), &out)); | 140 ParseGeneralizedTime(Input::FromCString("20140218161200"), &out)); |
| 141 EXPECT_FALSE( |
| 142 ParseGeneralizedTime(Input::FromCString("20140218161200Z0"), &out)); |
152 } | 143 } |
153 | 144 |
154 TEST(ParseValuesTest, TimesCompare) { | 145 TEST(ParseValuesTest, TimesCompare) { |
155 GeneralizedTime time1; | 146 GeneralizedTime time1; |
156 GeneralizedTime time2; | 147 GeneralizedTime time2; |
157 GeneralizedTime time3; | 148 GeneralizedTime time3; |
158 | 149 |
159 ASSERT_TRUE(ParseGeneralizedTime(Input("20140218161200Z"), &time1)); | 150 ASSERT_TRUE( |
160 ASSERT_TRUE(ParseUTCTime(Input("150218161200Z"), &time2)); | 151 ParseGeneralizedTime(Input::FromCString("20140218161200Z"), &time1)); |
161 ASSERT_TRUE(ParseGeneralizedTime(Input("20160218161200Z"), &time3)); | 152 ASSERT_TRUE(ParseUTCTime(Input::FromCString("150218161200Z"), &time2)); |
| 153 ASSERT_TRUE( |
| 154 ParseGeneralizedTime(Input::FromCString("20160218161200Z"), &time3)); |
162 EXPECT_TRUE(time1 < time2); | 155 EXPECT_TRUE(time1 < time2); |
163 EXPECT_TRUE(time2 < time3); | 156 EXPECT_TRUE(time2 < time3); |
164 EXPECT_TRUE(time1 < time3); | 157 EXPECT_TRUE(time1 < time3); |
165 } | 158 } |
166 | 159 |
167 struct Uint64TestData { | 160 struct Uint64TestData { |
168 bool should_pass; | 161 bool should_pass; |
169 const uint8_t input[9]; | 162 const uint8_t input[9]; |
170 size_t length; | 163 size_t length; |
171 uint64_t expected_value; | 164 uint64_t expected_value; |
(...skipping 20 matching lines...) Expand all Loading... |
192 EXPECT_EQ(test_case.should_pass, | 185 EXPECT_EQ(test_case.should_pass, |
193 ParseUint64(Input(test_case.input, test_case.length), &result)); | 186 ParseUint64(Input(test_case.input, test_case.length), &result)); |
194 if (test_case.should_pass) | 187 if (test_case.should_pass) |
195 EXPECT_EQ(test_case.expected_value, result); | 188 EXPECT_EQ(test_case.expected_value, result); |
196 } | 189 } |
197 } | 190 } |
198 | 191 |
199 } // namespace test | 192 } // namespace test |
200 } // namespace der | 193 } // namespace der |
201 } // namespace net | 194 } // namespace net |
OLD | NEW |