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