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