aboutsummaryrefslogtreecommitdiffstats
path: root/skeletons/tests/check-INTEGER.c
blob: 4ec9fd50c095ac000a9f5768b6e0cc5369d9b398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <assert.h>

#include <INTEGER.h>

static char *shared_scratch_start;

static int _print2buf(const void *buf, size_t size, void *key) {
	(void)key;
	memcpy(shared_scratch_start, buf, size);
	shared_scratch_start += size;
	*shared_scratch_start = '\0';	/* 0-termination */
	return 0;
}

static void
check(uint8_t *buf, int size, long check_long, int check_ret) {
	char scratch[128];
	char verify[32];
	INTEGER_t val;
	uint8_t *buf_end = buf + size;
	int ret;
	long rlong = 123;

	assert(buf);
	assert(size >= 0);

	val.buf = buf;
	val.size = size;

	printf("Testing: [");
	for(; buf < buf_end; buf++) {
		if(buf != val.buf) printf(":");
		printf("%02x", *buf);
	}
	printf("]: ");

	ret = asn_INTEGER2long(&val, &rlong);
	printf(" (%ld, %d) vs (%ld, %d)\n",
		rlong, ret, check_long, check_ret);
	assert(ret == check_ret);
	printf("%ld %ld\n", rlong, check_long);
	assert(rlong == check_long);

	if(check_ret == 0) {
		INTEGER_t val2;
		long rlong2;
		val2.buf = 0;
		val2.size = 0;
		ret = asn_long2INTEGER(&val2, rlong);
		assert(ret == 0);
		assert(val2.buf);
		assert(val2.size <= val.size);	/* At least as compact */
		ret = asn_INTEGER2long(&val, &rlong2);
		assert(ret == 0);
		assert(rlong == rlong2);
	}

	shared_scratch_start = scratch;
	ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
	assert(shared_scratch_start < scratch + sizeof(scratch));
	assert(ret == 0);
	ret = snprintf(verify, sizeof(verify), "%ld", check_long);
	assert(ret < 0 || (size_t)ret < sizeof(verify));
	ret = strcmp(scratch, verify);
	printf("         [%s] vs [%s]: %d%s\n",
		scratch, verify, ret,
		(check_ret == -1)?" (expected to fail)":""
		);
	if(check_ret == -1) {
		assert(strcmp(scratch, verify));
	} else {
		assert(strcmp(scratch, verify) == 0);
	}
}

static void
check_unsigned(uint8_t *buf, int size, unsigned long check_long, int check_ret) {
	char scratch[128];
	char verify[32];
	INTEGER_t val;
	uint8_t *buf_end = buf + size;
	int ret;
	unsigned long rlong = 123;

	assert(buf);
	assert(size >= 0);

	val.buf = buf;
	val.size = size;

	printf("Testing: [");
	for(; buf < buf_end; buf++) {
		if(buf != val.buf) printf(":");
		printf("%02x", *buf);
	}
	printf("]: ");

	ret = asn_INTEGER2ulong(&val, &rlong);
	printf(" (%lu, %d) vs (%lu, %d)\n",
		rlong, ret, check_long, check_ret);
	assert(ret == check_ret);
	assert(rlong == check_long);

	if(check_ret == 0) {
		INTEGER_t val2;
		unsigned long rlong2;
		val2.buf = 0;
		val2.size = 0;
		ret = asn_ulong2INTEGER(&val2, rlong);
		assert(ret == 0);
		assert(val2.buf);
		if(val2.size > val.size) {
			/* At least as compact */
			printf("val2.size=%d, val.size=%d\n",
				(int)val2.size, (int)val.size);
			assert(val2.size <= val.size);
		}
		ret = asn_INTEGER2ulong(&val, &rlong2);
		assert(ret == 0);
		assert(rlong == rlong2);
	}

	return;

	shared_scratch_start = scratch;
	ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
	assert(shared_scratch_start < scratch + sizeof(scratch));
	assert(ret == 0);
	ret = snprintf(verify, sizeof(verify), "%lu", check_long);
	assert(ret < sizeof(verify));
	ret = strcmp(scratch, verify);
	printf("         [%s] vs [%s]: %d%s\n",
		scratch, verify, ret,
		(check_ret == -1)?" (expected to fail)":""
		);
	if(check_ret == -1) {
		assert(strcmp(scratch, verify));
	} else {
		assert(strcmp(scratch, verify) == 0);
	}
}

static void
check_xer(int tofail, char *xmldata, long orig_value) {
	INTEGER_t *st = 0;
	asn_dec_rval_t rc;
	long value;
	int ret;

	printf("[%s] vs %ld: ", xmldata, orig_value);

	rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st,
		xmldata, strlen(xmldata));
	if(rc.code != RC_OK) {
		assert(tofail);
		printf("\tfailed, as expected\n");
		return;
	}
	assert(!tofail);

	ret = asn_INTEGER2long(st, &value);
	assert(ret == 0);

	printf("\t%ld\n", value);

	assert(value == orig_value);

	asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
}

int
main() {
	uint8_t buf1[] = { 1 };
	uint8_t buf2[] = { 0xff };
	uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
	uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
	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 };
	uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
	uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
	uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
	uint8_t buf11[] = { 0x80, 0, 0, 0 };
	uint8_t buf12[] = { 0x80, 0 };
	uint8_t buf13[] = { 0x80 };
	uint8_t buf14[] = { 0x00, 0x80, 0x00, 0x00 };
	uint8_t buf15[] = { 0x00, 0x80, 0x00, 0x00, 0x00 };
	uint8_t buf16[] = { 0x00, 0xff, 0xff, 0x00, 0x00 };

#define	UCHECK(buf, val, ret)	check_unsigned(buf, sizeof(buf), val, ret)

#define	CHECK(buf, val, ret)	check(buf, sizeof(buf), val, ret)

	CHECK(buf1, 1, 0);
	CHECK(buf2, -1, 0);
	CHECK(buf3, -1, 0);
	CHECK(buf4, -16, 0);
	CHECK(buf5, 257, 0);
	CHECK(buf6, 123, -1);
	CHECK(buf7, 123, -1);
	CHECK(buf8, 0x7F7E7D7C, 0);
	CHECK(buf9, 0x7F7E7D7C, 0);
	CHECK(buf10, 0x7F7E7D7C, 0);
	UCHECK(buf10, 0x7F7E7D7C, 0);
	CHECK(buf11, -2147483647-1, 0);	/* 0x80000000 */
	CHECK(buf12, -32768, 0);
	CHECK(buf13, -128, 0);
	UCHECK(buf14, 0x800000, 0);
	UCHECK(buf15, 0x80000000UL, 0);
	UCHECK(buf16, 0xffff0000UL, 0);

	check_xer(-1, "", 0);
	check_xer(-1, "<INTEGER></INTEGER>", 0);
	check_xer(-1, "<INTEGER> </INTEGER>", 0);
	check_xer(-1, "<INTEGER>-</INTEGER>", 0);
	check_xer(-1, "<INTEGER>+</INTEGER>", 0);
	check_xer(-1, "<INTEGER>+-</INTEGER>", 0);
	check_xer(-1, "<INTEGER> -</INTEGER>", 0);
	check_xer(-1, "<INTEGER> +</INTEGER>", 0);
	check_xer(-1, "<INTEGER> +-</INTEGER>", 0);
	check_xer(-1, "<INTEGER>- </INTEGER>", 0);
	check_xer(-1, "<INTEGER>+ </INTEGER>", 0);
	check_xer(-1, "<INTEGER>+- </INTEGER>", 0);
	check_xer(-1, "<INTEGER> - </INTEGER>", 0);
	check_xer(-1, "<INTEGER> + </INTEGER>", 0);
	check_xer(-1, "<INTEGER> +- </INTEGER>", 0);
	check_xer(0, "<INTEGER>+0</INTEGER>", 0);
	check_xer(0, "<INTEGER>-0</INTEGER>", 0);
	check_xer(0, "<INTEGER>+1</INTEGER>", 1);
	check_xer(0, "<INTEGER>-1</INTEGER>", -1);
	check_xer(0, "<INTEGER>1</INTEGER>", 1);
	check_xer(0, "<INTEGER>-15</INTEGER>", -15);
	check_xer(0, "<INTEGER>+15</INTEGER>", 15);
	check_xer(0, "<INTEGER>15</INTEGER>", 15);
	check_xer(0, "<INTEGER> 15</INTEGER>", 15);
	check_xer(0, "<INTEGER> 15 </INTEGER>", 15);
	check_xer(0, "<INTEGER>15 </INTEGER>", 15);
	check_xer(0, "<INTEGER> +15 </INTEGER>", 15);
	check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0);
	check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0);
	check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0);
	check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0);
	check_xer(-1, "<INTEGER>12 <z>34</INTEGER>", 0);
	check_xer(-1, "<INTEGER>12 <z></INTEGER>", 0);
	check_xer(0, "<INTEGER>1234</INTEGER>", 1234);
	check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0);
	check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647);
	check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483647-1);
	check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647);
	check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647);
	if(sizeof(long) == 4) {
		check_xer( 0, "<INTEGER>-2147483648</INTEGER>", -2147483648);
		check_xer(-1, "<INTEGER>-2147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0);
		check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */
		check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-5147483649</INTEGER>", 0);/* special */
		check_xer(-1, "<INTEGER>-9147483649</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-9999999999</INTEGER>", 0);
	}
	if(sizeof(long) == 8) {
		check_xer(0, "<INTEGER>2147483648</INTEGER>", 2147483648);
		check_xer(0, "<INTEGER>2147483649</INTEGER>", 2147483649);
		check_xer(0, "<INTEGER>3147483649</INTEGER>", 3147483649);
		check_xer(0, "<INTEGER>4147483649</INTEGER>", 4147483649);
		check_xer(0, "<INTEGER>5147483649</INTEGER>", 5147483649);
		check_xer(0, "<INTEGER>9147483649</INTEGER>", 9147483649);
		check_xer(0, "<INTEGER>9999999999</INTEGER>", 9999999999);
		check_xer(0, "<INTEGER>9223372036854775807</INTEGER>", 9223372036854775807);
		check_xer(-1, "<INTEGER>9223372036854775808</INTEGER>", 0);
		check_xer(-1, "<INTEGER>10223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>50223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>100223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>500223372036854775807</INTEGER>", 0);
		check_xer(0, "<INTEGER>-9223372036854775808</INTEGER>", -9223372036854775807-1);
		check_xer(-1, "<INTEGER>-9223372036854775809</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-10223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-50223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-100223372036854775807</INTEGER>", 0);
		check_xer(-1, "<INTEGER>-500223372036854775807</INTEGER>", 0);
	}

	return 0;
}