blob: 951f75277add156ec1f45e76cd021ea347979f17 [file] [log] [blame]
Lev Walkin4eceeba2007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
Lev Walkin6cbed3d2017-10-07 16:42:41 -07003#include <errno.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00004#include <sys/time.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00005
Lev Walkin4eceeba2007-07-23 06:48:26 +00006#include <OBJECT_IDENTIFIER.h>
7#include <RELATIVE-OID.h>
8
Lev Walkinf15320b2004-06-03 03:38:44 +00009static int
10_print(const void *buffer, size_t size, void *app_key) {
Lev Walkin46bf9352004-06-14 07:40:17 +000011 (void)app_key;
Lev Walkinf15320b2004-06-03 03:38:44 +000012 fwrite(buffer, size, 1, stdout);
13 return 0;
14}
15
16static void
Lev Walkin6cbed3d2017-10-07 16:42:41 -070017check_OID(int lineno, uint8_t *buf, size_t len, unsigned *ck_buf, int ck_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 OBJECT_IDENTIFIER_t *oid;
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000020 unsigned long arcs[10];
21 int alen;
22 int i;
23
Lev Walkin0995f352017-09-17 23:16:38 -070024 printf("%03d: Checking {", lineno);
Lev Walkin46bf9352004-06-14 07:40:17 +000025 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
Lev Walkinf15320b2004-06-03 03:38:44 +000026 printf("} against {");
Lev Walkin6cbed3d2017-10-07 16:42:41 -070027 for(i = 0; i < ck_len; i++) { printf("%s%u", i?" ":"", ck_buf[i]); }
Lev Walkinf15320b2004-06-03 03:38:44 +000028 printf("}\n");
29
30 oid = NULL;
Lev Walkin5e033762004-09-29 13:26:15 +000031 rval = ber_decode(0, &asn_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
Lev Walkinf15320b2004-06-03 03:38:44 +000032 assert(rval.code == RC_OK);
33
Lev Walkin494fb702017-08-07 20:07:00 -070034 assert((ssize_t)oid->size == (ssize_t)len - 2);
Lev Walkinf15320b2004-06-03 03:38:44 +000035
36 /*
37 * Print the contents for visual debugging.
38 */
39 printf("OBJECT_IDENTIFIER_print() => ");
Lev Walkin5e033762004-09-29 13:26:15 +000040 OBJECT_IDENTIFIER_print(&asn_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000041 printf("\n");
42
Lev Walkin0787ff02004-06-17 23:43:39 +000043 memset(arcs, 'A', sizeof(arcs));
Lev Walkin29a044b2004-06-14 07:24:36 +000044 alen = OBJECT_IDENTIFIER_get_arcs(oid,
45 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
Lev Walkinf15320b2004-06-03 03:38:44 +000046 assert(alen > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkin6c9e0712004-08-22 03:01:37 +000048 printf("OBJECT_IDENTIFIER_get_arcs() => {");
Lev Walkinf15320b2004-06-03 03:38:44 +000049 /*
50 * Make sure they are equivalent.
51 */
Lev Walkinf15320b2004-06-03 03:38:44 +000052 for(i = 0; i < alen; i++) {
53 printf(" %lu", arcs[i]);
Lev Walkin6c9e0712004-08-22 03:01:37 +000054 if(alen == ck_len) {
Lev Walkin6cbed3d2017-10-07 16:42:41 -070055 assert(arcs[i] == ck_buf[i]);
Lev Walkin6c9e0712004-08-22 03:01:37 +000056 }
Lev Walkinf15320b2004-06-03 03:38:44 +000057 }
58 printf(" }\n");
Lev Walkin6c9e0712004-08-22 03:01:37 +000059 assert(alen == ck_len);
60
Lev Walkin229ad002017-09-18 20:13:49 -070061 ASN_STRUCT_FREE(asn_DEF_OBJECT_IDENTIFIER, oid);
Lev Walkinf15320b2004-06-03 03:38:44 +000062}
63
64static void
Lev Walkin6cbed3d2017-10-07 16:42:41 -070065check_ROID(int lineno, uint8_t *buf, size_t len, unsigned *ck_buf, int ck_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +000066 RELATIVE_OID_t *oid;
Lev Walkindc06f6b2004-10-20 15:50:55 +000067 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000068 unsigned long arcs[10];
69 int alen;
70 int i;
71
Lev Walkin0995f352017-09-17 23:16:38 -070072 printf("%03d: Checking {", lineno);
Lev Walkin46bf9352004-06-14 07:40:17 +000073 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
Lev Walkinf15320b2004-06-03 03:38:44 +000074 printf("} against {");
Lev Walkin6cbed3d2017-10-07 16:42:41 -070075 for(i = 0; i < ck_len; i++) { printf("%s%u", i?" ":"", ck_buf[i]); }
Lev Walkinf15320b2004-06-03 03:38:44 +000076 printf("}\n");
77
78 oid = NULL;
Lev Walkin5e033762004-09-29 13:26:15 +000079 rval = ber_decode(0, &asn_DEF_RELATIVE_OID, (void *)&oid, buf, len);
Lev Walkinf15320b2004-06-03 03:38:44 +000080 assert(rval.code == RC_OK);
81
Lev Walkin494fb702017-08-07 20:07:00 -070082 assert((ssize_t)oid->size == (ssize_t)len - 2);
Lev Walkinf15320b2004-06-03 03:38:44 +000083
84 /*
85 * Print the contents for visual debugging.
86 */
87 printf("RELATIVE_OID_print() => ");
Lev Walkin5e033762004-09-29 13:26:15 +000088 RELATIVE_OID_print(&asn_DEF_RELATIVE_OID, oid, 0, _print, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000089 printf("\n");
90
Lev Walkin0787ff02004-06-17 23:43:39 +000091 memset(arcs, 'A', sizeof(arcs));
Lev Walkin29a044b2004-06-14 07:24:36 +000092 alen = RELATIVE_OID_get_arcs(oid,
93 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
Lev Walkinf15320b2004-06-03 03:38:44 +000094 assert(alen > 0);
95 assert(alen == ck_len);
96
97 /*
98 * Make sure they are equivalent.
99 */
100 printf("RELATIVE_OID_get_arcs() => {");
101 for(i = 0; i < alen; i++) {
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700102 printf(" %lu", arcs[i]);
103 assert(arcs[i] == ck_buf[i]);
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 }
105 printf(" }\n");
Lev Walkin229ad002017-09-18 20:13:49 -0700106 ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, oid);
Lev Walkinf15320b2004-06-03 03:38:44 +0000107}
108
Lev Walkin29a044b2004-06-14 07:24:36 +0000109static int
110check_speed() {
111 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
112 int ret = 0;
113 int cycles = 100000000;
114 double a, b, c;
115 struct timeval tv;
116 unsigned long value;
117 int i;
118
Lev Walkin46bf9352004-06-14 07:40:17 +0000119 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
Lev Walkin29a044b2004-06-14 07:24:36 +0000120 assert(ret == 0);
121 assert(value == 0x7040c20d);
122
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), 0,
127 &value, sizeof(value));
128 }
129 assert(ret == 0);
130 assert(value == 0x7040c20d);
131 gettimeofday(&tv, 0);
132 b = tv.tv_sec + tv.tv_usec / 1000000.0;
133 for(i = 0; i < cycles; i++) {
134 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
135 &value, sizeof(value));
136 }
137 assert(ret == 0);
138 assert(value == 0x7040c20d);
139 gettimeofday(&tv, 0);
140 c = tv.tv_sec + tv.tv_usec / 1000000.0;
141
142 a = b - a;
143 b = c - b;
144 printf("Time for single_arc(): %f\n", a);
145 printf("Time for get_arc_l(): %f\n", b);
146
147 return 0;
148}
149
Lev Walkin92302252004-10-23 10:16:51 +0000150static void check_parse(const char *oid_txt, int retval) {
151 int ret;
152 long l[2];
Lev Walkincad560a2013-03-16 07:00:58 -0700153 const char *p = oid_txt - 13;
154 assert(p < oid_txt);
Lev Walkin92302252004-10-23 10:16:51 +0000155
156 ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, l, 2, &p);
157 printf("[%s] => %d == %d\n", oid_txt, ret, retval);
158 assert(ret == retval);
159 assert(p >= oid_txt);
160}
161
162static void check_xer(int expect_arcs, char *xer) {
163 asn_dec_rval_t rc;
164 RELATIVE_OID_t *st = 0;
165 long arcs[10];
166 int ret;
167 int i;
168
169 printf("[%s] => ", xer); fflush(stdout);
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800170 rc = asn_DEF_RELATIVE_OID.op->xer_decoder(0,
Lev Walkin229ad002017-09-18 20:13:49 -0700171 &asn_DEF_RELATIVE_OID, (void **)&st, "t",
Lev Walkin92302252004-10-23 10:16:51 +0000172 xer, strlen(xer));
173 if(expect_arcs == -1) {
Lev Walkinf5927112012-09-03 00:48:45 -0700174 if(rc.code != RC_OK) {
175 printf("-1\n");
Lev Walkin229ad002017-09-18 20:13:49 -0700176 ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
Lev Walkin92302252004-10-23 10:16:51 +0000177 return;
Lev Walkinf5927112012-09-03 00:48:45 -0700178 }
Lev Walkin92302252004-10-23 10:16:51 +0000179 }
180 assert(rc.code == RC_OK);
181
182 ret = RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs[0]),
183 sizeof(arcs)/sizeof(arcs[0]));
184 assert(ret < 10);
185 if(expect_arcs == -1) {
186 assert(ret == -1);
Lev Walkin229ad002017-09-18 20:13:49 -0700187 ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
Lev Walkin92302252004-10-23 10:16:51 +0000188 return;
189 }
190 for(i = 0; i < ret; i++) {
191 if(i) printf(".");
192 printf("%ld", arcs[i]);
Lev Walkincad560a2013-03-16 07:00:58 -0700193 if(arcs[i] != i + 1) printf(" != %d\n", i + 1);
Lev Walkin92302252004-10-23 10:16:51 +0000194 assert(arcs[i] == i + 1);
195 }
196 printf(": %d == %d\n", ret, expect_arcs);
197 assert(ret == expect_arcs);
Lev Walkin229ad002017-09-18 20:13:49 -0700198 ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
Lev Walkin92302252004-10-23 10:16:51 +0000199}
200
Lev Walkin0995f352017-09-17 23:16:38 -0700201#define CHECK_OID(n) \
202 check_OID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
203 sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
204#define CHECK_ROID(n) \
205 check_ROID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
206 sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
Lev Walkinf15320b2004-06-03 03:38:44 +0000207
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700208static void
209check_binary_parsing() {
Lev Walkin0787ff02004-06-17 23:43:39 +0000210
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700211 /* {joint-iso-itu-t 230 3} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 uint8_t buf1[] = {
213 0x06, /* OBJECT IDENTIFIER */
214 0x03, /* Length */
Lev Walkin29a044b2004-06-14 07:24:36 +0000215 0x82, 0x36, 0x03
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700217 unsigned buf1_check[] = { 2, 230, 3 };
Lev Walkinf15320b2004-06-03 03:38:44 +0000218
219 /* {8571 3 2} */
220 uint8_t buf2[] = {
221 0x0D, /* RELATIVE-OID */
222 0x04, /* Length */
223 0xC2, 0x7B, 0x03, 0x02
224 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700225 unsigned buf2_check[] = { 8571, 3, 2 };
Lev Walkinf15320b2004-06-03 03:38:44 +0000226
Lev Walkin0787ff02004-06-17 23:43:39 +0000227 /* {joint-iso-itu-t 42 } */
228 uint8_t buf3[] = {
229 0x06, /* OBJECT IDENTIFIER */
230 0x01, /* Length */
231 0x7A
232 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700233 unsigned buf3_check[] = { 2, 42 };
Lev Walkin0787ff02004-06-17 23:43:39 +0000234
235 /* {joint-iso-itu-t 25957 } */
236 uint8_t buf4[] = {
237 0x06, /* OBJECT IDENTIFIER */
238 0x03, /* Length */
239 0x81, 0x80 + 0x4B, 0x35
240 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700241 unsigned buf4_check[] = { 2, 25957 };
Lev Walkin0787ff02004-06-17 23:43:39 +0000242
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700243 /* { jounsigned-iso-itu-t 2 1 1 } */
244 uint8_t buf5[] = {
Lev Walkin6c9e0712004-08-22 03:01:37 +0000245 0x06, /* OBJECT IDENTIFIER */
246 0x03, /* Length */
247 0x52, 0x01, 0x01
248 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700249 unsigned buf5_check[] = { 2, 2, 1, 1 };
Lev Walkinf15320b2004-06-03 03:38:44 +0000250
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700251 /* { jounsigned-iso-itu-t 2 1 0 1 } */
252 uint8_t buf6[] = {
Lev Walkin3251b8e2004-08-23 09:23:02 +0000253 0x06, /* OBJECT IDENTIFIER */
254 0x04, /* Length */
255 0x52, 0x01, 0x00, 0x01
256 };
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700257 unsigned buf6_check[] = { 2, 2, 1, 0, 1 };
Lev Walkin3251b8e2004-08-23 09:23:02 +0000258
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700259 CHECK_OID(1);
260 CHECK_ROID(2);
261 CHECK_OID(3);
262 CHECK_OID(4);
263 CHECK_OID(5);
264 CHECK_OID(6);
265}
Lev Walkinf15320b2004-06-03 03:38:44 +0000266
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700267static void
268check_text_parsing() {
269 check_parse("", 0);
270 check_parse(" ", 0);
Lev Walkin92302252004-10-23 10:16:51 +0000271 check_parse(".", -1);
272 check_parse(" .", -1);
Lev Walkinf5927112012-09-03 00:48:45 -0700273 check_parse(".1", -1);
274 check_parse("1.", -1);
275 check_parse("1. ", -1);
276 check_parse(".1. ", -1);
277 check_parse(" .1. ", -1);
Lev Walkin92302252004-10-23 10:16:51 +0000278 check_parse(" 1", 1);
279 check_parse(" 1.2", 2);
280 check_parse(" 1.", -1);
281 check_parse(" 1. ", -1);
282 check_parse("1. ", -1);
283 check_parse("1.2", 2);
Lev Walkinf5927112012-09-03 00:48:45 -0700284 check_parse("1.2 ", 2);
285 check_parse("1.2 ", 2);
286 check_parse(" 1.2 ", 2);
287 check_parse("1. 2", -1);
288 check_parse("1 .2", -1);
Lev Walkincad560a2013-03-16 07:00:58 -0700289 check_parse(" 1 .2", -1);
290 check_parse(" 1 .2 ", -1);
291 check_parse("1 .2 ", -1);
292 check_parse("1.+1", -1);
Lev Walkin92302252004-10-23 10:16:51 +0000293 check_parse("10.30.234.234", 4);
294 check_parse("10.30.234.234 ", 4);
295 check_parse("10.30.234. 234 ", -1);
296 check_parse("10.30.234.234.", -1);
297 check_parse("1.2000000000.3", 3);
298 check_parse("1.2147483647.3", 3);
299 if(sizeof(long) == 4) {
300 check_parse("1.2147483648.3", -1); /* overflow on ILP32 */
Lev Walkina7d1fb32012-01-23 01:21:55 +0000301 check_parse("1.2147483649.3", -1); /* overflow on ILP32 */
Lev Walkin92302252004-10-23 10:16:51 +0000302 check_parse("1.3000000000.3", -1);
303 check_parse("1.4000000000.3", -1);
304 check_parse("1.5000000000.3", -1);
305 check_parse("1.6000000000.3", -1);
306 check_parse("1.9000000000.3", -1);
Lev Walkina7d1fb32012-01-23 01:21:55 +0000307 } else if(sizeof(long) == 8) {
Lev Walkin92302252004-10-23 10:16:51 +0000308 check_parse("1.2147483648.3", 3);
Lev Walkina7d1fb32012-01-23 01:21:55 +0000309 check_parse("1.9223372036854775807.3", 3);
310 check_parse("1.9223372036854775808.3", -1);
Lev Walkin92302252004-10-23 10:16:51 +0000311 }
312 check_parse("1.900a0000000.3", -1);
313 check_parse("1.900a.3", -1);
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700314}
Lev Walkin92302252004-10-23 10:16:51 +0000315
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700316static void
317check_xer_parsing() {
Lev Walkinf5927112012-09-03 00:48:45 -0700318 check_xer(-1, "<t></t>");
Lev Walkin92302252004-10-23 10:16:51 +0000319 check_xer(2, "<t>1.2</t>");
320 check_xer(3, "<t>1.2.3</t>");
321 check_xer(3, "<t> 1.2.3 </t>");
322 check_xer(-1, "<t>1.2.3 1</t>");
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700323}
Lev Walkin92302252004-10-23 10:16:51 +0000324
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700325int
326main() {
327 check_binary_parsing();
328 check_text_parsing();
329 check_xer_parsing();
Lev Walkinf15320b2004-06-03 03:38:44 +0000330
Lev Walkinc4c61962004-06-14 08:17:27 +0000331 if(getenv("CHECK_SPEED")) {
332 /* Useful for developers only */
333 check_speed();
334 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000335
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 return 0;
337}
Lev Walkin725883b2006-10-09 12:07:58 +0000338