blob: 81aae6e5e45313db1da30473ccbe2c40f2d2f754 [file] [log] [blame]
Lev Walkin44212662004-08-19 13:26:54 +00001#include <INTEGER.c>
Lev Walkinc698ee52004-10-21 11:19:51 +00002#include <asn_codecs_prim.c>
Lev Walkin44212662004-08-19 13:26:54 +00003#include <ber_decoder.c>
4#include <ber_tlv_length.c>
5#include <ber_tlv_tag.c>
6#include <der_encoder.c>
Lev Walkinc698ee52004-10-21 11:19:51 +00007#include <xer_decoder.c>
8#include <xer_support.c>
Lev Walkin44212662004-08-19 13:26:54 +00009#include <constraints.c>
Lev Walkinf15320b2004-06-03 03:38:44 +000010
Lev Walkindb13f512004-07-19 17:30:25 +000011static char *shared_scratch_start;
12
13static int _print2buf(const void *buf, size_t size, void *key) {
14 (void)key;
15 memcpy(shared_scratch_start, buf, size);
16 shared_scratch_start += size;
17 *shared_scratch_start = '\0'; /* 0-termination */
18 return 0;
19}
20
Lev Walkinf15320b2004-06-03 03:38:44 +000021static void
22check(uint8_t *buf, int size, long check_long, int check_ret) {
Lev Walkindb13f512004-07-19 17:30:25 +000023 char scratch[128];
24 char verify[32];
Lev Walkinf15320b2004-06-03 03:38:44 +000025 INTEGER_t val;
Lev Walkindb13f512004-07-19 17:30:25 +000026 uint8_t *buf_end = buf + size;
Lev Walkinf15320b2004-06-03 03:38:44 +000027 int ret;
28 long rlong = 123;
29
30 assert(buf);
31 assert(size >= 0);
32
33 val.buf = buf;
34 val.size = size;
35
Lev Walkindb13f512004-07-19 17:30:25 +000036 printf("Testing: [");
37 for(; buf < buf_end; buf++) {
38 if(buf != val.buf) printf(":");
39 printf("%02x", *buf);
40 }
41 printf("]: ");
Lev Walkinf15320b2004-06-03 03:38:44 +000042
Lev Walkin5e033762004-09-29 13:26:15 +000043 ret = asn_INTEGER2long(&val, &rlong);
Lev Walkindb13f512004-07-19 17:30:25 +000044 printf(" (%ld, %d) vs (%ld, %d)\n",
Lev Walkinf15320b2004-06-03 03:38:44 +000045 rlong, ret, check_long, check_ret);
46 assert(ret == check_ret);
Lev Walkinf15320b2004-06-03 03:38:44 +000047 assert(rlong == check_long);
Lev Walkindb13f512004-07-19 17:30:25 +000048
Lev Walkinc698ee52004-10-21 11:19:51 +000049 if(check_ret == 0) {
50 INTEGER_t val2;
51 long rlong2;
52 val2.buf = 0;
53 val2.size = 0;
54 ret = asn_long2INTEGER(&val2, rlong);
55 assert(ret == 0);
56 assert(val2.buf);
57 assert(val2.size <= val.size); /* At least as compact */
58 ret = asn_INTEGER2long(&val, &rlong2);
59 assert(ret == 0);
60 assert(rlong == rlong2);
61 }
62
Lev Walkindb13f512004-07-19 17:30:25 +000063 shared_scratch_start = scratch;
Lev Walkin5e033762004-09-29 13:26:15 +000064 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
Lev Walkindb13f512004-07-19 17:30:25 +000065 assert(shared_scratch_start < scratch + sizeof(scratch));
66 assert(ret == 0);
67 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
68 assert(ret < sizeof(verify));
69 ret = strcmp(scratch, verify);
70 printf(" [%s] vs [%s]: %d%s\n",
71 scratch, verify, ret,
72 (check_ret == -1)?" (expected to fail)":""
73 );
74 if(check_ret == -1) {
75 assert(strcmp(scratch, verify));
76 } else {
77 assert(strcmp(scratch, verify) == 0);
78 }
Lev Walkinf15320b2004-06-03 03:38:44 +000079}
80
Lev Walkin0be3a992004-10-21 12:11:57 +000081static void
82check_xer(int tofail, char *xmldata, long orig_value) {
83 INTEGER_t *st = 0;
84 asn_dec_rval_t rc;
85 long value;
86 int ret;
87
88 printf("[%s] vs %ld:\n", xmldata, orig_value);
89
90 rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st,
91 xmldata, strlen(xmldata));
92 if(rc.code != RC_OK) {
93 assert(tofail);
94 printf("\tfailed, as expected\n");
95 return;
96 }
97 assert(!tofail);
98
99 ret = asn_INTEGER2long(st, &value);
100 assert(ret == 0);
101
102 printf("\t%ld\n", value);
103
104 assert(value == orig_value);
105
106 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
107}
108
Lev Walkinf15320b2004-06-03 03:38:44 +0000109int
110main(int ac, char **av) {
111 uint8_t buf1[] = { 1 };
112 uint8_t buf2[] = { 0xff };
113 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
114 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
115 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 };
116 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
117 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
118 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
119 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
120 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
Lev Walkinc698ee52004-10-21 11:19:51 +0000121 uint8_t buf11[] = { 0x80, 0, 0, 0 };
122 uint8_t buf12[] = { 0x80, 0 };
123 uint8_t buf13[] = { 0x80 };
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
126
127 CHECK(buf1, 1, 0);
128 CHECK(buf2, -1, 0);
129 CHECK(buf3, -1, 0);
130 CHECK(buf4, -16, 0);
131 CHECK(buf5, 257, 0);
132 CHECK(buf6, 123, -1);
133 CHECK(buf7, 123, -1);
134 CHECK(buf8, 0x7F7E7D7C, 0);
135 CHECK(buf9, 0x7F7E7D7C, 0);
136 CHECK(buf10, 0x7F7E7D7C, 0);
Lev Walkinc698ee52004-10-21 11:19:51 +0000137 CHECK(buf11, 0x80000000, 0);
138 CHECK(buf12, -32768, 0);
139 CHECK(buf13, -128, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000140
Lev Walkin0be3a992004-10-21 12:11:57 +0000141 check_xer(-1, "", 0);
142 check_xer(-1, "<INTEGER></INTEGER>", 0);
143 check_xer(-1, "<INTEGER>-</INTEGER>", 0);
144 check_xer(-1, "<INTEGER>+</INTEGER>", 0);
145 check_xer(-1, "<INTEGER>+-</INTEGER>", 0);
146 check_xer(0, "<INTEGER>+0</INTEGER>", 0);
147 check_xer(0, "<INTEGER>-0</INTEGER>", 0);
148 check_xer(0, "<INTEGER>+1</INTEGER>", 1);
149 check_xer(0, "<INTEGER>-1</INTEGER>", -1);
150 check_xer(0, "<INTEGER>1</INTEGER>", 1);
151 check_xer(0, "<INTEGER>-15</INTEGER>", -15);
152 check_xer(0, "<INTEGER>+15</INTEGER>", 15);
153 check_xer(0, "<INTEGER>15</INTEGER>", 15);
154 check_xer(0, "<INTEGER> 15</INTEGER>", 15);
155 check_xer(0, "<INTEGER> 15 </INTEGER>", 15);
156 check_xer(0, "<INTEGER>15 </INTEGER>", 15);
157 check_xer(0, "<INTEGER> +15 </INTEGER>", 15);
158 check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0);
159 check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0);
160 check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0);
161 check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0);
162 check_xer(0, "<INTEGER>1234</INTEGER>", 1234);
163 check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0);
164 check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647);
165 check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483648);
166 check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647);
167 check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647);
168 check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0);
169 check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0);
170 check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0);
171 check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0);
172 check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* unobvious */
173 check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0);
174 check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0);
175
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 return 0;
177}