blob: bacb618e75cf41b6efb787a256089adaf793ea4a [file] [log] [blame]
vlm46ffdf62007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
3
4#include <INTEGER.h>
vlmfa67ddc2004-06-03 03:38:44 +00005
vlm7d278c42004-07-19 17:30:25 +00006static char *shared_scratch_start;
7
8static 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
vlmfa67ddc2004-06-03 03:38:44 +000016static void
17check(uint8_t *buf, int size, long check_long, int check_ret) {
vlm7d278c42004-07-19 17:30:25 +000018 char scratch[128];
19 char verify[32];
vlmfa67ddc2004-06-03 03:38:44 +000020 INTEGER_t val;
vlm7d278c42004-07-19 17:30:25 +000021 uint8_t *buf_end = buf + size;
vlmfa67ddc2004-06-03 03:38:44 +000022 int ret;
23 long rlong = 123;
24
25 assert(buf);
26 assert(size >= 0);
27
28 val.buf = buf;
29 val.size = size;
30
vlm7d278c42004-07-19 17:30:25 +000031 printf("Testing: [");
32 for(; buf < buf_end; buf++) {
33 if(buf != val.buf) printf(":");
34 printf("%02x", *buf);
35 }
36 printf("]: ");
vlmfa67ddc2004-06-03 03:38:44 +000037
vlmef6355b2004-09-29 13:26:15 +000038 ret = asn_INTEGER2long(&val, &rlong);
vlm7d278c42004-07-19 17:30:25 +000039 printf(" (%ld, %d) vs (%ld, %d)\n",
vlmfa67ddc2004-06-03 03:38:44 +000040 rlong, ret, check_long, check_ret);
41 assert(ret == check_ret);
vlm6c593842004-10-26 09:03:31 +000042 printf("%ld %ld\n", rlong, check_long);
vlmfa67ddc2004-06-03 03:38:44 +000043 assert(rlong == check_long);
vlm7d278c42004-07-19 17:30:25 +000044
vlm19c3a192004-10-21 11:19:51 +000045 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
vlm7d278c42004-07-19 17:30:25 +000059 shared_scratch_start = scratch;
vlmef6355b2004-09-29 13:26:15 +000060 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
vlm7d278c42004-07-19 17:30:25 +000061 assert(shared_scratch_start < scratch + sizeof(scratch));
62 assert(ret == 0);
63 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
64 assert(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 }
vlmfa67ddc2004-06-03 03:38:44 +000075}
76
vlme4f9cac2004-10-21 12:11:57 +000077static void
78check_xer(int tofail, char *xmldata, long orig_value) {
79 INTEGER_t *st = 0;
80 asn_dec_rval_t rc;
81 long value;
82 int ret;
83
vlmcdc58f42004-10-21 12:23:41 +000084 printf("[%s] vs %ld: ", xmldata, orig_value);
vlme4f9cac2004-10-21 12:11:57 +000085
86 rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st,
87 xmldata, strlen(xmldata));
88 if(rc.code != RC_OK) {
89 assert(tofail);
90 printf("\tfailed, as expected\n");
91 return;
92 }
93 assert(!tofail);
94
95 ret = asn_INTEGER2long(st, &value);
96 assert(ret == 0);
97
98 printf("\t%ld\n", value);
99
100 assert(value == orig_value);
101
102 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
103}
104
vlmfa67ddc2004-06-03 03:38:44 +0000105int
106main(int ac, char **av) {
107 uint8_t buf1[] = { 1 };
108 uint8_t buf2[] = { 0xff };
109 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
110 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
111 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 };
112 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
113 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
114 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
115 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
116 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
vlm19c3a192004-10-21 11:19:51 +0000117 uint8_t buf11[] = { 0x80, 0, 0, 0 };
118 uint8_t buf12[] = { 0x80, 0 };
119 uint8_t buf13[] = { 0x80 };
vlmfa67ddc2004-06-03 03:38:44 +0000120
121#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
122
123 CHECK(buf1, 1, 0);
124 CHECK(buf2, -1, 0);
125 CHECK(buf3, -1, 0);
126 CHECK(buf4, -16, 0);
127 CHECK(buf5, 257, 0);
128 CHECK(buf6, 123, -1);
129 CHECK(buf7, 123, -1);
130 CHECK(buf8, 0x7F7E7D7C, 0);
131 CHECK(buf9, 0x7F7E7D7C, 0);
132 CHECK(buf10, 0x7F7E7D7C, 0);
vlme4c682a2005-08-15 01:02:47 +0000133 CHECK(buf11, -2147483647-1, 0); /* 0x80000000 */
vlm19c3a192004-10-21 11:19:51 +0000134 CHECK(buf12, -32768, 0);
135 CHECK(buf13, -128, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000136
vlme4f9cac2004-10-21 12:11:57 +0000137 check_xer(-1, "", 0);
138 check_xer(-1, "<INTEGER></INTEGER>", 0);
vlmcdc58f42004-10-21 12:23:41 +0000139 check_xer(-1, "<INTEGER> </INTEGER>", 0);
vlme4f9cac2004-10-21 12:11:57 +0000140 check_xer(-1, "<INTEGER>-</INTEGER>", 0);
141 check_xer(-1, "<INTEGER>+</INTEGER>", 0);
142 check_xer(-1, "<INTEGER>+-</INTEGER>", 0);
143 check_xer(0, "<INTEGER>+0</INTEGER>", 0);
144 check_xer(0, "<INTEGER>-0</INTEGER>", 0);
145 check_xer(0, "<INTEGER>+1</INTEGER>", 1);
146 check_xer(0, "<INTEGER>-1</INTEGER>", -1);
147 check_xer(0, "<INTEGER>1</INTEGER>", 1);
148 check_xer(0, "<INTEGER>-15</INTEGER>", -15);
149 check_xer(0, "<INTEGER>+15</INTEGER>", 15);
150 check_xer(0, "<INTEGER>15</INTEGER>", 15);
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(-1, "<INTEGER> +15 -</INTEGER>", 0);
156 check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0);
157 check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0);
158 check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0);
159 check_xer(0, "<INTEGER>1234</INTEGER>", 1234);
160 check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0);
161 check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647);
vlme4c682a2005-08-15 01:02:47 +0000162 check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483647-1);
vlme4f9cac2004-10-21 12:11:57 +0000163 check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647);
164 check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647);
vlm1069dac2004-10-23 10:17:02 +0000165 if(sizeof(long) == 4) {
166 check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0);
167 check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0);
168 check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0);
169 check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0);
170 check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */
171 check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0);
172 check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0);
173 }
vlme4f9cac2004-10-21 12:11:57 +0000174
vlmfa67ddc2004-06-03 03:38:44 +0000175 return 0;
176}