fix memset
[heimdal.git] / lib / asn1 / check-template.c
blob02af09c0970877a81773119e39d9ff155e0455a9
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;
281 static int
282 test_seqof4(void)
284 struct test_case tests[] = {
285 { NULL, 2,
286 "\x30\x00",
287 "seq4 0" },
288 { NULL, 4,
289 "\x30\x02" "\xa1\x00",
290 "seq4 1" },
291 { NULL, 8,
292 "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
293 "seq4 2" },
294 { NULL, 2 + (2 + 0x18) + (2 + 0x27) + (2 + 0x31),
295 "\x30\x76" /* 2 SEQ */
296 "\xa0\x18\x30\x16" /* 4 [0] SEQ */
297 "\x30\x14" /* 2 SEQ */
298 "\x04\x00" /* 2 OCTET-STRING */
299 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
300 "\x02\x01\x01" /* 3 INT */
301 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
302 /* 11 INT */
303 "\xa1\x27" /* 2 [1] IMPL SEQ */
304 "\x30\x25" /* 2 SEQ */
305 "\x02\x01\x01" /* 3 INT */
306 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
307 /* 11 INT */
308 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
309 /* 11 INT */
310 "\x04\x00" /* 2 OCTET-STRING */
311 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
312 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
313 "\xa2\x31" /* 2 [2] IMPL SEQ */
314 "\x30\x2f" /* 2 SEQ */
315 "\x04\x00" /* 2 OCTET-STRING */
316 "\x02\x01\x01" /* 3 INT */
317 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
318 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
319 /* 11 INT */
320 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
321 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
322 /* 11 INT */
323 "\x04\x01\x00" /* 3 OCTET-STRING */
324 "\x02\x05\x01\x00\x00\x00\x00", /* 7 INT */
325 "seq4 3" },
328 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
329 TESTSeqOf4 c[4];
330 struct TESTSeqOf4_b1 b1[4];
331 struct TESTSeqOf4_b2 b2[4];
332 struct TESTSeqOf4_b3 b3[4];
333 struct TESTSeqOf4_b1_val b1val[4];
334 struct TESTSeqOf4_b2_val b2val[4];
335 struct TESTSeqOf4_b3_val b3val[4];
337 c[0].b1 = NULL;
338 c[0].b2 = NULL;
339 c[0].b3 = NULL;
340 tests[0].val = &c[0];
342 b2[1].len = 0;
343 b2[1].val = NULL;
344 c[1].b1 = NULL;
345 c[1].b2 = &b2[1];
346 c[1].b3 = NULL;
347 tests[1].val = &c[1];
349 b1[2].len = 0;
350 b1[2].val = NULL;
351 b2[2].len = 0;
352 b2[2].val = NULL;
353 c[2].b1 = &b1[2];
354 c[2].b2 = &b2[2];
355 c[2].b3 = NULL;
356 tests[2].val = &c[2];
358 b1val[3].s1.data = "";
359 b1val[3].s1.length = 0;
360 b1val[3].u1 = 1LL;
361 b1val[3].s2.data = "\x01\x02";
362 b1val[3].s2.length = 2;
363 b1val[3].u2 = -1LL;
365 b2val[3].s1.data = "";
366 b2val[3].s1.length = 0;
367 b2val[3].u1 = 1LL;
368 b2val[3].s2.data = "\x01\x02";
369 b2val[3].s2.length = 2;
370 b2val[3].u2 = -1LL;
371 b2val[3].s3.data = "\x00\x01\x02\x03";
372 b2val[3].s3.length = 4;
373 b2val[3].u3 = 1LL<<63;
375 b3val[3].s1.data = "";
376 b3val[3].s1.length = 0;
377 b3val[3].u1 = 1LL;
378 b3val[3].s2.data = "\x01\x02";
379 b3val[3].s2.length = 2;
380 b3val[3].u2 = -1LL;
381 b3val[3].s3.data = "\x00\x01\x02\x03";
382 b3val[3].s3.length = 4;
383 b3val[3].u3 = 1LL<<63;
384 b3val[3].s4.data = "\x00";
385 b3val[3].s4.length = 1;
386 b3val[3].u4 = 1LL<<32;
388 b1[3].len = 1;
389 b1[3].val = &b1val[3];
390 b2[3].len = 1;
391 b2[3].val = &b2val[3];
392 b3[3].len = 1;
393 b3[3].val = &b3val[3];
394 c[3].b1 = &b1[3];
395 c[3].b2 = &b2[3];
396 c[3].b3 = &b3[3];
397 tests[3].val = &c[3];
399 ret += generic_test (tests, ntests, sizeof(TESTSeqOf4),
400 (generic_encode)encode_TESTSeqOf4,
401 (generic_length)length_TESTSeqOf4,
402 (generic_decode)decode_TESTSeqOf4,
403 (generic_free)free_TESTSeqOf4,
404 cmp_dummy,
405 NULL);
406 return ret;
410 main(int argc, char **argv)
412 int ret = 0;
414 ret += test_uint64();
415 ret += test_seqofseq();
416 ret += test_seqofseq2();
417 ret += test_seqof2();
418 ret += test_seqof3();
419 ret += test_seqof4();
421 return ret;