blob: 97ed33ff1de15ba93b39f179db7ec0079e0f0fb9 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "../OBJECT_IDENTIFIER.c"
vlm12557712004-06-17 23:43:39 +00002#include "../RELATIVE-OID.c"
vlmfa67ddc2004-06-03 03:38:44 +00003#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"
vlm2e3dd3b2004-06-14 07:24:36 +00009#include <sys/time.h>
vlmfa67ddc2004-06-03 03:38:44 +000010
11static int
12_print(const void *buffer, size_t size, void *app_key) {
vlma97e2242004-06-14 07:40:17 +000013 (void)app_key;
vlmfa67ddc2004-06-03 03:38:44 +000014 fwrite(buffer, size, 1, stdout);
15 return 0;
16}
17
18static void
19check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
20 OBJECT_IDENTIFIER_t *oid;
21 ber_dec_rval_t rval;
22 unsigned long arcs[10];
23 int alen;
24 int i;
25
26 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000027 for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000028 printf("} against {");
29 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
30 printf("}\n");
31
32 oid = NULL;
33 rval = ber_decode(&asn1_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
34 assert(rval.code == RC_OK);
35
vlma97e2242004-06-14 07:40:17 +000036 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000037
38 /*
39 * Print the contents for visual debugging.
40 */
41 printf("OBJECT_IDENTIFIER_print() => ");
42 OBJECT_IDENTIFIER_print(&asn1_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
43 printf("\n");
44
vlm12557712004-06-17 23:43:39 +000045 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000046 alen = OBJECT_IDENTIFIER_get_arcs(oid,
47 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000048 assert(alen > 0);
49 assert(alen == ck_len);
50
51 /*
52 * Make sure they are equivalent.
53 */
54 printf("OBJECT_IDENTIFIER_get_arcs() => {");
55 for(i = 0; i < alen; i++) {
56 printf(" %lu", arcs[i]);
vlma97e2242004-06-14 07:40:17 +000057 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +000058 }
59 printf(" }\n");
60}
61
62static void
63check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
64 RELATIVE_OID_t *oid;
65 ber_dec_rval_t rval;
66 unsigned long arcs[10];
67 int alen;
68 int i;
69
70 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000071 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000072 printf("} against {");
73 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
74 printf("}\n");
75
76 oid = NULL;
77 rval = ber_decode(&asn1_DEF_RELATIVE_OID, (void *)&oid, buf, len);
78 assert(rval.code == RC_OK);
79
vlma97e2242004-06-14 07:40:17 +000080 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000081
82 /*
83 * Print the contents for visual debugging.
84 */
85 printf("RELATIVE_OID_print() => ");
86 RELATIVE_OID_print(&asn1_DEF_RELATIVE_OID, oid, 0, _print, 0);
87 printf("\n");
88
vlm12557712004-06-17 23:43:39 +000089 memset(arcs, 'A', sizeof(arcs));
vlm2e3dd3b2004-06-14 07:24:36 +000090 alen = RELATIVE_OID_get_arcs(oid,
91 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000092 assert(alen > 0);
93 assert(alen == ck_len);
94
95 /*
96 * Make sure they are equivalent.
97 */
98 printf("RELATIVE_OID_get_arcs() => {");
99 for(i = 0; i < alen; i++) {
100 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000101 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000102 }
103 printf(" }\n");
104}
105
106/*
107 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
108 */
109static void
110check_REGEN(int *arcs, int acount) {
111 static RELATIVE_OID_t oid;
112 unsigned long tmp_arcs[10];
113 int tmp_alen = 10;
114 int alen;
115 int ret;
116 int i;
117
vlm12557712004-06-17 23:43:39 +0000118 printf("Encoding (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000119 for(i = 0; i < acount; i++) {
120 printf(" %u", arcs[i]);
121 }
122 printf(" }\n");
123
vlm12557712004-06-17 23:43:39 +0000124 ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000125 assert(ret == 0);
126
vlm12557712004-06-17 23:43:39 +0000127 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000128 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
129 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000130 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000131 assert(alen <= tmp_alen);
132 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000133
vlm12557712004-06-17 23:43:39 +0000134 printf("Encoded (R) {");
vlmfa67ddc2004-06-03 03:38:44 +0000135 for(i = 0; i < alen; i++) {
136 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000137 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000138 }
139 printf(" }\n");
140}
141
142/*
143 * Encode the specified array of arcs as OBJECT IDENTIFIER,
144 * decode it and compare.
145 */
146static void
147check_REGEN_OID(int *arcs, int acount) {
148 static OBJECT_IDENTIFIER_t oid;
149 unsigned long tmp_arcs[10];
150 int tmp_alen = 10;
151 int alen;
152 int ret;
153 int i;
154
vlm12557712004-06-17 23:43:39 +0000155 printf("Encoding (O) {");
vlmfa67ddc2004-06-03 03:38:44 +0000156 for(i = 0; i < acount; i++) {
157 printf(" %u", arcs[i]);
158 }
159 printf(" }\n");
160
vlm12557712004-06-17 23:43:39 +0000161 ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount);
vlmfa67ddc2004-06-03 03:38:44 +0000162 assert(ret == 0);
163
vlm12557712004-06-17 23:43:39 +0000164 memset(tmp_arcs, 'A', sizeof(tmp_arcs));
vlm2e3dd3b2004-06-14 07:24:36 +0000165 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
166 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000167 assert(alen >= 0);
vlm12557712004-06-17 23:43:39 +0000168 assert(alen <= tmp_alen);
169 assert(alen == acount);
vlmfa67ddc2004-06-03 03:38:44 +0000170
vlm12557712004-06-17 23:43:39 +0000171 printf("Encoded (O) { ");
vlmfa67ddc2004-06-03 03:38:44 +0000172 for(i = 0; i < alen; i++) {
vlm12557712004-06-17 23:43:39 +0000173 printf("%lu ", tmp_arcs[i]); fflush(stdout);
vlma97e2242004-06-14 07:40:17 +0000174 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000175 }
vlm12557712004-06-17 23:43:39 +0000176 printf("}\n");
vlmfa67ddc2004-06-03 03:38:44 +0000177}
178
vlm2e3dd3b2004-06-14 07:24:36 +0000179static int
180check_speed() {
181 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
182 int ret = 0;
183 int cycles = 100000000;
184 double a, b, c;
185 struct timeval tv;
186 unsigned long value;
187 int i;
188
vlma97e2242004-06-14 07:40:17 +0000189 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000190 assert(ret == 0);
191 assert(value == 0x7040c20d);
192
193 gettimeofday(&tv, 0);
194 a = tv.tv_sec + tv.tv_usec / 1000000.0;
195 for(i = 0; i < cycles; i++) {
196 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
197 &value, sizeof(value));
198 }
199 assert(ret == 0);
200 assert(value == 0x7040c20d);
201 gettimeofday(&tv, 0);
202 b = tv.tv_sec + tv.tv_usec / 1000000.0;
203 for(i = 0; i < cycles; i++) {
204 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
205 &value, sizeof(value));
206 }
207 assert(ret == 0);
208 assert(value == 0x7040c20d);
209 gettimeofday(&tv, 0);
210 c = tv.tv_sec + tv.tv_usec / 1000000.0;
211
212 a = b - a;
213 b = c - b;
214 printf("Time for single_arc(): %f\n", a);
215 printf("Time for get_arc_l(): %f\n", b);
216
217 return 0;
218}
219
vlmfa67ddc2004-06-03 03:38:44 +0000220#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
221 buf ## n ## _check, \
222 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
223#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
224 buf ## n ## _check, \
225 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
226#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
227 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
228#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
229 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
230
231int
vlma97e2242004-06-14 07:40:17 +0000232main() {
vlm12557712004-06-17 23:43:39 +0000233 int i;
234
vlm2e3dd3b2004-06-14 07:24:36 +0000235 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000236 uint8_t buf1[] = {
237 0x06, /* OBJECT IDENTIFIER */
238 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000239 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000240 };
vlm2e3dd3b2004-06-14 07:24:36 +0000241 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000242
243 /* {8571 3 2} */
244 uint8_t buf2[] = {
245 0x0D, /* RELATIVE-OID */
246 0x04, /* Length */
247 0xC2, 0x7B, 0x03, 0x02
248 };
249 int buf2_check[] = { 8571, 3, 2 };
250
vlm12557712004-06-17 23:43:39 +0000251 /* {joint-iso-itu-t 42 } */
252 uint8_t buf3[] = {
253 0x06, /* OBJECT IDENTIFIER */
254 0x01, /* Length */
255 0x7A
256 };
257 int buf3_check[] = { 2, 42 };
258
259 /* {joint-iso-itu-t 25957 } */
260 uint8_t buf4[] = {
261 0x06, /* OBJECT IDENTIFIER */
262 0x03, /* Length */
263 0x81, 0x80 + 0x4B, 0x35
264 };
265 int buf4_check[] = { 2, 25957 };
266
267 int buf5_check[] = { 0 };
268 int buf6_check[] = { 1 };
269 int buf7_check[] = { 80, 40 };
270 int buf8_check[] = { 127 };
271 int buf9_check[] = { 128 };
272 int buf10_check[] = { 65535, 65536 };
273 int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
274 int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
275 int buf13_check[] = { 0, 1, 2 };
276 int buf14_check[] = { 1, 38, 3 };
277 int buf15_check[] = { 0, 0, 0xf000 };
278 int buf16_check[] = { 0, 0, 0, 1, 0 };
279 int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 };
vlmfa67ddc2004-06-03 03:38:44 +0000280
281
282 CHECK_OID(1); /* buf1, buf1_check */
283 CHECK_ROID(2); /* buf2, buf2_check */
vlm12557712004-06-17 23:43:39 +0000284 CHECK_OID(3); /* buf3, buf3_check */
285 CHECK_OID(4); /* buf4, buf4_check */
vlmfa67ddc2004-06-03 03:38:44 +0000286
vlm12557712004-06-17 23:43:39 +0000287 CHECK_REGEN(5); /* Regenerate RELATIVE-OID */
vlmfa67ddc2004-06-03 03:38:44 +0000288 CHECK_REGEN(6);
289 CHECK_REGEN(7);
290 CHECK_REGEN(8);
291 CHECK_REGEN(9);
292 CHECK_REGEN(10);
vlm12557712004-06-17 23:43:39 +0000293 CHECK_REGEN(11);
294 CHECK_REGEN(12);
295 CHECK_REGEN(13);
296 CHECK_REGEN(14);
297 CHECK_REGEN(15);
298 CHECK_REGEN(16);
299 CHECK_REGEN(17);
vlmfa67ddc2004-06-03 03:38:44 +0000300 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
vlm12557712004-06-17 23:43:39 +0000301 CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */
302 CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */
vlmfa67ddc2004-06-03 03:38:44 +0000303 CHECK_REGEN_OID(13);
vlm12557712004-06-17 23:43:39 +0000304 CHECK_REGEN_OID(14);
305 CHECK_REGEN_OID(15);
306 CHECK_REGEN_OID(16);
307 CHECK_REGEN_OID(17);
308
309 for(i = 0; i < 100000; i++) {
310 int bufA_check[3] = { 2, i, rand() };
311 int bufB_check[2] = { rand(), i * 121 };
312 CHECK_REGEN(A);
313 CHECK_REGEN_OID(A);
314 CHECK_REGEN(B);
315 if(i > 100) i++;
316 if(i > 500) i++;
317 if(i > 1000) i += 3;
318 if(i > 5000) i += 151;
319 }
vlmfa67ddc2004-06-03 03:38:44 +0000320
vlm3717fb32004-06-14 08:17:27 +0000321 if(getenv("CHECK_SPEED")) {
322 /* Useful for developers only */
323 check_speed();
324 }
vlm2e3dd3b2004-06-14 07:24:36 +0000325
vlmfa67ddc2004-06-03 03:38:44 +0000326 return 0;
327}