blob: 7609ed128e5fd1adebad656a39f63168b03097c7 [file] [log] [blame]
vlm46ffdf62007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
vlm2e3dd3b2004-06-14 07:24:36 +00003#include <sys/time.h>
vlmfa67ddc2004-06-03 03:38:44 +00004
vlm46ffdf62007-07-23 06:48:26 +00005#include <OBJECT_IDENTIFIER.h>
6#include <RELATIVE-OID.h>
7
vlmfa67ddc2004-06-03 03:38:44 +00008static int
9_print(const void *buffer, size_t size, void *app_key) {
vlma97e2242004-06-14 07:40:17 +000010 (void)app_key;
vlmfa67ddc2004-06-03 03:38:44 +000011 fwrite(buffer, size, 1, stdout);
12 return 0;
13}
14
15static void
16check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
17 OBJECT_IDENTIFIER_t *oid;
vlm9de248e2004-10-20 15:50:55 +000018 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000019 unsigned long arcs[10];
20 int alen;
21 int i;
22
23 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000024 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000025 printf("} against {");
26 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
27 printf("}\n");
28
29 oid = NULL;
vlmef6355b2004-09-29 13:26:15 +000030 rval = ber_decode(0, &asn_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
vlmfa67ddc2004-06-03 03:38:44 +000031 assert(rval.code == RC_OK);
32
vlma97e2242004-06-14 07:40:17 +000033 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000034
35 /*
36 * Print the contents for visual debugging.
37 */
38 printf("OBJECT_IDENTIFIER_print() => ");
vlmef6355b2004-09-29 13:26:15 +000039 OBJECT_IDENTIFIER_print(&asn_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
vlmfa67ddc2004-06-03 03:38:44 +000040 printf("\n");
41
vlm12557712004-06-17 23:43:39 +000042 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000043 alen = OBJECT_IDENTIFIER_get_arcs(oid,
44 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000045 assert(alen > 0);
vlmfa67ddc2004-06-03 03:38:44 +000046
vlm903a2132004-08-22 03:01:37 +000047 printf("OBJECT_IDENTIFIER_get_arcs() => {");
vlmfa67ddc2004-06-03 03:38:44 +000048 /*
49 * Make sure they are equivalent.
50 */
vlmfa67ddc2004-06-03 03:38:44 +000051 for(i = 0; i < alen; i++) {
52 printf(" %lu", arcs[i]);
vlm903a2132004-08-22 03:01:37 +000053 if(alen == ck_len) {
54 assert(arcs[i] == (unsigned long)ck_buf[i]);
55 }
vlmfa67ddc2004-06-03 03:38:44 +000056 }
57 printf(" }\n");
vlm903a2132004-08-22 03:01:37 +000058 assert(alen == ck_len);
59
vlmfa67ddc2004-06-03 03:38:44 +000060}
61
62static void
63check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
64 RELATIVE_OID_t *oid;
vlm9de248e2004-10-20 15:50:55 +000065 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000066 unsigned long arcs[10];
67 int alen;
68 int i;
69
70 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000071 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000072 printf("} against {");
73 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
74 printf("}\n");
75
76 oid = NULL;
vlmef6355b2004-09-29 13:26:15 +000077 rval = ber_decode(0, &asn_DEF_RELATIVE_OID, (void *)&oid, buf, len);
vlmfa67ddc2004-06-03 03:38:44 +000078 assert(rval.code == RC_OK);
79
vlma97e2242004-06-14 07:40:17 +000080 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000081
82 /*
83 * Print the contents for visual debugging.
84 */
85 printf("RELATIVE_OID_print() => ");
vlmef6355b2004-09-29 13:26:15 +000086 RELATIVE_OID_print(&asn_DEF_RELATIVE_OID, oid, 0, _print, 0);
vlmfa67ddc2004-06-03 03:38:44 +000087 printf("\n");
88
vlm12557712004-06-17 23:43:39 +000089 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000090 alen = RELATIVE_OID_get_arcs(oid,
91 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000092 assert(alen > 0);
93 assert(alen == ck_len);
94
95 /*
96 * Make sure they are equivalent.
97 */
98 printf("RELATIVE_OID_get_arcs() => {");
99 for(i = 0; i < alen; i++) {
100 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000101 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000102 }
103 printf(" }\n");
104}
105
106/*
107 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
108 */
109static void
110check_REGEN(int *arcs, int acount) {
111 static RELATIVE_OID_t oid;
112 unsigned long tmp_arcs[10];
113 int tmp_alen = 10;
114 int alen;
115 int ret;
116 int i;
117
vlm735e4612006-07-13 09:22:34 +0000118 if(0) {
119 fprintf(stderr, "Encoding (R) {");
120 for(i = 0; i < acount; i++) {
121 fprintf(stderr, " %u", arcs[i]);
122 }
123 fprintf(stderr, " }\n");
vlmfa67ddc2004-06-03 03:38:44 +0000124 }
vlmfa67ddc2004-06-03 03:38:44 +0000125
vlm12557712004-06-17 23:43:39 +0000126 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000127 assert(ret == 0);
128
vlm12557712004-06-17 23:43:39 +0000129 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000130 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
131 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000132 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000133 assert(alen <= tmp_alen);
134 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000135
vlm735e4612006-07-13 09:22:34 +0000136 if(0) {
137 fprintf(stderr, "Encoded (R) { ");
138 for(i = 0; i < alen; i++) {
139 fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout);
140 assert(arcs[i] == (int)tmp_arcs[i]);
141 }
142 fprintf(stderr, "}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000143 }
vlm735e4612006-07-13 09:22:34 +0000144
vlmfa67ddc2004-06-03 03:38:44 +0000145}
146
147/*
148 * Encode the specified array of arcs as OBJECT IDENTIFIER,
149 * decode it and compare.
150 */
151static void
152check_REGEN_OID(int *arcs, int acount) {
153 static OBJECT_IDENTIFIER_t oid;
154 unsigned long tmp_arcs[10];
155 int tmp_alen = 10;
156 int alen;
157 int ret;
158 int i;
159
vlm735e4612006-07-13 09:22:34 +0000160 if(0) {
161 fprintf(stderr, "Encoding (O) {");
162 for(i = 0; i < acount; i++) {
163 fprintf(stderr, " %u", arcs[i]);
164 }
165 fprintf(stderr, " }\n");
vlmfa67ddc2004-06-03 03:38:44 +0000166 }
vlmfa67ddc2004-06-03 03:38:44 +0000167
vlm12557712004-06-17 23:43:39 +0000168 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000169 assert(ret == 0);
170
vlm12557712004-06-17 23:43:39 +0000171 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000172 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
173 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000174 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000175 assert(alen <= tmp_alen);
176 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000177
vlm735e4612006-07-13 09:22:34 +0000178 if(0) {
179 fprintf(stderr, "Encoded (O) { ");
180 for(i = 0; i < alen; i++) {
181 fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout);
182 assert(arcs[i] == (int)tmp_arcs[i]);
183 }
184 fprintf(stderr, "}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000185 }
vlmfa67ddc2004-06-03 03:38:44 +0000186}
vlm2e3dd3b2004-06-14 07:24:36 +0000187static int
188check_speed() {
189 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
190 int ret = 0;
191 int cycles = 100000000;
192 double a, b, c;
193 struct timeval tv;
194 unsigned long value;
195 int i;
196
vlma97e2242004-06-14 07:40:17 +0000197 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000198 assert(ret == 0);
199 assert(value == 0x7040c20d);
200
201 gettimeofday(&tv, 0);
202 a = tv.tv_sec + tv.tv_usec / 1000000.0;
203 for(i = 0; i < cycles; i++) {
204 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
205 &value, sizeof(value));
206 }
207 assert(ret == 0);
208 assert(value == 0x7040c20d);
209 gettimeofday(&tv, 0);
210 b = tv.tv_sec + tv.tv_usec / 1000000.0;
211 for(i = 0; i < cycles; i++) {
212 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
213 &value, sizeof(value));
214 }
215 assert(ret == 0);
216 assert(value == 0x7040c20d);
217 gettimeofday(&tv, 0);
218 c = tv.tv_sec + tv.tv_usec / 1000000.0;
219
220 a = b - a;
221 b = c - b;
222 printf("Time for single_arc(): %f\n", a);
223 printf("Time for get_arc_l(): %f\n", b);
224
225 return 0;
226}
227
vlm1f73df22004-10-23 10:16:51 +0000228static void check_parse(const char *oid_txt, int retval) {
229 int ret;
230 long l[2];
vlm31feaf32005-03-10 11:50:24 +0000231 const char *p;
vlm1f73df22004-10-23 10:16:51 +0000232
233 ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, l, 2, &p);
234 printf("[%s] => %d == %d\n", oid_txt, ret, retval);
235 assert(ret == retval);
236 assert(p >= oid_txt);
237}
238
239static void check_xer(int expect_arcs, char *xer) {
240 asn_dec_rval_t rc;
241 RELATIVE_OID_t *st = 0;
vlm170e42c2006-07-27 11:46:25 +0000242 RELATIVE_OID_t **stp = &st;
vlm1f73df22004-10-23 10:16:51 +0000243 long arcs[10];
244 int ret;
245 int i;
246
247 printf("[%s] => ", xer); fflush(stdout);
248 rc = asn_DEF_RELATIVE_OID.xer_decoder(0,
vlm170e42c2006-07-27 11:46:25 +0000249 &asn_DEF_RELATIVE_OID, (void **)stp, "t",
vlm1f73df22004-10-23 10:16:51 +0000250 xer, strlen(xer));
251 if(expect_arcs == -1) {
252 if(rc.code != RC_OK)
253 return;
254 }
255 assert(rc.code == RC_OK);
256
257 ret = RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs[0]),
258 sizeof(arcs)/sizeof(arcs[0]));
259 assert(ret < 10);
260 if(expect_arcs == -1) {
261 assert(ret == -1);
262 return;
263 }
264 for(i = 0; i < ret; i++) {
265 if(i) printf(".");
266 printf("%ld", arcs[i]);
267 assert(arcs[i] == i + 1);
268 }
269 printf(": %d == %d\n", ret, expect_arcs);
270 assert(ret == expect_arcs);
271}
272
vlmfa67ddc2004-06-03 03:38:44 +0000273#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
274 buf ## n ## _check, \
275 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
276#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
277 buf ## n ## _check, \
278 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
279#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
280 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
281#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
282 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
283
284int
vlma97e2242004-06-14 07:40:17 +0000285main() {
vlm12557712004-06-17 23:43:39 +0000286 int i;
287
vlm2e3dd3b2004-06-14 07:24:36 +0000288 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000289 uint8_t buf1[] = {
290 0x06, /* OBJECT IDENTIFIER */
291 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000292 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000293 };
vlm2e3dd3b2004-06-14 07:24:36 +0000294 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000295
296 /* {8571 3 2} */
297 uint8_t buf2[] = {
298 0x0D, /* RELATIVE-OID */
299 0x04, /* Length */
300 0xC2, 0x7B, 0x03, 0x02
301 };
302 int buf2_check[] = { 8571, 3, 2 };
303
vlm12557712004-06-17 23:43:39 +0000304 /* {joint-iso-itu-t 42 } */
305 uint8_t buf3[] = {
306 0x06, /* OBJECT IDENTIFIER */
307 0x01, /* Length */
308 0x7A
309 };
310 int buf3_check[] = { 2, 42 };
311
312 /* {joint-iso-itu-t 25957 } */
313 uint8_t buf4[] = {
314 0x06, /* OBJECT IDENTIFIER */
315 0x03, /* Length */
316 0x81, 0x80 + 0x4B, 0x35
317 };
318 int buf4_check[] = { 2, 25957 };
319
320 int buf5_check[] = { 0 };
321 int buf6_check[] = { 1 };
322 int buf7_check[] = { 80, 40 };
323 int buf8_check[] = { 127 };
324 int buf9_check[] = { 128 };
325 int buf10_check[] = { 65535, 65536 };
326 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
327 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
328 int buf13_check[] = { 0, 1, 2 };
329 int buf14_check[] = { 1, 38, 3 };
330 int buf15_check[] = { 0, 0, 0xf000 };
331 int buf16_check[] = { 0, 0, 0, 1, 0 };
332 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlm903a2132004-08-22 03:01:37 +0000333 int buf18_check[] = { 2, 2, 1, 1 };
334
335 /* { joint-iso-itu-t 2 1 1 } */
336 uint8_t buf19[] = {
337 0x06, /* OBJECT IDENTIFIER */
338 0x03, /* Length */
339 0x52, 0x01, 0x01
340 };
341 int buf19_check[] = { 2, 2, 1, 1 };
vlmfa67ddc2004-06-03 03:38:44 +0000342
vlm3fff06b2004-08-23 09:23:02 +0000343 /* { joint-iso-itu-t 2 1 0 1 } */
344 uint8_t buf20[] = {
345 0x06, /* OBJECT IDENTIFIER */
346 0x04, /* Length */
347 0x52, 0x01, 0x00, 0x01
348 };
349 int buf20_check[] = { 2, 2, 1, 0, 1 };
350
vlmfa67ddc2004-06-03 03:38:44 +0000351
352 CHECK_OID(1); /* buf1, buf1_check */
353 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000354 CHECK_OID(3); /* buf3, buf3_check */
355 CHECK_OID(4); /* buf4, buf4_check */
vlm903a2132004-08-22 03:01:37 +0000356 CHECK_OID(19); /* buf19, buf19_check */
vlm3fff06b2004-08-23 09:23:02 +0000357 CHECK_OID(20); /* buf20, buf20_check */
vlmfa67ddc2004-06-03 03:38:44 +0000358
vlm12557712004-06-17 23:43:39 +0000359 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000360 CHECK_REGEN(6);
361 CHECK_REGEN(7);
362 CHECK_REGEN(8);
363 CHECK_REGEN(9);
364 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000365 CHECK_REGEN(11);
366 CHECK_REGEN(12);
367 CHECK_REGEN(13);
368 CHECK_REGEN(14);
369 CHECK_REGEN(15);
370 CHECK_REGEN(16);
371 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000372 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000373 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
374 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000375 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000376 CHECK_REGEN_OID(14);
377 CHECK_REGEN_OID(15);
378 CHECK_REGEN_OID(16);
379 CHECK_REGEN_OID(17);
vlm903a2132004-08-22 03:01:37 +0000380 CHECK_REGEN_OID(18);
vlm3fff06b2004-08-23 09:23:02 +0000381 CHECK_REGEN_OID(19);
382 CHECK_REGEN_OID(20);
vlm12557712004-06-17 23:43:39 +0000383
vlm1f73df22004-10-23 10:16:51 +0000384 check_parse("", 0);
385 check_parse(" ", 0);
386 check_parse(" ", 0);
387 check_parse(".", -1);
388 check_parse(" .", -1);
389 check_parse(" 1", 1);
390 check_parse(" 1.2", 2);
391 check_parse(" 1.", -1);
392 check_parse(" 1. ", -1);
393 check_parse("1. ", -1);
394 check_parse("1.2", 2);
395 check_parse("10.30.234.234", 4);
396 check_parse("10.30.234.234 ", 4);
397 check_parse("10.30.234. 234 ", -1);
398 check_parse("10.30.234.234.", -1);
399 check_parse("1.2000000000.3", 3);
400 check_parse("1.2147483647.3", 3);
401 if(sizeof(long) == 4) {
402 check_parse("1.2147483648.3", -1); /* overflow on ILP32 */
403 check_parse("1.3000000000.3", -1);
404 check_parse("1.4000000000.3", -1);
405 check_parse("1.5000000000.3", -1);
406 check_parse("1.6000000000.3", -1);
407 check_parse("1.9000000000.3", -1);
408 } else {
409 check_parse("1.2147483648.3", 3);
410 }
411 check_parse("1.900a0000000.3", -1);
412 check_parse("1.900a.3", -1);
413
414 check_xer(0, "<t></t>");
415 check_xer(2, "<t>1.2</t>");
416 check_xer(3, "<t>1.2.3</t>");
417 check_xer(3, "<t> 1.2.3 </t>");
418 check_xer(-1, "<t>1.2.3 1</t>");
419
vlm12557712004-06-17 23:43:39 +0000420 for(i = 0; i < 100000; i++) {
421 int bufA_check[3] = { 2, i, rand() };
422 int bufB_check[2] = { rand(), i * 121 };
423 CHECK_REGEN(A);
424 CHECK_REGEN_OID(A);
425 CHECK_REGEN(B);
426 if(i > 100) i++;
427 if(i > 500) i++;
428 if(i > 1000) i += 3;
429 if(i > 5000) i += 151;
430 }
vlmfa67ddc2004-06-03 03:38:44 +0000431
vlm3717fb32004-06-14 08:17:27 +0000432 if(getenv("CHECK_SPEED")) {
433 /* Useful for developers only */
434 check_speed();
435 }
vlm2e3dd3b2004-06-14 07:24:36 +0000436
vlmfa67ddc2004-06-03 03:38:44 +0000437 return 0;
438}
vlm86380d32006-10-09 12:07:58 +0000439