summaryrefslogtreecommitdiff
path: root/Tests/LibCrypto/TestASN1.cpp
blob: 6c766b787e5f16dea5954170d9b6d2f223b6b09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (c) 2022, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/StringView.h>
#include <LibCrypto/ASN1/ASN1.h>
#include <LibTest/TestCase.h>

#define EXPECT_DATETIME(sv, y, mo, d, h, mi, s) \
    EXPECT_EQ(Crypto::ASN1::parse_utc_time(sv).value(), Core::DateTime::create(y, mo, d, h, mi, s))

TEST_CASE(test_utc_boring)
{
    // YYMMDDhhmm[ss]Z
    EXPECT_DATETIME("010101010101Z"sv, 2001, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("010203040506Z"sv, 2001, 2, 3, 4, 5, 6);
    EXPECT_DATETIME("020406081012Z"sv, 2002, 4, 6, 8, 10, 12);
    EXPECT_DATETIME("0204060810Z"sv, 2002, 4, 6, 8, 10, 0);
    EXPECT_DATETIME("220911220000Z"sv, 2022, 9, 11, 22, 0, 0);
}

TEST_CASE(test_utc_year_rollover)
{
    // YYMMDDhhmm[ss]Z
    EXPECT_DATETIME("000101010101Z"sv, 2000, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("010101010101Z"sv, 2001, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("020101010101Z"sv, 2002, 1, 1, 1, 1, 1);
    // ...
    EXPECT_DATETIME("480101010101Z"sv, 2048, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("490101010101Z"sv, 2049, 1, 1, 1, 1, 1);
    // This Y2050-problem is hardcoded in the spec. Oh no.
    EXPECT_DATETIME("500101010101Z"sv, 1950, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("510101010101Z"sv, 1951, 1, 1, 1, 1, 1);
    // ...
    EXPECT_DATETIME("970101010101Z"sv, 1997, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("980101010101Z"sv, 1998, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("990101010101Z"sv, 1999, 1, 1, 1, 1, 1);
}

TEST_CASE(test_utc_offset)
{
    // YYMMDDhhmm[ss](+|-)hhmm
    // We don't yet support storing the offset anywhere and instead just assume that the offset is just +0000.
    EXPECT_DATETIME("010101010101+0000"sv, 2001, 1, 1, 1, 1, 1);
    EXPECT_DATETIME("010203040506+0000"sv, 2001, 2, 3, 4, 5, 6);
    EXPECT_DATETIME("020406081012+0000"sv, 2002, 4, 6, 8, 10, 12);
    EXPECT_DATETIME("0204060810+0000"sv, 2002, 4, 6, 8, 10, 0);
    EXPECT_DATETIME("220911220000+0000"sv, 2022, 9, 11, 22, 0, 0);
    // Designed to fail once we support offsets:
    EXPECT_DATETIME("220911220000+0600"sv, 2022, 9, 11, 22, 0, 0);
}

TEST_CASE(test_utc_missing_z)
{
    // YYMMDDhhmm[ss]
    // We don't actually need to parse this correctly; rejecting these inputs is fine.
    // This test just makes sure that we don't crash.
    // FIXME: This currently crashes
    // (void)Crypto::ASN1::parse_utc_time("010101010101"sv);
    // (void)Crypto::ASN1::parse_utc_time("010203040506"sv);
    // (void)Crypto::ASN1::parse_utc_time("020406081012"sv);
    // (void)Crypto::ASN1::parse_utc_time("0204060810"sv);
    // (void)Crypto::ASN1::parse_utc_time("220911220000"sv);
}