blob: a28cd00e8afe3c0aa63a306f6d62cce1bf48c9e7 [file] [log] [blame]
vlmd3da8512004-08-19 13:26:54 +00001#include <OBJECT_IDENTIFIER.c>
2#include <RELATIVE-OID.c>
vlm69be6fa2004-10-21 12:23:47 +00003#include <asn_codecs_prim.c>
vlmd3da8512004-08-19 13:26:54 +00004#include <ber_decoder.c>
5#include <ber_tlv_length.c>
6#include <ber_tlv_tag.c>
7#include <der_encoder.c>
vlm69be6fa2004-10-21 12:23:47 +00008#include <xer_decoder.c>
9#include <xer_support.c>
vlmd3da8512004-08-19 13:26:54 +000010#include <constraints.c>
11
vlm2e3dd3b2004-06-14 07:24:36 +000012#include <sys/time.h>
vlmfa67ddc2004-06-03 03:38:44 +000013
14static int
15_print(const void *buffer, size_t size, void *app_key) {
vlma97e2242004-06-14 07:40:17 +000016 (void)app_key;
vlmfa67ddc2004-06-03 03:38:44 +000017 fwrite(buffer, size, 1, stdout);
18 return 0;
19}
20
21static void
22check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
23 OBJECT_IDENTIFIER_t *oid;
vlm9de248e2004-10-20 15:50:55 +000024 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000025 unsigned long arcs[10];
26 int alen;
27 int i;
28
29 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000030 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000031 printf("} against {");
32 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
33 printf("}\n");
34
35 oid = NULL;
vlmef6355b2004-09-29 13:26:15 +000036 rval = ber_decode(0, &asn_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
vlmfa67ddc2004-06-03 03:38:44 +000037 assert(rval.code == RC_OK);
38
vlma97e2242004-06-14 07:40:17 +000039 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000040
41 /*
42 * Print the contents for visual debugging.
43 */
44 printf("OBJECT_IDENTIFIER_print() => ");
vlmef6355b2004-09-29 13:26:15 +000045 OBJECT_IDENTIFIER_print(&asn_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
vlmfa67ddc2004-06-03 03:38:44 +000046 printf("\n");
47
vlm12557712004-06-17 23:43:39 +000048 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000049 alen = OBJECT_IDENTIFIER_get_arcs(oid,
50 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000051 assert(alen > 0);
vlmfa67ddc2004-06-03 03:38:44 +000052
vlm903a2132004-08-22 03:01:37 +000053 printf("OBJECT_IDENTIFIER_get_arcs() => {");
vlmfa67ddc2004-06-03 03:38:44 +000054 /*
55 * Make sure they are equivalent.
56 */
vlmfa67ddc2004-06-03 03:38:44 +000057 for(i = 0; i < alen; i++) {
58 printf(" %lu", arcs[i]);
vlm903a2132004-08-22 03:01:37 +000059 if(alen == ck_len) {
60 assert(arcs[i] == (unsigned long)ck_buf[i]);
61 }
vlmfa67ddc2004-06-03 03:38:44 +000062 }
63 printf(" }\n");
vlm903a2132004-08-22 03:01:37 +000064 assert(alen == ck_len);
65
vlmfa67ddc2004-06-03 03:38:44 +000066}
67
68static void
69check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
70 RELATIVE_OID_t *oid;
vlm9de248e2004-10-20 15:50:55 +000071 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000072 unsigned long arcs[10];
73 int alen;
74 int i;
75
76 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000077 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000078 printf("} against {");
79 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
80 printf("}\n");
81
82 oid = NULL;
vlmef6355b2004-09-29 13:26:15 +000083 rval = ber_decode(0, &asn_DEF_RELATIVE_OID, (void *)&oid, buf, len);
vlmfa67ddc2004-06-03 03:38:44 +000084 assert(rval.code == RC_OK);
85
vlma97e2242004-06-14 07:40:17 +000086 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000087
88 /*
89 * Print the contents for visual debugging.
90 */
91 printf("RELATIVE_OID_print() => ");
vlmef6355b2004-09-29 13:26:15 +000092 RELATIVE_OID_print(&asn_DEF_RELATIVE_OID, oid, 0, _print, 0);
vlmfa67ddc2004-06-03 03:38:44 +000093 printf("\n");
94
vlm12557712004-06-17 23:43:39 +000095 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000096 alen = RELATIVE_OID_get_arcs(oid,
97 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000098 assert(alen > 0);
99 assert(alen == ck_len);
100
101 /*
102 * Make sure they are equivalent.
103 */
104 printf("RELATIVE_OID_get_arcs() => {");
105 for(i = 0; i < alen; i++) {
106 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000107 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000108 }
109 printf(" }\n");
110}
111
112/*
113 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
114 */
115static void
116check_REGEN(int *arcs, int acount) {
117 static RELATIVE_OID_t oid;
118 unsigned long tmp_arcs[10];
119 int tmp_alen = 10;
120 int alen;
121 int ret;
122 int i;
123
vlm735e4612006-07-13 09:22:34 +0000124 if(0) {
125 fprintf(stderr, "Encoding (R) {");
126 for(i = 0; i < acount; i++) {
127 fprintf(stderr, " %u", arcs[i]);
128 }
129 fprintf(stderr, " }\n");
vlmfa67ddc2004-06-03 03:38:44 +0000130 }
vlmfa67ddc2004-06-03 03:38:44 +0000131
vlm12557712004-06-17 23:43:39 +0000132 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000133 assert(ret == 0);
134
vlm12557712004-06-17 23:43:39 +0000135 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000136 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
137 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000138 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000139 assert(alen <= tmp_alen);
140 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000141
vlm735e4612006-07-13 09:22:34 +0000142 if(0) {
143 fprintf(stderr, "Encoded (R) { ");
144 for(i = 0; i < alen; i++) {
145 fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout);
146 assert(arcs[i] == (int)tmp_arcs[i]);
147 }
148 fprintf(stderr, "}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000149 }
vlm735e4612006-07-13 09:22:34 +0000150
vlmfa67ddc2004-06-03 03:38:44 +0000151}
152
153/*
154 * Encode the specified array of arcs as OBJECT IDENTIFIER,
155 * decode it and compare.
156 */
157static void
158check_REGEN_OID(int *arcs, int acount) {
159 static OBJECT_IDENTIFIER_t oid;
160 unsigned long tmp_arcs[10];
161 int tmp_alen = 10;
162 int alen;
163 int ret;
164 int i;
165
vlm735e4612006-07-13 09:22:34 +0000166 if(0) {
167 fprintf(stderr, "Encoding (O) {");
168 for(i = 0; i < acount; i++) {
169 fprintf(stderr, " %u", arcs[i]);
170 }
171 fprintf(stderr, " }\n");
vlmfa67ddc2004-06-03 03:38:44 +0000172 }
vlmfa67ddc2004-06-03 03:38:44 +0000173
vlm12557712004-06-17 23:43:39 +0000174 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000175 assert(ret == 0);
176
vlm12557712004-06-17 23:43:39 +0000177 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000178 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
179 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000180 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000181 assert(alen <= tmp_alen);
182 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000183
vlm735e4612006-07-13 09:22:34 +0000184 if(0) {
185 fprintf(stderr, "Encoded (O) { ");
186 for(i = 0; i < alen; i++) {
187 fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout);
188 assert(arcs[i] == (int)tmp_arcs[i]);
189 }
190 fprintf(stderr, "}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000191 }
vlmfa67ddc2004-06-03 03:38:44 +0000192}
vlm2e3dd3b2004-06-14 07:24:36 +0000193static int
194check_speed() {
195 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
196 int ret = 0;
197 int cycles = 100000000;
198 double a, b, c;
199 struct timeval tv;
200 unsigned long value;
201 int i;
202
vlma97e2242004-06-14 07:40:17 +0000203 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000204 assert(ret == 0);
205 assert(value == 0x7040c20d);
206
207 gettimeofday(&tv, 0);
208 a = tv.tv_sec + tv.tv_usec / 1000000.0;
209 for(i = 0; i < cycles; i++) {
210 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
211 &value, sizeof(value));
212 }
213 assert(ret == 0);
214 assert(value == 0x7040c20d);
215 gettimeofday(&tv, 0);
216 b = tv.tv_sec + tv.tv_usec / 1000000.0;
217 for(i = 0; i < cycles; i++) {
218 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
219 &value, sizeof(value));
220 }
221 assert(ret == 0);
222 assert(value == 0x7040c20d);
223 gettimeofday(&tv, 0);
224 c = tv.tv_sec + tv.tv_usec / 1000000.0;
225
226 a = b - a;
227 b = c - b;
228 printf("Time for single_arc(): %f\n", a);
229 printf("Time for get_arc_l(): %f\n", b);
230
231 return 0;
232}
233
vlm1f73df22004-10-23 10:16:51 +0000234static void check_parse(const char *oid_txt, int retval) {
235 int ret;
236 long l[2];
vlm31feaf32005-03-10 11:50:24 +0000237 const char *p;
vlm1f73df22004-10-23 10:16:51 +0000238
239 ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, l, 2, &p);
240 printf("[%s] => %d == %d\n", oid_txt, ret, retval);
241 assert(ret == retval);
242 assert(p >= oid_txt);
243}
244
245static void check_xer(int expect_arcs, char *xer) {
246 asn_dec_rval_t rc;
247 RELATIVE_OID_t *st = 0;
248 long arcs[10];
249 int ret;
250 int i;
251
252 printf("[%s] => ", xer); fflush(stdout);
253 rc = asn_DEF_RELATIVE_OID.xer_decoder(0,
254 &asn_DEF_RELATIVE_OID, (void **)&st, "t",
255 xer, strlen(xer));
256 if(expect_arcs == -1) {
257 if(rc.code != RC_OK)
258 return;
259 }
260 assert(rc.code == RC_OK);
261
262 ret = RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs[0]),
263 sizeof(arcs)/sizeof(arcs[0]));
264 assert(ret < 10);
265 if(expect_arcs == -1) {
266 assert(ret == -1);
267 return;
268 }
269 for(i = 0; i < ret; i++) {
270 if(i) printf(".");
271 printf("%ld", arcs[i]);
272 assert(arcs[i] == i + 1);
273 }
274 printf(": %d == %d\n", ret, expect_arcs);
275 assert(ret == expect_arcs);
276}
277
vlmfa67ddc2004-06-03 03:38:44 +0000278#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
279 buf ## n ## _check, \
280 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
281#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
282 buf ## n ## _check, \
283 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
284#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
285 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
286#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
287 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
288
289int
vlma97e2242004-06-14 07:40:17 +0000290main() {
vlm12557712004-06-17 23:43:39 +0000291 int i;
292
vlm2e3dd3b2004-06-14 07:24:36 +0000293 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000294 uint8_t buf1[] = {
295 0x06, /* OBJECT IDENTIFIER */
296 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000297 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000298 };
vlm2e3dd3b2004-06-14 07:24:36 +0000299 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000300
301 /* {8571 3 2} */
302 uint8_t buf2[] = {
303 0x0D, /* RELATIVE-OID */
304 0x04, /* Length */
305 0xC2, 0x7B, 0x03, 0x02
306 };
307 int buf2_check[] = { 8571, 3, 2 };
308
vlm12557712004-06-17 23:43:39 +0000309 /* {joint-iso-itu-t 42 } */
310 uint8_t buf3[] = {
311 0x06, /* OBJECT IDENTIFIER */
312 0x01, /* Length */
313 0x7A
314 };
315 int buf3_check[] = { 2, 42 };
316
317 /* {joint-iso-itu-t 25957 } */
318 uint8_t buf4[] = {
319 0x06, /* OBJECT IDENTIFIER */
320 0x03, /* Length */
321 0x81, 0x80 + 0x4B, 0x35
322 };
323 int buf4_check[] = { 2, 25957 };
324
325 int buf5_check[] = { 0 };
326 int buf6_check[] = { 1 };
327 int buf7_check[] = { 80, 40 };
328 int buf8_check[] = { 127 };
329 int buf9_check[] = { 128 };
330 int buf10_check[] = { 65535, 65536 };
331 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
332 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
333 int buf13_check[] = { 0, 1, 2 };
334 int buf14_check[] = { 1, 38, 3 };
335 int buf15_check[] = { 0, 0, 0xf000 };
336 int buf16_check[] = { 0, 0, 0, 1, 0 };
337 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlm903a2132004-08-22 03:01:37 +0000338 int buf18_check[] = { 2, 2, 1, 1 };
339
340 /* { joint-iso-itu-t 2 1 1 } */
341 uint8_t buf19[] = {
342 0x06, /* OBJECT IDENTIFIER */
343 0x03, /* Length */
344 0x52, 0x01, 0x01
345 };
346 int buf19_check[] = { 2, 2, 1, 1 };
vlmfa67ddc2004-06-03 03:38:44 +0000347
vlm3fff06b2004-08-23 09:23:02 +0000348 /* { joint-iso-itu-t 2 1 0 1 } */
349 uint8_t buf20[] = {
350 0x06, /* OBJECT IDENTIFIER */
351 0x04, /* Length */
352 0x52, 0x01, 0x00, 0x01
353 };
354 int buf20_check[] = { 2, 2, 1, 0, 1 };
355
vlmfa67ddc2004-06-03 03:38:44 +0000356
357 CHECK_OID(1); /* buf1, buf1_check */
358 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000359 CHECK_OID(3); /* buf3, buf3_check */
360 CHECK_OID(4); /* buf4, buf4_check */
vlm903a2132004-08-22 03:01:37 +0000361 CHECK_OID(19); /* buf19, buf19_check */
vlm3fff06b2004-08-23 09:23:02 +0000362 CHECK_OID(20); /* buf20, buf20_check */
vlmfa67ddc2004-06-03 03:38:44 +0000363
vlm12557712004-06-17 23:43:39 +0000364 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000365 CHECK_REGEN(6);
366 CHECK_REGEN(7);
367 CHECK_REGEN(8);
368 CHECK_REGEN(9);
369 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000370 CHECK_REGEN(11);
371 CHECK_REGEN(12);
372 CHECK_REGEN(13);
373 CHECK_REGEN(14);
374 CHECK_REGEN(15);
375 CHECK_REGEN(16);
376 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000377 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000378 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
379 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000380 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000381 CHECK_REGEN_OID(14);
382 CHECK_REGEN_OID(15);
383 CHECK_REGEN_OID(16);
384 CHECK_REGEN_OID(17);
vlm903a2132004-08-22 03:01:37 +0000385 CHECK_REGEN_OID(18);
vlm3fff06b2004-08-23 09:23:02 +0000386 CHECK_REGEN_OID(19);
387 CHECK_REGEN_OID(20);
vlm12557712004-06-17 23:43:39 +0000388
vlm1f73df22004-10-23 10:16:51 +0000389 check_parse("", 0);
390 check_parse(" ", 0);
391 check_parse(" ", 0);
392 check_parse(".", -1);
393 check_parse(" .", -1);
394 check_parse(" 1", 1);
395 check_parse(" 1.2", 2);
396 check_parse(" 1.", -1);
397 check_parse(" 1. ", -1);
398 check_parse("1. ", -1);
399 check_parse("1.2", 2);
400 check_parse("10.30.234.234", 4);
401 check_parse("10.30.234.234 ", 4);
402 check_parse("10.30.234. 234 ", -1);
403 check_parse("10.30.234.234.", -1);
404 check_parse("1.2000000000.3", 3);
405 check_parse("1.2147483647.3", 3);
406 if(sizeof(long) == 4) {
407 check_parse("1.2147483648.3", -1); /* overflow on ILP32 */
408 check_parse("1.3000000000.3", -1);
409 check_parse("1.4000000000.3", -1);
410 check_parse("1.5000000000.3", -1);
411 check_parse("1.6000000000.3", -1);
412 check_parse("1.9000000000.3", -1);
413 } else {
414 check_parse("1.2147483648.3", 3);
415 }
416 check_parse("1.900a0000000.3", -1);
417 check_parse("1.900a.3", -1);
418
419 check_xer(0, "<t></t>");
420 check_xer(2, "<t>1.2</t>");
421 check_xer(3, "<t>1.2.3</t>");
422 check_xer(3, "<t> 1.2.3 </t>");
423 check_xer(-1, "<t>1.2.3 1</t>");
424
vlm12557712004-06-17 23:43:39 +0000425 for(i = 0; i < 100000; i++) {
426 int bufA_check[3] = { 2, i, rand() };
427 int bufB_check[2] = { rand(), i * 121 };
428 CHECK_REGEN(A);
429 CHECK_REGEN_OID(A);
430 CHECK_REGEN(B);
431 if(i > 100) i++;
432 if(i > 500) i++;
433 if(i > 1000) i += 3;
434 if(i > 5000) i += 151;
435 }
vlmfa67ddc2004-06-03 03:38:44 +0000436
vlm3717fb32004-06-14 08:17:27 +0000437 if(getenv("CHECK_SPEED")) {
438 /* Useful for developers only */
439 check_speed();
440 }
vlm2e3dd3b2004-06-14 07:24:36 +0000441
vlmfa67ddc2004-06-03 03:38:44 +0000442 return 0;
443}