blob: e696d8027be343a5abaac603c7270e8f20c65b1d [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
vlmef8f4ef2005-03-06 10:00:57 +0000124 fprintf(stderr, "Encoding (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000125 for(i = 0; i < acount; i++) {
126 printf(" %u", arcs[i]);
127 }
128 printf(" }\n");
129
vlm12557712004-06-17 23:43:39 +0000130 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000131 assert(ret == 0);
132
vlm12557712004-06-17 23:43:39 +0000133 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000134 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
135 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000136 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000137 assert(alen <= tmp_alen);
138 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000139
vlm12557712004-06-17 23:43:39 +0000140 printf("Encoded (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000141 for(i = 0; i < alen; i++) {
vlm899ee7b2004-10-26 08:02:01 +0000142 printf(" %lu)", tmp_arcs[i]);
143 assert(arcs[i] == (int)tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000144 }
145 printf(" }\n");
146}
147
148/*
149 * Encode the specified array of arcs as OBJECT IDENTIFIER,
150 * decode it and compare.
151 */
152static void
153check_REGEN_OID(int *arcs, int acount) {
154 static OBJECT_IDENTIFIER_t oid;
155 unsigned long tmp_arcs[10];
156 int tmp_alen = 10;
157 int alen;
158 int ret;
159 int i;
160
vlmef8f4ef2005-03-06 10:00:57 +0000161 fprintf(stderr, "Encoding (O) {");
vlmfa67ddc2004-06-03 03:38:44 +0000162 for(i = 0; i < acount; i++) {
163 printf(" %u", arcs[i]);
164 }
165 printf(" }\n");
166
vlm12557712004-06-17 23:43:39 +0000167 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000168 assert(ret == 0);
169
vlm12557712004-06-17 23:43:39 +0000170 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000171 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
172 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000173 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000174 assert(alen <= tmp_alen);
175 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000176
vlm12557712004-06-17 23:43:39 +0000177 printf("Encoded (O) { ");
vlmfa67ddc2004-06-03 03:38:44 +0000178 for(i = 0; i < alen; i++) {
vlm12557712004-06-17 23:43:39 +0000179 printf("%lu ", tmp_arcs[i]); fflush(stdout);
vlm899ee7b2004-10-26 08:02:01 +0000180 assert(arcs[i] == (int)tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000181 }
vlm12557712004-06-17 23:43:39 +0000182 printf("}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000183}
184
vlm2e3dd3b2004-06-14 07:24:36 +0000185static int
186check_speed() {
187 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
188 int ret = 0;
189 int cycles = 100000000;
190 double a, b, c;
191 struct timeval tv;
192 unsigned long value;
193 int i;
194
vlma97e2242004-06-14 07:40:17 +0000195 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000196 assert(ret == 0);
197 assert(value == 0x7040c20d);
198
199 gettimeofday(&tv, 0);
200 a = tv.tv_sec + tv.tv_usec / 1000000.0;
201 for(i = 0; i < cycles; i++) {
202 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
203 &value, sizeof(value));
204 }
205 assert(ret == 0);
206 assert(value == 0x7040c20d);
207 gettimeofday(&tv, 0);
208 b = 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 c = tv.tv_sec + tv.tv_usec / 1000000.0;
217
218 a = b - a;
219 b = c - b;
220 printf("Time for single_arc(): %f\n", a);
221 printf("Time for get_arc_l(): %f\n", b);
222
223 return 0;
224}
225
vlm1f73df22004-10-23 10:16:51 +0000226static void check_parse(const char *oid_txt, int retval) {
227 int ret;
228 long l[2];
vlm31feaf32005-03-10 11:50:24 +0000229 const char *p;
vlm1f73df22004-10-23 10:16:51 +0000230
231 ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, l, 2, &p);
232 printf("[%s] => %d == %d\n", oid_txt, ret, retval);
233 assert(ret == retval);
234 assert(p >= oid_txt);
235}
236
237static void check_xer(int expect_arcs, char *xer) {
238 asn_dec_rval_t rc;
239 RELATIVE_OID_t *st = 0;
240 long arcs[10];
241 int ret;
242 int i;
243
244 printf("[%s] => ", xer); fflush(stdout);
245 rc = asn_DEF_RELATIVE_OID.xer_decoder(0,
246 &asn_DEF_RELATIVE_OID, (void **)&st, "t",
247 xer, strlen(xer));
248 if(expect_arcs == -1) {
249 if(rc.code != RC_OK)
250 return;
251 }
252 assert(rc.code == RC_OK);
253
254 ret = RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs[0]),
255 sizeof(arcs)/sizeof(arcs[0]));
256 assert(ret < 10);
257 if(expect_arcs == -1) {
258 assert(ret == -1);
259 return;
260 }
261 for(i = 0; i < ret; i++) {
262 if(i) printf(".");
263 printf("%ld", arcs[i]);
264 assert(arcs[i] == i + 1);
265 }
266 printf(": %d == %d\n", ret, expect_arcs);
267 assert(ret == expect_arcs);
268}
269
vlmfa67ddc2004-06-03 03:38:44 +0000270#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
271 buf ## n ## _check, \
272 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
273#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
274 buf ## n ## _check, \
275 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
276#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
277 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
278#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
279 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
280
281int
vlma97e2242004-06-14 07:40:17 +0000282main() {
vlm12557712004-06-17 23:43:39 +0000283 int i;
284
vlm2e3dd3b2004-06-14 07:24:36 +0000285 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000286 uint8_t buf1[] = {
287 0x06, /* OBJECT IDENTIFIER */
288 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000289 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000290 };
vlm2e3dd3b2004-06-14 07:24:36 +0000291 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000292
293 /* {8571 3 2} */
294 uint8_t buf2[] = {
295 0x0D, /* RELATIVE-OID */
296 0x04, /* Length */
297 0xC2, 0x7B, 0x03, 0x02
298 };
299 int buf2_check[] = { 8571, 3, 2 };
300
vlm12557712004-06-17 23:43:39 +0000301 /* {joint-iso-itu-t 42 } */
302 uint8_t buf3[] = {
303 0x06, /* OBJECT IDENTIFIER */
304 0x01, /* Length */
305 0x7A
306 };
307 int buf3_check[] = { 2, 42 };
308
309 /* {joint-iso-itu-t 25957 } */
310 uint8_t buf4[] = {
311 0x06, /* OBJECT IDENTIFIER */
312 0x03, /* Length */
313 0x81, 0x80 + 0x4B, 0x35
314 };
315 int buf4_check[] = { 2, 25957 };
316
317 int buf5_check[] = { 0 };
318 int buf6_check[] = { 1 };
319 int buf7_check[] = { 80, 40 };
320 int buf8_check[] = { 127 };
321 int buf9_check[] = { 128 };
322 int buf10_check[] = { 65535, 65536 };
323 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
324 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
325 int buf13_check[] = { 0, 1, 2 };
326 int buf14_check[] = { 1, 38, 3 };
327 int buf15_check[] = { 0, 0, 0xf000 };
328 int buf16_check[] = { 0, 0, 0, 1, 0 };
329 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlm903a2132004-08-22 03:01:37 +0000330 int buf18_check[] = { 2, 2, 1, 1 };
331
332 /* { joint-iso-itu-t 2 1 1 } */
333 uint8_t buf19[] = {
334 0x06, /* OBJECT IDENTIFIER */
335 0x03, /* Length */
336 0x52, 0x01, 0x01
337 };
338 int buf19_check[] = { 2, 2, 1, 1 };
vlmfa67ddc2004-06-03 03:38:44 +0000339
vlm3fff06b2004-08-23 09:23:02 +0000340 /* { joint-iso-itu-t 2 1 0 1 } */
341 uint8_t buf20[] = {
342 0x06, /* OBJECT IDENTIFIER */
343 0x04, /* Length */
344 0x52, 0x01, 0x00, 0x01
345 };
346 int buf20_check[] = { 2, 2, 1, 0, 1 };
347
vlmfa67ddc2004-06-03 03:38:44 +0000348
349 CHECK_OID(1); /* buf1, buf1_check */
350 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000351 CHECK_OID(3); /* buf3, buf3_check */
352 CHECK_OID(4); /* buf4, buf4_check */
vlm903a2132004-08-22 03:01:37 +0000353 CHECK_OID(19); /* buf19, buf19_check */
vlm3fff06b2004-08-23 09:23:02 +0000354 CHECK_OID(20); /* buf20, buf20_check */
vlmfa67ddc2004-06-03 03:38:44 +0000355
vlm12557712004-06-17 23:43:39 +0000356 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000357 CHECK_REGEN(6);
358 CHECK_REGEN(7);
359 CHECK_REGEN(8);
360 CHECK_REGEN(9);
361 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000362 CHECK_REGEN(11);
363 CHECK_REGEN(12);
364 CHECK_REGEN(13);
365 CHECK_REGEN(14);
366 CHECK_REGEN(15);
367 CHECK_REGEN(16);
368 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000369 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000370 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
371 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000372 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000373 CHECK_REGEN_OID(14);
374 CHECK_REGEN_OID(15);
375 CHECK_REGEN_OID(16);
376 CHECK_REGEN_OID(17);
vlm903a2132004-08-22 03:01:37 +0000377 CHECK_REGEN_OID(18);
vlm3fff06b2004-08-23 09:23:02 +0000378 CHECK_REGEN_OID(19);
379 CHECK_REGEN_OID(20);
vlm12557712004-06-17 23:43:39 +0000380
vlm1f73df22004-10-23 10:16:51 +0000381 check_parse("", 0);
382 check_parse(" ", 0);
383 check_parse(" ", 0);
384 check_parse(".", -1);
385 check_parse(" .", -1);
386 check_parse(" 1", 1);
387 check_parse(" 1.2", 2);
388 check_parse(" 1.", -1);
389 check_parse(" 1. ", -1);
390 check_parse("1. ", -1);
391 check_parse("1.2", 2);
392 check_parse("10.30.234.234", 4);
393 check_parse("10.30.234.234 ", 4);
394 check_parse("10.30.234. 234 ", -1);
395 check_parse("10.30.234.234.", -1);
396 check_parse("1.2000000000.3", 3);
397 check_parse("1.2147483647.3", 3);
398 if(sizeof(long) == 4) {
399 check_parse("1.2147483648.3", -1); /* overflow on ILP32 */
400 check_parse("1.3000000000.3", -1);
401 check_parse("1.4000000000.3", -1);
402 check_parse("1.5000000000.3", -1);
403 check_parse("1.6000000000.3", -1);
404 check_parse("1.9000000000.3", -1);
405 } else {
406 check_parse("1.2147483648.3", 3);
407 }
408 check_parse("1.900a0000000.3", -1);
409 check_parse("1.900a.3", -1);
410
411 check_xer(0, "<t></t>");
412 check_xer(2, "<t>1.2</t>");
413 check_xer(3, "<t>1.2.3</t>");
414 check_xer(3, "<t> 1.2.3 </t>");
415 check_xer(-1, "<t>1.2.3 1</t>");
416
vlm12557712004-06-17 23:43:39 +0000417 for(i = 0; i < 100000; i++) {
418 int bufA_check[3] = { 2, i, rand() };
419 int bufB_check[2] = { rand(), i * 121 };
420 CHECK_REGEN(A);
421 CHECK_REGEN_OID(A);
422 CHECK_REGEN(B);
423 if(i > 100) i++;
424 if(i > 500) i++;
425 if(i > 1000) i += 3;
426 if(i > 5000) i += 151;
427 }
vlmfa67ddc2004-06-03 03:38:44 +0000428
vlm3717fb32004-06-14 08:17:27 +0000429 if(getenv("CHECK_SPEED")) {
430 /* Useful for developers only */
431 check_speed();
432 }
vlm2e3dd3b2004-06-14 07:24:36 +0000433
vlmfa67ddc2004-06-03 03:38:44 +0000434 return 0;
435}