blob: 05d34ee046d92b6d0fddbd7c8b36be9744455de4 [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);
50 assert(alen == ck_len);
51
52 /*
53 * Make sure they are equivalent.
54 */
55 printf("OBJECT_IDENTIFIER_get_arcs() => {");
56 for(i = 0; i < alen; i++) {
57 printf(" %lu", arcs[i]);
vlma97e2242004-06-14 07:40:17 +000058 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +000059 }
60 printf(" }\n");
61}
62
63static void
64check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
65 RELATIVE_OID_t *oid;
66 ber_dec_rval_t rval;
67 unsigned long arcs[10];
68 int alen;
69 int i;
70
71 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000072 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000073 printf("} against {");
74 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
75 printf("}\n");
76
77 oid = NULL;
78 rval = ber_decode(&asn1_DEF_RELATIVE_OID, (void *)&oid, buf, len);
79 assert(rval.code == RC_OK);
80
vlma97e2242004-06-14 07:40:17 +000081 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000082
83 /*
84 * Print the contents for visual debugging.
85 */
86 printf("RELATIVE_OID_print() => ");
87 RELATIVE_OID_print(&asn1_DEF_RELATIVE_OID, oid, 0, _print, 0);
88 printf("\n");
89
vlm12557712004-06-17 23:43:39 +000090 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000091 alen = RELATIVE_OID_get_arcs(oid,
92 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000093 assert(alen > 0);
94 assert(alen == ck_len);
95
96 /*
97 * Make sure they are equivalent.
98 */
99 printf("RELATIVE_OID_get_arcs() => {");
100 for(i = 0; i < alen; i++) {
101 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000102 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000103 }
104 printf(" }\n");
105}
106
107/*
108 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
109 */
110static void
111check_REGEN(int *arcs, int acount) {
112 static RELATIVE_OID_t oid;
113 unsigned long tmp_arcs[10];
114 int tmp_alen = 10;
115 int alen;
116 int ret;
117 int i;
118
vlm12557712004-06-17 23:43:39 +0000119 printf("Encoding (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000120 for(i = 0; i < acount; i++) {
121 printf(" %u", arcs[i]);
122 }
123 printf(" }\n");
124
vlm12557712004-06-17 23:43:39 +0000125 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000126 assert(ret == 0);
127
vlm12557712004-06-17 23:43:39 +0000128 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000129 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
130 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000131 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000132 assert(alen <= tmp_alen);
133 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000134
vlm12557712004-06-17 23:43:39 +0000135 printf("Encoded (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000136 for(i = 0; i < alen; i++) {
137 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000138 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000139 }
140 printf(" }\n");
141}
142
143/*
144 * Encode the specified array of arcs as OBJECT IDENTIFIER,
145 * decode it and compare.
146 */
147static void
148check_REGEN_OID(int *arcs, int acount) {
149 static OBJECT_IDENTIFIER_t oid;
150 unsigned long tmp_arcs[10];
151 int tmp_alen = 10;
152 int alen;
153 int ret;
154 int i;
155
vlm12557712004-06-17 23:43:39 +0000156 printf("Encoding (O) {");
vlmfa67ddc2004-06-03 03:38:44 +0000157 for(i = 0; i < acount; i++) {
158 printf(" %u", arcs[i]);
159 }
160 printf(" }\n");
161
vlm12557712004-06-17 23:43:39 +0000162 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000163 assert(ret == 0);
164
vlm12557712004-06-17 23:43:39 +0000165 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000166 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
167 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000168 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000169 assert(alen <= tmp_alen);
170 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000171
vlm12557712004-06-17 23:43:39 +0000172 printf("Encoded (O) { ");
vlmfa67ddc2004-06-03 03:38:44 +0000173 for(i = 0; i < alen; i++) {
vlm12557712004-06-17 23:43:39 +0000174 printf("%lu ", tmp_arcs[i]); fflush(stdout);
vlma97e2242004-06-14 07:40:17 +0000175 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000176 }
vlm12557712004-06-17 23:43:39 +0000177 printf("}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000178}
179
vlm2e3dd3b2004-06-14 07:24:36 +0000180static int
181check_speed() {
182 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
183 int ret = 0;
184 int cycles = 100000000;
185 double a, b, c;
186 struct timeval tv;
187 unsigned long value;
188 int i;
189
vlma97e2242004-06-14 07:40:17 +0000190 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000191 assert(ret == 0);
192 assert(value == 0x7040c20d);
193
194 gettimeofday(&tv, 0);
195 a = tv.tv_sec + tv.tv_usec / 1000000.0;
196 for(i = 0; i < cycles; i++) {
197 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
198 &value, sizeof(value));
199 }
200 assert(ret == 0);
201 assert(value == 0x7040c20d);
202 gettimeofday(&tv, 0);
203 b = tv.tv_sec + tv.tv_usec / 1000000.0;
204 for(i = 0; i < cycles; i++) {
205 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
206 &value, sizeof(value));
207 }
208 assert(ret == 0);
209 assert(value == 0x7040c20d);
210 gettimeofday(&tv, 0);
211 c = tv.tv_sec + tv.tv_usec / 1000000.0;
212
213 a = b - a;
214 b = c - b;
215 printf("Time for single_arc(): %f\n", a);
216 printf("Time for get_arc_l(): %f\n", b);
217
218 return 0;
219}
220
vlmfa67ddc2004-06-03 03:38:44 +0000221#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
222 buf ## n ## _check, \
223 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
224#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
225 buf ## n ## _check, \
226 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
227#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
228 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
229#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
230 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
231
232int
vlma97e2242004-06-14 07:40:17 +0000233main() {
vlm12557712004-06-17 23:43:39 +0000234 int i;
235
vlm2e3dd3b2004-06-14 07:24:36 +0000236 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000237 uint8_t buf1[] = {
238 0x06, /* OBJECT IDENTIFIER */
239 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000240 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000241 };
vlm2e3dd3b2004-06-14 07:24:36 +0000242 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000243
244 /* {8571 3 2} */
245 uint8_t buf2[] = {
246 0x0D, /* RELATIVE-OID */
247 0x04, /* Length */
248 0xC2, 0x7B, 0x03, 0x02
249 };
250 int buf2_check[] = { 8571, 3, 2 };
251
vlm12557712004-06-17 23:43:39 +0000252 /* {joint-iso-itu-t 42 } */
253 uint8_t buf3[] = {
254 0x06, /* OBJECT IDENTIFIER */
255 0x01, /* Length */
256 0x7A
257 };
258 int buf3_check[] = { 2, 42 };
259
260 /* {joint-iso-itu-t 25957 } */
261 uint8_t buf4[] = {
262 0x06, /* OBJECT IDENTIFIER */
263 0x03, /* Length */
264 0x81, 0x80 + 0x4B, 0x35
265 };
266 int buf4_check[] = { 2, 25957 };
267
268 int buf5_check[] = { 0 };
269 int buf6_check[] = { 1 };
270 int buf7_check[] = { 80, 40 };
271 int buf8_check[] = { 127 };
272 int buf9_check[] = { 128 };
273 int buf10_check[] = { 65535, 65536 };
274 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
275 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
276 int buf13_check[] = { 0, 1, 2 };
277 int buf14_check[] = { 1, 38, 3 };
278 int buf15_check[] = { 0, 0, 0xf000 };
279 int buf16_check[] = { 0, 0, 0, 1, 0 };
280 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlmfa67ddc2004-06-03 03:38:44 +0000281
282
283 CHECK_OID(1); /* buf1, buf1_check */
284 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000285 CHECK_OID(3); /* buf3, buf3_check */
286 CHECK_OID(4); /* buf4, buf4_check */
vlmfa67ddc2004-06-03 03:38:44 +0000287
vlm12557712004-06-17 23:43:39 +0000288 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000289 CHECK_REGEN(6);
290 CHECK_REGEN(7);
291 CHECK_REGEN(8);
292 CHECK_REGEN(9);
293 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000294 CHECK_REGEN(11);
295 CHECK_REGEN(12);
296 CHECK_REGEN(13);
297 CHECK_REGEN(14);
298 CHECK_REGEN(15);
299 CHECK_REGEN(16);
300 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000301 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000302 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
303 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000304 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000305 CHECK_REGEN_OID(14);
306 CHECK_REGEN_OID(15);
307 CHECK_REGEN_OID(16);
308 CHECK_REGEN_OID(17);
309
310 for(i = 0; i < 100000; i++) {
311 int bufA_check[3] = { 2, i, rand() };
312 int bufB_check[2] = { rand(), i * 121 };
313 CHECK_REGEN(A);
314 CHECK_REGEN_OID(A);
315 CHECK_REGEN(B);
316 if(i > 100) i++;
317 if(i > 500) i++;
318 if(i > 1000) i += 3;
319 if(i > 5000) i += 151;
320 }
vlmfa67ddc2004-06-03 03:38:44 +0000321
vlm3717fb32004-06-14 08:17:27 +0000322 if(getenv("CHECK_SPEED")) {
323 /* Useful for developers only */
324 check_speed();
325 }
vlm2e3dd3b2004-06-14 07:24:36 +0000326
vlmfa67ddc2004-06-03 03:38:44 +0000327 return 0;
328}