blob: 4961250837ed886d7a2da5367c8bb0f8b6dad4ed [file] [log] [blame]
vlmd3da8512004-08-19 13:26:54 +00001#include <OBJECT_IDENTIFIER.c>
2#include <RELATIVE-OID.c>
3#include <INTEGER.c>
4#include <ber_decoder.c>
5#include <ber_tlv_length.c>
6#include <ber_tlv_tag.c>
7#include <der_encoder.c>
8#include <constraints.c>
9
vlm2e3dd3b2004-06-14 07:24:36 +000010#include <sys/time.h>
vlmfa67ddc2004-06-03 03:38:44 +000011
12static int
13_print(const void *buffer, size_t size, void *app_key) {
vlma97e2242004-06-14 07:40:17 +000014 (void)app_key;
vlmfa67ddc2004-06-03 03:38:44 +000015 fwrite(buffer, size, 1, stdout);
16 return 0;
17}
18
19static void
20check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
21 OBJECT_IDENTIFIER_t *oid;
22 ber_dec_rval_t rval;
23 unsigned long arcs[10];
24 int alen;
25 int i;
26
27 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000028 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000029 printf("} against {");
30 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
31 printf("}\n");
32
33 oid = NULL;
34 rval = ber_decode(&asn1_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
35 assert(rval.code == RC_OK);
36
vlma97e2242004-06-14 07:40:17 +000037 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000038
39 /*
40 * Print the contents for visual debugging.
41 */
42 printf("OBJECT_IDENTIFIER_print() => ");
43 OBJECT_IDENTIFIER_print(&asn1_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
44 printf("\n");
45
vlm12557712004-06-17 23:43:39 +000046 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000047 alen = OBJECT_IDENTIFIER_get_arcs(oid,
48 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000049 assert(alen > 0);
vlmfa67ddc2004-06-03 03:38:44 +000050
vlm903a2132004-08-22 03:01:37 +000051 printf("OBJECT_IDENTIFIER_get_arcs() => {");
vlmfa67ddc2004-06-03 03:38:44 +000052 /*
53 * Make sure they are equivalent.
54 */
vlmfa67ddc2004-06-03 03:38:44 +000055 for(i = 0; i < alen; i++) {
56 printf(" %lu", arcs[i]);
vlm903a2132004-08-22 03:01:37 +000057 if(alen == ck_len) {
58 assert(arcs[i] == (unsigned long)ck_buf[i]);
59 }
vlmfa67ddc2004-06-03 03:38:44 +000060 }
61 printf(" }\n");
vlm903a2132004-08-22 03:01:37 +000062 assert(alen == ck_len);
63
vlmfa67ddc2004-06-03 03:38:44 +000064}
65
66static void
67check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
68 RELATIVE_OID_t *oid;
69 ber_dec_rval_t rval;
70 unsigned long arcs[10];
71 int alen;
72 int i;
73
74 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000075 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000076 printf("} against {");
77 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
78 printf("}\n");
79
80 oid = NULL;
81 rval = ber_decode(&asn1_DEF_RELATIVE_OID, (void *)&oid, buf, len);
82 assert(rval.code == RC_OK);
83
vlma97e2242004-06-14 07:40:17 +000084 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000085
86 /*
87 * Print the contents for visual debugging.
88 */
89 printf("RELATIVE_OID_print() => ");
90 RELATIVE_OID_print(&asn1_DEF_RELATIVE_OID, oid, 0, _print, 0);
91 printf("\n");
92
vlm12557712004-06-17 23:43:39 +000093 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000094 alen = RELATIVE_OID_get_arcs(oid,
95 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000096 assert(alen > 0);
97 assert(alen == ck_len);
98
99 /*
100 * Make sure they are equivalent.
101 */
102 printf("RELATIVE_OID_get_arcs() => {");
103 for(i = 0; i < alen; i++) {
104 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000105 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000106 }
107 printf(" }\n");
108}
109
110/*
111 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
112 */
113static void
114check_REGEN(int *arcs, int acount) {
115 static RELATIVE_OID_t oid;
116 unsigned long tmp_arcs[10];
117 int tmp_alen = 10;
118 int alen;
119 int ret;
120 int i;
121
vlm12557712004-06-17 23:43:39 +0000122 printf("Encoding (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000123 for(i = 0; i < acount; i++) {
124 printf(" %u", arcs[i]);
125 }
126 printf(" }\n");
127
vlm12557712004-06-17 23:43:39 +0000128 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000129 assert(ret == 0);
130
vlm12557712004-06-17 23:43:39 +0000131 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000132 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
133 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000134 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000135 assert(alen <= tmp_alen);
136 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000137
vlm12557712004-06-17 23:43:39 +0000138 printf("Encoded (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000139 for(i = 0; i < alen; i++) {
140 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000141 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000142 }
143 printf(" }\n");
144}
145
146/*
147 * Encode the specified array of arcs as OBJECT IDENTIFIER,
148 * decode it and compare.
149 */
150static void
151check_REGEN_OID(int *arcs, int acount) {
152 static OBJECT_IDENTIFIER_t oid;
153 unsigned long tmp_arcs[10];
154 int tmp_alen = 10;
155 int alen;
156 int ret;
157 int i;
158
vlm12557712004-06-17 23:43:39 +0000159 printf("Encoding (O) {");
vlmfa67ddc2004-06-03 03:38:44 +0000160 for(i = 0; i < acount; i++) {
161 printf(" %u", arcs[i]);
162 }
163 printf(" }\n");
164
vlm12557712004-06-17 23:43:39 +0000165 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000166 assert(ret == 0);
167
vlm12557712004-06-17 23:43:39 +0000168 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000169 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
170 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000171 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000172 assert(alen <= tmp_alen);
173 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000174
vlm12557712004-06-17 23:43:39 +0000175 printf("Encoded (O) { ");
vlmfa67ddc2004-06-03 03:38:44 +0000176 for(i = 0; i < alen; i++) {
vlm12557712004-06-17 23:43:39 +0000177 printf("%lu ", tmp_arcs[i]); fflush(stdout);
vlma97e2242004-06-14 07:40:17 +0000178 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000179 }
vlm12557712004-06-17 23:43:39 +0000180 printf("}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000181}
182
vlm2e3dd3b2004-06-14 07:24:36 +0000183static int
184check_speed() {
185 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
186 int ret = 0;
187 int cycles = 100000000;
188 double a, b, c;
189 struct timeval tv;
190 unsigned long value;
191 int i;
192
vlma97e2242004-06-14 07:40:17 +0000193 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000194 assert(ret == 0);
195 assert(value == 0x7040c20d);
196
197 gettimeofday(&tv, 0);
198 a = tv.tv_sec + tv.tv_usec / 1000000.0;
199 for(i = 0; i < cycles; i++) {
200 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
201 &value, sizeof(value));
202 }
203 assert(ret == 0);
204 assert(value == 0x7040c20d);
205 gettimeofday(&tv, 0);
206 b = tv.tv_sec + tv.tv_usec / 1000000.0;
207 for(i = 0; i < cycles; i++) {
208 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
209 &value, sizeof(value));
210 }
211 assert(ret == 0);
212 assert(value == 0x7040c20d);
213 gettimeofday(&tv, 0);
214 c = tv.tv_sec + tv.tv_usec / 1000000.0;
215
216 a = b - a;
217 b = c - b;
218 printf("Time for single_arc(): %f\n", a);
219 printf("Time for get_arc_l(): %f\n", b);
220
221 return 0;
222}
223
vlmfa67ddc2004-06-03 03:38:44 +0000224#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
225 buf ## n ## _check, \
226 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
227#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
228 buf ## n ## _check, \
229 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
230#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
231 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
232#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
233 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
234
235int
vlma97e2242004-06-14 07:40:17 +0000236main() {
vlm12557712004-06-17 23:43:39 +0000237 int i;
238
vlm2e3dd3b2004-06-14 07:24:36 +0000239 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000240 uint8_t buf1[] = {
241 0x06, /* OBJECT IDENTIFIER */
242 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000243 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000244 };
vlm2e3dd3b2004-06-14 07:24:36 +0000245 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000246
247 /* {8571 3 2} */
248 uint8_t buf2[] = {
249 0x0D, /* RELATIVE-OID */
250 0x04, /* Length */
251 0xC2, 0x7B, 0x03, 0x02
252 };
253 int buf2_check[] = { 8571, 3, 2 };
254
vlm12557712004-06-17 23:43:39 +0000255 /* {joint-iso-itu-t 42 } */
256 uint8_t buf3[] = {
257 0x06, /* OBJECT IDENTIFIER */
258 0x01, /* Length */
259 0x7A
260 };
261 int buf3_check[] = { 2, 42 };
262
263 /* {joint-iso-itu-t 25957 } */
264 uint8_t buf4[] = {
265 0x06, /* OBJECT IDENTIFIER */
266 0x03, /* Length */
267 0x81, 0x80 + 0x4B, 0x35
268 };
269 int buf4_check[] = { 2, 25957 };
270
271 int buf5_check[] = { 0 };
272 int buf6_check[] = { 1 };
273 int buf7_check[] = { 80, 40 };
274 int buf8_check[] = { 127 };
275 int buf9_check[] = { 128 };
276 int buf10_check[] = { 65535, 65536 };
277 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
278 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
279 int buf13_check[] = { 0, 1, 2 };
280 int buf14_check[] = { 1, 38, 3 };
281 int buf15_check[] = { 0, 0, 0xf000 };
282 int buf16_check[] = { 0, 0, 0, 1, 0 };
283 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlm903a2132004-08-22 03:01:37 +0000284 int buf18_check[] = { 2, 2, 1, 1 };
285
286 /* { joint-iso-itu-t 2 1 1 } */
287 uint8_t buf19[] = {
288 0x06, /* OBJECT IDENTIFIER */
289 0x03, /* Length */
290 0x52, 0x01, 0x01
291 };
292 int buf19_check[] = { 2, 2, 1, 1 };
vlmfa67ddc2004-06-03 03:38:44 +0000293
294
295 CHECK_OID(1); /* buf1, buf1_check */
296 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000297 CHECK_OID(3); /* buf3, buf3_check */
298 CHECK_OID(4); /* buf4, buf4_check */
vlm903a2132004-08-22 03:01:37 +0000299 CHECK_OID(19); /* buf19, buf19_check */
vlmfa67ddc2004-06-03 03:38:44 +0000300
vlm12557712004-06-17 23:43:39 +0000301 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000302 CHECK_REGEN(6);
303 CHECK_REGEN(7);
304 CHECK_REGEN(8);
305 CHECK_REGEN(9);
306 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000307 CHECK_REGEN(11);
308 CHECK_REGEN(12);
309 CHECK_REGEN(13);
310 CHECK_REGEN(14);
311 CHECK_REGEN(15);
312 CHECK_REGEN(16);
313 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000314 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000315 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
316 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000317 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000318 CHECK_REGEN_OID(14);
319 CHECK_REGEN_OID(15);
320 CHECK_REGEN_OID(16);
321 CHECK_REGEN_OID(17);
vlm903a2132004-08-22 03:01:37 +0000322 CHECK_REGEN_OID(18);
vlm12557712004-06-17 23:43:39 +0000323
324 for(i = 0; i < 100000; i++) {
325 int bufA_check[3] = { 2, i, rand() };
326 int bufB_check[2] = { rand(), i * 121 };
327 CHECK_REGEN(A);
328 CHECK_REGEN_OID(A);
329 CHECK_REGEN(B);
330 if(i > 100) i++;
331 if(i > 500) i++;
332 if(i > 1000) i += 3;
333 if(i > 5000) i += 151;
334 }
vlmfa67ddc2004-06-03 03:38:44 +0000335
vlm3717fb32004-06-14 08:17:27 +0000336 if(getenv("CHECK_SPEED")) {
337 /* Useful for developers only */
338 check_speed();
339 }
vlm2e3dd3b2004-06-14 07:24:36 +0000340
vlmfa67ddc2004-06-03 03:38:44 +0000341 return 0;
342}