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 const uint8_t valid_utc_time[] = { |
42 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'Z'}; | |
43 EXPECT_TRUE(ParseUTCTime(Input(valid_utc_time), &out)); | |
42 | 44 |
43 // DER-encoded UTCTime must end with 'Z'. | 45 // DER-encoded UTCTime must end with 'Z'. |
44 EXPECT_FALSE(ParseUTCTime(Input("140218161200X"), &out)); | 46 const uint8_t utc_time_missing_z[] = { |
47 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'X'}; | |
48 EXPECT_FALSE(ParseUTCTime(Input(utc_time_missing_z), &out)); | |
45 | 49 |
46 // Check that a negative number (-4 in this case) doesn't get parsed as | 50 // Check that a negative number (-4 in this case) doesn't get parsed as |
47 // a 2-digit number. | 51 // a 2-digit number. |
48 EXPECT_FALSE(ParseUTCTime(Input("-40218161200Z"), &out)); | 52 const uint8_t utc_time_negative_number[] = { |
53 '-', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'Z'}; | |
54 EXPECT_FALSE(ParseUTCTime(Input(utc_time_negative_number), &out)); | |
49 | 55 |
50 // Check that numbers with a leading 0 don't get parsed in octal by making | 56 // 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). | 57 // the second digit an invalid octal digit (e.g. 09). |
52 EXPECT_TRUE(ParseUTCTime(Input("090218161200Z"), &out)); | 58 const uint8_t utc_time_test_octal[] = { |
59 '0', '9', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'Z'}; | |
60 EXPECT_TRUE(ParseUTCTime(Input(utc_time_test_octal), &out)); | |
53 | 61 |
54 // Check that the length is validated. | 62 // Check that the length is validated. |
55 EXPECT_FALSE(ParseUTCTime(Input("140218161200"), &out)); | 63 const uint8_t utc_time_too_short[] = { |
56 EXPECT_FALSE(ParseUTCTime(Input("140218161200Z0"), &out)); | 64 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0'}; |
57 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200"), &out)); | 65 const uint8_t utc_time_too_long[] = { |
58 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200Z0"), &out)); | 66 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'Z', '0'}; |
67 EXPECT_FALSE(ParseUTCTime(Input(utc_time_too_short), &out)); | |
68 EXPECT_FALSE(ParseUTCTime(Input(utc_time_too_long), &out)); | |
69 EXPECT_FALSE(ParseUTCTimeRelaxed(Input(utc_time_too_short), &out)); | |
70 EXPECT_FALSE(ParseUTCTimeRelaxed(Input(utc_time_too_long), &out)); | |
59 | 71 |
60 // Check strictness of UTCTime parsers. | 72 // Check strictness of UTCTime parsers. |
61 EXPECT_FALSE(ParseUTCTime(Input("1402181612Z"), &out)); | 73 const uint8_t utc_time_relaxed[] = { |
62 EXPECT_TRUE(ParseUTCTimeRelaxed(Input("1402181612Z"), &out)); | 74 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', 'Z'}; |
75 EXPECT_FALSE(ParseUTCTime(Input(utc_time_relaxed), &out)); | |
76 EXPECT_TRUE(ParseUTCTimeRelaxed(Input(utc_time_relaxed), &out)); | |
63 | 77 |
64 // Check that the time ends in Z. | 78 // Check that the time ends in Z. |
65 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("1402181612Z0"), &out)); | 79 const uint8_t utc_time_relaxed_too_long[] = { |
80 '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', 'Z', '0'}; | |
81 EXPECT_FALSE(ParseUTCTimeRelaxed(Input(utc_time_relaxed_too_long), &out)); | |
66 | 82 |
67 // Check format of GeneralizedTime. | 83 // Check format of GeneralizedTime. |
68 | 84 |
69 // Leap seconds are allowed. | 85 // Leap seconds are allowed. |
70 EXPECT_TRUE(ParseGeneralizedTime(Input("20140218161260Z"), &out)); | 86 const uint8_t generalized_time_with_leap_second[] = {'2', |
87 '0', | |
88 '1', | |
89 '4', | |
90 '0', | |
91 '2', | |
92 '1', | |
93 '8', | |
94 '1', | |
95 '6', | |
96 '1', | |
97 '2', | |
98 '6', | |
99 '0', | |
100 'Z'}; | |
nharper
2015/05/26 22:11:49
I don't particularly like how these look. I tried
Ryan Sleevi
2015/06/03 23:09:11
As soon as the new clang rolls in to Chromium, thi
| |
101 EXPECT_TRUE( | |
102 ParseGeneralizedTime(Input(generalized_time_with_leap_second), &out)); | |
71 | 103 |
72 // But nothing larger than a leap second. | 104 // But nothing larger than a leap second. |
73 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161261Z"), &out)); | 105 const uint8_t generalized_time_second_too_big[] = {'2', |
106 '0', | |
107 '1', | |
108 '4', | |
109 '0', | |
110 '2', | |
111 '1', | |
112 '8', | |
113 '1', | |
114 '6', | |
115 '1', | |
116 '2', | |
117 '6', | |
118 '1', | |
119 'Z'}; | |
120 EXPECT_FALSE( | |
121 ParseGeneralizedTime(Input(generalized_time_second_too_big), &out)); | |
74 | 122 |
75 // Minutes only go up to 59. | 123 // Minutes only go up to 59. |
76 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218166000Z"), &out)); | 124 const uint8_t generalized_time_minute_too_big[] = {'2', |
125 '0', | |
126 '1', | |
127 '4', | |
128 '0', | |
129 '2', | |
130 '1', | |
131 '8', | |
132 '1', | |
133 '6', | |
134 '6', | |
135 '0', | |
136 '0', | |
137 '0', | |
138 'Z'}; | |
139 EXPECT_FALSE( | |
140 ParseGeneralizedTime(Input(generalized_time_minute_too_big), &out)); | |
77 | 141 |
78 // Hours only go up to 23. | 142 // Hours only go up to 23. |
79 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218240000Z"), &out)); | 143 const uint8_t generalized_time_hour_too_big[] = {'2', |
144 '0', | |
145 '1', | |
146 '4', | |
147 '0', | |
148 '2', | |
149 '1', | |
150 '8', | |
151 '2', | |
152 '4', | |
153 '0', | |
154 '0', | |
155 '0', | |
156 '0', | |
157 'Z'}; | |
158 EXPECT_FALSE( | |
159 ParseGeneralizedTime(Input(generalized_time_hour_too_big), &out)); | |
160 | |
80 // The 0th day of a month is invalid. | 161 // The 0th day of a month is invalid. |
81 EXPECT_FALSE(ParseGeneralizedTime(Input("20140200161200Z"), &out)); | 162 const uint8_t generalized_time_zeroth_day[] = {'2', |
163 '0', | |
164 '1', | |
165 '4', | |
166 '0', | |
167 '2', | |
168 '0', | |
169 '0', | |
170 '1', | |
171 '6', | |
172 '1', | |
173 '2', | |
174 '0', | |
175 '0', | |
176 'Z'}; | |
177 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_zeroth_day), &out)); | |
82 // The 0th month is invalid. | 178 // The 0th month is invalid. |
83 EXPECT_FALSE(ParseGeneralizedTime(Input("20140018161200Z"), &out)); | 179 const uint8_t generalized_time_zeroth_month[] = {'2', |
180 '0', | |
181 '1', | |
182 '4', | |
183 '0', | |
184 '0', | |
185 '1', | |
186 '8', | |
187 '1', | |
188 '6', | |
189 '1', | |
190 '2', | |
191 '0', | |
192 '0', | |
193 'Z'}; | |
194 EXPECT_FALSE( | |
195 ParseGeneralizedTime(Input(generalized_time_zeroth_month), &out)); | |
84 // Months greater than 12 are invalid. | 196 // Months greater than 12 are invalid. |
85 EXPECT_FALSE(ParseGeneralizedTime(Input("20141318161200Z"), &out)); | 197 const uint8_t generalized_time_13th_month[] = {'2', |
198 '0', | |
199 '1', | |
200 '4', | |
201 '1', | |
202 '3', | |
203 '1', | |
204 '8', | |
205 '1', | |
206 '6', | |
207 '1', | |
208 '2', | |
209 '0', | |
210 '0', | |
211 'Z'}; | |
212 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_13th_month), &out)); | |
86 | 213 |
87 // Some months have 31 days. | 214 // Some months have 31 days. |
88 EXPECT_TRUE(ParseGeneralizedTime(Input("20140131000000Z"), &out)); | 215 const uint8_t generalized_time_january_thirty_first[] = {'2', |
216 '0', | |
217 '1', | |
218 '4', | |
219 '0', | |
220 '1', | |
221 '3', | |
222 '1', | |
223 '0', | |
224 '0', | |
225 '0', | |
226 '0', | |
227 '0', | |
228 '0', | |
229 'Z'}; | |
230 EXPECT_TRUE( | |
231 ParseGeneralizedTime(Input(generalized_time_january_thirty_first), &out)); | |
89 | 232 |
90 // September has only 30 days. | 233 // September has only 30 days. |
91 EXPECT_FALSE(ParseGeneralizedTime(Input("20140931000000Z"), &out)); | 234 const uint8_t generalized_time_september_31st[] = {'2', |
235 '0', | |
236 '1', | |
237 '4', | |
238 '0', | |
239 '9', | |
240 '3', | |
241 '1', | |
242 '0', | |
243 '0', | |
244 '0', | |
245 '0', | |
246 '0', | |
247 '0', | |
248 'Z'}; | |
249 EXPECT_FALSE( | |
250 ParseGeneralizedTime(Input(generalized_time_september_31st), &out)); | |
92 | 251 |
93 // February has only 28 days... | 252 // February has only 28 days... |
94 EXPECT_FALSE(ParseGeneralizedTime(Input("20140229000000Z"), &out)); | 253 const uint8_t generalized_time_february_29th_non_leap_year[] = {'2', |
254 '0', | |
255 '1', | |
256 '4', | |
257 '0', | |
258 '2', | |
259 '2', | |
260 '9', | |
261 '0', | |
262 '0', | |
263 '0', | |
264 '0', | |
265 '0', | |
266 '0', | |
267 'Z'}; | |
268 EXPECT_FALSE(ParseGeneralizedTime( | |
269 Input(generalized_time_february_29th_non_leap_year), &out)); | |
95 | 270 |
96 // ... unless it's a leap year. | 271 // ... unless it's a leap year. |
97 EXPECT_TRUE(ParseGeneralizedTime(Input("20160229000000Z"), &out)); | 272 const uint8_t generalized_time_february_29th_leap_year[] = {'2', |
273 '0', | |
274 '1', | |
275 '6', | |
276 '0', | |
277 '2', | |
278 '2', | |
279 '9', | |
280 '0', | |
281 '0', | |
282 '0', | |
283 '0', | |
284 '0', | |
285 '0', | |
286 'Z'}; | |
287 EXPECT_TRUE(ParseGeneralizedTime( | |
288 Input(generalized_time_february_29th_leap_year), &out)); | |
98 | 289 |
99 // There aren't any leap days in years divisible by 100... | 290 // There aren't any leap days in years divisible by 100... |
100 EXPECT_FALSE(ParseGeneralizedTime(Input("21000229000000Z"), &out)); | 291 const uint8_t generalized_time_leap_day_100th_year[] = {'2', |
292 '1', | |
293 '0', | |
294 '0', | |
295 '0', | |
296 '2', | |
297 '2', | |
298 '9', | |
299 '0', | |
300 '0', | |
301 '0', | |
302 '0', | |
303 '0', | |
304 '0', | |
305 'Z'}; | |
306 EXPECT_FALSE( | |
307 ParseGeneralizedTime(Input(generalized_time_leap_day_100th_year), &out)); | |
101 | 308 |
102 // ...unless it's also divisible by 400. | 309 // ...unless it's also divisible by 400. |
103 EXPECT_TRUE(ParseGeneralizedTime(Input("20000229000000Z"), &out)); | 310 const uint8_t generalized_time_leap_day_400th_year[] = {'2', |
311 '0', | |
312 '0', | |
313 '0', | |
314 '0', | |
315 '2', | |
316 '2', | |
317 '9', | |
318 '0', | |
319 '0', | |
320 '0', | |
321 '0', | |
322 '0', | |
323 '0', | |
324 'Z'}; | |
325 EXPECT_TRUE( | |
326 ParseGeneralizedTime(Input(generalized_time_leap_day_400th_year), &out)); | |
104 | 327 |
105 // Check more perverse invalid inputs. | 328 // Check more perverse invalid inputs. |
106 | 329 |
107 const uint8_t trailing_null_bytes[] = {'2', | 330 // Check that trailing null bytes are not ignored. |
108 '0', | 331 const uint8_t trailing_null[] = {'2', |
109 '0', | 332 '0', |
110 '0', | 333 '0', |
111 '1', | 334 '0', |
112 '2', | 335 '1', |
113 '3', | 336 '2', |
114 '1', | 337 '3', |
115 '0', | 338 '1', |
116 '1', | 339 '0', |
117 '0', | 340 '1', |
118 '2', | 341 '0', |
119 '0', | 342 '2', |
120 '3', | 343 '0', |
121 'Z', | 344 '3', |
122 '\0'}; | 345 'Z', |
123 Input trailing_null(trailing_null_bytes, sizeof(trailing_null_bytes)); | 346 '\0'}; |
124 EXPECT_FALSE(ParseGeneralizedTime(trailing_null, &out)); | 347 EXPECT_FALSE(ParseGeneralizedTime(Input(trailing_null), &out)); |
125 const uint8_t embedded_null_bytes[] = {'2', | 348 |
126 '0', | 349 // Check what happens when a null byte is in the middle of the input. |
127 '0', | 350 const uint8_t embedded_null[] = {'2', |
128 '\0', | 351 '0', |
129 '1', | 352 '0', |
130 '2', | 353 '\0', |
131 '3', | 354 '1', |
132 '1', | 355 '2', |
133 '0', | 356 '3', |
134 '1', | 357 '1', |
135 '0', | 358 '0', |
136 '2', | 359 '1', |
137 '0', | 360 '0', |
138 '3', | 361 '2', |
139 'Z'}; | 362 '0', |
140 Input embedded_null(embedded_null_bytes, sizeof(embedded_null_bytes)); | 363 '3', |
141 EXPECT_FALSE(ParseGeneralizedTime(embedded_null, &out)); | 364 'Z'}; |
365 EXPECT_FALSE(ParseGeneralizedTime(Input(embedded_null), &out)); | |
142 | 366 |
143 // The year can't be in hex. | 367 // The year can't be in hex. |
144 EXPECT_FALSE(ParseGeneralizedTime(Input("0x201231000000Z"), &out)); | 368 const uint8_t generalized_time_year_in_hex[] = {'0', |
369 'x', | |
370 '2', | |
371 '0', | |
372 '1', | |
373 '2', | |
374 '3', | |
375 '1', | |
376 '0', | |
377 '0', | |
378 '0', | |
379 '0', | |
380 '0', | |
381 '0', | |
382 'Z'}; | |
383 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_year_in_hex), &out)); | |
145 | 384 |
146 // The last byte must be 'Z'. | 385 // The last byte must be 'Z'. |
147 EXPECT_FALSE(ParseGeneralizedTime(Input("20001231000000X"), &out)); | 386 const uint8_t generalized_time_missing_z[] = {'2', |
387 '0', | |
388 '0', | |
389 '0', | |
390 '1', | |
391 '2', | |
392 '3', | |
393 '1', | |
394 '0', | |
395 '0', | |
396 '0', | |
397 '0', | |
398 '0', | |
399 '0', | |
400 'X'}; | |
401 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_missing_z), &out)); | |
148 | 402 |
149 // Check that the length is validated. | 403 // Check that the length is validated. |
150 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200"), &out)); | 404 const uint8_t generalized_time_too_short[] = { |
151 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200Z0"), &out)); | 405 '2', '0', '1', '4', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0'}; |
406 const uint8_t generalized_time_too_long[] = {'2', | |
407 '0', | |
408 '1', | |
409 '4', | |
410 '0', | |
411 '2', | |
412 '1', | |
413 '8', | |
414 '1', | |
415 '6', | |
416 '1', | |
417 '2', | |
418 '0', | |
419 '0', | |
420 'Z', | |
421 '0'}; | |
422 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_too_short), &out)); | |
423 EXPECT_FALSE(ParseGeneralizedTime(Input(generalized_time_too_long), &out)); | |
152 } | 424 } |
153 | 425 |
154 TEST(ParseValuesTest, TimesCompare) { | 426 TEST(ParseValuesTest, TimesCompare) { |
155 GeneralizedTime time1; | 427 GeneralizedTime time1; |
156 GeneralizedTime time2; | 428 GeneralizedTime time2; |
157 GeneralizedTime time3; | 429 GeneralizedTime time3; |
158 | 430 |
159 ASSERT_TRUE(ParseGeneralizedTime(Input("20140218161200Z"), &time1)); | 431 const uint8_t time1_bytes[] = {'2', |
160 ASSERT_TRUE(ParseUTCTime(Input("150218161200Z"), &time2)); | 432 '0', |
161 ASSERT_TRUE(ParseGeneralizedTime(Input("20160218161200Z"), &time3)); | 433 '1', |
434 '4', | |
435 '0', | |
436 '2', | |
437 '1', | |
438 '8', | |
439 '1', | |
440 '6', | |
441 '1', | |
442 '2', | |
443 '0', | |
444 '0', | |
445 'Z'}; | |
446 const uint8_t time2_bytes[] = { | |
447 '1', '5', '0', '2', '1', '8', '1', '6', '1', '2', '0', '0', 'Z'}; | |
448 const uint8_t time3_bytes[] = {'2', | |
449 '0', | |
450 '1', | |
451 '6', | |
452 '0', | |
453 '2', | |
454 '1', | |
455 '8', | |
456 '1', | |
457 '6', | |
458 '1', | |
459 '2', | |
460 '0', | |
461 '0', | |
462 'Z'}; | |
463 | |
464 ASSERT_TRUE(ParseGeneralizedTime(Input(time1_bytes), &time1)); | |
465 ASSERT_TRUE(ParseUTCTime(Input(time2_bytes), &time2)); | |
466 ASSERT_TRUE(ParseGeneralizedTime(Input(time3_bytes), &time3)); | |
162 EXPECT_TRUE(time1 < time2); | 467 EXPECT_TRUE(time1 < time2); |
163 EXPECT_TRUE(time2 < time3); | 468 EXPECT_TRUE(time2 < time3); |
164 EXPECT_TRUE(time1 < time3); | 469 EXPECT_TRUE(time1 < time3); |
165 } | 470 } |
166 | 471 |
167 struct Uint64TestData { | 472 struct Uint64TestData { |
168 bool should_pass; | 473 bool should_pass; |
169 const uint8_t input[9]; | 474 const uint8_t input[9]; |
170 size_t length; | 475 size_t length; |
171 uint64_t expected_value; | 476 uint64_t expected_value; |
(...skipping 20 matching lines...) Expand all Loading... | |
192 EXPECT_EQ(test_case.should_pass, | 497 EXPECT_EQ(test_case.should_pass, |
193 ParseUint64(Input(test_case.input, test_case.length), &result)); | 498 ParseUint64(Input(test_case.input, test_case.length), &result)); |
194 if (test_case.should_pass) | 499 if (test_case.should_pass) |
195 EXPECT_EQ(test_case.expected_value, result); | 500 EXPECT_EQ(test_case.expected_value, result); |
196 } | 501 } |
197 } | 502 } |
198 | 503 |
199 } // namespace test | 504 } // namespace test |
200 } // namespace der | 505 } // namespace der |
201 } // namespace net | 506 } // namespace net |
OLD | NEW |