blob: 8033d7cc5b8056db8bceeefdbbe3c9b2d68410e0 [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
vlm12557712004-06-17 23:43:39 +0000124 printf("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++) {
142 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000143 assert((unsigned long)arcs[i] == 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
vlm12557712004-06-17 23:43:39 +0000161 printf("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);
vlma97e2242004-06-14 07:40:17 +0000180 assert((unsigned long)arcs[i] == 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
vlmfa67ddc2004-06-03 03:38:44 +0000226#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
227 buf ## n ## _check, \
228 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
229#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
230 buf ## n ## _check, \
231 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
232#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
233 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
234#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
235 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
236
237int
vlma97e2242004-06-14 07:40:17 +0000238main() {
vlm12557712004-06-17 23:43:39 +0000239 int i;
240
vlm2e3dd3b2004-06-14 07:24:36 +0000241 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000242 uint8_t buf1[] = {
243 0x06, /* OBJECT IDENTIFIER */
244 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000245 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000246 };
vlm2e3dd3b2004-06-14 07:24:36 +0000247 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000248
249 /* {8571 3 2} */
250 uint8_t buf2[] = {
251 0x0D, /* RELATIVE-OID */
252 0x04, /* Length */
253 0xC2, 0x7B, 0x03, 0x02
254 };
255 int buf2_check[] = { 8571, 3, 2 };
256
vlm12557712004-06-17 23:43:39 +0000257 /* {joint-iso-itu-t 42 } */
258 uint8_t buf3[] = {
259 0x06, /* OBJECT IDENTIFIER */
260 0x01, /* Length */
261 0x7A
262 };
263 int buf3_check[] = { 2, 42 };
264
265 /* {joint-iso-itu-t 25957 } */
266 uint8_t buf4[] = {
267 0x06, /* OBJECT IDENTIFIER */
268 0x03, /* Length */
269 0x81, 0x80 + 0x4B, 0x35
270 };
271 int buf4_check[] = { 2, 25957 };
272
273 int buf5_check[] = { 0 };
274 int buf6_check[] = { 1 };
275 int buf7_check[] = { 80, 40 };
276 int buf8_check[] = { 127 };
277 int buf9_check[] = { 128 };
278 int buf10_check[] = { 65535, 65536 };
279 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
280 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
281 int buf13_check[] = { 0, 1, 2 };
282 int buf14_check[] = { 1, 38, 3 };
283 int buf15_check[] = { 0, 0, 0xf000 };
284 int buf16_check[] = { 0, 0, 0, 1, 0 };
285 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlm903a2132004-08-22 03:01:37 +0000286 int buf18_check[] = { 2, 2, 1, 1 };
287
288 /* { joint-iso-itu-t 2 1 1 } */
289 uint8_t buf19[] = {
290 0x06, /* OBJECT IDENTIFIER */
291 0x03, /* Length */
292 0x52, 0x01, 0x01
293 };
294 int buf19_check[] = { 2, 2, 1, 1 };
vlmfa67ddc2004-06-03 03:38:44 +0000295
vlm3fff06b2004-08-23 09:23:02 +0000296 /* { joint-iso-itu-t 2 1 0 1 } */
297 uint8_t buf20[] = {
298 0x06, /* OBJECT IDENTIFIER */
299 0x04, /* Length */
300 0x52, 0x01, 0x00, 0x01
301 };
302 int buf20_check[] = { 2, 2, 1, 0, 1 };
303
vlmfa67ddc2004-06-03 03:38:44 +0000304
305 CHECK_OID(1); /* buf1, buf1_check */
306 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000307 CHECK_OID(3); /* buf3, buf3_check */
308 CHECK_OID(4); /* buf4, buf4_check */
vlm903a2132004-08-22 03:01:37 +0000309 CHECK_OID(19); /* buf19, buf19_check */
vlm3fff06b2004-08-23 09:23:02 +0000310 CHECK_OID(20); /* buf20, buf20_check */
vlmfa67ddc2004-06-03 03:38:44 +0000311
vlm12557712004-06-17 23:43:39 +0000312 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000313 CHECK_REGEN(6);
314 CHECK_REGEN(7);
315 CHECK_REGEN(8);
316 CHECK_REGEN(9);
317 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000318 CHECK_REGEN(11);
319 CHECK_REGEN(12);
320 CHECK_REGEN(13);
321 CHECK_REGEN(14);
322 CHECK_REGEN(15);
323 CHECK_REGEN(16);
324 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000325 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000326 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
327 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000328 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000329 CHECK_REGEN_OID(14);
330 CHECK_REGEN_OID(15);
331 CHECK_REGEN_OID(16);
332 CHECK_REGEN_OID(17);
vlm903a2132004-08-22 03:01:37 +0000333 CHECK_REGEN_OID(18);
vlm3fff06b2004-08-23 09:23:02 +0000334 CHECK_REGEN_OID(19);
335 CHECK_REGEN_OID(20);
vlm12557712004-06-17 23:43:39 +0000336
337 for(i = 0; i < 100000; i++) {
338 int bufA_check[3] = { 2, i, rand() };
339 int bufB_check[2] = { rand(), i * 121 };
340 CHECK_REGEN(A);
341 CHECK_REGEN_OID(A);
342 CHECK_REGEN(B);
343 if(i > 100) i++;
344 if(i > 500) i++;
345 if(i > 1000) i += 3;
346 if(i > 5000) i += 151;
347 }
vlmfa67ddc2004-06-03 03:38:44 +0000348
vlm3717fb32004-06-14 08:17:27 +0000349 if(getenv("CHECK_SPEED")) {
350 /* Useful for developers only */
351 check_speed();
352 }
vlm2e3dd3b2004-06-14 07:24:36 +0000353
vlmfa67ddc2004-06-03 03:38:44 +0000354 return 0;
355}