summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2022-09-11 22:55:33 +0200
committerAndreas Kling <kling@serenityos.org>2022-12-31 23:21:17 +0100
commit0aed7f1c8e93cb9121d526584cf657a9f7171279 (patch)
tree85090977b55783567433d707ede711cc77c20248 /Tests
parent0ca41d2813838872c469b11c9831d994bde4787b (diff)
downloadserenity-0aed7f1c8e93cb9121d526584cf657a9f7171279.zip
LibCrypto: Don't crash in ASN1::parse_generalized_time on missing 'Z'
Diffstat (limited to 'Tests')
-rw-r--r--Tests/LibCrypto/TestASN1.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/Tests/LibCrypto/TestASN1.cpp b/Tests/LibCrypto/TestASN1.cpp
index e18b61174d..3b752d8e4b 100644
--- a/Tests/LibCrypto/TestASN1.cpp
+++ b/Tests/LibCrypto/TestASN1.cpp
@@ -63,3 +63,105 @@ TEST_CASE(test_utc_missing_z)
(void)Crypto::ASN1::parse_utc_time("0204060810"sv);
(void)Crypto::ASN1::parse_utc_time("220911220000"sv);
}
+
+#undef EXPECT_DATETIME
+#define EXPECT_DATETIME(sv, y, mo, d, h, mi, s) \
+ EXPECT_EQ(Crypto::ASN1::parse_generalized_time(sv).value(), Core::DateTime::create(y, mo, d, h, mi, s))
+
+TEST_CASE(test_generalized_boring)
+{
+ // YYYYMMDDhh[mm[ss[.fff]]]
+ EXPECT_DATETIME("20010101010101Z"sv, 2001, 1, 1, 1, 1, 1);
+ EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("20020406081012Z"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("200204060810Z"sv, 2002, 4, 6, 8, 10, 0);
+ EXPECT_DATETIME("2002040608Z"sv, 2002, 4, 6, 8, 0, 0);
+ // TODO: We probably should not discard the milliseconds.
+ EXPECT_DATETIME("20020406081012.567Z"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("20220911220000Z"sv, 2022, 9, 11, 22, 0, 0);
+}
+
+TEST_CASE(test_generalized_offset)
+{
+ // YYYYMMDDhh[mm[ss[.fff]]](+|-)hhmm
+ // We don't yet support storing the offset anywhere and instead just assume that the offset is just +0000.
+ EXPECT_DATETIME("20010101010101+0000"sv, 2001, 1, 1, 1, 1, 1);
+ EXPECT_DATETIME("20010203040506+0000"sv, 2001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("20020406081012+0000"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("200204060810+0000"sv, 2002, 4, 6, 8, 10, 0);
+ EXPECT_DATETIME("2002040608+0000"sv, 2002, 4, 6, 8, 0, 0);
+ // TODO: We probably should not discard the milliseconds.
+ EXPECT_DATETIME("20020406081012.567+0000"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("20220911220000+0000"sv, 2022, 9, 11, 22, 0, 0);
+ // Designed to fail once we support offsets:
+ EXPECT_DATETIME("20220911220000+0600"sv, 2022, 9, 11, 22, 0, 0);
+}
+
+TEST_CASE(test_generalized_missing_z)
+{
+ // YYYYMMDDhh[mm[ss[.fff]]]
+ EXPECT_DATETIME("20010101010101"sv, 2001, 1, 1, 1, 1, 1);
+ EXPECT_DATETIME("20010203040506"sv, 2001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("20020406081012"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("200204060810"sv, 2002, 4, 6, 8, 10, 0);
+ EXPECT_DATETIME("2002040608"sv, 2002, 4, 6, 8, 0, 0);
+ // TODO: We probably should not discard the milliseconds.
+ EXPECT_DATETIME("20020406081012.567"sv, 2002, 4, 6, 8, 10, 12);
+ EXPECT_DATETIME("20220911220000"sv, 2022, 9, 11, 22, 0, 0);
+}
+
+TEST_CASE(test_generalized_unusual_year)
+{
+ // Towards the positive
+ EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("20110203040506Z"sv, 2011, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("21010203040506Z"sv, 2101, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("30010203040506Z"sv, 3001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("40010203040506Z"sv, 4001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("90010203040506Z"sv, 9001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("99990203040506Z"sv, 9999, 2, 3, 4, 5, 6);
+
+ // Towards zero
+ EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("19990203040506Z"sv, 1999, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("19500203040506Z"sv, 1950, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("19010203040506Z"sv, 1901, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("18010203040506Z"sv, 1801, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("15010203040506Z"sv, 1501, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("10010203040506Z"sv, 1001, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("01010203040506Z"sv, 101, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("00110203040506Z"sv, 11, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("00010203040506Z"sv, 1, 2, 3, 4, 5, 6);
+ EXPECT_DATETIME("00000203040506Z"sv, 0, 2, 3, 4, 5, 6);
+
+ // Problematic dates
+ EXPECT_DATETIME("20200229040506Z"sv, 2020, 2, 29, 4, 5, 6);
+ EXPECT_DATETIME("20000229040506Z"sv, 2000, 2, 29, 4, 5, 6);
+ EXPECT_DATETIME("24000229040506Z"sv, 2400, 2, 29, 4, 5, 6);
+}
+
+TEST_CASE(test_generalized_nonexistent_dates)
+{
+ // The following dates don't exist. I'm not sure what the "correct" result is,
+ // but we need to make sure that we don't crash.
+ (void)Crypto::ASN1::parse_generalized_time("20210229040506Z"sv); // Not a leap year (not divisible by 4)
+ (void)Crypto::ASN1::parse_generalized_time("21000229040506Z"sv); // Not a leap year (divisible by 100)
+ (void)Crypto::ASN1::parse_generalized_time("20220230040506Z"sv); // Never exists
+ (void)Crypto::ASN1::parse_generalized_time("20220631040506Z"sv); // Never exists
+ (void)Crypto::ASN1::parse_generalized_time("20220732040506Z"sv); // Never exists
+
+ // https://www.timeanddate.com/calendar/julian-gregorian-switch.html
+ (void)Crypto::ASN1::parse_generalized_time("15821214040506Z"sv); // Gregorian switch; France
+ (void)Crypto::ASN1::parse_generalized_time("15821011040506Z"sv); // Gregorian switch; Italy, Poland, Portugal, Spain
+ (void)Crypto::ASN1::parse_generalized_time("15830105040506Z"sv); // Gregorian switch; Germany (Catholic)
+ (void)Crypto::ASN1::parse_generalized_time("15831011040506Z"sv); // Gregorian switch; Austria
+ (void)Crypto::ASN1::parse_generalized_time("15871026040506Z"sv); // Gregorian switch; Hungary
+ (void)Crypto::ASN1::parse_generalized_time("16100826040506Z"sv); // Gregorian switch; Germany (old Prussia)
+ (void)Crypto::ASN1::parse_generalized_time("17000223040506Z"sv); // Gregorian switch; Germany (Protestant)
+ (void)Crypto::ASN1::parse_generalized_time("17520908040506Z"sv); // Gregorian switch; US, Canada, UK
+ (void)Crypto::ASN1::parse_generalized_time("18711225040506Z"sv); // Gregorian switch; Japan
+ (void)Crypto::ASN1::parse_generalized_time("19160407040506Z"sv); // Gregorian switch; Bulgaria
+ (void)Crypto::ASN1::parse_generalized_time("19180207040506Z"sv); // Gregorian switch; Estonia, Russia
+ (void)Crypto::ASN1::parse_generalized_time("19230222040506Z"sv); // Gregorian switch; Greece
+ (void)Crypto::ASN1::parse_generalized_time("19261224040506Z"sv); // Gregorian switch; Turkey
+}