[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 1 | // Copyright (c) 2011 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 "crypto/nss_util.h" |
| 6 | |
| 7 | #include <prtime.h> |
| 8 | |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 9 | #include "base/time/time.h" |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace crypto { |
| 13 | |
| 14 | TEST(NSSUtilTest, PRTimeConversion) { |
| 15 | EXPECT_EQ(base::Time::UnixEpoch(), PRTimeToBaseTime(0)); |
| 16 | EXPECT_EQ(0, BaseTimeToPRTime(base::Time::UnixEpoch())); |
| 17 | |
| 18 | PRExplodedTime prxtime; |
| 19 | prxtime.tm_params.tp_gmt_offset = 0; |
| 20 | prxtime.tm_params.tp_dst_offset = 0; |
| 21 | base::Time::Exploded exploded; |
pkasting | 0755163 | 2014-10-21 07:43:02 | [diff] [blame] | 22 | exploded.year = prxtime.tm_year = 2011; |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 23 | exploded.month = 12; |
| 24 | prxtime.tm_month = 11; |
pkasting | 0755163 | 2014-10-21 07:43:02 | [diff] [blame] | 25 | // PRExplodedTime::tm_wday is a smaller type than Exploded::day_of_week, so |
| 26 | // assigning the two in this order instead of the reverse avoids potential |
| 27 | // warnings about type downcasting. |
| 28 | exploded.day_of_week = prxtime.tm_wday = 0; // Should be unused. |
| 29 | exploded.day_of_month = prxtime.tm_mday = 10; |
| 30 | exploded.hour = prxtime.tm_hour = 2; |
| 31 | exploded.minute = prxtime.tm_min = 52; |
| 32 | exploded.second = prxtime.tm_sec = 19; |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 33 | exploded.millisecond = 342; |
| 34 | prxtime.tm_usec = 342000; |
| 35 | |
| 36 | PRTime pr_time = PR_ImplodeTime(&prxtime); |
maksim.sisov | 5ae4586 | 2016-06-29 05:14:29 | [diff] [blame] | 37 | base::Time base_time; |
| 38 | EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &base_time)); |
[email protected] | ca929ed3 | 2011-12-15 20:37:28 | [diff] [blame] | 39 | |
| 40 | EXPECT_EQ(base_time, PRTimeToBaseTime(pr_time)); |
| 41 | EXPECT_EQ(pr_time, BaseTimeToPRTime(base_time)); |
| 42 | } |
| 43 | |
| 44 | } // namespace crypto |