2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Time tests for the asn1 module */
15 #include <openssl/asn1.h>
16 #include <openssl/evp.h>
17 #include <openssl/objects.h>
19 #include "internal/nelem.h"
22 char *data
; /* TIME string value */
23 int type
; /* GENERALIZED OR UTC */
24 int expected_type
; /* expected type after set/set_string_gmt */
25 int check_result
; /* check result */
26 time_t t
; /* expected time_t*/
27 int cmp_result
; /* compariston to baseline result */
28 int convert_result
; /* convertion result */
31 static struct testdata tbl_testdata_pos
[] = {
32 { "0", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, }, /* Bad time */
33 { "ABCD", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
34 { "0ABCD", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
35 { "1-700101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
36 { "`9700101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
37 { "19700101000000Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 0, 0, 0, 0, },
38 { "A00101000000Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 0, 0, 0, 0, },
39 { "A9700101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
40 { "1A700101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
41 { "19A00101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
42 { "197A0101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
43 { "1970A101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
44 { "19700A01000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
45 { "197001A1000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
46 { "1970010A000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
47 { "19700101A00000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
48 { "197001010A0000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
49 { "1970010100A000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
50 { "19700101000A00Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
51 { "197001010000A0Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
52 { "1970010100000AZ", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
53 { "700101000000X", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 0, 0, 0, 0, },
54 { "19700101000000X", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 0, 0, 0, 0, },
55 { "19700101000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 0, -1, 1, }, /* Epoch begins */
56 { "700101000000Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 0, -1, 1, }, /* ditto */
57 { "20380119031407Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
58 { "380119031407Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 0x7FFFFFFF, 1, 1, },
59 { "20371231235959Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 2145916799, 1, 1, }, /* Just before 2038 */
60 { "20371231235959Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 0, 0, 0, 1, }, /* Bad UTC time */
61 { "371231235959Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 2145916799, 1, 1, },
62 { "19701006121456Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 24063296, -1, 1, },
63 { "701006121456Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 24063296, -1, 1, },
64 { "19991231000000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, }, /* Match baseline */
65 { "199912310000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, }, /* In various flavors */
66 { "991231000000Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
67 { "9912310000Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
68 { "9912310000+0000", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
69 { "199912310000+0000", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
70 { "9912310000-0000", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
71 { "199912310000-0000", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
72 { "199912310100+0100", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
73 { "199912302300-0100", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
74 { "199912302300-A000", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 0, 946598400, 0, 1, },
75 { "199912302300-0A00", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 0, 946598400, 0, 1, },
76 { "9912310100+0100", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
77 { "9912302300-0100", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, 946598400, 0, 1, },
80 /* ASSUMES SIGNED TIME_T */
81 static struct testdata tbl_testdata_neg
[] = {
82 { "19011213204552Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 1, INT_MIN
, -1, 0, },
83 { "691006121456Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, -7472704, -1, 1, },
84 { "19691006121456Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, -7472704, -1, 1, },
87 /* explicit casts to time_t short warnings on systems with 32-bit time_t */
88 static struct testdata tbl_testdata_pos_64bit
[] = {
89 { "20380119031408Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, (time_t)0x80000000, 1, 1, },
90 { "20380119031409Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_UTCTIME
, 1, (time_t)0x80000001, 1, 1, },
91 { "380119031408Z", V_ASN1_UTCTIME
, V_ASN1_UTCTIME
, 1, (time_t)0x80000000, 1, 1, },
92 { "20500101120000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 1, (time_t)0x967b1ec0, 1, 0, },
95 /* ASSUMES SIGNED TIME_T */
96 static struct testdata tbl_testdata_neg_64bit
[] = {
97 { "19011213204551Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 1, (time_t)-2147483649LL, -1, 0, },
98 { "19000101120000Z", V_ASN1_GENERALIZEDTIME
, V_ASN1_GENERALIZEDTIME
, 1, (time_t)-2208945600LL, -1, 0, },
101 /* A baseline time to compare to */
102 static ASN1_TIME gtime
= {
104 V_ASN1_GENERALIZEDTIME
,
105 (unsigned char*)"19991231000000Z",
108 static time_t gtime_t
= 946598400;
110 static int test_table(struct testdata
*tbl
, int idx
)
115 struct testdata
*td
= &tbl
[idx
];
118 atime
.data
= (unsigned char*)td
->data
;
119 atime
.length
= strlen((char*)atime
.data
);
120 atime
.type
= td
->type
;
123 if (!TEST_int_eq(ASN1_TIME_check(&atime
), td
->check_result
)) {
124 TEST_info("ASN1_TIME_check(%s) unexpected result", atime
.data
);
127 if (td
->check_result
== 0)
130 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime
, td
->t
), 0)) {
131 TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime
.data
, (long)td
->t
);
135 if (!TEST_true(ASN1_TIME_diff(&day
, &sec
, &atime
, &atime
))) {
136 TEST_info("ASN1_TIME_diff(%s) to self failed", atime
.data
);
139 if (!TEST_int_eq(day
, 0) || !TEST_int_eq(sec
, 0)) {
140 TEST_info("ASN1_TIME_diff(%s) to self not equal", atime
.data
);
144 if (!TEST_true(ASN1_TIME_diff(&day
, &sec
, >ime
, &atime
))) {
145 TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime
.data
);
147 } else if (!((td
->cmp_result
== 0 && TEST_true((day
== 0 && sec
== 0))) ||
148 (td
->cmp_result
== -1 && TEST_true((day
< 0 || sec
< 0))) ||
149 (td
->cmp_result
== 1 && TEST_true((day
> 0 || sec
> 0))))) {
150 TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime
.data
);
154 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime
, gtime_t
), td
->cmp_result
)) {
155 TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime
.data
);
159 ptime
= ASN1_TIME_set(NULL
, td
->t
);
160 if (!TEST_ptr(ptime
)) {
161 TEST_info("ASN1_TIME_set(%ld) failed", (long)td
->t
);
165 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime
, td
->t
), 0)) {
166 TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
167 (long)td
->t
, td
->data
, ptime
->data
);
168 local_error
= error
= 1;
170 if (!TEST_int_eq(ptime
->type
, td
->expected_type
)) {
171 TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td
->t
);
172 local_error
= error
= 1;
175 TEST_info("ASN1_TIME_set() = %*s", ptime
->length
, ptime
->data
);
176 ASN1_TIME_free(ptime
);
179 ptime
= ASN1_TIME_new();
180 if (!TEST_ptr(ptime
)) {
181 TEST_info("ASN1_TIME_new() failed");
185 if (!TEST_int_eq(ASN1_TIME_set_string(ptime
, td
->data
), td
->check_result
)) {
186 TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td
->data
);
187 local_error
= error
= 1;
189 if (!TEST_int_eq(ASN1_TIME_normalize(ptime
), td
->check_result
)) {
190 TEST_info("ASN1_TIME_normalize(%s) failed", td
->data
);
191 local_error
= error
= 1;
193 if (!TEST_int_eq(ptime
->type
, td
->expected_type
)) {
194 TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td
->data
);
195 local_error
= error
= 1;
198 if (!TEST_true(ASN1_TIME_diff(&day
, &sec
, ptime
, &atime
)) || !TEST_int_eq(day
, 0) || !TEST_int_eq(sec
, 0)) {
199 TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day
, sec
, td
->data
);
200 local_error
= error
= 1;
202 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime
, gtime_t
), td
->cmp_result
)) {
203 TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td
->data
);
204 local_error
= error
= 1;
207 TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime
->length
, ptime
->data
);
208 ASN1_TIME_free(ptime
);
211 ptime
= ASN1_TIME_new();
212 if (!TEST_ptr(ptime
)) {
213 TEST_info("ASN1_TIME_new() failed");
217 if (!TEST_int_eq(ASN1_TIME_set_string(ptime
, td
->data
), td
->check_result
)) {
218 TEST_info("ASN1_TIME_set_string(%s) failed", td
->data
);
219 local_error
= error
= 1;
222 if (!TEST_true(ASN1_TIME_diff(&day
, &sec
, ptime
, &atime
)) || !TEST_int_eq(day
, 0) || !TEST_int_eq(sec
, 0)) {
223 TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day
, sec
, td
->data
);
224 local_error
= error
= 1;
226 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime
, gtime_t
), td
->cmp_result
)) {
227 TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td
->data
);
228 local_error
= error
= 1;
231 TEST_info("ASN1_TIME_set_string() = %*s", ptime
->length
, ptime
->data
);
232 ASN1_TIME_free(ptime
);
235 if (td
->type
== V_ASN1_UTCTIME
) {
236 ptime
= ASN1_TIME_to_generalizedtime(&atime
, NULL
);
237 if (td
->convert_result
== 1 && !TEST_ptr(ptime
)) {
238 TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime
.data
);
240 } else if (td
->convert_result
== 0 && !TEST_ptr_null(ptime
)) {
241 TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime
.data
);
244 if (ptime
!= NULL
&& !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime
, td
->t
), 0)) {
245 TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime
.data
, ptime
->data
);
248 ASN1_TIME_free(ptime
);
250 /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
253 TEST_error("atime=%s", atime
.data
);
258 static int test_table_pos(int idx
)
260 return test_table(tbl_testdata_pos
, idx
);
263 static int test_table_neg(int idx
)
265 return test_table(tbl_testdata_neg
, idx
);
268 static int test_table_pos_64bit(int idx
)
270 return test_table(tbl_testdata_pos_64bit
, idx
);
273 static int test_table_neg_64bit(int idx
)
275 return test_table(tbl_testdata_neg_64bit
, idx
);
278 struct compare_testdata
{
284 static unsigned char TODAY_GEN_STR
[] = "20170825000000Z";
285 static unsigned char TOMORROW_GEN_STR
[] = "20170826000000Z";
286 static unsigned char TODAY_UTC_STR
[] = "170825000000Z";
287 static unsigned char TOMORROW_UTC_STR
[] = "170826000000Z";
289 #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
290 #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
291 #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
292 #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
294 static struct compare_testdata tbl_compare_testdata
[] = {
295 { TODAY_GEN
, TODAY_GEN
, 0 },
296 { TODAY_GEN
, TODAY_UTC
, 0 },
297 { TODAY_GEN
, TOMORROW_GEN
, -1 },
298 { TODAY_GEN
, TOMORROW_UTC
, -1 },
300 { TODAY_UTC
, TODAY_GEN
, 0 },
301 { TODAY_UTC
, TODAY_UTC
, 0 },
302 { TODAY_UTC
, TOMORROW_GEN
, -1 },
303 { TODAY_UTC
, TOMORROW_UTC
, -1 },
305 { TOMORROW_GEN
, TODAY_GEN
, 1 },
306 { TOMORROW_GEN
, TODAY_UTC
, 1 },
307 { TOMORROW_GEN
, TOMORROW_GEN
, 0 },
308 { TOMORROW_GEN
, TOMORROW_UTC
, 0 },
310 { TOMORROW_UTC
, TODAY_GEN
, 1 },
311 { TOMORROW_UTC
, TODAY_UTC
, 1 },
312 { TOMORROW_UTC
, TOMORROW_GEN
, 0 },
313 { TOMORROW_UTC
, TOMORROW_UTC
, 0 }
316 static int test_table_compare(int idx
)
318 struct compare_testdata
*td
= &tbl_compare_testdata
[idx
];
320 return TEST_int_eq(ASN1_TIME_compare(&td
->t1
, &td
->t2
), td
->result
);
323 int setup_tests(void)
326 * On platforms where |time_t| is an unsigned integer, t will be a
329 * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
330 * because some compilers are picky if you do 't < 0', or even 't <= 0'
331 * if |t| is unsigned.
335 * On some platforms, |time_t| is signed, but a negative value is an
336 * error, and using it with gmtime() or localtime() generates a NULL.
337 * If that is the case, we can't perform tests on negative values.
339 struct tm
*ptm
= localtime(&t
);
341 ADD_ALL_TESTS(test_table_pos
, OSSL_NELEM(tbl_testdata_pos
));
342 if (!(t
> 0) && ptm
!= NULL
) {
343 TEST_info("Adding negative-sign time_t tests");
344 ADD_ALL_TESTS(test_table_neg
, OSSL_NELEM(tbl_testdata_neg
));
346 if (sizeof(time_t) > sizeof(uint32_t)) {
347 TEST_info("Adding 64-bit time_t tests");
348 ADD_ALL_TESTS(test_table_pos_64bit
, OSSL_NELEM(tbl_testdata_pos_64bit
));
350 if (!(t
> 0) && ptm
!= NULL
) {
351 TEST_info("Adding negative-sign 64-bit time_t tests");
352 ADD_ALL_TESTS(test_table_neg_64bit
, OSSL_NELEM(tbl_testdata_neg_64bit
));
356 ADD_ALL_TESTS(test_table_compare
, OSSL_NELEM(tbl_compare_testdata
));