2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Tests for X509 time functions */
15 #include <openssl/asn1.h>
16 #include <openssl/x509.h>
18 #include "test_main.h"
25 /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
29 static TESTDATA x509_cmp_tests
[] = {
31 "20170217180154Z", V_ASN1_GENERALIZEDTIME
,
32 /* The same in seconds since epoch. */
36 "20170217180154Z", V_ASN1_GENERALIZEDTIME
,
37 /* One second more. */
41 "20170217180154Z", V_ASN1_GENERALIZEDTIME
,
42 /* One second less. */
45 /* Same as UTC time. */
47 "170217180154Z", V_ASN1_UTCTIME
,
48 /* The same in seconds since epoch. */
52 "170217180154Z", V_ASN1_UTCTIME
,
53 /* One second more. */
57 "170217180154Z", V_ASN1_UTCTIME
,
58 /* One second less. */
61 /* UTCTime from the 20th century. */
63 "990217180154Z", V_ASN1_UTCTIME
,
64 /* The same in seconds since epoch. */
68 "990217180154Z", V_ASN1_UTCTIME
,
69 /* One second more. */
73 "990217180154Z", V_ASN1_UTCTIME
,
74 /* One second less. */
77 /* Various invalid formats. */
80 "20170217180154", V_ASN1_GENERALIZEDTIME
, 0, 0,
83 /* No trailing Z, UTCTime. */
84 "170217180154", V_ASN1_UTCTIME
, 0, 0,
88 "201702171801Z", V_ASN1_GENERALIZEDTIME
, 0, 0,
91 /* No seconds, UTCTime. */
92 "1702171801Z", V_ASN1_UTCTIME
, 0, 0,
95 /* Fractional seconds. */
96 "20170217180154.001Z", V_ASN1_GENERALIZEDTIME
, 0, 0,
99 /* Fractional seconds, UTCTime. */
100 "170217180154.001Z", V_ASN1_UTCTIME
, 0, 0,
103 /* Timezone offset. */
104 "20170217180154+0100", V_ASN1_GENERALIZEDTIME
, 0, 0,
107 /* Timezone offset, UTCTime. */
108 "170217180154+0100", V_ASN1_UTCTIME
, 0, 0,
112 "2017021718015400Z", V_ASN1_GENERALIZEDTIME
, 0, 0,
115 /* Extra digits, UTCTime. */
116 "17021718015400Z", V_ASN1_UTCTIME
, 0, 0,
120 "2017021718015aZ", V_ASN1_GENERALIZEDTIME
, 0, 0,
123 /* Non-digits, UTCTime. */
124 "17021718015aZ", V_ASN1_UTCTIME
, 0, 0,
127 /* Trailing garbage. */
128 "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME
, 0, 0,
131 /* Trailing garbage, UTCTime. */
132 "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME
, 0, 0,
136 "20170217180154Z", V_ASN1_UTCTIME
, 0, 0,
140 "170217180154Z", V_ASN1_GENERALIZEDTIME
, 0, 0,
144 "20170217180154Z", V_ASN1_OCTET_STRING
, 0, 0,
148 static int test_x509_cmp_time(int idx
)
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
);
158 result
= X509_cmp_time(&t
, &x509_cmp_tests
[idx
].cmp_time
);
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",
161 idx
, x509_cmp_tests
[idx
].expected
, result
);
167 static int test_x509_cmp_time_current()
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;
174 asn1_before
= ASN1_TIME_adj(NULL
, now
, -1, 0);
175 asn1_after
= ASN1_TIME_adj(NULL
, now
, 1, 0);
177 cmp_result
= X509_cmp_time(asn1_before
, NULL
);
178 if (!TEST_int_eq(cmp_result
, -1))
181 cmp_result
= X509_cmp_time(asn1_after
, NULL
);
182 if (!TEST_int_eq(cmp_result
, 1))
185 ASN1_TIME_free(asn1_before
);
186 ASN1_TIME_free(asn1_after
);
191 void register_tests()
193 ADD_TEST(test_x509_cmp_time_current
);
194 ADD_ALL_TESTS(test_x509_cmp_time
, OSSL_NELEM(x509_cmp_tests
));