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 asprintf (&tests
[i
].name
, "integer %d", values
[i
]);
81 if (tests
[i
].name
== NULL
)
85 ret
= generic_test (tests
, ntests
, sizeof(int),
86 (generic_encode
)der_put_integer
,
87 (generic_length
) der_length_integer
,
88 (generic_decode
)der_get_integer
,
93 for (i
= 0; i
< ntests
; ++i
)
105 len
= _heim_len_int(val
);
107 buf
= emalloc(len
+ 2);
110 buf
[len
+ 1] = '\xff';
111 memset(buf
+ 1, 0, len
);
113 ret
= der_put_integer(buf
+ 1 + len
- 1, len
, &val
, &len_len
);
115 printf("integer %d encode failed %d\n", val
, ret
);
118 if (len
!= len_len
) {
119 printf("integer %d encode fail with %d len %lu, result len %lu\n",
120 val
, ret
, (unsigned long)len
, (unsigned long)len_len
);
124 ret
= der_get_integer(buf
+ 1, len
, &dval
, &len_len
);
126 printf("integer %d decode failed %d\n", val
, ret
);
129 if (len
!= len_len
) {
130 printf("integer %d decoded diffrent len %lu != %lu",
131 val
, (unsigned long)len
, (unsigned long)len_len
);
135 printf("decode decoded to diffrent value %d != %d",
140 if (buf
[0] != (unsigned char)'\xff') {
141 printf("precanary dead %d\n", val
);
144 if (buf
[len
+ 1] != (unsigned char)'\xff') {
145 printf("postecanary dead %d\n", val
);
153 test_integer_more (void)
155 int i
, n1
, n2
, n3
, n4
, n5
, n6
;
158 for (i
= 0; i
< (sizeof(int) * 8); i
++) {
163 n5
= (-1) & ~(0x3f << i
);
164 n6
= (-1) & ~(0x7f << i
);
177 cmp_unsigned (void *a
, void *b
)
179 return *(unsigned int*)b
- *(unsigned int*)a
;
185 struct test_case tests
[] = {
188 {NULL
, 2, "\x00\x80"},
189 {NULL
, 2, "\x01\x00"},
190 {NULL
, 2, "\x02\x00"},
191 {NULL
, 3, "\x00\x80\x00"},
192 {NULL
, 5, "\x00\x80\x00\x00\x00"},
193 {NULL
, 4, "\x7f\xff\xff\xff"}
196 unsigned int values
[] = {0, 127, 128, 256, 512, 32768,
197 0x80000000, 0x7fffffff};
199 int ntests
= sizeof(tests
) / sizeof(*tests
);
201 for (i
= 0; i
< ntests
; ++i
) {
202 tests
[i
].val
= &values
[i
];
203 asprintf (&tests
[i
].name
, "unsigned %u", values
[i
]);
204 if (tests
[i
].name
== NULL
)
208 ret
= generic_test (tests
, ntests
, sizeof(int),
209 (generic_encode
)der_put_unsigned
,
210 (generic_length
)der_length_unsigned
,
211 (generic_decode
)der_get_unsigned
,
215 for (i
= 0; i
< ntests
; ++i
)
216 free (tests
[i
].name
);
221 cmp_octet_string (void *a
, void *b
)
223 heim_octet_string
*oa
= (heim_octet_string
*)a
;
224 heim_octet_string
*ob
= (heim_octet_string
*)b
;
226 if (oa
->length
!= ob
->length
)
227 return ob
->length
- oa
->length
;
229 return (memcmp (oa
->data
, ob
->data
, oa
->length
));
233 test_octet_string (void)
235 heim_octet_string s1
= {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
237 struct test_case tests
[] = {
238 {NULL
, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}
240 int ntests
= sizeof(tests
) / sizeof(*tests
);
244 asprintf (&tests
[0].name
, "a octet string");
245 if (tests
[0].name
== NULL
)
248 ret
= generic_test (tests
, ntests
, sizeof(heim_octet_string
),
249 (generic_encode
)der_put_octet_string
,
250 (generic_length
)der_length_octet_string
,
251 (generic_decode
)der_get_octet_string
,
252 (generic_free
)der_free_octet_string
,
260 cmp_bmp_string (void *a
, void *b
)
262 heim_bmp_string
*oa
= (heim_bmp_string
*)a
;
263 heim_bmp_string
*ob
= (heim_bmp_string
*)b
;
265 return der_heim_bmp_string_cmp(oa
, ob
);
268 static uint16_t bmp_d1
[] = { 32 };
269 static uint16_t bmp_d2
[] = { 32, 32 };
272 test_bmp_string (void)
274 heim_bmp_string s1
= { 1, bmp_d1
};
275 heim_bmp_string s2
= { 2, bmp_d2
};
277 struct test_case tests
[] = {
278 {NULL
, 2, "\x00\x20"},
279 {NULL
, 4, "\x00\x20\x00\x20"}
281 int ntests
= sizeof(tests
) / sizeof(*tests
);
285 asprintf (&tests
[0].name
, "a bmp string");
286 if (tests
[0].name
== NULL
)
289 asprintf (&tests
[1].name
, "second bmp string");
290 if (tests
[1].name
== NULL
)
293 ret
= generic_test (tests
, ntests
, sizeof(heim_bmp_string
),
294 (generic_encode
)der_put_bmp_string
,
295 (generic_length
)der_length_bmp_string
,
296 (generic_decode
)der_get_bmp_string
,
297 (generic_free
)der_free_bmp_string
,
306 cmp_universal_string (void *a
, void *b
)
308 heim_universal_string
*oa
= (heim_universal_string
*)a
;
309 heim_universal_string
*ob
= (heim_universal_string
*)b
;
311 return der_heim_universal_string_cmp(oa
, ob
);
314 static uint32_t universal_d1
[] = { 32 };
315 static uint32_t universal_d2
[] = { 32, 32 };
318 test_universal_string (void)
320 heim_universal_string s1
= { 1, universal_d1
};
321 heim_universal_string s2
= { 2, universal_d2
};
323 struct test_case tests
[] = {
324 {NULL
, 4, "\x00\x00\x00\x20"},
325 {NULL
, 8, "\x00\x00\x00\x20\x00\x00\x00\x20"}
327 int ntests
= sizeof(tests
) / sizeof(*tests
);
331 asprintf (&tests
[0].name
, "a universal string");
332 if (tests
[0].name
== NULL
)
335 asprintf (&tests
[1].name
, "second universal string");
336 if (tests
[1].name
== NULL
)
339 ret
= generic_test (tests
, ntests
, sizeof(heim_universal_string
),
340 (generic_encode
)der_put_universal_string
,
341 (generic_length
)der_length_universal_string
,
342 (generic_decode
)der_get_universal_string
,
343 (generic_free
)der_free_universal_string
,
344 cmp_universal_string
,
352 cmp_general_string (void *a
, void *b
)
354 char **sa
= (char **)a
;
355 char **sb
= (char **)b
;
357 return strcmp (*sa
, *sb
);
361 test_general_string (void)
363 char *s1
= "Test User 1";
365 struct test_case tests
[] = {
366 {NULL
, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
368 int ret
, ntests
= sizeof(tests
) / sizeof(*tests
);
371 asprintf (&tests
[0].name
, "the string \"%s\"", s1
);
372 if (tests
[0].name
== NULL
)
375 ret
= generic_test (tests
, ntests
, sizeof(unsigned char *),
376 (generic_encode
)der_put_general_string
,
377 (generic_length
)der_length_general_string
,
378 (generic_decode
)der_get_general_string
,
379 (generic_free
)der_free_general_string
,
387 cmp_generalized_time (void *a
, void *b
)
389 time_t *ta
= (time_t *)a
;
390 time_t *tb
= (time_t *)b
;
396 test_generalized_time (void)
398 struct test_case tests
[] = {
399 {NULL
, 15, "19700101000000Z"},
400 {NULL
, 15, "19851106210627Z"}
402 time_t values
[] = {0, 500159187};
404 int ntests
= sizeof(tests
) / sizeof(*tests
);
406 for (i
= 0; i
< ntests
; ++i
) {
407 tests
[i
].val
= &values
[i
];
408 asprintf (&tests
[i
].name
, "time %d", (int)values
[i
]);
409 if (tests
[i
].name
== NULL
)
413 ret
= generic_test (tests
, ntests
, sizeof(time_t),
414 (generic_encode
)der_put_generalized_time
,
415 (generic_length
)der_length_generalized_time
,
416 (generic_decode
)der_get_generalized_time
,
418 cmp_generalized_time
,
420 for (i
= 0; i
< ntests
; ++i
)
426 test_cmp_oid (void *a
, void *b
)
428 return der_heim_oid_cmp((heim_oid
*)a
, (heim_oid
*)b
);
431 static unsigned oid_comp1
[] = { 1, 1, 1 };
432 static unsigned oid_comp2
[] = { 1, 1 };
433 static unsigned oid_comp3
[] = { 6, 15, 1 };
434 static unsigned oid_comp4
[] = { 6, 15 };
439 struct test_case tests
[] = {
440 {NULL
, 2, "\x29\x01"},
442 {NULL
, 2, "\xff\x01"},
445 heim_oid values
[] = {
452 int ntests
= sizeof(tests
) / sizeof(*tests
);
454 for (i
= 0; i
< ntests
; ++i
) {
455 tests
[i
].val
= &values
[i
];
456 asprintf (&tests
[i
].name
, "oid %d", i
);
457 if (tests
[i
].name
== NULL
)
461 ret
= generic_test (tests
, ntests
, sizeof(heim_oid
),
462 (generic_encode
)der_put_oid
,
463 (generic_length
)der_length_oid
,
464 (generic_decode
)der_get_oid
,
465 (generic_free
)der_free_oid
,
468 for (i
= 0; i
< ntests
; ++i
)
474 test_cmp_bit_string (void *a
, void *b
)
476 return der_heim_bit_string_cmp((heim_bit_string
*)a
, (heim_bit_string
*)b
);
480 test_bit_string (void)
482 struct test_case tests
[] = {
485 heim_bit_string values
[] = {
489 int ntests
= sizeof(tests
) / sizeof(*tests
);
491 for (i
= 0; i
< ntests
; ++i
) {
492 tests
[i
].val
= &values
[i
];
493 asprintf (&tests
[i
].name
, "bit_string %d", i
);
494 if (tests
[i
].name
== NULL
)
498 ret
= generic_test (tests
, ntests
, sizeof(heim_bit_string
),
499 (generic_encode
)der_put_bit_string
,
500 (generic_length
)der_length_bit_string
,
501 (generic_decode
)der_get_bit_string
,
502 (generic_free
)der_free_bit_string
,
505 for (i
= 0; i
< ntests
; ++i
)
511 test_cmp_heim_integer (void *a
, void *b
)
513 return der_heim_integer_cmp((heim_integer
*)a
, (heim_integer
*)b
);
517 test_heim_integer (void)
519 struct test_case tests
[] = {
520 {NULL
, 2, "\xfe\x01"},
521 {NULL
, 2, "\xef\x01"},
522 {NULL
, 3, "\xff\x00\xff"},
523 {NULL
, 3, "\xff\x01\x00"},
526 {NULL
, 2, "\x00\x80"}
529 heim_integer values
[] = {
530 { 2, "\x01\xff", 1 },
531 { 2, "\x10\xff", 1 },
532 { 2, "\xff\x01", 1 },
533 { 2, "\xff\x00", 1 },
539 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
543 for (i
= 0; i
< ntests
; ++i
) {
544 tests
[i
].val
= &values
[i
];
545 asprintf (&tests
[i
].name
, "heim_integer %d", i
);
546 if (tests
[i
].name
== NULL
)
550 ret
= generic_test (tests
, ntests
, sizeof(heim_integer
),
551 (generic_encode
)der_put_heim_integer
,
552 (generic_length
)der_length_heim_integer
,
553 (generic_decode
)der_get_heim_integer
,
554 (generic_free
)der_free_heim_integer
,
555 test_cmp_heim_integer
,
557 for (i
= 0; i
< ntests
; ++i
)
558 free (tests
[i
].name
);
562 /* test zero length integer (BER format) */
563 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
565 errx(1, "der_get_heim_integer");
567 errx(1, "der_get_heim_integer wrong length");
568 der_free_heim_integer(&i2
);
574 test_cmp_boolean (void *a
, void *b
)
576 return !!*(int *)a
!= !!*(int *)b
;
582 struct test_case tests
[] = {
587 int values
[] = { 1, 0 };
589 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
593 for (i
= 0; i
< ntests
; ++i
) {
594 tests
[i
].val
= &values
[i
];
595 asprintf (&tests
[i
].name
, "heim_boolean %d", i
);
596 if (tests
[i
].name
== NULL
)
600 ret
= generic_test (tests
, ntests
, sizeof(int),
601 (generic_encode
)der_put_boolean
,
602 (generic_length
)der_length_boolean
,
603 (generic_decode
)der_get_boolean
,
607 for (i
= 0; i
< ntests
; ++i
)
608 free (tests
[i
].name
);
612 /* test zero length integer (BER format) */
613 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
615 errx(1, "der_get_heim_integer");
617 errx(1, "der_get_heim_integer wrong length");
618 der_free_heim_integer(&i2
);
624 check_fail_unsigned(void)
626 struct test_case tests
[] = {
627 {NULL
, sizeof(unsigned) + 1,
628 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
630 int ntests
= sizeof(tests
) / sizeof(*tests
);
632 return generic_decode_fail(tests
, ntests
, sizeof(unsigned),
633 (generic_decode
)der_get_unsigned
);
637 check_fail_integer(void)
639 struct test_case tests
[] = {
640 {NULL
, sizeof(int) + 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(int),
646 (generic_decode
)der_get_integer
);
650 check_fail_length(void)
652 struct test_case tests
[] = {
653 {NULL
, 0, "", "empty input data"},
654 {NULL
, 1, "\x82", "internal length overrun" }
656 int ntests
= sizeof(tests
) / sizeof(*tests
);
658 return generic_decode_fail(tests
, ntests
, sizeof(size_t),
659 (generic_decode
)der_get_length
);
663 check_fail_boolean(void)
665 struct test_case tests
[] = {
666 {NULL
, 0, "", "empty input data"}
668 int ntests
= sizeof(tests
) / sizeof(*tests
);
670 return generic_decode_fail(tests
, ntests
, sizeof(int),
671 (generic_decode
)der_get_boolean
);
675 check_fail_general_string(void)
677 struct test_case tests
[] = {
678 { NULL
, 3, "A\x00i", "NUL char in string"}
680 int ntests
= sizeof(tests
) / sizeof(*tests
);
682 return generic_decode_fail(tests
, ntests
, sizeof(heim_general_string
),
683 (generic_decode
)der_get_general_string
);
687 check_fail_bmp_string(void)
689 struct test_case tests
[] = {
690 {NULL
, 1, "\x00", "odd (1) length bmpstring"},
691 {NULL
, 3, "\x00\x00\x00", "odd (3) length bmpstring"}
693 int ntests
= sizeof(tests
) / sizeof(*tests
);
695 return generic_decode_fail(tests
, ntests
, sizeof(heim_bmp_string
),
696 (generic_decode
)der_get_bmp_string
);
700 check_fail_universal_string(void)
702 struct test_case tests
[] = {
703 {NULL
, 1, "\x00", "x & 3 == 1 universal string"},
704 {NULL
, 2, "\x00\x00", "x & 3 == 2 universal string"},
705 {NULL
, 3, "\x00\x00\x00", "x & 3 == 3 universal string"},
706 {NULL
, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"},
707 {NULL
, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"},
708 {NULL
, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"}
710 int ntests
= sizeof(tests
) / sizeof(*tests
);
712 return generic_decode_fail(tests
, ntests
, sizeof(heim_universal_string
),
713 (generic_decode
)der_get_universal_string
);
717 check_fail_heim_integer(void)
720 struct test_case tests
[] = {
722 int ntests
= sizeof(tests
) / sizeof(*tests
);
724 return generic_decode_fail(tests
, ntests
, sizeof(heim_integer
),
725 (generic_decode
)der_get_heim_integer
);
732 check_fail_generalized_time(void)
734 struct test_case tests
[] = {
735 {NULL
, 1, "\x00", "no time"}
737 int ntests
= sizeof(tests
) / sizeof(*tests
);
739 return generic_decode_fail(tests
, ntests
, sizeof(time_t),
740 (generic_decode
)der_get_generalized_time
);
746 struct test_case tests
[] = {
747 {NULL
, 0, "", "empty input data"},
748 {NULL
, 2, "\x00\x80", "last byte continuation" },
749 {NULL
, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00",
750 "oid element overflow" }
752 int ntests
= sizeof(tests
) / sizeof(*tests
);
754 return generic_decode_fail(tests
, ntests
, sizeof(heim_oid
),
755 (generic_decode
)der_get_oid
);
759 check_fail_bitstring(void)
761 struct test_case tests
[] = {
762 {NULL
, 0, "", "empty input data"},
763 {NULL
, 1, "\x08", "larger then 8 bits trailer"},
764 {NULL
, 1, "\x01", "to few bytes for bits"},
765 {NULL
, -2, "\x00", "length overrun"},
766 {NULL
, -1, "", "length to short"}
768 int ntests
= sizeof(tests
) / sizeof(*tests
);
770 return generic_decode_fail(tests
, ntests
, sizeof(heim_bit_string
),
771 (generic_decode
)der_get_bit_string
);
775 check_heim_integer_same(const char *p
, const char *norm_p
, heim_integer
*i
)
781 ret
= der_print_hex_heim_integer(i
, &str
);
783 errx(1, "der_print_hex_heim_integer: %d", ret
);
785 if (strcmp(str
, norm_p
) != 0)
786 errx(1, "der_print_hex_heim_integer: %s != %s", str
, p
);
788 ret
= der_parse_hex_heim_integer(str
, &i2
);
790 errx(1, "der_parse_hex_heim_integer: %d", ret
);
792 if (der_heim_integer_cmp(i
, &i2
) != 0)
793 errx(1, "der_heim_integer_cmp: p %s", p
);
795 der_free_heim_integer(&i2
);
798 ret
= der_parse_hex_heim_integer(p
, &i2
);
800 errx(1, "der_parse_hex_heim_integer: %d", ret
);
802 if (der_heim_integer_cmp(i
, &i2
) != 0)
803 errx(1, "der_heim_integer_cmp: norm");
805 der_free_heim_integer(&i2
);
811 test_heim_int_format(void)
813 heim_integer i
= { 1, "\x10", 0 };
814 heim_integer i2
= { 1, "\x10", 1 };
815 heim_integer i3
= { 1, "\01", 0 };
817 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
818 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
819 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
820 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
821 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
822 "FFFFFFFF" "FFFFFFFF";
825 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2"
826 "\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1"
827 "\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6"
828 "\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD"
829 "\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D"
830 "\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45"
831 "\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9"
832 "\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED"
833 "\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11"
834 "\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81"
835 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
841 ret
+= check_heim_integer_same(p
, p
, &bni
);
842 ret
+= check_heim_integer_same("10", "10", &i
);
843 ret
+= check_heim_integer_same("00000010", "10", &i
);
844 ret
+= check_heim_integer_same("-10", "-10", &i2
);
845 ret
+= check_heim_integer_same("-00000010", "-10", &i2
);
846 ret
+= check_heim_integer_same("01", "01", &i3
);
847 ret
+= check_heim_integer_same("1", "01", &i3
);
851 r
= der_parse_hex_heim_integer("-", &f
);
853 der_free_heim_integer(&f
);
856 /* used to cause UMR */
857 r
= der_parse_hex_heim_integer("00", &f
);
859 der_free_heim_integer(&f
);
868 test_heim_oid_format_same(const char *str
, const heim_oid
*oid
)
874 ret
= der_print_heim_oid(oid
, ' ', &p
);
876 printf("fail to print oid: %s\n", str
);
879 ret
= strcmp(p
, str
);
881 printf("oid %s != formated oid %s\n", str
, p
);
886 ret
= der_parse_heim_oid(p
, " ", &o2
);
888 printf("failed to parse %s\n", p
);
893 ret
= der_heim_oid_cmp(&o2
, oid
);
899 static unsigned sha1_oid_tree
[] = { 1, 3, 14, 3, 2, 26 };
902 test_heim_oid_format(void)
904 heim_oid sha1
= { 6, sha1_oid_tree
};
907 ret
+= test_heim_oid_format_same("1 3 14 3 2 26", &sha1
);
913 check_trailing_nul(void)
918 const unsigned char *p
;
923 { 1, (const unsigned char *)"foo\x00o", 5, NULL
, 0 },
924 { 1, (const unsigned char *)"\x00o", 2, NULL
, 0 },
925 { 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 },
926 { 0, (const unsigned char *)"\x00", 1, "", 1 },
927 { 0, (const unsigned char *)"", 0, "", 0 },
928 { 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 },
929 { 0, (const unsigned char *)"foo\0", 4, "foo", 4 },
930 { 0, (const unsigned char *)"foo", 3, "foo", 3 }
933 for (i
= 0; i
< sizeof(foo
)/sizeof(foo
[0]); i
++) {
936 ret
= der_get_general_string(foo
[i
].p
, foo
[i
].len
, &s
, &size
);
939 errx(1, "check %d NULL didn't fail", i
);
943 errx(1, "NULL check %d der_get_general_string failed", i
);
944 if (foo
[i
].size
!= size
)
945 errx(1, "NUL check i = %d size failed", i
);
946 if (strcmp(foo
[i
].s
, s
) != 0)
947 errx(1, "NUL check i = %d content failed", i
);
958 /* diffrent lengths are diffrent */
960 const heim_octet_string os1
= { 1, "a" } , os2
= { 0, NULL
};
961 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
965 /* diffrent data are diffrent */
967 const heim_octet_string os1
= { 1, "a" } , os2
= { 1, "b" };
968 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
972 /* diffrent lengths are diffrent */
974 const heim_bit_string bs1
= { 8, "a" } , bs2
= { 7, "a" };
975 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
979 /* diffrent data are diffrent */
981 const heim_bit_string bs1
= { 7, "\x0f" } , bs2
= { 7, "\x02" };
982 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
986 /* diffrent lengths are diffrent */
989 heim_bmp_string bs1
= { 1, NULL
} , bs2
= { 0, NULL
};
991 ret
= der_heim_bmp_string_cmp(&bs1
, &bs2
);
995 /* diffrent lengths are diffrent */
998 heim_universal_string us1
= { 1, NULL
} , us2
= { 0, NULL
};
1000 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1006 uint32_t data
= (uint32_t)'a';
1007 heim_universal_string us1
= { 1, NULL
} , us2
= { 1, NULL
};
1010 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1019 corner_generalized_time(void)
1021 const char *str
= "760520140000Z";
1026 ret
= der_get_generalized_time((const unsigned char*)str
, strlen(str
),
1043 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
1051 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
1052 ret
= der_get_tag((const unsigned char*)tests
[i
].ptr
,
1053 tests
[i
].len
, &cl
, &ty
, &tag
, &size
);
1056 errx(1, "failed while shouldn't");
1059 errx(1, "passed while shouldn't");
1066 main(int argc
, char **argv
)
1070 ret
+= test_integer ();
1071 ret
+= test_integer_more();
1072 ret
+= test_unsigned ();
1073 ret
+= test_octet_string ();
1074 ret
+= test_bmp_string ();
1075 ret
+= test_universal_string ();
1076 ret
+= test_general_string ();
1077 ret
+= test_generalized_time ();
1079 ret
+= test_bit_string();
1080 ret
+= test_heim_integer();
1081 ret
+= test_boolean();
1083 ret
+= check_fail_unsigned();
1084 ret
+= check_fail_integer();
1085 ret
+= check_fail_length();
1086 ret
+= check_fail_boolean();
1087 ret
+= check_fail_general_string();
1088 ret
+= check_fail_bmp_string();
1089 ret
+= check_fail_universal_string();
1090 ret
+= check_fail_heim_integer();
1091 ret
+= check_fail_generalized_time();
1092 ret
+= check_fail_oid();
1093 ret
+= check_fail_bitstring();
1094 ret
+= test_heim_int_format();
1095 ret
+= test_heim_oid_format();
1096 ret
+= check_trailing_nul();
1097 ret
+= test_misc_cmp();
1098 ret
+= corner_generalized_time();
1099 ret
+= corner_tag();