blob: a7edb73eaa16b0178aa80e7a179c1a0068e77412 [file] [log] [blame]
Lev Walkin44212662004-08-19 13:26:54 +00001#include <INTEGER.c>
2#include <ber_decoder.c>
3#include <ber_tlv_length.c>
4#include <ber_tlv_tag.c>
5#include <der_encoder.c>
6#include <constraints.c>
Lev Walkinf15320b2004-06-03 03:38:44 +00007
Lev Walkindb13f512004-07-19 17:30:25 +00008static char *shared_scratch_start;
9
10static int _print2buf(const void *buf, size_t size, void *key) {
11 (void)key;
12 memcpy(shared_scratch_start, buf, size);
13 shared_scratch_start += size;
14 *shared_scratch_start = '\0'; /* 0-termination */
15 return 0;
16}
17
Lev Walkinf15320b2004-06-03 03:38:44 +000018static void
19check(uint8_t *buf, int size, long check_long, int check_ret) {
Lev Walkindb13f512004-07-19 17:30:25 +000020 char scratch[128];
21 char verify[32];
Lev Walkinf15320b2004-06-03 03:38:44 +000022 INTEGER_t val;
Lev Walkindb13f512004-07-19 17:30:25 +000023 uint8_t *buf_end = buf + size;
Lev Walkinf15320b2004-06-03 03:38:44 +000024 int ret;
25 long rlong = 123;
26
27 assert(buf);
28 assert(size >= 0);
29
30 val.buf = buf;
31 val.size = size;
32
Lev Walkindb13f512004-07-19 17:30:25 +000033 printf("Testing: [");
34 for(; buf < buf_end; buf++) {
35 if(buf != val.buf) printf(":");
36 printf("%02x", *buf);
37 }
38 printf("]: ");
Lev Walkinf15320b2004-06-03 03:38:44 +000039
40 ret = asn1_INTEGER2long(&val, &rlong);
Lev Walkindb13f512004-07-19 17:30:25 +000041 printf(" (%ld, %d) vs (%ld, %d)\n",
Lev Walkinf15320b2004-06-03 03:38:44 +000042 rlong, ret, check_long, check_ret);
43 assert(ret == check_ret);
Lev Walkinf15320b2004-06-03 03:38:44 +000044 assert(rlong == check_long);
Lev Walkindb13f512004-07-19 17:30:25 +000045
46 shared_scratch_start = scratch;
47 ret = INTEGER_print(&asn1_DEF_INTEGER, &val, 0, _print2buf, scratch);
48 assert(shared_scratch_start < scratch + sizeof(scratch));
49 assert(ret == 0);
50 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
51 assert(ret < sizeof(verify));
52 ret = strcmp(scratch, verify);
53 printf(" [%s] vs [%s]: %d%s\n",
54 scratch, verify, ret,
55 (check_ret == -1)?" (expected to fail)":""
56 );
57 if(check_ret == -1) {
58 assert(strcmp(scratch, verify));
59 } else {
60 assert(strcmp(scratch, verify) == 0);
61 }
Lev Walkinf15320b2004-06-03 03:38:44 +000062}
63
64int
65main(int ac, char **av) {
66 uint8_t buf1[] = { 1 };
67 uint8_t buf2[] = { 0xff };
68 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
69 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
70 uint8_t buf5[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
71 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
72 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
73 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
74 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
75 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
76
77#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
78
79 CHECK(buf1, 1, 0);
80 CHECK(buf2, -1, 0);
81 CHECK(buf3, -1, 0);
82 CHECK(buf4, -16, 0);
83 CHECK(buf5, 257, 0);
84 CHECK(buf6, 123, -1);
85 CHECK(buf7, 123, -1);
86 CHECK(buf8, 0x7F7E7D7C, 0);
87 CHECK(buf9, 0x7F7E7D7C, 0);
88 CHECK(buf10, 0x7F7E7D7C, 0);
89
90 return 0;
91}