s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / third_party / heimdal / lib / asn1 / check-template.c
1 /*
2  * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35
36 #include <config.h>
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <err.h>
41 #include <roken.h>
42
43 #include <asn1-common.h>
44 #include <asn1_err.h>
45 #include <der.h>
46 #include <test_asn1.h>
47
48 #include "check-common.h"
49 #include "der_locl.h"
50
51 static int
52 cmp_dummy (void *a, void *b)
53 {
54     return 0;
55 }
56
57 static int
58 test_uint64(void)
59 {
60     struct test_case tests[] = {
61         { NULL, 3, "\x02\x01\x00", "uint64 0" },
62         { NULL, 7, "\x02\x05\x01\xff\xff\xff\xff", "uint64 1" },
63         { NULL, 7, "\x02\x05\x02\x00\x00\x00\x00", "uint64 2" },
64         { NULL, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff", "uint64 3" },
65         { NULL, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00", "uint64 4" },
66         { NULL, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", "uint64 5" },
67         { NULL, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", "uint64 6" }
68     };
69
70     size_t i;
71     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
72     TESTuint64 values[] = { 0, 8589934591LL, 8589934592LL,
73                            36028797018963967LL, 36028797018963968LL,
74                            9223372036854775807LL, 18446744073709551615ULL };
75
76     for (i = 0; i < ntests; i++)
77        tests[i].val = &values[i];
78
79     if (sizeof(TESTuint64) != sizeof(uint64_t)) {
80        ret += 1;
81        printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
82               (int)sizeof(TESTuint64), (int)sizeof(uint64_t));
83     }
84
85     ret += generic_test (tests, ntests, sizeof(TESTuint64),
86                         (generic_encode)encode_TESTuint64,
87                         (generic_length)length_TESTuint64,
88                         (generic_decode)decode_TESTuint64,
89                         (generic_free)free_TESTuint64,
90                         cmp_dummy,
91                         NULL);
92     return ret;
93 }
94
95 static int
96 test_seqofseq(void)
97 {
98     struct test_case tests[] = {
99         { NULL,  2,
100           "\x30\x00",
101           "seqofseq 0" },
102         { NULL,  9,
103           "\x30\x07\x30\x05\xa0\x03\x02\x01\x00",
104           "seqofseq 1" },
105         { NULL,  16,
106           "\x30\x0e\x30\x05\xa0\x03\x02\x01\x00\x30\x05\xa0\x03\x02\x01\x01",
107           "seqofseq 2" }
108     };
109
110     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
111     TESTSeqOfSeq c0, c1, c2;
112     struct TESTSeqOfSeq_val i[2];
113
114     i[0].zero = 0;
115     i[1].zero = 1;
116
117     c0.len = 0;
118     c0.val = NULL;
119     tests[0].val = &c0;
120
121     c1.len = 1;
122     c1.val = i;
123     tests[1].val = &c1;
124
125     c2.len = 2;
126     c2.val = i;
127     tests[2].val = &c2;
128
129     ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq),
130                          (generic_encode)encode_TESTSeqOfSeq,
131                          (generic_length)length_TESTSeqOfSeq,
132                          (generic_decode)decode_TESTSeqOfSeq,
133                          (generic_free)free_TESTSeqOfSeq,
134                          cmp_dummy,
135                          NULL);
136     return ret;
137 }
138
139 static int
140 test_seqofseq2(void)
141 {
142     struct test_case tests[] = {
143         { NULL,  2,
144           "\x30\x00",
145           "seqofseq2 0" },
146         { NULL,  11,
147           "\x30\x09\x30\x07\xa0\x05\x1b\x03\x65\x74\x74",
148           "seqofseq2 1" },
149         { NULL,  21,
150           "\x30\x13\x30\x07\xa0\x05\x1b\x03\x65\x74\x74\x30\x08\xa0"
151           "\x06\x1b\x04\x74\x76\x61\x61",
152           "seqofseq2 2" }
153     };
154
155     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
156     TESTSeqOfSeq2 c0, c1, c2;
157     struct TESTSeqOfSeq2_val i[2];
158
159     i[0].string = "ett";
160     i[1].string = "tvaa";
161
162     c0.len = 0;
163     c0.val = NULL;
164     tests[0].val = &c0;
165
166     c1.len = 1;
167     c1.val = i;
168     tests[1].val = &c1;
169
170     c2.len = 2;
171     c2.val = i;
172     tests[2].val = &c2;
173
174     ret += generic_test (tests, ntests, sizeof(TESTSeqOfSeq2),
175                          (generic_encode)encode_TESTSeqOfSeq2,
176                          (generic_length)length_TESTSeqOfSeq2,
177                          (generic_decode)decode_TESTSeqOfSeq2,
178                          (generic_free)free_TESTSeqOfSeq2,
179                          cmp_dummy,
180                          NULL);
181     return ret;
182 }
183
184 static int
185 test_seqof2(void)
186 {
187     struct test_case tests[] = {
188         { NULL,  4,
189           "\x30\x02\x30\x00",
190           "seqof2 1" },
191         { NULL,  9,
192           "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
193           "seqof2 2" },
194         { NULL,  14,
195           "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
196           "seqof2 3" }
197     };
198
199     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
200     TESTSeqOf2 c0, c1, c2;
201     heim_general_string i[2];
202
203     i[0] = "foo";
204     i[1] = "bar";
205
206     c0.strings.val = NULL;
207     c0.strings.len = 0;
208     tests[0].val = &c0;
209
210     c1.strings.len = 1;
211     c1.strings.val = i;
212     tests[1].val = &c1;
213
214     c2.strings.len = 2;
215     c2.strings.val = i;
216     tests[2].val = &c2;
217
218     ret += generic_test (tests, ntests, sizeof(TESTSeqOf2),
219                          (generic_encode)encode_TESTSeqOf2,
220                          (generic_length)length_TESTSeqOf2,
221                          (generic_decode)decode_TESTSeqOf2,
222                          (generic_free)free_TESTSeqOf2,
223                          cmp_dummy,
224                          NULL);
225     return ret;
226 }
227
228 static int
229 test_seqof3(void)
230 {
231     struct test_case tests[] = {
232         { NULL,  2,
233           "\x30\x00",
234           "seqof3 0" },
235         { NULL,  4,
236           "\x30\x02\x30\x00",
237           "seqof3 1" },
238         { NULL,  9,
239           "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
240           "seqof3 2" },
241         { NULL,  14,
242           "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
243           "seqof3 3" }
244     };
245
246     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
247     TESTSeqOf3 c0, c1, c2, c3;
248     struct TESTSeqOf3_strings s1, s2, s3;
249     heim_general_string i[2];
250
251     i[0] = "foo";
252     i[1] = "bar";
253
254     c0.strings = NULL;
255     tests[0].val = &c0;
256
257     s1.val = NULL;
258     s1.len = 0;
259     c1.strings = &s1;
260     tests[1].val = &c1;
261
262     s2.len = 1;
263     s2.val = i;
264     c2.strings = &s2;
265     tests[2].val = &c2;
266
267     s3.len = 2;
268     s3.val = i;
269     c3.strings = &s3;
270     tests[3].val = &c3;
271
272     ret += generic_test (tests, ntests, sizeof(TESTSeqOf3),
273                          (generic_encode)encode_TESTSeqOf3,
274                          (generic_length)length_TESTSeqOf3,
275                          (generic_decode)decode_TESTSeqOf3,
276                          (generic_free)free_TESTSeqOf3,
277                          cmp_dummy,
278                          NULL);
279     return ret;
280 }
281
282
283 static int
284 test_seqof4(void)
285 {
286     struct test_case tests[] = {
287         { NULL,  2,
288           "\x30\x00",
289           "seq4 0" },
290         { NULL,  4,
291           "\x30\x02" "\xa1\x00",
292           "seq4 1" },
293         { NULL,  8,
294           "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
295           "seq4 2" },
296         { NULL,  2 + (2 + 0x18) + (2 + 0x27) + (2 + 0x31),
297           "\x30\x76"                                    /* 2 SEQ */
298            "\xa0\x18\x30\x16"                           /* 4 [0] SEQ */
299             "\x30\x14"                                  /* 2 SEQ */
300              "\x04\x00"                                 /* 2 OCTET-STRING */
301              "\x04\x02\x01\x02"                         /* 4 OCTET-STRING */
302              "\x02\x01\x01"                             /* 3 INT */
303              "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
304                                                         /* 11 INT */
305            "\xa1\x27"                                   /* 2 [1] IMPL SEQ */
306             "\x30\x25"                                  /* 2 SEQ */
307              "\x02\x01\x01"                             /* 3 INT */
308              "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
309                                                         /* 11 INT */
310              "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
311                                                         /* 11 INT */
312              "\x04\x00"                                 /* 2 OCTET-STRING */
313              "\x04\x02\x01\x02"                         /* 4 OCTET-STRING */
314              "\x04\x04\x00\x01\x02\x03"                 /* 6 OCTET-STRING */
315            "\xa2\x31"                                   /* 2 [2] IMPL SEQ */
316             "\x30\x2f"                                  /* 2 SEQ */
317              "\x04\x00"                                 /* 2 OCTET-STRING */
318              "\x02\x01\x01"                             /* 3 INT */
319              "\x04\x02\x01\x02"                         /* 4 OCTET-STRING */
320              "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
321                                                         /* 11 INT */
322              "\x04\x04\x00\x01\x02\x03"                 /* 6 OCTET-STRING */
323              "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
324                                                         /* 11 INT */
325              "\x04\x01\x00"                             /* 3 OCTET-STRING */
326              "\x02\x05\x01\x00\x00\x00\x00",            /* 7 INT */
327           "seq4 3" },
328     };
329
330     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
331     TESTSeqOf4 c[4];
332     struct TESTSeqOf4_b1 b1[4];
333     struct TESTSeqOf4_b2 b2[4];
334     struct TESTSeqOf4_b3 b3[4];
335     struct TESTSeqOf4_b1_val b1val[4];
336     struct TESTSeqOf4_b2_val b2val[4];
337     struct TESTSeqOf4_b3_val b3val[4];
338
339     c[0].b1 = NULL;
340     c[0].b2 = NULL;
341     c[0].b3 = NULL;
342     tests[0].val = &c[0];
343
344     b2[1].len = 0;
345     b2[1].val = NULL;
346     c[1].b1 = NULL;
347     c[1].b2 = &b2[1];
348     c[1].b3 = NULL;
349     tests[1].val = &c[1];
350
351     b1[2].len = 0;
352     b1[2].val = NULL;
353     b2[2].len = 0;
354     b2[2].val = NULL;
355     c[2].b1 = &b1[2];
356     c[2].b2 = &b2[2];
357     c[2].b3 = NULL;
358     tests[2].val = &c[2];
359
360     b1val[3].s1.data = "";
361     b1val[3].s1.length = 0;
362     b1val[3].u1 = 1LL;
363     b1val[3].s2.data = "\x01\x02";
364     b1val[3].s2.length = 2;
365     b1val[3].u2 = (TESTuint64)-1LL;
366
367     b2val[3].s1.data = "";
368     b2val[3].s1.length = 0;
369     b2val[3].u1 = 1LL;
370     b2val[3].s2.data = "\x01\x02";
371     b2val[3].s2.length = 2;
372     b2val[3].u2 = (TESTuint64)-1LL;
373     b2val[3].s3.data = "\x00\x01\x02\x03";
374     b2val[3].s3.length = 4;
375     b2val[3].u3 = 1ULL<<63;
376
377     b3val[3].s1.data = "";
378     b3val[3].s1.length = 0;
379     b3val[3].u1 = 1LL;
380     b3val[3].s2.data = "\x01\x02";
381     b3val[3].s2.length = 2;
382     b3val[3].u2 = (TESTuint64)-1LL;
383     b3val[3].s3.data = "\x00\x01\x02\x03";
384     b3val[3].s3.length = 4;
385     b3val[3].u3 = 1ULL<<63;
386     b3val[3].s4.data = "\x00";
387     b3val[3].s4.length = 1;
388     b3val[3].u4 = 1LL<<32;
389
390     b1[3].len = 1;
391     b1[3].val = &b1val[3];
392     b2[3].len = 1;
393     b2[3].val = &b2val[3];
394     b3[3].len = 1;
395     b3[3].val = &b3val[3];
396     c[3].b1 = &b1[3];
397     c[3].b2 = &b2[3];
398     c[3].b3 = &b3[3];
399     tests[3].val = &c[3];
400
401     ret += generic_test (tests, ntests, sizeof(TESTSeqOf4),
402                          (generic_encode)encode_TESTSeqOf4,
403                          (generic_length)length_TESTSeqOf4,
404                          (generic_decode)decode_TESTSeqOf4,
405                          (generic_free)free_TESTSeqOf4,
406                          cmp_dummy,
407                          NULL);
408     return ret;
409 }
410
411 static int
412 cmp_test_seqof5 (void *a, void *b)
413 {
414     TESTSeqOf5 *aval = a;
415     TESTSeqOf5 *bval = b;
416
417     IF_OPT_COMPARE(aval, bval, outer) {
418             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u0);
419             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s0);
420             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u1);
421             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s1);
422             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u2);
423             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s2);
424             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u3);
425             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s3);
426             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u4);
427             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s4);
428             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u5);
429             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s5);
430             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u6);
431             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s6);
432             COMPARE_INTEGER(&aval->outer->inner, &bval->outer->inner, u7);
433             COMPARE_OCTET_STRING(&aval->outer->inner, &bval->outer->inner, s7);
434     }
435     return 0;
436 }
437
438 static int
439 test_seqof5(void)
440 {
441     struct test_case tests[] = {
442         { NULL,  2, "\x30\x00", "seq5 0" },
443         { NULL,  126,
444           "\x30\x7c"                                            /* SEQ */
445             "\x30\x7a"                                          /* SEQ */
446               "\x30\x78"                                        /* SEQ */
447                 "\x02\x01\x01"                                  /* INT 1 */
448                 "\x04\x06\x01\x01\x01\x01\x01\x01"              /* "\0x1"x6 */
449                 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfe"  /* INT ~1 */
450                 "\x04\x06\x02\x02\x02\x02\x02\x02"              /* "\x02"x6 */
451                 "\x02\x01\x02"                                  /* INT 2 */
452                 "\x04\x06\x03\x03\x03\x03\x03\x03"              /* "\x03"x6 */
453                 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfd"  /* INT ~2 */
454                 "\x04\x06\x04\x04\x04\x04\x04\x04"              /* ... */
455                 "\x02\x01\x03"
456                 "\x04\x06\x05\x05\x05\x05\x05\x05"
457                 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfc"
458                 "\x04\x06\x06\x06\x06\x06\x06\x06"
459                 "\x02\x01\x04"
460                 "\x04\x06\x07\x07\x07\x07\x07\x07"
461                 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfb"
462                 "\x04\x06\x08\x08\x08\x08\x08\x08",
463           "seq5 1" },
464     };
465
466     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
467     TESTSeqOf5 c[2];
468     struct TESTSeqOf5_outer outer;
469     struct TESTSeqOf5_outer_inner inner;
470     TESTuint64 u[8];
471     heim_octet_string s[8];
472     int i;
473
474     c[0].outer = NULL;
475     tests[0].val = &c[0];
476
477     for (i = 0; i < 8; ++i) {
478         u[i] = (i&1) == 0 ? i/2+1 : ~(i/2+1);
479         s[i].data = memset(malloc(s[i].length = 6), i+1, 6);
480     }
481
482     inner.u0 = u[0]; inner.u1 = u[1]; inner.u2 = u[2]; inner.u3 = u[3];
483     inner.u4 = u[4]; inner.u5 = u[5]; inner.u6 = u[6]; inner.u7 = u[7];
484     inner.s0 = s[0]; inner.s1 = s[1]; inner.s2 = s[2]; inner.s3 = s[3];
485     inner.s4 = s[4]; inner.s5 = s[5]; inner.s6 = s[6]; inner.s7 = s[7];
486
487     outer.inner = inner;
488     c[1].outer = &outer;
489     tests[1].val = &c[1];
490
491     ret += generic_test (tests, ntests, sizeof(TESTSeqOf5),
492                          (generic_encode)encode_TESTSeqOf5,
493                          (generic_length)length_TESTSeqOf5,
494                          (generic_decode)decode_TESTSeqOf5,
495                          (generic_free)free_TESTSeqOf5,
496                          cmp_test_seqof5,
497                          NULL);
498
499     for (i = 0; i < 8; ++i)
500         free(s[i].data);
501
502     return ret;
503 }
504
505 int
506 main(int argc, char **argv)
507 {
508     int ret = 0;
509
510     ret += test_uint64();
511     ret += test_seqofseq();
512     ret += test_seqofseq2();
513     ret += test_seqof2();
514     ret += test_seqof3();
515     ret += test_seqof4();
516     ret += test_seqof5();
517
518     return ret;
519 }