Debian Codegraph @

skeletons/tests/check-INTEGER.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <assert.h>
3
4 #include <INTEGER.h>
5
6 static char *shared_scratch_start;
7
8 static int _print2buf(const void *buf, size_t size, void *key) {
9 (void)key;
10 memcpy(shared_scratch_start, buf, size);
11 shared_scratch_start += size;
12 *shared_scratch_start = '\0'; /* 0-termination */
13 return 0;
14 }
15
16 static void
17 check(uint8_t *buf, int size, long check_long, int check_ret) {
18 char scratch[128];
19 char verify[32];
20 INTEGER_t val;
21 uint8_t *buf_end = buf + size;
22 int ret;
23 long rlong = 123;
24
25 assert(buf);
26 assert(size >= 0);
27
28 val.buf = buf;
29 val.size = size;
30
31 printf("Testing: [");
32 for(; buf < buf_end; buf++) {
33 if(buf != val.buf) printf(":");
34 printf("%02x", *buf);
35 }
36 printf("]: ");
37
38 ret = asn_INTEGER2long(&val, &rlong);
39 printf(" (%ld, %d) vs (%ld, %d)\n",
40 rlong, ret, check_long, check_ret);
41 assert(ret == check_ret);
42 printf("%ld %ld\n", rlong, check_long);
43 assert(rlong == check_long);
44
45 if(check_ret == 0) {
46 INTEGER_t val2;
47 long rlong2;
48 val2.buf = 0;
49 val2.size = 0;
50 ret = asn_long2INTEGER(&val2, rlong);
51 assert(ret == 0);
52 assert(val2.buf);
53 assert(val2.size <= val.size); /* At least as compact */
54 ret = asn_INTEGER2long(&val, &rlong2);
55 assert(ret == 0);
56 assert(rlong == rlong2);
57 }
58
59 shared_scratch_start = scratch;
60 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
61 assert(shared_scratch_start < scratch + sizeof(scratch));
62 assert(ret == 0);
63 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
64 assert(ret < 0 || (size_t)ret < sizeof(verify));
65 ret = strcmp(scratch, verify);
66 printf(" [%s] vs [%s]: %d%s\n",
67 scratch, verify, ret,
68 (check_ret == -1)?" (expected to fail)":""
69 );
70 if(check_ret == -1) {
71 assert(strcmp(scratch, verify));
72 } else {
73 assert(strcmp(scratch, verify) == 0);
74 }
75 }
76
77 static void
78 check_unsigned(uint8_t *buf, int size, unsigned long check_long, int check_ret) {
79 char scratch[128];
80 char verify[32];
81 INTEGER_t val;
82 uint8_t *buf_end = buf + size;
83 int ret;
84 unsigned long rlong = 123;
85
86 assert(buf);
87 assert(size >= 0);
88
89 val.buf = buf;
90 val.size = size;
91
92 printf("Testing: [");
93 for(; buf < buf_end; buf++) {
94 if(buf != val.buf) printf(":");
95 printf("%02x", *buf);
96 }
97 printf("]: ");
98
99 ret = asn_INTEGER2ulong(&val, &rlong);
100 printf(" (%lu, %d) vs (%lu, %d)\n",
101 rlong, ret, check_long, check_ret);
102 assert(ret == check_ret);
103 assert(rlong == check_long);
104
105 if(check_ret == 0) {
106 INTEGER_t val2;
107 unsigned long rlong2;
108 val2.buf = 0;
109 val2.size = 0;
110 ret = asn_ulong2INTEGER(&val2, rlong);
111 assert(ret == 0);
112 assert(val2.buf);
113 if(val2.size > val.size) {
114 /* At least as compact */
115 printf("val2.size=%d, val.size=%d\n",
116 (int)val2.size, (int)val.size);
117 assert(val2.size <= val.size);
118 }
119 ret = asn_INTEGER2ulong(&val, &rlong2);
120 assert(ret == 0);
121 assert(rlong == rlong2);
122 }
123
124 return;
125
126 shared_scratch_start = scratch;
127 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
128 assert(shared_scratch_start < scratch + sizeof(scratch));
129 assert(ret == 0);
130 ret = snprintf(verify, sizeof(verify), "%lu", check_long);
131 assert(ret < sizeof(verify));
132 ret = strcmp(scratch, verify);
133 printf(" [%s] vs [%s]: %d%s\n",
134 scratch, verify, ret,
135 (check_ret == -1)?" (expected to fail)":""
136 );
137 if(check_ret == -1) {
138 assert(strcmp(scratch, verify));
139 } else {
140 assert(strcmp(scratch, verify) == 0);
141 }
142 }
143
144 static void
145 check_xer(int tofail, char *xmldata, long orig_value) {
146 INTEGER_t *st = 0;
147 asn_dec_rval_t rc;
148 long value;
149 int ret;
150
151 printf("[%s] vs %ld: ", xmldata, orig_value);
152
153 rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st,
154 xmldata, strlen(xmldata));
155 if(rc.code != RC_OK) {
156 assert(tofail);
157 printf("\tfailed, as expected\n");
158 return;
159 }
160 assert(!tofail);
161
162 ret = asn_INTEGER2long(st, &value);
163 assert(ret == 0);
164
165 printf("\t%ld\n", value);
166
167 assert(value == orig_value);
168
169 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
170 }
171
172 int
173 main() {
174 uint8_t buf1[] = { 1 };
175 uint8_t buf2[] = { 0xff };
176 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
177 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
178 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 };
179 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
180 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
181 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
182 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
183 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
184 uint8_t buf11[] = { 0x80, 0, 0, 0 };
185 uint8_t buf12[] = { 0x80, 0 };
186 uint8_t buf13[] = { 0x80 };
187 uint8_t buf14[] = { 0x00, 0x80, 0x00, 0x00 };
188 uint8_t buf15[] = { 0x00, 0x80, 0x00, 0x00, 0x00 };
189 uint8_t buf16[] = { 0x00, 0xff, 0xff, 0x00, 0x00 };
190
191 #define UCHECK(buf, val, ret) check_unsigned(buf, sizeof(buf), val, ret)
192
193 #define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
194
195 CHECK(buf1, 1, 0);
196 CHECK(buf2, -1, 0);
197 CHECK(buf3, -1, 0);
198 CHECK(buf4, -16, 0);
199 CHECK(buf5, 257, 0);
200 CHECK(buf6, 123, -1);
201 CHECK(buf7, 123, -1);
202 CHECK(buf8, 0x7F7E7D7C, 0);
203 CHECK(buf9, 0x7F7E7D7C, 0);
204 CHECK(buf10, 0x7F7E7D7C, 0);
205 UCHECK(buf10, 0x7F7E7D7C, 0);
206 CHECK(buf11, -2147483647-1, 0); /* 0x80000000 */
207 CHECK(buf12, -32768, 0);
208 CHECK(buf13, -128, 0);
209 UCHECK(buf14, 0x800000, 0);
210 UCHECK(buf15, 0x80000000UL, 0);
211 UCHECK(buf16, 0xffff0000UL, 0);
212
213 check_xer(-1, "", 0);
214 check_xer(-1, "<INTEGER></INTEGER>", 0);
215 check_xer(-1, "<INTEGER> </INTEGER>", 0);
216 check_xer(-1, "<INTEGER>-</INTEGER>", 0);
217 check_xer(-1, "<INTEGER>+</INTEGER>", 0);
218 check_xer(-1, "<INTEGER>+-</INTEGER>", 0);
219 check_xer(-1, "<INTEGER> -</INTEGER>", 0);
220 check_xer(-1, "<INTEGER> +</INTEGER>", 0);
221 check_xer(-1, "<INTEGER> +-</INTEGER>", 0);
222 check_xer(-1, "<INTEGER>- </INTEGER>", 0);
223 check_xer(-1, "<INTEGER>+ </INTEGER>", 0);
224 check_xer(-1, "<INTEGER>+- </INTEGER>", 0);
225 check_xer(-1, "<INTEGER> - </INTEGER>", 0);
226 check_xer(-1, "<INTEGER> + </INTEGER>", 0);
227 check_xer(-1, "<INTEGER> +- </INTEGER>", 0);
228 check_xer(0, "<INTEGER>+0</INTEGER>", 0);
229 check_xer(0, "<INTEGER>-0</INTEGER>", 0);
230 check_xer(0, "<INTEGER>+1</INTEGER>", 1);
231 check_xer(0, "<INTEGER>-1</INTEGER>", -1);
232 check_xer(0, "<INTEGER>1</INTEGER>", 1);
233 check_xer(0, "<INTEGER>-15</INTEGER>", -15);
234 check_xer(0, "<INTEGER>+15</INTEGER>", 15);
235 check_xer(0, "<INTEGER>15</INTEGER>", 15);
236 check_xer(0, "<INTEGER> 15</INTEGER>", 15);
237 check_xer(0, "<INTEGER> 15 </INTEGER>", 15);
238 check_xer(0, "<INTEGER>15 </INTEGER>", 15);
239 check_xer(0, "<INTEGER> +15 </INTEGER>", 15);
240 check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0);
241 check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0);
242 check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0);
243 check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0);
244 check_xer(-1, "<INTEGER>12 <z>34</INTEGER>", 0);
245 check_xer(-1, "<INTEGER>12 <z></INTEGER>", 0);
246 check_xer(0, "<INTEGER>1234</INTEGER>", 1234);
247 check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0);
248 check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647);
249 check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483647-1);
250 check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647);
251 check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647);
252 if(sizeof(long) == 4) {
253 check_xer( 0, "<INTEGER>-2147483648</INTEGER>", -2147483648);
254 check_xer(-1, "<INTEGER>-2147483649</INTEGER>", 0);
255 check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0);
256 check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0);
257 check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0);
258 check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0);
259 check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */
260 check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0);
261 check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0);
262 check_xer(-1, "<INTEGER>-5147483649</INTEGER>", 0);/* special */
263 check_xer(-1, "<INTEGER>-9147483649</INTEGER>", 0);
264 check_xer(-1, "<INTEGER>-9999999999</INTEGER>", 0);
265 }
266 if(sizeof(long) == 8) {
267 check_xer(0, "<INTEGER>2147483648</INTEGER>", 2147483648);
268 check_xer(0, "<INTEGER>2147483649</INTEGER>", 2147483649);
269 check_xer(0, "<INTEGER>3147483649</INTEGER>", 3147483649);
270 check_xer(0, "<INTEGER>4147483649</INTEGER>", 4147483649);
271 check_xer(0, "<INTEGER>5147483649</INTEGER>", 5147483649);
272 check_xer(0, "<INTEGER>9147483649</INTEGER>", 9147483649);
273 check_xer(0, "<INTEGER>9999999999</INTEGER>", 9999999999);
274 check_xer(0, "<INTEGER>9223372036854775807</INTEGER>", 9223372036854775807);
275 check_xer(-1, "<INTEGER>9223372036854775808</INTEGER>", 0);
276 check_xer(-1, "<INTEGER>10223372036854775807</INTEGER>", 0);
277 check_xer(-1, "<INTEGER>50223372036854775807</INTEGER>", 0);
278 check_xer(-1, "<INTEGER>100223372036854775807</INTEGER>", 0);
279 check_xer(-1, "<INTEGER>500223372036854775807</INTEGER>", 0);
280 check_xer(0, "<INTEGER>-9223372036854775808</INTEGER>", -9223372036854775807-1);
281 check_xer(-1, "<INTEGER>-9223372036854775809</INTEGER>", 0);
282 check_xer(-1, "<INTEGER>-10223372036854775807</INTEGER>", 0);
283 check_xer(-1, "<INTEGER>-50223372036854775807</INTEGER>", 0);
284 check_xer(-1, "<INTEGER>-100223372036854775807</INTEGER>", 0);
285 check_xer(-1, "<INTEGER>-500223372036854775807</INTEGER>", 0);
286 }
287
288 return 0;
289 }