vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #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" |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 9 | #include <sys/time.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | |
| 11 | static int |
| 12 | _print(const void *buffer, size_t size, void *app_key) { |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 13 | (void)app_key; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | fwrite(buffer, size, 1, stdout); |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | static void |
| 19 | check_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 {"); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 27 | for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | 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 | |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 36 | assert(oid->size == (ssize_t)len - 2); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | |
| 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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 45 | alen = OBJECT_IDENTIFIER_get_arcs(oid, |
| 46 | arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0])); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | 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]); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 56 | assert(arcs[i] == (unsigned long)ck_buf[i]); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 57 | } |
| 58 | printf(" }\n"); |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | check_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 {"); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 70 | for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | 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 | |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 79 | assert(oid->size == (ssize_t)len - 2); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | |
| 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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 88 | alen = RELATIVE_OID_get_arcs(oid, |
| 89 | arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0])); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | 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]); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 99 | assert(arcs[i] == (unsigned long)ck_buf[i]); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | } |
| 101 | printf(" }\n"); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Encode the specified array of arcs as RELATIVE-OID, decode it and compare. |
| 106 | */ |
| 107 | static void |
| 108 | check_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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 125 | alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs, |
| 126 | sizeof(tmp_arcs[0]), tmp_alen); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | 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]); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 133 | assert((unsigned long)arcs[i] == tmp_arcs[i]); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | } |
| 135 | printf(" }\n"); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Encode the specified array of arcs as OBJECT IDENTIFIER, |
| 140 | * decode it and compare. |
| 141 | */ |
| 142 | static void |
| 143 | check_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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 160 | alen = OBJECT_IDENTIFIER_get_arcs(&oid, |
| 161 | tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | 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]); |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 168 | assert((unsigned long)arcs[i] == tmp_arcs[i]); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | } |
| 170 | printf(" }\n"); |
| 171 | } |
| 172 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 173 | static int |
| 174 | check_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 | |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 183 | ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value)); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 214 | #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 | |
| 225 | int |
vlm | a97e224 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 226 | main() { |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 227 | /* {joint-iso-itu-t 230 3} */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 228 | uint8_t buf1[] = { |
| 229 | 0x06, /* OBJECT IDENTIFIER */ |
| 230 | 0x03, /* Length */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 231 | 0x82, 0x36, 0x03 |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | }; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 233 | int buf1_check[] = { 2, 230, 3 }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | |
| 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 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 272 | if(getenv("CHECK_SPEED")) { |
| 273 | /* Useful for developers only */ |
| 274 | check_speed(); |
| 275 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 276 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | } |