File: s2n_asn1_time_test.c

package info (click to toggle)
aws-crt-python 0.16.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,328 kB
  • sloc: ansic: 330,743; python: 18,949; makefile: 6,271; sh: 3,712; asm: 754; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (223 lines) | stat: -rw-r--r-- 8,591 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include "utils/s2n_asn1_time.h"

#include <stdlib.h>
#include <time.h>

#include "s2n_test.h"

int main(int argc, char **argv)
{
    BEGIN_TEST();
    EXPECT_SUCCESS(s2n_disable_tls13_in_test());

    /* October 20, 2017 3:09:11 PM GMT-07:00 */
    uint64_t expected_ns = 1508539878000000000;

    /* test GMT date parses without the millis*/
    {
        const char *time_str = "20171020225118Z";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);
    }

    /* test GMT date parses */
    {
        const char *time_str = "20171020225118.999Z";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);
    }

    /* test zero offset date parses */
    {
        const char *time_str = "20171020225118.999+0000";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);
    }

    /* test 1:15 west offset date parses */
    {
        const char *time_str = "20171020213618.999-0115";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);
    }

    /* test 1:15 east offset date parses */
    {
        const char *time_str = "20171021000618.999+0115";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);
    }

    /* test invalid date fails */
    {
        const char *time_str = "201710210";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid tz character fails. */
    {
        const char *time_str = "20171020225118.999q";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid month fails. */
    {
        const char *time_str = "20171320225118.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid day fails. */
    {
        const char *time_str = "20171032225118.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid hour fails. */
    {
        const char *time_str = "20171020255118.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid minute fails. */
    {
        const char *time_str = "20171020226118.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test invalid second fails. */
    {
        const char *time_str = "20171020225161.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* test empty fails */
    {
        const char *time_str = "";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* now run a test where we are certain UTC is not the timezone used, but make sure it still converts.
     * Also note, the moment timezones come into play, so does daylight savings time. So there are two tests here, across the timezone boundaries.*/
    {
        char *tz = getenv("TZ");
        setenv("TZ", "US/Pacific", 1);
        tzset();
        const char *dst_time_str = "20171020225118.999Z";
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(dst_time_str, strlen(dst_time_str), &timestamp));
        EXPECT_EQUAL(expected_ns, timestamp);

        uint64_t non_dst_stamp = 1510610608000000000;
        const char *non_dst_str = "20171113220328.999Z";
        timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(non_dst_str, strlen(non_dst_str), &timestamp));
        EXPECT_EQUAL(non_dst_stamp, timestamp);
        if (tz) {
            setenv("TZ", tz, 1);
        }
        tzset();
    }

    /* make sure a leap-year date works */
    {
        const char *leap_yr = "20200229220328.999Z";
        uint64_t leap_yr_stamp = 1583013808000000000;
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(leap_yr, strlen(leap_yr), &timestamp));
        EXPECT_EQUAL(leap_yr_stamp, timestamp);
    }

    /* make sure a leap-year date on a non-leap year works */
    {
        const char *non_leap_yr = "20170229220328.999Z";
        uint64_t non_leap_yr_stamp = 1488405808000000000;
        uint64_t timestamp = 0;
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(non_leap_yr, strlen(non_leap_yr), &timestamp));
        EXPECT_EQUAL(non_leap_yr_stamp, timestamp);
    }

    /* test non digit character fails */
    {
        const char *time_str = "2017102B225118.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_INVALID_ARGUMENT);
    }

    /* Test Epoch timestamp in UTC */
    {
        const char *time_str = "19700101000000.000Z";
        uint64_t timestamp = 1; /* Initial assignment must be non-zero, so we know if it was set correctly. */
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(0, timestamp);
    }

    /* Test Epoch timestamp with 1:15 east offset */
    {
        const char *time_str = "19700101011500.000+0115";
        uint64_t timestamp = 1; /* Initial assignment must be non-zero, so we know if it was set correctly. */
        EXPECT_OK(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp));
        EXPECT_EQUAL(0, timestamp);
    }

    /* Note: We cannot use the function to convert a timestamp from before Epoch with an offset,
     * even if the adjusted time falls after the Epoch, e.g. "19691231224500.000-0115", because
     * mktime() will fail. */

    /* Test UTC time before Epoch fails */
    {
        const char *time_str = "19691231235959.999Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_SAFETY);
    }

    /* Test time from way before Epoch*/
    {
        const char *time_str = "19680101000000.000Z";
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_SAFETY);
    }

    /* Test time before Epoch with east offset fails */
    {
        const char *time_str = "19700101011500.000+0116"; /* One minute before Epoch */
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_SAFETY);
    }

    /* Test time before Epoch with west offset fails */
    {
        const char *time_str = "19691231224400.000-0115"; /* One minute before Epoch */
        uint64_t timestamp = 0;
        EXPECT_ERROR_WITH_ERRNO(s2n_asn1_time_to_nano_since_epoch_ticks(time_str, strlen(time_str), &timestamp), S2N_ERR_SAFETY);
    }

    END_TEST();
}