Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "base/time/time.h" | |
| 6 #include "net/der/encode_values.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace net { | |
| 10 namespace der { | |
| 11 namespace test { | |
| 12 | |
| 13 TEST(EncodeValuesTest, EncodeTimeAsGeneralizedTime) { | |
| 14 // Fri, 24 Jun 2016 17:04:54 GMT | |
| 15 base::Time time = | |
| 16 base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(1466787894); | |
| 17 GeneralizedTime generalized_time; | |
| 18 ASSERT_TRUE(EncodeTimeAsGeneralizedTime(time, &generalized_time)); | |
| 19 EXPECT_EQ(2016, generalized_time.year); | |
| 20 EXPECT_EQ(6, generalized_time.month); | |
| 21 EXPECT_EQ(24, generalized_time.day); | |
| 22 EXPECT_EQ(17, generalized_time.hours); | |
| 23 EXPECT_EQ(4, generalized_time.minutes); | |
| 24 EXPECT_EQ(54, generalized_time.seconds); | |
| 25 } | |
| 26 | |
| 27 // ASN.1 GeneralizedTime can represent dates from year 0000 to 9999, and | |
| 28 // although base::Time can represent times from before the Windows epoch and | |
| 29 // after the 32-bit time_t maximum, the conversion between base::Time and | |
| 30 // der::GeneralizedTime goes through the time representation of the underlying | |
| 31 // platform, which might not be able to handle the full GeneralizedTime date | |
| 32 // range. Out-of-range times should not be converted to der::GeneralizedTime. In | |
| 33 // tests, possibly-out-of-range test times are specified as a | |
| 34 // base::Time::Exploded, and then converted to a base::Time. If the conversion | |
| 35 // fails, this signals the underlying platform cannot handle the time, and the | |
| 36 // test aborts early. If the underlying platform can represent the time, then | |
| 37 // the conversion is successful, and the encoded GeneralizedTime can should | |
| 38 // match the test time. | |
| 39 // | |
| 40 // Thu, 1 Jan 1570 00:00:00 GMT. This time is unrepresentable by the Windows | |
| 41 // native time libraries. | |
| 42 TEST(EncodeValuesTest, EncodeTimeFromBeforeWindowsEpoch) { | |
| 43 base::Time::Exploded exploded; | |
|
Lei Zhang
2016/07/14 10:39:51
Something is uninitialized here, which is the real
dadrian
2016/07/14 17:37:29
Done.
| |
| 44 exploded.year = 1570; | |
| 45 exploded.month = 1; | |
| 46 exploded.day_of_week = 5; | |
| 47 exploded.hour = 0; | |
| 48 exploded.minute = 0; | |
| 49 exploded.second = 0; | |
| 50 exploded.millisecond = 0; | |
| 51 | |
| 52 base::Time time; | |
| 53 if (!base::Time::FromUTCExploded(exploded, &time)) | |
| 54 return; | |
| 55 | |
| 56 GeneralizedTime generalized_time; | |
| 57 ASSERT_TRUE(EncodeTimeAsGeneralizedTime(time, &generalized_time)); | |
| 58 EXPECT_EQ(1570, generalized_time.year); | |
| 59 EXPECT_EQ(1, generalized_time.month); | |
| 60 EXPECT_EQ(1, generalized_time.day); | |
| 61 EXPECT_EQ(0, generalized_time.hours); | |
| 62 EXPECT_EQ(0, generalized_time.minutes); | |
| 63 EXPECT_EQ(0, generalized_time.seconds); | |
| 64 } | |
| 65 | |
| 66 // Sat, 1 Jan 2039 00:00:00 GMT. See above comment. This time may be | |
| 67 // unrepresentable on 32-bit systems. | |
| 68 TEST(EncodeValuesTest, EncodeTimeAfterTimeTMax) { | |
| 69 base::Time::Exploded exploded; | |
|
Lei Zhang
2016/07/14 10:39:51
Ditto
dadrian
2016/07/14 17:37:29
Done.
| |
| 70 exploded.year = 2039; | |
| 71 exploded.month = 1; | |
| 72 exploded.day_of_week = 7; | |
| 73 exploded.hour = 0; | |
| 74 exploded.minute = 0; | |
| 75 exploded.second = 0; | |
| 76 exploded.millisecond = 0; | |
| 77 | |
| 78 base::Time time; | |
| 79 if (!base::Time::FromUTCExploded(exploded, &time)) | |
| 80 return; | |
| 81 | |
| 82 GeneralizedTime generalized_time; | |
| 83 ASSERT_TRUE(EncodeTimeAsGeneralizedTime(time, &generalized_time)); | |
| 84 EXPECT_EQ(2039, generalized_time.year); | |
| 85 EXPECT_EQ(1, generalized_time.month); | |
| 86 EXPECT_EQ(1, generalized_time.day); | |
| 87 EXPECT_EQ(0, generalized_time.hours); | |
| 88 EXPECT_EQ(0, generalized_time.minutes); | |
| 89 EXPECT_EQ(0, generalized_time.seconds); | |
| 90 } | |
| 91 | |
| 92 } // namespace test | |
| 93 | |
| 94 } // namespace der | |
| 95 | |
| 96 } // namespace net | |
| OLD | NEW |