Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mode of operation: |
| 3 | * Each of the *.in files is XER-decoded, then converted into DER, |
| 4 | * then decoded from DER and encoded into XER again. The resulting |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 5 | * stream is checked against rules specified in ../data-70/README file. |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 6 | */ |
| 7 | #undef NDEBUG |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <sys/types.h> |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 11 | #include <unistd.h> /* for chdir(2), getcwd(3) */ |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 12 | #include <string.h> |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 13 | #include <ctype.h> |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 14 | #include <dirent.h> |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <errno.h> |
| 17 | |
| 18 | #include <PDU.h> |
| 19 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 20 | #ifndef SRCDIR |
| 21 | #define SRCDIR_S ".." |
| 22 | #else |
| 23 | #define STRINGIFY_MACRO2(x) #x |
| 24 | #define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x) |
| 25 | #define SRCDIR_S STRINGIFY_MACRO(SRCDIR) |
| 26 | #endif |
| 27 | |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 28 | enum expectation { |
| 29 | EXP_OK, /* Encoding/decoding must succeed */ |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 30 | EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */ |
| 31 | EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */ |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 32 | EXP_BROKEN, /* Decoding must fail */ |
| 33 | EXP_DIFFERENT, /* Reconstruction will yield different encoding */ |
| 34 | }; |
| 35 | |
| 36 | static unsigned char buf[4096]; |
| 37 | static int buf_offset; |
| 38 | |
| 39 | static int |
| 40 | _buf_writer(const void *buffer, size_t size, void *app_key) { |
| 41 | unsigned char *b, *bend; |
| 42 | (void)app_key; |
| 43 | assert(buf_offset + size < sizeof(buf)); |
| 44 | memcpy(buf + buf_offset, buffer, size); |
| 45 | b = buf + buf_offset; |
| 46 | bend = b + size; |
| 47 | fprintf(stderr, "=> ["); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 48 | for(; b < bend; b++) { |
| 49 | if(*b >= 32 && *b < 127 && *b != '%') |
| 50 | fprintf(stderr, "%c", *b); |
| 51 | else |
| 52 | fprintf(stderr, "%%%02x", *b); |
| 53 | } |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 54 | fprintf(stderr, "]:%zd\n", size); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 55 | buf_offset += size; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | enum der_or_xer { |
| 60 | AS_DER, |
| 61 | AS_XER, |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 62 | AS_CXER, |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static void |
| 66 | save_object_as(PDU_t *st, enum der_or_xer how) { |
| 67 | asn_enc_rval_t rval; /* Return value */ |
| 68 | |
| 69 | buf_offset = 0; |
| 70 | |
| 71 | /* |
| 72 | * Save object using specified method. |
| 73 | */ |
| 74 | switch(how) { |
| 75 | case AS_DER: |
| 76 | rval = der_encode(&asn_DEF_PDU, st, |
| 77 | _buf_writer, 0); |
| 78 | break; |
| 79 | case AS_XER: |
| 80 | rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC, |
| 81 | _buf_writer, 0); |
| 82 | break; |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 83 | case AS_CXER: |
| 84 | rval = xer_encode(&asn_DEF_PDU, st, XER_F_CANONICAL, |
| 85 | _buf_writer, 0); |
| 86 | break; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 87 | } |
| 88 | if (rval.encoded == -1) { |
| 89 | fprintf(stderr, |
| 90 | "Cannot encode %s: %s\n", |
| 91 | rval.failed_type->name, strerror(errno)); |
| 92 | assert(rval.encoded != -1); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); |
| 97 | } |
| 98 | |
| 99 | static PDU_t * |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 100 | load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum der_or_xer how) { |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 101 | asn_dec_rval_t rval; |
| 102 | asn_dec_rval_t (*zer_decode)(struct asn_codec_ctx_s *, |
| 103 | asn_TYPE_descriptor_t *, void **, const void *, size_t); |
| 104 | PDU_t *st = 0; |
Lev Walkin | b7c40b6 | 2013-03-28 05:54:12 -0700 | [diff] [blame] | 105 | size_t csize = 1; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 106 | |
| 107 | if(how == AS_DER) |
| 108 | zer_decode = ber_decode; |
| 109 | else |
| 110 | zer_decode = xer_decode; |
| 111 | |
| 112 | if(getenv("INITIAL_CHUNK_SIZE")) |
| 113 | csize = atoi(getenv("INITIAL_CHUNK_SIZE")); |
| 114 | |
| 115 | /* Perform multiple iterations with multiple chunks sizes */ |
| 116 | for(; csize < 20; csize += 1) { |
| 117 | int fbuf_offset = 0; |
| 118 | int fbuf_left = size; |
| 119 | int fbuf_chunk = csize; |
| 120 | |
Lev Walkin | b7c40b6 | 2013-03-28 05:54:12 -0700 | [diff] [blame] | 121 | fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n", |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 122 | size, csize); |
| 123 | |
| 124 | if(st) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0); |
| 125 | st = 0; |
| 126 | |
| 127 | do { |
Lev Walkin | 15d38f4 | 2014-09-17 01:19:31 -0700 | [diff] [blame] | 128 | ASN_DEBUG("Decoding bytes %d..%d (left %d)", |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 129 | fbuf_offset, |
| 130 | fbuf_chunk < fbuf_left |
| 131 | ? fbuf_chunk : fbuf_left, |
| 132 | fbuf_left); |
Lev Walkin | 15d38f4 | 2014-09-17 01:19:31 -0700 | [diff] [blame] | 133 | #ifdef EMIT_ASN_DEBUG |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 134 | if(st) { |
| 135 | fprintf(stderr, "=== currently ===\n"); |
| 136 | asn_fprint(stderr, &asn_DEF_PDU, st); |
| 137 | fprintf(stderr, "=== end ===\n"); |
| 138 | } |
Lev Walkin | 15d38f4 | 2014-09-17 01:19:31 -0700 | [diff] [blame] | 139 | #endif |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 140 | rval = zer_decode(0, &asn_DEF_PDU, (void **)&st, |
| 141 | fbuf + fbuf_offset, |
| 142 | fbuf_chunk < fbuf_left |
| 143 | ? fbuf_chunk : fbuf_left); |
| 144 | fbuf_offset += rval.consumed; |
| 145 | fbuf_left -= rval.consumed; |
| 146 | if(rval.code == RC_WMORE) |
| 147 | fbuf_chunk += 1; /* Give little more */ |
| 148 | else |
| 149 | fbuf_chunk = csize; /* Back off */ |
| 150 | } while(fbuf_left && rval.code == RC_WMORE); |
| 151 | |
| 152 | if(expectation != EXP_BROKEN) { |
| 153 | assert(rval.code == RC_OK); |
| 154 | if(how == AS_DER) { |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 155 | assert(fbuf_offset == (ssize_t)size); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 156 | } else { |
| 157 | assert(fbuf_offset - size < 2 |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 158 | || (fbuf_offset + 1 /* "\n" */ == (ssize_t)size |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 159 | && fbuf[size - 1] == '\n') |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 160 | || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 161 | && fbuf[size - 2] == '\r' |
| 162 | && fbuf[size - 1] == '\n') |
| 163 | ); |
| 164 | } |
| 165 | } else { |
| 166 | assert(rval.code != RC_OK); |
| 167 | fprintf(stderr, "Failed, but this was expected\n"); |
| 168 | asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0); |
| 169 | st = 0; /* ignore leak for now */ |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if(st) asn_fprint(stderr, &asn_DEF_PDU, st); |
| 174 | return st; |
| 175 | } |
| 176 | |
| 177 | static int |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 178 | xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) { |
| 179 | char *obuf = obufp; |
| 180 | char *nbuf = nbufp; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 181 | char *oend = obuf + osize; |
| 182 | char *nend = nbuf + nsize; |
| 183 | |
| 184 | if((osize && !nsize) || (!osize && nsize)) |
| 185 | return 0; /* not equal apriori */ |
| 186 | |
| 187 | while(1) { |
| 188 | while(obuf < oend && isspace(*obuf)) obuf++; |
| 189 | while(nbuf < nend && isspace(*nbuf)) nbuf++; |
| 190 | |
| 191 | if(obuf == oend || nbuf == nend) { |
| 192 | if(obuf == oend && nbuf == nend) |
| 193 | break; |
| 194 | fprintf(stderr, "%s data in reconstructed encoding\n", |
| 195 | (obuf == oend) ? "More" : "Less"); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | if(*obuf != *nbuf) { |
| 200 | printf("%c%c != %c%c\n", |
| 201 | obuf[0], obuf[1], |
| 202 | nbuf[0], nbuf[1]); |
| 203 | return 0; |
| 204 | } |
| 205 | obuf++, nbuf++; |
| 206 | } |
| 207 | |
| 208 | return 1; |
| 209 | } |
| 210 | |
| 211 | static void |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 212 | process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) { |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 213 | PDU_t *st; |
| 214 | |
| 215 | st = load_object_from(expectation, fbuf, size, AS_XER); |
| 216 | if(!st) return; |
| 217 | |
| 218 | /* Save and re-load as DER */ |
| 219 | save_object_as(st, AS_DER); |
| 220 | st = load_object_from(expectation, buf, buf_offset, AS_DER); |
| 221 | assert(st); |
| 222 | |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 223 | save_object_as(st, |
| 224 | (expectation == EXP_CXER_EXACT |
| 225 | || expectation == EXP_CXER_DIFF) |
| 226 | ? AS_CXER : AS_XER); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 227 | fprintf(stderr, "=== original ===\n"); |
| 228 | fwrite(fbuf, 1, size, stderr); |
| 229 | fprintf(stderr, "=== re-encoded ===\n"); |
| 230 | fwrite(buf, 1, buf_offset, stderr); |
| 231 | fprintf(stderr, "=== end ===\n"); |
| 232 | |
| 233 | switch(expectation) { |
| 234 | case EXP_DIFFERENT: |
| 235 | assert(!xer_encoding_equal(fbuf, size, buf, buf_offset)); |
| 236 | break; |
| 237 | case EXP_BROKEN: |
| 238 | assert(!xer_encoding_equal(fbuf, size, buf, buf_offset)); |
| 239 | break; |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 240 | case EXP_CXER_EXACT: |
| 241 | buf[buf_offset++] = '\n'; |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 242 | assert((ssize_t)size == buf_offset); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 243 | assert(memcmp(fbuf, buf, size) == 0); |
| 244 | break; |
| 245 | case EXP_CXER_DIFF: |
| 246 | buf[buf_offset++] = '\n'; |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 247 | assert((ssize_t)size != buf_offset |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 248 | || memcmp(fbuf, buf, size)); |
| 249 | break; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 250 | case EXP_OK: |
| 251 | assert(xer_encoding_equal(fbuf, size, buf, buf_offset)); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Decode the .der files and try to regenerate them. |
| 260 | */ |
| 261 | static int |
| 262 | process(const char *fname) { |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 263 | char prevdir[256]; |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 264 | unsigned char fbuf[4096]; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 265 | char *ext = strrchr(fname, '.'); |
| 266 | enum expectation expectation; |
| 267 | int ret; |
| 268 | int rd; |
| 269 | FILE *fp; |
| 270 | |
| 271 | if(ext == 0 || strcmp(ext, ".in")) |
| 272 | return 0; |
| 273 | |
| 274 | switch(ext[-1]) { |
| 275 | case 'B': /* The file is intentionally broken */ |
| 276 | expectation = EXP_BROKEN; break; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 277 | case 'D': /* Reconstructing should yield different data */ |
| 278 | expectation = EXP_DIFFERENT; break; |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 279 | case 'E': /* Byte to byte exact reconstruction */ |
| 280 | expectation = EXP_CXER_EXACT; break; |
| 281 | case 'X': /* Should fail byte-to-byte comparison */ |
| 282 | expectation = EXP_CXER_DIFF; break; |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 283 | default: |
| 284 | expectation = EXP_OK; break; |
| 285 | } |
| 286 | |
| 287 | fprintf(stderr, "\nProcessing file [../%s]\n", fname); |
| 288 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 289 | getcwd(prevdir, sizeof(prevdir)); |
| 290 | ret = chdir(SRCDIR_S "/data-70"); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 291 | assert(ret == 0); |
| 292 | fp = fopen(fname, "r"); |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 293 | ret = chdir(prevdir); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 294 | assert(ret == 0); |
| 295 | assert(fp); |
| 296 | |
| 297 | rd = fread(fbuf, 1, sizeof(fbuf), fp); |
| 298 | fclose(fp); |
| 299 | |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 300 | assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */ |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 301 | |
| 302 | process_XER_data(expectation, fbuf, rd); |
| 303 | |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | int |
| 308 | main() { |
| 309 | DIR *dir; |
| 310 | struct dirent *dent; |
| 311 | int processed_files = 0; |
| 312 | char *str; |
| 313 | |
| 314 | /* Process a specific test file */ |
| 315 | str = getenv("DATA_70_FILE"); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 316 | if(str && strncmp(str, "data-70-", 8) == 0) { |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 317 | process(str); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 318 | return 0; |
| 319 | } |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 320 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 321 | dir = opendir(SRCDIR_S "/data-70"); |
Lev Walkin | 397d59d | 2006-07-13 11:11:33 +0000 | [diff] [blame] | 322 | assert(dir); |
| 323 | |
| 324 | /* |
| 325 | * Process each file in that directory. |
| 326 | */ |
| 327 | while((dent = readdir(dir))) { |
| 328 | if(strncmp(dent->d_name, "data-70-", 8) == 0) |
| 329 | if(process(dent->d_name)) |
| 330 | processed_files++; |
| 331 | } |
| 332 | |
| 333 | assert(processed_files); |
| 334 | closedir(dir); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |