s4:heimdal A real fix for bug 6801
[heimdal.git] / lib / asn1 / check-gen.c
blob9adf4f3e0f2654efd22f65fb676b76962ab2ecd2
1 /*
2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <string.h>
39 #include <err.h>
40 #include <roken.h>
42 #include <asn1-common.h>
43 #include <asn1_err.h>
44 #include <der.h>
45 #include <krb5_asn1.h>
46 #include <heim_asn1.h>
47 #include <rfc2459_asn1.h>
48 #include <test_asn1.h>
50 #include "check-common.h"
52 RCSID("$Id$");
54 static char *lha_principal[] = { "lha" };
55 static char *lharoot_princ[] = { "lha", "root" };
56 static char *datan_princ[] = { "host", "nutcracker.e.kth.se" };
57 static char *nada_tgt_principal[] = { "krbtgt", "NADA.KTH.SE" };
60 #define IF_OPT_COMPARE(ac,bc,e) \
61 if (((ac)->e == NULL && (bc)->e != NULL) || (((ac)->e != NULL && (bc)->e == NULL))) return 1; if ((ab)->e)
62 #define COMPARE_OPT_STRING(ac,bc,e) \
63 do { if (strcmp(*(ac)->e, *(bc)->e) != 0) return 1; } while(0)
64 #define COMPARE_OPT_OCTECT_STRING(ac,bc,e) \
65 do { if ((ac)->e->length != (bc)->e->length || memcmp((ac)->e->data, (bc)->e->data, (ac)->e->length) != 0) return 1; } while(0)
66 #define COMPARE_STRING(ac,bc,e) \
67 do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
68 #define COMPARE_INTEGER(ac,bc,e) \
69 do { if ((ac)->e != (bc)->e) return 1; } while(0)
70 #define COMPARE_MEM(ac,bc,e,len) \
71 do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
73 static int
74 cmp_principal (void *a, void *b)
76 Principal *pa = a;
77 Principal *pb = b;
78 int i;
80 COMPARE_STRING(pa,pb,realm);
81 COMPARE_INTEGER(pa,pb,name.name_type);
82 COMPARE_INTEGER(pa,pb,name.name_string.len);
84 for (i = 0; i < pa->name.name_string.len; i++)
85 COMPARE_STRING(pa,pb,name.name_string.val[i]);
87 return 0;
90 static int
91 test_principal (void)
94 struct test_case tests[] = {
95 { NULL, 29,
96 "\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
97 "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45"
99 { NULL, 35,
100 "\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
101 "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
102 "\x2e\x53\x45"
104 { NULL, 54,
105 "\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
106 "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
107 "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
108 "\x4b\x54\x48\x2e\x53\x45"
113 Principal values[] = {
114 { { KRB5_NT_PRINCIPAL, { 1, lha_principal } }, "SU.SE" },
115 { { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } }, "SU.SE" },
116 { { KRB5_NT_SRV_HST, { 2, datan_princ } }, "E.KTH.SE" }
118 int i, ret;
119 int ntests = sizeof(tests) / sizeof(*tests);
121 for (i = 0; i < ntests; ++i) {
122 tests[i].val = &values[i];
123 asprintf (&tests[i].name, "Principal %d", i);
126 ret = generic_test (tests, ntests, sizeof(Principal),
127 (generic_encode)encode_Principal,
128 (generic_length)length_Principal,
129 (generic_decode)decode_Principal,
130 (generic_free)free_Principal,
131 cmp_principal);
132 for (i = 0; i < ntests; ++i)
133 free (tests[i].name);
135 return ret;
138 static int
139 cmp_authenticator (void *a, void *b)
141 Authenticator *aa = a;
142 Authenticator *ab = b;
143 int i;
145 COMPARE_INTEGER(aa,ab,authenticator_vno);
146 COMPARE_STRING(aa,ab,crealm);
148 COMPARE_INTEGER(aa,ab,cname.name_type);
149 COMPARE_INTEGER(aa,ab,cname.name_string.len);
151 for (i = 0; i < aa->cname.name_string.len; i++)
152 COMPARE_STRING(aa,ab,cname.name_string.val[i]);
154 return 0;
157 static int
158 test_authenticator (void)
160 struct test_case tests[] = {
161 { NULL, 63,
162 "\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
163 "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
164 "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
165 "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
166 "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a"
168 { NULL, 67,
169 "\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
170 "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
171 "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
172 "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
173 "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
174 "\x39\x5a"
178 Authenticator values[] = {
179 { 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL, { 1, lha_principal } },
180 NULL, 10, 99, NULL, NULL, NULL },
181 { 5, "SU.SE", { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },
182 NULL, 292, 999, NULL, NULL, NULL }
184 int i, ret;
185 int ntests = sizeof(tests) / sizeof(*tests);
187 for (i = 0; i < ntests; ++i) {
188 tests[i].val = &values[i];
189 asprintf (&tests[i].name, "Authenticator %d", i);
192 ret = generic_test (tests, ntests, sizeof(Authenticator),
193 (generic_encode)encode_Authenticator,
194 (generic_length)length_Authenticator,
195 (generic_decode)decode_Authenticator,
196 (generic_free)free_Authenticator,
197 cmp_authenticator);
198 for (i = 0; i < ntests; ++i)
199 free(tests[i].name);
201 return ret;
204 static int
205 cmp_KRB_ERROR (void *a, void *b)
207 KRB_ERROR *aa = a;
208 KRB_ERROR *ab = b;
209 int i;
211 COMPARE_INTEGER(aa,ab,pvno);
212 COMPARE_INTEGER(aa,ab,msg_type);
214 IF_OPT_COMPARE(aa,ab,ctime) {
215 COMPARE_INTEGER(aa,ab,ctime);
217 IF_OPT_COMPARE(aa,ab,cusec) {
218 COMPARE_INTEGER(aa,ab,cusec);
220 COMPARE_INTEGER(aa,ab,stime);
221 COMPARE_INTEGER(aa,ab,susec);
222 COMPARE_INTEGER(aa,ab,error_code);
224 IF_OPT_COMPARE(aa,ab,crealm) {
225 COMPARE_OPT_STRING(aa,ab,crealm);
227 #if 0
228 IF_OPT_COMPARE(aa,ab,cname) {
229 COMPARE_OPT_STRING(aa,ab,cname);
231 #endif
232 COMPARE_STRING(aa,ab,realm);
234 COMPARE_INTEGER(aa,ab,sname.name_string.len);
235 for (i = 0; i < aa->sname.name_string.len; i++)
236 COMPARE_STRING(aa,ab,sname.name_string.val[i]);
238 IF_OPT_COMPARE(aa,ab,e_text) {
239 COMPARE_OPT_STRING(aa,ab,e_text);
241 IF_OPT_COMPARE(aa,ab,e_data) {
242 /* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
245 return 0;
248 static int
249 test_krb_error (void)
251 struct test_case tests[] = {
252 { NULL, 127,
253 "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
254 "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
255 "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
256 "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
257 "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
258 "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
259 "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
260 "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
261 "KRB-ERROR Test 1"
264 int ntests = sizeof(tests) / sizeof(*tests);
265 KRB_ERROR e1;
266 PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
267 PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
268 char *realm = "NADA.KTH.SE";
270 e1.pvno = 5;
271 e1.msg_type = 30;
272 e1.ctime = NULL;
273 e1.cusec = NULL;
274 e1.stime = 1069632679;
275 e1.susec = 322981;
276 e1.error_code = 31;
277 e1.crealm = &realm;
278 e1.cname = &lhaprincipalname;
279 e1.realm = "NADA.KTH.SE";
280 e1.sname = tgtprincipalname;
281 e1.e_text = NULL;
282 e1.e_data = NULL;
284 tests[0].val = &e1;
286 return generic_test (tests, ntests, sizeof(KRB_ERROR),
287 (generic_encode)encode_KRB_ERROR,
288 (generic_length)length_KRB_ERROR,
289 (generic_decode)decode_KRB_ERROR,
290 (generic_free)free_KRB_ERROR,
291 cmp_KRB_ERROR);
294 static int
295 cmp_Name (void *a, void *b)
297 Name *aa = a;
298 Name *ab = b;
300 COMPARE_INTEGER(aa,ab,element);
302 return 0;
305 static int
306 test_Name (void)
308 struct test_case tests[] = {
309 { NULL, 35,
310 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
311 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
312 "\x4f\x4c\x4d",
313 "Name CN=Love+L=STOCKHOLM"
315 { NULL, 35,
316 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
317 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
318 "\x4f\x4c\x4d",
319 "Name L=STOCKHOLM+CN=Love"
323 int ntests = sizeof(tests) / sizeof(*tests);
324 Name n1, n2;
325 RelativeDistinguishedName rdn1[1];
326 RelativeDistinguishedName rdn2[1];
327 AttributeTypeAndValue atv1[2];
328 AttributeTypeAndValue atv2[2];
329 unsigned cmp_CN[] = { 2, 5, 4, 3 };
330 unsigned cmp_L[] = { 2, 5, 4, 7 };
332 /* n1 */
333 n1.element = choice_Name_rdnSequence;
334 n1.u.rdnSequence.val = rdn1;
335 n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
336 rdn1[0].val = atv1;
337 rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
339 atv1[0].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
340 atv1[0].type.components = cmp_CN;
341 atv1[0].value.element = choice_DirectoryString_printableString;
342 atv1[0].value.u.printableString = "Love";
344 atv1[1].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
345 atv1[1].type.components = cmp_L;
346 atv1[1].value.element = choice_DirectoryString_printableString;
347 atv1[1].value.u.printableString = "STOCKHOLM";
349 /* n2 */
350 n2.element = choice_Name_rdnSequence;
351 n2.u.rdnSequence.val = rdn2;
352 n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
353 rdn2[0].val = atv2;
354 rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
356 atv2[0].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
357 atv2[0].type.components = cmp_L;
358 atv2[0].value.element = choice_DirectoryString_printableString;
359 atv2[0].value.u.printableString = "STOCKHOLM";
361 atv2[1].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
362 atv2[1].type.components = cmp_CN;
363 atv2[1].value.element = choice_DirectoryString_printableString;
364 atv2[1].value.u.printableString = "Love";
366 /* */
367 tests[0].val = &n1;
368 tests[1].val = &n2;
370 return generic_test (tests, ntests, sizeof(Name),
371 (generic_encode)encode_Name,
372 (generic_length)length_Name,
373 (generic_decode)decode_Name,
374 (generic_free)free_Name,
375 cmp_Name);
378 static int
379 cmp_KeyUsage (void *a, void *b)
381 KeyUsage *aa = a;
382 KeyUsage *ab = b;
384 return KeyUsage2int(*aa) != KeyUsage2int(*ab);
387 static int
388 test_bit_string (void)
390 struct test_case tests[] = {
391 { NULL, 4,
392 "\x03\x02\x07\x80",
393 "bitstring 1"
395 { NULL, 4,
396 "\x03\x02\x05\xa0",
397 "bitstring 2"
399 { NULL, 5,
400 "\x03\x03\x07\x00\x80",
401 "bitstring 3"
403 { NULL, 3,
404 "\x03\x01\x00",
405 "bitstring 4"
409 int ntests = sizeof(tests) / sizeof(*tests);
410 KeyUsage ku1, ku2, ku3, ku4;
412 memset(&ku1, 0, sizeof(ku1));
413 ku1.digitalSignature = 1;
414 tests[0].val = &ku1;
416 memset(&ku2, 0, sizeof(ku2));
417 ku2.digitalSignature = 1;
418 ku2.keyEncipherment = 1;
419 tests[1].val = &ku2;
421 memset(&ku3, 0, sizeof(ku3));
422 ku3.decipherOnly = 1;
423 tests[2].val = &ku3;
425 memset(&ku4, 0, sizeof(ku4));
426 tests[3].val = &ku4;
429 return generic_test (tests, ntests, sizeof(KeyUsage),
430 (generic_encode)encode_KeyUsage,
431 (generic_length)length_KeyUsage,
432 (generic_decode)decode_KeyUsage,
433 (generic_free)free_KeyUsage,
434 cmp_KeyUsage);
437 static int
438 cmp_TESTLargeTag (void *a, void *b)
440 TESTLargeTag *aa = a;
441 TESTLargeTag *ab = b;
443 COMPARE_INTEGER(aa,ab,foo);
444 return 0;
447 static int
448 test_large_tag (void)
450 struct test_case tests[] = {
451 { NULL, 8, "\x30\x06\xbf\x7f\x03\x02\x01\x01", "large tag 1" }
454 int ntests = sizeof(tests) / sizeof(*tests);
455 TESTLargeTag lt1;
457 memset(&lt1, 0, sizeof(lt1));
458 lt1.foo = 1;
460 tests[0].val = &lt1;
462 return generic_test (tests, ntests, sizeof(TESTLargeTag),
463 (generic_encode)encode_TESTLargeTag,
464 (generic_length)length_TESTLargeTag,
465 (generic_decode)decode_TESTLargeTag,
466 (generic_free)free_TESTLargeTag,
467 cmp_TESTLargeTag);
470 struct test_data {
471 int ok;
472 size_t len;
473 size_t expected_len;
474 void *data;
477 static int
478 check_tag_length(void)
480 struct test_data td[] = {
481 { 1, 3, 3, "\x02\x01\x00"},
482 { 1, 3, 3, "\x02\x01\x7f"},
483 { 1, 4, 4, "\x02\x02\x00\x80"},
484 { 1, 4, 4, "\x02\x02\x01\x00"},
485 { 1, 4, 4, "\x02\x02\x02\x00"},
486 { 0, 3, 0, "\x02\x02\x00"},
487 { 0, 3, 0, "\x02\x7f\x7f"},
488 { 0, 4, 0, "\x02\x03\x00\x80"},
489 { 0, 4, 0, "\x02\x7f\x01\x00"},
490 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
492 size_t sz;
493 krb5uint32 values[] = {0, 127, 128, 256, 512,
494 0, 127, 128, 256, 512 };
495 krb5uint32 u;
496 int i, ret, failed = 0;
497 void *buf;
499 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
500 struct map_page *page;
502 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
504 ret = decode_krb5uint32(buf, td[i].len, &u, &sz);
505 if (ret) {
506 if (td[i].ok) {
507 printf("failed with tag len test %d\n", i);
508 failed = 1;
510 } else {
511 if (td[i].ok == 0) {
512 printf("failed with success for tag len test %d\n", i);
513 failed = 1;
515 if (td[i].expected_len != sz) {
516 printf("wrong expected size for tag test %d\n", i);
517 failed = 1;
519 if (values[i] != u) {
520 printf("wrong value for tag test %d\n", i);
521 failed = 1;
524 map_free(page, "test", "decode");
526 return failed;
529 static int
530 cmp_TESTChoice (void *a, void *b)
532 return 0;
535 static int
536 test_choice (void)
538 struct test_case tests[] = {
539 { NULL, 5, "\xa1\x03\x02\x01\x01", "large choice 1" },
540 { NULL, 5, "\xa2\x03\x02\x01\x02", "large choice 2" }
543 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
544 TESTChoice1 c1;
545 TESTChoice1 c2_1;
546 TESTChoice2 c2_2;
548 memset(&c1, 0, sizeof(c1));
549 c1.element = choice_TESTChoice1_i1;
550 c1.u.i1 = 1;
551 tests[0].val = &c1;
553 memset(&c2_1, 0, sizeof(c2_1));
554 c2_1.element = choice_TESTChoice1_i2;
555 c2_1.u.i2 = 2;
556 tests[1].val = &c2_1;
558 ret += generic_test (tests, ntests, sizeof(TESTChoice1),
559 (generic_encode)encode_TESTChoice1,
560 (generic_length)length_TESTChoice1,
561 (generic_decode)decode_TESTChoice1,
562 (generic_free)free_TESTChoice1,
563 cmp_TESTChoice);
565 memset(&c2_2, 0, sizeof(c2_2));
566 c2_2.element = choice_TESTChoice2_asn1_ellipsis;
567 c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
568 c2_2.u.asn1_ellipsis.length = 5;
569 tests[1].val = &c2_2;
571 ret += generic_test (tests, ntests, sizeof(TESTChoice2),
572 (generic_encode)encode_TESTChoice2,
573 (generic_length)length_TESTChoice2,
574 (generic_decode)decode_TESTChoice2,
575 (generic_free)free_TESTChoice2,
576 cmp_TESTChoice);
578 return ret;
581 static int
582 cmp_TESTImplicit (void *a, void *b)
584 TESTImplicit *aa = a;
585 TESTImplicit *ab = b;
587 COMPARE_INTEGER(aa,ab,ti1);
588 COMPARE_INTEGER(aa,ab,ti2.foo);
589 COMPARE_INTEGER(aa,ab,ti3);
590 return 0;
594 UNIV CONS Sequence 14
595 CONTEXT PRIM 0 1 00
596 CONTEXT CONS 1 6
597 CONTEXT CONS 127 3
598 UNIV PRIM Integer 1 02
599 CONTEXT PRIM 2 1 03
602 static int
603 test_implicit (void)
605 struct test_case tests[] = {
606 { NULL, 16,
607 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
608 "\x7f\x03\x02\x01\x02\x82\x01\x03",
609 "implicit 1" }
612 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
613 TESTImplicit c0;
615 memset(&c0, 0, sizeof(c0));
616 c0.ti1 = 0;
617 c0.ti2.foo = 2;
618 c0.ti3 = 3;
619 tests[0].val = &c0;
621 ret += generic_test (tests, ntests, sizeof(TESTImplicit),
622 (generic_encode)encode_TESTImplicit,
623 (generic_length)length_TESTImplicit,
624 (generic_decode)decode_TESTImplicit,
625 (generic_free)free_TESTImplicit,
626 cmp_TESTImplicit);
628 #ifdef IMPLICIT_TAGGING_WORKS
629 ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
630 (generic_encode)encode_TESTImplicit2,
631 (generic_length)length_TESTImplicit2,
632 (generic_decode)decode_TESTImplicit2,
633 (generic_free)free_TESTImplicit2,
634 cmp_TESTImplicit);
636 #endif /* IMPLICIT_TAGGING_WORKS */
637 return ret;
640 static int
641 cmp_TESTAlloc (void *a, void *b)
643 TESTAlloc *aa = a;
644 TESTAlloc *ab = b;
646 IF_OPT_COMPARE(aa,ab,tagless) {
647 COMPARE_INTEGER(aa,ab,tagless->ai);
650 COMPARE_INTEGER(aa,ab,three);
652 IF_OPT_COMPARE(aa,ab,tagless2) {
653 COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
656 return 0;
660 UNIV CONS Sequence 12
661 UNIV CONS Sequence 5
662 CONTEXT CONS 0 3
663 UNIV PRIM Integer 1 01
664 CONTEXT CONS 1 3
665 UNIV PRIM Integer 1 03
667 UNIV CONS Sequence 5
668 CONTEXT CONS 1 3
669 UNIV PRIM Integer 1 03
671 UNIV CONS Sequence 8
672 CONTEXT CONS 1 3
673 UNIV PRIM Integer 1 04
674 UNIV PRIM Integer 1 05
678 static int
679 test_taglessalloc (void)
681 struct test_case tests[] = {
682 { NULL, 14,
683 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
684 "alloc 1" },
685 { NULL, 7,
686 "\x30\x05\xa1\x03\x02\x01\x03",
687 "alloc 2" },
688 { NULL, 10,
689 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
690 "alloc 3" }
693 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
694 TESTAlloc c1, c2, c3;
695 heim_any any3;
697 memset(&c1, 0, sizeof(c1));
698 c1.tagless = ecalloc(1, sizeof(*c1.tagless));
699 c1.tagless->ai = 1;
700 c1.three = 3;
701 tests[0].val = &c1;
703 memset(&c2, 0, sizeof(c2));
704 c2.tagless = NULL;
705 c2.three = 3;
706 tests[1].val = &c2;
708 memset(&c3, 0, sizeof(c3));
709 c3.tagless = NULL;
710 c3.three = 4;
711 c3.tagless2 = &any3;
712 any3.data = "\x02\x01\x05";
713 any3.length = 3;
714 tests[2].val = &c3;
716 ret += generic_test (tests, ntests, sizeof(TESTAlloc),
717 (generic_encode)encode_TESTAlloc,
718 (generic_length)length_TESTAlloc,
719 (generic_decode)decode_TESTAlloc,
720 (generic_free)free_TESTAlloc,
721 cmp_TESTAlloc);
723 free(c1.tagless);
725 return ret;
729 static int
730 check_fail_largetag(void)
732 struct test_case tests[] = {
733 {NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
734 "tag overflow"},
735 {NULL, 0, "", "empty buffer"},
736 {NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
737 "one too short" },
738 {NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
739 "two too short" },
740 {NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
741 "three too short" },
742 {NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
743 "four too short" },
744 {NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
745 "five too short" },
746 {NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
747 "six too short" },
748 {NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
749 "inner one too long" },
750 {NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
751 "inner one too short" },
752 {NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
753 "inner one too short"},
754 {NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
755 "wrong tag"},
756 {NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
757 "still wrong tag"}
759 int ntests = sizeof(tests) / sizeof(*tests);
761 return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
762 (generic_decode)decode_TESTLargeTag);
766 static int
767 check_fail_sequence(void)
769 struct test_case tests[] = {
770 {NULL, 0, "", "empty buffer"},
771 {NULL, 24,
772 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
773 "\x02\x01\x01\xa2\x03\x02\x01\x01"
774 "missing one byte from the end, internal length ok"},
775 {NULL, 25,
776 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
777 "\x02\x01\x01\xa2\x03\x02\x01\x01",
778 "inner length one byte too long"},
779 {NULL, 24,
780 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
781 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
782 "correct buffer but missing one too short"}
784 int ntests = sizeof(tests) / sizeof(*tests);
786 return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
787 (generic_decode)decode_TESTSeq);
790 static int
791 check_fail_choice(void)
793 struct test_case tests[] = {
794 {NULL, 6,
795 "\xa1\x02\x02\x01\x01",
796 "one too short"},
797 {NULL, 6,
798 "\xa1\x03\x02\x02\x01",
799 "one too short inner"}
801 int ntests = sizeof(tests) / sizeof(*tests);
803 return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
804 (generic_decode)decode_TESTChoice1);
807 static int
808 check_seq(void)
810 TESTSeqOf seq;
811 TESTInteger i;
812 int ret;
814 seq.val = NULL;
815 seq.len = 0;
817 ret = add_TESTSeqOf(&seq, &i);
818 if (ret) { printf("failed adding\n"); goto out; }
819 ret = add_TESTSeqOf(&seq, &i);
820 if (ret) { printf("failed adding\n"); goto out; }
821 ret = add_TESTSeqOf(&seq, &i);
822 if (ret) { printf("failed adding\n"); goto out; }
823 ret = add_TESTSeqOf(&seq, &i);
824 if (ret) { printf("failed adding\n"); goto out; }
826 ret = remove_TESTSeqOf(&seq, seq.len - 1);
827 if (ret) { printf("failed removing\n"); goto out; }
828 ret = remove_TESTSeqOf(&seq, 2);
829 if (ret) { printf("failed removing\n"); goto out; }
830 ret = remove_TESTSeqOf(&seq, 0);
831 if (ret) { printf("failed removing\n"); goto out; }
832 ret = remove_TESTSeqOf(&seq, 0);
833 if (ret) { printf("failed removing\n"); goto out; }
834 ret = remove_TESTSeqOf(&seq, 0);
835 if (ret == 0) {
836 printf("can remove from empty list");
837 return 1;
840 if (seq.len != 0) {
841 printf("seq not empty!");
842 return 1;
844 free_TESTSeqOf(&seq);
845 ret = 0;
847 out:
849 return ret;
852 #define test_seq_of(type, ok, ptr) \
854 heim_octet_string os; \
855 size_t size; \
856 type decode; \
857 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
858 if (ret) \
859 return ret; \
860 if (os.length != size) \
861 abort(); \
862 ret = decode_##type(os.data, os.length, &decode, &size); \
863 free(os.data); \
864 if (ret) { \
865 if (ok) \
866 return 1; \
867 } else { \
868 free_##type(&decode); \
869 if (!ok) \
870 return 1; \
871 if (size != 0) \
872 return 1; \
874 return 0; \
877 static int
878 check_seq_of_size(void)
880 TESTInteger integers[4] = { 1, 2, 3, 4 };
881 int ret;
884 TESTSeqSizeOf1 ssof1f1 = { 1, integers };
885 TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
886 TESTSeqSizeOf1 ssof1f2 = { 3, integers };
888 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
889 test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
890 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
893 TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
894 TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
895 TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
896 TESTSeqSizeOf2 ssof2f2 = { 3, integers };
898 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f1);
899 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok1);
900 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok2);
901 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f2);
904 TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
905 TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
906 TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
908 test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
909 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
910 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
913 TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
914 TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
915 TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
916 TESTSeqSizeOf4 ssof4f1 = { 3, integers };
918 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok1);
919 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok2);
920 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok3);
921 test_seq_of(TESTSeqSizeOf4, 0, &ssof4f1);
924 return 0;
930 main(int argc, char **argv)
932 int ret = 0;
934 ret += test_principal ();
935 ret += test_authenticator();
936 ret += test_krb_error();
937 ret += test_Name();
938 ret += test_bit_string();
940 ret += check_tag_length();
941 ret += test_large_tag();
942 ret += test_choice();
944 ret += test_implicit();
945 ret += test_taglessalloc();
947 ret += check_fail_largetag();
948 ret += check_fail_sequence();
949 ret += check_fail_choice();
951 ret += check_seq();
952 ret += check_seq_of_size();
954 return ret;