unit-tests: Clean up memory in new asn1 unit tests
[strongswan.git] / src / libstrongswan / tests / suites / test_asn1.c
1 /*
2 * Copyright (C) 2013 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16
17 #include "test_suite.h"
18
19 #include <asn1/asn1.h>
20 #include <asn1/oid.h>
21 #include <utils/chunk.h>
22
23 /*******************************************************************************
24 * algorithm_identifier
25 */
26
27 START_TEST(test_asn1_algorithmIdentifier)
28 {
29 typedef struct {
30 int n;
31 chunk_t algid;
32 } testdata_t;
33
34 testdata_t test[] = {
35 { OID_ECDSA_WITH_SHA1, chunk_from_chars(0x30, 0x09, 0x06, 0x07,
36 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
37 { OID_SHA1_WITH_RSA, chunk_from_chars(0x30, 0x0d, 0x06, 0x09,
38 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00) },
39 };
40
41 chunk_t algid;
42 int i;
43
44 for (i = 0; i < countof(test); i++)
45 {
46 algid = asn1_algorithmIdentifier(test[i].n);
47 ck_assert(chunk_equals(algid, test[i].algid));
48 free(algid.ptr);
49 }
50 }
51 END_TEST
52
53 /*******************************************************************************
54 * parse_algorithm_identifier
55 */
56
57 START_TEST(test_asn1_parse_algorithmIdentifier)
58 {
59 typedef struct {
60 int alg;
61 bool empty;
62 chunk_t parameters;
63 } testdata_t;
64
65 testdata_t test[] = {
66 { OID_ECDSA_WITH_SHA1, TRUE, chunk_empty },
67 { OID_SHA1_WITH_RSA, TRUE, chunk_from_chars(0x05, 0x00) },
68 { OID_3DES_EDE_CBC, FALSE, chunk_from_chars(0x04, 0x01, 0xaa) },
69 { OID_PBKDF2, FALSE, chunk_from_chars(0x30, 0x01, 0xaa) }
70 };
71
72 chunk_t algid, parameters;
73 int i, alg;
74
75 for (i = 0; i < countof(test); i++)
76 {
77 algid = asn1_wrap(ASN1_SEQUENCE, "mc",
78 asn1_build_known_oid(test[i].alg), test[i].parameters);
79 parameters = chunk_empty;
80 if (i == 2)
81 {
82 alg = asn1_parse_algorithmIdentifier(algid, 0, NULL);
83 }
84 else
85 {
86 alg = asn1_parse_algorithmIdentifier(algid, 0, &parameters);
87 if (test[i].empty)
88 {
89 ck_assert(parameters.len == 0 && parameters.ptr == NULL);
90 }
91 else
92 {
93 ck_assert(chunk_equals(parameters, test[i].parameters));
94 }
95 }
96 ck_assert(alg == test[i].alg);
97 chunk_free(&algid);
98 }
99 }
100 END_TEST
101
102 /*******************************************************************************
103 * known_oid
104 */
105
106 START_TEST(test_asn1_known_oid)
107 {
108 typedef struct {
109 int n;
110 chunk_t oid;
111 } testdata_t;
112
113 testdata_t test[] = {
114 { OID_UNKNOWN, chunk_empty },
115 { OID_UNKNOWN, chunk_from_chars(0x55, 0x04, 0x02) },
116 { OID_COUNTRY, chunk_from_chars(0x55, 0x04, 0x06) },
117 { OID_STRONGSWAN, chunk_from_chars(0x2b, 0x06, 0x01, 0x04, 0x01,
118 0x82, 0xa0, 0x2a, 0x01) }
119 };
120
121 int i;
122
123 for (i = 0; i < countof(test); i++)
124 {
125 ck_assert(asn1_known_oid(test[i].oid) == test[i].n);
126 }
127 }
128 END_TEST
129
130 /*******************************************************************************
131 * build_known_oid
132 */
133
134 START_TEST(test_asn1_build_known_oid)
135 {
136 typedef struct {
137 int n;
138 chunk_t oid;
139 } testdata_t;
140
141 testdata_t test[] = {
142 { OID_UNKNOWN, chunk_empty },
143 { OID_MAX, chunk_empty },
144 { OID_COUNTRY, chunk_from_chars(0x06, 0x03, 0x55, 0x04, 0x06) },
145 { OID_STRONGSWAN, chunk_from_chars(0x06, 0x09, 0x2b, 0x06, 0x01, 0x04,
146 0x01, 0x82, 0xa0, 0x2a, 0x01) }
147 };
148
149 int i;
150 chunk_t oid = chunk_empty;
151
152 for (i = 0; i < countof(test); i++)
153 {
154 oid = asn1_build_known_oid(test[i].n);
155 if (test[i].oid.len == 0)
156 {
157 ck_assert(oid.len == 0 && oid.ptr == NULL);
158 }
159 else
160 {
161 ck_assert(chunk_equals(oid, test[i].oid));
162 chunk_free(&oid);
163 }
164 }
165 }
166 END_TEST
167
168 /*******************************************************************************
169 * oid_from_string
170 */
171
172 START_TEST(test_asn1_oid_from_string)
173 {
174 typedef struct {
175 char *string;
176 chunk_t oid;
177 } testdata_t;
178
179 testdata_t test[] = {
180 { "", chunk_empty },
181 { " ", chunk_empty },
182 { "0.2.262.1", chunk_from_chars(
183 0x02, 0x82, 0x06, 0x01) },
184 { "1.2.840.10045.4.1", chunk_from_chars(
185 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
186 { "1.3.6.1.4.1.36906.1", chunk_from_chars(
187 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
188 { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
189 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
190 { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
191 chunk_from_chars(0x0a,0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
192 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
193 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
194 { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
195 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
196 "2097153", chunk_from_chars(
197 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
198 0x81, 0x00, 0x81, 0x01, 0x81, 0x02, 0x81, 0x03, 0x81, 0x04,
199 0x81, 0x05, 0x81, 0x06, 0x81, 0x07, 0x81, 0x08, 0x81, 0x09,
200 0x82, 0x00, 0x82, 0x01, 0x82, 0x02, 0x82, 0x03, 0x82, 0x04,
201 0x82, 0x05, 0x82, 0x06, 0x82, 0x07, 0x82, 0x08, 0x82, 0x09,
202 0x83, 0x00, 0x83, 0x01, 0x83, 0x02, 0x83, 0x03, 0x83, 0x04,
203 0x81, 0x80, 0x80, 0x01) },
204 { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
205 "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
206 "1.2097153", chunk_empty },
207 { "1.a.2.b.3", chunk_empty }
208 };
209
210 int i;
211 chunk_t oid = chunk_empty;
212
213 for (i = 0; i < countof(test); i++)
214 {
215 oid = asn1_oid_from_string(test[i].string);
216 if (test[i].oid.len == 0)
217 {
218 ck_assert(oid.len == 0 && oid.ptr == NULL);
219 }
220 else
221 {
222 ck_assert(chunk_equals(oid, test[i].oid));
223 chunk_free(&oid);
224 }
225 }
226 }
227 END_TEST
228
229 /*******************************************************************************
230 * oid_to_string
231 */
232
233 START_TEST(test_asn1_oid_to_string)
234 {
235 typedef struct {
236 char *string;
237 chunk_t oid;
238 } testdata_t;
239
240 testdata_t test[] = {
241 { NULL, chunk_empty },
242 { "0.2.262.1", chunk_from_chars(
243 0x02, 0x82, 0x06, 0x01) },
244 { "1.2.840.10045.4.1", chunk_from_chars(
245 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
246 { "1.3.6.1.4.1.36906.1", chunk_from_chars(
247 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
248 { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
249 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
250 { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
251 chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
252 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
253 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
254 { NULL, chunk_from_chars(
255 0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
256 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
257 0xff, 0xff, 0xff, 0x7f) },
258 { NULL, chunk_from_chars(0x0a, 0x87) }
259 };
260
261 int i;
262 char *string = NULL;
263
264 for (i = 0; i < countof(test); i++)
265 {
266 string = asn1_oid_to_string(test[i].oid);
267 if (test[i].string == NULL)
268 {
269 ck_assert(string == NULL);
270 }
271 else
272 {
273 ck_assert(streq(string, test[i].string));
274 free(string);
275 }
276 }
277 }
278 END_TEST
279
280 /*******************************************************************************
281 * length
282 */
283
284 START_TEST(test_asn1_length)
285 {
286 chunk_t a;
287
288 a = chunk_empty;
289 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
290
291 a = chunk_from_chars(0x04);
292 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
293
294 a = chunk_from_chars(0x04, 0x00);
295 ck_assert(asn1_length(&a) == 0);
296
297 a = chunk_from_chars(0x04, 0x01);
298 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
299
300 a = chunk_from_chars(0x04, 0x01, 0xaa);
301 ck_assert(asn1_length(&a) == 1);
302
303 a = chunk_from_chars(0x04, 0x7f, 0xaa);
304 a.len = 2 + 127;
305 ck_assert(asn1_length(&a) == 127);
306
307 a = chunk_from_chars(0x04, 0x80, 0xaa);
308 a.len = 2 + 128;
309 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
310
311 a = chunk_from_chars(0x04, 0x81);
312 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
313
314 a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
315 ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
316
317 a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
318 a.len = 3 + 128;
319 ck_assert(asn1_length(&a) == 128);
320
321 a = chunk_from_chars(0x04, 0x82, 0x01, 0x02, 0xaa);
322 a.len = 4 + 258;
323 ck_assert(asn1_length(&a) == 258);
324
325 a = chunk_from_chars(0x04, 0x83, 0x01, 0x02, 0x03, 0xaa);
326 a.len = 5 + 66051;
327 ck_assert(asn1_length(&a) == 66051);
328 }
329 END_TEST
330
331 /*******************************************************************************
332 * unwrap
333 */
334
335 START_TEST(test_asn1_unwrap)
336 {
337 chunk_t c0 = chunk_from_chars(0x30);
338 chunk_t c1 = chunk_from_chars(0x30, 0x01, 0xaa);
339 chunk_t c2 = chunk_from_chars(0x30, 0x80);
340 chunk_t c3 = chunk_from_chars(0x30, 0x81);
341 chunk_t c4 = chunk_from_chars(0x30, 0x81, 0x01, 0xaa);
342 chunk_t c5 = chunk_from_chars(0x30, 0x81, 0x02, 0xaa);
343
344 chunk_t inner;
345 chunk_t inner_ref = chunk_from_chars(0xaa);
346
347 ck_assert(asn1_unwrap(&c0, &inner) == ASN1_INVALID);
348
349 ck_assert(asn1_unwrap(&c1, &inner) == ASN1_SEQUENCE);
350
351 ck_assert(chunk_equals(inner, inner_ref));
352
353 ck_assert(asn1_unwrap(&c2, &inner) == ASN1_INVALID);
354
355 ck_assert(asn1_unwrap(&c3, &inner) == ASN1_INVALID);
356
357 ck_assert(asn1_unwrap(&c4, &inner) == ASN1_SEQUENCE);
358
359 ck_assert(chunk_equals(inner, inner_ref));
360
361 ck_assert(asn1_unwrap(&c5, &inner) == ASN1_INVALID);
362 }
363 END_TEST
364
365 /*******************************************************************************
366 * is_asn1
367 */
368
369 START_TEST(test_is_asn1)
370 {
371 typedef struct {
372 bool asn1;
373 chunk_t chunk;
374 } testdata_t;
375
376 u_char buf[8];
377 chunk_t chunk_zero = { buf, 0 };
378 chunk_t chunk_mean = { 0, 1 };
379
380 testdata_t test[] = {
381 { FALSE, chunk_zero },
382 { FALSE, chunk_empty },
383 { FALSE, chunk_mean },
384 { TRUE, chunk_from_chars(0x30, 0x00) },
385 { TRUE, chunk_from_chars(0x31, 0x00) },
386 { TRUE, chunk_from_chars(0x04, 0x00) },
387 { FALSE, chunk_from_chars(0x02, 0x00) },
388 { FALSE, chunk_from_chars(0x30, 0x01) },
389 { FALSE, chunk_from_chars(0x30, 0x80) },
390 { TRUE, chunk_from_chars(0x30, 0x01, 0xa1) },
391 { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2) },
392 { TRUE, chunk_from_chars(0x30, 0x01, 0xa1, 0x0a) },
393 { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2, 0x0a) },
394 };
395
396 int i;
397
398 for (i = 0; i < countof(test); i++)
399 {
400 ck_assert(is_asn1(test[i].chunk) == test[i].asn1);
401 }
402 }
403 END_TEST
404
405 /*******************************************************************************
406 * is_printablestring
407 */
408
409 START_TEST(test_asn1_is_printablestring)
410 {
411 typedef struct {
412 bool printable;
413 char *string;
414 } testdata_t;
415
416
417 testdata_t test[] = {
418 { TRUE, "" },
419 { TRUE, "Z" },
420 { FALSE, "Z#" },
421 { FALSE, "&Z" },
422 { FALSE, "Z@z" },
423 { FALSE, "!" }, { FALSE, "*" }, { FALSE, "$" }, { FALSE, "%" },
424 { FALSE, "[" }, { FALSE, "]" }, { FALSE, "{" }, { FALSE, "}" },
425 { FALSE, "|" }, { FALSE, "~" }, { FALSE, "^" }, { FALSE, "_" },
426 { FALSE, "\"" }, { FALSE, "\\" }, { FALSE, "ä" }, { FALSE, "à" },
427 { TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
428 "0123456789 '()+,-./:=?" },
429 };
430
431 chunk_t chunk;
432 int i;
433
434 ck_assert(asn1_is_printablestring(chunk_empty));
435
436 for (i = 0; i < countof(test); i++)
437 {
438 chunk = chunk_from_str(test[i].string);
439 ck_assert(asn1_is_printablestring(chunk) == test[i].printable);
440 }
441 }
442 END_TEST
443
444 /*******************************************************************************
445 * to_time
446 */
447
448 START_TEST(test_asn1_to_time)
449 {
450 typedef struct {
451 time_t time;
452 u_int8_t type;
453 char *string;
454 } testdata_t;
455
456 testdata_t test[] = {
457 { 352980, 0x18, "197001050203Z" },
458 { 352984, 0x18, "19700105020304Z" },
459 { 352980, 0x17, "7001050203Z" },
460 { 347580, 0x17, "7001050203+0130" },
461 { 358380, 0x17, "7001050203-0130" },
462 { 352984, 0x17, "700105020304Z" },
463 { 347584, 0x17, "700105020304+0130" },
464 { 358384, 0x17, "700105020304-0130" },
465 { 0, 0x17, "700105020304+01" },
466 { 0, 0x17, "700105020304-01" },
467 { 0, 0x17, "700105020304" },
468 { 0, 0x17, "70010502Z" },
469 { 0, 0x17, "7001050203xxZ" },
470 { 0, 0x17, "7000050203Z" },
471 { 0, 0x17, "7013050203Z" },
472 { 5097600, 0x17, "7003010000Z" },
473 { 68256000, 0x17, "7203010000Z" },
474 { 951868800, 0x17, "0003010000Z" },
475 { 4107542400, 0x18, "210003010000Z" }
476 };
477
478 int i;
479 chunk_t chunk;
480
481 for (i = 0; i < countof(test); i++)
482 {
483 if (test[i].time > TIME_32_BIT_SIGNED_MAX && sizeof(time_t) == 4)
484 {
485 continue;
486 }
487 chunk = chunk_from_str(test[i].string);
488 ck_assert(asn1_to_time(&chunk, test[i].type) == test[i].time);
489 }
490 }
491 END_TEST
492
493 /*******************************************************************************
494 * from_time
495 */
496
497 START_TEST(test_asn1_from_time)
498 {
499 typedef struct {
500 time_t time;
501 u_int8_t type;
502 chunk_t chunk;
503 } testdata_t;
504
505 testdata_t test[] = {
506 { 352984, 0x18, chunk_from_chars(
507 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
508 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
509 { 352984, 0x17, chunk_from_chars(
510 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
511 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
512 { 1078099200, 0x17, chunk_from_chars(
513 0x17, 0x0d, 0x30, 0x34, 0x30, 0x33, 0x30, 0x31,
514 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) },
515 { 4107542400, 0x18, chunk_from_chars(
516 0x18, 0x0f, 0x32, 0x31, 0x30, 0x30, 0x30, 0x33, 0x30, 0x31,
517 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) }
518 };
519
520 int i;
521 chunk_t chunk;
522
523 for (i = 0; i < countof(test); i++)
524 {
525 if (test[i].time > TIME_32_BIT_SIGNED_MAX && sizeof(time_t) == 4)
526 {
527 continue;
528 }
529 chunk = asn1_from_time(&test[i].time, test[i].type);
530 ck_assert(chunk_equals(chunk, test[i].chunk));
531 free(chunk.ptr);
532 }
533 }
534 END_TEST
535
536 /*******************************************************************************
537 * parse_time
538 */
539
540 START_TEST(test_asn1_parse_time)
541 {
542 typedef struct {
543 time_t time;
544 chunk_t chunk;
545 } testdata_t;
546
547 testdata_t test[] = {
548 { 352984, chunk_from_chars(
549 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
550 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
551 { 352984, chunk_from_chars(
552 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
553 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
554 { 0, chunk_from_chars(0x05, 0x00) }
555 };
556
557 int i;
558
559 for (i = 0; i < countof(test); i++)
560 {
561 ck_assert(asn1_parse_time(test[i].chunk, 0) == test[i].time);
562 }
563 }
564 END_TEST
565
566 /*******************************************************************************
567 * build_object
568 */
569
570 START_TEST(test_asn1_build_object)
571 {
572 typedef struct {
573 size_t len;
574 size_t size;
575 u_char *b;
576 } testdata_t;
577
578 u_char b0[] = { 0x05, 0x00 };
579 u_char b1[] = { 0x04, 0x7f };
580 u_char b2[] = { 0x04, 0x81, 0x80 };
581 u_char b3[] = { 0x04, 0x81, 0xff };
582 u_char b4[] = { 0x04, 0x82, 0x01, 0x00 };
583 u_char b5[] = { 0x04, 0x82, 0xff, 0xff };
584 u_char b6[] = { 0x04, 0x83, 0x01, 0x00, 0x00 };
585
586 testdata_t test[] = {
587 { 0, sizeof(b0), b0 },
588 { 127, sizeof(b1), b1 },
589 { 128, sizeof(b2), b2 },
590 { 255, sizeof(b3), b3 },
591 { 256, sizeof(b4), b4 },
592 { 65535, sizeof(b5), b5 },
593 { 65536, sizeof(b6), b6 }
594 };
595
596 chunk_t a = chunk_empty;
597 u_char *pos;
598 int i;
599
600 for (i = 0; i < countof(test); i++)
601 {
602 pos = asn1_build_object(&a, test[i].b[0], test[i].len);
603 ck_assert(pos == (a.ptr + test[i].size));
604 ck_assert(a.len == test[i].size + test[i].len);
605 ck_assert(memeq(a.ptr, test[i].b, test[i].size));
606 chunk_free(&a);
607 }
608 }
609 END_TEST
610
611 /*******************************************************************************
612 * simple_object
613 */
614
615 START_TEST(test_asn1_simple_object)
616 {
617 chunk_t a = chunk_empty;
618 chunk_t b = chunk_from_chars(0x04, 0x05, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
619 chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
620
621 a = asn1_simple_object(0x04, c);
622 ck_assert(chunk_equals(a, b));
623 chunk_free(&a);
624 }
625 END_TEST
626
627 /*******************************************************************************
628 * parse_simple_object
629 */
630
631 START_TEST(test_asn1_parse_simple_object)
632 {
633 typedef struct {
634 bool res;
635 int type;
636 chunk_t chunk;
637 } testdata_t;
638
639 testdata_t test[] = {
640 { FALSE, 0x04, chunk_from_chars(0x04) },
641 { FALSE, 0x04, chunk_from_chars(0x02, 0x01, 0x55) },
642 { FALSE, 0x04, chunk_from_chars(0x04, 0x01) },
643 { TRUE, 0x04, chunk_from_chars(0x04, 0x01, 0x55) },
644 { TRUE, 0x06, chunk_from_chars(0x06, 0x02, 0x55, 0x03) },
645 { TRUE, 0x06, chunk_from_chars(0x06, 0x00) },
646 { TRUE, 0x13, chunk_from_chars(0x13, 0x01, 0x55), }
647 };
648
649 int i;
650 bool res;
651
652 for (i = 0; i < countof(test); i++)
653 {
654 res = asn1_parse_simple_object(&test[i].chunk, test[i].type, 0, "test");
655 ck_assert(res == test[i].res);
656 if (res && test[i].chunk.len)
657 {
658 ck_assert(*test[i].chunk.ptr == 0x55);
659 }
660 }
661 }
662 END_TEST
663
664 /*******************************************************************************
665 * bitstring
666 */
667
668 START_TEST(test_asn1_bitstring)
669 {
670 chunk_t a = chunk_empty;
671 chunk_t b = chunk_from_chars(0x03, 0x05, 0x00, 0xa1, 0xa2, 0xa3, 0xa4);
672 chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4);
673 chunk_t d = chunk_clone(c);
674
675 a = asn1_bitstring("c", c);
676 ck_assert(chunk_equals(a, b));
677 chunk_free(&a);
678
679 a = asn1_bitstring("m", d);
680 ck_assert(chunk_equals(a, b));
681 chunk_free(&a);
682 }
683 END_TEST
684
685 /*******************************************************************************
686 * integer
687 */
688
689 START_TEST(test_asn1_integer)
690 {
691 typedef struct {
692 chunk_t b;
693 chunk_t c;
694 } testdata_t;
695
696 chunk_t b0 = chunk_from_chars(0x02, 0x01, 0x00);
697 chunk_t b1 = chunk_from_chars(0x02, 0x01, 0x7f);
698 chunk_t b2 = chunk_from_chars(0x02, 0x02, 0x00, 0x80);
699
700 chunk_t c0 = chunk_empty;
701 chunk_t c1 = chunk_from_chars(0x7f);
702 chunk_t c2 = chunk_from_chars(0x80);
703 chunk_t c3 = chunk_from_chars(0x00, 0x80);
704
705 testdata_t test[] = {
706 { b0, c0 },
707 { b1, c1 },
708 { b2, c2 },
709 { b2, c3 }
710 };
711
712 chunk_t a = chunk_empty;
713 int i;
714
715 for (i = 0; i < countof(test); i++)
716 {
717 a = asn1_integer("c", test[i].c);
718 ck_assert(chunk_equals(a, test[i].b));
719 chunk_free(&a);
720
721 a = asn1_integer("m", chunk_clone(test[i].c));
722 ck_assert(chunk_equals(a, test[i].b));
723 chunk_free(&a);
724 }
725 }
726 END_TEST
727
728 /*******************************************************************************
729 * parse_integer_uint64
730 */
731
732 START_TEST(test_asn1_parse_integer_uint64)
733 {
734 typedef struct {
735 u_int64_t n;
736 chunk_t chunk;
737 } testdata_t;
738
739
740 testdata_t test[] = {
741 { 67305985ULL, chunk_from_chars(
742 0x04, 0x03, 0x02, 0x01) },
743 { 578437695752307201ULL, chunk_from_chars(
744 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01) },
745 { 18446744073709551615ULL, chunk_from_chars(
746 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) }
747 };
748
749 int i;
750
751 for (i = 0; i < countof(test); i++)
752 {
753 ck_assert(asn1_parse_integer_uint64(test[i].chunk) == test[i].n);
754 }
755 }
756 END_TEST
757
758 Suite *asn1_suite_create()
759 {
760 Suite *s;
761 TCase *tc;
762
763 s = suite_create("asn1");
764
765 tc = tcase_create("algorithmIdentifier");
766 tcase_add_test(tc, test_asn1_algorithmIdentifier);
767 suite_add_tcase(s, tc);
768
769 tc = tcase_create("parse_algorithmIdentifier");
770 tcase_add_test(tc, test_asn1_parse_algorithmIdentifier);
771 suite_add_tcase(s, tc);
772
773 tc = tcase_create("known_oid");
774 tcase_add_test(tc, test_asn1_known_oid);
775 suite_add_tcase(s, tc);
776
777 tc = tcase_create("build_known_oid");
778 tcase_add_test(tc, test_asn1_build_known_oid);
779 suite_add_tcase(s, tc);
780
781 tc = tcase_create("oid_from_string");
782 tcase_add_test(tc, test_asn1_oid_from_string);
783 suite_add_tcase(s, tc);
784
785 tc = tcase_create("oid_to_string");
786 tcase_add_test(tc, test_asn1_oid_to_string);
787 suite_add_tcase(s, tc);
788
789 tc = tcase_create("length");
790 tcase_add_test(tc, test_asn1_length);
791 suite_add_tcase(s, tc);
792
793 tc = tcase_create("unwrap");
794 tcase_add_test(tc, test_asn1_unwrap);
795 suite_add_tcase(s, tc);
796
797 tc = tcase_create("is_asn1");
798 tcase_add_test(tc, test_is_asn1);
799 suite_add_tcase(s, tc);
800
801 tc = tcase_create("is_printablestring");
802 tcase_add_test(tc, test_asn1_is_printablestring);
803 suite_add_tcase(s, tc);
804
805 tc = tcase_create("to_time");
806 tcase_add_test(tc, test_asn1_to_time);
807 suite_add_tcase(s, tc);
808
809 tc = tcase_create("from_time");
810 tcase_add_test(tc, test_asn1_from_time);
811 suite_add_tcase(s, tc);
812
813 tc = tcase_create("parse_time");
814 tcase_add_test(tc, test_asn1_parse_time);
815 suite_add_tcase(s, tc);
816
817 tc = tcase_create("build_object");
818 tcase_add_test(tc, test_asn1_build_object);
819 suite_add_tcase(s, tc);
820
821 tc = tcase_create("simple_object");
822 tcase_add_test(tc, test_asn1_simple_object);
823 suite_add_tcase(s, tc);
824
825 tc = tcase_create("parse_simple_object");
826 tcase_add_test(tc, test_asn1_parse_simple_object);
827 suite_add_tcase(s, tc);
828
829 tc = tcase_create("bitstring");
830 tcase_add_test(tc, test_asn1_bitstring);
831 suite_add_tcase(s, tc);
832
833 tc = tcase_create("integer");
834 tcase_add_test(tc, test_asn1_integer);
835 suite_add_tcase(s, tc);
836
837 tc = tcase_create("parse_integer_uint64");
838 tcase_add_test(tc, test_asn1_parse_integer_uint64);
839 suite_add_tcase(s, tc);
840
841 return s;
842 }