blob: e7105f7633cbb23195d38b7c1b2dc32536d08164 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "../RELATIVE-OID.c"
2#include "../OBJECT_IDENTIFIER.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"
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) {
13 fwrite(buffer, size, 1, stdout);
14 return 0;
15}
16
17static void
18check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
19 OBJECT_IDENTIFIER_t *oid;
20 ber_dec_rval_t rval;
21 unsigned long arcs[10];
22 int alen;
23 int i;
24
25 printf("Checking {");
26 for(i = 0; i < len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
27 printf("} against {");
28 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
29 printf("}\n");
30
31 oid = NULL;
32 rval = ber_decode(&asn1_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len);
33 assert(rval.code == RC_OK);
34
35 assert(oid->size == len - 2);
36
37 /*
38 * Print the contents for visual debugging.
39 */
40 printf("OBJECT_IDENTIFIER_print() => ");
41 OBJECT_IDENTIFIER_print(&asn1_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0);
42 printf("\n");
43
vlm2e3dd3b2004-06-14 07:24:36 +000044 alen = OBJECT_IDENTIFIER_get_arcs(oid,
45 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000046 assert(alen > 0);
47 assert(alen == ck_len);
48
49 /*
50 * Make sure they are equivalent.
51 */
52 printf("OBJECT_IDENTIFIER_get_arcs() => {");
53 for(i = 0; i < alen; i++) {
54 printf(" %lu", arcs[i]);
55 assert(arcs[i] == ck_buf[i]);
56 }
57 printf(" }\n");
58}
59
60static void
61check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
62 RELATIVE_OID_t *oid;
63 ber_dec_rval_t rval;
64 unsigned long arcs[10];
65 int alen;
66 int i;
67
68 printf("Checking {");
69 for(i = 0; i < len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
70 printf("} against {");
71 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
72 printf("}\n");
73
74 oid = NULL;
75 rval = ber_decode(&asn1_DEF_RELATIVE_OID, (void *)&oid, buf, len);
76 assert(rval.code == RC_OK);
77
78 assert(oid->size == len - 2);
79
80 /*
81 * Print the contents for visual debugging.
82 */
83 printf("RELATIVE_OID_print() => ");
84 RELATIVE_OID_print(&asn1_DEF_RELATIVE_OID, oid, 0, _print, 0);
85 printf("\n");
86
vlm2e3dd3b2004-06-14 07:24:36 +000087 alen = RELATIVE_OID_get_arcs(oid,
88 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000089 assert(alen > 0);
90 assert(alen == ck_len);
91
92 /*
93 * Make sure they are equivalent.
94 */
95 printf("RELATIVE_OID_get_arcs() => {");
96 for(i = 0; i < alen; i++) {
97 printf(" %lu", (unsigned long)arcs[i]);
98 assert(arcs[i] == ck_buf[i]);
99 }
100 printf(" }\n");
101}
102
103/*
104 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
105 */
106static void
107check_REGEN(int *arcs, int acount) {
108 static RELATIVE_OID_t oid;
109 unsigned long tmp_arcs[10];
110 int tmp_alen = 10;
111 int alen;
112 int ret;
113 int i;
114
115 printf("Encoding {");
116 for(i = 0; i < acount; i++) {
117 printf(" %u", arcs[i]);
118 }
119 printf(" }\n");
120
121 ret = RELATIVE_OID_set_arcs_l(&oid, (unsigned long *)arcs, acount);
122 assert(ret == 0);
123
vlm2e3dd3b2004-06-14 07:24:36 +0000124 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
125 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000126 assert(alen >= 0);
127 assert(alen < tmp_alen);
128
129 printf("Encoded {");
130 for(i = 0; i < alen; i++) {
131 printf(" %lu", tmp_arcs[i]);
132 assert(arcs[i] == tmp_arcs[i]);
133 }
134 printf(" }\n");
135}
136
137/*
138 * Encode the specified array of arcs as OBJECT IDENTIFIER,
139 * decode it and compare.
140 */
141static void
142check_REGEN_OID(int *arcs, int acount) {
143 static OBJECT_IDENTIFIER_t oid;
144 unsigned long tmp_arcs[10];
145 int tmp_alen = 10;
146 int alen;
147 int ret;
148 int i;
149
150 printf("Encoding {");
151 for(i = 0; i < acount; i++) {
152 printf(" %u", arcs[i]);
153 }
154 printf(" }\n");
155
156 ret = OBJECT_IDENTIFIER_set_arcs_l(&oid, (unsigned long *)arcs, acount);
157 assert(ret == 0);
158
vlm2e3dd3b2004-06-14 07:24:36 +0000159 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
160 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000161 assert(alen >= 0);
162 assert(alen < tmp_alen);
163
164 printf("Encoded {");
165 for(i = 0; i < alen; i++) {
166 printf(" %lu", tmp_arcs[i]);
167 assert(arcs[i] == tmp_arcs[i]);
168 }
169 printf(" }\n");
170}
171
vlm2e3dd3b2004-06-14 07:24:36 +0000172static int
173check_speed() {
174 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
175 int ret = 0;
176 int cycles = 100000000;
177 double a, b, c;
178 struct timeval tv;
179 unsigned long value;
180 int i;
181
182 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
183 &value, sizeof(value));
184 assert(ret == 0);
185 assert(value == 0x7040c20d);
186
187 gettimeofday(&tv, 0);
188 a = tv.tv_sec + tv.tv_usec / 1000000.0;
189 for(i = 0; i < cycles; i++) {
190 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
191 &value, sizeof(value));
192 }
193 assert(ret == 0);
194 assert(value == 0x7040c20d);
195 gettimeofday(&tv, 0);
196 b = tv.tv_sec + tv.tv_usec / 1000000.0;
197 for(i = 0; i < cycles; i++) {
198 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0,
199 &value, sizeof(value));
200 }
201 assert(ret == 0);
202 assert(value == 0x7040c20d);
203 gettimeofday(&tv, 0);
204 c = tv.tv_sec + tv.tv_usec / 1000000.0;
205
206 a = b - a;
207 b = c - b;
208 printf("Time for single_arc(): %f\n", a);
209 printf("Time for get_arc_l(): %f\n", b);
210
211 return 0;
212}
213
vlmfa67ddc2004-06-03 03:38:44 +0000214#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
215 buf ## n ## _check, \
216 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
217#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
218 buf ## n ## _check, \
219 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
220#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
221 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
222#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
223 sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
224
225int
226main(int ac, char **av) {
vlm2e3dd3b2004-06-14 07:24:36 +0000227 /* {joint-iso-itu-t 230 3} */
vlmfa67ddc2004-06-03 03:38:44 +0000228 uint8_t buf1[] = {
229 0x06, /* OBJECT IDENTIFIER */
230 0x03, /* Length */
vlm2e3dd3b2004-06-14 07:24:36 +0000231 0x82, 0x36, 0x03
vlmfa67ddc2004-06-03 03:38:44 +0000232 };
vlm2e3dd3b2004-06-14 07:24:36 +0000233 int buf1_check[] = { 2, 230, 3 };
vlmfa67ddc2004-06-03 03:38:44 +0000234
235 /* {8571 3 2} */
236 uint8_t buf2[] = {
237 0x0D, /* RELATIVE-OID */
238 0x04, /* Length */
239 0xC2, 0x7B, 0x03, 0x02
240 };
241 int buf2_check[] = { 8571, 3, 2 };
242
243 int buf3_check[] = { 0 };
244 int buf4_check[] = { 1 };
245 int buf5_check[] = { 80, 40 };
246 int buf6_check[] = { 127 };
247 int buf7_check[] = { 128 };
248 int buf8_check[] = { 65535, 65536 };
249 int buf9_check[] = { 100000, 0x20000, 1234, 256, 127, 128 };
250 int buf10_check[] = { 0, 0xffffffff, 0xff00ff00, 0 };
251 int buf11_check[] = { 0, 1, 2 };
252 int buf12_check[] = { 1, 38, 3 };
253 int buf13_check[] = { 0, 0, 0xf000 };
254
255
256 CHECK_OID(1); /* buf1, buf1_check */
257 CHECK_ROID(2); /* buf2, buf2_check */
258
259 CHECK_REGEN(3); /* Regenerate RELATIVE-OID */
260 CHECK_REGEN(4);
261 CHECK_REGEN(5);
262 CHECK_REGEN(6);
263 CHECK_REGEN(7);
264 CHECK_REGEN(8);
265 CHECK_REGEN(9);
266 CHECK_REGEN(10);
267 CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */
268 CHECK_REGEN_OID(11);
269 CHECK_REGEN_OID(12);
270 CHECK_REGEN_OID(13);
271
vlm2e3dd3b2004-06-14 07:24:36 +0000272 check_speed();
273
vlmfa67ddc2004-06-03 03:38:44 +0000274 return 0;
275}