No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / check-gen.c
blob92cc5f7ef02aacb6c63d31fe52058dcc76b139bf
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("$Heimdal: check-gen.c 21539 2007-07-14 16:12:04Z lha $"
53 "$NetBSD$");
55 static char *lha_principal[] = { "lha" };
56 static char *lharoot_princ[] = { "lha", "root" };
57 static char *datan_princ[] = { "host", "nutcracker.e.kth.se" };
58 static char *nada_tgt_principal[] = { "krbtgt", "NADA.KTH.SE" };
61 #define IF_OPT_COMPARE(ac,bc,e) \
62 if (((ac)->e == NULL && (bc)->e != NULL) || (((ac)->e != NULL && (bc)->e == NULL))) return 1; if ((ab)->e)
63 #define COMPARE_OPT_STRING(ac,bc,e) \
64 do { if (strcmp(*(ac)->e, *(bc)->e) != 0) return 1; } while(0)
65 #define COMPARE_OPT_OCTECT_STRING(ac,bc,e) \
66 do { if ((ac)->e->length != (bc)->e->length || memcmp((ac)->e->data, (bc)->e->data, (ac)->e->length) != 0) return 1; } while(0)
67 #define COMPARE_STRING(ac,bc,e) \
68 do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
69 #define COMPARE_INTEGER(ac,bc,e) \
70 do { if ((ac)->e != (bc)->e) return 1; } while(0)
71 #define COMPARE_MEM(ac,bc,e,len) \
72 do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
74 static int
75 cmp_principal (void *a, void *b)
77 Principal *pa = a;
78 Principal *pb = b;
79 int i;
81 COMPARE_STRING(pa,pb,realm);
82 COMPARE_INTEGER(pa,pb,name.name_type);
83 COMPARE_INTEGER(pa,pb,name.name_string.len);
85 for (i = 0; i < pa->name.name_string.len; i++)
86 COMPARE_STRING(pa,pb,name.name_string.val[i]);
88 return 0;
91 static int
92 test_principal (void)
95 struct test_case tests[] = {
96 { NULL, 29,
97 "\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
98 "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45"
100 { NULL, 35,
101 "\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
102 "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
103 "\x2e\x53\x45"
105 { NULL, 54,
106 "\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
107 "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
108 "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
109 "\x4b\x54\x48\x2e\x53\x45"
114 Principal values[] = {
115 { { KRB5_NT_PRINCIPAL, { 1, lha_principal } }, "SU.SE" },
116 { { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } }, "SU.SE" },
117 { { KRB5_NT_SRV_HST, { 2, datan_princ } }, "E.KTH.SE" }
119 int i, ret;
120 int ntests = sizeof(tests) / sizeof(*tests);
122 for (i = 0; i < ntests; ++i) {
123 tests[i].val = &values[i];
124 asprintf (&tests[i].name, "Principal %d", i);
127 ret = generic_test (tests, ntests, sizeof(Principal),
128 (generic_encode)encode_Principal,
129 (generic_length)length_Principal,
130 (generic_decode)decode_Principal,
131 (generic_free)free_Principal,
132 cmp_principal);
133 for (i = 0; i < ntests; ++i)
134 free (tests[i].name);
136 return ret;
139 static int
140 cmp_authenticator (void *a, void *b)
142 Authenticator *aa = a;
143 Authenticator *ab = b;
144 int i;
146 COMPARE_INTEGER(aa,ab,authenticator_vno);
147 COMPARE_STRING(aa,ab,crealm);
149 COMPARE_INTEGER(aa,ab,cname.name_type);
150 COMPARE_INTEGER(aa,ab,cname.name_string.len);
152 for (i = 0; i < aa->cname.name_string.len; i++)
153 COMPARE_STRING(aa,ab,cname.name_string.val[i]);
155 return 0;
158 static int
159 test_authenticator (void)
161 struct test_case tests[] = {
162 { NULL, 63,
163 "\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
164 "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
165 "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
166 "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
167 "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a"
169 { NULL, 67,
170 "\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
171 "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
172 "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
173 "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
174 "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
175 "\x39\x5a"
179 Authenticator values[] = {
180 { 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL, { 1, lha_principal } },
181 NULL, 10, 99, NULL, NULL, NULL },
182 { 5, "SU.SE", { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },
183 NULL, 292, 999, NULL, NULL, NULL }
185 int i, ret;
186 int ntests = sizeof(tests) / sizeof(*tests);
188 for (i = 0; i < ntests; ++i) {
189 tests[i].val = &values[i];
190 asprintf (&tests[i].name, "Authenticator %d", i);
193 ret = generic_test (tests, ntests, sizeof(Authenticator),
194 (generic_encode)encode_Authenticator,
195 (generic_length)length_Authenticator,
196 (generic_decode)decode_Authenticator,
197 (generic_free)free_Authenticator,
198 cmp_authenticator);
199 for (i = 0; i < ntests; ++i)
200 free(tests[i].name);
202 return ret;
205 static int
206 cmp_KRB_ERROR (void *a, void *b)
208 KRB_ERROR *aa = a;
209 KRB_ERROR *ab = b;
210 int i;
212 COMPARE_INTEGER(aa,ab,pvno);
213 COMPARE_INTEGER(aa,ab,msg_type);
215 IF_OPT_COMPARE(aa,ab,ctime) {
216 COMPARE_INTEGER(aa,ab,ctime);
218 IF_OPT_COMPARE(aa,ab,cusec) {
219 COMPARE_INTEGER(aa,ab,cusec);
221 COMPARE_INTEGER(aa,ab,stime);
222 COMPARE_INTEGER(aa,ab,susec);
223 COMPARE_INTEGER(aa,ab,error_code);
225 IF_OPT_COMPARE(aa,ab,crealm) {
226 COMPARE_OPT_STRING(aa,ab,crealm);
228 #if 0
229 IF_OPT_COMPARE(aa,ab,cname) {
230 COMPARE_OPT_STRING(aa,ab,cname);
232 #endif
233 COMPARE_STRING(aa,ab,realm);
235 COMPARE_INTEGER(aa,ab,sname.name_string.len);
236 for (i = 0; i < aa->sname.name_string.len; i++)
237 COMPARE_STRING(aa,ab,sname.name_string.val[i]);
239 IF_OPT_COMPARE(aa,ab,e_text) {
240 COMPARE_OPT_STRING(aa,ab,e_text);
242 IF_OPT_COMPARE(aa,ab,e_data) {
243 /* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
246 return 0;
249 static int
250 test_krb_error (void)
252 struct test_case tests[] = {
253 { NULL, 127,
254 "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
255 "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
256 "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
257 "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
258 "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
259 "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
260 "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
261 "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
262 "KRB-ERROR Test 1"
265 int ntests = sizeof(tests) / sizeof(*tests);
266 KRB_ERROR e1;
267 PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
268 PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
269 char *realm = "NADA.KTH.SE";
271 e1.pvno = 5;
272 e1.msg_type = 30;
273 e1.ctime = NULL;
274 e1.cusec = NULL;
275 e1.stime = 1069632679;
276 e1.susec = 322981;
277 e1.error_code = 31;
278 e1.crealm = &realm;
279 e1.cname = &lhaprincipalname;
280 e1.realm = "NADA.KTH.SE";
281 e1.sname = tgtprincipalname;
282 e1.e_text = NULL;
283 e1.e_data = NULL;
285 tests[0].val = &e1;
287 return generic_test (tests, ntests, sizeof(KRB_ERROR),
288 (generic_encode)encode_KRB_ERROR,
289 (generic_length)length_KRB_ERROR,
290 (generic_decode)decode_KRB_ERROR,
291 (generic_free)free_KRB_ERROR,
292 cmp_KRB_ERROR);
295 static int
296 cmp_Name (void *a, void *b)
298 Name *aa = a;
299 Name *ab = b;
301 COMPARE_INTEGER(aa,ab,element);
303 return 0;
306 static int
307 test_Name (void)
309 struct test_case tests[] = {
310 { NULL, 35,
311 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
312 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
313 "\x4f\x4c\x4d",
314 "Name CN=Love+L=STOCKHOLM"
316 { NULL, 35,
317 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
318 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
319 "\x4f\x4c\x4d",
320 "Name L=STOCKHOLM+CN=Love"
324 int ntests = sizeof(tests) / sizeof(*tests);
325 Name n1, n2;
326 RelativeDistinguishedName rdn1[1];
327 RelativeDistinguishedName rdn2[1];
328 AttributeTypeAndValue atv1[2];
329 AttributeTypeAndValue atv2[2];
330 unsigned cmp_CN[] = { 2, 5, 4, 3 };
331 unsigned cmp_L[] = { 2, 5, 4, 7 };
333 /* n1 */
334 n1.element = choice_Name_rdnSequence;
335 n1.u.rdnSequence.val = rdn1;
336 n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
337 rdn1[0].val = atv1;
338 rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
340 atv1[0].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
341 atv1[0].type.components = cmp_CN;
342 atv1[0].value.element = choice_DirectoryString_printableString;
343 atv1[0].value.u.printableString = "Love";
345 atv1[1].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
346 atv1[1].type.components = cmp_L;
347 atv1[1].value.element = choice_DirectoryString_printableString;
348 atv1[1].value.u.printableString = "STOCKHOLM";
350 /* n2 */
351 n2.element = choice_Name_rdnSequence;
352 n2.u.rdnSequence.val = rdn2;
353 n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
354 rdn2[0].val = atv2;
355 rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
357 atv2[0].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
358 atv2[0].type.components = cmp_L;
359 atv2[0].value.element = choice_DirectoryString_printableString;
360 atv2[0].value.u.printableString = "STOCKHOLM";
362 atv2[1].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
363 atv2[1].type.components = cmp_CN;
364 atv2[1].value.element = choice_DirectoryString_printableString;
365 atv2[1].value.u.printableString = "Love";
367 /* */
368 tests[0].val = &n1;
369 tests[1].val = &n2;
371 return generic_test (tests, ntests, sizeof(Name),
372 (generic_encode)encode_Name,
373 (generic_length)length_Name,
374 (generic_decode)decode_Name,
375 (generic_free)free_Name,
376 cmp_Name);
379 static int
380 cmp_KeyUsage (void *a, void *b)
382 KeyUsage *aa = a;
383 KeyUsage *ab = b;
385 return KeyUsage2int(*aa) != KeyUsage2int(*ab);
388 static int
389 test_bit_string (void)
391 struct test_case tests[] = {
392 { NULL, 4,
393 "\x03\x02\x07\x80",
394 "bitstring 1"
396 { NULL, 4,
397 "\x03\x02\x05\xa0",
398 "bitstring 2"
400 { NULL, 5,
401 "\x03\x03\x07\x00\x80",
402 "bitstring 3"
404 { NULL, 3,
405 "\x03\x01\x00",
406 "bitstring 4"
410 int ntests = sizeof(tests) / sizeof(*tests);
411 KeyUsage ku1, ku2, ku3, ku4;
413 memset(&ku1, 0, sizeof(ku1));
414 ku1.digitalSignature = 1;
415 tests[0].val = &ku1;
417 memset(&ku2, 0, sizeof(ku2));
418 ku2.digitalSignature = 1;
419 ku2.keyEncipherment = 1;
420 tests[1].val = &ku2;
422 memset(&ku3, 0, sizeof(ku3));
423 ku3.decipherOnly = 1;
424 tests[2].val = &ku3;
426 memset(&ku4, 0, sizeof(ku4));
427 tests[3].val = &ku4;
430 return generic_test (tests, ntests, sizeof(KeyUsage),
431 (generic_encode)encode_KeyUsage,
432 (generic_length)length_KeyUsage,
433 (generic_decode)decode_KeyUsage,
434 (generic_free)free_KeyUsage,
435 cmp_KeyUsage);
438 static int
439 cmp_TESTLargeTag (void *a, void *b)
441 TESTLargeTag *aa = a;
442 TESTLargeTag *ab = b;
444 COMPARE_INTEGER(aa,ab,foo);
445 return 0;
448 static int
449 test_large_tag (void)
451 struct test_case tests[] = {
452 { NULL, 8, "\x30\x06\xbf\x7f\x03\x02\x01\x01", "large tag 1" }
455 int ntests = sizeof(tests) / sizeof(*tests);
456 TESTLargeTag lt1;
458 memset(&lt1, 0, sizeof(lt1));
459 lt1.foo = 1;
461 tests[0].val = &lt1;
463 return generic_test (tests, ntests, sizeof(TESTLargeTag),
464 (generic_encode)encode_TESTLargeTag,
465 (generic_length)length_TESTLargeTag,
466 (generic_decode)decode_TESTLargeTag,
467 (generic_free)free_TESTLargeTag,
468 cmp_TESTLargeTag);
471 struct test_data {
472 int ok;
473 size_t len;
474 size_t expected_len;
475 void *data;
478 static int
479 check_tag_length(void)
481 struct test_data td[] = {
482 { 1, 3, 3, "\x02\x01\x00"},
483 { 1, 3, 3, "\x02\x01\x7f"},
484 { 1, 4, 4, "\x02\x02\x00\x80"},
485 { 1, 4, 4, "\x02\x02\x01\x00"},
486 { 1, 4, 4, "\x02\x02\x02\x00"},
487 { 0, 3, 0, "\x02\x02\x00"},
488 { 0, 3, 0, "\x02\x7f\x7f"},
489 { 0, 4, 0, "\x02\x03\x00\x80"},
490 { 0, 4, 0, "\x02\x7f\x01\x00"},
491 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
493 size_t sz;
494 krb5uint32 values[] = {0, 127, 128, 256, 512,
495 0, 127, 128, 256, 512 };
496 krb5uint32 u;
497 int i, ret, failed = 0;
498 void *buf;
500 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
501 struct map_page *page;
503 buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
505 ret = decode_krb5uint32(buf, td[i].len, &u, &sz);
506 if (ret) {
507 if (td[i].ok) {
508 printf("failed with tag len test %d\n", i);
509 failed = 1;
511 } else {
512 if (td[i].ok == 0) {
513 printf("failed with success for tag len test %d\n", i);
514 failed = 1;
516 if (td[i].expected_len != sz) {
517 printf("wrong expected size for tag test %d\n", i);
518 failed = 1;
520 if (values[i] != u) {
521 printf("wrong value for tag test %d\n", i);
522 failed = 1;
525 map_free(page, "test", "decode");
527 return failed;
530 static int
531 cmp_TESTChoice (void *a, void *b)
533 return 0;
536 static int
537 test_choice (void)
539 struct test_case tests[] = {
540 { NULL, 5, "\xa1\x03\x02\x01\x01", "large choice 1" },
541 { NULL, 5, "\xa2\x03\x02\x01\x02", "large choice 2" }
544 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
545 TESTChoice1 c1;
546 TESTChoice1 c2_1;
547 TESTChoice2 c2_2;
549 memset(&c1, 0, sizeof(c1));
550 c1.element = choice_TESTChoice1_i1;
551 c1.u.i1 = 1;
552 tests[0].val = &c1;
554 memset(&c2_1, 0, sizeof(c2_1));
555 c2_1.element = choice_TESTChoice1_i2;
556 c2_1.u.i2 = 2;
557 tests[1].val = &c2_1;
559 ret += generic_test (tests, ntests, sizeof(TESTChoice1),
560 (generic_encode)encode_TESTChoice1,
561 (generic_length)length_TESTChoice1,
562 (generic_decode)decode_TESTChoice1,
563 (generic_free)free_TESTChoice1,
564 cmp_TESTChoice);
566 memset(&c2_2, 0, sizeof(c2_2));
567 c2_2.element = choice_TESTChoice2_asn1_ellipsis;
568 c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
569 c2_2.u.asn1_ellipsis.length = 5;
570 tests[1].val = &c2_2;
572 ret += generic_test (tests, ntests, sizeof(TESTChoice2),
573 (generic_encode)encode_TESTChoice2,
574 (generic_length)length_TESTChoice2,
575 (generic_decode)decode_TESTChoice2,
576 (generic_free)free_TESTChoice2,
577 cmp_TESTChoice);
579 return ret;
582 static int
583 cmp_TESTImplicit (void *a, void *b)
585 TESTImplicit *aa = a;
586 TESTImplicit *ab = b;
588 COMPARE_INTEGER(aa,ab,ti1);
589 COMPARE_INTEGER(aa,ab,ti2.foo);
590 COMPARE_INTEGER(aa,ab,ti3);
591 return 0;
595 UNIV CONS Sequence 14
596 CONTEXT PRIM 0 1 00
597 CONTEXT CONS 1 6
598 CONTEXT CONS 127 3
599 UNIV PRIM Integer 1 02
600 CONTEXT PRIM 2 1 03
603 static int
604 test_implicit (void)
606 struct test_case tests[] = {
607 { NULL, 16,
608 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
609 "\x7f\x03\x02\x01\x02\x82\x01\x03",
610 "implicit 1" }
613 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
614 TESTImplicit c0;
616 memset(&c0, 0, sizeof(c0));
617 c0.ti1 = 0;
618 c0.ti2.foo = 2;
619 c0.ti3 = 3;
620 tests[0].val = &c0;
622 ret += generic_test (tests, ntests, sizeof(TESTImplicit),
623 (generic_encode)encode_TESTImplicit,
624 (generic_length)length_TESTImplicit,
625 (generic_decode)decode_TESTImplicit,
626 (generic_free)free_TESTImplicit,
627 cmp_TESTImplicit);
629 #ifdef IMPLICIT_TAGGING_WORKS
630 ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
631 (generic_encode)encode_TESTImplicit2,
632 (generic_length)length_TESTImplicit2,
633 (generic_decode)decode_TESTImplicit2,
634 (generic_free)free_TESTImplicit2,
635 cmp_TESTImplicit);
637 #endif /* IMPLICIT_TAGGING_WORKS */
638 return ret;
641 static int
642 cmp_TESTAlloc (void *a, void *b)
644 TESTAlloc *aa = a;
645 TESTAlloc *ab = b;
647 IF_OPT_COMPARE(aa,ab,tagless) {
648 COMPARE_INTEGER(aa,ab,tagless->ai);
651 COMPARE_INTEGER(aa,ab,three);
653 IF_OPT_COMPARE(aa,ab,tagless2) {
654 COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
657 return 0;
661 UNIV CONS Sequence 12
662 UNIV CONS Sequence 5
663 CONTEXT CONS 0 3
664 UNIV PRIM Integer 1 01
665 CONTEXT CONS 1 3
666 UNIV PRIM Integer 1 03
668 UNIV CONS Sequence 5
669 CONTEXT CONS 1 3
670 UNIV PRIM Integer 1 03
672 UNIV CONS Sequence 8
673 CONTEXT CONS 1 3
674 UNIV PRIM Integer 1 04
675 UNIV PRIM Integer 1 05
679 static int
680 test_taglessalloc (void)
682 struct test_case tests[] = {
683 { NULL, 14,
684 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
685 "alloc 1" },
686 { NULL, 7,
687 "\x30\x05\xa1\x03\x02\x01\x03",
688 "alloc 2" },
689 { NULL, 10,
690 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
691 "alloc 3" }
694 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
695 TESTAlloc c1, c2, c3;
696 heim_any any3;
698 memset(&c1, 0, sizeof(c1));
699 c1.tagless = ecalloc(1, sizeof(*c1.tagless));
700 c1.tagless->ai = 1;
701 c1.three = 3;
702 tests[0].val = &c1;
704 memset(&c2, 0, sizeof(c2));
705 c2.tagless = NULL;
706 c2.three = 3;
707 tests[1].val = &c2;
709 memset(&c3, 0, sizeof(c3));
710 c3.tagless = NULL;
711 c3.three = 4;
712 c3.tagless2 = &any3;
713 any3.data = "\x02\x01\x05";
714 any3.length = 3;
715 tests[2].val = &c3;
717 ret += generic_test (tests, ntests, sizeof(TESTAlloc),
718 (generic_encode)encode_TESTAlloc,
719 (generic_length)length_TESTAlloc,
720 (generic_decode)decode_TESTAlloc,
721 (generic_free)free_TESTAlloc,
722 cmp_TESTAlloc);
724 free(c1.tagless);
726 return ret;
730 static int
731 check_fail_largetag(void)
733 struct test_case tests[] = {
734 {NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
735 "tag overflow"},
736 {NULL, 0, "", "empty buffer"},
737 {NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
738 "one too short" },
739 {NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
740 "two too short" },
741 {NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
742 "three too short" },
743 {NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
744 "four too short" },
745 {NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
746 "five too short" },
747 {NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
748 "six too short" },
749 {NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
750 "inner one too long" },
751 {NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
752 "inner one too short" },
753 {NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
754 "inner one too short"},
755 {NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
756 "wrong tag"},
757 {NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
758 "still wrong tag"}
760 int ntests = sizeof(tests) / sizeof(*tests);
762 return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
763 (generic_decode)decode_TESTLargeTag);
767 static int
768 check_fail_sequence(void)
770 struct test_case tests[] = {
771 {NULL, 0, "", "empty buffer"},
772 {NULL, 24,
773 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
774 "\x02\x01\x01\xa2\x03\x02\x01\x01"
775 "missing one byte from the end, internal length ok"},
776 {NULL, 25,
777 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
778 "\x02\x01\x01\xa2\x03\x02\x01\x01",
779 "inner length one byte too long"},
780 {NULL, 24,
781 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
782 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
783 "correct buffer but missing one too short"}
785 int ntests = sizeof(tests) / sizeof(*tests);
787 return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
788 (generic_decode)decode_TESTSeq);
791 static int
792 check_fail_choice(void)
794 struct test_case tests[] = {
795 {NULL, 6,
796 "\xa1\x02\x02\x01\x01",
797 "one too short"},
798 {NULL, 6,
799 "\xa1\x03\x02\x02\x01",
800 "one too short inner"}
802 int ntests = sizeof(tests) / sizeof(*tests);
804 return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
805 (generic_decode)decode_TESTChoice1);
808 static int
809 check_seq(void)
811 TESTSeqOf seq;
812 TESTInteger i;
813 int ret;
815 seq.val = NULL;
816 seq.len = 0;
818 ret = add_TESTSeqOf(&seq, &i);
819 if (ret) { printf("failed adding\n"); goto out; }
820 ret = add_TESTSeqOf(&seq, &i);
821 if (ret) { printf("failed adding\n"); goto out; }
822 ret = add_TESTSeqOf(&seq, &i);
823 if (ret) { printf("failed adding\n"); goto out; }
824 ret = add_TESTSeqOf(&seq, &i);
825 if (ret) { printf("failed adding\n"); goto out; }
827 ret = remove_TESTSeqOf(&seq, seq.len - 1);
828 if (ret) { printf("failed removing\n"); goto out; }
829 ret = remove_TESTSeqOf(&seq, 2);
830 if (ret) { printf("failed removing\n"); goto out; }
831 ret = remove_TESTSeqOf(&seq, 0);
832 if (ret) { printf("failed removing\n"); goto out; }
833 ret = remove_TESTSeqOf(&seq, 0);
834 if (ret) { printf("failed removing\n"); goto out; }
835 ret = remove_TESTSeqOf(&seq, 0);
836 if (ret == 0) {
837 printf("can remove from empty list");
838 return 1;
841 if (seq.len != 0) {
842 printf("seq not empty!");
843 return 1;
845 free_TESTSeqOf(&seq);
846 ret = 0;
848 out:
850 return ret;
853 #define test_seq_of(type, ok, ptr) \
855 heim_octet_string os; \
856 size_t size; \
857 type decode; \
858 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
859 if (ret) \
860 return ret; \
861 if (os.length != size) \
862 abort(); \
863 ret = decode_##type(os.data, os.length, &decode, &size); \
864 free(os.data); \
865 if (ret) { \
866 if (ok) \
867 return 1; \
868 } else { \
869 free_##type(&decode); \
870 if (!ok) \
871 return 1; \
872 if (size != 0) \
873 return 1; \
875 return 0; \
878 static int
879 check_seq_of_size(void)
881 TESTInteger integers[4] = { 1, 2, 3, 4 };
882 int ret;
885 TESTSeqSizeOf1 ssof1f1 = { 1, integers };
886 TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
887 TESTSeqSizeOf1 ssof1f2 = { 3, integers };
889 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
890 test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
891 test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
894 TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
895 TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
896 TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
897 TESTSeqSizeOf2 ssof2f2 = { 3, integers };
899 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f1);
900 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok1);
901 test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok2);
902 test_seq_of(TESTSeqSizeOf2, 0, &ssof2f2);
905 TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
906 TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
907 TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
909 test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
910 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
911 test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
914 TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
915 TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
916 TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
917 TESTSeqSizeOf4 ssof4f1 = { 3, integers };
919 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok1);
920 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok2);
921 test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok3);
922 test_seq_of(TESTSeqSizeOf4, 0, &ssof4f1);
925 return 0;
931 main(int argc, char **argv)
933 int ret = 0;
935 ret += test_principal ();
936 ret += test_authenticator();
937 ret += test_krb_error();
938 ret += test_Name();
939 ret += test_bit_string();
941 ret += check_tag_length();
942 ret += test_large_tag();
943 ret += test_choice();
945 ret += test_implicit();
946 ret += test_taglessalloc();
948 ret += check_fail_largetag();
949 ret += check_fail_sequence();
950 ret += check_fail_choice();
952 ret += check_seq();
953 ret += check_seq_of_size();
955 return ret;