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/%zd",
169 rval.code, rval.consumed);
170 /* uper_decode() returns _bits_ */
177 assert(!"Unexpected DER or CXER load request");
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(void *obufp, size_t osize, void *nbufp, size_t nsize) {
219 char *oend = obuf + osize;
220 char *nend = nbuf + nsize;
222 if((osize && !nsize) || (!osize && nsize))
223 return 0; /* not equal apriori */
226 while(obuf < oend && isspace(*obuf)) obuf++;
227 while(nbuf < nend && isspace(*nbuf)) nbuf++;
229 if(obuf == oend || nbuf == nend) {
230 if(obuf == oend && nbuf == nend)
232 fprintf(stderr, "%s data in reconstructed encoding\n",
233 (obuf == oend) ? "More" : "Less");
238 printf("%c%c != %c%c\n",
250 process_XER_data(const char *fname, enum expectation expectation, unsigned char *fbuf, ssize_t size) {
253 st = load_object_from(fname, expectation, fbuf, size, AS_XER);
256 /* Save and re-load as PER */
257 save_object_as(st, expectation, AS_PER);
258 ASN_STRUCT_FREE(asn_DEF_PDU, st);
260 if(expectation == EXP_PER_NOCOMP)
261 return; /* Already checked */
262 st = load_object_from("buffer", expectation, buf, buf_offset, AS_PER);
267 (expectation == EXP_CXER_EXACT
268 || expectation == EXP_CXER_DIFF)
270 fprintf(stderr, "=== original ===\n");
271 fwrite(fbuf, 1, size, stderr);
272 fprintf(stderr, "=== re-encoded ===\n");
273 fwrite(buf, 1, buf_offset, stderr);
274 fprintf(stderr, "=== end ===\n");
276 switch(expectation) {
278 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
281 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
284 buf[buf_offset++] = '\n';
285 assert((ssize_t)size == (ssize_t)buf_offset);
286 assert(memcmp(fbuf, buf, size) == 0);
289 buf[buf_offset++] = '\n';
290 assert((ssize_t)size != (ssize_t)buf_offset
291 || memcmp(fbuf, buf, size));
295 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
299 ASN_STRUCT_FREE(asn_DEF_PDU, st);
303 * Decode the .der files and try to regenerate them.
306 process(const char *fname) {
307 unsigned char fbuf[4096];
308 char *ext = strrchr(fname, '.');
309 enum expectation expectation;
313 if(ext == 0 || strcmp(ext, ".in"))
317 case 'B': /* The file is intentionally broken */
318 expectation = EXP_BROKEN; break;
319 case 'D': /* Reconstructing should yield different data */
320 expectation = EXP_DIFFERENT; break;
321 case 'E': /* Byte to byte exact reconstruction */
322 expectation = EXP_CXER_EXACT; break;
323 case 'X': /* Should fail byte-to-byte comparison */
324 expectation = EXP_CXER_DIFF; break;
325 case 'P': /* Incompatible with PER */
326 expectation = EXP_PER_NOCOMP; break;
328 expectation = EXP_OK; break;
331 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
333 snprintf((char *)fbuf, sizeof(fbuf), SRCDIR_S "/data-119/%s", fname);
334 fp = fopen((char *)fbuf, "r");
337 rd = fread(fbuf, 1, sizeof(fbuf), fp);
340 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
342 process_XER_data(fname, expectation, fbuf, rd);
344 fprintf(stderr, "Finished [%s]\n", fname);
353 int processed_files = 0;
356 /* Process a specific test file */
357 str = getenv("DATA_119_FILE");
358 if(str && strncmp(str, "data-119-", 9) == 0) {
363 dir = opendir(SRCDIR_S "/data-119");
367 * Process each file in that directory.
369 while((dent = readdir(dir))) {
370 if(strncmp(dent->d_name, "data-119-", 9) == 0)
371 if(process(dent->d_name))
375 assert(processed_files);