Lev Walkin | 4eceeba | 2007-07-23 06:48:26 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 3 | #include <sys/time.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | |
Lev Walkin | 4eceeba | 2007-07-23 06:48:26 +0000 | [diff] [blame] | 5 | #include <OBJECT_IDENTIFIER.h> |
| 6 | #include <RELATIVE-OID.h> |
| 7 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | static int |
| 9 | _print(const void *buffer, size_t size, void *app_key) { |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 10 | (void)app_key; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | fwrite(buffer, size, 1, stdout); |
| 12 | return 0; |
| 13 | } |
| 14 | |
| 15 | static void |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 16 | check_OID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | OBJECT_IDENTIFIER_t *oid; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 18 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | unsigned long arcs[10]; |
| 20 | int alen; |
| 21 | int i; |
| 22 | |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 23 | printf("%03d: Checking {", lineno); |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 24 | for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | printf("} against {"); |
| 26 | for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); } |
| 27 | printf("}\n"); |
| 28 | |
| 29 | oid = NULL; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 30 | rval = ber_decode(0, &asn_DEF_OBJECT_IDENTIFIER, (void *)&oid, buf, len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | assert(rval.code == RC_OK); |
| 32 | |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 33 | assert((ssize_t)oid->size == (ssize_t)len - 2); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Print the contents for visual debugging. |
| 37 | */ |
| 38 | printf("OBJECT_IDENTIFIER_print() => "); |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 39 | OBJECT_IDENTIFIER_print(&asn_DEF_OBJECT_IDENTIFIER, oid, 0, _print, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | printf("\n"); |
| 41 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 42 | memset(arcs, 'A', sizeof(arcs)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 43 | alen = OBJECT_IDENTIFIER_get_arcs(oid, |
| 44 | arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0])); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | assert(alen > 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 47 | printf("OBJECT_IDENTIFIER_get_arcs() => {"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | /* |
| 49 | * Make sure they are equivalent. |
| 50 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | for(i = 0; i < alen; i++) { |
| 52 | printf(" %lu", arcs[i]); |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 53 | if(alen == ck_len) { |
| 54 | assert(arcs[i] == (unsigned long)ck_buf[i]); |
| 55 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | } |
| 57 | printf(" }\n"); |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 58 | assert(alen == ck_len); |
| 59 | |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 60 | ASN_STRUCT_FREE(asn_DEF_OBJECT_IDENTIFIER, oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 64 | check_ROID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | RELATIVE_OID_t *oid; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 66 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | unsigned long arcs[10]; |
| 68 | int alen; |
| 69 | int i; |
| 70 | |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 71 | printf("%03d: Checking {", lineno); |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 72 | for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | printf("} against {"); |
| 74 | for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); } |
| 75 | printf("}\n"); |
| 76 | |
| 77 | oid = NULL; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 78 | rval = ber_decode(0, &asn_DEF_RELATIVE_OID, (void *)&oid, buf, len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | assert(rval.code == RC_OK); |
| 80 | |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 81 | assert((ssize_t)oid->size == (ssize_t)len - 2); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Print the contents for visual debugging. |
| 85 | */ |
| 86 | printf("RELATIVE_OID_print() => "); |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 87 | RELATIVE_OID_print(&asn_DEF_RELATIVE_OID, oid, 0, _print, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | printf("\n"); |
| 89 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 90 | memset(arcs, 'A', sizeof(arcs)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 91 | alen = RELATIVE_OID_get_arcs(oid, |
| 92 | arcs, sizeof(arcs[0]), sizeof(arcs)/sizeof(arcs[0])); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | 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]); |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 102 | assert(arcs[i] == (unsigned long)ck_buf[i]); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 103 | } |
| 104 | printf(" }\n"); |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 105 | ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Encode the specified array of arcs as RELATIVE-OID, decode it and compare. |
| 110 | */ |
| 111 | static void |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 112 | check_REGEN(int lineno, int *arcs, int acount) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | static RELATIVE_OID_t oid; |
| 114 | unsigned long tmp_arcs[10]; |
| 115 | int tmp_alen = 10; |
| 116 | int alen; |
| 117 | int ret; |
| 118 | int i; |
| 119 | |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 120 | if(0) { |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 121 | fprintf(stderr, "%03d: Encoding (R) {", lineno); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 122 | for(i = 0; i < acount; i++) { |
| 123 | fprintf(stderr, " %u", arcs[i]); |
| 124 | } |
| 125 | fprintf(stderr, " }\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 128 | ret = RELATIVE_OID_set_arcs(&oid, arcs, sizeof(arcs[0]), acount); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | assert(ret == 0); |
| 130 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 131 | memset(tmp_arcs, 'A', sizeof(tmp_arcs)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 132 | alen = RELATIVE_OID_get_arcs(&oid, tmp_arcs, |
| 133 | sizeof(tmp_arcs[0]), tmp_alen); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | assert(alen >= 0); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 135 | assert(alen <= tmp_alen); |
| 136 | assert(alen == acount); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 138 | if(0) { |
| 139 | fprintf(stderr, "Encoded (R) { "); |
| 140 | for(i = 0; i < alen; i++) { |
| 141 | fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout); |
| 142 | assert(arcs[i] == (int)tmp_arcs[i]); |
| 143 | } |
| 144 | fprintf(stderr, "}\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | } |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 146 | |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 147 | ASN_STRUCT_RESET(asn_DEF_RELATIVE_OID, &oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Encode the specified array of arcs as OBJECT IDENTIFIER, |
| 152 | * decode it and compare. |
| 153 | */ |
| 154 | static void |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 155 | check_REGEN_OID(int lineno, int *arcs, int acount) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | static OBJECT_IDENTIFIER_t oid; |
| 157 | unsigned long tmp_arcs[10]; |
| 158 | int tmp_alen = 10; |
| 159 | int alen; |
| 160 | int ret; |
| 161 | int i; |
| 162 | |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 163 | if(0) { |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 164 | fprintf(stderr, "%03d: Encoding (O) {", lineno); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 165 | for(i = 0; i < acount; i++) { |
| 166 | fprintf(stderr, " %u", arcs[i]); |
| 167 | } |
| 168 | fprintf(stderr, " }\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 171 | ret = OBJECT_IDENTIFIER_set_arcs(&oid, arcs, sizeof(arcs[0]), acount); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | assert(ret == 0); |
| 173 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 174 | memset(tmp_arcs, 'A', sizeof(tmp_arcs)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 175 | alen = OBJECT_IDENTIFIER_get_arcs(&oid, |
| 176 | tmp_arcs, sizeof(tmp_arcs[0]), tmp_alen); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | assert(alen >= 0); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 178 | assert(alen <= tmp_alen); |
| 179 | assert(alen == acount); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 181 | if(0) { |
| 182 | fprintf(stderr, "Encoded (O) { "); |
| 183 | for(i = 0; i < alen; i++) { |
| 184 | fprintf(stderr, "%lu ", tmp_arcs[i]); fflush(stdout); |
| 185 | assert(arcs[i] == (int)tmp_arcs[i]); |
| 186 | } |
| 187 | fprintf(stderr, "}\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | } |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 189 | |
| 190 | ASN_STRUCT_RESET(asn_DEF_RELATIVE_OID, &oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | } |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 192 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 193 | static int |
| 194 | check_speed() { |
| 195 | uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 }; |
| 196 | int ret = 0; |
| 197 | int cycles = 100000000; |
| 198 | double a, b, c; |
| 199 | struct timeval tv; |
| 200 | unsigned long value; |
| 201 | int i; |
| 202 | |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 203 | ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, &value, sizeof(value)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 204 | assert(ret == 0); |
| 205 | assert(value == 0x7040c20d); |
| 206 | |
| 207 | gettimeofday(&tv, 0); |
| 208 | a = tv.tv_sec + tv.tv_usec / 1000000.0; |
| 209 | for(i = 0; i < cycles; i++) { |
| 210 | ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, |
| 211 | &value, sizeof(value)); |
| 212 | } |
| 213 | assert(ret == 0); |
| 214 | assert(value == 0x7040c20d); |
| 215 | gettimeofday(&tv, 0); |
| 216 | b = tv.tv_sec + tv.tv_usec / 1000000.0; |
| 217 | for(i = 0; i < cycles; i++) { |
| 218 | ret = OBJECT_IDENTIFIER_get_single_arc(buf, sizeof(buf), 0, |
| 219 | &value, sizeof(value)); |
| 220 | } |
| 221 | assert(ret == 0); |
| 222 | assert(value == 0x7040c20d); |
| 223 | gettimeofday(&tv, 0); |
| 224 | c = tv.tv_sec + tv.tv_usec / 1000000.0; |
| 225 | |
| 226 | a = b - a; |
| 227 | b = c - b; |
| 228 | printf("Time for single_arc(): %f\n", a); |
| 229 | printf("Time for get_arc_l(): %f\n", b); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 234 | static void check_parse(const char *oid_txt, int retval) { |
| 235 | int ret; |
| 236 | long l[2]; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 237 | const char *p = oid_txt - 13; |
| 238 | assert(p < oid_txt); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 239 | |
| 240 | ret = OBJECT_IDENTIFIER_parse_arcs(oid_txt, -1, l, 2, &p); |
| 241 | printf("[%s] => %d == %d\n", oid_txt, ret, retval); |
| 242 | assert(ret == retval); |
| 243 | assert(p >= oid_txt); |
| 244 | } |
| 245 | |
| 246 | static void check_xer(int expect_arcs, char *xer) { |
| 247 | asn_dec_rval_t rc; |
| 248 | RELATIVE_OID_t *st = 0; |
| 249 | long arcs[10]; |
| 250 | int ret; |
| 251 | int i; |
| 252 | |
| 253 | printf("[%s] => ", xer); fflush(stdout); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 254 | rc = asn_DEF_RELATIVE_OID.op->xer_decoder(0, |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 255 | &asn_DEF_RELATIVE_OID, (void **)&st, "t", |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 256 | xer, strlen(xer)); |
| 257 | if(expect_arcs == -1) { |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 258 | if(rc.code != RC_OK) { |
| 259 | printf("-1\n"); |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 260 | ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 261 | return; |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 262 | } |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 263 | } |
| 264 | assert(rc.code == RC_OK); |
| 265 | |
| 266 | ret = RELATIVE_OID_get_arcs(st, arcs, sizeof(arcs[0]), |
| 267 | sizeof(arcs)/sizeof(arcs[0])); |
| 268 | assert(ret < 10); |
| 269 | if(expect_arcs == -1) { |
| 270 | assert(ret == -1); |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 271 | ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | for(i = 0; i < ret; i++) { |
| 275 | if(i) printf("."); |
| 276 | printf("%ld", arcs[i]); |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 277 | if(arcs[i] != i + 1) printf(" != %d\n", i + 1); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 278 | assert(arcs[i] == i + 1); |
| 279 | } |
| 280 | printf(": %d == %d\n", ret, expect_arcs); |
| 281 | assert(ret == expect_arcs); |
Lev Walkin | 229ad00 | 2017-09-18 20:13:49 -0700 | [diff] [blame] | 282 | ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Lev Walkin | 0995f35 | 2017-09-17 23:16:38 -0700 | [diff] [blame] | 285 | #define CHECK_OID(n) \ |
| 286 | check_OID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \ |
| 287 | sizeof(buf##n##_check) / sizeof(buf##n##_check[0])) |
| 288 | #define CHECK_ROID(n) \ |
| 289 | check_ROID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \ |
| 290 | sizeof(buf##n##_check) / sizeof(buf##n##_check[0])) |
| 291 | #define CHECK_REGEN(n) \ |
| 292 | check_REGEN(__LINE__, buf##n##_check, \ |
| 293 | sizeof(buf##n##_check) / sizeof(buf##n##_check[0])) |
| 294 | #define CHECK_REGEN_OID(n) \ |
| 295 | check_REGEN_OID(__LINE__, buf##n##_check, \ |
| 296 | sizeof(buf##n##_check) / sizeof(buf##n##_check[0])) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 297 | |
| 298 | int |
Lev Walkin | 46bf935 | 2004-06-14 07:40:17 +0000 | [diff] [blame] | 299 | main() { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 300 | int i; |
| 301 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 302 | /* {joint-iso-itu-t 230 3} */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | uint8_t buf1[] = { |
| 304 | 0x06, /* OBJECT IDENTIFIER */ |
| 305 | 0x03, /* Length */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 306 | 0x82, 0x36, 0x03 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 307 | }; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 308 | int buf1_check[] = { 2, 230, 3 }; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | |
| 310 | /* {8571 3 2} */ |
| 311 | uint8_t buf2[] = { |
| 312 | 0x0D, /* RELATIVE-OID */ |
| 313 | 0x04, /* Length */ |
| 314 | 0xC2, 0x7B, 0x03, 0x02 |
| 315 | }; |
| 316 | int buf2_check[] = { 8571, 3, 2 }; |
| 317 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 318 | /* {joint-iso-itu-t 42 } */ |
| 319 | uint8_t buf3[] = { |
| 320 | 0x06, /* OBJECT IDENTIFIER */ |
| 321 | 0x01, /* Length */ |
| 322 | 0x7A |
| 323 | }; |
| 324 | int buf3_check[] = { 2, 42 }; |
| 325 | |
| 326 | /* {joint-iso-itu-t 25957 } */ |
| 327 | uint8_t buf4[] = { |
| 328 | 0x06, /* OBJECT IDENTIFIER */ |
| 329 | 0x03, /* Length */ |
| 330 | 0x81, 0x80 + 0x4B, 0x35 |
| 331 | }; |
| 332 | int buf4_check[] = { 2, 25957 }; |
| 333 | |
| 334 | int buf5_check[] = { 0 }; |
| 335 | int buf6_check[] = { 1 }; |
| 336 | int buf7_check[] = { 80, 40 }; |
| 337 | int buf8_check[] = { 127 }; |
| 338 | int buf9_check[] = { 128 }; |
| 339 | int buf10_check[] = { 65535, 65536 }; |
| 340 | int buf11_check[] = { 100000, 0x20000, 1234, 256, 127, 128 }; |
| 341 | int buf12_check[] = { 0, 0xffffffff, 0xff00ff00, 0 }; |
| 342 | int buf13_check[] = { 0, 1, 2 }; |
| 343 | int buf14_check[] = { 1, 38, 3 }; |
| 344 | int buf15_check[] = { 0, 0, 0xf000 }; |
| 345 | int buf16_check[] = { 0, 0, 0, 1, 0 }; |
| 346 | int buf17_check[] = { 2, 0xffffffAf, 0xff00ff00, 0 }; |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 347 | int buf18_check[] = { 2, 2, 1, 1 }; |
| 348 | |
| 349 | /* { joint-iso-itu-t 2 1 1 } */ |
| 350 | uint8_t buf19[] = { |
| 351 | 0x06, /* OBJECT IDENTIFIER */ |
| 352 | 0x03, /* Length */ |
| 353 | 0x52, 0x01, 0x01 |
| 354 | }; |
| 355 | int buf19_check[] = { 2, 2, 1, 1 }; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 356 | |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 357 | /* { joint-iso-itu-t 2 1 0 1 } */ |
| 358 | uint8_t buf20[] = { |
| 359 | 0x06, /* OBJECT IDENTIFIER */ |
| 360 | 0x04, /* Length */ |
| 361 | 0x52, 0x01, 0x00, 0x01 |
| 362 | }; |
| 363 | int buf20_check[] = { 2, 2, 1, 0, 1 }; |
| 364 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 365 | |
| 366 | CHECK_OID(1); /* buf1, buf1_check */ |
| 367 | CHECK_ROID(2); /* buf2, buf2_check */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 368 | CHECK_OID(3); /* buf3, buf3_check */ |
| 369 | CHECK_OID(4); /* buf4, buf4_check */ |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 370 | CHECK_OID(19); /* buf19, buf19_check */ |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 371 | CHECK_OID(20); /* buf20, buf20_check */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 373 | CHECK_REGEN(5); /* Regenerate RELATIVE-OID */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | CHECK_REGEN(6); |
| 375 | CHECK_REGEN(7); |
| 376 | CHECK_REGEN(8); |
| 377 | CHECK_REGEN(9); |
| 378 | CHECK_REGEN(10); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 379 | CHECK_REGEN(11); |
| 380 | CHECK_REGEN(12); |
| 381 | CHECK_REGEN(13); |
| 382 | CHECK_REGEN(14); |
| 383 | CHECK_REGEN(15); |
| 384 | CHECK_REGEN(16); |
| 385 | CHECK_REGEN(17); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 386 | CHECK_REGEN_OID(1); /* Regenerate OBJECT IDENTIFIER */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 387 | CHECK_REGEN_OID(3); /* Regenerate OBJECT IDENTIFIER */ |
| 388 | CHECK_REGEN_OID(4); /* Regenerate OBJECT IDENTIFIER */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 389 | CHECK_REGEN_OID(13); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 390 | CHECK_REGEN_OID(14); |
| 391 | CHECK_REGEN_OID(15); |
| 392 | CHECK_REGEN_OID(16); |
| 393 | CHECK_REGEN_OID(17); |
Lev Walkin | 6c9e071 | 2004-08-22 03:01:37 +0000 | [diff] [blame] | 394 | CHECK_REGEN_OID(18); |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 395 | CHECK_REGEN_OID(19); |
| 396 | CHECK_REGEN_OID(20); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 397 | |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 398 | check_parse("", 0); |
| 399 | check_parse(" ", 0); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 400 | check_parse(".", -1); |
| 401 | check_parse(" .", -1); |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 402 | check_parse(".1", -1); |
| 403 | check_parse("1.", -1); |
| 404 | check_parse("1. ", -1); |
| 405 | check_parse(".1. ", -1); |
| 406 | check_parse(" .1. ", -1); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 407 | check_parse(" 1", 1); |
| 408 | check_parse(" 1.2", 2); |
| 409 | check_parse(" 1.", -1); |
| 410 | check_parse(" 1. ", -1); |
| 411 | check_parse("1. ", -1); |
| 412 | check_parse("1.2", 2); |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 413 | check_parse("1.2 ", 2); |
| 414 | check_parse("1.2 ", 2); |
| 415 | check_parse(" 1.2 ", 2); |
| 416 | check_parse("1. 2", -1); |
| 417 | check_parse("1 .2", -1); |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 418 | check_parse(" 1 .2", -1); |
| 419 | check_parse(" 1 .2 ", -1); |
| 420 | check_parse("1 .2 ", -1); |
| 421 | check_parse("1.+1", -1); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 422 | check_parse("10.30.234.234", 4); |
| 423 | check_parse("10.30.234.234 ", 4); |
| 424 | check_parse("10.30.234. 234 ", -1); |
| 425 | check_parse("10.30.234.234.", -1); |
| 426 | check_parse("1.2000000000.3", 3); |
| 427 | check_parse("1.2147483647.3", 3); |
| 428 | if(sizeof(long) == 4) { |
| 429 | check_parse("1.2147483648.3", -1); /* overflow on ILP32 */ |
Lev Walkin | a7d1fb3 | 2012-01-23 01:21:55 +0000 | [diff] [blame] | 430 | check_parse("1.2147483649.3", -1); /* overflow on ILP32 */ |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 431 | check_parse("1.3000000000.3", -1); |
| 432 | check_parse("1.4000000000.3", -1); |
| 433 | check_parse("1.5000000000.3", -1); |
| 434 | check_parse("1.6000000000.3", -1); |
| 435 | check_parse("1.9000000000.3", -1); |
Lev Walkin | a7d1fb3 | 2012-01-23 01:21:55 +0000 | [diff] [blame] | 436 | } else if(sizeof(long) == 8) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 437 | check_parse("1.2147483648.3", 3); |
Lev Walkin | a7d1fb3 | 2012-01-23 01:21:55 +0000 | [diff] [blame] | 438 | check_parse("1.9223372036854775807.3", 3); |
| 439 | check_parse("1.9223372036854775808.3", -1); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 440 | } |
| 441 | check_parse("1.900a0000000.3", -1); |
| 442 | check_parse("1.900a.3", -1); |
| 443 | |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 444 | check_xer(-1, "<t></t>"); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 445 | check_xer(2, "<t>1.2</t>"); |
| 446 | check_xer(3, "<t>1.2.3</t>"); |
| 447 | check_xer(3, "<t> 1.2.3 </t>"); |
| 448 | check_xer(-1, "<t>1.2.3 1</t>"); |
| 449 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 450 | for(i = 0; i < 100000; i++) { |
| 451 | int bufA_check[3] = { 2, i, rand() }; |
| 452 | int bufB_check[2] = { rand(), i * 121 }; |
| 453 | CHECK_REGEN(A); |
| 454 | CHECK_REGEN_OID(A); |
| 455 | CHECK_REGEN(B); |
| 456 | if(i > 100) i++; |
| 457 | if(i > 500) i++; |
| 458 | if(i > 1000) i += 3; |
| 459 | if(i > 5000) i += 151; |
| 460 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 461 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 462 | if(getenv("CHECK_SPEED")) { |
| 463 | /* Useful for developers only */ |
| 464 | check_speed(); |
| 465 | } |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 466 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 469 | |