summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2023-05-24 22:05:47 +0200
committerJelle Raaijmakers <jelle@gmta.nl>2023-05-27 12:17:50 +0200
commit815ea06d2c527d6827c4dfb4da3f734da90fb9b1 (patch)
tree0681446b9aeb1fa4c8e586ed3ea208e120f2c939
parente30709fca91715622a613fd5f1d998b760d441df (diff)
downloadserenity-815ea06d2c527d6827c4dfb4da3f734da90fb9b1.zip
AK: Test from_unix_time_parts intensively
-rw-r--r--Kernel/FileSystem/ISO9660FS/Inode.cpp2
-rw-r--r--Tests/AK/TestDuration.cpp118
2 files changed, 120 insertions, 0 deletions
diff --git a/Kernel/FileSystem/ISO9660FS/Inode.cpp b/Kernel/FileSystem/ISO9660FS/Inode.cpp
index 3f2d4f121b..1160e300bc 100644
--- a/Kernel/FileSystem/ISO9660FS/Inode.cpp
+++ b/Kernel/FileSystem/ISO9660FS/Inode.cpp
@@ -194,6 +194,8 @@ UnixDateTime ISO9660Inode::parse_numerical_date_time(ISO::NumericalDateAndTime c
i32 year_offset = date.years_since_1900 - 70;
// FIXME: This ignores timezone information in date.
+ // FIXME: This calls `VERIFY(month >= 1 && month <= 12)` (but irritatingly allows days 0 and 255),
+ // which means we can crash the kernel by inserting a CD with an invalid recording month.
return UnixDateTime::from_unix_time_parts(year_offset, date.month, date.day, date.hour, date.minute, date.second, 0);
}
diff --git a/Tests/AK/TestDuration.cpp b/Tests/AK/TestDuration.cpp
index d3f3b3dad6..261a5fdf2f 100644
--- a/Tests/AK/TestDuration.cpp
+++ b/Tests/AK/TestDuration.cpp
@@ -328,6 +328,43 @@ BENCHMARK_CASE(years_to_days_since_epoch_benchmark)
}
}
+TEST_CASE(days_since_epoch)
+{
+ EXPECT_EQ(days_since_epoch(1970, 1, 1), 0);
+ EXPECT_EQ(days_since_epoch(1970, 1, 2), 1);
+ EXPECT_EQ(days_since_epoch(1970, 2, 1), 31);
+ EXPECT_EQ(days_since_epoch(1970, 2, 27), 57);
+ EXPECT_EQ(days_since_epoch(1970, 2, 28), 58);
+ EXPECT_EQ(days_since_epoch(1970, 2, 29), 59); // doesn't really exist
+ EXPECT_EQ(days_since_epoch(1970, 3, 1), 59);
+ EXPECT_EQ(days_since_epoch(1971, 1, 1), 365);
+ EXPECT_EQ(days_since_epoch(1972, 1, 1), 730);
+ EXPECT_EQ(days_since_epoch(1972, 2, 1), 761);
+ EXPECT_EQ(days_since_epoch(1972, 2, 27), 787);
+ EXPECT_EQ(days_since_epoch(1972, 2, 28), 788);
+ EXPECT_EQ(days_since_epoch(1972, 2, 29), 789);
+ EXPECT_EQ(days_since_epoch(1972, 3, 1), 790);
+
+ // At least shouldn't crash:
+ EXPECT_EQ(days_since_epoch(1971, 1, 0), 364);
+ // FIXME shouldn't crash: EXPECT_EQ(days_since_epoch(1971, 0, 1), 334);
+ // FIXME shouldn't crash: EXPECT_EQ(days_since_epoch(1971, 0, 0), 333);
+ // FIXME shouldn't crash: EXPECT_EQ(days_since_epoch(1971, 13, 3), ???);
+
+ // I can't easily verify that these values are perfectly exact and correct, but they're close enough.
+ // Also, for these "years" the most important thing is to avoid crashing (i.e. signed overflow UB).
+ // Observe that these are very close to the naive guess of 365.2425 days per year.
+ EXPECT_EQ(days_since_epoch(0, 1, 1), -719528);
+ EXPECT_EQ(days_since_epoch(-1'000'000, 1, 1), -365962028);
+ EXPECT_EQ(days_since_epoch(-2'147'483'648, 1, 1), -784353015833); // Guess: 784353015832
+ EXPECT_EQ(days_since_epoch(1'000'000, 1, 1), 364522972);
+ EXPECT_EQ(days_since_epoch(2'147'483'647, 1, 1), 784351576412); // Guess: 784351576411
+ EXPECT_EQ(days_since_epoch(2'147'483'647, 12, 31), 784351576776); // Guess: 784351576777
+ EXPECT_EQ(days_since_epoch(2'147'483'647, 12, 255), 784351577000);
+ // FIXME shouldn't crash: EXPECT_EQ(days_since_epoch(2'147'483'647, 255, 255), 784351577000);
+ // FIXME: Restrict interface to only take sensible types, and ensure callers pass only sensible values for that type.
+}
+
TEST_CASE(div_floor_by)
{
EXPECT_EQ(AK::Detail::floor_div_by<4>(-5), -2);
@@ -490,3 +527,84 @@ TEST_CASE(user_defined_literals)
static_assert(100_ms <= 100'001_us, "LE UDL (lt)");
static_assert(1_sec != 2_sec, "NE UDL");
}
+
+TEST_CASE(from_unix_time_parts_common_values)
+{
+ // Non-negative "common" values.
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 0, 0, 0).offset_to_epoch(), 0, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 0, 0, 1).offset_to_epoch(), 0, 1'000'000);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 0, 0, 999).offset_to_epoch(), 0, 999'000'000);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 0, 1, 2).offset_to_epoch(), 1, 2'000'000);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 0, 59, 0).offset_to_epoch(), 59, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 1, 0, 0).offset_to_epoch(), 60, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 0, 59, 0, 0).offset_to_epoch(), 3540, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 1, 0, 0, 0).offset_to_epoch(), 3600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 1, 23, 0, 0, 0).offset_to_epoch(), 82800, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 2, 0, 0, 0, 0).offset_to_epoch(), 86400, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 2, 1, 0, 0, 0, 0).offset_to_epoch(), 2678400, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 2, 27, 0, 0, 0, 0).offset_to_epoch(), 4924800, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 2, 28, 0, 0, 0, 0).offset_to_epoch(), 5011200, 0);
+ // Note that this day does *not* exist:
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 2, 29, 0, 0, 0, 0).offset_to_epoch(), 5097600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 3, 0, 0, 0, 0, 0).offset_to_epoch(), 5011200, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 3, 1, 0, 0, 0, 0).offset_to_epoch(), 5097600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 3, 2, 0, 0, 0, 0).offset_to_epoch(), 5184000, 0);
+
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1971, 1, 1, 0, 0, 0, 0).offset_to_epoch(), 31536000, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 1, 1, 0, 0, 0, 0).offset_to_epoch(), 63072000, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 2, 1, 0, 0, 0, 0).offset_to_epoch(), 65750400, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 2, 27, 0, 0, 0, 0).offset_to_epoch(), 67996800, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 2, 28, 0, 0, 0, 0).offset_to_epoch(), 68083200, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 2, 29, 0, 0, 0, 0).offset_to_epoch(), 68169600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1972, 3, 1, 0, 0, 0, 0).offset_to_epoch(), 68256000, 0);
+
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(2023, 5, 24, 18, 44, 40, 0).offset_to_epoch(), 1684953880, 0);
+}
+
+TEST_CASE(from_unix_time_parts_negative)
+{
+ // Negative "common" values. These aren't really that well-defined, but we must make sure we don't crash.
+ // FIXME: We shouldn't VERIFY(), especially because this is used in the Kernel!
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 0, 23, 0, 0, 0).offset_to_epoch(), -3600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 1, 0, 24, 0, 0, 0).offset_to_epoch(), 0, 0);
+ // EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 0, 31, 0, 0, 0, 0).offset_to_epoch(), 0, 0);
+ EXPECT_CRASH("Month 0 currently not allowed", [] {
+ (void)UnixDateTime::from_unix_time_parts(1970, 0, 31, 0, 0, 0, 0);
+ return Test::Crash::Failure::DidNotCrash;
+ });
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 11, 30, 0, 0, 0, 0).offset_to_epoch(), 28771200, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 12, 1, 0, 0, 0, 0).offset_to_epoch(), 28857600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1970, 12, 31, 0, 0, 0, 0).offset_to_epoch(), 31449600, 0);
+ // EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1971, 0, 0, 0, 0, 0, 0).offset_to_epoch(), 28857600, 0);
+ EXPECT_CRASH("Month 0 currently not allowed", [] {
+ (void)UnixDateTime::from_unix_time_parts(1971, 0, 0, 0, 0, 0, 0);
+ return Test::Crash::Failure::DidNotCrash;
+ });
+ // EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1971, 0, 1, 0, 0, 0, 0).offset_to_epoch(), 28944000, 0);
+ EXPECT_CRASH("Month 0 currently not allowed", [] {
+ (void)UnixDateTime::from_unix_time_parts(1971, 0, 1, 0, 0, 0, 0);
+ return Test::Crash::Failure::DidNotCrash;
+ });
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1971, 1, 0, 0, 0, 0, 0).offset_to_epoch(), 31449600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1971, 1, 1, 0, 0, 0, 0).offset_to_epoch(), 31536000, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1969, 1, 1, 0, 0, 0, 0).offset_to_epoch(), -31536000, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 3, 1, 0, 0, 0, 0).offset_to_epoch(), -57974400, 0);
+ // Leap day!
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 2, 29, 0, 0, 0, 0).offset_to_epoch(), -58060800, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 2, 28, 0, 0, 0, 0).offset_to_epoch(), -58147200, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 2, 27, 0, 0, 0, 0).offset_to_epoch(), -58233600, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 2, 1, 0, 0, 0, 0).offset_to_epoch(), -60480000, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1968, 1, 1, 0, 0, 0, 0).offset_to_epoch(), -63158400, 0);
+ EXPECT_DURATION(UnixDateTime::from_unix_time_parts(1967, 1, 1, 0, 0, 0, 0).offset_to_epoch(), -94694400, 0);
+}
+
+TEST_CASE(from_milliseconds)
+{
+ EXPECT_DURATION(Duration::from_milliseconds(0), 0, 0);
+ EXPECT_DURATION(Duration::from_milliseconds(42), 0, 42'000'000);
+ EXPECT_DURATION(Duration::from_milliseconds(-1), -1, 999'000'000);
+ EXPECT_DURATION(Duration::from_milliseconds(-1'000'000'000), -1'000'000, 0);
+ EXPECT_DURATION(Duration::from_milliseconds(1'000'000'000), 1'000'000, 0);
+ EXPECT_DURATION(Duration::from_milliseconds(9223372036854775807), 9223372036854775, 807'000'000);
+ EXPECT_DURATION(Duration::from_milliseconds((i64)-0x8000'0000'0000'0000), -9223372036854776, 192'000'000);
+}