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 my_copy_vers(const my_vers
*from
, my_vers
*to
)
59 my_free_vers(my_vers
*v
)
65 cmp_dummy (void *a
, void *b
)
73 struct test_case tests
[] = {
74 { NULL
, 3, "\x02\x01\x00", "uint64 0" },
75 { NULL
, 7, "\x02\x05\x01\xff\xff\xff\xff", "uint64 1" },
76 { NULL
, 7, "\x02\x05\x02\x00\x00\x00\x00", "uint64 2" },
77 { NULL
, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff", "uint64 3" },
78 { NULL
, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00", "uint64 4" },
79 { NULL
, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", "uint64 5" },
80 { NULL
, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", "uint64 6" }
84 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
85 TESTuint64 values
[] = { 0, 8589934591LL, 8589934592LL,
86 36028797018963967LL, 36028797018963968LL,
87 9223372036854775807LL, 18446744073709551615ULL };
89 for (i
= 0; i
< ntests
; i
++)
90 tests
[i
].val
= &values
[i
];
92 if (sizeof(TESTuint64
) != sizeof(uint64_t)) {
94 printf("sizeof(TESTuint64) %d != sizeof(uint64_t) %d\n",
95 (int)sizeof(TESTuint64
), (int)sizeof(uint64_t));
98 ret
+= generic_test (tests
, ntests
, sizeof(TESTuint64
),
99 (generic_encode
)encode_TESTuint64
,
100 (generic_length
)length_TESTuint64
,
101 (generic_decode
)decode_TESTuint64
,
102 (generic_free
)free_TESTuint64
,
111 struct test_case tests
[] = {
116 "\x30\x07\x30\x05\xa0\x03\x02\x01\x00",
119 "\x30\x0e\x30\x05\xa0\x03\x02\x01\x00\x30\x05\xa0\x03\x02\x01\x01",
123 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
124 TESTSeqOfSeq c0
, c1
, c2
;
125 struct TESTSeqOfSeq_val i
[2];
142 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOfSeq
),
143 (generic_encode
)encode_TESTSeqOfSeq
,
144 (generic_length
)length_TESTSeqOfSeq
,
145 (generic_decode
)decode_TESTSeqOfSeq
,
146 (generic_free
)free_TESTSeqOfSeq
,
155 struct test_case tests
[] = {
160 "\x30\x09\x30\x07\xa0\x05\x1b\x03\x65\x74\x74",
163 "\x30\x13\x30\x07\xa0\x05\x1b\x03\x65\x74\x74\x30\x08\xa0"
164 "\x06\x1b\x04\x74\x76\x61\x61",
168 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
169 TESTSeqOfSeq2 c0
, c1
, c2
;
170 struct TESTSeqOfSeq2_val i
[2];
173 i
[1].string
= "tvaa";
187 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOfSeq2
),
188 (generic_encode
)encode_TESTSeqOfSeq2
,
189 (generic_length
)length_TESTSeqOfSeq2
,
190 (generic_decode
)decode_TESTSeqOfSeq2
,
191 (generic_free
)free_TESTSeqOfSeq2
,
200 struct test_case tests
[] = {
205 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
208 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
212 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
213 TESTSeqOf2 c0
, c1
, c2
;
214 heim_general_string i
[2];
219 c0
.strings
.val
= NULL
;
231 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOf2
),
232 (generic_encode
)encode_TESTSeqOf2
,
233 (generic_length
)length_TESTSeqOf2
,
234 (generic_decode
)decode_TESTSeqOf2
,
235 (generic_free
)free_TESTSeqOf2
,
244 struct test_case tests
[] = {
252 "\x30\x07\x30\x05\x1b\x03\x66\x6f\x6f",
255 "\x30\x0c\x30\x0a\x1b\x03\x66\x6f\x6f\x1b\x03\x62\x61\x72",
259 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
260 TESTSeqOf3 c0
, c1
, c2
, c3
;
261 struct TESTSeqOf3_strings s1
, s2
, s3
;
262 heim_general_string i
[2];
285 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOf3
),
286 (generic_encode
)encode_TESTSeqOf3
,
287 (generic_length
)length_TESTSeqOf3
,
288 (generic_decode
)decode_TESTSeqOf3
,
289 (generic_free
)free_TESTSeqOf3
,
299 struct test_case tests
[] = {
304 "\x30\x02" "\xa1\x00",
307 "\x30\x06" "\xa0\x02\x30\x00" "\xa1\x00",
309 { NULL
, 2 + (2 + 0x18) + (2 + 0x27) + (2 + 0x31),
310 "\x30\x76" /* 2 SEQ */
311 "\xa0\x18\x30\x16" /* 4 [0] SEQ */
312 "\x30\x14" /* 2 SEQ */
313 "\x04\x00" /* 2 OCTET-STRING */
314 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
315 "\x02\x01\x01" /* 3 INT */
316 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
318 "\xa1\x27" /* 2 [1] IMPL SEQ */
319 "\x30\x25" /* 2 SEQ */
320 "\x02\x01\x01" /* 3 INT */
321 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
323 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
325 "\x04\x00" /* 2 OCTET-STRING */
326 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
327 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
328 "\xa2\x31" /* 2 [2] IMPL SEQ */
329 "\x30\x2f" /* 2 SEQ */
330 "\x04\x00" /* 2 OCTET-STRING */
331 "\x02\x01\x01" /* 3 INT */
332 "\x04\x02\x01\x02" /* 4 OCTET-STRING */
333 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"
335 "\x04\x04\x00\x01\x02\x03" /* 6 OCTET-STRING */
336 "\x02\x09\x00\x80\x00\x00\x00\x00\x00\x00\x00"
338 "\x04\x01\x00" /* 3 OCTET-STRING */
339 "\x02\x05\x01\x00\x00\x00\x00", /* 7 INT */
343 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
345 struct TESTSeqOf4_b1 b1
[4];
346 struct TESTSeqOf4_b2 b2
[4];
347 struct TESTSeqOf4_b3 b3
[4];
348 struct TESTSeqOf4_b1_val b1val
[4];
349 struct TESTSeqOf4_b2_val b2val
[4];
350 struct TESTSeqOf4_b3_val b3val
[4];
355 tests
[0].val
= &c
[0];
362 tests
[1].val
= &c
[1];
371 tests
[2].val
= &c
[2];
373 b1val
[3].s1
.data
= "";
374 b1val
[3].s1
.length
= 0;
376 b1val
[3].s2
.data
= "\x01\x02";
377 b1val
[3].s2
.length
= 2;
378 b1val
[3].u2
= (TESTuint64
)-1LL;
380 b2val
[3].s1
.data
= "";
381 b2val
[3].s1
.length
= 0;
383 b2val
[3].s2
.data
= "\x01\x02";
384 b2val
[3].s2
.length
= 2;
385 b2val
[3].u2
= (TESTuint64
)-1LL;
386 b2val
[3].s3
.data
= "\x00\x01\x02\x03";
387 b2val
[3].s3
.length
= 4;
388 b2val
[3].u3
= 1ULL<<63;
390 b3val
[3].s1
.data
= "";
391 b3val
[3].s1
.length
= 0;
393 b3val
[3].s2
.data
= "\x01\x02";
394 b3val
[3].s2
.length
= 2;
395 b3val
[3].u2
= (TESTuint64
)-1LL;
396 b3val
[3].s3
.data
= "\x00\x01\x02\x03";
397 b3val
[3].s3
.length
= 4;
398 b3val
[3].u3
= 1ULL<<63;
399 b3val
[3].s4
.data
= "\x00";
400 b3val
[3].s4
.length
= 1;
401 b3val
[3].u4
= 1LL<<32;
404 b1
[3].val
= &b1val
[3];
406 b2
[3].val
= &b2val
[3];
408 b3
[3].val
= &b3val
[3];
412 tests
[3].val
= &c
[3];
414 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOf4
),
415 (generic_encode
)encode_TESTSeqOf4
,
416 (generic_length
)length_TESTSeqOf4
,
417 (generic_decode
)decode_TESTSeqOf4
,
418 (generic_free
)free_TESTSeqOf4
,
425 cmp_test_seqof5 (void *a
, void *b
)
427 TESTSeqOf5
*aval
= a
;
428 TESTSeqOf5
*bval
= b
;
430 IF_OPT_COMPARE(aval
, bval
, outer
) {
431 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u0
);
432 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s0
);
433 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u1
);
434 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s1
);
435 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u2
);
436 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s2
);
437 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u3
);
438 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s3
);
439 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u4
);
440 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s4
);
441 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u5
);
442 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s5
);
443 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u6
);
444 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s6
);
445 COMPARE_INTEGER(&aval
->outer
->inner
, &bval
->outer
->inner
, u7
);
446 COMPARE_OCTET_STRING(&aval
->outer
->inner
, &bval
->outer
->inner
, s7
);
454 struct test_case tests
[] = {
455 { NULL
, 2, "\x30\x00", "seq5 0" },
460 "\x02\x01\x01" /* INT 1 */
461 "\x04\x06\x01\x01\x01\x01\x01\x01" /* "\0x1"x6 */
462 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfe" /* INT ~1 */
463 "\x04\x06\x02\x02\x02\x02\x02\x02" /* "\x02"x6 */
464 "\x02\x01\x02" /* INT 2 */
465 "\x04\x06\x03\x03\x03\x03\x03\x03" /* "\x03"x6 */
466 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfd" /* INT ~2 */
467 "\x04\x06\x04\x04\x04\x04\x04\x04" /* ... */
469 "\x04\x06\x05\x05\x05\x05\x05\x05"
470 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfc"
471 "\x04\x06\x06\x06\x06\x06\x06\x06"
473 "\x04\x06\x07\x07\x07\x07\x07\x07"
474 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xfb"
475 "\x04\x06\x08\x08\x08\x08\x08\x08",
479 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
481 struct TESTSeqOf5_outer outer
;
482 struct TESTSeqOf5_outer_inner inner
;
484 heim_octet_string s
[8];
488 tests
[0].val
= &c
[0];
490 for (i
= 0; i
< 8; ++i
) {
491 u
[i
] = (i
&1) == 0 ? i
/2+1 : ~(i
/2+1);
492 s
[i
].data
= memset(malloc(s
[i
].length
= 6), i
+1, 6);
495 inner
.u0
= u
[0]; inner
.u1
= u
[1]; inner
.u2
= u
[2]; inner
.u3
= u
[3];
496 inner
.u4
= u
[4]; inner
.u5
= u
[5]; inner
.u6
= u
[6]; inner
.u7
= u
[7];
497 inner
.s0
= s
[0]; inner
.s1
= s
[1]; inner
.s2
= s
[2]; inner
.s3
= s
[3];
498 inner
.s4
= s
[4]; inner
.s5
= s
[5]; inner
.s6
= s
[6]; inner
.s7
= s
[7];
502 tests
[1].val
= &c
[1];
504 ret
+= generic_test (tests
, ntests
, sizeof(TESTSeqOf5
),
505 (generic_encode
)encode_TESTSeqOf5
,
506 (generic_length
)length_TESTSeqOf5
,
507 (generic_decode
)decode_TESTSeqOf5
,
508 (generic_free
)free_TESTSeqOf5
,
512 for (i
= 0; i
< 8; ++i
)
519 main(int argc
, char **argv
)
523 ret
+= test_uint64();
524 ret
+= test_seqofseq();
525 ret
+= test_seqofseq2();
526 ret
+= test_seqof2();
527 ret
+= test_seqof3();
528 ret
+= test_seqof4();
529 ret
+= test_seqof5();