summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2015-09-25 16:12:30 +0000
committerjsing <>2015-09-25 16:12:30 +0000
commit7cde7bd1f58e1043f515e7b33b358fe89477b273 (patch)
treed1232fc6e044861e2e299aa951f7c7d4f9803c48
parentb6def5032708273b2c52cd6ebf78eacaf4e64592 (diff)
downloadopenbsd-7cde7bd1f58e1043f515e7b33b358fe89477b273.tar.gz
openbsd-7cde7bd1f58e1043f515e7b33b358fe89477b273.tar.bz2
openbsd-7cde7bd1f58e1043f515e7b33b358fe89477b273.zip
Add initial regress tests for ASN.1 times.
-rw-r--r--src/regress/lib/libcrypto/Makefile3
-rw-r--r--src/regress/lib/libcrypto/asn1/Makefile21
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1time.c349
3 files changed, 372 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/Makefile b/src/regress/lib/libcrypto/Makefile
index 3b84856435..4931d0fdd8 100644
--- a/src/regress/lib/libcrypto/Makefile
+++ b/src/regress/lib/libcrypto/Makefile
@@ -1,8 +1,9 @@
1# $OpenBSD: Makefile,v 1.21 2015/09/14 01:45:03 doug Exp $ 1# $OpenBSD: Makefile,v 1.22 2015/09/25 16:12:30 jsing Exp $
2 2
3SUBDIR= \ 3SUBDIR= \
4 aead \ 4 aead \
5 aeswrap \ 5 aeswrap \
6 asn1 \
6 base64 \ 7 base64 \
7 bf \ 8 bf \
8 bio \ 9 bio \
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile
new file mode 100644
index 0000000000..f7a2df5459
--- /dev/null
+++ b/src/regress/lib/libcrypto/asn1/Makefile
@@ -0,0 +1,21 @@
1# $OpenBSD: Makefile,v 1.1 2015/09/25 16:12:30 jsing Exp $
2
3TESTS = \
4 asn1time
5
6REGRESS_TARGETS= all_tests
7
8LDADD= -lcrypto
9DPADD= ${LIBCRYPTO} ${LIBSSL}
10WARNINGS= Yes
11LDFLAGS+= -lcrypto
12CFLAGS+= -DLIBRESSL_INTERNAL -Wall -Wundef -Werror
13
14CLEANFILES+= ${TESTS}
15
16all_tests: ${TESTS}
17 @for test in $>; do \
18 ./$$test; \
19 done
20
21.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/asn1/asn1time.c b/src/regress/lib/libcrypto/asn1/asn1time.c
new file mode 100644
index 0000000000..342fcf5779
--- /dev/null
+++ b/src/regress/lib/libcrypto/asn1/asn1time.c
@@ -0,0 +1,349 @@
1/* $OpenBSD: asn1time.c,v 1.1 2015/09/25 16:12:30 jsing Exp $ */
2/*
3 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/asn1.h>
19
20#include <err.h>
21#include <stdio.h>
22#include <string.h>
23
24struct asn1_time_test {
25 const char *str;
26 const char *data;
27 time_t time;
28};
29
30struct asn1_time_test asn1_invtime_tests[] = {
31 {
32 .str = "",
33 },
34 {
35 .str = "2015",
36 },
37 {
38 .str = "201509",
39 },
40 {
41 .str = "20150923",
42 },
43 {
44 .str = "20150923032700",
45 },
46 {
47 .str = "20150923032700.Z",
48 },
49 {
50 .str = "20150923032700.123",
51 },
52 {
53 .str = "20150923032700+1100Z",
54 },
55 {
56 .str = "20150923032700-11001",
57 },
58 {
59 /* UTC time cannot have fractional seconds. */
60 .str = "150923032700.123Z",
61 },
62 {
63 .str = "aaaaaaaaaaaaaaZ",
64 },
65};
66
67struct asn1_time_test asn1_gentime_tests[] = {
68 {
69 .str = "19700101000000Z",
70 .data = "19700101000000Z",
71 .time = 0,
72 },
73 {
74 .str = "20150923032700Z",
75 .data = "20150923032700Z",
76 .time = 1442978820,
77 },
78 {
79 .str = "20150923032700.22-0700",
80 .data = "20150923102700Z",
81 .time = 1443004020,
82 },
83 {
84 .str = "20150923032712+1100",
85 .data = "20150922162712Z",
86 .time = 1442939232,
87 },
88 {
89 .str = "20150923032712+1115",
90 .data = "20150922161212Z",
91 .time = 1442938332,
92 },
93 {
94 .str = "20150923032700.12345678Z",
95 .data = "20150923032700Z",
96 .time = 1442978820,
97 },
98};
99
100struct asn1_time_test asn1_utctime_tests[] = {
101 {
102 .str = "7001010000Z",
103 .data = "700101000000Z",
104 .time = 0,
105 },
106 {
107 .str = "150923032700Z",
108 .data = "150923032700Z",
109 .time = 1442978820,
110 },
111 {
112 .str = "150923032700-0700",
113 .data = "150923102700Z",
114 .time = 1443004020,
115 },
116 {
117 .str = "150923032712+1100",
118 .data = "150922162712Z",
119 .time = 1442939232,
120 },
121};
122
123#define N_INVTIME_TESTS \
124 (sizeof(asn1_invtime_tests) / sizeof(*asn1_invtime_tests))
125#define N_GENTIME_TESTS \
126 (sizeof(asn1_gentime_tests) / sizeof(*asn1_gentime_tests))
127#define N_UTCTIME_TESTS \
128 (sizeof(asn1_utctime_tests) / sizeof(*asn1_utctime_tests))
129
130static int
131asn1_invtime_test(int test_no, struct asn1_time_test *att)
132{
133 ASN1_GENERALIZEDTIME *gt = NULL;
134 ASN1_UTCTIME *ut = NULL;
135 ASN1_TIME *t = NULL;
136 int failure = 1;
137
138 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
139 goto done;
140 if ((ut = ASN1_UTCTIME_new()) == NULL)
141 goto done;
142 if ((t = ASN1_TIME_new()) == NULL)
143 goto done;
144
145 if (ASN1_GENERALIZEDTIME_set_string(gt, att->str) != 0) {
146 fprintf(stderr, "FAIL: test %i - successfully set "
147 "GENERALIZEDTIME string '%s'\n", test_no, att->str);
148 goto done;
149 }
150 if (ASN1_UTCTIME_set_string(ut, att->str) != 0) {
151 fprintf(stderr, "FAIL: test %i - successfully set UTCTIME "
152 "string '%s'\n", test_no, att->str);
153 goto done;
154 }
155 if (ASN1_UTCTIME_set_string(ut, att->str) != 0) {
156 fprintf(stderr, "FAIL: test %i - successfully set TIME "
157 "string '%s'\n", test_no, att->str);
158 goto done;
159 }
160
161 failure = 0;
162
163 done:
164 ASN1_GENERALIZEDTIME_free(gt);
165 ASN1_UTCTIME_free(ut);
166 ASN1_TIME_free(t);
167
168 return (failure);
169}
170
171static int
172asn1_gentime_test(int test_no, struct asn1_time_test *att)
173{
174 ASN1_GENERALIZEDTIME *gt;
175 int failure = 1;
176 int length;
177
178 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
179 goto done;
180
181 if (ASN1_GENERALIZEDTIME_set_string(gt, att->str) != 1) {
182 fprintf(stderr, "FAIL: test %i - failed to set string '%s'\n",
183 test_no, att->str);
184 goto done;
185 }
186
187 /* ASN.1 preserves the original input. */
188 length = strlen(att->str);
189 if (gt->length != length) {
190 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
191 test_no, gt->length, length);
192 goto done;
193 }
194 if (strncmp(gt->data, att->str, length) != 0) {
195 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
196 test_no, gt->data, att->str);
197 goto done;
198 }
199
200 ASN1_GENERALIZEDTIME_free(gt);
201
202 if ((gt = ASN1_GENERALIZEDTIME_set(NULL, att->time)) == NULL) {
203 fprintf(stderr, "FAIL: test %i - failed to set time %lli\n",
204 test_no, (long long)att->time);
205 goto done;
206 }
207 length = strlen(att->data);
208 if (gt->length != length) {
209 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
210 test_no, gt->length, length);
211 goto done;
212 }
213 if (strncmp(gt->data, att->data, length) != 0) {
214 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
215 test_no, gt->data, att->data);
216 goto done;
217 }
218
219 failure = 0;
220
221 done:
222 ASN1_GENERALIZEDTIME_free(gt);
223
224 return (failure);
225}
226
227static int
228asn1_utctime_test(int test_no, struct asn1_time_test *att)
229{
230 ASN1_UTCTIME *ut;
231 int failure = 1;
232 int length;
233
234 if ((ut = ASN1_UTCTIME_new()) == NULL)
235 goto done;
236
237 if (ASN1_UTCTIME_set_string(ut, att->str) != 1) {
238 fprintf(stderr, "FAIL: test %i - failed to set string '%s'\n",
239 test_no, att->str);
240 goto done;
241 }
242
243 /* ASN.1 preserves the original input. */
244 length = strlen(att->str);
245 if (ut->length != length) {
246 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
247 test_no, ut->length, length);
248 goto done;
249 }
250 if (strncmp(ut->data, att->str, length) != 0) {
251 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
252 test_no, ut->data, att->str);
253 goto done;
254 }
255
256 ASN1_UTCTIME_free(ut);
257
258 if ((ut = ASN1_UTCTIME_set(NULL, att->time)) == NULL) {
259 fprintf(stderr, "FAIL: test %i - failed to set time %lli\n",
260 test_no, (long long)att->time);
261 goto done;
262 }
263 length = strlen(att->data);
264 if (ut->length != length) {
265 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
266 test_no, ut->length, length);
267 goto done;
268 }
269 if (strncmp(ut->data, att->data, length) != 0) {
270 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
271 test_no, ut->data, att->data);
272 goto done;
273 }
274
275 failure = 0;
276
277 done:
278 ASN1_UTCTIME_free(ut);
279
280 return (failure);
281}
282
283static int
284asn1_time_test(int test_no, struct asn1_time_test *att, int type)
285{
286 ASN1_TIME *t = NULL;
287 int failure = 1;
288
289 if ((t = ASN1_TIME_new()) == NULL)
290 goto done;
291
292 if (ASN1_TIME_set_string(t, att->str) != 1) {
293 fprintf(stderr, "FAIL: test %i - failed to set string '%s'\n",
294 test_no, att->str);
295 goto done;
296 }
297
298 if (t->type != type) {
299 fprintf(stderr, "FAIL: test %i - got type %i, want %i\n",
300 test_no, t->type, type);
301 goto done;
302 }
303
304 failure = 0;
305
306 done:
307
308 ASN1_TIME_free(t);
309
310 return (failure);
311}
312
313int
314main(int argc, char **argv)
315{
316 struct asn1_time_test *att;
317 int failed = 0;
318 size_t i;
319
320 fprintf(stderr, "Invalid time tests...\n");
321 for (i = 0; i < N_INVTIME_TESTS; i++) {
322 att = &asn1_invtime_tests[i];
323 failed |= asn1_invtime_test(i, att);
324 }
325
326 fprintf(stderr, "GENERALIZEDTIME tests...\n");
327 for (i = 0; i < N_GENTIME_TESTS; i++) {
328 att = &asn1_gentime_tests[i];
329 failed |= asn1_gentime_test(i, att);
330 }
331
332 fprintf(stderr, "UTCTIME tests...\n");
333 for (i = 0; i < N_UTCTIME_TESTS; i++) {
334 att = &asn1_utctime_tests[i];
335 failed |= asn1_utctime_test(i, att);
336 }
337
338 fprintf(stderr, "TIME tests...\n");
339 for (i = 0; i < N_UTCTIME_TESTS; i++) {
340 att = &asn1_utctime_tests[i];
341 failed |= asn1_time_test(i, att, V_ASN1_UTCTIME);
342 }
343 for (i = 0; i < N_GENTIME_TESTS; i++) {
344 att = &asn1_gentime_tests[i];
345 failed |= asn1_time_test(i, att, V_ASN1_GENERALIZEDTIME);
346 }
347
348 return (failed);
349}