blob: 4ec55a489ead13a72a03251c76ce3f2cd9e73416 [file] [log] [blame]
Lev Walkind49ea282007-06-23 20:18:15 +00001/*
2 * Mode of operation:
3 * Each of the *.in files is XER-decoded, then converted into PER,
4 * compared with *.out file, then decoded and compared with the original.
5 */
6#undef NDEBUG
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/types.h>
10#include <unistd.h> /* for chdir(2) */
11#include <string.h>
12#include <dirent.h>
13#include <assert.h>
14#include <ctype.h>
15#include <errno.h>
16
17#include <PDU.h>
18
Lev Walkine0d321a2014-09-11 01:28:57 -070019#ifndef SRCDIR
20#define SRCDIR_S ".."
21#else
22#define STRINGIFY_MACRO2(x) #x
23#define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
24#define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
25#endif
26
Lev Walkind49ea282007-06-23 20:18:15 +000027static unsigned char buf[4096];
28static int buf_offset;
29
30static int
31_buf_writer(const void *buffer, size_t size, void *app_key) {
Lev Walkind49ea282007-06-23 20:18:15 +000032 (void)app_key;
33 assert(buf_offset + size < sizeof(buf));
34 memcpy(buf + buf_offset, buffer, size);
Lev Walkinb1f4f332017-10-19 03:28:49 -070035#ifdef EMIT_ASN_DEBUG
36 unsigned char *b, *bend;
Lev Walkind49ea282007-06-23 20:18:15 +000037 b = buf + buf_offset;
38 bend = b + size;
39 fprintf(stderr, "=> [");
40 for(; b < bend; b++) {
41 if(*b >= 32 && *b < 127 && *b != '%')
42 fprintf(stderr, "%c", *b);
43 else
44 fprintf(stderr, "%%%02x", *b);
45 }
Lev Walkin1715b322013-03-28 04:02:13 -070046 fprintf(stderr, "]:%zd\n", size);
Lev Walkin15d38f42014-09-17 01:19:31 -070047#endif
Lev Walkind49ea282007-06-23 20:18:15 +000048 buf_offset += size;
49 return 0;
50}
51
52enum enctype {
53 AS_PER,
54 AS_DER,
55 AS_XER,
Lev Walkind49ea282007-06-23 20:18:15 +000056};
57
58static void
59save_object_as(PDU_t *st, enum enctype how) {
60 asn_enc_rval_t rval; /* Return value */
61
62 buf_offset = 0;
63
64 /*
65 * Save object using specified method.
66 */
67 switch(how) {
68 case AS_PER:
69 rval = uper_encode(&asn_DEF_PDU, st, _buf_writer, 0);
70 assert(rval.encoded > 0);
71 fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
72 return;
73 case AS_DER:
74 rval = der_encode(&asn_DEF_PDU, st,
75 _buf_writer, 0);
76 break;
77 case AS_XER:
78 rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
79 _buf_writer, 0);
80 break;
Lev Walkinb1f4f332017-10-19 03:28:49 -070081 default:
82 assert(!"Unreachable");
Lev Walkind49ea282007-06-23 20:18:15 +000083 }
84
85 if (rval.encoded == -1) {
86 fprintf(stderr,
87 "Cannot encode %s: %s\n",
88 rval.failed_type->name, strerror(errno));
89 assert(rval.encoded != -1);
90 return;
91 }
92
93 fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
94}
95
96static PDU_t *
Lev Walkin5b63acf2014-01-14 01:48:37 -080097load_object_from(const char *fname, unsigned char *fbuf, size_t size, enum enctype how, int mustfail) {
Lev Walkind49ea282007-06-23 20:18:15 +000098 asn_dec_rval_t rval;
99 PDU_t *st = 0;
Lev Walkin1715b322013-03-28 04:02:13 -0700100 ssize_t csize = 1;
Lev Walkind49ea282007-06-23 20:18:15 +0000101
102 if(getenv("INITIAL_CHUNK_SIZE"))
103 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
104
105 /* Perform multiple iterations with multiple chunks sizes */
106 for(; csize < 20; csize += 1) {
107 int fbuf_offset = 0;
108 int fbuf_left = size;
109 int fbuf_chunk = csize;
110
Lev Walkin1715b322013-03-28 04:02:13 -0700111 fprintf(stderr, "LOADING OBJECT OF SIZE %zd FROM [%s] as %s,"
112 " chunks %zd\n",
Lev Walkind49ea282007-06-23 20:18:15 +0000113 size, fname, how==AS_PER?"PER":"XER", csize);
114
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800115 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkind49ea282007-06-23 20:18:15 +0000116 st = 0;
117
118 do {
Lev Walkin3c7ac6c2014-09-17 01:24:10 -0700119 ASN_DEBUG("\nDecoding bytes %d..%d (left %d) [%s]",
Lev Walkind49ea282007-06-23 20:18:15 +0000120 fbuf_offset,
121 fbuf_chunk < fbuf_left
122 ? fbuf_chunk : fbuf_left,
Lev Walkin7ed25982007-06-24 08:38:34 +0000123 fbuf_left,
124 fname);
Lev Walkin3c7ac6c2014-09-17 01:24:10 -0700125#ifdef EMIT_ASN_DEBUG
Lev Walkind49ea282007-06-23 20:18:15 +0000126 if(st) {
127 fprintf(stderr, "=== currently ===\n");
128 asn_fprint(stderr, &asn_DEF_PDU, st);
129 fprintf(stderr, "=== end ===\n");
130 }
Lev Walkin3c7ac6c2014-09-17 01:24:10 -0700131#endif
Lev Walkind49ea282007-06-23 20:18:15 +0000132 switch(how) {
133 case AS_XER:
134 rval = xer_decode(0, &asn_DEF_PDU, (void **)&st,
135 fbuf + fbuf_offset,
136 fbuf_chunk < fbuf_left
137 ? fbuf_chunk : fbuf_left);
138 break;
Lev Walkinc9a78472007-06-26 06:47:18 +0000139 case AS_DER:
140 assert(0);
141 break;
Lev Walkind49ea282007-06-23 20:18:15 +0000142 case AS_PER:
143 rval = uper_decode(0, &asn_DEF_PDU,
144 (void **)&st, fbuf + fbuf_offset,
145 fbuf_chunk < fbuf_left
146 ? fbuf_chunk : fbuf_left, 0, 0);
147 if(rval.code == RC_WMORE) {
148 if(fbuf_chunk == fbuf_left) {
Lev Walkin1715b322013-03-28 04:02:13 -0700149 fprintf(stderr, "-> PER decode error (%zd bits of %zd bytes (c=%d,l=%d)) \n", rval.consumed, size, fbuf_chunk, fbuf_left);
Lev Walkind49ea282007-06-23 20:18:15 +0000150 rval.code = RC_FAIL;
151 rval.consumed += 7;
152 rval.consumed /= 8;
Lev Walkin7ed25982007-06-24 08:38:34 +0000153 if(mustfail) {
154 fprintf(stderr, "-> (this was expected failure)\n");
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300155 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin7ed25982007-06-24 08:38:34 +0000156 return 0;
157 }
Lev Walkind49ea282007-06-23 20:18:15 +0000158 } else {
159 rval.consumed = 0; /* Not restartable */
160 ASN_STRUCT_FREE(asn_DEF_PDU, st);
161 st = 0;
Lev Walkin15d38f42014-09-17 01:19:31 -0700162 ASN_DEBUG("-> PER wants more");
Lev Walkind49ea282007-06-23 20:18:15 +0000163 }
164 } else {
Lev Walkin15d38f42014-09-17 01:19:31 -0700165 ASN_DEBUG("-> PER ret %d/%zd mf=%d",
Lev Walkin7ed25982007-06-24 08:38:34 +0000166 rval.code, rval.consumed, mustfail);
Lev Walkind49ea282007-06-23 20:18:15 +0000167 /* uper_decode() returns _bits_ */
168 rval.consumed += 7;
169 rval.consumed /= 8;
Lev Walkin7ed25982007-06-24 08:38:34 +0000170 if((mustfail?1:0) == (rval.code == RC_FAIL)) {
171 if(mustfail) {
172 fprintf(stderr, "-> (this was expected failure)\n");
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300173 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkinc9a78472007-06-26 06:47:18 +0000174 return 0;
Lev Walkin7ed25982007-06-24 08:38:34 +0000175 }
176 } else {
177 fprintf(stderr, "-> (unexpected %s)\n", mustfail ? "success" : "failure");
Lev Walkinfcb0efb2007-06-26 06:46:31 +0000178 rval.code = RC_FAIL;
Lev Walkin7ed25982007-06-24 08:38:34 +0000179 }
Lev Walkind49ea282007-06-23 20:18:15 +0000180 }
181 break;
Lev Walkinb1f4f332017-10-19 03:28:49 -0700182 default:
183 assert(!"Unreachable");
Lev Walkind49ea282007-06-23 20:18:15 +0000184 }
185 fbuf_offset += rval.consumed;
186 fbuf_left -= rval.consumed;
187 if(rval.code == RC_WMORE)
188 fbuf_chunk += 1; /* Give little more */
189 else
190 fbuf_chunk = csize; /* Back off */
191 } while(fbuf_left && rval.code == RC_WMORE);
192
193 assert(rval.code == RC_OK);
194 if(how == AS_PER) {
Lev Walkin1715b322013-03-28 04:02:13 -0700195 fprintf(stderr, "[left %d, off %d, size %zd]\n",
Lev Walkind49ea282007-06-23 20:18:15 +0000196 fbuf_left, fbuf_offset, size);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800197 assert(fbuf_offset == (ssize_t)size);
Lev Walkind49ea282007-06-23 20:18:15 +0000198 } else {
Lev Walkina75b2472017-09-18 00:38:43 -0700199 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkind49ea282007-06-23 20:18:15 +0000200 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800201 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkind49ea282007-06-23 20:18:15 +0000202 && fbuf[size - 2] == '\r'
203 && fbuf[size - 1] == '\n')
204 );
205 }
206 }
207
208 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
209 return st;
210}
211
212static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800213xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
214 char *obuf = obufp;
215 char *nbuf = nbufp;
Lev Walkind49ea282007-06-23 20:18:15 +0000216 char *oend = obuf + osize;
217 char *nend = nbuf + nsize;
218
219 if((osize && !nsize) || (!osize && nsize))
220 return 0; /* not equal apriori */
221
222 while(1) {
223 while(obuf < oend && isspace(*obuf)) obuf++;
224 while(nbuf < nend && isspace(*nbuf)) nbuf++;
225
226 if(obuf == oend || nbuf == nend) {
227 if(obuf == oend && nbuf == nend)
228 break;
229 fprintf(stderr, "%s data in reconstructed encoding\n",
230 (obuf == oend) ? "More" : "Less");
231 return 0;
232 }
233
234 if(*obuf != *nbuf) {
235 printf("%c%c != %c%c\n",
236 obuf[0], obuf[1],
237 nbuf[0], nbuf[1]);
238 return 0;
239 }
240 obuf++, nbuf++;
241 }
242
243 return 1;
244}
245
246static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800247compare_with_data_out(const char *fname, void *datap, size_t size) {
248 char *data = datap;
Lev Walkine0d321a2014-09-11 01:28:57 -0700249 char outName[sizeof(SRCDIR_S) + 256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800250 unsigned char fbuf[1024];
Lev Walkin62258e22007-06-23 23:50:25 +0000251 size_t rd;
252 FILE *f;
Lev Walkinb4326de2007-06-26 09:54:17 +0000253 char lastChar;
254 int mustfail, compare;
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300255 PDU_t *st;
Lev Walkin62258e22007-06-23 23:50:25 +0000256
Lev Walkine0d321a2014-09-11 01:28:57 -0700257 sprintf(outName, SRCDIR_S "/data-126/%s", fname);
Lev Walkin62258e22007-06-23 23:50:25 +0000258 strcpy(outName + strlen(outName) - 3, ".out");
259
260 fprintf(stderr, "Comparing PER output with [%s]\n", outName);
261
Lev Walkinb4326de2007-06-26 09:54:17 +0000262 lastChar = outName[strlen(outName)-5];
263 mustfail = lastChar == 'P';
264 compare = lastChar != 'C';
Lev Walkin73a5cc62007-06-24 08:45:31 +0000265
Lev Walkinac6db372007-06-26 10:14:11 +0000266 if((compare && !mustfail) && getenv("REGENERATE")) {
Lev Walkinf55a6dd2007-06-24 00:35:51 +0000267 f = fopen(outName, "w");
Lev Walkin5b63acf2014-01-14 01:48:37 -0800268 fwrite(data, 1, size, f);
Lev Walkinf55a6dd2007-06-24 00:35:51 +0000269 fclose(f);
270 } else {
271 f = fopen(outName, "r");
Lev Walkin62258e22007-06-23 23:50:25 +0000272 assert(f);
273 rd = fread(fbuf, 1, sizeof(fbuf), f);
274 assert(rd);
275 fclose(f);
276
Lev Walkin7ed25982007-06-24 08:38:34 +0000277 fprintf(stderr, "Trying to decode [%s]\n", outName);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300278 st = load_object_from(outName, fbuf, rd, AS_PER, mustfail);
279 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin7ed25982007-06-24 08:38:34 +0000280 if(mustfail) return;
281
Lev Walkinb4326de2007-06-26 09:54:17 +0000282 if(compare) {
283 assert(rd == (size_t)size);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800284 assert(memcmp(fbuf, data, rd) == 0);
Lev Walkinb4326de2007-06-26 09:54:17 +0000285 fprintf(stderr, "XER->PER recoding .in->.out match.\n");
286 } else {
Lev Walkin5b63acf2014-01-14 01:48:37 -0800287 assert(rd != (size_t)size || memcmp(fbuf, data, rd));
Lev Walkinb4326de2007-06-26 09:54:17 +0000288 fprintf(stderr, "XER->PER recoding .in->.out diverge.\n");
289 }
Lev Walkin62258e22007-06-23 23:50:25 +0000290 }
291}
292
293static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800294process_XER_data(const char *fname, unsigned char *fbuf, size_t size) {
Lev Walkind49ea282007-06-23 20:18:15 +0000295 PDU_t *st;
Lev Walkind49ea282007-06-23 20:18:15 +0000296
Lev Walkin7ed25982007-06-24 08:38:34 +0000297 st = load_object_from(fname, fbuf, size, AS_XER, 0);
Lev Walkind49ea282007-06-23 20:18:15 +0000298 if(!st) return;
299
Lev Walkin62258e22007-06-23 23:50:25 +0000300 /* Save and re-load as PER */
Lev Walkind49ea282007-06-23 20:18:15 +0000301 save_object_as(st, AS_PER);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300302 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin62258e22007-06-23 23:50:25 +0000303 compare_with_data_out(fname, buf, buf_offset);
Lev Walkin7ed25982007-06-24 08:38:34 +0000304 st = load_object_from("buffer", buf, buf_offset, AS_PER, 0);
Lev Walkind49ea282007-06-23 20:18:15 +0000305 assert(st);
306
307 save_object_as(st, AS_XER);
308 fprintf(stderr, "=== original ===\n");
309 fwrite(fbuf, 1, size, stderr);
310 fprintf(stderr, "=== re-encoded ===\n");
311 fwrite(buf, 1, buf_offset, stderr);
312 fprintf(stderr, "=== end ===\n");
313
Lev Walkin8032f7a2007-06-27 01:54:57 +0000314 if(fname[strlen(fname) - 4] == 'X')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800315 assert(!xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
Lev Walkin8032f7a2007-06-27 01:54:57 +0000316 else
Lev Walkin5b63acf2014-01-14 01:48:37 -0800317 assert(xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
Lev Walkind49ea282007-06-23 20:18:15 +0000318
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800319 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkind49ea282007-06-23 20:18:15 +0000320}
321
322/*
323 * Decode the .der files and try to regenerate them.
324 */
325static int
326process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700327 unsigned char fbuf[sizeof(SRCDIR_S) + 4096];
Lev Walkind49ea282007-06-23 20:18:15 +0000328 char *ext = strrchr(fname, '.');
Lev Walkind49ea282007-06-23 20:18:15 +0000329 int rd;
330 FILE *fp;
331
332 if(ext == 0 || strcmp(ext, ".in"))
333 return 0;
334
335 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
336
Lev Walkine0d321a2014-09-11 01:28:57 -0700337 snprintf((char *)fbuf, sizeof(fbuf), SRCDIR_S "/data-126/%s", fname);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800338 fp = fopen((char *)fbuf, "r");
Lev Walkind49ea282007-06-23 20:18:15 +0000339 assert(fp);
340
341 rd = fread(fbuf, 1, sizeof(fbuf), fp);
342 fclose(fp);
343
Lev Walkinc9a78472007-06-26 06:47:18 +0000344 assert((size_t)rd < sizeof(fbuf)); /* expect small files */
Lev Walkind49ea282007-06-23 20:18:15 +0000345
346 process_XER_data(fname, fbuf, rd);
347
348 fprintf(stderr, "Finished [%s]\n", fname);
349
350 return 1;
351}
352
353int
354main() {
355 DIR *dir;
356 struct dirent *dent;
357 int processed_files = 0;
358 char *str;
359
360 /* Process a specific test file */
361 str = getenv("DATA_126_FILE");
362 if(str && strncmp(str, "data-126-", 9) == 0) {
363 process(str);
364 return 0;
365 }
366
Lev Walkine0d321a2014-09-11 01:28:57 -0700367 dir = opendir(SRCDIR_S "/data-126");
Lev Walkind49ea282007-06-23 20:18:15 +0000368 assert(dir);
369
370 /*
371 * Process each file in that directory.
372 */
373 while((dent = readdir(dir))) {
374 if(strncmp(dent->d_name, "data-126-", 9) == 0)
375 if(process(dent->d_name))
376 processed_files++;
377 }
378
379 assert(processed_files);
380 closedir(dir);
381
382 return 0;
383}
384