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