3 * Each of the *.in files is XER-decoded, then converted into PER,
4 * then decoded back from PER, then encoded into XER again,
5 * and finally compared to the original encoding.
10 #include <sys/types.h>
11 #include <unistd.h> /* for chdir(2) */
23 #define STRINGIFY_MACRO2(x) #x
24 #define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
25 #define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
29 EXP_OK, /* Encoding/decoding must succeed */
30 EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
31 EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
32 EXP_BROKEN, /* Decoding must fail */
33 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
34 EXP_PER_NOCOMP, /* Not PER compatible */
37 static unsigned char buf[4096];
38 static size_t buf_offset;
41 _buf_writer(const void *buffer, size_t size, void *app_key) {
43 assert(buf_offset + size < sizeof(buf));
44 memcpy(buf + buf_offset, buffer, size);
46 unsigned char *b, *bend;
49 fprintf(stderr, "=> [");
50 for(; b < bend; b++) {
51 if(*b >= 32 && *b < 127 && *b != '%')
52 fprintf(stderr, "%c", *b);
54 fprintf(stderr, "%%%02x", *b);
56 fprintf(stderr, "]:%zd\n", size);
70 save_object_as(PDU_t *st, enum expectation exp, enum enctype how) {
71 asn_enc_rval_t rval; /* Return value */
76 * Save object using specified method.
80 rval = uper_encode(&asn_DEF_PDU, 0, st,
82 if(exp == EXP_PER_NOCOMP)
83 assert(rval.encoded == -1);
85 assert(rval.encoded > 0);
86 fprintf(stderr, "SAVED OBJECT IN SIZE %zd\n", buf_offset);
89 rval = der_encode(&asn_DEF_PDU, st,
93 rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
97 rval = xer_encode(&asn_DEF_PDU, st, XER_F_CANONICAL,
101 assert(!"Unreachable");
104 if (rval.encoded == -1) {
106 "Cannot encode %s: %s\n",
107 rval.failed_type->name, strerror(errno));
108 assert(rval.encoded != -1);
112 fprintf(stderr, "SAVED OBJECT IN SIZE %zd\n", buf_offset);
116 load_object_from(const char *fname, enum expectation expectation, unsigned char *fbuf, size_t size, enum enctype how) {
121 if(getenv("INITIAL_CHUNK_SIZE"))
122 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
124 /* Perform multiple iterations with multiple chunks sizes */
125 for(; csize < 20; csize += 1) {
127 int fbuf_left = size;
128 int fbuf_chunk = csize;
130 fprintf(stderr, "LOADING OBJECT OF SIZE %zd FROM [%s] as %s,"
132 size, fname, how==AS_PER?"PER":"XER", csize);
134 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
138 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
140 fbuf_chunk < fbuf_left
141 ? fbuf_chunk : fbuf_left,
143 #ifdef ASN_EMIT_DEBUG
145 fprintf(stderr, "=== currently ===\n");
146 asn_fprint(stderr, &asn_DEF_PDU, st);
147 fprintf(stderr, "=== end ===\n");
152 rval = xer_decode(0, &asn_DEF_PDU, (void **)&st,
154 fbuf_chunk < fbuf_left
155 ? fbuf_chunk : fbuf_left);
158 rval = uper_decode(0, &asn_DEF_PDU,
159 (void **)&st, fbuf + fbuf_offset,
160 fbuf_chunk < fbuf_left
161 ? fbuf_chunk : fbuf_left, 0, 0);
162 if(rval.code == RC_WMORE) {
163 rval.consumed = 0; /* Not restartable */
164 ASN_STRUCT_FREE(asn_DEF_PDU, st);
166 ASN_DEBUG("-> PER wants more");
168 ASN_DEBUG("-> PER ret %d/%ld",
169 rval.code, rval.consumed);
170 /* uper_decode() returns _bits_ */
177 assert(!"DER or CXER not supported for load");
179 assert(!"Unreachable");
181 fbuf_offset += rval.consumed;
182 fbuf_left -= rval.consumed;
183 if(rval.code == RC_WMORE)
184 fbuf_chunk += 1; /* Give little more */
186 fbuf_chunk = csize; /* Back off */
187 } while(fbuf_left && rval.code == RC_WMORE);
189 if(expectation != EXP_BROKEN) {
190 assert(rval.code == RC_OK);
192 fprintf(stderr, "[left %d, off %d, size %zd]\n",
193 fbuf_left, fbuf_offset, size);
194 assert(fbuf_offset == (ssize_t)size);
196 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
197 && fbuf[size - 1] == '\n')
198 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
199 && fbuf[size - 2] == '\r'
200 && fbuf[size - 1] == '\n')
204 assert(rval.code != RC_OK);
205 fprintf(stderr, "Failed, but this was expected\n");
206 ASN_STRUCT_FREE(asn_DEF_PDU, st);
207 st = 0; /* ignore leak for now */
211 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
216 xer_encoding_equal(char *obuf, size_t osize, char *nbuf, size_t nsize) {
217 char *oend = obuf + osize;
218 char *nend = nbuf + nsize;
220 if((osize && !nsize) || (!osize && nsize))
221 return 0; /* not equal apriori */
224 while(obuf < oend && isspace(*obuf)) obuf++;
225 while(nbuf < nend && isspace(*nbuf)) nbuf++;
227 if(obuf == oend || nbuf == nend) {
228 if(obuf == oend && nbuf == nend)
230 fprintf(stderr, "%s data in reconstructed encoding\n",
231 (obuf == oend) ? "More" : "Less");
236 printf("%c%c != %c%c\n",
248 process_XER_data(const char *fname, enum expectation expectation, unsigned char *fbuf, size_t size) {
251 st = load_object_from(fname, expectation, fbuf, size, AS_XER);
254 /* Save and re-load as PER */
255 save_object_as(st, expectation, AS_PER);
256 ASN_STRUCT_FREE(asn_DEF_PDU, st);
257 if(expectation == EXP_PER_NOCOMP)
258 return; /* Already checked */
259 st = load_object_from("buffer", expectation, buf, buf_offset, AS_PER);
264 (expectation == EXP_CXER_EXACT
265 || expectation == EXP_CXER_DIFF)
267 fprintf(stderr, "=== original ===\n");
268 fwrite(fbuf, 1, size, stderr);
269 fprintf(stderr, "=== re-encoded ===\n");
270 fwrite(buf, 1, buf_offset, stderr);
271 fprintf(stderr, "=== end ===\n");
273 switch(expectation) {
275 assert(!xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
278 assert(!xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
281 buf[buf_offset++] = '\n';
282 assert(size == buf_offset);
283 assert(memcmp(fbuf, buf, size) == 0);
286 buf[buf_offset++] = '\n';
287 assert(size != buf_offset
288 || memcmp(fbuf, buf, size));
292 assert(xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
296 ASN_STRUCT_FREE(asn_DEF_PDU, st);
300 * Decode the .der files and try to regenerate them.
303 process(const char *fname) {
304 unsigned char fbuf[4096];
305 char *ext = strrchr(fname, '.');
306 enum expectation expectation;
310 if(ext == 0 || strcmp(ext, ".in"))
314 case 'B': /* The file is intentionally broken */
315 expectation = EXP_BROKEN; break;
316 case 'D': /* Reconstructing should yield different data */
317 expectation = EXP_DIFFERENT; break;
318 case 'E': /* Byte to byte exact reconstruction */
319 expectation = EXP_CXER_EXACT; break;
320 case 'X': /* Should fail byte-to-byte comparison */
321 expectation = EXP_CXER_DIFF; break;
322 case 'P': /* Incompatible with PER */
323 expectation = EXP_PER_NOCOMP; break;
325 expectation = EXP_OK; break;
328 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
330 snprintf((char *)fbuf, sizeof(fbuf), SRCDIR_S "/data-119/%s", fname);
331 fp = fopen((char *)fbuf, "r");
334 rd = fread(fbuf, 1, sizeof(fbuf), fp);
337 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
339 process_XER_data(fname, expectation, fbuf, rd);
341 fprintf(stderr, "Finished [%s]\n", fname);
350 int processed_files = 0;
353 /* Process a specific test file */
354 str = getenv("DATA_119_FILE");
355 if(str && strncmp(str, "data-119-", 9) == 0) {
360 dir = opendir(SRCDIR_S "/data-119");
364 * Process each file in that directory.
366 while((dent = readdir(dir))) {
367 if(strncmp(dent->d_name, "data-119-", 9) == 0)
368 if(process(dent->d_name))
372 assert(processed_files);