blob: 0b2fbf6fde5157c50822f4fdfde16c8f1abccd8c [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 Walkin33700162004-10-26 09:03:31 +000047 printf("%ld %ld\n", rlong, check_long);
Lev Walkinf15320b2004-06-03 03:38:44 +000048 assert(rlong == check_long);
Lev Walkindb13f512004-07-19 17:30:25 +000049
Lev Walkinc698ee52004-10-21 11:19:51 +000050 if(check_ret == 0) {
51 INTEGER_t val2;
52 long rlong2;
53 val2.buf = 0;
54 val2.size = 0;
55 ret = asn_long2INTEGER(&val2, rlong);
56 assert(ret == 0);
57 assert(val2.buf);
58 assert(val2.size <= val.size); /* At least as compact */
59 ret = asn_INTEGER2long(&val, &rlong2);
60 assert(ret == 0);
61 assert(rlong == rlong2);
62 }
63
Lev Walkindb13f512004-07-19 17:30:25 +000064 shared_scratch_start = scratch;
Lev Walkin5e033762004-09-29 13:26:15 +000065 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
Lev Walkindb13f512004-07-19 17:30:25 +000066 assert(shared_scratch_start < scratch + sizeof(scratch));
67 assert(ret == 0);
68 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
69 assert(ret < sizeof(verify));
70 ret = strcmp(scratch, verify);
71 printf(" [%s] vs [%s]: %d%s\n",
72 scratch, verify, ret,
73 (check_ret == -1)?" (expected to fail)":""
74 );
75 if(check_ret == -1) {
76 assert(strcmp(scratch, verify));
77 } else {
78 assert(strcmp(scratch, verify) == 0);
79 }
Lev Walkinf15320b2004-06-03 03:38:44 +000080}
81
Lev Walkin0be3a992004-10-21 12:11:57 +000082static void
83check_xer(int tofail, char *xmldata, long orig_value) {
84 INTEGER_t *st = 0;
85 asn_dec_rval_t rc;
86 long value;
87 int ret;
88
Lev Walkinbe03a2f2004-10-21 12:23:41 +000089 printf("[%s] vs %ld: ", xmldata, orig_value);
Lev Walkin0be3a992004-10-21 12:11:57 +000090
91 rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st,
92 xmldata, strlen(xmldata));
93 if(rc.code != RC_OK) {
94 assert(tofail);
95 printf("\tfailed, as expected\n");
96 return;
97 }
98 assert(!tofail);
99
100 ret = asn_INTEGER2long(st, &value);
101 assert(ret == 0);
102
103 printf("\t%ld\n", value);
104
105 assert(value == orig_value);
106
107 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
108}
109
Lev Walkinf15320b2004-06-03 03:38:44 +0000110int
111main(int ac, char **av) {
112 uint8_t buf1[] = { 1 };
113 uint8_t buf2[] = { 0xff };
114 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
115 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
116 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 };
117 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
118 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
119 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
120 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
121 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
Lev Walkinc698ee52004-10-21 11:19:51 +0000122 uint8_t buf11[] = { 0x80, 0, 0, 0 };
123 uint8_t buf12[] = { 0x80, 0 };
124 uint8_t buf13[] = { 0x80 };
Lev Walkinf15320b2004-06-03 03:38:44 +0000125
126#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
127
128 CHECK(buf1, 1, 0);
129 CHECK(buf2, -1, 0);
130 CHECK(buf3, -1, 0);
131 CHECK(buf4, -16, 0);
132 CHECK(buf5, 257, 0);
133 CHECK(buf6, 123, -1);
134 CHECK(buf7, 123, -1);
135 CHECK(buf8, 0x7F7E7D7C, 0);
136 CHECK(buf9, 0x7F7E7D7C, 0);
137 CHECK(buf10, 0x7F7E7D7C, 0);
Lev Walkin33700162004-10-26 09:03:31 +0000138 CHECK(buf11, -2147483648, 0); /* 0x80000000 */
Lev Walkinc698ee52004-10-21 11:19:51 +0000139 CHECK(buf12, -32768, 0);
140 CHECK(buf13, -128, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000141
Lev Walkin0be3a992004-10-21 12:11:57 +0000142 check_xer(-1, "", 0);
143 check_xer(-1, "<INTEGER></INTEGER>", 0);
Lev Walkinbe03a2f2004-10-21 12:23:41 +0000144 check_xer(-1, "<INTEGER> </INTEGER>", 0);
Lev Walkin0be3a992004-10-21 12:11:57 +0000145 check_xer(-1, "<INTEGER>-</INTEGER>", 0);
146 check_xer(-1, "<INTEGER>+</INTEGER>", 0);
147 check_xer(-1, "<INTEGER>+-</INTEGER>", 0);
148 check_xer(0, "<INTEGER>+0</INTEGER>", 0);
149 check_xer(0, "<INTEGER>-0</INTEGER>", 0);
150 check_xer(0, "<INTEGER>+1</INTEGER>", 1);
151 check_xer(0, "<INTEGER>-1</INTEGER>", -1);
152 check_xer(0, "<INTEGER>1</INTEGER>", 1);
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(0, "<INTEGER>15 </INTEGER>", 15);
159 check_xer(0, "<INTEGER> +15 </INTEGER>", 15);
160 check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0);
161 check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0);
162 check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0);
163 check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0);
164 check_xer(0, "<INTEGER>1234</INTEGER>", 1234);
165 check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0);
166 check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647);
167 check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483648);
168 check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647);
169 check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647);
Lev Walkine59f7672004-10-23 10:17:02 +0000170 if(sizeof(long) == 4) {
171 check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0);
172 check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0);
173 check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0);
174 check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0);
175 check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */
176 check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0);
177 check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0);
178 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000179
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 return 0;
181}