blob: 472e553637780fd1ed37cb6379a3303f1bea3204 [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) {
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
vlm2e3dd3b2004-06-14 07:24:36 +000045 alen = OBJECT_IDENTIFIER_get_arcs(oid,
46 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000047 assert(alen > 0);
48 assert(alen == ck_len);
49
50 /*
51 * Make sure they are equivalent.
52 */
53 printf("OBJECT_IDENTIFIER_get_arcs() => {");
54 for(i = 0; i < alen; i++) {
55 printf(" %lu", arcs[i]);
vlma97e2242004-06-14 07:40:17 +000056 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +000057 }
58 printf(" }\n");
59}
60
61static void
62check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
63 RELATIVE_OID_t *oid;
64 ber_dec_rval_t rval;
65 unsigned long arcs[10];
66 int alen;
67 int i;
68
69 printf("Checking {");
vlma97e2242004-06-14 07:40:17 +000070 for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
vlmfa67ddc2004-06-03 03:38:44 +000071 printf("} against {");
72 for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
73 printf("}\n");
74
75 oid = NULL;
76 rval = ber_decode(&asn1_DEF_RELATIVE_OID, (void *)&oid, buf, len);
77 assert(rval.code == RC_OK);
78
vlma97e2242004-06-14 07:40:17 +000079 assert(oid->size == (ssize_t)len - 2);
vlmfa67ddc2004-06-03 03:38:44 +000080
81 /*
82 * Print the contents for visual debugging.
83 */
84 printf("RELATIVE_OID_print() => ");
85 RELATIVE_OID_print(&asn1_DEF_RELATIVE_OID, oid, 0, _print, 0);
86 printf("\n");
87
vlm2e3dd3b2004-06-14 07:24:36 +000088 alen = RELATIVE_OID_get_arcs(oid,
89 arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0]));
vlmfa67ddc2004-06-03 03:38:44 +000090 assert(alen > 0);
91 assert(alen == ck_len);
92
93 /*
94 * Make sure they are equivalent.
95 */
96 printf("RELATIVE_OID_get_arcs() => {");
97 for(i = 0; i < alen; i++) {
98 printf(" %lu", (unsigned long)arcs[i]);
vlma97e2242004-06-14 07:40:17 +000099 assert(arcs[i] == (unsigned long)ck_buf[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000100 }
101 printf(" }\n");
102}
103
104/*
105 * Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
106 */
107static void
108check_REGEN(int *arcs, int acount) {
109 static RELATIVE_OID_t oid;
110 unsigned long tmp_arcs[10];
111 int tmp_alen = 10;
112 int alen;
113 int ret;
114 int i;
115
116 printf("Encoding {");
117 for(i = 0; i < acount; i++) {
118 printf(" %u", arcs[i]);
119 }
120 printf(" }\n");
121
122 ret = RELATIVE_OID_set_arcs_l(&oid, (unsigned long *)arcs, acount);
123 assert(ret == 0);
124
vlm2e3dd3b2004-06-14 07:24:36 +0000125 alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs,
126 sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000127 assert(alen >= 0);
128 assert(alen < tmp_alen);
129
130 printf("Encoded {");
131 for(i = 0; i < alen; i++) {
132 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000133 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000134 }
135 printf(" }\n");
136}
137
138/*
139 * Encode the specified array of arcs as OBJECT IDENTIFIER,
140 * decode it and compare.
141 */
142static void
143check_REGEN_OID(int *arcs, int acount) {
144 static OBJECT_IDENTIFIER_t oid;
145 unsigned long tmp_arcs[10];
146 int tmp_alen = 10;
147 int alen;
148 int ret;
149 int i;
150
151 printf("Encoding {");
152 for(i = 0; i < acount; i++) {
153 printf(" %u", arcs[i]);
154 }
155 printf(" }\n");
156
157 ret = OBJECT_IDENTIFIER_set_arcs_l(&oid, (unsigned long *)arcs, acount);
158 assert(ret == 0);
159
vlm2e3dd3b2004-06-14 07:24:36 +0000160 alen = OBJECT_IDENTIFIER_get_arcs(&oid,
161 tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen);
vlmfa67ddc2004-06-03 03:38:44 +0000162 assert(alen >= 0);
163 assert(alen < tmp_alen);
164
165 printf("Encoded {");
166 for(i = 0; i < alen; i++) {
167 printf(" %lu", tmp_arcs[i]);
vlma97e2242004-06-14 07:40:17 +0000168 assert((unsigned long)arcs[i] == tmp_arcs[i]);
vlmfa67ddc2004-06-03 03:38:44 +0000169 }
170 printf(" }\n");
171}
172
vlm2e3dd3b2004-06-14 07:24:36 +0000173static int
174check_speed() {
175 uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
176 int ret = 0;
177 int cycles = 100000000;
178 double a, b, c;
179 struct timeval tv;
180 unsigned long value;
181 int i;
182
vlma97e2242004-06-14 07:40:17 +0000183 ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value));
vlm2e3dd3b2004-06-14 07:24:36 +0000184 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
vlma97e2242004-06-14 07:40:17 +0000226main() {
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}