1120945Snectar/*
2233294Sstas * Copyright (c) 1999 - 2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5120945Snectar *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7120945Snectar *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
11120945Snectar *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
14120945Snectar *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
18120945Snectar *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
34120945Snectar */
35120945Snectar
36120945Snectar#ifdef HAVE_CONFIG_H
37120945Snectar#include <config.h>
38120945Snectar#endif
39120945Snectar#include <stdio.h>
40120945Snectar#include <string.h>
41120945Snectar#include <err.h>
42120945Snectar#include <roken.h>
43120945Snectar
44120945Snectar#include <asn1-common.h>
45120945Snectar#include <asn1_err.h>
46120945Snectar#include <der.h>
47120945Snectar#include <krb5_asn1.h>
48178825Sdfr#include <heim_asn1.h>
49178825Sdfr#include <rfc2459_asn1.h>
50178825Sdfr#include <test_asn1.h>
51120945Snectar
52120945Snectar#include "check-common.h"
53120945Snectar
54233294SstasRCSID("$Id$");
55120945Snectar
56178825Sdfrstatic char *lha_principal[] = { "lha" };
57120945Snectarstatic char *lharoot_princ[] = { "lha", "root" };
58120945Snectarstatic char *datan_princ[] = { "host", "nutcracker.e.kth.se" };
59178825Sdfrstatic char *nada_tgt_principal[] = { "krbtgt", "NADA.KTH.SE" };
60120945Snectar
61120945Snectar
62178825Sdfr#define IF_OPT_COMPARE(ac,bc,e) \
63178825Sdfr	if (((ac)->e == NULL && (bc)->e != NULL) || (((ac)->e != NULL && (bc)->e == NULL))) return 1; if ((ab)->e)
64178825Sdfr#define COMPARE_OPT_STRING(ac,bc,e) \
65178825Sdfr	do { if (strcmp(*(ac)->e, *(bc)->e) != 0) return 1; } while(0)
66178825Sdfr#define COMPARE_OPT_OCTECT_STRING(ac,bc,e) \
67178825Sdfr	do { if ((ac)->e->length != (bc)->e->length || memcmp((ac)->e->data, (bc)->e->data, (ac)->e->length) != 0) return 1; } while(0)
68120945Snectar#define COMPARE_STRING(ac,bc,e) \
69120945Snectar	do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
70120945Snectar#define COMPARE_INTEGER(ac,bc,e) \
71120945Snectar	do { if ((ac)->e != (bc)->e) return 1; } while(0)
72233294Sstas#define COMPARE_OPT_INTEGER(ac,bc,e) \
73233294Sstas	do { if (*(ac)->e != *(bc)->e) return 1; } while(0)
74120945Snectar#define COMPARE_MEM(ac,bc,e,len) \
75120945Snectar	do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
76120945Snectar
77120945Snectarstatic int
78120945Snectarcmp_principal (void *a, void *b)
79120945Snectar{
80120945Snectar    Principal *pa = a;
81120945Snectar    Principal *pb = b;
82120945Snectar    int i;
83120945Snectar
84120945Snectar    COMPARE_STRING(pa,pb,realm);
85120945Snectar    COMPARE_INTEGER(pa,pb,name.name_type);
86120945Snectar    COMPARE_INTEGER(pa,pb,name.name_string.len);
87120945Snectar
88120945Snectar    for (i = 0; i < pa->name.name_string.len; i++)
89120945Snectar	COMPARE_STRING(pa,pb,name.name_string.val[i]);
90120945Snectar
91120945Snectar    return 0;
92120945Snectar}
93120945Snectar
94120945Snectarstatic int
95120945Snectartest_principal (void)
96120945Snectar{
97120945Snectar
98120945Snectar    struct test_case tests[] = {
99233294Sstas	{ NULL, 29,
100178825Sdfr	  "\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
101120945Snectar	  "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45"
102120945Snectar	},
103120945Snectar	{ NULL, 35,
104178825Sdfr	  "\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
105120945Snectar	  "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
106120945Snectar	  "\x2e\x53\x45"
107120945Snectar	},
108233294Sstas	{ NULL, 54,
109178825Sdfr	  "\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
110120945Snectar	  "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
111120945Snectar	  "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
112120945Snectar	  "\x4b\x54\x48\x2e\x53\x45"
113120945Snectar	}
114120945Snectar    };
115120945Snectar
116120945Snectar
117233294Sstas    Principal values[] = {
118178825Sdfr	{ { KRB5_NT_PRINCIPAL, { 1, lha_principal } },  "SU.SE" },
119120945Snectar	{ { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },  "SU.SE" },
120120945Snectar	{ { KRB5_NT_SRV_HST, { 2, datan_princ } },  "E.KTH.SE" }
121120945Snectar    };
122178825Sdfr    int i, ret;
123120945Snectar    int ntests = sizeof(tests) / sizeof(*tests);
124120945Snectar
125120945Snectar    for (i = 0; i < ntests; ++i) {
126120945Snectar	tests[i].val = &values[i];
127233294Sstas	if (asprintf (&tests[i].name, "Principal %d", i) < 0)
128233294Sstas	    errx(1, "malloc");
129233294Sstas	if (tests[i].name == NULL)
130233294Sstas	    errx(1, "malloc");
131120945Snectar    }
132120945Snectar
133178825Sdfr    ret = generic_test (tests, ntests, sizeof(Principal),
134178825Sdfr			(generic_encode)encode_Principal,
135178825Sdfr			(generic_length)length_Principal,
136178825Sdfr			(generic_decode)decode_Principal,
137178825Sdfr			(generic_free)free_Principal,
138233294Sstas			cmp_principal,
139233294Sstas			NULL);
140178825Sdfr    for (i = 0; i < ntests; ++i)
141178825Sdfr	free (tests[i].name);
142178825Sdfr
143178825Sdfr    return ret;
144120945Snectar}
145120945Snectar
146120945Snectarstatic int
147120945Snectarcmp_authenticator (void *a, void *b)
148120945Snectar{
149120945Snectar    Authenticator *aa = a;
150120945Snectar    Authenticator *ab = b;
151120945Snectar    int i;
152120945Snectar
153120945Snectar    COMPARE_INTEGER(aa,ab,authenticator_vno);
154120945Snectar    COMPARE_STRING(aa,ab,crealm);
155120945Snectar
156120945Snectar    COMPARE_INTEGER(aa,ab,cname.name_type);
157120945Snectar    COMPARE_INTEGER(aa,ab,cname.name_string.len);
158120945Snectar
159120945Snectar    for (i = 0; i < aa->cname.name_string.len; i++)
160120945Snectar	COMPARE_STRING(aa,ab,cname.name_string.val[i]);
161120945Snectar
162120945Snectar    return 0;
163120945Snectar}
164120945Snectar
165120945Snectarstatic int
166120945Snectartest_authenticator (void)
167120945Snectar{
168120945Snectar    struct test_case tests[] = {
169233294Sstas	{ NULL, 63,
170178825Sdfr	  "\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
171120945Snectar	  "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
172120945Snectar	  "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
173120945Snectar	  "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
174120945Snectar	  "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a"
175120945Snectar	},
176233294Sstas	{ NULL, 67,
177178825Sdfr	  "\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
178120945Snectar	  "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
179120945Snectar	  "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
180120945Snectar	  "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
181120945Snectar	  "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
182120945Snectar	  "\x39\x5a"
183120945Snectar	}
184120945Snectar    };
185120945Snectar
186120945Snectar    Authenticator values[] = {
187178825Sdfr	{ 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL, { 1, lha_principal } },
188120945Snectar	  NULL, 10, 99, NULL, NULL, NULL },
189120945Snectar	{ 5, "SU.SE", { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },
190120945Snectar	  NULL, 292, 999, NULL, NULL, NULL }
191120945Snectar    };
192178825Sdfr    int i, ret;
193120945Snectar    int ntests = sizeof(tests) / sizeof(*tests);
194120945Snectar
195120945Snectar    for (i = 0; i < ntests; ++i) {
196120945Snectar	tests[i].val = &values[i];
197233294Sstas	if (asprintf (&tests[i].name, "Authenticator %d", i) < 0)
198233294Sstas	    errx(1, "malloc");
199233294Sstas	if (tests[i].name == NULL)
200233294Sstas	    errx(1, "malloc");
201120945Snectar    }
202120945Snectar
203178825Sdfr    ret = generic_test (tests, ntests, sizeof(Authenticator),
204178825Sdfr			(generic_encode)encode_Authenticator,
205178825Sdfr			(generic_length)length_Authenticator,
206178825Sdfr			(generic_decode)decode_Authenticator,
207178825Sdfr			(generic_free)free_Authenticator,
208233294Sstas			cmp_authenticator,
209233294Sstas			(generic_copy)copy_Authenticator);
210178825Sdfr    for (i = 0; i < ntests; ++i)
211178825Sdfr	free(tests[i].name);
212178825Sdfr
213178825Sdfr    return ret;
214120945Snectar}
215120945Snectar
216178825Sdfrstatic int
217178825Sdfrcmp_KRB_ERROR (void *a, void *b)
218178825Sdfr{
219178825Sdfr    KRB_ERROR *aa = a;
220178825Sdfr    KRB_ERROR *ab = b;
221178825Sdfr    int i;
222178825Sdfr
223178825Sdfr    COMPARE_INTEGER(aa,ab,pvno);
224178825Sdfr    COMPARE_INTEGER(aa,ab,msg_type);
225178825Sdfr
226178825Sdfr    IF_OPT_COMPARE(aa,ab,ctime) {
227178825Sdfr	COMPARE_INTEGER(aa,ab,ctime);
228178825Sdfr    }
229178825Sdfr    IF_OPT_COMPARE(aa,ab,cusec) {
230178825Sdfr	COMPARE_INTEGER(aa,ab,cusec);
231178825Sdfr    }
232178825Sdfr    COMPARE_INTEGER(aa,ab,stime);
233178825Sdfr    COMPARE_INTEGER(aa,ab,susec);
234178825Sdfr    COMPARE_INTEGER(aa,ab,error_code);
235178825Sdfr
236178825Sdfr    IF_OPT_COMPARE(aa,ab,crealm) {
237178825Sdfr	COMPARE_OPT_STRING(aa,ab,crealm);
238178825Sdfr    }
239178825Sdfr#if 0
240178825Sdfr    IF_OPT_COMPARE(aa,ab,cname) {
241178825Sdfr	COMPARE_OPT_STRING(aa,ab,cname);
242178825Sdfr    }
243178825Sdfr#endif
244178825Sdfr    COMPARE_STRING(aa,ab,realm);
245178825Sdfr
246178825Sdfr    COMPARE_INTEGER(aa,ab,sname.name_string.len);
247178825Sdfr    for (i = 0; i < aa->sname.name_string.len; i++)
248178825Sdfr	COMPARE_STRING(aa,ab,sname.name_string.val[i]);
249178825Sdfr
250178825Sdfr    IF_OPT_COMPARE(aa,ab,e_text) {
251178825Sdfr	COMPARE_OPT_STRING(aa,ab,e_text);
252178825Sdfr    }
253178825Sdfr    IF_OPT_COMPARE(aa,ab,e_data) {
254178825Sdfr	/* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
255178825Sdfr    }
256178825Sdfr
257178825Sdfr    return 0;
258178825Sdfr}
259178825Sdfr
260178825Sdfrstatic int
261178825Sdfrtest_krb_error (void)
262178825Sdfr{
263178825Sdfr    struct test_case tests[] = {
264233294Sstas	{ NULL, 127,
265178825Sdfr	  "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
266178825Sdfr	  "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
267178825Sdfr	  "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
268178825Sdfr	  "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
269178825Sdfr	  "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
270178825Sdfr	  "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
271178825Sdfr	  "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
272178825Sdfr	  "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
273178825Sdfr	  "KRB-ERROR Test 1"
274178825Sdfr	}
275178825Sdfr    };
276178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
277178825Sdfr    KRB_ERROR e1;
278178825Sdfr    PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
279178825Sdfr    PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
280178825Sdfr    char *realm = "NADA.KTH.SE";
281178825Sdfr
282178825Sdfr    e1.pvno = 5;
283178825Sdfr    e1.msg_type = 30;
284178825Sdfr    e1.ctime = NULL;
285178825Sdfr    e1.cusec = NULL;
286178825Sdfr    e1.stime = 1069632679;
287178825Sdfr    e1.susec = 322981;
288178825Sdfr    e1.error_code = 31;
289178825Sdfr    e1.crealm = &realm;
290178825Sdfr    e1.cname = &lhaprincipalname;
291178825Sdfr    e1.realm = "NADA.KTH.SE";
292178825Sdfr    e1.sname = tgtprincipalname;
293178825Sdfr    e1.e_text = NULL;
294178825Sdfr    e1.e_data = NULL;
295178825Sdfr
296178825Sdfr    tests[0].val = &e1;
297178825Sdfr
298178825Sdfr    return generic_test (tests, ntests, sizeof(KRB_ERROR),
299178825Sdfr			 (generic_encode)encode_KRB_ERROR,
300178825Sdfr			 (generic_length)length_KRB_ERROR,
301178825Sdfr			 (generic_decode)decode_KRB_ERROR,
302178825Sdfr			 (generic_free)free_KRB_ERROR,
303233294Sstas			 cmp_KRB_ERROR,
304233294Sstas			 (generic_copy)copy_KRB_ERROR);
305178825Sdfr}
306178825Sdfr
307178825Sdfrstatic int
308178825Sdfrcmp_Name (void *a, void *b)
309178825Sdfr{
310178825Sdfr    Name *aa = a;
311178825Sdfr    Name *ab = b;
312178825Sdfr
313178825Sdfr    COMPARE_INTEGER(aa,ab,element);
314178825Sdfr
315178825Sdfr    return 0;
316178825Sdfr}
317178825Sdfr
318178825Sdfrstatic int
319178825Sdfrtest_Name (void)
320178825Sdfr{
321178825Sdfr    struct test_case tests[] = {
322233294Sstas	{ NULL, 35,
323178825Sdfr	  "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
324178825Sdfr	  "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
325178825Sdfr	  "\x4f\x4c\x4d",
326178825Sdfr	  "Name CN=Love+L=STOCKHOLM"
327178825Sdfr	},
328233294Sstas	{ NULL, 35,
329178825Sdfr	  "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
330178825Sdfr	  "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
331178825Sdfr	  "\x4f\x4c\x4d",
332178825Sdfr	  "Name L=STOCKHOLM+CN=Love"
333178825Sdfr	}
334178825Sdfr    };
335178825Sdfr
336178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
337178825Sdfr    Name n1, n2;
338178825Sdfr    RelativeDistinguishedName rdn1[1];
339178825Sdfr    RelativeDistinguishedName rdn2[1];
340178825Sdfr    AttributeTypeAndValue atv1[2];
341178825Sdfr    AttributeTypeAndValue atv2[2];
342178825Sdfr    unsigned cmp_CN[] = { 2, 5, 4, 3 };
343178825Sdfr    unsigned cmp_L[] = { 2, 5, 4, 7 };
344178825Sdfr
345178825Sdfr    /* n1 */
346178825Sdfr    n1.element = choice_Name_rdnSequence;
347178825Sdfr    n1.u.rdnSequence.val = rdn1;
348178825Sdfr    n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
349178825Sdfr    rdn1[0].val = atv1;
350178825Sdfr    rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
351178825Sdfr
352178825Sdfr    atv1[0].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
353178825Sdfr    atv1[0].type.components = cmp_CN;
354178825Sdfr    atv1[0].value.element = choice_DirectoryString_printableString;
355233294Sstas    atv1[0].value.u.printableString.data = "Love";
356233294Sstas    atv1[0].value.u.printableString.length = 4;
357178825Sdfr
358178825Sdfr    atv1[1].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
359178825Sdfr    atv1[1].type.components = cmp_L;
360178825Sdfr    atv1[1].value.element = choice_DirectoryString_printableString;
361233294Sstas    atv1[1].value.u.printableString.data = "STOCKHOLM";
362233294Sstas    atv1[1].value.u.printableString.length = 9;
363178825Sdfr
364178825Sdfr    /* n2 */
365178825Sdfr    n2.element = choice_Name_rdnSequence;
366178825Sdfr    n2.u.rdnSequence.val = rdn2;
367178825Sdfr    n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
368178825Sdfr    rdn2[0].val = atv2;
369178825Sdfr    rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
370178825Sdfr
371178825Sdfr    atv2[0].type.length = sizeof(cmp_L)/sizeof(cmp_L[0]);
372178825Sdfr    atv2[0].type.components = cmp_L;
373178825Sdfr    atv2[0].value.element = choice_DirectoryString_printableString;
374233294Sstas    atv2[0].value.u.printableString.data = "STOCKHOLM";
375233294Sstas    atv2[0].value.u.printableString.length = 9;
376178825Sdfr
377178825Sdfr    atv2[1].type.length = sizeof(cmp_CN)/sizeof(cmp_CN[0]);
378178825Sdfr    atv2[1].type.components = cmp_CN;
379178825Sdfr    atv2[1].value.element = choice_DirectoryString_printableString;
380233294Sstas    atv2[1].value.u.printableString.data = "Love";
381233294Sstas    atv2[1].value.u.printableString.length = 4;
382178825Sdfr
383178825Sdfr    /* */
384178825Sdfr    tests[0].val = &n1;
385178825Sdfr    tests[1].val = &n2;
386178825Sdfr
387178825Sdfr    return generic_test (tests, ntests, sizeof(Name),
388178825Sdfr			 (generic_encode)encode_Name,
389178825Sdfr			 (generic_length)length_Name,
390178825Sdfr			 (generic_decode)decode_Name,
391178825Sdfr			 (generic_free)free_Name,
392233294Sstas			 cmp_Name,
393233294Sstas			 (generic_copy)copy_Name);
394178825Sdfr}
395178825Sdfr
396178825Sdfrstatic int
397178825Sdfrcmp_KeyUsage (void *a, void *b)
398178825Sdfr{
399178825Sdfr    KeyUsage *aa = a;
400178825Sdfr    KeyUsage *ab = b;
401178825Sdfr
402178825Sdfr    return KeyUsage2int(*aa) != KeyUsage2int(*ab);
403178825Sdfr}
404178825Sdfr
405178825Sdfrstatic int
406178825Sdfrtest_bit_string (void)
407178825Sdfr{
408178825Sdfr    struct test_case tests[] = {
409178825Sdfr	{ NULL, 4,
410178825Sdfr	  "\x03\x02\x07\x80",
411178825Sdfr	  "bitstring 1"
412178825Sdfr	},
413178825Sdfr	{ NULL, 4,
414178825Sdfr	  "\x03\x02\x05\xa0",
415178825Sdfr	  "bitstring 2"
416178825Sdfr	},
417178825Sdfr	{ NULL, 5,
418178825Sdfr	  "\x03\x03\x07\x00\x80",
419178825Sdfr	  "bitstring 3"
420178825Sdfr	},
421178825Sdfr	{ NULL, 3,
422178825Sdfr	  "\x03\x01\x00",
423178825Sdfr	  "bitstring 4"
424178825Sdfr	}
425178825Sdfr    };
426178825Sdfr
427178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
428178825Sdfr    KeyUsage ku1, ku2, ku3, ku4;
429178825Sdfr
430178825Sdfr    memset(&ku1, 0, sizeof(ku1));
431178825Sdfr    ku1.digitalSignature = 1;
432178825Sdfr    tests[0].val = &ku1;
433178825Sdfr
434178825Sdfr    memset(&ku2, 0, sizeof(ku2));
435178825Sdfr    ku2.digitalSignature = 1;
436178825Sdfr    ku2.keyEncipherment = 1;
437178825Sdfr    tests[1].val = &ku2;
438178825Sdfr
439178825Sdfr    memset(&ku3, 0, sizeof(ku3));
440178825Sdfr    ku3.decipherOnly = 1;
441178825Sdfr    tests[2].val = &ku3;
442178825Sdfr
443178825Sdfr    memset(&ku4, 0, sizeof(ku4));
444178825Sdfr    tests[3].val = &ku4;
445178825Sdfr
446178825Sdfr
447178825Sdfr    return generic_test (tests, ntests, sizeof(KeyUsage),
448178825Sdfr			 (generic_encode)encode_KeyUsage,
449178825Sdfr			 (generic_length)length_KeyUsage,
450178825Sdfr			 (generic_decode)decode_KeyUsage,
451178825Sdfr			 (generic_free)free_KeyUsage,
452233294Sstas			 cmp_KeyUsage,
453233294Sstas			 (generic_copy)copy_KeyUsage);
454178825Sdfr}
455178825Sdfr
456178825Sdfrstatic int
457233294Sstascmp_TicketFlags (void *a, void *b)
458233294Sstas{
459233294Sstas    TicketFlags *aa = a;
460233294Sstas    TicketFlags *ab = b;
461233294Sstas
462233294Sstas    return TicketFlags2int(*aa) != TicketFlags2int(*ab);
463233294Sstas}
464233294Sstas
465233294Sstasstatic int
466233294Sstastest_bit_string_rfc1510 (void)
467233294Sstas{
468233294Sstas    struct test_case tests[] = {
469233294Sstas	{ NULL, 7,
470233294Sstas	  "\x03\x05\x00\x80\x00\x00\x00",
471233294Sstas	  "TF bitstring 1"
472233294Sstas	},
473233294Sstas	{ NULL, 7,
474233294Sstas	  "\x03\x05\x00\x40\x20\x00\x00",
475233294Sstas	  "TF bitstring 2"
476233294Sstas	},
477233294Sstas	{ NULL, 7,
478233294Sstas	  "\x03\x05\x00\x00\x20\x00\x00",
479233294Sstas	  "TF bitstring 3"
480233294Sstas	},
481233294Sstas	{ NULL, 7,
482233294Sstas	  "\x03\x05\x00\x00\x00\x00\x00",
483233294Sstas	  "TF bitstring 4"
484233294Sstas	}
485233294Sstas    };
486233294Sstas
487233294Sstas    int ntests = sizeof(tests) / sizeof(*tests);
488233294Sstas    TicketFlags tf1, tf2, tf3, tf4;
489233294Sstas
490233294Sstas    memset(&tf1, 0, sizeof(tf1));
491233294Sstas    tf1.reserved = 1;
492233294Sstas    tests[0].val = &tf1;
493233294Sstas
494233294Sstas    memset(&tf2, 0, sizeof(tf2));
495233294Sstas    tf2.forwardable = 1;
496233294Sstas    tf2.pre_authent = 1;
497233294Sstas    tests[1].val = &tf2;
498233294Sstas
499233294Sstas    memset(&tf3, 0, sizeof(tf3));
500233294Sstas    tf3.pre_authent = 1;
501233294Sstas    tests[2].val = &tf3;
502233294Sstas
503233294Sstas    memset(&tf4, 0, sizeof(tf4));
504233294Sstas    tests[3].val = &tf4;
505233294Sstas
506233294Sstas
507233294Sstas    return generic_test (tests, ntests, sizeof(TicketFlags),
508233294Sstas			 (generic_encode)encode_TicketFlags,
509233294Sstas			 (generic_length)length_TicketFlags,
510233294Sstas			 (generic_decode)decode_TicketFlags,
511233294Sstas			 (generic_free)free_TicketFlags,
512233294Sstas			 cmp_TicketFlags,
513233294Sstas			 (generic_copy)copy_TicketFlags);
514233294Sstas}
515233294Sstas
516233294Sstasstatic int
517233294Sstascmp_KerberosTime (void *a, void *b)
518233294Sstas{
519233294Sstas    KerberosTime *aa = a;
520233294Sstas    KerberosTime *ab = b;
521233294Sstas
522233294Sstas    return *aa != *ab;
523233294Sstas}
524233294Sstas
525233294Sstasstatic int
526233294Sstastest_time (void)
527233294Sstas{
528233294Sstas    struct test_case tests[] = {
529233294Sstas	{ NULL,  17,
530233294Sstas	  "\x18\x0f\x31\x39\x37\x30\x30\x31\x30\x31\x30\x31\x31\x38\x33\x31"
531233294Sstas	  "\x5a",
532233294Sstas	  "time 1" },
533233294Sstas	{ NULL,  17,
534233294Sstas	  "\x18\x0f\x32\x30\x30\x39\x30\x35\x32\x34\x30\x32\x30\x32\x34\x30"
535233294Sstas	  "\x5a"
536233294Sstas	  "time 2" }
537233294Sstas    };
538233294Sstas
539233294Sstas    int ntests = sizeof(tests) / sizeof(*tests);
540233294Sstas    KerberosTime times[] = {
541233294Sstas	4711,
542233294Sstas	1243130560
543233294Sstas    };
544233294Sstas
545233294Sstas    tests[0].val = &times[0];
546233294Sstas    tests[1].val = &times[1];
547233294Sstas
548233294Sstas    return generic_test (tests, ntests, sizeof(KerberosTime),
549233294Sstas			 (generic_encode)encode_KerberosTime,
550233294Sstas			 (generic_length)length_KerberosTime,
551233294Sstas			 (generic_decode)decode_KerberosTime,
552233294Sstas			 (generic_free)free_KerberosTime,
553233294Sstas			 cmp_KerberosTime,
554233294Sstas			 (generic_copy)copy_KerberosTime);
555233294Sstas}
556233294Sstas
557233294Sstasstruct {
558233294Sstas    const char *cert;
559233294Sstas    size_t len;
560233294Sstas} certs[] = {
561233294Sstas    {
562233294Sstas	"\x30\x82\x02\x6c\x30\x82\x01\xd5\xa0\x03\x02\x01\x02\x02\x09\x00"
563233294Sstas	"\x99\x32\xde\x61\x0e\x40\x19\x8a\x30\x0d\x06\x09\x2a\x86\x48\x86"
564233294Sstas	"\xf7\x0d\x01\x01\x05\x05\x00\x30\x2a\x31\x1b\x30\x19\x06\x03\x55"
565233294Sstas	"\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52"
566233294Sstas	"\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13"
567233294Sstas	"\x02\x53\x45\x30\x1e\x17\x0d\x30\x39\x30\x34\x32\x36\x32\x30\x32"
568233294Sstas	"\x39\x34\x30\x5a\x17\x0d\x31\x39\x30\x34\x32\x34\x32\x30\x32\x39"
569233294Sstas	"\x34\x30\x5a\x30\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12"
570233294Sstas	"\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20"
571233294Sstas	"\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30"
572233294Sstas	"\x81\x9f\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05"
573233294Sstas	"\x00\x03\x81\x8d\x00\x30\x81\x89\x02\x81\x81\x00\xb9\xd3\x1b\x67"
574233294Sstas	"\x1c\xf7\x5e\x26\x81\x3b\x82\xff\x03\xa4\x43\xb5\xb2\x63\x0b\x89"
575233294Sstas	"\x58\x43\xfe\x3d\xe0\x38\x7d\x93\x74\xbb\xad\x21\xa4\x29\xd9\x34"
576233294Sstas	"\x79\xf3\x1c\x8c\x5a\xd6\xb0\xd7\x19\xea\xcc\xaf\xe0\xa8\x40\x02"
577233294Sstas	"\x1d\x91\xf1\xac\x36\xb0\xfb\x08\xbd\xcc\x9a\xe1\xb7\x6e\xee\x0a"
578233294Sstas	"\x69\xbf\x6d\x2b\xee\x20\x82\x61\x06\xf2\x18\xcc\x89\x11\x64\x7e"
579233294Sstas	"\xb2\xff\x47\xd1\x3b\x52\x73\xeb\x5a\xc0\x03\xa6\x4b\xc7\x40\x7e"
580233294Sstas	"\xbc\xe1\x0e\x65\x44\x3f\x40\x8b\x02\x82\x54\x04\xd9\xcc\x2c\x67"
581233294Sstas	"\x01\xb6\x16\x82\xd8\x33\x53\x17\xd7\xde\x8d\x5d\x02\x03\x01\x00"
582233294Sstas	"\x01\xa3\x81\x99\x30\x81\x96\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16"
583233294Sstas	"\x04\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30\xdd"
584233294Sstas	"\x27\x96\x59\x9b\x0e\x68\x30\x5a\x06\x03\x55\x1d\x23\x04\x53\x30"
585233294Sstas	"\x51\x80\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30"
586233294Sstas	"\xdd\x27\x96\x59\x9b\x0e\x68\xa1\x2e\xa4\x2c\x30\x2a\x31\x1b\x30"
587233294Sstas	"\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65"
588233294Sstas	"\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03"
589233294Sstas	"\x55\x04\x06\x13\x02\x53\x45\x82\x09\x00\x99\x32\xde\x61\x0e\x40"
590233294Sstas	"\x19\x8a\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff"
591233294Sstas	"\x30\x0b\x06\x03\x55\x1d\x0f\x04\x04\x03\x02\x01\xe6\x30\x0d\x06"
592233294Sstas	"\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x81\x81\x00"
593233294Sstas	"\x52\x9b\xe4\x0e\xee\xc2\x5d\xb7\xf1\xba\x47\xe3\xfe\xaf\x3d\x51"
594233294Sstas	"\x10\xfd\xe8\x0d\x14\x58\x05\x36\xa7\xeb\xd8\x05\xe5\x27\x6f\x51"
595233294Sstas	"\xb8\xec\x90\xd9\x03\xe1\xbc\x9c\x93\x38\x21\x5c\xaf\x4e\x6c\x7b"
596233294Sstas	"\x6c\x65\xa9\x92\xcd\x94\xef\xa8\xae\x90\x12\x14\x78\x2d\xa3\x15"
597233294Sstas	"\xaa\x42\xf1\xd9\x44\x64\x2c\x3c\xc0\xbd\x3a\x48\xd8\x80\x45\x8b"
598233294Sstas	"\xd1\x79\x82\xe0\x0f\xdf\x08\x3c\x60\x21\x6f\x31\x47\x98\xae\x2f"
599233294Sstas	"\xcb\xb1\xa1\xb9\xc1\xa3\x71\x5e\x4a\xc2\x67\xdf\x66\x0a\x51\xb5"
600233294Sstas	"\xad\x60\x05\xdb\x02\xd4\x1a\xd2\xb9\x4e\x01\x08\x2b\xc3\x57\xaf",
601233294Sstas	624 },
602233294Sstas    {
603233294Sstas	"\x30\x82\x02\x54\x30\x82\x01\xbd\xa0\x03\x02\x01\x02\x02\x01\x08"
604233294Sstas	"\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x30"
605233294Sstas	"\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30"
606233294Sstas	"\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b"
607233294Sstas	"\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30\x1e\x17\x0d\x30"
608233294Sstas	"\x39\x30\x34\x32\x36\x32\x30\x32\x39\x34\x30\x5a\x17\x0d\x31\x39"
609233294Sstas	"\x30\x34\x32\x34\x32\x30\x32\x39\x34\x30\x5a\x30\x1b\x31\x0b\x30"
610233294Sstas	"\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x31\x0c\x30\x0a\x06\x03"
611233294Sstas	"\x55\x04\x03\x0c\x03\x6b\x64\x63\x30\x81\x9f\x30\x0d\x06\x09\x2a"
612233294Sstas	"\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81\x8d\x00\x30\x81"
613233294Sstas	"\x89\x02\x81\x81\x00\xd2\x41\x7a\xf8\x4b\x55\xb2\xaf\x11\xf9\x43"
614233294Sstas	"\x9b\x43\x81\x09\x3b\x9a\x94\xcf\x00\xf4\x85\x75\x92\xd7\x2a\xa5"
615233294Sstas	"\x11\xf1\xa8\x50\x6e\xc6\x84\x74\x24\x17\xda\x84\xc8\x03\x37\xb2"
616233294Sstas	"\x20\xf3\xba\xb5\x59\x36\x21\x4d\xab\x70\xe2\xc3\x09\x93\x68\x14"
617233294Sstas	"\x12\x79\xc5\xbb\x9e\x1b\x4a\xf0\xc6\x24\x59\x25\xc3\x1c\xa8\x70"
618233294Sstas	"\x66\x5b\x3e\x41\x8e\xe3\x25\x71\x9a\x94\xa0\x5b\x46\x91\x6f\xdd"
619233294Sstas	"\x58\x14\xec\x89\xe5\x8c\x96\xc5\x38\x60\xe4\xab\xf2\x75\xee\x6e"
620233294Sstas	"\x62\xfc\xe1\xbd\x03\x47\xff\xc4\xbe\x0f\xca\x70\x73\xe3\x74\x58"
621233294Sstas	"\x3a\x2f\x04\x2d\x39\x02\x03\x01\x00\x01\xa3\x81\x98\x30\x81\x95"
622233294Sstas	"\x30\x09\x06\x03\x55\x1d\x13\x04\x02\x30\x00\x30\x0b\x06\x03\x55"
623233294Sstas	"\x1d\x0f\x04\x04\x03\x02\x05\xe0\x30\x12\x06\x03\x55\x1d\x25\x04"
624233294Sstas	"\x0b\x30\x09\x06\x07\x2b\x06\x01\x05\x02\x03\x05\x30\x1d\x06\x03"
625233294Sstas	"\x55\x1d\x0e\x04\x16\x04\x14\x3a\xd3\x73\xff\xab\xdb\x7d\x8d\xc6"
626233294Sstas	"\x3a\xa2\x26\x3e\xae\x78\x95\x80\xc9\xe6\x31\x30\x48\x06\x03\x55"
627233294Sstas	"\x1d\x11\x04\x41\x30\x3f\xa0\x3d\x06\x06\x2b\x06\x01\x05\x02\x02"
628233294Sstas	"\xa0\x33\x30\x31\xa0\x0d\x1b\x0b\x54\x45\x53\x54\x2e\x48\x35\x4c"
629233294Sstas	"\x2e\x53\x45\xa1\x20\x30\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15"
630233294Sstas	"\x1b\x06\x6b\x72\x62\x74\x67\x74\x1b\x0b\x54\x45\x53\x54\x2e\x48"
631233294Sstas	"\x35\x4c\x2e\x53\x45\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01"
632233294Sstas	"\x01\x05\x05\x00\x03\x81\x81\x00\x83\xf4\x14\xa7\x6e\x59\xff\x80"
633233294Sstas	"\x64\xe7\xfa\xcf\x13\x80\x86\xe1\xed\x02\x38\xad\x96\x72\x25\xe5"
634233294Sstas	"\x06\x7a\x9a\xbc\x24\x74\xa9\x75\x55\xb2\x49\x80\x69\x45\x95\x4a"
635233294Sstas	"\x4c\x76\xa9\xe3\x4e\x49\xd3\xc2\x69\x5a\x95\x03\xeb\xba\x72\x23"
636233294Sstas	"\x9c\xfd\x3d\x8b\xc6\x07\x82\x3b\xf4\xf3\xef\x6c\x2e\x9e\x0b\xac"
637233294Sstas	"\x9e\x6c\xbb\x37\x4a\xa1\x9e\x73\xd1\xdc\x97\x61\xba\xfc\xd3\x49"
638233294Sstas	"\xa6\xc2\x4c\x55\x2e\x06\x37\x76\xb5\xef\x57\xe7\x57\x58\x8a\x71"
639233294Sstas	"\x63\xf3\xeb\xe7\x55\x68\x0d\xf6\x46\x4c\xfb\xf9\x43\xbb\x0c\x92"
640233294Sstas	"\x4f\x4e\x22\x7b\x63\xe8\x4f\x9c",
641233294Sstas	600
642233294Sstas    }
643233294Sstas};
644233294Sstas
645233294Sstasstatic int
646233294Sstastest_cert(void)
647233294Sstas{
648233294Sstas    Certificate c, c2;
649233294Sstas    size_t size;
650233294Sstas    size_t i;
651233294Sstas    int ret;
652233294Sstas
653233294Sstas    for (i = 0; i < sizeof(certs)/sizeof(certs[0]); i++) {
654233294Sstas
655233294Sstas	ret = decode_Certificate((unsigned char *)certs[i].cert,
656233294Sstas				 certs[i].len, &c, &size);
657233294Sstas	if (ret)
658233294Sstas	    return ret;
659233294Sstas
660233294Sstas	ret = copy_Certificate(&c, &c2);
661233294Sstas	free_Certificate(&c);
662233294Sstas	if (ret)
663233294Sstas	    return ret;
664233294Sstas
665233294Sstas	free_Certificate(&c2);
666233294Sstas    }
667233294Sstas
668233294Sstas    return 0;
669233294Sstas}
670233294Sstas
671233294Sstas
672233294Sstasstatic int
673178825Sdfrcmp_TESTLargeTag (void *a, void *b)
674178825Sdfr{
675178825Sdfr    TESTLargeTag *aa = a;
676178825Sdfr    TESTLargeTag *ab = b;
677178825Sdfr
678178825Sdfr    COMPARE_INTEGER(aa,ab,foo);
679233294Sstas    COMPARE_INTEGER(aa,ab,bar);
680178825Sdfr    return 0;
681178825Sdfr}
682178825Sdfr
683178825Sdfrstatic int
684178825Sdfrtest_large_tag (void)
685178825Sdfr{
686178825Sdfr    struct test_case tests[] = {
687233294Sstas	{ NULL,  15,  "\x30\x0d\xbf\x7f\x03\x02\x01\x01\xbf\x81\x00\x03\x02\x01\x02", "large tag 1" }
688178825Sdfr    };
689178825Sdfr
690178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
691178825Sdfr    TESTLargeTag lt1;
692178825Sdfr
693178825Sdfr    memset(&lt1, 0, sizeof(lt1));
694178825Sdfr    lt1.foo = 1;
695233294Sstas    lt1.bar = 2;
696178825Sdfr
697178825Sdfr    tests[0].val = &lt1;
698178825Sdfr
699178825Sdfr    return generic_test (tests, ntests, sizeof(TESTLargeTag),
700178825Sdfr			 (generic_encode)encode_TESTLargeTag,
701178825Sdfr			 (generic_length)length_TESTLargeTag,
702178825Sdfr			 (generic_decode)decode_TESTLargeTag,
703178825Sdfr			 (generic_free)free_TESTLargeTag,
704233294Sstas			 cmp_TESTLargeTag,
705233294Sstas			 (generic_copy)copy_TESTLargeTag);
706178825Sdfr}
707178825Sdfr
708178825Sdfrstruct test_data {
709178825Sdfr    int ok;
710178825Sdfr    size_t len;
711178825Sdfr    size_t expected_len;
712178825Sdfr    void *data;
713178825Sdfr};
714178825Sdfr
715178825Sdfrstatic int
716178825Sdfrcheck_tag_length(void)
717178825Sdfr{
718178825Sdfr    struct test_data td[] = {
719178825Sdfr	{ 1, 3, 3, "\x02\x01\x00"},
720178825Sdfr	{ 1, 3, 3, "\x02\x01\x7f"},
721178825Sdfr	{ 1, 4, 4, "\x02\x02\x00\x80"},
722178825Sdfr	{ 1, 4, 4, "\x02\x02\x01\x00"},
723178825Sdfr	{ 1, 4, 4, "\x02\x02\x02\x00"},
724178825Sdfr	{ 0, 3, 0, "\x02\x02\x00"},
725178825Sdfr	{ 0, 3, 0, "\x02\x7f\x7f"},
726178825Sdfr	{ 0, 4, 0, "\x02\x03\x00\x80"},
727178825Sdfr	{ 0, 4, 0, "\x02\x7f\x01\x00"},
728178825Sdfr	{ 0, 5, 0, "\x02\xff\x7f\x02\x00"}
729178825Sdfr    };
730178825Sdfr    size_t sz;
731233294Sstas    TESTuint32 values[] = {0, 127, 128, 256, 512,
732178825Sdfr			 0, 127, 128, 256, 512 };
733233294Sstas    TESTuint32 u;
734178825Sdfr    int i, ret, failed = 0;
735178825Sdfr    void *buf;
736178825Sdfr
737178825Sdfr    for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
738178825Sdfr	struct map_page *page;
739178825Sdfr
740178825Sdfr	buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
741178825Sdfr
742233294Sstas	ret = decode_TESTuint32(buf, td[i].len, &u, &sz);
743178825Sdfr	if (ret) {
744178825Sdfr	    if (td[i].ok) {
745178825Sdfr		printf("failed with tag len test %d\n", i);
746178825Sdfr		failed = 1;
747178825Sdfr	    }
748178825Sdfr	} else {
749178825Sdfr	    if (td[i].ok == 0) {
750178825Sdfr		printf("failed with success for tag len test %d\n", i);
751178825Sdfr		failed = 1;
752178825Sdfr	    }
753178825Sdfr	    if (td[i].expected_len != sz) {
754178825Sdfr		printf("wrong expected size for tag test %d\n", i);
755178825Sdfr		failed = 1;
756178825Sdfr	    }
757178825Sdfr	    if (values[i] != u) {
758178825Sdfr		printf("wrong value for tag test %d\n", i);
759178825Sdfr		failed = 1;
760178825Sdfr	    }
761178825Sdfr	}
762178825Sdfr	map_free(page, "test", "decode");
763178825Sdfr    }
764178825Sdfr    return failed;
765178825Sdfr}
766178825Sdfr
767178825Sdfrstatic int
768178825Sdfrcmp_TESTChoice (void *a, void *b)
769178825Sdfr{
770178825Sdfr    return 0;
771178825Sdfr}
772178825Sdfr
773178825Sdfrstatic int
774178825Sdfrtest_choice (void)
775178825Sdfr{
776178825Sdfr    struct test_case tests[] = {
777178825Sdfr	{ NULL,  5,  "\xa1\x03\x02\x01\x01", "large choice 1" },
778178825Sdfr	{ NULL,  5,  "\xa2\x03\x02\x01\x02", "large choice 2" }
779178825Sdfr    };
780178825Sdfr
781178825Sdfr    int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
782178825Sdfr    TESTChoice1 c1;
783178825Sdfr    TESTChoice1 c2_1;
784178825Sdfr    TESTChoice2 c2_2;
785178825Sdfr
786178825Sdfr    memset(&c1, 0, sizeof(c1));
787178825Sdfr    c1.element = choice_TESTChoice1_i1;
788178825Sdfr    c1.u.i1 = 1;
789178825Sdfr    tests[0].val = &c1;
790178825Sdfr
791178825Sdfr    memset(&c2_1, 0, sizeof(c2_1));
792178825Sdfr    c2_1.element = choice_TESTChoice1_i2;
793178825Sdfr    c2_1.u.i2 = 2;
794178825Sdfr    tests[1].val = &c2_1;
795178825Sdfr
796178825Sdfr    ret += generic_test (tests, ntests, sizeof(TESTChoice1),
797178825Sdfr			 (generic_encode)encode_TESTChoice1,
798178825Sdfr			 (generic_length)length_TESTChoice1,
799178825Sdfr			 (generic_decode)decode_TESTChoice1,
800178825Sdfr			 (generic_free)free_TESTChoice1,
801233294Sstas			 cmp_TESTChoice,
802233294Sstas			 (generic_copy)copy_TESTChoice1);
803178825Sdfr
804178825Sdfr    memset(&c2_2, 0, sizeof(c2_2));
805178825Sdfr    c2_2.element = choice_TESTChoice2_asn1_ellipsis;
806178825Sdfr    c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
807178825Sdfr    c2_2.u.asn1_ellipsis.length = 5;
808178825Sdfr    tests[1].val = &c2_2;
809178825Sdfr
810178825Sdfr    ret += generic_test (tests, ntests, sizeof(TESTChoice2),
811178825Sdfr			 (generic_encode)encode_TESTChoice2,
812178825Sdfr			 (generic_length)length_TESTChoice2,
813178825Sdfr			 (generic_decode)decode_TESTChoice2,
814178825Sdfr			 (generic_free)free_TESTChoice2,
815233294Sstas			 cmp_TESTChoice,
816233294Sstas			 (generic_copy)copy_TESTChoice2);
817178825Sdfr
818178825Sdfr    return ret;
819178825Sdfr}
820178825Sdfr
821178825Sdfrstatic int
822178825Sdfrcmp_TESTImplicit (void *a, void *b)
823178825Sdfr{
824178825Sdfr    TESTImplicit *aa = a;
825178825Sdfr    TESTImplicit *ab = b;
826178825Sdfr
827178825Sdfr    COMPARE_INTEGER(aa,ab,ti1);
828178825Sdfr    COMPARE_INTEGER(aa,ab,ti2.foo);
829178825Sdfr    COMPARE_INTEGER(aa,ab,ti3);
830178825Sdfr    return 0;
831178825Sdfr}
832178825Sdfr
833178825Sdfr/*
834178825SdfrUNIV CONS Sequence 14
835178825Sdfr  CONTEXT PRIM 0 1 00
836178825Sdfr  CONTEXT CONS 1 6
837178825Sdfr   CONTEXT CONS 127 3
838178825Sdfr     UNIV PRIM Integer 1 02
839178825Sdfr  CONTEXT PRIM 2 1 03
840178825Sdfr*/
841178825Sdfr
842178825Sdfrstatic int
843178825Sdfrtest_implicit (void)
844178825Sdfr{
845178825Sdfr    struct test_case tests[] = {
846233294Sstas	{ NULL,  16,
847178825Sdfr	  "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
848233294Sstas	  "\x7f\x03\x02\x01\x02\x82\x01\x03",
849178825Sdfr	  "implicit 1" }
850178825Sdfr    };
851178825Sdfr
852178825Sdfr    int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
853178825Sdfr    TESTImplicit c0;
854178825Sdfr
855178825Sdfr    memset(&c0, 0, sizeof(c0));
856178825Sdfr    c0.ti1 = 0;
857178825Sdfr    c0.ti2.foo = 2;
858178825Sdfr    c0.ti3 = 3;
859178825Sdfr    tests[0].val = &c0;
860178825Sdfr
861178825Sdfr    ret += generic_test (tests, ntests, sizeof(TESTImplicit),
862178825Sdfr			 (generic_encode)encode_TESTImplicit,
863178825Sdfr			 (generic_length)length_TESTImplicit,
864178825Sdfr			 (generic_decode)decode_TESTImplicit,
865178825Sdfr			 (generic_free)free_TESTImplicit,
866233294Sstas			 cmp_TESTImplicit,
867233294Sstas			 (generic_copy)copy_TESTImplicit);
868178825Sdfr
869178825Sdfr#ifdef IMPLICIT_TAGGING_WORKS
870178825Sdfr    ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
871178825Sdfr			 (generic_encode)encode_TESTImplicit2,
872178825Sdfr			 (generic_length)length_TESTImplicit2,
873178825Sdfr			 (generic_decode)decode_TESTImplicit2,
874178825Sdfr			 (generic_free)free_TESTImplicit2,
875233294Sstas			 cmp_TESTImplicit,
876233294Sstas			 NULL);
877178825Sdfr
878178825Sdfr#endif /* IMPLICIT_TAGGING_WORKS */
879178825Sdfr    return ret;
880178825Sdfr}
881178825Sdfr
882178825Sdfrstatic int
883178825Sdfrcmp_TESTAlloc (void *a, void *b)
884178825Sdfr{
885178825Sdfr    TESTAlloc *aa = a;
886178825Sdfr    TESTAlloc *ab = b;
887178825Sdfr
888178825Sdfr    IF_OPT_COMPARE(aa,ab,tagless) {
889178825Sdfr	COMPARE_INTEGER(aa,ab,tagless->ai);
890178825Sdfr    }
891178825Sdfr
892178825Sdfr    COMPARE_INTEGER(aa,ab,three);
893178825Sdfr
894178825Sdfr    IF_OPT_COMPARE(aa,ab,tagless2) {
895178825Sdfr	COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
896178825Sdfr    }
897178825Sdfr
898178825Sdfr    return 0;
899178825Sdfr}
900178825Sdfr
901178825Sdfr/*
902178825SdfrUNIV CONS Sequence 12
903178825Sdfr  UNIV CONS Sequence 5
904178825Sdfr    CONTEXT CONS 0 3
905178825Sdfr      UNIV PRIM Integer 1 01
906178825Sdfr  CONTEXT CONS 1 3
907178825Sdfr    UNIV PRIM Integer 1 03
908178825Sdfr
909178825SdfrUNIV CONS Sequence 5
910178825Sdfr  CONTEXT CONS 1 3
911178825Sdfr    UNIV PRIM Integer 1 03
912178825Sdfr
913178825SdfrUNIV CONS Sequence 8
914178825Sdfr  CONTEXT CONS 1 3
915178825Sdfr    UNIV PRIM Integer 1 04
916178825Sdfr  UNIV PRIM Integer 1 05
917178825Sdfr
918178825Sdfr*/
919178825Sdfr
920178825Sdfrstatic int
921178825Sdfrtest_taglessalloc (void)
922178825Sdfr{
923178825Sdfr    struct test_case tests[] = {
924233294Sstas	{ NULL,  14,
925233294Sstas	  "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
926178825Sdfr	  "alloc 1" },
927233294Sstas	{ NULL,  7,
928233294Sstas	  "\x30\x05\xa1\x03\x02\x01\x03",
929178825Sdfr	  "alloc 2" },
930233294Sstas	{ NULL,  10,
931233294Sstas	  "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
932178825Sdfr	  "alloc 3" }
933178825Sdfr    };
934178825Sdfr
935178825Sdfr    int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
936178825Sdfr    TESTAlloc c1, c2, c3;
937178825Sdfr    heim_any any3;
938178825Sdfr
939178825Sdfr    memset(&c1, 0, sizeof(c1));
940178825Sdfr    c1.tagless = ecalloc(1, sizeof(*c1.tagless));
941178825Sdfr    c1.tagless->ai = 1;
942178825Sdfr    c1.three = 3;
943178825Sdfr    tests[0].val = &c1;
944178825Sdfr
945178825Sdfr    memset(&c2, 0, sizeof(c2));
946178825Sdfr    c2.tagless = NULL;
947178825Sdfr    c2.three = 3;
948178825Sdfr    tests[1].val = &c2;
949178825Sdfr
950178825Sdfr    memset(&c3, 0, sizeof(c3));
951178825Sdfr    c3.tagless = NULL;
952178825Sdfr    c3.three = 4;
953178825Sdfr    c3.tagless2 = &any3;
954178825Sdfr    any3.data = "\x02\x01\x05";
955178825Sdfr    any3.length = 3;
956178825Sdfr    tests[2].val = &c3;
957178825Sdfr
958178825Sdfr    ret += generic_test (tests, ntests, sizeof(TESTAlloc),
959178825Sdfr			 (generic_encode)encode_TESTAlloc,
960178825Sdfr			 (generic_length)length_TESTAlloc,
961178825Sdfr			 (generic_decode)decode_TESTAlloc,
962178825Sdfr			 (generic_free)free_TESTAlloc,
963233294Sstas			 cmp_TESTAlloc,
964233294Sstas			 (generic_copy)copy_TESTAlloc);
965178825Sdfr
966178825Sdfr    free(c1.tagless);
967178825Sdfr
968178825Sdfr    return ret;
969178825Sdfr}
970178825Sdfr
971233294Sstasstatic int
972233294Sstascmp_TESTOptional (void *a, void *b)
973233294Sstas{
974233294Sstas    TESTOptional *aa = a;
975233294Sstas    TESTOptional *ab = b;
976178825Sdfr
977233294Sstas    IF_OPT_COMPARE(aa,ab,zero) {
978233294Sstas	COMPARE_OPT_INTEGER(aa,ab,zero);
979233294Sstas    }
980233294Sstas    IF_OPT_COMPARE(aa,ab,one) {
981233294Sstas	COMPARE_OPT_INTEGER(aa,ab,one);
982233294Sstas    }
983233294Sstas    return 0;
984233294Sstas}
985233294Sstas
986233294Sstas/*
987233294SstasUNIV CONS Sequence 5
988233294Sstas  CONTEXT CONS 0 3
989233294Sstas    UNIV PRIM Integer 1 00
990233294Sstas
991233294SstasUNIV CONS Sequence 5
992233294Sstas  CONTEXT CONS 1 3
993233294Sstas    UNIV PRIM Integer 1 03
994233294Sstas
995233294SstasUNIV CONS Sequence 10
996233294Sstas  CONTEXT CONS 0 3
997233294Sstas    UNIV PRIM Integer 1 00
998233294Sstas  CONTEXT CONS 1 3
999233294Sstas    UNIV PRIM Integer 1 01
1000233294Sstas
1001233294Sstas*/
1002233294Sstas
1003178825Sdfrstatic int
1004233294Sstastest_optional (void)
1005233294Sstas{
1006233294Sstas    struct test_case tests[] = {
1007233294Sstas	{ NULL,  2,
1008233294Sstas	  "\x30\x00",
1009233294Sstas	  "optional 0" },
1010233294Sstas	{ NULL,  7,
1011233294Sstas	  "\x30\x05\xa0\x03\x02\x01\x00",
1012233294Sstas	  "optional 1" },
1013233294Sstas	{ NULL,  7,
1014233294Sstas	  "\x30\x05\xa1\x03\x02\x01\x01",
1015233294Sstas	  "optional 2" },
1016233294Sstas	{ NULL,  12,
1017233294Sstas	  "\x30\x0a\xa0\x03\x02\x01\x00\xa1\x03\x02\x01\x01",
1018233294Sstas	  "optional 3" }
1019233294Sstas    };
1020233294Sstas
1021233294Sstas    int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1022233294Sstas    TESTOptional c0, c1, c2, c3;
1023233294Sstas    int zero = 0;
1024233294Sstas    int one = 1;
1025233294Sstas
1026233294Sstas    c0.zero = NULL;
1027233294Sstas    c0.one = NULL;
1028233294Sstas    tests[0].val = &c0;
1029233294Sstas
1030233294Sstas    c1.zero = &zero;
1031233294Sstas    c1.one = NULL;
1032233294Sstas    tests[1].val = &c1;
1033233294Sstas
1034233294Sstas    c2.zero = NULL;
1035233294Sstas    c2.one = &one;
1036233294Sstas    tests[2].val = &c2;
1037233294Sstas
1038233294Sstas    c3.zero = &zero;
1039233294Sstas    c3.one = &one;
1040233294Sstas    tests[3].val = &c3;
1041233294Sstas
1042233294Sstas    ret += generic_test (tests, ntests, sizeof(TESTOptional),
1043233294Sstas			 (generic_encode)encode_TESTOptional,
1044233294Sstas			 (generic_length)length_TESTOptional,
1045233294Sstas			 (generic_decode)decode_TESTOptional,
1046233294Sstas			 (generic_free)free_TESTOptional,
1047233294Sstas			 cmp_TESTOptional,
1048233294Sstas			 (generic_copy)copy_TESTOptional);
1049233294Sstas
1050233294Sstas    return ret;
1051233294Sstas}
1052233294Sstas
1053233294Sstasstatic int
1054178825Sdfrcheck_fail_largetag(void)
1055178825Sdfr{
1056178825Sdfr    struct test_case tests[] = {
1057178825Sdfr	{NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
1058178825Sdfr	 "tag overflow"},
1059178825Sdfr	{NULL, 0, "", "empty buffer"},
1060178825Sdfr	{NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
1061178825Sdfr	 "one too short" },
1062178825Sdfr	{NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
1063178825Sdfr	 "two too short" },
1064178825Sdfr	{NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
1065178825Sdfr	 "three too short" },
1066178825Sdfr	{NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
1067178825Sdfr	 "four too short" },
1068178825Sdfr	{NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
1069178825Sdfr	 "five too short" },
1070178825Sdfr	{NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
1071178825Sdfr	 "six too short" },
1072178825Sdfr	{NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
1073178825Sdfr	 "inner one too long" },
1074178825Sdfr	{NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
1075178825Sdfr	 "inner one too short" },
1076178825Sdfr	{NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
1077178825Sdfr	 "inner one too short"},
1078178825Sdfr	{NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
1079178825Sdfr	 "wrong tag"},
1080178825Sdfr	{NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
1081178825Sdfr	 "still wrong tag"}
1082178825Sdfr    };
1083178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
1084178825Sdfr
1085178825Sdfr    return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
1086178825Sdfr			       (generic_decode)decode_TESTLargeTag);
1087178825Sdfr}
1088178825Sdfr
1089178825Sdfr
1090178825Sdfrstatic int
1091178825Sdfrcheck_fail_sequence(void)
1092178825Sdfr{
1093178825Sdfr    struct test_case tests[] = {
1094178825Sdfr	{NULL, 0, "", "empty buffer"},
1095233294Sstas	{NULL, 24,
1096178825Sdfr	 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1097178825Sdfr	 "\x02\x01\x01\xa2\x03\x02\x01\x01"
1098178825Sdfr	 "missing one byte from the end, internal length ok"},
1099178825Sdfr	{NULL, 25,
1100178825Sdfr	 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1101178825Sdfr	 "\x02\x01\x01\xa2\x03\x02\x01\x01",
1102178825Sdfr	 "inner length one byte too long"},
1103233294Sstas	{NULL, 24,
1104178825Sdfr	 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
1105178825Sdfr	 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
1106178825Sdfr	 "correct buffer but missing one too short"}
1107178825Sdfr    };
1108178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
1109178825Sdfr
1110178825Sdfr    return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
1111178825Sdfr			       (generic_decode)decode_TESTSeq);
1112178825Sdfr}
1113178825Sdfr
1114178825Sdfrstatic int
1115178825Sdfrcheck_fail_choice(void)
1116178825Sdfr{
1117178825Sdfr    struct test_case tests[] = {
1118178825Sdfr	{NULL, 6,
1119178825Sdfr	 "\xa1\x02\x02\x01\x01",
1120233294Sstas	 "choice one too short"},
1121178825Sdfr	{NULL, 6,
1122178825Sdfr	 "\xa1\x03\x02\x02\x01",
1123233294Sstas	 "choice one too short inner"}
1124178825Sdfr    };
1125178825Sdfr    int ntests = sizeof(tests) / sizeof(*tests);
1126178825Sdfr
1127178825Sdfr    return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
1128178825Sdfr			       (generic_decode)decode_TESTChoice1);
1129178825Sdfr}
1130178825Sdfr
1131178825Sdfrstatic int
1132178825Sdfrcheck_seq(void)
1133178825Sdfr{
1134178825Sdfr    TESTSeqOf seq;
1135178825Sdfr    TESTInteger i;
1136178825Sdfr    int ret;
1137178825Sdfr
1138178825Sdfr    seq.val = NULL;
1139178825Sdfr    seq.len = 0;
1140178825Sdfr
1141178825Sdfr    ret = add_TESTSeqOf(&seq, &i);
1142178825Sdfr    if (ret) { printf("failed adding\n"); goto out; }
1143178825Sdfr    ret = add_TESTSeqOf(&seq, &i);
1144178825Sdfr    if (ret) { printf("failed adding\n"); goto out; }
1145178825Sdfr    ret = add_TESTSeqOf(&seq, &i);
1146178825Sdfr    if (ret) { printf("failed adding\n"); goto out; }
1147178825Sdfr    ret = add_TESTSeqOf(&seq, &i);
1148178825Sdfr    if (ret) { printf("failed adding\n"); goto out; }
1149178825Sdfr
1150178825Sdfr    ret = remove_TESTSeqOf(&seq, seq.len - 1);
1151178825Sdfr    if (ret) { printf("failed removing\n"); goto out; }
1152178825Sdfr    ret = remove_TESTSeqOf(&seq, 2);
1153178825Sdfr    if (ret) { printf("failed removing\n"); goto out; }
1154178825Sdfr    ret = remove_TESTSeqOf(&seq, 0);
1155178825Sdfr    if (ret) { printf("failed removing\n"); goto out; }
1156178825Sdfr    ret = remove_TESTSeqOf(&seq, 0);
1157178825Sdfr    if (ret) { printf("failed removing\n"); goto out; }
1158178825Sdfr    ret = remove_TESTSeqOf(&seq, 0);
1159178825Sdfr    if (ret == 0) {
1160178825Sdfr	printf("can remove from empty list");
1161178825Sdfr	return 1;
1162178825Sdfr    }
1163178825Sdfr
1164178825Sdfr    if (seq.len != 0) {
1165178825Sdfr	printf("seq not empty!");
1166178825Sdfr	return 1;
1167178825Sdfr    }
1168178825Sdfr    free_TESTSeqOf(&seq);
1169178825Sdfr    ret = 0;
1170178825Sdfr
1171178825Sdfrout:
1172178825Sdfr
1173178825Sdfr    return ret;
1174178825Sdfr}
1175178825Sdfr
1176178825Sdfr#define test_seq_of(type, ok, ptr)					\
1177178825Sdfr{									\
1178178825Sdfr    heim_octet_string os;						\
1179178825Sdfr    size_t size;							\
1180178825Sdfr    type decode;							\
1181178825Sdfr    ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret);	\
1182178825Sdfr    if (ret)								\
1183178825Sdfr	return ret;							\
1184178825Sdfr    if (os.length != size)						\
1185178825Sdfr	abort();							\
1186178825Sdfr    ret = decode_##type(os.data, os.length, &decode, &size);		\
1187178825Sdfr    free(os.data);							\
1188178825Sdfr    if (ret) {								\
1189178825Sdfr	if (ok)								\
1190178825Sdfr	    return 1;							\
1191178825Sdfr    } else {								\
1192178825Sdfr	free_##type(&decode);						\
1193178825Sdfr	if (!ok)							\
1194178825Sdfr	    return 1;							\
1195178825Sdfr	if (size != 0)							\
1196178825Sdfr            return 1;							\
1197178825Sdfr    }									\
1198178825Sdfr    return 0;								\
1199178825Sdfr}
1200178825Sdfr
1201178825Sdfrstatic int
1202178825Sdfrcheck_seq_of_size(void)
1203178825Sdfr{
1204233294Sstas#if 0 /* template */
1205178825Sdfr    TESTInteger integers[4] = { 1, 2, 3, 4 };
1206178825Sdfr    int ret;
1207178825Sdfr
1208178825Sdfr    {
1209178825Sdfr	TESTSeqSizeOf1 ssof1f1 = { 1, integers };
1210178825Sdfr	TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
1211178825Sdfr	TESTSeqSizeOf1 ssof1f2 = { 3, integers };
1212178825Sdfr
1213178825Sdfr	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
1214178825Sdfr	test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
1215178825Sdfr	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
1216178825Sdfr    }
1217178825Sdfr    {
1218178825Sdfr	TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
1219178825Sdfr	TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
1220178825Sdfr	TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
1221178825Sdfr	TESTSeqSizeOf2 ssof2f2 = { 3, integers };
1222233294Sstas
1223178825Sdfr	test_seq_of(TESTSeqSizeOf2, 0, &ssof2f1);
1224178825Sdfr	test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok1);
1225178825Sdfr	test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok2);
1226178825Sdfr	test_seq_of(TESTSeqSizeOf2, 0, &ssof2f2);
1227178825Sdfr    }
1228178825Sdfr    {
1229178825Sdfr	TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
1230178825Sdfr	TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
1231178825Sdfr	TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
1232233294Sstas
1233178825Sdfr	test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
1234178825Sdfr	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
1235178825Sdfr	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
1236178825Sdfr    }
1237178825Sdfr    {
1238178825Sdfr	TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
1239178825Sdfr	TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
1240178825Sdfr	TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
1241178825Sdfr	TESTSeqSizeOf4 ssof4f1  = { 3, integers };
1242233294Sstas
1243178825Sdfr	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok1);
1244233294Sstas	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok2);
1245178825Sdfr	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok3);
1246178825Sdfr	test_seq_of(TESTSeqSizeOf4, 0, &ssof4f1);
1247178825Sdfr   }
1248233294Sstas#endif
1249178825Sdfr    return 0;
1250178825Sdfr}
1251178825Sdfr
1252233294Sstasstatic int
1253233294Sstascheck_TESTMechTypeList(void)
1254233294Sstas{
1255233294Sstas    TESTMechTypeList tl;
1256233294Sstas    unsigned oid1[] =  { 1, 2, 840, 48018, 1, 2, 2};
1257233294Sstas    unsigned oid2[] =  { 1, 2, 840, 113554, 1, 2, 2};
1258233294Sstas    unsigned oid3[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 30};
1259233294Sstas    unsigned oid4[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10};
1260233294Sstas    TESTMechType array[] = {{ 7, oid1 },
1261233294Sstas                            { 7, oid2 },
1262233294Sstas                            { 10, oid3 },
1263233294Sstas                            { 10, oid4 }};
1264233294Sstas    size_t size, len;
1265233294Sstas    void *ptr;
1266233294Sstas    int ret;
1267178825Sdfr
1268233294Sstas    tl.len = 4;
1269233294Sstas    tl.val = array;
1270178825Sdfr
1271233294Sstas    ASN1_MALLOC_ENCODE(TESTMechTypeList, ptr, len, &tl, &size, ret);
1272233294Sstas    if (ret)
1273233294Sstas	errx(1, "TESTMechTypeList: %d", ret);
1274233294Sstas    if (len != size)
1275233294Sstas	abort();
1276233294Sstas    return 0;
1277233294Sstas}
1278233294Sstas
1279120945Snectarint
1280120945Snectarmain(int argc, char **argv)
1281120945Snectar{
1282120945Snectar    int ret = 0;
1283120945Snectar
1284120945Snectar    ret += test_principal ();
1285120945Snectar    ret += test_authenticator();
1286178825Sdfr    ret += test_krb_error();
1287178825Sdfr    ret += test_Name();
1288178825Sdfr    ret += test_bit_string();
1289233294Sstas    ret += test_bit_string_rfc1510();
1290233294Sstas    ret += test_time();
1291233294Sstas    ret += test_cert();
1292120945Snectar
1293178825Sdfr    ret += check_tag_length();
1294178825Sdfr    ret += test_large_tag();
1295178825Sdfr    ret += test_choice();
1296178825Sdfr
1297178825Sdfr    ret += test_implicit();
1298178825Sdfr    ret += test_taglessalloc();
1299233294Sstas    ret += test_optional();
1300178825Sdfr
1301178825Sdfr    ret += check_fail_largetag();
1302178825Sdfr    ret += check_fail_sequence();
1303178825Sdfr    ret += check_fail_choice();
1304178825Sdfr
1305178825Sdfr    ret += check_seq();
1306178825Sdfr    ret += check_seq_of_size();
1307178825Sdfr
1308233294Sstas    ret += check_TESTMechTypeList();
1309233294Sstas
1310120945Snectar    return ret;
1311120945Snectar}
1312