Release 0.8.1
[heimdal.git] / lib / asn1 / check-gen.c
blobe7514a82ee322101977fa0eba6acaf8757bd7b68
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;
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 return 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);
200 static int
201 cmp_KRB_ERROR (void *a, void *b)
203 KRB_ERROR *aa = a;
204 KRB_ERROR *ab = b;
205 int i;
207 COMPARE_INTEGER(aa,ab,pvno);
208 COMPARE_INTEGER(aa,ab,msg_type);
210 IF_OPT_COMPARE(aa,ab,ctime) {
211 COMPARE_INTEGER(aa,ab,ctime);
213 IF_OPT_COMPARE(aa,ab,cusec) {
214 COMPARE_INTEGER(aa,ab,cusec);
216 COMPARE_INTEGER(aa,ab,stime);
217 COMPARE_INTEGER(aa,ab,susec);
218 COMPARE_INTEGER(aa,ab,error_code);
220 IF_OPT_COMPARE(aa,ab,crealm) {
221 COMPARE_OPT_STRING(aa,ab,crealm);
223 #if 0
224 IF_OPT_COMPARE(aa,ab,cname) {
225 COMPARE_OPT_STRING(aa,ab,cname);
227 #endif
228 COMPARE_STRING(aa,ab,realm);
230 COMPARE_INTEGER(aa,ab,sname.name_string.len);
231 for (i = 0; i < aa->sname.name_string.len; i++)
232 COMPARE_STRING(aa,ab,sname.name_string.val[i]);
234 IF_OPT_COMPARE(aa,ab,e_text) {
235 COMPARE_OPT_STRING(aa,ab,e_text);
237 IF_OPT_COMPARE(aa,ab,e_data) {
238 /* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
241 return 0;
244 static int
245 test_krb_error (void)
247 struct test_case tests[] = {
248 { NULL, 127,
249 "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
250 "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
251 "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
252 "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
253 "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
254 "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
255 "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
256 "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
257 "KRB-ERROR Test 1"
260 int ntests = sizeof(tests) / sizeof(*tests);
261 KRB_ERROR e1;
262 PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
263 PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
264 char *realm = "NADA.KTH.SE";
266 e1.pvno = 5;
267 e1.msg_type = 30;
268 e1.ctime = NULL;
269 e1.cusec = NULL;
270 e1.stime = 1069632679;
271 e1.susec = 322981;
272 e1.error_code = 31;
273 e1.crealm = &realm;
274 e1.cname = &lhaprincipalname;
275 e1.realm = "NADA.KTH.SE";
276 e1.sname = tgtprincipalname;
277 e1.e_text = NULL;
278 e1.e_data = NULL;
280 tests[0].val = &e1;
282 return generic_test (tests, ntests, sizeof(KRB_ERROR),
283 (generic_encode)encode_KRB_ERROR,
284 (generic_length)length_KRB_ERROR,
285 (generic_decode)decode_KRB_ERROR,
286 (generic_free)free_KRB_ERROR,
287 cmp_KRB_ERROR);
290 static int
291 cmp_Name (void *a, void *b)
293 Name *aa = a;
294 Name *ab = b;
296 COMPARE_INTEGER(aa,ab,element);
298 return 0;
301 static int
302 test_Name (void)
304 struct test_case tests[] = {
305 { NULL, 35,
306 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
307 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
308 "\x4f\x4c\x4d",
309 "Name CN=Love+L=STOCKHOLM"
311 { NULL, 35,
312 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
313 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
314 "\x4f\x4c\x4d",
315 "Name L=STOCKHOLM+CN=Love"
319 int ntests = sizeof(tests) / sizeof(*tests);
320 Name n1, n2;
321 RelativeDistinguishedName rdn1[1];
322 RelativeDistinguishedName rdn2[1];
323 AttributeTypeAndValue atv1[2];
324 AttributeTypeAndValue atv2[2];
325 unsigned cmp_CN[] = { 2, 5, 4, 3 };
326 unsigned cmp_L[] = { 2, 5, 4, 7 };
328 /* n1 */
329 n1.element = choice_Name_rdnSequence;
330 n1.u.rdnSequence.val = rdn1;
331 n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
332 rdn1[0].val = atv1;
333 rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
335 atv1[0].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
336 atv1[0].type.components = cmp_CN;
337 atv1[0].value.element = choice_DirectoryString_printableString;
338 atv1[0].value.u.printableString = "Love";
340 atv1[1].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
341 atv1[1].type.components = cmp_L;
342 atv1[1].value.element = choice_DirectoryString_printableString;
343 atv1[1].value.u.printableString = "STOCKHOLM";
345 /* n2 */
346 n2.element = choice_Name_rdnSequence;
347 n2.u.rdnSequence.val = rdn2;
348 n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
349 rdn2[0].val = atv2;
350 rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
352 atv2[0].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
353 atv2[0].type.components = cmp_L;
354 atv2[0].value.element = choice_DirectoryString_printableString;
355 atv2[0].value.u.printableString = "STOCKHOLM";
357 atv2[1].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
358 atv2[1].type.components = cmp_CN;
359 atv2[1].value.element = choice_DirectoryString_printableString;
360 atv2[1].value.u.printableString = "Love";
362 /* */
363 tests[0].val = &n1;
364 tests[1].val = &n2;
366 return generic_test (tests, ntests, sizeof(Name),
367 (generic_encode)encode_Name,
368 (generic_length)length_Name,
369 (generic_decode)decode_Name,
370 (generic_free)free_Name,
371 cmp_Name);
374 static int
375 cmp_KeyUsage (void *a, void *b)
377 KeyUsage *aa = a;
378 KeyUsage *ab = b;
380 return KeyUsage2int(*aa) != KeyUsage2int(*ab);
383 static int
384 test_bit_string (void)
386 struct test_case tests[] = {
387 { NULL, 4,
388 "\x03\x02\x07\x80",
389 "bitstring 1"
391 { NULL, 4,
392 "\x03\x02\x05\xa0",
393 "bitstring 2"
395 { NULL, 5,
396 "\x03\x03\x07\x00\x80",
397 "bitstring 3"
399 { NULL, 3,
400 "\x03\x01\x00",
401 "bitstring 4"
405 int ntests = sizeof(tests) / sizeof(*tests);
406 KeyUsage ku1, ku2, ku3, ku4;
408 memset(&ku1, 0, sizeof(ku1));
409 ku1.digitalSignature = 1;
410 tests[0].val = &ku1;
412 memset(&ku2, 0, sizeof(ku2));
413 ku2.digitalSignature = 1;
414 ku2.keyEncipherment = 1;
415 tests[1].val = &ku2;
417 memset(&ku3, 0, sizeof(ku3));
418 ku3.decipherOnly = 1;
419 tests[2].val = &ku3;
421 memset(&ku4, 0, sizeof(ku4));
422 tests[3].val = &ku4;
425 return generic_test (tests, ntests, sizeof(KeyUsage),
426 (generic_encode)encode_KeyUsage,
427 (generic_length)length_KeyUsage,
428 (generic_decode)decode_KeyUsage,
429 (generic_free)free_KeyUsage,
430 cmp_KeyUsage);
433 static int
434 cmp_TESTLargeTag (void *a, void *b)
436 TESTLargeTag *aa = a;
437 TESTLargeTag *ab = b;
439 COMPARE_INTEGER(aa,ab,foo);
440 return 0;
443 static int
444 test_large_tag (void)
446 struct test_case tests[] = {
447 { NULL, 8, "\x30\x06\xbf\x7f\x03\x02\x01\x01", "large tag 1" }
450 int ntests = sizeof(tests) / sizeof(*tests);
451 TESTLargeTag lt1;
453 memset(&lt1, 0, sizeof(lt1));
454 lt1.foo = 1;
456 tests[0].val = &lt1;
458 return generic_test (tests, ntests, sizeof(TESTLargeTag),
459 (generic_encode)encode_TESTLargeTag,
460 (generic_length)length_TESTLargeTag,
461 (generic_decode)decode_TESTLargeTag,
462 (generic_free)free_TESTLargeTag,
463 cmp_TESTLargeTag);
466 struct test_data {
467 int ok;
468 size_t len;
469 size_t expected_len;
470 void *data;
473 static int
474 check_tag_length(void)
476 struct test_data td[] = {
477 { 1, 3, 3, "\x02\x01\x00"},
478 { 1, 3, 3, "\x02\x01\x7f"},
479 { 1, 4, 4, "\x02\x02\x00\x80"},
480 { 1, 4, 4, "\x02\x02\x01\x00"},
481 { 1, 4, 4, "\x02\x02\x02\x00"},
482 { 0, 3, 0, "\x02\x02\x00"},
483 { 0, 3, 0, "\x02\x7f\x7f"},
484 { 0, 4, 0, "\x02\x03\x00\x80"},
485 { 0, 4, 0, "\x02\x7f\x01\x00"},
486 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
488 size_t sz;
489 krb5uint32 values[] = {0, 127, 128, 256, 512,
490 0, 127, 128, 256, 512 };
491 krb5uint32 u;
492 int i, ret, failed = 0;
493 void *buf;
495 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
496 struct map_page *page;
498 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
500 ret = decode_krb5uint32(buf, td[i].len, &u, &sz);
501 if (ret) {
502 if (td[i].ok) {
503 printf("failed with tag len test %d\n", i);
504 failed = 1;
506 } else {
507 if (td[i].ok == 0) {
508 printf("failed with success for tag len test %d\n", i);
509 failed = 1;
511 if (td[i].expected_len != sz) {
512 printf("wrong expected size for tag test %d\n", i);
513 failed = 1;
515 if (values[i] != u) {
516 printf("wrong value for tag test %d\n", i);
517 failed = 1;
520 map_free(page, "test", "decode");
522 return failed;
525 static int
526 cmp_TESTChoice (void *a, void *b)
528 return 0;
531 static int
532 test_choice (void)
534 struct test_case tests[] = {
535 { NULL, 5, "\xa1\x03\x02\x01\x01", "large choice 1" },
536 { NULL, 5, "\xa2\x03\x02\x01\x02", "large choice 2" }
539 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
540 TESTChoice1 c1;
541 TESTChoice1 c2_1;
542 TESTChoice2 c2_2;
544 memset(&c1, 0, sizeof(c1));
545 c1.element = choice_TESTChoice1_i1;
546 c1.u.i1 = 1;
547 tests[0].val = &c1;
549 memset(&c2_1, 0, sizeof(c2_1));
550 c2_1.element = choice_TESTChoice1_i2;
551 c2_1.u.i2 = 2;
552 tests[1].val = &c2_1;
554 ret += generic_test (tests, ntests, sizeof(TESTChoice1),
555 (generic_encode)encode_TESTChoice1,
556 (generic_length)length_TESTChoice1,
557 (generic_decode)decode_TESTChoice1,
558 (generic_free)free_TESTChoice1,
559 cmp_TESTChoice);
561 memset(&c2_2, 0, sizeof(c2_2));
562 c2_2.element = choice_TESTChoice2_asn1_ellipsis;
563 c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
564 c2_2.u.asn1_ellipsis.length = 5;
565 tests[1].val = &c2_2;
567 ret += generic_test (tests, ntests, sizeof(TESTChoice2),
568 (generic_encode)encode_TESTChoice2,
569 (generic_length)length_TESTChoice2,
570 (generic_decode)decode_TESTChoice2,
571 (generic_free)free_TESTChoice2,
572 cmp_TESTChoice);
574 return ret;
577 static int
578 cmp_TESTImplicit (void *a, void *b)
580 TESTImplicit *aa = a;
581 TESTImplicit *ab = b;
583 COMPARE_INTEGER(aa,ab,ti1);
584 COMPARE_INTEGER(aa,ab,ti2.foo);
585 COMPARE_INTEGER(aa,ab,ti3);
586 return 0;
590 UNIV CONS Sequence 14
591 CONTEXT PRIM 0 1 00
592 CONTEXT CONS 1 6
593 CONTEXT CONS 127 3
594 UNIV PRIM Integer 1 02
595 CONTEXT PRIM 2 1 03
598 static int
599 test_implicit (void)
601 struct test_case tests[] = {
602 { NULL, 16,
603 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
604 "\x7f\x03\x02\x01\x02\x82\x01\x03",
605 "implicit 1" }
608 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
609 TESTImplicit c0;
611 memset(&c0, 0, sizeof(c0));
612 c0.ti1 = 0;
613 c0.ti2.foo = 2;
614 c0.ti3 = 3;
615 tests[0].val = &c0;
617 ret += generic_test (tests, ntests, sizeof(TESTImplicit),
618 (generic_encode)encode_TESTImplicit,
619 (generic_length)length_TESTImplicit,
620 (generic_decode)decode_TESTImplicit,
621 (generic_free)free_TESTImplicit,
622 cmp_TESTImplicit);
624 #ifdef IMPLICIT_TAGGING_WORKS
625 ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
626 (generic_encode)encode_TESTImplicit2,
627 (generic_length)length_TESTImplicit2,
628 (generic_decode)decode_TESTImplicit2,
629 (generic_free)free_TESTImplicit2,
630 cmp_TESTImplicit);
632 #endif /* IMPLICIT_TAGGING_WORKS */
633 return ret;
636 static int
637 cmp_TESTAlloc (void *a, void *b)
639 TESTAlloc *aa = a;
640 TESTAlloc *ab = b;
642 IF_OPT_COMPARE(aa,ab,tagless) {
643 COMPARE_INTEGER(aa,ab,tagless->ai);
646 COMPARE_INTEGER(aa,ab,three);
648 IF_OPT_COMPARE(aa,ab,tagless2) {
649 COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
652 return 0;
656 UNIV CONS Sequence 12
657 UNIV CONS Sequence 5
658 CONTEXT CONS 0 3
659 UNIV PRIM Integer 1 01
660 CONTEXT CONS 1 3
661 UNIV PRIM Integer 1 03
663 UNIV CONS Sequence 5
664 CONTEXT CONS 1 3
665 UNIV PRIM Integer 1 03
667 UNIV CONS Sequence 8
668 CONTEXT CONS 1 3
669 UNIV PRIM Integer 1 04
670 UNIV PRIM Integer 1 05
674 static int
675 test_taglessalloc (void)
677 struct test_case tests[] = {
678 { NULL, 14,
679 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
680 "alloc 1" },
681 { NULL, 7,
682 "\x30\x05\xa1\x03\x02\x01\x03",
683 "alloc 2" },
684 { NULL, 10,
685 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
686 "alloc 3" }
689 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
690 TESTAlloc c1, c2, c3;
691 heim_any any3;
693 memset(&c1, 0, sizeof(c1));
694 c1.tagless = ecalloc(1, sizeof(*c1.tagless));
695 c1.tagless->ai = 1;
696 c1.three = 3;
697 tests[0].val = &c1;
699 memset(&c2, 0, sizeof(c2));
700 c2.tagless = NULL;
701 c2.three = 3;
702 tests[1].val = &c2;
704 memset(&c3, 0, sizeof(c3));
705 c3.tagless = NULL;
706 c3.three = 4;
707 c3.tagless2 = &any3;
708 any3.data = "\x02\x01\x05";
709 any3.length = 3;
710 tests[2].val = &c3;
712 ret += generic_test (tests, ntests, sizeof(TESTAlloc),
713 (generic_encode)encode_TESTAlloc,
714 (generic_length)length_TESTAlloc,
715 (generic_decode)decode_TESTAlloc,
716 (generic_free)free_TESTAlloc,
717 cmp_TESTAlloc);
719 free(c1.tagless);
721 return ret;
725 static int
726 check_fail_largetag(void)
728 struct test_case tests[] = {
729 {NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
730 "tag overflow"},
731 {NULL, 0, "", "empty buffer"},
732 {NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
733 "one too short" },
734 {NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
735 "two too short" },
736 {NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
737 "three too short" },
738 {NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
739 "four too short" },
740 {NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
741 "five too short" },
742 {NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
743 "six too short" },
744 {NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
745 "inner one too long" },
746 {NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
747 "inner one too short" },
748 {NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
749 "inner one too short"},
750 {NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
751 "wrong tag"},
752 {NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
753 "still wrong tag"}
755 int ntests = sizeof(tests) / sizeof(*tests);
757 return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
758 (generic_decode)decode_TESTLargeTag);
762 static int
763 check_fail_sequence(void)
765 struct test_case tests[] = {
766 {NULL, 0, "", "empty buffer"},
767 {NULL, 24,
768 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
769 "\x02\x01\x01\xa2\x03\x02\x01\x01"
770 "missing one byte from the end, internal length ok"},
771 {NULL, 25,
772 "\x30\x18\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 "inner length one byte too long"},
775 {NULL, 24,
776 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
777 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
778 "correct buffer but missing one too short"}
780 int ntests = sizeof(tests) / sizeof(*tests);
782 return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
783 (generic_decode)decode_TESTSeq);
786 static int
787 check_fail_choice(void)
789 struct test_case tests[] = {
790 {NULL, 6,
791 "\xa1\x02\x02\x01\x01",
792 "one too short"},
793 {NULL, 6,
794 "\xa1\x03\x02\x02\x01",
795 "one too short inner"}
797 int ntests = sizeof(tests) / sizeof(*tests);
799 return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
800 (generic_decode)decode_TESTChoice1);
803 static int
804 check_seq(void)
806 TESTSeqOf seq;
807 TESTInteger i;
808 int ret;
810 seq.val = NULL;
811 seq.len = 0;
813 ret = add_TESTSeqOf(&seq, &i);
814 if (ret) { printf("failed adding\n"); goto out; }
815 ret = add_TESTSeqOf(&seq, &i);
816 if (ret) { printf("failed adding\n"); goto out; }
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; }
822 ret = remove_TESTSeqOf(&seq, seq.len - 1);
823 if (ret) { printf("failed removing\n"); goto out; }
824 ret = remove_TESTSeqOf(&seq, 2);
825 if (ret) { printf("failed removing\n"); goto out; }
826 ret = remove_TESTSeqOf(&seq, 0);
827 if (ret) { printf("failed removing\n"); goto out; }
828 ret = remove_TESTSeqOf(&seq, 0);
829 if (ret) { printf("failed removing\n"); goto out; }
830 ret = remove_TESTSeqOf(&seq, 0);
831 if (ret == 0) {
832 printf("can remove from empty list");
833 return 1;
836 if (seq.len != 0) {
837 printf("seq not empty!");
838 return 1;
840 ret = 0;
842 out:
844 return ret;
849 main(int argc, char **argv)
851 int ret = 0;
853 ret += test_principal ();
854 ret += test_authenticator();
855 ret += test_krb_error();
856 ret += test_Name();
857 ret += test_bit_string();
859 ret += check_tag_length();
860 ret += test_large_tag();
861 ret += test_choice();
863 ret += test_implicit();
864 ret += test_taglessalloc();
866 ret += check_fail_largetag();
867 ret += check_fail_sequence();
868 ret += check_fail_choice();
870 ret += check_seq();
872 return ret;