Add tests for invalid datetimes in time_utils
Add unit tests for dateStringToEpoch method for various invalid datetime
strings and make sure that std::nullopt is returned as expected in each
test case.
Change-Id: I1eaa1beb471e02e7c11e575fa13bb175b48dc30d
Signed-off-by: Farah Rasheed <Farah.Rasheed1@dell.com>
diff --git a/test/redfish-core/include/utils/time_utils_test.cpp b/test/redfish-core/include/utils/time_utils_test.cpp
index a1ae058..3777208 100644
--- a/test/redfish-core/include/utils/time_utils_test.cpp
+++ b/test/redfish-core/include/utils/time_utils_test.cpp
@@ -176,5 +176,41 @@
std::nullopt);
}
+TEST(Utility, DateStringToEpochWithInvalidDateTimeFormats)
+{
+ // invalid month (13)
+ EXPECT_EQ(dateStringToEpoch("2024-13-01T12:00:00Z"), std::nullopt);
+
+ // invalid character for month
+ EXPECT_EQ(dateStringToEpoch("2024-X-01T12:00:00Z"), std::nullopt);
+
+ // invalid day (32)
+ EXPECT_EQ(dateStringToEpoch("2024-07-32T12:00:00Z"), std::nullopt);
+
+ // invalid character for day
+ EXPECT_EQ(dateStringToEpoch("2024-07-XT12:00:00Z"), std::nullopt);
+
+ // invalid hour (25)
+ EXPECT_EQ(dateStringToEpoch("2024-07-01T25:00:00Z"), std::nullopt);
+
+ // invalid character for hour
+ EXPECT_EQ(dateStringToEpoch("2024-07-01TX:00:00Z"), std::nullopt);
+
+ // invalid minute (60)
+ EXPECT_EQ(dateStringToEpoch("2024-07-01T12:60:00Z"), std::nullopt);
+
+ // invalid character for minute
+ EXPECT_EQ(dateStringToEpoch("2024-13-01T12:X:00Z"), std::nullopt);
+
+ // invalid second (60)
+ EXPECT_EQ(dateStringToEpoch("2024-07-01T12:00:XZ"), std::nullopt);
+
+ // invalid character for second
+ EXPECT_EQ(dateStringToEpoch("2024-13-01T12:00:00Z"), std::nullopt);
+
+ // invalid timezone
+ EXPECT_EQ(dateStringToEpoch("2024-07-01T12:00:00X"), std::nullopt);
+}
+
} // namespace
} // namespace redfish::time_utils