diff options
Diffstat (limited to 'third_party/chromium/base/time/time_unittest.cc')
-rw-r--r-- | third_party/chromium/base/time/time_unittest.cc | 64 |
1 files changed, 58 insertions, 6 deletions
diff --git a/third_party/chromium/base/time/time_unittest.cc b/third_party/chromium/base/time/time_unittest.cc index c471a76..c4c61df 100644 --- a/third_party/chromium/base/time/time_unittest.cc +++ b/third_party/chromium/base/time/time_unittest.cc @@ -21,6 +21,52 @@ namespace base { namespace { +TEST(TimeTestOutOfBounds, FromExplodedOutOfBoundsTime) { + // FromUTCExploded must set time to Time(0) and failure, if the day is set to + // 31 on a 28-30 day month. Test |exploded| returns Time(0) on 31st of + // February and 31st of April. New implementation handles this. + + const struct DateTestData { + Time::Exploded explode; + bool is_valid; + } kDateTestData[] = { + // 31st of February + {{2016, 2, 0, 31, 12, 30, 0, 0}, true}, + // 31st of April + {{2016, 4, 0, 31, 8, 43, 0, 0}, true}, + // Negative month + {{2016, -5, 0, 2, 4, 10, 0, 0}, false}, + // Negative date of month + {{2016, 6, 0, -15, 2, 50, 0, 0}, false}, + // Negative hours + {{2016, 7, 0, 10, -11, 29, 0, 0}, false}, + // Negative minutes + {{2016, 3, 0, 14, 10, -29, 0, 0}, false}, + // Negative seconds + {{2016, 10, 0, 25, 7, 47, -30, 0}, false}, + // Negative milliseconds + {{2016, 10, 0, 25, 7, 47, 20, -500}, false}, + // Hours are too large + {{2016, 7, 0, 10, 26, 29, 0, 0}, false}, + // Minutes are too large + {{2016, 3, 0, 14, 10, 78, 0, 0}, false}, + // Seconds are too large + {{2016, 10, 0, 25, 7, 47, 234, 0}, false}, + // Milliseconds are too large + {{2016, 10, 0, 25, 6, 31, 23, 1643}, false}, + }; + + for (const auto& test : kDateTestData) { + EXPECT_EQ(test.explode.HasValidValues(), test.is_valid); + + base::Time result; + EXPECT_FALSE(base::Time::FromUTCExploded(test.explode, &result)); + EXPECT_TRUE(result.is_null()); + EXPECT_FALSE(base::Time::FromLocalExploded(test.explode, &result)); + EXPECT_TRUE(result.is_null()); + } +} + // Specialized test fixture allowing time strings without timezones to be // tested by comparing them to a known time in the local zone. // See also pr_time_unittests.cc @@ -80,7 +126,8 @@ TEST_F(TimeTest, TimeT) { EXPECT_EQ(tms.tm_sec, exploded.second); // Convert exploded back to the time struct. - Time our_time_2 = Time::FromLocalExploded(exploded); + Time our_time_2; + EXPECT_TRUE(Time::FromLocalExploded(exploded, &our_time_2)); EXPECT_TRUE(our_time_1 == our_time_2); time_t now_t_2 = our_time_2.ToTimeT(); @@ -119,7 +166,8 @@ TEST_F(TimeTest, FromExplodedWithMilliseconds) { Time::Exploded exploded1 = {0}; now.UTCExplode(&exploded1); exploded1.millisecond = 500; - Time time = Time::FromUTCExploded(exploded1); + Time time; + EXPECT_TRUE(Time::FromUTCExploded(exploded1, &time)); Time::Exploded exploded2 = {0}; time.UTCExplode(&exploded2); EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); @@ -137,7 +185,8 @@ TEST_F(TimeTest, LocalExplode) { Time::Exploded exploded; a.LocalExplode(&exploded); - Time b = Time::FromLocalExploded(exploded); + Time b; + EXPECT_TRUE(Time::FromLocalExploded(exploded, &b)); // The exploded structure doesn't have microseconds, and on Mac & Linux, the // internal OS conversion uses seconds, which will cause truncation. So we @@ -150,7 +199,8 @@ TEST_F(TimeTest, UTCExplode) { Time::Exploded exploded; a.UTCExplode(&exploded); - Time b = Time::FromUTCExploded(exploded); + Time b; + EXPECT_TRUE(Time::FromUTCExploded(exploded, &b)); EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); } @@ -370,7 +420,8 @@ TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { static char buffer[] = "TZ=America/Santiago"; putenv(buffer); tzset(); - Time t = Time::FromLocalExploded(midnight); + Time t; + EXPECT_TRUE(Time::FromLocalExploded(midnight, &t)); EXPECT_EQ(1381633200, t.ToTimeT()); } #endif // OS_ANDROID @@ -532,7 +583,8 @@ TEST(TimeDelta, WindowsEpoch) { exploded.minute = 0; exploded.second = 0; exploded.millisecond = 0; - Time t = Time::FromUTCExploded(exploded); + Time t; + EXPECT_TRUE(Time::FromUTCExploded(exploded, &t)); // Unix 1970 epoch. EXPECT_EQ(11644473600000000ll, t.ToInternalValue()); |