Extend the BIO callback tests to check the return value semantics
[openssl.git] / test / asn1_time_test.c
1 /*
2  * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /* Time tests for the asn1 module */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <openssl/asn1.h>
16 #include <openssl/evp.h>
17 #include <openssl/objects.h>
18 #include "testutil.h"
19 #include "internal/nelem.h"
20
21 struct testdata {
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 */
29 };
30
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, },
78 };
79
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, },
85 };
86
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, },
93 };
94
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, },
99 };
100
101 /* A baseline time to compare to */
102 static ASN1_TIME gtime = {
103     15,
104     V_ASN1_GENERALIZEDTIME,
105     (unsigned char*)"19991231000000Z",
106     0
107 };
108 static time_t gtime_t = 946598400;
109
110 static int test_table(struct testdata *tbl, int idx)
111 {
112     int error = 0;
113     ASN1_TIME atime;
114     ASN1_TIME *ptime;
115     struct testdata *td = &tbl[idx];
116     int day, sec;
117
118     atime.data = (unsigned char*)td->data;
119     atime.length = strlen((char*)atime.data);
120     atime.type = td->type;
121     atime.flags = 0;
122
123     if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
124         TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
125         error = 1;
126     }
127     if (td->check_result == 0)
128         return 1;
129
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);
132         error = 1;
133     }
134
135     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
136         TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
137         error = 1;
138     }
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);
141         error = 1;
142     }
143
144     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
145         TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
146         error = 1;
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);
151         error = 1;
152     }
153
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);
156         error = 1;
157     }
158
159     ptime = ASN1_TIME_set(NULL, td->t);
160     if (!TEST_ptr(ptime)) {
161         TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
162         error = 1;
163     } else {
164         int local_error = 0;
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;
169         }
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;
173         }
174         if (local_error)
175             TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
176         ASN1_TIME_free(ptime);
177     }
178
179     ptime = ASN1_TIME_new();
180     if (!TEST_ptr(ptime)) {
181         TEST_info("ASN1_TIME_new() failed");
182         error = 1;
183     } else {
184         int local_error = 0;
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;
188         }
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;
192         }
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;
196         }
197         day = sec = 0;
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;
201         }
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;
205         }
206         if (local_error)
207             TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
208         ASN1_TIME_free(ptime);
209     }
210
211     ptime = ASN1_TIME_new();
212     if (!TEST_ptr(ptime)) {
213         TEST_info("ASN1_TIME_new() failed");
214         error = 1;
215     } else {
216         int local_error = 0;
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;
220         }
221         day = sec = 0;
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;
225         }
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;
229         }
230         if (local_error)
231             TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
232         ASN1_TIME_free(ptime);
233     }
234
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);
239             error = 1;
240         } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
241             TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
242             error = 1;
243         }
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);
246             error = 1;
247         }
248         ASN1_TIME_free(ptime);
249     }
250     /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
251
252     if (error)
253         TEST_error("atime=%s", atime.data);
254
255     return !error;
256 }
257
258 static int test_table_pos(int idx)
259 {
260     return test_table(tbl_testdata_pos, idx);
261 }
262
263 static int test_table_neg(int idx)
264 {
265     return test_table(tbl_testdata_neg, idx);
266 }
267
268 static int test_table_pos_64bit(int idx)
269 {
270     return test_table(tbl_testdata_pos_64bit, idx);
271 }
272
273 static int test_table_neg_64bit(int idx)
274 {
275     return test_table(tbl_testdata_neg_64bit, idx);
276 }
277
278 struct compare_testdata {
279     ASN1_TIME t1;
280     ASN1_TIME t2;
281     int result;
282 };
283
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";
288
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 }
293
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 },
299
300     { TODAY_UTC,    TODAY_GEN,     0 },
301     { TODAY_UTC,    TODAY_UTC,     0 },
302     { TODAY_UTC,    TOMORROW_GEN, -1 },
303     { TODAY_UTC,    TOMORROW_UTC, -1 },
304
305     { TOMORROW_GEN, TODAY_GEN,     1 },
306     { TOMORROW_GEN, TODAY_UTC,     1 },
307     { TOMORROW_GEN, TOMORROW_GEN,  0 },
308     { TOMORROW_GEN, TOMORROW_UTC,  0 },
309
310     { TOMORROW_UTC, TODAY_GEN,     1 },
311     { TOMORROW_UTC, TODAY_UTC,     1 },
312     { TOMORROW_UTC, TOMORROW_GEN,  0 },
313     { TOMORROW_UTC, TOMORROW_UTC,  0 }
314 };
315
316 static int test_table_compare(int idx)
317 {
318     struct compare_testdata *td = &tbl_compare_testdata[idx];
319
320     return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
321 }
322
323 int setup_tests(void)
324 {
325     /*
326      * On platforms where |time_t| is an unsigned integer, t will be a
327      * positive number.
328      *
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.
332      */
333     time_t t = -1;
334     /*
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.
338      */
339     struct tm *ptm = localtime(&t);
340
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));
345     }
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));
349 #ifndef __hpux
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));
353         }
354 #endif
355     }
356     ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
357     return 1;
358 }