]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/asn1_time_test.c
aa1aa79ebbbca3900f8810e2b1e2bfaa7eca5cac
[thirdparty/openssl.git] / test / asn1_time_test.c
1 /*
2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 /* Time tests for the asn1 module */
11
12 #include <limits.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <crypto/asn1.h>
17 #include <openssl/asn1.h>
18 #include <openssl/evp.h>
19 #include <openssl/objects.h>
20 #include "testutil.h"
21 #include "internal/nelem.h"
22
23 struct testdata {
24 char *data; /* TIME string value */
25 int type; /* GENERALIZED OR UTC */
26 int expected_type; /* expected type after set/set_string_gmt */
27 int check_result; /* check result */
28 time_t t; /* expected time_t*/
29 int cmp_result; /* comparison to baseline result */
30 int convert_result; /* conversion result */
31 };
32
33 struct TESTDATA_asn1_to_utc {
34 char *input;
35 time_t expected;
36 };
37
38 static const struct TESTDATA_asn1_to_utc asn1_to_utc[] = {
39 {
40 /*
41 * last second of standard time in central Europe in 2021
42 * specified in GMT
43 */
44 "210328005959Z",
45 1616893199,
46 },
47 {
48 /*
49 * first second of daylight saving time in central Europe in 2021
50 * specified in GMT
51 */
52 "210328010000Z",
53 1616893200,
54 },
55 {
56 /*
57 * last second of standard time in central Europe in 2021
58 * specified in offset to GMT
59 */
60 "20210328015959+0100",
61 1616893199,
62 },
63 {
64 /*
65 * first second of daylight saving time in central Europe in 2021
66 * specified in offset to GMT
67 */
68 "20210328030000+0200",
69 1616893200,
70 },
71 {
72 /*
73 * Invalid strings should get -1 as a result
74 */
75 "INVALID",
76 -1,
77 },
78 };
79
80 static struct testdata tbl_testdata_pos[] = {
81 { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, /* Bad time */
82 { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
83 { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
84 { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
85 { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
86 { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
87 { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
88 { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
89 { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
90 { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
91 { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
92 { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
93 { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
94 { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
95 { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
96 { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
97 { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
98 { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
99 { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
100 { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
101 { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
102 { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
103 { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
104 { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* Epoch begins */
105 { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* ditto */
106 { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
107 { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, },
108 { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, /* Just before 2038 */
109 { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, /* Bad UTC time */
110 { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, },
111 { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
112 { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
113 { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* Match baseline */
114 { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* In various flavors */
115 { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
116 { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
117 { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
118 { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
119 { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
120 { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
121 { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
122 { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
123 { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
124 { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
125 { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
126 { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
127 };
128
129 /* ASSUMES SIGNED TIME_T */
130 static struct testdata tbl_testdata_neg[] = {
131 { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
132 { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
133 { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
134 };
135
136 /* explicit casts to time_t short warnings on systems with 32-bit time_t */
137 static struct testdata tbl_testdata_pos_64bit[] = {
138 { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
139 { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
140 { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
141 { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
142 };
143
144 /* ASSUMES SIGNED TIME_T */
145 static struct testdata tbl_testdata_neg_64bit[] = {
146 { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
147 { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
148 };
149
150 /* A baseline time to compare to */
151 static ASN1_TIME gtime = {
152 15,
153 V_ASN1_GENERALIZEDTIME,
154 (unsigned char*)"19991231000000Z",
155 0
156 };
157 static time_t gtime_t = 946598400;
158
159 static int test_table(struct testdata *tbl, int idx)
160 {
161 int error = 0;
162 ASN1_TIME atime;
163 ASN1_TIME *ptime;
164 struct testdata *td = &tbl[idx];
165 int day, sec;
166
167 atime.data = (unsigned char*)td->data;
168 atime.length = strlen((char*)atime.data);
169 atime.type = td->type;
170 atime.flags = 0;
171
172 if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
173 TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
174 error = 1;
175 }
176 if (td->check_result == 0)
177 return 1;
178
179 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
180 TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
181 error = 1;
182 }
183
184 if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
185 TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
186 error = 1;
187 }
188 if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
189 TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
190 error = 1;
191 }
192
193 if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
194 TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
195 error = 1;
196 } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
197 (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
198 (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
199 TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
200 error = 1;
201 }
202
203 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
204 TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
205 error = 1;
206 }
207
208 ptime = ASN1_TIME_set(NULL, td->t);
209 if (!TEST_ptr(ptime)) {
210 TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
211 error = 1;
212 } else {
213 int local_error = 0;
214 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
215 TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
216 (long)td->t, td->data, ptime->data);
217 local_error = error = 1;
218 }
219 if (!TEST_int_eq(ptime->type, td->expected_type)) {
220 TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
221 local_error = error = 1;
222 }
223 if (local_error)
224 TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
225 ASN1_TIME_free(ptime);
226 }
227
228 ptime = ASN1_TIME_new();
229 if (!TEST_ptr(ptime)) {
230 TEST_info("ASN1_TIME_new() failed");
231 error = 1;
232 } else {
233 int local_error = 0;
234 if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
235 TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
236 local_error = error = 1;
237 }
238 if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
239 TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
240 local_error = error = 1;
241 }
242 if (!TEST_int_eq(ptime->type, td->expected_type)) {
243 TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
244 local_error = error = 1;
245 }
246 day = sec = 0;
247 if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
248 TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
249 local_error = error = 1;
250 }
251 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
252 TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
253 local_error = error = 1;
254 }
255 if (local_error)
256 TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
257 ASN1_TIME_free(ptime);
258 }
259
260 ptime = ASN1_TIME_new();
261 if (!TEST_ptr(ptime)) {
262 TEST_info("ASN1_TIME_new() failed");
263 error = 1;
264 } else {
265 int local_error = 0;
266 if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
267 TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
268 local_error = error = 1;
269 }
270 day = sec = 0;
271 if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
272 TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
273 local_error = error = 1;
274 }
275 if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
276 TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
277 local_error = error = 1;
278 }
279 if (local_error)
280 TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
281 ASN1_TIME_free(ptime);
282 }
283
284 if (td->type == V_ASN1_UTCTIME) {
285 ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
286 if (td->convert_result == 1 && !TEST_ptr(ptime)) {
287 TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
288 error = 1;
289 } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
290 TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
291 error = 1;
292 }
293 if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
294 TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
295 error = 1;
296 }
297 ASN1_TIME_free(ptime);
298 }
299 /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
300
301 if (error)
302 TEST_error("atime=%s", atime.data);
303
304 return !error;
305 }
306
307 static int test_table_pos(int idx)
308 {
309 return test_table(tbl_testdata_pos, idx);
310 }
311
312 static int test_table_neg(int idx)
313 {
314 return test_table(tbl_testdata_neg, idx);
315 }
316
317 static int test_table_pos_64bit(int idx)
318 {
319 return test_table(tbl_testdata_pos_64bit, idx);
320 }
321
322 static int test_table_neg_64bit(int idx)
323 {
324 return test_table(tbl_testdata_neg_64bit, idx);
325 }
326
327 struct compare_testdata {
328 ASN1_TIME t1;
329 ASN1_TIME t2;
330 int result;
331 };
332
333 static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
334 static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
335 static unsigned char TODAY_UTC_STR[] = "170825000000Z";
336 static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
337
338 #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
339 #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
340 #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
341 #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
342
343 static struct compare_testdata tbl_compare_testdata[] = {
344 { TODAY_GEN, TODAY_GEN, 0 },
345 { TODAY_GEN, TODAY_UTC, 0 },
346 { TODAY_GEN, TOMORROW_GEN, -1 },
347 { TODAY_GEN, TOMORROW_UTC, -1 },
348
349 { TODAY_UTC, TODAY_GEN, 0 },
350 { TODAY_UTC, TODAY_UTC, 0 },
351 { TODAY_UTC, TOMORROW_GEN, -1 },
352 { TODAY_UTC, TOMORROW_UTC, -1 },
353
354 { TOMORROW_GEN, TODAY_GEN, 1 },
355 { TOMORROW_GEN, TODAY_UTC, 1 },
356 { TOMORROW_GEN, TOMORROW_GEN, 0 },
357 { TOMORROW_GEN, TOMORROW_UTC, 0 },
358
359 { TOMORROW_UTC, TODAY_GEN, 1 },
360 { TOMORROW_UTC, TODAY_UTC, 1 },
361 { TOMORROW_UTC, TOMORROW_GEN, 0 },
362 { TOMORROW_UTC, TOMORROW_UTC, 0 }
363 };
364
365 static int test_table_compare(int idx)
366 {
367 struct compare_testdata *td = &tbl_compare_testdata[idx];
368
369 return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
370 }
371
372 static int test_time_dup(void)
373 {
374 int ret = 0;
375 ASN1_TIME *asn1_time = NULL;
376 ASN1_TIME *asn1_time_dup = NULL;
377 ASN1_TIME *asn1_gentime = NULL;
378
379 asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
380 if (asn1_time == NULL) {
381 TEST_info("Internal error.");
382 goto err;
383 }
384
385 asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
386 if (asn1_gentime == NULL) {
387 TEST_info("Internal error.");
388 goto err;
389 }
390
391 asn1_time_dup = ASN1_TIME_dup(asn1_time);
392 if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
393 TEST_info("ASN1_TIME_dup() failed.");
394 goto err;
395 }
396 if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
397 TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
398 goto err;
399 }
400 ASN1_STRING_free(asn1_time_dup);
401
402 asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
403 if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
404 TEST_info("ASN1_UTCTIME_dup() failed.");
405 goto err;
406 }
407 if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
408 TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
409 goto err;
410 }
411 ASN1_STRING_free(asn1_time_dup);
412
413 asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
414 if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
415 TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
416 goto err;
417 }
418 if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
419 TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
420 goto err;
421 }
422
423 ret = 1;
424 err:
425 ASN1_STRING_free(asn1_time);
426 ASN1_STRING_free(asn1_gentime);
427 ASN1_STRING_free(asn1_time_dup);
428 return ret;
429 }
430
431 static int convert_asn1_to_time_t(int idx)
432 {
433 time_t testdateutc;
434
435 testdateutc = ossl_asn1_string_to_time_t(asn1_to_utc[idx].input);
436
437 if (!TEST_time_t_eq(testdateutc, asn1_to_utc[idx].expected)) {
438 TEST_info("ossl_asn1_string_to_time_t (%s) failed: expected %lli, got %lli\n",
439 asn1_to_utc[idx].input,
440 (long long int)asn1_to_utc[idx].expected,
441 (long long int)testdateutc);
442 return 0;
443 }
444 return 1;
445 }
446
447 /*
448 * this test is here to exercise ossl_asn1_time_from_tm
449 * with an integer year close to INT_MAX.
450 */
451 static int convert_tm_to_asn1_time(void)
452 {
453 /* we need 64 bit time_t */
454 #if ((ULONG_MAX >> 31) >> 31) >= 1
455 time_t t;
456 ASN1_TIME *at;
457
458 if (sizeof(time_t) * CHAR_BIT >= 64) {
459 t = 67768011791126057ULL;
460 at = ASN1_TIME_set(NULL, t);
461 /*
462 * If ASN1_TIME_set returns NULL, it means it could not handle the input
463 * which is fine for this edge case.
464 */
465 ASN1_STRING_free(at);
466 }
467 #endif
468 return 1;
469 }
470
471 int setup_tests(void)
472 {
473 /*
474 * On platforms where |time_t| is an unsigned integer, t will be a
475 * positive number.
476 *
477 * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
478 * because some compilers are picky if you do 't < 0', or even 't <= 0'
479 * if |t| is unsigned.
480 */
481 time_t t = -1;
482 /*
483 * On some platforms, |time_t| is signed, but a negative value is an
484 * error, and using it with gmtime() or localtime() generates a NULL.
485 * If that is the case, we can't perform tests on negative values.
486 */
487 struct tm *ptm = localtime(&t);
488
489 ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
490 if (!(t > 0) && ptm != NULL) {
491 TEST_info("Adding negative-sign time_t tests");
492 ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
493 }
494 if (sizeof(time_t) > sizeof(uint32_t)) {
495 TEST_info("Adding 64-bit time_t tests");
496 ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
497 #ifndef __hpux
498 if (!(t > 0) && ptm != NULL) {
499 TEST_info("Adding negative-sign 64-bit time_t tests");
500 ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
501 }
502 #endif
503 }
504 ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
505 ADD_TEST(test_time_dup);
506 ADD_ALL_TESTS(convert_asn1_to_time_t, OSSL_NELEM(asn1_to_utc));
507 ADD_TEST(convert_tm_to_asn1_time);
508 return 1;
509 }