Adapt all test programs
[openssl.git] / test / x509_time_test.c
1 /*
2  * Copyright 2017 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 /* Tests for X509 time functions */
11
12 #include <string.h>
13 #include <time.h>
14
15 #include <openssl/asn1.h>
16 #include <openssl/x509.h>
17 #include "testutil.h"
18 #include "e_os.h"
19
20 typedef struct {
21     const char *data;
22     int type;
23     time_t cmp_time;
24     /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
25     int expected;
26 } TESTDATA;
27
28 static TESTDATA x509_cmp_tests[] = {
29     {
30         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
31         /* The same in seconds since epoch. */
32         1487354514, -1,
33     },
34     {
35         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
36         /* One second more. */
37         1487354515, -1,
38     },
39     {
40         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
41         /* One second less. */
42         1487354513, 1,
43     },
44     /* Same as UTC time. */
45     {
46         "170217180154Z", V_ASN1_UTCTIME,
47         /* The same in seconds since epoch. */
48         1487354514, -1,
49     },
50     {
51         "170217180154Z", V_ASN1_UTCTIME,
52         /* One second more. */
53         1487354515, -1,
54     },
55     {
56         "170217180154Z", V_ASN1_UTCTIME,
57         /* One second less. */
58         1487354513, 1,
59     },
60     /* UTCTime from the 20th century. */
61     {
62         "990217180154Z", V_ASN1_UTCTIME,
63         /* The same in seconds since epoch. */
64         919274514, -1,
65     },
66     {
67         "990217180154Z", V_ASN1_UTCTIME,
68         /* One second more. */
69         919274515, -1,
70     },
71     {
72         "990217180154Z", V_ASN1_UTCTIME,
73         /* One second less. */
74         919274513, 1,
75     },
76     /* Various invalid formats. */
77     {
78         /* No trailing Z. */
79         "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
80     },
81     {
82         /* No trailing Z, UTCTime. */
83         "170217180154", V_ASN1_UTCTIME, 0, 0,
84     },
85     {
86         /* No seconds. */
87         "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
88     },
89     {
90         /* No seconds, UTCTime. */
91         "1702171801Z", V_ASN1_UTCTIME, 0, 0,
92     },
93     {
94         /* Fractional seconds. */
95         "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
96     },
97     {
98         /* Fractional seconds, UTCTime. */
99         "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
100     },
101     {
102         /* Timezone offset. */
103         "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
104     },
105     {
106         /* Timezone offset, UTCTime. */
107         "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
108     },
109     {
110         /* Extra digits. */
111         "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
112     },
113     {
114         /* Extra digits, UTCTime. */
115         "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
116     },
117     {
118         /* Non-digits. */
119         "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
120     },
121     {
122         /* Non-digits, UTCTime. */
123         "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
124     },
125     {
126         /* Trailing garbage. */
127         "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
128     },
129     {
130         /* Trailing garbage, UTCTime. */
131         "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
132     },
133     {
134          /* Swapped type. */
135         "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
136     },
137     {
138         /* Swapped type. */
139         "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
140     },
141     {
142         /* Bad type. */
143         "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
144     },
145 };
146
147 static int test_x509_cmp_time(int idx)
148 {
149     ASN1_TIME t;
150     int result;
151
152     memset(&t, 0, sizeof(t));
153     t.type = x509_cmp_tests[idx].type;
154     t.data = (unsigned char*)(x509_cmp_tests[idx].data);
155     t.length = strlen(x509_cmp_tests[idx].data);
156
157     result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
158     if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
159         TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
160                 idx, x509_cmp_tests[idx].expected, result);
161         return 0;
162     }
163     return 1;
164 }
165
166 static int test_x509_cmp_time_current()
167 {
168     time_t now = time(NULL);
169     /* Pick a day earlier and later, relative to any system clock. */
170     ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
171     int cmp_result, failed = 0;
172
173     asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
174     asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
175
176     cmp_result  = X509_cmp_time(asn1_before, NULL);
177     if (!TEST_int_eq(cmp_result, -1))
178         failed = 1;
179
180     cmp_result = X509_cmp_time(asn1_after, NULL);
181     if (!TEST_int_eq(cmp_result, 1))
182         failed = 1;
183
184     ASN1_TIME_free(asn1_before);
185     ASN1_TIME_free(asn1_after);
186
187     return failed == 0;
188 }
189
190 void register_tests()
191 {
192     ADD_TEST(test_x509_cmp_time_current);
193     ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
194 }