2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
42 #include <asn1-common.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 $"
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)
75 cmp_principal (void *a
, void *b
)
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
]);
95 struct test_case tests
[] = {
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"
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"
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" }
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
,
133 for (i
= 0; i
< ntests
; ++i
)
134 free (tests
[i
].name
);
140 cmp_authenticator (void *a
, void *b
)
142 Authenticator
*aa
= a
;
143 Authenticator
*ab
= b
;
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
]);
159 test_authenticator (void)
161 struct test_case tests
[] = {
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"
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"
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
}
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
,
199 for (i
= 0; i
< ntests
; ++i
)
206 cmp_KRB_ERROR (void *a
, void *b
)
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
);
229 IF_OPT_COMPARE(aa
,ab
,cname
) {
230 COMPARE_OPT_STRING(aa
,ab
,cname
);
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); */
250 test_krb_error (void)
252 struct test_case tests
[] = {
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",
265 int ntests
= sizeof(tests
) / sizeof(*tests
);
267 PrincipalName lhaprincipalname
= { 1, { 1, lha_principal
} };
268 PrincipalName tgtprincipalname
= { 1, { 2, nada_tgt_principal
} };
269 char *realm
= "NADA.KTH.SE";
275 e1
.stime
= 1069632679;
279 e1
.cname
= &lhaprincipalname
;
280 e1
.realm
= "NADA.KTH.SE";
281 e1
.sname
= tgtprincipalname
;
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
,
296 cmp_Name (void *a
, void *b
)
301 COMPARE_INTEGER(aa
,ab
,element
);
309 struct test_case tests
[] = {
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"
314 "Name CN=Love+L=STOCKHOLM"
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"
320 "Name L=STOCKHOLM+CN=Love"
324 int ntests
= sizeof(tests
) / sizeof(*tests
);
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 };
334 n1
.element
= choice_Name_rdnSequence
;
335 n1
.u
.rdnSequence
.val
= rdn1
;
336 n1
.u
.rdnSequence
.len
= sizeof(rdn1
)/sizeof(rdn1
[0]);
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";
351 n2
.element
= choice_Name_rdnSequence
;
352 n2
.u
.rdnSequence
.val
= rdn2
;
353 n2
.u
.rdnSequence
.len
= sizeof(rdn2
)/sizeof(rdn2
[0]);
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";
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
,
380 cmp_KeyUsage (void *a
, void *b
)
385 return KeyUsage2int(*aa
) != KeyUsage2int(*ab
);
389 test_bit_string (void)
391 struct test_case tests
[] = {
401 "\x03\x03\x07\x00\x80",
410 int ntests
= sizeof(tests
) / sizeof(*tests
);
411 KeyUsage ku1
, ku2
, ku3
, ku4
;
413 memset(&ku1
, 0, sizeof(ku1
));
414 ku1
.digitalSignature
= 1;
417 memset(&ku2
, 0, sizeof(ku2
));
418 ku2
.digitalSignature
= 1;
419 ku2
.keyEncipherment
= 1;
422 memset(&ku3
, 0, sizeof(ku3
));
423 ku3
.decipherOnly
= 1;
426 memset(&ku4
, 0, sizeof(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
,
439 cmp_TESTLargeTag (void *a
, void *b
)
441 TESTLargeTag
*aa
= a
;
442 TESTLargeTag
*ab
= b
;
444 COMPARE_INTEGER(aa
,ab
,foo
);
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
);
458 memset(<1
, 0, sizeof(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
,
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"}
494 krb5uint32 values
[] = {0, 127, 128, 256, 512,
495 0, 127, 128, 256, 512 };
497 int i
, ret
, failed
= 0;
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
);
508 printf("failed with tag len test %d\n", i
);
513 printf("failed with success for tag len test %d\n", i
);
516 if (td
[i
].expected_len
!= sz
) {
517 printf("wrong expected size for tag test %d\n", i
);
520 if (values
[i
] != u
) {
521 printf("wrong value for tag test %d\n", i
);
525 map_free(page
, "test", "decode");
531 cmp_TESTChoice (void *a
, void *b
)
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
);
549 memset(&c1
, 0, sizeof(c1
));
550 c1
.element
= choice_TESTChoice1_i1
;
554 memset(&c2_1
, 0, sizeof(c2_1
));
555 c2_1
.element
= choice_TESTChoice1_i2
;
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
,
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
,
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
);
595 UNIV CONS Sequence 14
599 UNIV PRIM Integer 1 02
606 struct test_case tests
[] = {
608 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
609 "\x7f\x03\x02\x01\x02\x82\x01\x03",
613 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
616 memset(&c0
, 0, sizeof(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
,
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
,
637 #endif /* IMPLICIT_TAGGING_WORKS */
642 cmp_TESTAlloc (void *a
, void *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
);
661 UNIV CONS Sequence 12
664 UNIV PRIM Integer 1 01
666 UNIV PRIM Integer 1 03
670 UNIV PRIM Integer 1 03
674 UNIV PRIM Integer 1 04
675 UNIV PRIM Integer 1 05
680 test_taglessalloc (void)
682 struct test_case tests
[] = {
684 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
687 "\x30\x05\xa1\x03\x02\x01\x03",
690 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
694 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
695 TESTAlloc c1
, c2
, c3
;
698 memset(&c1
, 0, sizeof(c1
));
699 c1
.tagless
= ecalloc(1, sizeof(*c1
.tagless
));
704 memset(&c2
, 0, sizeof(c2
));
709 memset(&c3
, 0, sizeof(c3
));
713 any3
.data
= "\x02\x01\x05";
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
,
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",
736 {NULL
, 0, "", "empty buffer"},
737 {NULL
, 7, "\x30\x05\xa1\x03\x02\x02\x01",
739 {NULL
, 7, "\x30\x04\xa1\x03\x02\x02\x01"
741 {NULL
, 7, "\x30\x03\xa1\x03\x02\x02\x01",
743 {NULL
, 7, "\x30\x02\xa1\x03\x02\x02\x01",
745 {NULL
, 7, "\x30\x01\xa1\x03\x02\x02\x01",
747 {NULL
, 7, "\x30\x00\xa1\x03\x02\x02\x01",
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",
757 {NULL
, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
760 int ntests
= sizeof(tests
) / sizeof(*tests
);
762 return generic_decode_fail(tests
, ntests
, sizeof(TESTLargeTag
),
763 (generic_decode
)decode_TESTLargeTag
);
768 check_fail_sequence(void)
770 struct test_case tests
[] = {
771 {NULL
, 0, "", "empty buffer"},
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"},
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"},
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
);
792 check_fail_choice(void)
794 struct test_case tests
[] = {
796 "\xa1\x02\x02\x01\x01",
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
);
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);
837 printf("can remove from empty list");
842 printf("seq not empty!");
845 free_TESTSeqOf(&seq
);
853 #define test_seq_of(type, ok, ptr) \
855 heim_octet_string os; \
858 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
861 if (os.length != size) \
863 ret = decode_##type(os.data, os.length, &decode, &size); \
869 free_##type(&decode); \
879 check_seq_of_size(void)
881 TESTInteger integers
[4] = { 1, 2, 3, 4 };
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
);
931 main(int argc
, char **argv
)
935 ret
+= test_principal ();
936 ret
+= test_authenticator();
937 ret
+= test_krb_error();
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();
953 ret
+= check_seq_of_size();