fixup logic for prompting for password
[heimdal.git] / lib / asn1 / check-gen.c
blob614190548e49ffb2d7e25ada0eab8641d88fa552
1 /*
2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <err.h>
42 #include <roken.h>
44 #include <asn1-common.h>
45 #include <asn1_err.h>
46 #include <der.h>
47 #include <krb5_asn1.h>
48 #include <heim_asn1.h>
49 #include <rfc2459_asn1.h>
50 #include <test_asn1.h>
52 #include "check-common.h"
54 RCSID("$Id$");
56 static char *lha_principal[] = { "lha" };
57 static char *lharoot_princ[] = { "lha", "root" };
58 static char *datan_princ[] = { "host", "nutcracker.e.kth.se" };
59 static char *nada_tgt_principal[] = { "krbtgt", "NADA.KTH.SE" };
62 #define IF_OPT_COMPARE(ac,bc,e) \
63 if (((ac)->e == NULL && (bc)->e != NULL) || (((ac)->e != NULL && (bc)->e == NULL))) return 1; if ((ab)->e)
64 #define COMPARE_OPT_STRING(ac,bc,e) \
65 do { if (strcmp(*(ac)->e, *(bc)->e) != 0) return 1; } while(0)
66 #define COMPARE_OPT_OCTECT_STRING(ac,bc,e) \
67 do { if ((ac)->e->length != (bc)->e->length || memcmp((ac)->e->data, (bc)->e->data, (ac)->e->length) != 0) return 1; } while(0)
68 #define COMPARE_STRING(ac,bc,e) \
69 do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
70 #define COMPARE_INTEGER(ac,bc,e) \
71 do { if ((ac)->e != (bc)->e) return 1; } while(0)
72 #define COMPARE_OPT_INTEGER(ac,bc,e) \
73 do { if (*(ac)->e != *(bc)->e) return 1; } while(0)
74 #define COMPARE_MEM(ac,bc,e,len) \
75 do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
77 static int
78 cmp_principal (void *a, void *b)
80 Principal *pa = a;
81 Principal *pb = b;
82 int i;
84 COMPARE_STRING(pa,pb,realm);
85 COMPARE_INTEGER(pa,pb,name.name_type);
86 COMPARE_INTEGER(pa,pb,name.name_string.len);
88 for (i = 0; i < pa->name.name_string.len; i++)
89 COMPARE_STRING(pa,pb,name.name_string.val[i]);
91 return 0;
94 static int
95 test_principal (void)
98 struct test_case tests[] = {
99 { NULL, 29,
100 "\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
101 "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45",
102 NULL
104 { NULL, 35,
105 "\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
106 "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
107 "\x2e\x53\x45",
108 NULL
110 { NULL, 54,
111 "\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
112 "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
113 "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
114 "\x4b\x54\x48\x2e\x53\x45",
115 NULL
120 Principal values[] = {
121 { { KRB5_NT_PRINCIPAL, { 1, lha_principal } }, "SU.SE" },
122 { { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } }, "SU.SE" },
123 { { KRB5_NT_SRV_HST, { 2, datan_princ } }, "E.KTH.SE" }
125 int i, ret;
126 int ntests = sizeof(tests) / sizeof(*tests);
128 for (i = 0; i < ntests; ++i) {
129 tests[i].val = &values[i];
130 if (asprintf (&tests[i].name, "Principal %d", i) < 0)
131 errx(1, "malloc");
132 if (tests[i].name == NULL)
133 errx(1, "malloc");
136 ret = generic_test (tests, ntests, sizeof(Principal),
137 (generic_encode)encode_Principal,
138 (generic_length)length_Principal,
139 (generic_decode)decode_Principal,
140 (generic_free)free_Principal,
141 cmp_principal,
142 NULL);
143 for (i = 0; i < ntests; ++i)
144 free (tests[i].name);
146 return ret;
149 static int
150 cmp_authenticator (void *a, void *b)
152 Authenticator *aa = a;
153 Authenticator *ab = b;
154 int i;
156 COMPARE_INTEGER(aa,ab,authenticator_vno);
157 COMPARE_STRING(aa,ab,crealm);
159 COMPARE_INTEGER(aa,ab,cname.name_type);
160 COMPARE_INTEGER(aa,ab,cname.name_string.len);
162 for (i = 0; i < aa->cname.name_string.len; i++)
163 COMPARE_STRING(aa,ab,cname.name_string.val[i]);
165 return 0;
168 static int
169 test_authenticator (void)
171 struct test_case tests[] = {
172 { NULL, 63,
173 "\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
174 "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
175 "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
176 "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
177 "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a",
178 NULL
180 { NULL, 67,
181 "\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
182 "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
183 "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
184 "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
185 "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
186 "\x39\x5a",
187 NULL
191 Authenticator values[] = {
192 { 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL, { 1, lha_principal } },
193 NULL, 10, 99, NULL, NULL, NULL },
194 { 5, "SU.SE", { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },
195 NULL, 292, 999, NULL, NULL, NULL }
197 int i, ret;
198 int ntests = sizeof(tests) / sizeof(*tests);
200 for (i = 0; i < ntests; ++i) {
201 tests[i].val = &values[i];
202 if (asprintf (&tests[i].name, "Authenticator %d", i) < 0)
203 errx(1, "malloc");
204 if (tests[i].name == NULL)
205 errx(1, "malloc");
208 ret = generic_test (tests, ntests, sizeof(Authenticator),
209 (generic_encode)encode_Authenticator,
210 (generic_length)length_Authenticator,
211 (generic_decode)decode_Authenticator,
212 (generic_free)free_Authenticator,
213 cmp_authenticator,
214 (generic_copy)copy_Authenticator);
215 for (i = 0; i < ntests; ++i)
216 free(tests[i].name);
218 return ret;
221 static int
222 cmp_KRB_ERROR (void *a, void *b)
224 KRB_ERROR *aa = a;
225 KRB_ERROR *ab = b;
226 int i;
228 COMPARE_INTEGER(aa,ab,pvno);
229 COMPARE_INTEGER(aa,ab,msg_type);
231 IF_OPT_COMPARE(aa,ab,ctime) {
232 COMPARE_INTEGER(aa,ab,ctime);
234 IF_OPT_COMPARE(aa,ab,cusec) {
235 COMPARE_INTEGER(aa,ab,cusec);
237 COMPARE_INTEGER(aa,ab,stime);
238 COMPARE_INTEGER(aa,ab,susec);
239 COMPARE_INTEGER(aa,ab,error_code);
241 IF_OPT_COMPARE(aa,ab,crealm) {
242 COMPARE_OPT_STRING(aa,ab,crealm);
244 #if 0
245 IF_OPT_COMPARE(aa,ab,cname) {
246 COMPARE_OPT_STRING(aa,ab,cname);
248 #endif
249 COMPARE_STRING(aa,ab,realm);
251 COMPARE_INTEGER(aa,ab,sname.name_string.len);
252 for (i = 0; i < aa->sname.name_string.len; i++)
253 COMPARE_STRING(aa,ab,sname.name_string.val[i]);
255 IF_OPT_COMPARE(aa,ab,e_text) {
256 COMPARE_OPT_STRING(aa,ab,e_text);
258 IF_OPT_COMPARE(aa,ab,e_data) {
259 /* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
262 return 0;
265 static int
266 test_krb_error (void)
268 struct test_case tests[] = {
269 { NULL, 127,
270 "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
271 "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
272 "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
273 "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
274 "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
275 "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
276 "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
277 "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
278 "KRB-ERROR Test 1"
281 int ntests = sizeof(tests) / sizeof(*tests);
282 KRB_ERROR e1;
283 PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
284 PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
285 char *realm = "NADA.KTH.SE";
287 e1.pvno = 5;
288 e1.msg_type = 30;
289 e1.ctime = NULL;
290 e1.cusec = NULL;
291 e1.stime = 1069632679;
292 e1.susec = 322981;
293 e1.error_code = 31;
294 e1.crealm = &realm;
295 e1.cname = &lhaprincipalname;
296 e1.realm = "NADA.KTH.SE";
297 e1.sname = tgtprincipalname;
298 e1.e_text = NULL;
299 e1.e_data = NULL;
301 tests[0].val = &e1;
303 return generic_test (tests, ntests, sizeof(KRB_ERROR),
304 (generic_encode)encode_KRB_ERROR,
305 (generic_length)length_KRB_ERROR,
306 (generic_decode)decode_KRB_ERROR,
307 (generic_free)free_KRB_ERROR,
308 cmp_KRB_ERROR,
309 (generic_copy)copy_KRB_ERROR);
312 static int
313 cmp_Name (void *a, void *b)
315 Name *aa = a;
316 Name *ab = b;
318 COMPARE_INTEGER(aa,ab,element);
320 return 0;
323 static int
324 test_Name (void)
326 struct test_case tests[] = {
327 { NULL, 35,
328 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
329 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
330 "\x4f\x4c\x4d",
331 "Name CN=Love+L=STOCKHOLM"
333 { NULL, 35,
334 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
335 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
336 "\x4f\x4c\x4d",
337 "Name L=STOCKHOLM+CN=Love"
341 int ntests = sizeof(tests) / sizeof(*tests);
342 Name n1, n2;
343 RelativeDistinguishedName rdn1[1];
344 RelativeDistinguishedName rdn2[1];
345 AttributeTypeAndValue atv1[2];
346 AttributeTypeAndValue atv2[2];
347 unsigned cmp_CN[] = { 2, 5, 4, 3 };
348 unsigned cmp_L[] = { 2, 5, 4, 7 };
350 /* n1 */
351 n1.element = choice_Name_rdnSequence;
352 n1.u.rdnSequence.val = rdn1;
353 n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
354 rdn1[0].val = atv1;
355 rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
357 atv1[0].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
358 atv1[0].type.components = cmp_CN;
359 atv1[0].value.element = choice_DirectoryString_printableString;
360 atv1[0].value.u.printableString.data = "Love";
361 atv1[0].value.u.printableString.length = 4;
363 atv1[1].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
364 atv1[1].type.components = cmp_L;
365 atv1[1].value.element = choice_DirectoryString_printableString;
366 atv1[1].value.u.printableString.data = "STOCKHOLM";
367 atv1[1].value.u.printableString.length = 9;
369 /* n2 */
370 n2.element = choice_Name_rdnSequence;
371 n2.u.rdnSequence.val = rdn2;
372 n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
373 rdn2[0].val = atv2;
374 rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
376 atv2[0].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
377 atv2[0].type.components = cmp_L;
378 atv2[0].value.element = choice_DirectoryString_printableString;
379 atv2[0].value.u.printableString.data = "STOCKHOLM";
380 atv2[0].value.u.printableString.length = 9;
382 atv2[1].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
383 atv2[1].type.components = cmp_CN;
384 atv2[1].value.element = choice_DirectoryString_printableString;
385 atv2[1].value.u.printableString.data = "Love";
386 atv2[1].value.u.printableString.length = 4;
388 /* */
389 tests[0].val = &n1;
390 tests[1].val = &n2;
392 return generic_test (tests, ntests, sizeof(Name),
393 (generic_encode)encode_Name,
394 (generic_length)length_Name,
395 (generic_decode)decode_Name,
396 (generic_free)free_Name,
397 cmp_Name,
398 (generic_copy)copy_Name);
401 static int
402 cmp_KeyUsage (void *a, void *b)
404 KeyUsage *aa = a;
405 KeyUsage *ab = b;
407 return KeyUsage2int(*aa) != KeyUsage2int(*ab);
410 static int
411 test_bit_string (void)
413 struct test_case tests[] = {
414 { NULL, 4,
415 "\x03\x02\x07\x80",
416 "bitstring 1"
418 { NULL, 4,
419 "\x03\x02\x05\xa0",
420 "bitstring 2"
422 { NULL, 5,
423 "\x03\x03\x07\x00\x80",
424 "bitstring 3"
426 { NULL, 3,
427 "\x03\x01\x00",
428 "bitstring 4"
432 int ntests = sizeof(tests) / sizeof(*tests);
433 KeyUsage ku1, ku2, ku3, ku4;
435 memset(&ku1, 0, sizeof(ku1));
436 ku1.digitalSignature = 1;
437 tests[0].val = &ku1;
439 memset(&ku2, 0, sizeof(ku2));
440 ku2.digitalSignature = 1;
441 ku2.keyEncipherment = 1;
442 tests[1].val = &ku2;
444 memset(&ku3, 0, sizeof(ku3));
445 ku3.decipherOnly = 1;
446 tests[2].val = &ku3;
448 memset(&ku4, 0, sizeof(ku4));
449 tests[3].val = &ku4;
452 return generic_test (tests, ntests, sizeof(KeyUsage),
453 (generic_encode)encode_KeyUsage,
454 (generic_length)length_KeyUsage,
455 (generic_decode)decode_KeyUsage,
456 (generic_free)free_KeyUsage,
457 cmp_KeyUsage,
458 (generic_copy)copy_KeyUsage);
461 static int
462 cmp_TicketFlags (void *a, void *b)
464 TicketFlags *aa = a;
465 TicketFlags *ab = b;
467 return TicketFlags2int(*aa) != TicketFlags2int(*ab);
470 static int
471 test_bit_string_rfc1510 (void)
473 struct test_case tests[] = {
474 { NULL, 7,
475 "\x03\x05\x00\x80\x00\x00\x00",
476 "TF bitstring 1"
478 { NULL, 7,
479 "\x03\x05\x00\x40\x20\x00\x00",
480 "TF bitstring 2"
482 { NULL, 7,
483 "\x03\x05\x00\x00\x20\x00\x00",
484 "TF bitstring 3"
486 { NULL, 7,
487 "\x03\x05\x00\x00\x00\x00\x00",
488 "TF bitstring 4"
492 int ntests = sizeof(tests) / sizeof(*tests);
493 TicketFlags tf1, tf2, tf3, tf4;
495 memset(&tf1, 0, sizeof(tf1));
496 tf1.reserved = 1;
497 tests[0].val = &tf1;
499 memset(&tf2, 0, sizeof(tf2));
500 tf2.forwardable = 1;
501 tf2.pre_authent = 1;
502 tests[1].val = &tf2;
504 memset(&tf3, 0, sizeof(tf3));
505 tf3.pre_authent = 1;
506 tests[2].val = &tf3;
508 memset(&tf4, 0, sizeof(tf4));
509 tests[3].val = &tf4;
512 return generic_test (tests, ntests, sizeof(TicketFlags),
513 (generic_encode)encode_TicketFlags,
514 (generic_length)length_TicketFlags,
515 (generic_decode)decode_TicketFlags,
516 (generic_free)free_TicketFlags,
517 cmp_TicketFlags,
518 (generic_copy)copy_TicketFlags);
521 static int
522 cmp_KerberosTime (void *a, void *b)
524 KerberosTime *aa = a;
525 KerberosTime *ab = b;
527 return *aa != *ab;
530 static int
531 test_time (void)
533 struct test_case tests[] = {
534 { NULL, 17,
535 "\x18\x0f\x31\x39\x37\x30\x30\x31\x30\x31\x30\x31\x31\x38\x33\x31"
536 "\x5a",
537 "time 1" },
538 { NULL, 17,
539 "\x18\x0f\x32\x30\x30\x39\x30\x35\x32\x34\x30\x32\x30\x32\x34\x30"
540 "\x5a",
541 "time 2" }
544 int ntests = sizeof(tests) / sizeof(*tests);
545 KerberosTime times[] = {
546 4711,
547 1243130560
550 tests[0].val = &times[0];
551 tests[1].val = &times[1];
553 return generic_test (tests, ntests, sizeof(KerberosTime),
554 (generic_encode)encode_KerberosTime,
555 (generic_length)length_KerberosTime,
556 (generic_decode)decode_KerberosTime,
557 (generic_free)free_KerberosTime,
558 cmp_KerberosTime,
559 (generic_copy)copy_KerberosTime);
562 struct {
563 const char *cert;
564 size_t len;
565 } certs[] = {
567 "\x30\x82\x02\x6c\x30\x82\x01\xd5\xa0\x03\x02\x01\x02\x02\x09\x00"
568 "\x99\x32\xde\x61\x0e\x40\x19\x8a\x30\x0d\x06\x09\x2a\x86\x48\x86"
569 "\xf7\x0d\x01\x01\x05\x05\x00\x30\x2a\x31\x1b\x30\x19\x06\x03\x55"
570 "\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52"
571 "\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13"
572 "\x02\x53\x45\x30\x1e\x17\x0d\x30\x39\x30\x34\x32\x36\x32\x30\x32"
573 "\x39\x34\x30\x5a\x17\x0d\x31\x39\x30\x34\x32\x34\x32\x30\x32\x39"
574 "\x34\x30\x5a\x30\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12"
575 "\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20"
576 "\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30"
577 "\x81\x9f\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05"
578 "\x00\x03\x81\x8d\x00\x30\x81\x89\x02\x81\x81\x00\xb9\xd3\x1b\x67"
579 "\x1c\xf7\x5e\x26\x81\x3b\x82\xff\x03\xa4\x43\xb5\xb2\x63\x0b\x89"
580 "\x58\x43\xfe\x3d\xe0\x38\x7d\x93\x74\xbb\xad\x21\xa4\x29\xd9\x34"
581 "\x79\xf3\x1c\x8c\x5a\xd6\xb0\xd7\x19\xea\xcc\xaf\xe0\xa8\x40\x02"
582 "\x1d\x91\xf1\xac\x36\xb0\xfb\x08\xbd\xcc\x9a\xe1\xb7\x6e\xee\x0a"
583 "\x69\xbf\x6d\x2b\xee\x20\x82\x61\x06\xf2\x18\xcc\x89\x11\x64\x7e"
584 "\xb2\xff\x47\xd1\x3b\x52\x73\xeb\x5a\xc0\x03\xa6\x4b\xc7\x40\x7e"
585 "\xbc\xe1\x0e\x65\x44\x3f\x40\x8b\x02\x82\x54\x04\xd9\xcc\x2c\x67"
586 "\x01\xb6\x16\x82\xd8\x33\x53\x17\xd7\xde\x8d\x5d\x02\x03\x01\x00"
587 "\x01\xa3\x81\x99\x30\x81\x96\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16"
588 "\x04\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30\xdd"
589 "\x27\x96\x59\x9b\x0e\x68\x30\x5a\x06\x03\x55\x1d\x23\x04\x53\x30"
590 "\x51\x80\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30"
591 "\xdd\x27\x96\x59\x9b\x0e\x68\xa1\x2e\xa4\x2c\x30\x2a\x31\x1b\x30"
592 "\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65"
593 "\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03"
594 "\x55\x04\x06\x13\x02\x53\x45\x82\x09\x00\x99\x32\xde\x61\x0e\x40"
595 "\x19\x8a\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff"
596 "\x30\x0b\x06\x03\x55\x1d\x0f\x04\x04\x03\x02\x01\xe6\x30\x0d\x06"
597 "\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x81\x81\x00"
598 "\x52\x9b\xe4\x0e\xee\xc2\x5d\xb7\xf1\xba\x47\xe3\xfe\xaf\x3d\x51"
599 "\x10\xfd\xe8\x0d\x14\x58\x05\x36\xa7\xeb\xd8\x05\xe5\x27\x6f\x51"
600 "\xb8\xec\x90\xd9\x03\xe1\xbc\x9c\x93\x38\x21\x5c\xaf\x4e\x6c\x7b"
601 "\x6c\x65\xa9\x92\xcd\x94\xef\xa8\xae\x90\x12\x14\x78\x2d\xa3\x15"
602 "\xaa\x42\xf1\xd9\x44\x64\x2c\x3c\xc0\xbd\x3a\x48\xd8\x80\x45\x8b"
603 "\xd1\x79\x82\xe0\x0f\xdf\x08\x3c\x60\x21\x6f\x31\x47\x98\xae\x2f"
604 "\xcb\xb1\xa1\xb9\xc1\xa3\x71\x5e\x4a\xc2\x67\xdf\x66\x0a\x51\xb5"
605 "\xad\x60\x05\xdb\x02\xd4\x1a\xd2\xb9\x4e\x01\x08\x2b\xc3\x57\xaf",
606 624 },
608 "\x30\x82\x02\x54\x30\x82\x01\xbd\xa0\x03\x02\x01\x02\x02\x01\x08"
609 "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x30"
610 "\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30"
611 "\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b"
612 "\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30\x1e\x17\x0d\x30"
613 "\x39\x30\x34\x32\x36\x32\x30\x32\x39\x34\x30\x5a\x17\x0d\x31\x39"
614 "\x30\x34\x32\x34\x32\x30\x32\x39\x34\x30\x5a\x30\x1b\x31\x0b\x30"
615 "\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x31\x0c\x30\x0a\x06\x03"
616 "\x55\x04\x03\x0c\x03\x6b\x64\x63\x30\x81\x9f\x30\x0d\x06\x09\x2a"
617 "\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81\x8d\x00\x30\x81"
618 "\x89\x02\x81\x81\x00\xd2\x41\x7a\xf8\x4b\x55\xb2\xaf\x11\xf9\x43"
619 "\x9b\x43\x81\x09\x3b\x9a\x94\xcf\x00\xf4\x85\x75\x92\xd7\x2a\xa5"
620 "\x11\xf1\xa8\x50\x6e\xc6\x84\x74\x24\x17\xda\x84\xc8\x03\x37\xb2"
621 "\x20\xf3\xba\xb5\x59\x36\x21\x4d\xab\x70\xe2\xc3\x09\x93\x68\x14"
622 "\x12\x79\xc5\xbb\x9e\x1b\x4a\xf0\xc6\x24\x59\x25\xc3\x1c\xa8\x70"
623 "\x66\x5b\x3e\x41\x8e\xe3\x25\x71\x9a\x94\xa0\x5b\x46\x91\x6f\xdd"
624 "\x58\x14\xec\x89\xe5\x8c\x96\xc5\x38\x60\xe4\xab\xf2\x75\xee\x6e"
625 "\x62\xfc\xe1\xbd\x03\x47\xff\xc4\xbe\x0f\xca\x70\x73\xe3\x74\x58"
626 "\x3a\x2f\x04\x2d\x39\x02\x03\x01\x00\x01\xa3\x81\x98\x30\x81\x95"
627 "\x30\x09\x06\x03\x55\x1d\x13\x04\x02\x30\x00\x30\x0b\x06\x03\x55"
628 "\x1d\x0f\x04\x04\x03\x02\x05\xe0\x30\x12\x06\x03\x55\x1d\x25\x04"
629 "\x0b\x30\x09\x06\x07\x2b\x06\x01\x05\x02\x03\x05\x30\x1d\x06\x03"
630 "\x55\x1d\x0e\x04\x16\x04\x14\x3a\xd3\x73\xff\xab\xdb\x7d\x8d\xc6"
631 "\x3a\xa2\x26\x3e\xae\x78\x95\x80\xc9\xe6\x31\x30\x48\x06\x03\x55"
632 "\x1d\x11\x04\x41\x30\x3f\xa0\x3d\x06\x06\x2b\x06\x01\x05\x02\x02"
633 "\xa0\x33\x30\x31\xa0\x0d\x1b\x0b\x54\x45\x53\x54\x2e\x48\x35\x4c"
634 "\x2e\x53\x45\xa1\x20\x30\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15"
635 "\x1b\x06\x6b\x72\x62\x74\x67\x74\x1b\x0b\x54\x45\x53\x54\x2e\x48"
636 "\x35\x4c\x2e\x53\x45\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01"
637 "\x01\x05\x05\x00\x03\x81\x81\x00\x83\xf4\x14\xa7\x6e\x59\xff\x80"
638 "\x64\xe7\xfa\xcf\x13\x80\x86\xe1\xed\x02\x38\xad\x96\x72\x25\xe5"
639 "\x06\x7a\x9a\xbc\x24\x74\xa9\x75\x55\xb2\x49\x80\x69\x45\x95\x4a"
640 "\x4c\x76\xa9\xe3\x4e\x49\xd3\xc2\x69\x5a\x95\x03\xeb\xba\x72\x23"
641 "\x9c\xfd\x3d\x8b\xc6\x07\x82\x3b\xf4\xf3\xef\x6c\x2e\x9e\x0b\xac"
642 "\x9e\x6c\xbb\x37\x4a\xa1\x9e\x73\xd1\xdc\x97\x61\xba\xfc\xd3\x49"
643 "\xa6\xc2\x4c\x55\x2e\x06\x37\x76\xb5\xef\x57\xe7\x57\x58\x8a\x71"
644 "\x63\xf3\xeb\xe7\x55\x68\x0d\xf6\x46\x4c\xfb\xf9\x43\xbb\x0c\x92"
645 "\x4f\x4e\x22\x7b\x63\xe8\x4f\x9c",
650 static int
651 test_cert(void)
653 Certificate c, c2;
654 size_t size;
655 size_t i;
656 int ret;
658 for (i = 0; i < sizeof(certs)/sizeof(certs[0]); i++) {
660 ret = decode_Certificate((unsigned char *)certs[i].cert,
661 certs[i].len, &c, &size);
662 if (ret)
663 return ret;
665 ret = copy_Certificate(&c, &c2);
666 free_Certificate(&c);
667 if (ret)
668 return ret;
670 free_Certificate(&c2);
673 return 0;
677 static int
678 cmp_TESTLargeTag (void *a, void *b)
680 TESTLargeTag *aa = a;
681 TESTLargeTag *ab = b;
683 COMPARE_INTEGER(aa,ab,foo);
684 COMPARE_INTEGER(aa,ab,bar);
685 return 0;
688 static int
689 test_large_tag (void)
691 struct test_case tests[] = {
692 { NULL, 15, "\x30\x0d\xbf\x7f\x03\x02\x01\x01\xbf\x81\x00\x03\x02\x01\x02", "large tag 1" }
695 int ntests = sizeof(tests) / sizeof(*tests);
696 TESTLargeTag lt1;
698 memset(&lt1, 0, sizeof(lt1));
699 lt1.foo = 1;
700 lt1.bar = 2;
702 tests[0].val = &lt1;
704 return generic_test (tests, ntests, sizeof(TESTLargeTag),
705 (generic_encode)encode_TESTLargeTag,
706 (generic_length)length_TESTLargeTag,
707 (generic_decode)decode_TESTLargeTag,
708 (generic_free)free_TESTLargeTag,
709 cmp_TESTLargeTag,
710 (generic_copy)copy_TESTLargeTag);
713 struct test_data {
714 int ok;
715 size_t len;
716 size_t expected_len;
717 void *data;
720 static int
721 check_tag_length(void)
723 struct test_data td[] = {
724 { 1, 3, 3, "\x02\x01\x00"},
725 { 1, 3, 3, "\x02\x01\x7f"},
726 { 1, 4, 4, "\x02\x02\x00\x80"},
727 { 1, 4, 4, "\x02\x02\x01\x00"},
728 { 1, 4, 4, "\x02\x02\x02\x00"},
729 { 0, 3, 0, "\x02\x02\x00"},
730 { 0, 3, 0, "\x02\x7f\x7f"},
731 { 0, 4, 0, "\x02\x03\x00\x80"},
732 { 0, 4, 0, "\x02\x7f\x01\x00"},
733 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
735 size_t sz;
736 TESTuint32 values[] = {0, 127, 128, 256, 512,
737 0, 127, 128, 256, 512 };
738 TESTuint32 u;
739 int i, ret, failed = 0;
740 void *buf;
742 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
743 struct map_page *page;
745 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
747 ret = decode_TESTuint32(buf, td[i].len, &u, &sz);
748 if (ret) {
749 if (td[i].ok) {
750 printf("failed with tag len test %d\n", i);
751 failed = 1;
753 } else {
754 if (td[i].ok == 0) {
755 printf("failed with success for tag len test %d\n", i);
756 failed = 1;
758 if (td[i].expected_len != sz) {
759 printf("wrong expected size for tag test %d\n", i);
760 failed = 1;
762 if (values[i] != u) {
763 printf("wrong value for tag test %d\n", i);
764 failed = 1;
767 map_free(page, "test", "decode");
769 return failed;
772 static int
773 check_tag_length64(void)
775 struct test_data td[] = {
776 { 1, 3, 3, "\x02\x01\x00"},
777 { 1, 7, 7, "\x02\x05\x01\xff\xff\xff\xff"},
778 { 1, 7, 7, "\x02\x05\x02\x00\x00\x00\x00"},
779 { 1, 9, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff"},
780 { 1, 10, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00"},
781 { 1, 10, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff"},
782 { 1, 11, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"},
783 { 0, 3, 0, "\x02\x02\x00"},
784 { 0, 3, 0, "\x02\x7f\x7f"},
785 { 0, 4, 0, "\x02\x03\x00\x80"},
786 { 0, 4, 0, "\x02\x7f\x01\x00"},
787 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
789 size_t sz;
790 TESTuint64 values[] = {0, 8589934591LL, 8589934592LL,
791 36028797018963967LL, 36028797018963968LL,
792 9223372036854775807LL, 18446744073709551615ULL,
793 0, 127, 128, 256, 512 };
794 TESTuint64 u;
795 int i, ret, failed = 0;
796 void *buf;
798 if (sizeof(TESTuint64) != sizeof(uint64_t)) {
799 ret += 1;
800 printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
801 (int)sizeof(TESTuint64), (int)sizeof(uint64_t));
804 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
805 struct map_page *page;
807 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
809 ret = decode_TESTuint64(buf, td[i].len, &u, &sz);
810 if (ret) {
811 if (td[i].ok) {
812 printf("failed with tag len test %d\n", i);
813 printf("ret = %d\n", ret);
814 failed = 1;
816 } else {
817 if (td[i].ok == 0) {
818 printf("failed with success for tag len test %d\n", i);
819 failed = 1;
821 if (td[i].expected_len != sz) {
822 printf("wrong expected size for tag test %d\n", i);
823 printf("sz = %lu\n", (unsigned long)sz);
824 failed = 1;
826 if (values[i] != u) {
827 printf("wrong value for tag test %d\n", i);
828 printf("Expected value: %llu\nActual value: %llu\n",
829 (unsigned long long)values[i], (unsigned long long)u);
830 failed = 1;
833 map_free(page, "test", "decode");
835 return failed;
838 static int
839 check_tag_length64s(void)
841 struct test_data td[] = {
842 { 1, 3, 3, "\x02\x01\x00"},
843 { 1, 7, 7, "\x02\x05\xfe\x00\x00\x00\x01"},
844 { 1, 7, 7, "\x02\x05\xfe\x00\x00\x00\x00"},
845 { 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x01"},
846 { 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x00"},
847 { 1, 10, 10, "\x02\x08\x80\x00\x00\x00\x00\x00\x00\x01"},
848 { 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x01"},
849 { 0, 3, 0, "\x02\x02\x00"},
850 { 0, 3, 0, "\x02\x7f\x7f"},
851 { 0, 4, 0, "\x02\x03\x00\x80"},
852 { 0, 4, 0, "\x02\x7f\x01\x00"},
853 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
855 size_t sz;
856 TESTint64 values[] = {0, -8589934591LL, -8589934592LL,
857 -36028797018963967LL, -36028797018963968LL,
858 -9223372036854775807LL, -36028797018963967LL,
859 0, 127, 128, 256, 512 };
860 TESTint64 u;
861 int i, ret, failed = 0;
862 void *buf;
864 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
865 struct map_page *page;
867 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
869 ret = decode_TESTint64(buf, td[i].len, &u, &sz);
870 if (ret) {
871 if (td[i].ok) {
872 printf("failed with tag len test %d\n", i);
873 printf("ret = %d\n", ret);
874 failed = 1;
876 } else {
877 if (td[i].ok == 0) {
878 printf("failed with success for tag len test %d\n", i);
879 failed = 1;
881 if (td[i].expected_len != sz) {
882 printf("wrong expected size for tag test %d\n", i);
883 printf("sz = %lu\n", (unsigned long)sz);
884 failed = 1;
886 if (values[i] != u) {
887 printf("wrong value for tag test %d\n", i);
888 printf("Expected value: %lld\nActual value: %lld\n",
889 (long long)values[i], (long long)u);
890 failed = 1;
893 map_free(page, "test", "decode");
895 return failed;
898 static int
899 cmp_TESTChoice (void *a, void *b)
901 return 0;
904 static int
905 test_choice (void)
907 struct test_case tests[] = {
908 { NULL, 5, "\xa1\x03\x02\x01\x01", "large choice 1" },
909 { NULL, 5, "\xa2\x03\x02\x01\x02", "large choice 2" }
912 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
913 TESTChoice1 c1;
914 TESTChoice1 c2_1;
915 TESTChoice2 c2_2;
917 memset(&c1, 0, sizeof(c1));
918 c1.element = choice_TESTChoice1_i1;
919 c1.u.i1 = 1;
920 tests[0].val = &c1;
922 memset(&c2_1, 0, sizeof(c2_1));
923 c2_1.element = choice_TESTChoice1_i2;
924 c2_1.u.i2 = 2;
925 tests[1].val = &c2_1;
927 ret += generic_test (tests, ntests, sizeof(TESTChoice1),
928 (generic_encode)encode_TESTChoice1,
929 (generic_length)length_TESTChoice1,
930 (generic_decode)decode_TESTChoice1,
931 (generic_free)free_TESTChoice1,
932 cmp_TESTChoice,
933 (generic_copy)copy_TESTChoice1);
935 memset(&c2_2, 0, sizeof(c2_2));
936 c2_2.element = choice_TESTChoice2_asn1_ellipsis;
937 c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
938 c2_2.u.asn1_ellipsis.length = 5;
939 tests[1].val = &c2_2;
941 ret += generic_test (tests, ntests, sizeof(TESTChoice2),
942 (generic_encode)encode_TESTChoice2,
943 (generic_length)length_TESTChoice2,
944 (generic_decode)decode_TESTChoice2,
945 (generic_free)free_TESTChoice2,
946 cmp_TESTChoice,
947 (generic_copy)copy_TESTChoice2);
949 return ret;
952 static int
953 cmp_TESTImplicit (void *a, void *b)
955 TESTImplicit *aa = a;
956 TESTImplicit *ab = b;
958 COMPARE_INTEGER(aa,ab,ti1);
959 COMPARE_INTEGER(aa,ab,ti2.foo);
960 COMPARE_INTEGER(aa,ab,ti3);
961 return 0;
965 UNIV CONS Sequence 14
966 CONTEXT PRIM 0 1 00
967 CONTEXT CONS 1 6
968 CONTEXT CONS 127 3
969 UNIV PRIM Integer 1 02
970 CONTEXT PRIM 2 1 03
973 static int
974 test_implicit (void)
976 struct test_case tests[] = {
977 { NULL, 16,
978 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
979 "\x7f\x03\x02\x01\x02\x82\x01\x03",
980 "implicit 1" }
983 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
984 TESTImplicit c0;
986 memset(&c0, 0, sizeof(c0));
987 c0.ti1 = 0;
988 c0.ti2.foo = 2;
989 c0.ti3 = 3;
990 tests[0].val = &c0;
992 ret += generic_test (tests, ntests, sizeof(TESTImplicit),
993 (generic_encode)encode_TESTImplicit,
994 (generic_length)length_TESTImplicit,
995 (generic_decode)decode_TESTImplicit,
996 (generic_free)free_TESTImplicit,
997 cmp_TESTImplicit,
998 (generic_copy)copy_TESTImplicit);
1000 #ifdef IMPLICIT_TAGGING_WORKS
1001 ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
1002 (generic_encode)encode_TESTImplicit2,
1003 (generic_length)length_TESTImplicit2,
1004 (generic_decode)decode_TESTImplicit2,
1005 (generic_free)free_TESTImplicit2,
1006 cmp_TESTImplicit,
1007 NULL);
1009 #endif /* IMPLICIT_TAGGING_WORKS */
1010 return ret;
1013 static int
1014 cmp_TESTAlloc (void *a, void *b)
1016 TESTAlloc *aa = a;
1017 TESTAlloc *ab = b;
1019 IF_OPT_COMPARE(aa,ab,tagless) {
1020 COMPARE_INTEGER(aa,ab,tagless->ai);
1023 COMPARE_INTEGER(aa,ab,three);
1025 IF_OPT_COMPARE(aa,ab,tagless2) {
1026 COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
1029 return 0;
1033 UNIV CONS Sequence 12
1034 UNIV CONS Sequence 5
1035 CONTEXT CONS 0 3
1036 UNIV PRIM Integer 1 01
1037 CONTEXT CONS 1 3
1038 UNIV PRIM Integer 1 03
1040 UNIV CONS Sequence 5
1041 CONTEXT CONS 1 3
1042 UNIV PRIM Integer 1 03
1044 UNIV CONS Sequence 8
1045 CONTEXT CONS 1 3
1046 UNIV PRIM Integer 1 04
1047 UNIV PRIM Integer 1 05
1051 static int
1052 test_taglessalloc (void)
1054 struct test_case tests[] = {
1055 { NULL, 14,
1056 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
1057 "alloc 1" },
1058 { NULL, 7,
1059 "\x30\x05\xa1\x03\x02\x01\x03",
1060 "alloc 2" },
1061 { NULL, 10,
1062 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
1063 "alloc 3" }
1066 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1067 TESTAlloc c1, c2, c3;
1068 heim_any any3;
1070 memset(&c1, 0, sizeof(c1));
1071 c1.tagless = ecalloc(1, sizeof(*c1.tagless));
1072 c1.tagless->ai = 1;
1073 c1.three = 3;
1074 tests[0].val = &c1;
1076 memset(&c2, 0, sizeof(c2));
1077 c2.tagless = NULL;
1078 c2.three = 3;
1079 tests[1].val = &c2;
1081 memset(&c3, 0, sizeof(c3));
1082 c3.tagless = NULL;
1083 c3.three = 4;
1084 c3.tagless2 = &any3;
1085 any3.data = "\x02\x01\x05";
1086 any3.length = 3;
1087 tests[2].val = &c3;
1089 ret += generic_test (tests, ntests, sizeof(TESTAlloc),
1090 (generic_encode)encode_TESTAlloc,
1091 (generic_length)length_TESTAlloc,
1092 (generic_decode)decode_TESTAlloc,
1093 (generic_free)free_TESTAlloc,
1094 cmp_TESTAlloc,
1095 (generic_copy)copy_TESTAlloc);
1097 free(c1.tagless);
1099 return ret;
1102 static int
1103 cmp_TESTOptional (void *a, void *b)
1105 TESTOptional *aa = a;
1106 TESTOptional *ab = b;
1108 IF_OPT_COMPARE(aa,ab,zero) {
1109 COMPARE_OPT_INTEGER(aa,ab,zero);
1111 IF_OPT_COMPARE(aa,ab,one) {
1112 COMPARE_OPT_INTEGER(aa,ab,one);
1114 return 0;
1118 UNIV CONS Sequence 5
1119 CONTEXT CONS 0 3
1120 UNIV PRIM Integer 1 00
1122 UNIV CONS Sequence 5
1123 CONTEXT CONS 1 3
1124 UNIV PRIM Integer 1 03
1126 UNIV CONS Sequence 10
1127 CONTEXT CONS 0 3
1128 UNIV PRIM Integer 1 00
1129 CONTEXT CONS 1 3
1130 UNIV PRIM Integer 1 01
1134 static int
1135 test_optional (void)
1137 struct test_case tests[] = {
1138 { NULL, 2,
1139 "\x30\x00",
1140 "optional 0" },
1141 { NULL, 7,
1142 "\x30\x05\xa0\x03\x02\x01\x00",
1143 "optional 1" },
1144 { NULL, 7,
1145 "\x30\x05\xa1\x03\x02\x01\x01",
1146 "optional 2" },
1147 { NULL, 12,
1148 "\x30\x0a\xa0\x03\x02\x01\x00\xa1\x03\x02\x01\x01",
1149 "optional 3" }
1152 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1153 TESTOptional c0, c1, c2, c3;
1154 int zero = 0;
1155 int one = 1;
1157 c0.zero = NULL;
1158 c0.one = NULL;
1159 tests[0].val = &c0;
1161 c1.zero = &zero;
1162 c1.one = NULL;
1163 tests[1].val = &c1;
1165 c2.zero = NULL;
1166 c2.one = &one;
1167 tests[2].val = &c2;
1169 c3.zero = &zero;
1170 c3.one = &one;
1171 tests[3].val = &c3;
1173 ret += generic_test (tests, ntests, sizeof(TESTOptional),
1174 (generic_encode)encode_TESTOptional,
1175 (generic_length)length_TESTOptional,
1176 (generic_decode)decode_TESTOptional,
1177 (generic_free)free_TESTOptional,
1178 cmp_TESTOptional,
1179 (generic_copy)copy_TESTOptional);
1181 return ret;
1184 static int
1185 check_fail_largetag(void)
1187 struct test_case tests[] = {
1188 {NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
1189 "tag overflow"},
1190 {NULL, 0, "", "empty buffer"},
1191 {NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
1192 "one too short" },
1193 {NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01",
1194 "two too short" },
1195 {NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
1196 "three too short" },
1197 {NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
1198 "four too short" },
1199 {NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
1200 "five too short" },
1201 {NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
1202 "six too short" },
1203 {NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
1204 "inner one too long" },
1205 {NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
1206 "inner one too short" },
1207 {NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
1208 "inner one too short"},
1209 {NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
1210 "wrong tag"},
1211 {NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
1212 "still wrong tag"}
1214 int ntests = sizeof(tests) / sizeof(*tests);
1216 return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
1217 (generic_decode)decode_TESTLargeTag);
1221 static int
1222 check_fail_sequence(void)
1224 struct test_case tests[] = {
1225 {NULL, 0, "", "empty buffer"},
1226 {NULL, 24,
1227 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1228 "\x02\x01\x01\xa2\x03\x02\x01\x01",
1229 "missing one byte from the end, internal length ok"},
1230 {NULL, 25,
1231 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1232 "\x02\x01\x01\xa2\x03\x02\x01\x01",
1233 "inner length one byte too long"},
1234 {NULL, 24,
1235 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
1236 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
1237 "correct buffer but missing one too short"}
1239 int ntests = sizeof(tests) / sizeof(*tests);
1241 return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
1242 (generic_decode)decode_TESTSeq);
1245 static int
1246 check_fail_choice(void)
1248 struct test_case tests[] = {
1249 {NULL, 6,
1250 "\xa1\x02\x02\x01\x01",
1251 "choice one too short"},
1252 {NULL, 6,
1253 "\xa1\x03\x02\x02\x01",
1254 "choice one too short inner"}
1256 int ntests = sizeof(tests) / sizeof(*tests);
1258 return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
1259 (generic_decode)decode_TESTChoice1);
1262 static int
1263 check_seq(void)
1265 TESTSeqOf seq;
1266 TESTInteger i;
1267 int ret;
1269 seq.val = NULL;
1270 seq.len = 0;
1272 ret = add_TESTSeqOf(&seq, &i);
1273 if (ret) { printf("failed adding\n"); goto out; }
1274 ret = add_TESTSeqOf(&seq, &i);
1275 if (ret) { printf("failed adding\n"); goto out; }
1276 ret = add_TESTSeqOf(&seq, &i);
1277 if (ret) { printf("failed adding\n"); goto out; }
1278 ret = add_TESTSeqOf(&seq, &i);
1279 if (ret) { printf("failed adding\n"); goto out; }
1281 ret = remove_TESTSeqOf(&seq, seq.len - 1);
1282 if (ret) { printf("failed removing\n"); goto out; }
1283 ret = remove_TESTSeqOf(&seq, 2);
1284 if (ret) { printf("failed removing\n"); goto out; }
1285 ret = remove_TESTSeqOf(&seq, 0);
1286 if (ret) { printf("failed removing\n"); goto out; }
1287 ret = remove_TESTSeqOf(&seq, 0);
1288 if (ret) { printf("failed removing\n"); goto out; }
1289 ret = remove_TESTSeqOf(&seq, 0);
1290 if (ret == 0) {
1291 printf("can remove from empty list");
1292 return 1;
1295 if (seq.len != 0) {
1296 printf("seq not empty!");
1297 return 1;
1299 free_TESTSeqOf(&seq);
1300 ret = 0;
1302 out:
1304 return ret;
1307 #define test_seq_of(type, ok, ptr) \
1309 heim_octet_string os; \
1310 size_t size; \
1311 type decode; \
1312 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
1313 if (ret) \
1314 return ret; \
1315 if (os.length != size) \
1316 abort(); \
1317 ret = decode_##type(os.data, os.length, &decode, &size); \
1318 free(os.data); \
1319 if (ret) { \
1320 if (ok) \
1321 return 1; \
1322 } else { \
1323 free_##type(&decode); \
1324 if (!ok) \
1325 return 1; \
1326 if (size != 0) \
1327 return 1; \
1329 return 0; \
1332 static int
1333 check_seq_of_size(void)
1335 #if 0 /* template */
1336 TESTInteger integers[4] = { 1, 2, 3, 4 };
1337 int ret;
1340 TESTSeqSizeOf1 ssof1f1 = { 1, integers };
1341 TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
1342 TESTSeqSizeOf1 ssof1f2 = { 3, integers };
1344 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
1345 test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
1346 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
1349 TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
1350 TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
1351 TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
1352 TESTSeqSizeOf2 ssof2f2 = { 3, integers };
1354 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f1);
1355 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok1);
1356 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok2);
1357 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f2);
1360 TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
1361 TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
1362 TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
1364 test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
1365 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
1366 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
1369 TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
1370 TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
1371 TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
1372 TESTSeqSizeOf4 ssof4f1 = { 3, integers };
1374 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok1);
1375 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok2);
1376 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok3);
1377 test_seq_of(TESTSeqSizeOf4, 0, &ssof4f1);
1379 #endif
1380 return 0;
1383 static int
1384 check_TESTMechTypeList(void)
1386 TESTMechTypeList tl;
1387 unsigned oid1[] = { 1, 2, 840, 48018, 1, 2, 2};
1388 unsigned oid2[] = { 1, 2, 840, 113554, 1, 2, 2};
1389 unsigned oid3[] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 30};
1390 unsigned oid4[] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10};
1391 TESTMechType array[] = {{ 7, oid1 },
1392 { 7, oid2 },
1393 { 10, oid3 },
1394 { 10, oid4 }};
1395 size_t size, len;
1396 void *ptr;
1397 int ret;
1399 tl.len = 4;
1400 tl.val = array;
1402 ASN1_MALLOC_ENCODE(TESTMechTypeList, ptr, len, &tl, &size, ret);
1403 if (ret)
1404 errx(1, "TESTMechTypeList: %d", ret);
1405 if (len != size)
1406 abort();
1407 return 0;
1411 main(int argc, char **argv)
1413 int ret = 0;
1415 ret += test_principal ();
1416 ret += test_authenticator();
1417 ret += test_krb_error();
1418 ret += test_Name();
1419 ret += test_bit_string();
1420 ret += test_bit_string_rfc1510();
1421 ret += test_time();
1422 ret += test_cert();
1424 ret += check_tag_length();
1425 ret += check_tag_length64();
1426 ret += check_tag_length64s();
1427 ret += test_large_tag();
1428 ret += test_choice();
1430 ret += test_implicit();
1431 ret += test_taglessalloc();
1432 ret += test_optional();
1434 ret += check_fail_largetag();
1435 ret += check_fail_sequence();
1436 ret += check_fail_choice();
1438 ret += check_seq();
1439 ret += check_seq_of_size();
1441 ret += check_TESTMechTypeList();
1443 return ret;