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