xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/test/asn1_time_test.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos /*
2*4724848cSchristos  * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos  *
4*4724848cSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4724848cSchristos  * this file except in compliance with the License.  You can obtain a copy
6*4724848cSchristos  * in the file LICENSE in the source distribution or at
7*4724848cSchristos  * https://www.openssl.org/source/license.html
8*4724848cSchristos  */
9*4724848cSchristos 
10*4724848cSchristos /* Time tests for the asn1 module */
11*4724848cSchristos 
12*4724848cSchristos #include <stdio.h>
13*4724848cSchristos #include <string.h>
14*4724848cSchristos 
15*4724848cSchristos #include <openssl/asn1.h>
16*4724848cSchristos #include <openssl/evp.h>
17*4724848cSchristos #include <openssl/objects.h>
18*4724848cSchristos #include "testutil.h"
19*4724848cSchristos #include "internal/nelem.h"
20*4724848cSchristos 
21*4724848cSchristos struct testdata {
22*4724848cSchristos     char *data;             /* TIME string value */
23*4724848cSchristos     int type;               /* GENERALIZED OR UTC */
24*4724848cSchristos     int expected_type;      /* expected type after set/set_string_gmt */
25*4724848cSchristos     int check_result;       /* check result */
26*4724848cSchristos     time_t t;               /* expected time_t*/
27*4724848cSchristos     int cmp_result;         /* comparison to baseline result */
28*4724848cSchristos     int convert_result;     /* conversion result */
29*4724848cSchristos };
30*4724848cSchristos 
31*4724848cSchristos static struct testdata tbl_testdata_pos[] = {
32*4724848cSchristos     { "0",                 V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, }, /* Bad time */
33*4724848cSchristos     { "ABCD",              V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
34*4724848cSchristos     { "0ABCD",             V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
35*4724848cSchristos     { "1-700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
36*4724848cSchristos     { "`9700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
37*4724848cSchristos     { "19700101000000Z",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
38*4724848cSchristos     { "A00101000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
39*4724848cSchristos     { "A9700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
40*4724848cSchristos     { "1A700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
41*4724848cSchristos     { "19A00101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
42*4724848cSchristos     { "197A0101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
43*4724848cSchristos     { "1970A101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
44*4724848cSchristos     { "19700A01000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
45*4724848cSchristos     { "197001A1000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
46*4724848cSchristos     { "1970010A000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
47*4724848cSchristos     { "19700101A00000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
48*4724848cSchristos     { "197001010A0000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
49*4724848cSchristos     { "1970010100A000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
50*4724848cSchristos     { "19700101000A00Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
51*4724848cSchristos     { "197001010000A0Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
52*4724848cSchristos     { "1970010100000AZ",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
53*4724848cSchristos     { "700101000000X",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
54*4724848cSchristos     { "19700101000000X",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
55*4724848cSchristos     { "19700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,           0, -1, 1, }, /* Epoch begins */
56*4724848cSchristos     { "700101000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,           0, -1, 1, }, /* ditto */
57*4724848cSchristos     { "20380119031407Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  0x7FFFFFFF,  1, 1, }, /* Max 32bit time_t */
58*4724848cSchristos     { "380119031407Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  0x7FFFFFFF,  1, 1, },
59*4724848cSchristos     { "20371231235959Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  2145916799,  1, 1, }, /* Just before 2038 */
60*4724848cSchristos     { "20371231235959Z",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 1, }, /* Bad UTC time */
61*4724848cSchristos     { "371231235959Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  2145916799,  1, 1, },
62*4724848cSchristos     { "19701006121456Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,    24063296, -1, 1, },
63*4724848cSchristos     { "701006121456Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,    24063296, -1, 1, },
64*4724848cSchristos     { "19991231000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, }, /* Match baseline */
65*4724848cSchristos     { "199912310000Z",     V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, }, /* In various flavors */
66*4724848cSchristos     { "991231000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
67*4724848cSchristos     { "9912310000Z",       V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
68*4724848cSchristos     { "9912310000+0000",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
69*4724848cSchristos     { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
70*4724848cSchristos     { "9912310000-0000",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
71*4724848cSchristos     { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
72*4724848cSchristos     { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
73*4724848cSchristos     { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
74*4724848cSchristos     { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         0,   946598400,  0, 1, },
75*4724848cSchristos     { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         0,   946598400,  0, 1, },
76*4724848cSchristos     { "9912310100+0100",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
77*4724848cSchristos     { "9912302300-0100",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
78*4724848cSchristos };
79*4724848cSchristos 
80*4724848cSchristos /* ASSUMES SIGNED TIME_T */
81*4724848cSchristos static struct testdata tbl_testdata_neg[] = {
82*4724848cSchristos     { "19011213204552Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1,     INT_MIN, -1, 0, },
83*4724848cSchristos     { "691006121456Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,    -7472704, -1, 1, },
84*4724848cSchristos     { "19691006121456Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,    -7472704, -1, 1, },
85*4724848cSchristos };
86*4724848cSchristos 
87*4724848cSchristos /* explicit casts to time_t short warnings on systems with 32-bit time_t */
88*4724848cSchristos static struct testdata tbl_testdata_pos_64bit[] = {
89*4724848cSchristos     { "20380119031408Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  (time_t)0x80000000,  1, 1, },
90*4724848cSchristos     { "20380119031409Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  (time_t)0x80000001,  1, 1, },
91*4724848cSchristos     { "380119031408Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  (time_t)0x80000000,  1, 1, },
92*4724848cSchristos     { "20500101120000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1,  (time_t)0x967b1ec0,  1, 0, },
93*4724848cSchristos };
94*4724848cSchristos 
95*4724848cSchristos /* ASSUMES SIGNED TIME_T */
96*4724848cSchristos static struct testdata tbl_testdata_neg_64bit[] = {
97*4724848cSchristos     { "19011213204551Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
98*4724848cSchristos     { "19000101120000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
99*4724848cSchristos };
100*4724848cSchristos 
101*4724848cSchristos /* A baseline time to compare to */
102*4724848cSchristos static ASN1_TIME gtime = {
103*4724848cSchristos     15,
104*4724848cSchristos     V_ASN1_GENERALIZEDTIME,
105*4724848cSchristos     (unsigned char*)"19991231000000Z",
106*4724848cSchristos     0
107*4724848cSchristos };
108*4724848cSchristos static time_t gtime_t = 946598400;
109*4724848cSchristos 
test_table(struct testdata * tbl,int idx)110*4724848cSchristos static int test_table(struct testdata *tbl, int idx)
111*4724848cSchristos {
112*4724848cSchristos     int error = 0;
113*4724848cSchristos     ASN1_TIME atime;
114*4724848cSchristos     ASN1_TIME *ptime;
115*4724848cSchristos     struct testdata *td = &tbl[idx];
116*4724848cSchristos     int day, sec;
117*4724848cSchristos 
118*4724848cSchristos     atime.data = (unsigned char*)td->data;
119*4724848cSchristos     atime.length = strlen((char*)atime.data);
120*4724848cSchristos     atime.type = td->type;
121*4724848cSchristos     atime.flags = 0;
122*4724848cSchristos 
123*4724848cSchristos     if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
124*4724848cSchristos         TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
125*4724848cSchristos         error = 1;
126*4724848cSchristos     }
127*4724848cSchristos     if (td->check_result == 0)
128*4724848cSchristos         return 1;
129*4724848cSchristos 
130*4724848cSchristos     if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
131*4724848cSchristos         TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
132*4724848cSchristos         error = 1;
133*4724848cSchristos     }
134*4724848cSchristos 
135*4724848cSchristos     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
136*4724848cSchristos         TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
137*4724848cSchristos         error = 1;
138*4724848cSchristos     }
139*4724848cSchristos     if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
140*4724848cSchristos         TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
141*4724848cSchristos         error = 1;
142*4724848cSchristos     }
143*4724848cSchristos 
144*4724848cSchristos     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
145*4724848cSchristos         TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
146*4724848cSchristos         error = 1;
147*4724848cSchristos     } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
148*4724848cSchristos                  (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
149*4724848cSchristos                  (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
150*4724848cSchristos         TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
151*4724848cSchristos         error = 1;
152*4724848cSchristos     }
153*4724848cSchristos 
154*4724848cSchristos     if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
155*4724848cSchristos         TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
156*4724848cSchristos         error = 1;
157*4724848cSchristos     }
158*4724848cSchristos 
159*4724848cSchristos     ptime = ASN1_TIME_set(NULL, td->t);
160*4724848cSchristos     if (!TEST_ptr(ptime)) {
161*4724848cSchristos         TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
162*4724848cSchristos         error = 1;
163*4724848cSchristos     } else {
164*4724848cSchristos         int local_error = 0;
165*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
166*4724848cSchristos             TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
167*4724848cSchristos                     (long)td->t, td->data, ptime->data);
168*4724848cSchristos             local_error = error = 1;
169*4724848cSchristos         }
170*4724848cSchristos         if (!TEST_int_eq(ptime->type, td->expected_type)) {
171*4724848cSchristos             TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
172*4724848cSchristos             local_error = error = 1;
173*4724848cSchristos         }
174*4724848cSchristos         if (local_error)
175*4724848cSchristos             TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
176*4724848cSchristos         ASN1_TIME_free(ptime);
177*4724848cSchristos     }
178*4724848cSchristos 
179*4724848cSchristos     ptime = ASN1_TIME_new();
180*4724848cSchristos     if (!TEST_ptr(ptime)) {
181*4724848cSchristos         TEST_info("ASN1_TIME_new() failed");
182*4724848cSchristos         error = 1;
183*4724848cSchristos     } else {
184*4724848cSchristos         int local_error = 0;
185*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
186*4724848cSchristos             TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
187*4724848cSchristos             local_error = error = 1;
188*4724848cSchristos         }
189*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
190*4724848cSchristos             TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
191*4724848cSchristos             local_error = error = 1;
192*4724848cSchristos         }
193*4724848cSchristos         if (!TEST_int_eq(ptime->type, td->expected_type)) {
194*4724848cSchristos             TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
195*4724848cSchristos             local_error = error = 1;
196*4724848cSchristos         }
197*4724848cSchristos         day = sec = 0;
198*4724848cSchristos         if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
199*4724848cSchristos             TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
200*4724848cSchristos             local_error = error = 1;
201*4724848cSchristos         }
202*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
203*4724848cSchristos             TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
204*4724848cSchristos             local_error = error = 1;
205*4724848cSchristos         }
206*4724848cSchristos         if (local_error)
207*4724848cSchristos             TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
208*4724848cSchristos         ASN1_TIME_free(ptime);
209*4724848cSchristos     }
210*4724848cSchristos 
211*4724848cSchristos     ptime = ASN1_TIME_new();
212*4724848cSchristos     if (!TEST_ptr(ptime)) {
213*4724848cSchristos         TEST_info("ASN1_TIME_new() failed");
214*4724848cSchristos         error = 1;
215*4724848cSchristos     } else {
216*4724848cSchristos         int local_error = 0;
217*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
218*4724848cSchristos             TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
219*4724848cSchristos             local_error = error = 1;
220*4724848cSchristos         }
221*4724848cSchristos         day = sec = 0;
222*4724848cSchristos         if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
223*4724848cSchristos             TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
224*4724848cSchristos             local_error = error = 1;
225*4724848cSchristos         }
226*4724848cSchristos         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
227*4724848cSchristos             TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
228*4724848cSchristos             local_error = error = 1;
229*4724848cSchristos         }
230*4724848cSchristos         if (local_error)
231*4724848cSchristos             TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
232*4724848cSchristos         ASN1_TIME_free(ptime);
233*4724848cSchristos     }
234*4724848cSchristos 
235*4724848cSchristos     if (td->type == V_ASN1_UTCTIME) {
236*4724848cSchristos         ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
237*4724848cSchristos         if (td->convert_result == 1 && !TEST_ptr(ptime)) {
238*4724848cSchristos             TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
239*4724848cSchristos             error = 1;
240*4724848cSchristos         } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
241*4724848cSchristos             TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
242*4724848cSchristos             error = 1;
243*4724848cSchristos         }
244*4724848cSchristos         if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
245*4724848cSchristos             TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
246*4724848cSchristos             error = 1;
247*4724848cSchristos         }
248*4724848cSchristos         ASN1_TIME_free(ptime);
249*4724848cSchristos     }
250*4724848cSchristos     /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
251*4724848cSchristos 
252*4724848cSchristos     if (error)
253*4724848cSchristos         TEST_error("atime=%s", atime.data);
254*4724848cSchristos 
255*4724848cSchristos     return !error;
256*4724848cSchristos }
257*4724848cSchristos 
test_table_pos(int idx)258*4724848cSchristos static int test_table_pos(int idx)
259*4724848cSchristos {
260*4724848cSchristos     return test_table(tbl_testdata_pos, idx);
261*4724848cSchristos }
262*4724848cSchristos 
test_table_neg(int idx)263*4724848cSchristos static int test_table_neg(int idx)
264*4724848cSchristos {
265*4724848cSchristos     return test_table(tbl_testdata_neg, idx);
266*4724848cSchristos }
267*4724848cSchristos 
test_table_pos_64bit(int idx)268*4724848cSchristos static int test_table_pos_64bit(int idx)
269*4724848cSchristos {
270*4724848cSchristos     return test_table(tbl_testdata_pos_64bit, idx);
271*4724848cSchristos }
272*4724848cSchristos 
test_table_neg_64bit(int idx)273*4724848cSchristos static int test_table_neg_64bit(int idx)
274*4724848cSchristos {
275*4724848cSchristos     return test_table(tbl_testdata_neg_64bit, idx);
276*4724848cSchristos }
277*4724848cSchristos 
278*4724848cSchristos struct compare_testdata {
279*4724848cSchristos     ASN1_TIME t1;
280*4724848cSchristos     ASN1_TIME t2;
281*4724848cSchristos     int result;
282*4724848cSchristos };
283*4724848cSchristos 
284*4724848cSchristos static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
285*4724848cSchristos static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
286*4724848cSchristos static unsigned char TODAY_UTC_STR[] = "170825000000Z";
287*4724848cSchristos static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
288*4724848cSchristos 
289*4724848cSchristos #define TODAY_GEN    { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
290*4724848cSchristos #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
291*4724848cSchristos #define TODAY_UTC    { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
292*4724848cSchristos #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
293*4724848cSchristos 
294*4724848cSchristos static struct compare_testdata tbl_compare_testdata[] = {
295*4724848cSchristos     { TODAY_GEN,    TODAY_GEN,     0 },
296*4724848cSchristos     { TODAY_GEN,    TODAY_UTC,     0 },
297*4724848cSchristos     { TODAY_GEN,    TOMORROW_GEN, -1 },
298*4724848cSchristos     { TODAY_GEN,    TOMORROW_UTC, -1 },
299*4724848cSchristos 
300*4724848cSchristos     { TODAY_UTC,    TODAY_GEN,     0 },
301*4724848cSchristos     { TODAY_UTC,    TODAY_UTC,     0 },
302*4724848cSchristos     { TODAY_UTC,    TOMORROW_GEN, -1 },
303*4724848cSchristos     { TODAY_UTC,    TOMORROW_UTC, -1 },
304*4724848cSchristos 
305*4724848cSchristos     { TOMORROW_GEN, TODAY_GEN,     1 },
306*4724848cSchristos     { TOMORROW_GEN, TODAY_UTC,     1 },
307*4724848cSchristos     { TOMORROW_GEN, TOMORROW_GEN,  0 },
308*4724848cSchristos     { TOMORROW_GEN, TOMORROW_UTC,  0 },
309*4724848cSchristos 
310*4724848cSchristos     { TOMORROW_UTC, TODAY_GEN,     1 },
311*4724848cSchristos     { TOMORROW_UTC, TODAY_UTC,     1 },
312*4724848cSchristos     { TOMORROW_UTC, TOMORROW_GEN,  0 },
313*4724848cSchristos     { TOMORROW_UTC, TOMORROW_UTC,  0 }
314*4724848cSchristos };
315*4724848cSchristos 
test_table_compare(int idx)316*4724848cSchristos static int test_table_compare(int idx)
317*4724848cSchristos {
318*4724848cSchristos     struct compare_testdata *td = &tbl_compare_testdata[idx];
319*4724848cSchristos 
320*4724848cSchristos     return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
321*4724848cSchristos }
322*4724848cSchristos 
setup_tests(void)323*4724848cSchristos int setup_tests(void)
324*4724848cSchristos {
325*4724848cSchristos     /*
326*4724848cSchristos      * On platforms where |time_t| is an unsigned integer, t will be a
327*4724848cSchristos      * positive number.
328*4724848cSchristos      *
329*4724848cSchristos      * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
330*4724848cSchristos      * because some compilers are picky if you do 't < 0', or even 't <= 0'
331*4724848cSchristos      * if |t| is unsigned.
332*4724848cSchristos      */
333*4724848cSchristos     time_t t = -1;
334*4724848cSchristos     /*
335*4724848cSchristos      * On some platforms, |time_t| is signed, but a negative value is an
336*4724848cSchristos      * error, and using it with gmtime() or localtime() generates a NULL.
337*4724848cSchristos      * If that is the case, we can't perform tests on negative values.
338*4724848cSchristos      */
339*4724848cSchristos     struct tm *ptm = localtime(&t);
340*4724848cSchristos 
341*4724848cSchristos     ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
342*4724848cSchristos     if (!(t > 0) && ptm != NULL) {
343*4724848cSchristos         TEST_info("Adding negative-sign time_t tests");
344*4724848cSchristos         ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
345*4724848cSchristos     }
346*4724848cSchristos     if (sizeof(time_t) > sizeof(uint32_t)) {
347*4724848cSchristos         TEST_info("Adding 64-bit time_t tests");
348*4724848cSchristos         ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
349*4724848cSchristos #ifndef __hpux
350*4724848cSchristos         if (!(t > 0) && ptm != NULL) {
351*4724848cSchristos             TEST_info("Adding negative-sign 64-bit time_t tests");
352*4724848cSchristos             ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
353*4724848cSchristos         }
354*4724848cSchristos #endif
355*4724848cSchristos     }
356*4724848cSchristos     ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
357*4724848cSchristos     return 1;
358*4724848cSchristos }
359