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"
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)
74 cmp_principal (void *a
, void *b
)
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
]);
94 struct test_case tests
[] = {
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"
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"
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" }
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
,
132 for (i
= 0; i
< ntests
; ++i
)
133 free (tests
[i
].name
);
139 cmp_authenticator (void *a
, void *b
)
141 Authenticator
*aa
= a
;
142 Authenticator
*ab
= b
;
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
]);
158 test_authenticator (void)
160 struct test_case tests
[] = {
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"
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"
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
}
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
,
198 for (i
= 0; i
< ntests
; ++i
)
205 cmp_KRB_ERROR (void *a
, void *b
)
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
);
228 IF_OPT_COMPARE(aa
,ab
,cname
) {
229 COMPARE_OPT_STRING(aa
,ab
,cname
);
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); */
249 test_krb_error (void)
251 struct test_case tests
[] = {
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",
264 int ntests
= sizeof(tests
) / sizeof(*tests
);
266 PrincipalName lhaprincipalname
= { 1, { 1, lha_principal
} };
267 PrincipalName tgtprincipalname
= { 1, { 2, nada_tgt_principal
} };
268 char *realm
= "NADA.KTH.SE";
274 e1
.stime
= 1069632679;
278 e1
.cname
= &lhaprincipalname
;
279 e1
.realm
= "NADA.KTH.SE";
280 e1
.sname
= tgtprincipalname
;
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
,
295 cmp_Name (void *a
, void *b
)
300 COMPARE_INTEGER(aa
,ab
,element
);
308 struct test_case tests
[] = {
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"
313 "Name CN=Love+L=STOCKHOLM"
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"
319 "Name L=STOCKHOLM+CN=Love"
323 int ntests
= sizeof(tests
) / sizeof(*tests
);
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 };
333 n1
.element
= choice_Name_rdnSequence
;
334 n1
.u
.rdnSequence
.val
= rdn1
;
335 n1
.u
.rdnSequence
.len
= sizeof(rdn1
)/sizeof(rdn1
[0]);
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";
350 n2
.element
= choice_Name_rdnSequence
;
351 n2
.u
.rdnSequence
.val
= rdn2
;
352 n2
.u
.rdnSequence
.len
= sizeof(rdn2
)/sizeof(rdn2
[0]);
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";
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
,
379 cmp_KeyUsage (void *a
, void *b
)
384 return KeyUsage2int(*aa
) != KeyUsage2int(*ab
);
388 test_bit_string (void)
390 struct test_case tests
[] = {
400 "\x03\x03\x07\x00\x80",
409 int ntests
= sizeof(tests
) / sizeof(*tests
);
410 KeyUsage ku1
, ku2
, ku3
, ku4
;
412 memset(&ku1
, 0, sizeof(ku1
));
413 ku1
.digitalSignature
= 1;
416 memset(&ku2
, 0, sizeof(ku2
));
417 ku2
.digitalSignature
= 1;
418 ku2
.keyEncipherment
= 1;
421 memset(&ku3
, 0, sizeof(ku3
));
422 ku3
.decipherOnly
= 1;
425 memset(&ku4
, 0, sizeof(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
,
438 cmp_TESTLargeTag (void *a
, void *b
)
440 TESTLargeTag
*aa
= a
;
441 TESTLargeTag
*ab
= b
;
443 COMPARE_INTEGER(aa
,ab
,foo
);
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
);
457 memset(<1
, 0, sizeof(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
,
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"}
493 krb5uint32 values
[] = {0, 127, 128, 256, 512,
494 0, 127, 128, 256, 512 };
496 int i
, ret
, failed
= 0;
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
);
507 printf("failed with tag len test %d\n", i
);
512 printf("failed with success for tag len test %d\n", i
);
515 if (td
[i
].expected_len
!= sz
) {
516 printf("wrong expected size for tag test %d\n", i
);
519 if (values
[i
] != u
) {
520 printf("wrong value for tag test %d\n", i
);
524 map_free(page
, "test", "decode");
530 cmp_TESTChoice (void *a
, void *b
)
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
);
548 memset(&c1
, 0, sizeof(c1
));
549 c1
.element
= choice_TESTChoice1_i1
;
553 memset(&c2_1
, 0, sizeof(c2_1
));
554 c2_1
.element
= choice_TESTChoice1_i2
;
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
,
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
,
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
);
594 UNIV CONS Sequence 14
598 UNIV PRIM Integer 1 02
605 struct test_case tests
[] = {
607 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
608 "\x7f\x03\x02\x01\x02\x82\x01\x03",
612 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
615 memset(&c0
, 0, sizeof(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
,
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
,
636 #endif /* IMPLICIT_TAGGING_WORKS */
641 cmp_TESTAlloc (void *a
, void *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
);
660 UNIV CONS Sequence 12
663 UNIV PRIM Integer 1 01
665 UNIV PRIM Integer 1 03
669 UNIV PRIM Integer 1 03
673 UNIV PRIM Integer 1 04
674 UNIV PRIM Integer 1 05
679 test_taglessalloc (void)
681 struct test_case tests
[] = {
683 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
686 "\x30\x05\xa1\x03\x02\x01\x03",
689 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
693 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
694 TESTAlloc c1
, c2
, c3
;
697 memset(&c1
, 0, sizeof(c1
));
698 c1
.tagless
= ecalloc(1, sizeof(*c1
.tagless
));
703 memset(&c2
, 0, sizeof(c2
));
708 memset(&c3
, 0, sizeof(c3
));
712 any3
.data
= "\x02\x01\x05";
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
,
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",
735 {NULL
, 0, "", "empty buffer"},
736 {NULL
, 7, "\x30\x05\xa1\x03\x02\x02\x01",
738 {NULL
, 7, "\x30\x04\xa1\x03\x02\x02\x01"
740 {NULL
, 7, "\x30\x03\xa1\x03\x02\x02\x01",
742 {NULL
, 7, "\x30\x02\xa1\x03\x02\x02\x01",
744 {NULL
, 7, "\x30\x01\xa1\x03\x02\x02\x01",
746 {NULL
, 7, "\x30\x00\xa1\x03\x02\x02\x01",
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",
756 {NULL
, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
759 int ntests
= sizeof(tests
) / sizeof(*tests
);
761 return generic_decode_fail(tests
, ntests
, sizeof(TESTLargeTag
),
762 (generic_decode
)decode_TESTLargeTag
);
767 check_fail_sequence(void)
769 struct test_case tests
[] = {
770 {NULL
, 0, "", "empty buffer"},
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"},
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"},
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
);
791 check_fail_choice(void)
793 struct test_case tests
[] = {
795 "\xa1\x02\x02\x01\x01",
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
);
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);
836 printf("can remove from empty list");
841 printf("seq not empty!");
844 free_TESTSeqOf(&seq
);
852 #define test_seq_of(type, ok, ptr) \
854 heim_octet_string os; \
857 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
860 if (os.length != size) \
862 ret = decode_##type(os.data, os.length, &decode, &size); \
868 free_##type(&decode); \
878 check_seq_of_size(void)
880 TESTInteger integers
[4] = { 1, 2, 3, 4 };
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
);
930 main(int argc
, char **argv
)
934 ret
+= test_principal ();
935 ret
+= test_authenticator();
936 ret
+= test_krb_error();
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();
952 ret
+= check_seq_of_size();