Store the canonical client princ in ipropd-slave
[heimdal.git] / lib / asn1 / check-template.c
blob33d66f4659b33ba0f86d7735cdbda55f41345d72
1 /*
2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include <config.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <err.h>
41 #include <roken.h>
43 #include <asn1-common.h>
44 #include <asn1_err.h>
45 #include <der.h>
46 #include <test_asn1.h>
48 #include "check-common.h"
50 static int
51 cmp_dummy (void *a, void *b)
53 return 0;
56 static int
57 test_uint64(void)
59 struct test_case tests[] = {
60 { NULL, 3, "\x02\x01\x00", "uint64 0" },
61 { NULL, 7, "\x02\x05\x01\xff\xff\xff\xff", "uint64 1" },
62 { NULL, 7, "\x02\x05\x02\x00\x00\x00\x00", "uint64 2" },
63 { NULL, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff", "uint64 3" },
64 { NULL, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00", "uint64 4" },
65 { NULL, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", "uint64 5" },
66 { NULL, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", "uint64 6" }
69 size_t i;
70 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
71 TESTuint64 values[] = { 0, 8589934591LL, 8589934592LL,
72 36028797018963967LL, 36028797018963968LL,
73 9223372036854775807LL, 18446744073709551615ULL };
75 for (i = 0; i < ntests; i++)
76 tests[i].val = &values[i];
78 if (sizeof(TESTuint64) != sizeof(uint64_t)) {
79 ret += 1;
80 printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
81 (int)sizeof(TESTuint64), (int)sizeof(uint64_t));
84 ret += generic_test (tests, ntests, sizeof(TESTuint64),
85 (generic_encode)encode_TESTuint64,
86 (generic_length)length_TESTuint64,
87 (generic_decode)decode_TESTuint64,
88 (generic_free)free_TESTuint64,
89 cmp_dummy,
90 NULL);
91 return ret;
94 static int
95 test_seqofseq(void)
97 struct test_case tests[] = {
98 { NULL, 2,
99 "\x30\x00",
100 "seqofseq 0" },
101 { NULL, 9,
102 "\x30\x07\x30\x05\xa0\x03\x02\x01\x00",
103 "seqofseq 1" },
104 { NULL, 16,
105 "\x30\x0e\x30\x05\xa0\x03\x02\x01\x00\x30\x05\xa0\x03\x02\x01\x01",
106 "seqofseq 2" }
109 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
110 TESTSeqOfSeq c0, c1, c2;
111 struct TESTSeqOfSeq_val i[2];
113 i[0].zero = 0;
114 i[1].zero = 1;
116 c0.len = 0;
117 c0.val = NULL;
118 tests[0].val = &c0;
120 c1.len = 1;
121 c1.val = i;
122 tests[1].val = &c1;
124 c2.len = 2;
125 c2.val = i;
126 tests[2].val = &c2;
128 ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq),
129 (generic_encode)encode_TESTSeqOfSeq,
130 (generic_length)length_TESTSeqOfSeq,
131 (generic_decode)decode_TESTSeqOfSeq,
132 (generic_free)free_TESTSeqOfSeq,
133 cmp_dummy,
134 NULL);
135 return ret;
138 static int
139 test_seqofseq2(void)
141 struct test_case tests[] = {
142 { NULL, 2,
143 "\x30\x00",
144 "seqofseq2 0" },
145 { NULL, 11,
146 "\x30\x09\x30\x07\xa0\x05\x1b\x03\x65\x74\x74",
147 "seqofseq2 1" },
148 { NULL, 21,
149 "\x30\x13\x30\x07\xa0\x05\x1b\x03\x65\x74\x74\x30\x08\xa0"
150 "\x06\x1b\x04\x74\x76\x61\x61",
151 "seqofseq2 2" }
154 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
155 TESTSeqOfSeq2 c0, c1, c2;
156 struct TESTSeqOfSeq2_val i[2];
158 i[0].string = "ett";
159 i[1].string = "tvaa";
161 c0.len = 0;
162 c0.val = NULL;
163 tests[0].val = &c0;
165 c1.len = 1;
166 c1.val = i;
167 tests[1].val = &c1;
169 c2.len = 2;
170 c2.val = i;
171 tests[2].val = &c2;
173 ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq2),
174 (generic_encode)encode_TESTSeqOfSeq2,
175 (generic_length)length_TESTSeqOfSeq2,
176 (generic_decode)decode_TESTSeqOfSeq2,
177 (generic_free)free_TESTSeqOfSeq2,
178 cmp_dummy,
179 NULL);
180 return ret;
183 static int
184 test_seqof2(void)
186 struct test_case tests[] = {
187 { NULL, 4,
188 "\x30\x02\x30\x00",
189 "seqof2 1" },
190 { NULL, 9,
191 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
192 "seqof2 2" },
193 { NULL, 14,
194 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
195 "seqof2 3" }
198 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
199 TESTSeqOf2 c0, c1, c2;
200 heim_general_string i[2];
202 i[0] = "foo";
203 i[1] = "bar";
205 c0.strings.val = NULL;
206 c0.strings.len = 0;
207 tests[0].val = &c0;
209 c1.strings.len = 1;
210 c1.strings.val = i;
211 tests[1].val = &c1;
213 c2.strings.len = 2;
214 c2.strings.val = i;
215 tests[2].val = &c2;
217 ret += generic_test (tests, ntests, sizeof(TESTSeqOf2),
218 (generic_encode)encode_TESTSeqOf2,
219 (generic_length)length_TESTSeqOf2,
220 (generic_decode)decode_TESTSeqOf2,
221 (generic_free)free_TESTSeqOf2,
222 cmp_dummy,
223 NULL);
224 return ret;
227 static int
228 test_seqof3(void)
230 struct test_case tests[] = {
231 { NULL, 2,
232 "\x30\x00",
233 "seqof3 0" },
234 { NULL, 4,
235 "\x30\x02\x30\x00",
236 "seqof3 1" },
237 { NULL, 9,
238 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
239 "seqof3 2" },
240 { NULL, 14,
241 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
242 "seqof3 3" }
245 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
246 TESTSeqOf3 c0, c1, c2, c3;
247 struct TESTSeqOf3_strings s1, s2, s3;
248 heim_general_string i[2];
250 i[0] = "foo";
251 i[1] = "bar";
253 c0.strings = NULL;
254 tests[0].val = &c0;
256 s1.val = NULL;
257 s1.len = 0;
258 c1.strings = &s1;
259 tests[1].val = &c1;
261 s2.len = 1;
262 s2.val = i;
263 c2.strings = &s2;
264 tests[2].val = &c2;
266 s3.len = 2;
267 s3.val = i;
268 c3.strings = &s3;
269 tests[3].val = &c3;
271 ret += generic_test (tests, ntests, sizeof(TESTSeqOf3),
272 (generic_encode)encode_TESTSeqOf3,
273 (generic_length)length_TESTSeqOf3,
274 (generic_decode)decode_TESTSeqOf3,
275 (generic_free)free_TESTSeqOf3,
276 cmp_dummy,
277 NULL);
278 return ret;
282 static int
283 test_seqof4(void)
285 struct test_case tests[] = {
286 { NULL, 2,
287 "\x30\x00",
288 "seq4 0" },
289 { NULL, 4,
290 "\x30\x02" "\xa1\x00",
291 "seq4 1" },
292 { NULL, 8,
293 "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
294 "seq4 2" },
295 { NULL, 2 + (2 + 0x18) + (2 + 0x27) + (2 + 0x31),
296 "\x30\x76" /* 2 SEQ */
297 "\xa0\x18\x30\x16" /* 4 [0] SEQ */
298 "\x30\x14" /* 2 SEQ */
299 "\x04\x00" /* 2 OCTET-STRING */
300 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
301 "\x02\x01\x01" /* 3 INT */
302 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
303 /* 11 INT */
304 "\xa1\x27" /* 2 [1] IMPL SEQ */
305 "\x30\x25" /* 2 SEQ */
306 "\x02\x01\x01" /* 3 INT */
307 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
308 /* 11 INT */
309 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
310 /* 11 INT */
311 "\x04\x00" /* 2 OCTET-STRING */
312 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
313 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
314 "\xa2\x31" /* 2 [2] IMPL SEQ */
315 "\x30\x2f" /* 2 SEQ */
316 "\x04\x00" /* 2 OCTET-STRING */
317 "\x02\x01\x01" /* 3 INT */
318 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
319 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
320 /* 11 INT */
321 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
322 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
323 /* 11 INT */
324 "\x04\x01\x00" /* 3 OCTET-STRING */
325 "\x02\x05\x01\x00\x00\x00\x00", /* 7 INT */
326 "seq4 3" },
329 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
330 TESTSeqOf4 c[4];
331 struct TESTSeqOf4_b1 b1[4];
332 struct TESTSeqOf4_b2 b2[4];
333 struct TESTSeqOf4_b3 b3[4];
334 struct TESTSeqOf4_b1_val b1val[4];
335 struct TESTSeqOf4_b2_val b2val[4];
336 struct TESTSeqOf4_b3_val b3val[4];
338 c[0].b1 = NULL;
339 c[0].b2 = NULL;
340 c[0].b3 = NULL;
341 tests[0].val = &c[0];
343 b2[1].len = 0;
344 b2[1].val = NULL;
345 c[1].b1 = NULL;
346 c[1].b2 = &b2[1];
347 c[1].b3 = NULL;
348 tests[1].val = &c[1];
350 b1[2].len = 0;
351 b1[2].val = NULL;
352 b2[2].len = 0;
353 b2[2].val = NULL;
354 c[2].b1 = &b1[2];
355 c[2].b2 = &b2[2];
356 c[2].b3 = NULL;
357 tests[2].val = &c[2];
359 b1val[3].s1.data = "";
360 b1val[3].s1.length = 0;
361 b1val[3].u1 = 1LL;
362 b1val[3].s2.data = "\x01\x02";
363 b1val[3].s2.length = 2;
364 b1val[3].u2 = -1LL;
366 b2val[3].s1.data = "";
367 b2val[3].s1.length = 0;
368 b2val[3].u1 = 1LL;
369 b2val[3].s2.data = "\x01\x02";
370 b2val[3].s2.length = 2;
371 b2val[3].u2 = -1LL;
372 b2val[3].s3.data = "\x00\x01\x02\x03";
373 b2val[3].s3.length = 4;
374 b2val[3].u3 = 1LL<<63;
376 b3val[3].s1.data = "";
377 b3val[3].s1.length = 0;
378 b3val[3].u1 = 1LL;
379 b3val[3].s2.data = "\x01\x02";
380 b3val[3].s2.length = 2;
381 b3val[3].u2 = -1LL;
382 b3val[3].s3.data = "\x00\x01\x02\x03";
383 b3val[3].s3.length = 4;
384 b3val[3].u3 = 1LL<<63;
385 b3val[3].s4.data = "\x00";
386 b3val[3].s4.length = 1;
387 b3val[3].u4 = 1LL<<32;
389 b1[3].len = 1;
390 b1[3].val = &b1val[3];
391 b2[3].len = 1;
392 b2[3].val = &b2val[3];
393 b3[3].len = 1;
394 b3[3].val = &b3val[3];
395 c[3].b1 = &b1[3];
396 c[3].b2 = &b2[3];
397 c[3].b3 = &b3[3];
398 tests[3].val = &c[3];
400 ret += generic_test (tests, ntests, sizeof(TESTSeqOf4),
401 (generic_encode)encode_TESTSeqOf4,
402 (generic_length)length_TESTSeqOf4,
403 (generic_decode)decode_TESTSeqOf4,
404 (generic_free)free_TESTSeqOf4,
405 cmp_dummy,
406 NULL);
407 return ret;
411 main(int argc, char **argv)
413 int ret = 0;
415 ret += test_uint64();
416 ret += test_seqofseq();
417 ret += test_seqofseq2();
418 ret += test_seqof2();
419 ret += test_seqof3();
420 ret += test_seqof4();
422 return ret;