6 #include <OBJECT_IDENTIFIER.h>
7 #include <RELATIVE-OID.h>
10 _print(const void *buffer, size_t size, void *app_key) {
12 fwrite(buffer, size, 1, stdout);
17 check_OID(int lineno, uint8_t *buf, size_t len, unsigned *ck_buf, int ck_len) {
18 OBJECT_IDENTIFIER_t *oid;
24 printf("%03d: Checking {", lineno);
25 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
26 printf("} against {");
27 for(i = 0; i < ck_len; i++) { printf("%s%u", i?" ":"", ck_buf[i]); }
31 rval = ber_decode(0, &asn_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
32 assert(rval.code == RC_OK);
34 assert((ssize_t)oid->size == (ssize_t)len - 2);
37 * Print the contents for visual debugging.
39 printf("OBJECT_IDENTIFIER_print() => ");
40 OBJECT_IDENTIFIER_print(&asn_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
43 memset(arcs, 'A', sizeof(arcs));
45 OBJECT_IDENTIFIER_get_arcs(oid, arcs, sizeof(arcs) / sizeof(arcs[0]));
48 printf("OBJECT_IDENTIFIER_get_arcs() => {");
50 * Make sure they are equivalent.
52 for(i = 0; i < alen; i++) {
53 printf(" %" PRIu32, arcs[i]);
55 assert(arcs[i] == ck_buf[i]);
59 assert(alen == ck_len);
61 ASN_STRUCT_FREE(asn_DEF_OBJECT_IDENTIFIER, oid);
65 check_ROID(int lineno, uint8_t *buf, size_t len, unsigned *ck_buf,
73 printf("%03d: Checking {", lineno);
74 for(i = 0; i < len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
75 printf("} against {");
76 for(i = 0; i < ck_len; i++) { printf("%s%u", i?" ":"", ck_buf[i]); }
80 rval = ber_decode(0, &asn_DEF_RELATIVE_OID, (void *)&oid, buf, len);
81 assert(rval.code == RC_OK);
83 assert((ssize_t)oid->size == (ssize_t)len - 2);
86 * Print the contents for visual debugging.
88 printf("RELATIVE_OID_print() => ");
89 RELATIVE_OID_print(&asn_DEF_RELATIVE_OID, oid, 0, _print, 0);
92 memset(arcs, 'A', sizeof(arcs));
93 alen = RELATIVE_OID_get_arcs(oid, arcs, sizeof(arcs) / sizeof(arcs[0]));
95 assert((size_t)alen == ck_len);
98 * Make sure they are equivalent.
100 printf("RELATIVE_OID_get_arcs() => {");
101 for(i = 0; i < (size_t)alen; i++) {
102 printf(" %" PRIu32, arcs[i]);
103 assert(arcs[i] == ck_buf[i]);
106 ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, oid);
111 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
113 int cycles = 100000000;
119 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), &value);
121 assert(value == 0x7040c20d);
123 gettimeofday(&tv, 0);
124 a = tv.tv_sec + tv.tv_usec / 1000000.0;
125 for(i = 0; i < cycles; i++) {
126 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), &value);
129 assert(value == 0x7040c20d);
130 gettimeofday(&tv, 0);
131 b = tv.tv_sec + tv.tv_usec / 1000000.0;
132 for(i = 0; i < cycles; i++) {
133 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), &value);
136 assert(value == 0x7040c20d);
137 gettimeofday(&tv, 0);
138 c = tv.tv_sec + tv.tv_usec / 1000000.0;
142 printf("Time for single_arc(): %f\n", a);
143 printf("Time for get_arc_l(): %f\n", b);
148 #define CHECK_PARSE(a, b) check_parse(__LINE__, a, b)
149 static void check_parse(int lineno, const char *oid_txt, int retval) {
152 const char *p = oid_txt - 13;
155 ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, arcs, 2, &p);
156 printf("%03d: [%s] => %d == %d\n", lineno, oid_txt, ret, retval);
157 assert(ret == retval);
158 assert(p >= oid_txt);
162 check_xer(int lineno, asn_TYPE_descriptor_t *td, ssize_t expect_arcs, char *xer,
163 const asn_oid_arc_t *expected_arcs) {
170 printf("%03d: [%s] => ", lineno, xer);
172 rc = td->op->xer_decoder(0, td, (void **)&st, "t", xer, strlen(xer));
173 if(expect_arcs == -1) {
174 if(rc.code != RC_OK) {
176 ASN_STRUCT_FREE(*td, st);
180 assert(rc.code == RC_OK);
182 if(td == &asn_DEF_OBJECT_IDENTIFIER) {
183 got_arcs = OBJECT_IDENTIFIER_get_arcs(st, arcs,
184 sizeof(arcs) / sizeof(arcs[0]));
185 } else if(td == &asn_DEF_RELATIVE_OID) {
187 RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs) / sizeof(arcs[0]));
189 assert(!"Unreachable");
191 assert(got_arcs < 10);
192 if(expect_arcs == -1) {
193 assert(got_arcs == -1);
194 ASN_STRUCT_FREE(*td, st);
197 for(i = 0; i < got_arcs; i++) {
199 printf("%" PRIu32, arcs[i]);
200 if(arcs[i] != expected_arcs[i]) {
201 printf(" != %" PRIu32 "\n", expected_arcs[i]);
203 assert(arcs[i] == expected_arcs[i]);
205 printf(": %zd == %zd\n", got_arcs, expect_arcs);
206 assert(got_arcs == expect_arcs);
207 ASN_STRUCT_FREE(*td, st);
210 #define CHECK_OID(n) \
211 check_OID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
212 sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
213 #define CHECK_ROID(n) \
214 check_ROID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
215 sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
218 check_binary_parsing() {
222 0x06, /* OBJECT IDENTIFIER */
226 uint32_t buf0_check[] = { 0, 0 };
228 /* {joint-iso-itu-t 230 3} */
230 0x06, /* OBJECT IDENTIFIER */
234 uint32_t buf1_check[] = { 2, 230, 3 };
236 /* {joint-iso-itu-t 42 } */
238 0x06, /* OBJECT IDENTIFIER */
242 uint32_t buf2_check[] = { 2, 42 };
244 /* {joint-iso-itu-t 25957 } */
246 0x06, /* OBJECT IDENTIFIER */
248 0x81, 0x80 + 0x4B, 0x35
250 uint32_t buf3_check[] = { 2, 25957 };
252 /* { jounsigned-iso-itu-t 2 1 1 } */
254 0x06, /* OBJECT IDENTIFIER */
258 uint32_t buf4_check[] = { 2, 2, 1, 1 };
260 /* { jounsigned-iso-itu-t 2 1 0 1 } */
262 0x06, /* OBJECT IDENTIFIER */
264 0x52, 0x01, 0x00, 0x01
266 uint32_t buf5_check[] = { 2, 2, 1, 0, 1 };
268 /* {joint-iso-itu-t 25957 } */
270 0x06, /* OBJECT IDENTIFIER */
272 0x2a, 0x03, 0x01, 0x01, 0x01, 0x81, 0xaa, 0x11, 0x03, 0x05, 0x01, 0x01
274 uint32_t buf6_check[] = { 1, 2, 3, 1, 1, 1, 21777, 3, 5, 1, 1 };
278 0x0D, /* RELATIVE-OID */
282 uint32_t buf10_check[] = { 0 };
286 0x0D, /* RELATIVE-OID */
290 uint32_t buf11_check[] = { 0, 0 };
294 0x0D, /* RELATIVE-OID */
296 0xC2, 0x7B, 0x03, 0x02
298 uint32_t buf12_check[] = { 8571, 3, 2 };
314 check_text_parsing() {
317 CHECK_PARSE(".", -1);
318 CHECK_PARSE(" .", -1);
319 CHECK_PARSE(".1", -1);
320 CHECK_PARSE("1.", -1);
321 CHECK_PARSE("1. ", -1);
322 CHECK_PARSE(".1. ", -1);
323 CHECK_PARSE(" .1. ", -1);
324 CHECK_PARSE(" 1", 1);
325 CHECK_PARSE(" 1.2", 2);
326 CHECK_PARSE(" 1.", -1);
327 CHECK_PARSE(" 1. ", -1);
328 CHECK_PARSE("1. ", -1);
329 CHECK_PARSE("1.2", 2);
330 CHECK_PARSE("1.2 ", 2);
331 CHECK_PARSE("1.2 ", 2);
332 CHECK_PARSE(" 1.2 ", 2);
333 CHECK_PARSE("1. 2", -1);
334 CHECK_PARSE("1 .2", -1);
335 CHECK_PARSE(" 1 .2", -1);
336 CHECK_PARSE(" 1 .2 ", -1);
337 CHECK_PARSE("1 .2 ", -1);
338 CHECK_PARSE("1.+1", -1);
339 CHECK_PARSE("2.0", 2);
340 CHECK_PARSE("2.1", 2);
341 CHECK_PARSE("10.30.234.234", 4);
342 CHECK_PARSE("10.30.234.234 ", 4);
343 CHECK_PARSE("10.30.234. 234 ", -1);
344 CHECK_PARSE("10.30.234.234.", -1);
345 CHECK_PARSE("1.2000000000.3", 3);
346 CHECK_PARSE("1.2147483647.3", 3);
347 CHECK_PARSE("1.4294967295.3", 3);
348 CHECK_PARSE("1.4294967296.3", -1); /* overflow on ILP32 */
349 CHECK_PARSE("1.3000000000.3", 3);
350 CHECK_PARSE("1.4000000000.3", 3);
351 CHECK_PARSE("1.5000000000.3", -1);
352 CHECK_PARSE("1.6000000000.3", -1);
353 CHECK_PARSE("1.9000000000.3", -1);
354 CHECK_PARSE("1.900a0000000.3", -1);
355 CHECK_PARSE("1.900a.3", -1);
358 #define CHECK_XER(a, b, c, data) check_xer(__LINE__, &asn_DEF_##a, b, c, data)
361 check_xer_parsing() {
362 asn_oid_arc_t zero_zero[] = {0, 0};
363 asn_oid_arc_t zero_one[] = {0, 1};
364 asn_oid_arc_t one_zero[] = {1, 0};
365 asn_oid_arc_t one_one[] = {1, 1};
366 asn_oid_arc_t one_two[] = {1, 2};
367 asn_oid_arc_t two_one[] = {2, 1};
368 asn_oid_arc_t one_two_three[] = {1, 2,3};
370 CHECK_XER(RELATIVE_OID, -1, "<t></t>", 0);
371 CHECK_XER(RELATIVE_OID, -1, "<t>1.</t>", 0);
372 CHECK_XER(RELATIVE_OID, -1, "<t>2.1.</t>", 0);
373 CHECK_XER(RELATIVE_OID, 2, "<t>0.0</t>", zero_zero);
374 CHECK_XER(RELATIVE_OID, 2, "<t>0.1</t>", zero_one);
375 CHECK_XER(RELATIVE_OID, 2, "<t>1.0</t>", one_zero);
376 CHECK_XER(RELATIVE_OID, 2, "<t>1.2</t>", one_two);
377 CHECK_XER(RELATIVE_OID, -1, "<t>1.-2</t>", 0);
378 CHECK_XER(RELATIVE_OID, -1, "<t>1.+2</t>", 0);
379 CHECK_XER(RELATIVE_OID, 3, "<t>1.2.3</t>", one_two_three);
380 CHECK_XER(RELATIVE_OID, 3, "<t> 1.2.3 </t>", one_two_three);
381 CHECK_XER(RELATIVE_OID, -1, "<t>1.2.3 1</t>", 0);
382 CHECK_XER(RELATIVE_OID, 2, "<t>2.1</t>", two_one);
384 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t></t>", 0);
385 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>1</t>", 0);
386 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>1.</t>", 0);
387 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>2.1.</t>", 0);
388 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t>0.0</t>", zero_zero);
389 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t>0.1</t>", zero_one);
390 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t>1.0</t>", one_zero);
391 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t>1.1</t>", one_one);
392 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t>1.2</t>", one_two);
393 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>1.-2</t>", 0);
394 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>1.+2</t>", 0);
395 CHECK_XER(OBJECT_IDENTIFIER, 3, "<t>1.2.3</t>", one_two_three);
396 CHECK_XER(OBJECT_IDENTIFIER, 3, "<t> 1.2.3 </t>", one_two_three);
397 CHECK_XER(OBJECT_IDENTIFIER, -1, "<t>1.2.3 1</t>", 0);
398 CHECK_XER(OBJECT_IDENTIFIER, 2, "<t> 2.1 </t>", two_one);
403 check_binary_parsing();
404 check_text_parsing();
407 if(getenv("CHECK_SPEED")) {
408 /* Useful for developers only */