2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
43 #include <asn1-common.h>
46 #include <test_asn1.h>
48 #include "check-common.h"
52 cmp_dummy (void *a, void *b)
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" }
71 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
72 TESTuint64 values[] = { 0, 8589934591LL, 8589934592LL,
73 36028797018963967LL, 36028797018963968LL,
74 9223372036854775807LL, 18446744073709551615ULL };
76 for (i = 0; i < ntests; i++)
77 tests[i].val = &values[i];
79 if (sizeof(TESTuint64) != sizeof(uint64_t)) {
81 printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
82 (int)sizeof(TESTuint64), (int)sizeof(uint64_t));
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,
98 struct test_case tests[] = {
103 "\x30\x07\x30\x05\xa0\x03\x02\x01\x00",
106 "\x30\x0e\x30\x05\xa0\x03\x02\x01\x00\x30\x05\xa0\x03\x02\x01\x01",
110 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
111 TESTSeqOfSeq c0, c1, c2;
112 struct TESTSeqOfSeq_val i[2];
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,
142 struct test_case tests[] = {
147 "\x30\x09\x30\x07\xa0\x05\x1b\x03\x65\x74\x74",
150 "\x30\x13\x30\x07\xa0\x05\x1b\x03\x65\x74\x74\x30\x08\xa0"
151 "\x06\x1b\x04\x74\x76\x61\x61",
155 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
156 TESTSeqOfSeq2 c0, c1, c2;
157 struct TESTSeqOfSeq2_val i[2];
160 i[1].string = "tvaa";
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,
187 struct test_case tests[] = {
192 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
195 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
199 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
200 TESTSeqOf2 c0, c1, c2;
201 heim_general_string i[2];
206 c0.strings.val = NULL;
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,
231 struct test_case tests[] = {
239 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
242 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
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];
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,
286 struct test_case tests[] = {
291 "\x30\x02" "\xa1\x00",
294 "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
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"
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"
310 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
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"
322 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
323 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
325 "\x04\x01\x00" /* 3 OCTET-STRING */
326 "\x02\x05\x01\x00\x00\x00\x00", /* 7 INT */
330 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
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];
342 tests[0].val = &c[0];
349 tests[1].val = &c[1];
358 tests[2].val = &c[2];
360 b1val[3].s1.data = "";
361 b1val[3].s1.length = 0;
363 b1val[3].s2.data = "\x01\x02";
364 b1val[3].s2.length = 2;
365 b1val[3].u2 = (TESTuint64)-1LL;
367 b2val[3].s1.data = "";
368 b2val[3].s1.length = 0;
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;
377 b3val[3].s1.data = "";
378 b3val[3].s1.length = 0;
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;
391 b1[3].val = &b1val[3];
393 b2[3].val = &b2val[3];
395 b3[3].val = &b3val[3];
399 tests[3].val = &c[3];
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,
412 cmp_test_seqof5 (void *a, void *b)
414 TESTSeqOf5 *aval = a;
415 TESTSeqOf5 *bval = b;
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);
441 struct test_case tests[] = {
442 { NULL, 2, "\x30\x00", "seq5 0" },
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" /* ... */
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"
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",
466 int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
468 struct TESTSeqOf5_outer outer;
469 struct TESTSeqOf5_outer_inner inner;
471 heim_octet_string s[8];
475 tests[0].val = &c[0];
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);
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];
489 tests[1].val = &c[1];
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,
499 for (i = 0; i < 8; ++i)
506 main(int argc, char **argv)
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();