2 * Copyright (c) 1999 - 2007 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
40 #include <asn1-common.h>
44 #include "check-common.h"
49 cmp_integer (void *a
, void *b
)
60 struct test_case tests
[] = {
63 {NULL
, 2, "\x00\x80"},
64 {NULL
, 2, "\x01\x00"},
66 {NULL
, 2, "\xff\x7f"},
68 {NULL
, 2, "\xff\x01"},
69 {NULL
, 2, "\x00\xff"},
70 {NULL
, 4, "\x7f\xff\xff\xff"}
73 int values
[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
76 int ntests
= sizeof(tests
) / sizeof(*tests
);
78 for (i
= 0; i
< ntests
; ++i
) {
79 tests
[i
].val
= &values
[i
];
80 if (asprintf (&tests
[i
].name
, "integer %d", values
[i
]) < 0)
82 if (tests
[i
].name
== NULL
)
86 ret
= generic_test (tests
, ntests
, sizeof(int),
87 (generic_encode
)der_put_integer
,
88 (generic_length
) der_length_integer
,
89 (generic_decode
)der_get_integer
,
94 for (i
= 0; i
< ntests
; ++i
)
100 test_one_int(int val
)
106 len
= _heim_len_int(val
);
108 buf
= emalloc(len
+ 2);
111 buf
[len
+ 1] = '\xff';
112 memset(buf
+ 1, 0, len
);
114 ret
= der_put_integer(buf
+ 1 + len
- 1, len
, &val
, &len_len
);
116 printf("integer %d encode failed %d\n", val
, ret
);
119 if (len
!= len_len
) {
120 printf("integer %d encode fail with %d len %lu, result len %lu\n",
121 val
, ret
, (unsigned long)len
, (unsigned long)len_len
);
125 ret
= der_get_integer(buf
+ 1, len
, &dval
, &len_len
);
127 printf("integer %d decode failed %d\n", val
, ret
);
130 if (len
!= len_len
) {
131 printf("integer %d decoded diffrent len %lu != %lu",
132 val
, (unsigned long)len
, (unsigned long)len_len
);
136 printf("decode decoded to diffrent value %d != %d",
141 if (buf
[0] != (unsigned char)'\xff') {
142 printf("precanary dead %d\n", val
);
145 if (buf
[len
+ 1] != (unsigned char)'\xff') {
146 printf("postecanary dead %d\n", val
);
154 test_integer_more (void)
156 int i
, n1
, n2
, n3
, n4
, n5
, n6
;
159 for (i
= 0; i
< (sizeof(int) * 8); i
++) {
164 n5
= (-1) & ~(0x3f << i
);
165 n6
= (-1) & ~(0x7f << i
);
178 cmp_unsigned (void *a
, void *b
)
180 return *(unsigned int*)b
- *(unsigned int*)a
;
186 struct test_case tests
[] = {
189 {NULL
, 2, "\x00\x80"},
190 {NULL
, 2, "\x01\x00"},
191 {NULL
, 2, "\x02\x00"},
192 {NULL
, 3, "\x00\x80\x00"},
193 {NULL
, 5, "\x00\x80\x00\x00\x00"},
194 {NULL
, 4, "\x7f\xff\xff\xff"}
197 unsigned int values
[] = {0, 127, 128, 256, 512, 32768,
198 0x80000000, 0x7fffffff};
200 int ntests
= sizeof(tests
) / sizeof(*tests
);
202 for (i
= 0; i
< ntests
; ++i
) {
203 tests
[i
].val
= &values
[i
];
204 if (asprintf (&tests
[i
].name
, "unsigned %u", values
[i
]) < 0)
206 if (tests
[i
].name
== NULL
)
210 ret
= generic_test (tests
, ntests
, sizeof(int),
211 (generic_encode
)der_put_unsigned
,
212 (generic_length
)der_length_unsigned
,
213 (generic_decode
)der_get_unsigned
,
217 for (i
= 0; i
< ntests
; ++i
)
218 free (tests
[i
].name
);
223 cmp_octet_string (void *a
, void *b
)
225 heim_octet_string
*oa
= (heim_octet_string
*)a
;
226 heim_octet_string
*ob
= (heim_octet_string
*)b
;
228 if (oa
->length
!= ob
->length
)
229 return ob
->length
- oa
->length
;
231 return (memcmp (oa
->data
, ob
->data
, oa
->length
));
235 test_octet_string (void)
237 heim_octet_string s1
= {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
239 struct test_case tests
[] = {
240 {NULL
, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}
242 int ntests
= sizeof(tests
) / sizeof(*tests
);
246 if (asprintf (&tests
[0].name
, "a octet string") < 0)
248 if (tests
[0].name
== NULL
)
251 ret
= generic_test (tests
, ntests
, sizeof(heim_octet_string
),
252 (generic_encode
)der_put_octet_string
,
253 (generic_length
)der_length_octet_string
,
254 (generic_decode
)der_get_octet_string
,
255 (generic_free
)der_free_octet_string
,
263 cmp_bmp_string (void *a
, void *b
)
265 heim_bmp_string
*oa
= (heim_bmp_string
*)a
;
266 heim_bmp_string
*ob
= (heim_bmp_string
*)b
;
268 return der_heim_bmp_string_cmp(oa
, ob
);
271 static uint16_t bmp_d1
[] = { 32 };
272 static uint16_t bmp_d2
[] = { 32, 32 };
275 test_bmp_string (void)
277 heim_bmp_string s1
= { 1, bmp_d1
};
278 heim_bmp_string s2
= { 2, bmp_d2
};
280 struct test_case tests
[] = {
281 {NULL
, 2, "\x00\x20"},
282 {NULL
, 4, "\x00\x20\x00\x20"}
284 int ntests
= sizeof(tests
) / sizeof(*tests
);
288 if (asprintf (&tests
[0].name
, "a bmp string") < 0)
290 if (tests
[0].name
== NULL
)
293 if (asprintf (&tests
[1].name
, "second bmp string") < 0)
295 if (tests
[1].name
== NULL
)
298 ret
= generic_test (tests
, ntests
, sizeof(heim_bmp_string
),
299 (generic_encode
)der_put_bmp_string
,
300 (generic_length
)der_length_bmp_string
,
301 (generic_decode
)der_get_bmp_string
,
302 (generic_free
)der_free_bmp_string
,
311 cmp_universal_string (void *a
, void *b
)
313 heim_universal_string
*oa
= (heim_universal_string
*)a
;
314 heim_universal_string
*ob
= (heim_universal_string
*)b
;
316 return der_heim_universal_string_cmp(oa
, ob
);
319 static uint32_t universal_d1
[] = { 32 };
320 static uint32_t universal_d2
[] = { 32, 32 };
323 test_universal_string (void)
325 heim_universal_string s1
= { 1, universal_d1
};
326 heim_universal_string s2
= { 2, universal_d2
};
328 struct test_case tests
[] = {
329 {NULL
, 4, "\x00\x00\x00\x20"},
330 {NULL
, 8, "\x00\x00\x00\x20\x00\x00\x00\x20"}
332 int ntests
= sizeof(tests
) / sizeof(*tests
);
336 if (asprintf (&tests
[0].name
, "a universal string") < 0)
338 if (tests
[0].name
== NULL
)
341 if (asprintf (&tests
[1].name
, "second universal string") < 0)
343 if (tests
[1].name
== NULL
)
346 ret
= generic_test (tests
, ntests
, sizeof(heim_universal_string
),
347 (generic_encode
)der_put_universal_string
,
348 (generic_length
)der_length_universal_string
,
349 (generic_decode
)der_get_universal_string
,
350 (generic_free
)der_free_universal_string
,
351 cmp_universal_string
,
359 cmp_general_string (void *a
, void *b
)
361 char **sa
= (char **)a
;
362 char **sb
= (char **)b
;
364 return strcmp (*sa
, *sb
);
368 test_general_string (void)
370 char *s1
= "Test User 1";
372 struct test_case tests
[] = {
373 {NULL
, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
375 int ret
, ntests
= sizeof(tests
) / sizeof(*tests
);
378 if (asprintf (&tests
[0].name
, "the string \"%s\"", s1
) < 0)
380 if (tests
[0].name
== NULL
)
383 ret
= generic_test (tests
, ntests
, sizeof(unsigned char *),
384 (generic_encode
)der_put_general_string
,
385 (generic_length
)der_length_general_string
,
386 (generic_decode
)der_get_general_string
,
387 (generic_free
)der_free_general_string
,
395 cmp_generalized_time (void *a
, void *b
)
397 time_t *ta
= (time_t *)a
;
398 time_t *tb
= (time_t *)b
;
404 test_generalized_time (void)
406 struct test_case tests
[] = {
407 {NULL
, 15, "19700101000000Z"},
408 {NULL
, 15, "19851106210627Z"}
410 time_t values
[] = {0, 500159187};
412 int ntests
= sizeof(tests
) / sizeof(*tests
);
414 for (i
= 0; i
< ntests
; ++i
) {
415 tests
[i
].val
= &values
[i
];
416 if (asprintf (&tests
[i
].name
, "time %d", (int)values
[i
]) < 0)
418 if (tests
[i
].name
== NULL
)
422 ret
= generic_test (tests
, ntests
, sizeof(time_t),
423 (generic_encode
)der_put_generalized_time
,
424 (generic_length
)der_length_generalized_time
,
425 (generic_decode
)der_get_generalized_time
,
427 cmp_generalized_time
,
429 for (i
= 0; i
< ntests
; ++i
)
435 test_cmp_oid (void *a
, void *b
)
437 return der_heim_oid_cmp((heim_oid
*)a
, (heim_oid
*)b
);
440 static unsigned oid_comp1
[] = { 1, 1, 1 };
441 static unsigned oid_comp2
[] = { 1, 1 };
442 static unsigned oid_comp3
[] = { 6, 15, 1 };
443 static unsigned oid_comp4
[] = { 6, 15 };
448 struct test_case tests
[] = {
449 {NULL
, 2, "\x29\x01"},
451 {NULL
, 2, "\xff\x01"},
454 heim_oid values
[] = {
461 int ntests
= sizeof(tests
) / sizeof(*tests
);
463 for (i
= 0; i
< ntests
; ++i
) {
464 tests
[i
].val
= &values
[i
];
465 if (asprintf (&tests
[i
].name
, "oid %d", i
) < 0)
467 if (tests
[i
].name
== NULL
)
471 ret
= generic_test (tests
, ntests
, sizeof(heim_oid
),
472 (generic_encode
)der_put_oid
,
473 (generic_length
)der_length_oid
,
474 (generic_decode
)der_get_oid
,
475 (generic_free
)der_free_oid
,
478 for (i
= 0; i
< ntests
; ++i
)
484 test_cmp_bit_string (void *a
, void *b
)
486 return der_heim_bit_string_cmp((heim_bit_string
*)a
, (heim_bit_string
*)b
);
490 test_bit_string (void)
492 struct test_case tests
[] = {
495 heim_bit_string values
[] = {
499 int ntests
= sizeof(tests
) / sizeof(*tests
);
501 for (i
= 0; i
< ntests
; ++i
) {
502 tests
[i
].val
= &values
[i
];
503 if (asprintf (&tests
[i
].name
, "bit_string %d", i
) < 0)
505 if (tests
[i
].name
== NULL
)
509 ret
= generic_test (tests
, ntests
, sizeof(heim_bit_string
),
510 (generic_encode
)der_put_bit_string
,
511 (generic_length
)der_length_bit_string
,
512 (generic_decode
)der_get_bit_string
,
513 (generic_free
)der_free_bit_string
,
516 for (i
= 0; i
< ntests
; ++i
)
522 test_cmp_heim_integer (void *a
, void *b
)
524 return der_heim_integer_cmp((heim_integer
*)a
, (heim_integer
*)b
);
528 test_heim_integer (void)
530 struct test_case tests
[] = {
531 {NULL
, 2, "\xfe\x01"},
532 {NULL
, 2, "\xef\x01"},
533 {NULL
, 3, "\xff\x00\xff"},
534 {NULL
, 3, "\xff\x01\x00"},
537 {NULL
, 2, "\x00\x80"}
540 heim_integer values
[] = {
541 { 2, "\x01\xff", 1 },
542 { 2, "\x10\xff", 1 },
543 { 2, "\xff\x01", 1 },
544 { 2, "\xff\x00", 1 },
550 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
554 for (i
= 0; i
< ntests
; ++i
) {
555 tests
[i
].val
= &values
[i
];
556 if (asprintf (&tests
[i
].name
, "heim_integer %d", i
) < 0)
558 if (tests
[i
].name
== NULL
)
562 ret
= generic_test (tests
, ntests
, sizeof(heim_integer
),
563 (generic_encode
)der_put_heim_integer
,
564 (generic_length
)der_length_heim_integer
,
565 (generic_decode
)der_get_heim_integer
,
566 (generic_free
)der_free_heim_integer
,
567 test_cmp_heim_integer
,
569 for (i
= 0; i
< ntests
; ++i
)
570 free (tests
[i
].name
);
574 /* test zero length integer (BER format) */
575 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
577 errx(1, "der_get_heim_integer");
579 errx(1, "der_get_heim_integer wrong length");
580 der_free_heim_integer(&i2
);
586 test_cmp_boolean (void *a
, void *b
)
588 return !!*(int *)a
!= !!*(int *)b
;
594 struct test_case tests
[] = {
599 int values
[] = { 1, 0 };
601 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
605 for (i
= 0; i
< ntests
; ++i
) {
606 tests
[i
].val
= &values
[i
];
607 if (asprintf (&tests
[i
].name
, "heim_boolean %d", i
) < 0)
609 if (tests
[i
].name
== NULL
)
613 ret
= generic_test (tests
, ntests
, sizeof(int),
614 (generic_encode
)der_put_boolean
,
615 (generic_length
)der_length_boolean
,
616 (generic_decode
)der_get_boolean
,
620 for (i
= 0; i
< ntests
; ++i
)
621 free (tests
[i
].name
);
625 /* test zero length integer (BER format) */
626 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
628 errx(1, "der_get_heim_integer");
630 errx(1, "der_get_heim_integer wrong length");
631 der_free_heim_integer(&i2
);
637 check_fail_unsigned(void)
639 struct test_case tests
[] = {
640 {NULL
, sizeof(unsigned) + 1,
641 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
643 int ntests
= sizeof(tests
) / sizeof(*tests
);
645 return generic_decode_fail(tests
, ntests
, sizeof(unsigned),
646 (generic_decode
)der_get_unsigned
);
650 check_fail_integer(void)
652 struct test_case tests
[] = {
653 {NULL
, sizeof(int) + 1,
654 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
656 int ntests
= sizeof(tests
) / sizeof(*tests
);
658 return generic_decode_fail(tests
, ntests
, sizeof(int),
659 (generic_decode
)der_get_integer
);
663 check_fail_length(void)
665 struct test_case tests
[] = {
666 {NULL
, 0, "", "empty input data"},
667 {NULL
, 1, "\x82", "internal length overrun" }
669 int ntests
= sizeof(tests
) / sizeof(*tests
);
671 return generic_decode_fail(tests
, ntests
, sizeof(size_t),
672 (generic_decode
)der_get_length
);
676 check_fail_boolean(void)
678 struct test_case tests
[] = {
679 {NULL
, 0, "", "empty input data"}
681 int ntests
= sizeof(tests
) / sizeof(*tests
);
683 return generic_decode_fail(tests
, ntests
, sizeof(int),
684 (generic_decode
)der_get_boolean
);
688 check_fail_general_string(void)
690 struct test_case tests
[] = {
691 { NULL
, 3, "A\x00i", "NUL char in string"}
693 int ntests
= sizeof(tests
) / sizeof(*tests
);
695 return generic_decode_fail(tests
, ntests
, sizeof(heim_general_string
),
696 (generic_decode
)der_get_general_string
);
700 check_fail_bmp_string(void)
702 struct test_case tests
[] = {
703 {NULL
, 1, "\x00", "odd (1) length bmpstring"},
704 {NULL
, 3, "\x00\x00\x00", "odd (3) length bmpstring"}
706 int ntests
= sizeof(tests
) / sizeof(*tests
);
708 return generic_decode_fail(tests
, ntests
, sizeof(heim_bmp_string
),
709 (generic_decode
)der_get_bmp_string
);
713 check_fail_universal_string(void)
715 struct test_case tests
[] = {
716 {NULL
, 1, "\x00", "x & 3 == 1 universal string"},
717 {NULL
, 2, "\x00\x00", "x & 3 == 2 universal string"},
718 {NULL
, 3, "\x00\x00\x00", "x & 3 == 3 universal string"},
719 {NULL
, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"},
720 {NULL
, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"},
721 {NULL
, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"}
723 int ntests
= sizeof(tests
) / sizeof(*tests
);
725 return generic_decode_fail(tests
, ntests
, sizeof(heim_universal_string
),
726 (generic_decode
)der_get_universal_string
);
730 check_fail_heim_integer(void)
733 struct test_case tests
[] = {
735 int ntests
= sizeof(tests
) / sizeof(*tests
);
737 return generic_decode_fail(tests
, ntests
, sizeof(heim_integer
),
738 (generic_decode
)der_get_heim_integer
);
745 check_fail_generalized_time(void)
747 struct test_case tests
[] = {
748 {NULL
, 1, "\x00", "no time"}
750 int ntests
= sizeof(tests
) / sizeof(*tests
);
752 return generic_decode_fail(tests
, ntests
, sizeof(time_t),
753 (generic_decode
)der_get_generalized_time
);
759 struct test_case tests
[] = {
760 {NULL
, 0, "", "empty input data"},
761 {NULL
, 2, "\x00\x80", "last byte continuation" },
762 {NULL
, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00",
763 "oid element overflow" }
765 int ntests
= sizeof(tests
) / sizeof(*tests
);
767 return generic_decode_fail(tests
, ntests
, sizeof(heim_oid
),
768 (generic_decode
)der_get_oid
);
772 check_fail_bitstring(void)
774 struct test_case tests
[] = {
775 {NULL
, 0, "", "empty input data"},
776 {NULL
, 1, "\x08", "larger then 8 bits trailer"},
777 {NULL
, 1, "\x01", "to few bytes for bits"},
778 {NULL
, -2, "\x00", "length overrun"},
779 {NULL
, -1, "", "length to short"}
781 int ntests
= sizeof(tests
) / sizeof(*tests
);
783 return generic_decode_fail(tests
, ntests
, sizeof(heim_bit_string
),
784 (generic_decode
)der_get_bit_string
);
788 check_heim_integer_same(const char *p
, const char *norm_p
, heim_integer
*i
)
794 ret
= der_print_hex_heim_integer(i
, &str
);
796 errx(1, "der_print_hex_heim_integer: %d", ret
);
798 if (strcmp(str
, norm_p
) != 0)
799 errx(1, "der_print_hex_heim_integer: %s != %s", str
, p
);
801 ret
= der_parse_hex_heim_integer(str
, &i2
);
803 errx(1, "der_parse_hex_heim_integer: %d", ret
);
805 if (der_heim_integer_cmp(i
, &i2
) != 0)
806 errx(1, "der_heim_integer_cmp: p %s", p
);
808 der_free_heim_integer(&i2
);
811 ret
= der_parse_hex_heim_integer(p
, &i2
);
813 errx(1, "der_parse_hex_heim_integer: %d", ret
);
815 if (der_heim_integer_cmp(i
, &i2
) != 0)
816 errx(1, "der_heim_integer_cmp: norm");
818 der_free_heim_integer(&i2
);
824 test_heim_int_format(void)
826 heim_integer i
= { 1, "\x10", 0 };
827 heim_integer i2
= { 1, "\x10", 1 };
828 heim_integer i3
= { 1, "\01", 0 };
830 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
831 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
832 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
833 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
834 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
835 "FFFFFFFF" "FFFFFFFF";
838 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2"
839 "\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1"
840 "\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6"
841 "\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD"
842 "\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D"
843 "\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45"
844 "\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9"
845 "\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED"
846 "\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11"
847 "\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81"
848 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
854 ret
+= check_heim_integer_same(p
, p
, &bni
);
855 ret
+= check_heim_integer_same("10", "10", &i
);
856 ret
+= check_heim_integer_same("00000010", "10", &i
);
857 ret
+= check_heim_integer_same("-10", "-10", &i2
);
858 ret
+= check_heim_integer_same("-00000010", "-10", &i2
);
859 ret
+= check_heim_integer_same("01", "01", &i3
);
860 ret
+= check_heim_integer_same("1", "01", &i3
);
864 r
= der_parse_hex_heim_integer("-", &f
);
866 der_free_heim_integer(&f
);
869 /* used to cause UMR */
870 r
= der_parse_hex_heim_integer("00", &f
);
872 der_free_heim_integer(&f
);
881 test_heim_oid_format_same(const char *str
, const heim_oid
*oid
)
887 ret
= der_print_heim_oid(oid
, ' ', &p
);
889 printf("fail to print oid: %s\n", str
);
892 ret
= strcmp(p
, str
);
894 printf("oid %s != formated oid %s\n", str
, p
);
899 ret
= der_parse_heim_oid(p
, " ", &o2
);
901 printf("failed to parse %s\n", p
);
906 ret
= der_heim_oid_cmp(&o2
, oid
);
912 static unsigned sha1_oid_tree
[] = { 1, 3, 14, 3, 2, 26 };
915 test_heim_oid_format(void)
917 heim_oid sha1
= { 6, sha1_oid_tree
};
920 ret
+= test_heim_oid_format_same("1 3 14 3 2 26", &sha1
);
926 check_trailing_nul(void)
931 const unsigned char *p
;
936 { 1, (const unsigned char *)"foo\x00o", 5, NULL
, 0 },
937 { 1, (const unsigned char *)"\x00o", 2, NULL
, 0 },
938 { 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 },
939 { 0, (const unsigned char *)"\x00", 1, "", 1 },
940 { 0, (const unsigned char *)"", 0, "", 0 },
941 { 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 },
942 { 0, (const unsigned char *)"foo\0", 4, "foo", 4 },
943 { 0, (const unsigned char *)"foo", 3, "foo", 3 }
946 for (i
= 0; i
< sizeof(foo
)/sizeof(foo
[0]); i
++) {
949 ret
= der_get_general_string(foo
[i
].p
, foo
[i
].len
, &s
, &size
);
952 errx(1, "check %d NULL didn't fail", i
);
956 errx(1, "NULL check %d der_get_general_string failed", i
);
957 if (foo
[i
].size
!= size
)
958 errx(1, "NUL check i = %d size failed", i
);
959 if (strcmp(foo
[i
].s
, s
) != 0)
960 errx(1, "NUL check i = %d content failed", i
);
971 /* diffrent lengths are diffrent */
973 const heim_octet_string os1
= { 1, "a" } , os2
= { 0, NULL
};
974 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
978 /* diffrent data are diffrent */
980 const heim_octet_string os1
= { 1, "a" } , os2
= { 1, "b" };
981 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
985 /* diffrent lengths are diffrent */
987 const heim_bit_string bs1
= { 8, "a" } , bs2
= { 7, "a" };
988 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
992 /* diffrent data are diffrent */
994 const heim_bit_string bs1
= { 7, "\x0f" } , bs2
= { 7, "\x02" };
995 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
999 /* diffrent lengths are diffrent */
1002 heim_bmp_string bs1
= { 1, NULL
} , bs2
= { 0, NULL
};
1004 ret
= der_heim_bmp_string_cmp(&bs1
, &bs2
);
1008 /* diffrent lengths are diffrent */
1011 heim_universal_string us1
= { 1, NULL
} , us2
= { 0, NULL
};
1013 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1019 uint32_t data
= (uint32_t)'a';
1020 heim_universal_string us1
= { 1, NULL
} , us2
= { 1, NULL
};
1023 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1032 corner_generalized_time(void)
1034 const char *str
= "760520140000Z";
1039 ret
= der_get_generalized_time((const unsigned char*)str
, strlen(str
),
1056 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
1064 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
1065 ret
= der_get_tag((const unsigned char*)tests
[i
].ptr
,
1066 tests
[i
].len
, &cl
, &ty
, &tag
, &size
);
1069 errx(1, "failed while shouldn't");
1072 errx(1, "passed while shouldn't");
1079 main(int argc
, char **argv
)
1083 ret
+= test_integer ();
1084 ret
+= test_integer_more();
1085 ret
+= test_unsigned ();
1086 ret
+= test_octet_string ();
1087 ret
+= test_bmp_string ();
1088 ret
+= test_universal_string ();
1089 ret
+= test_general_string ();
1090 ret
+= test_generalized_time ();
1092 ret
+= test_bit_string();
1093 ret
+= test_heim_integer();
1094 ret
+= test_boolean();
1096 ret
+= check_fail_unsigned();
1097 ret
+= check_fail_integer();
1098 ret
+= check_fail_length();
1099 ret
+= check_fail_boolean();
1100 ret
+= check_fail_general_string();
1101 ret
+= check_fail_bmp_string();
1102 ret
+= check_fail_universal_string();
1103 ret
+= check_fail_heim_integer();
1104 ret
+= check_fail_generalized_time();
1105 ret
+= check_fail_oid();
1106 ret
+= check_fail_bitstring();
1107 ret
+= test_heim_int_format();
1108 ret
+= test_heim_oid_format();
1109 ret
+= check_trailing_nul();
1110 ret
+= test_misc_cmp();
1111 ret
+= corner_generalized_time();
1112 ret
+= corner_tag();