blob: bd96978605c4f50b4bbfc016eee61fa4a7141939 [file] [log] [blame]
Lev Walkin397d59d2006-07-13 11:11:33 +00001/*
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 Walkin2a744a72013-03-27 01:56:23 -07005 * stream is checked against rules specified in ../data-70/README file.
Lev Walkin397d59d2006-07-13 11:11:33 +00006 */
7#undef NDEBUG
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/types.h>
Lev Walkine0d321a2014-09-11 01:28:57 -070011#include <unistd.h> /* for chdir(2), getcwd(3) */
Lev Walkin397d59d2006-07-13 11:11:33 +000012#include <string.h>
Lev Walkin5b63acf2014-01-14 01:48:37 -080013#include <ctype.h>
Lev Walkin397d59d2006-07-13 11:11:33 +000014#include <dirent.h>
Lev Walkin397d59d2006-07-13 11:11:33 +000015#include <assert.h>
16#include <errno.h>
17
18#include <PDU.h>
19
Lev Walkine0d321a2014-09-11 01:28:57 -070020#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 Walkin397d59d2006-07-13 11:11:33 +000028enum expectation {
29 EXP_OK, /* Encoding/decoding must succeed */
Lev Walkin2a744a72013-03-27 01:56:23 -070030 EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
31 EXP_CXER_DIFF, /* Encoding/decoding using CXER must be different */
Lev Walkin397d59d2006-07-13 11:11:33 +000032 EXP_BROKEN, /* Decoding must fail */
33 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
34};
35
36static unsigned char buf[4096];
37static int buf_offset;
38
39static 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 Walkin2a744a72013-03-27 01:56:23 -070048 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 Walkin1715b322013-03-28 04:02:13 -070054 fprintf(stderr, "]:%zd\n", size);
Lev Walkin397d59d2006-07-13 11:11:33 +000055 buf_offset += size;
56 return 0;
57}
58
59enum der_or_xer {
60 AS_DER,
61 AS_XER,
Lev Walkin2a744a72013-03-27 01:56:23 -070062 AS_CXER,
Lev Walkin397d59d2006-07-13 11:11:33 +000063};
64
65static void
66save_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 Walkin2a744a72013-03-27 01:56:23 -070083 case AS_CXER:
84 rval = xer_encode(&asn_DEF_PDU, st, XER_F_CANONICAL,
85 _buf_writer, 0);
86 break;
Lev Walkin397d59d2006-07-13 11:11:33 +000087 }
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
99static PDU_t *
Lev Walkin5b63acf2014-01-14 01:48:37 -0800100load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum der_or_xer how) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000101 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 Walkinb7c40b62013-03-28 05:54:12 -0700105 size_t csize = 1;
Lev Walkin397d59d2006-07-13 11:11:33 +0000106
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 Walkinb7c40b62013-03-28 05:54:12 -0700121 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
Lev Walkin397d59d2006-07-13 11:11:33 +0000122 size, csize);
123
124 if(st) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
125 st = 0;
126
127 do {
Lev Walkin15d38f42014-09-17 01:19:31 -0700128 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
Lev Walkin397d59d2006-07-13 11:11:33 +0000129 fbuf_offset,
130 fbuf_chunk < fbuf_left
131 ? fbuf_chunk : fbuf_left,
132 fbuf_left);
Lev Walkin15d38f42014-09-17 01:19:31 -0700133#ifdef EMIT_ASN_DEBUG
Lev Walkin397d59d2006-07-13 11:11:33 +0000134 if(st) {
135 fprintf(stderr, "=== currently ===\n");
136 asn_fprint(stderr, &asn_DEF_PDU, st);
137 fprintf(stderr, "=== end ===\n");
138 }
Lev Walkin15d38f42014-09-17 01:19:31 -0700139#endif
Lev Walkin397d59d2006-07-13 11:11:33 +0000140 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 Walkin5b63acf2014-01-14 01:48:37 -0800155 assert(fbuf_offset == (ssize_t)size);
Lev Walkin397d59d2006-07-13 11:11:33 +0000156 } else {
157 assert(fbuf_offset - size < 2
Lev Walkin5b63acf2014-01-14 01:48:37 -0800158 || (fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000159 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800160 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000161 && 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
177static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800178xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
179 char *obuf = obufp;
180 char *nbuf = nbufp;
Lev Walkin397d59d2006-07-13 11:11:33 +0000181 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
211static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800212process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000213 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 Walkin2a744a72013-03-27 01:56:23 -0700223 save_object_as(st,
224 (expectation == EXP_CXER_EXACT
225 || expectation == EXP_CXER_DIFF)
226 ? AS_CXER : AS_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000227 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 Walkin2a744a72013-03-27 01:56:23 -0700240 case EXP_CXER_EXACT:
241 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800242 assert((ssize_t)size == buf_offset);
Lev Walkin2a744a72013-03-27 01:56:23 -0700243 assert(memcmp(fbuf, buf, size) == 0);
244 break;
245 case EXP_CXER_DIFF:
246 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800247 assert((ssize_t)size != buf_offset
Lev Walkin2a744a72013-03-27 01:56:23 -0700248 || memcmp(fbuf, buf, size));
249 break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000250 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 */
261static int
262process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700263 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800264 unsigned char fbuf[4096];
Lev Walkin397d59d2006-07-13 11:11:33 +0000265 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 Walkin397d59d2006-07-13 11:11:33 +0000277 case 'D': /* Reconstructing should yield different data */
278 expectation = EXP_DIFFERENT; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700279 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 Walkin397d59d2006-07-13 11:11:33 +0000283 default:
284 expectation = EXP_OK; break;
285 }
286
287 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
288
Lev Walkine0d321a2014-09-11 01:28:57 -0700289 getcwd(prevdir, sizeof(prevdir));
290 ret = chdir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000291 assert(ret == 0);
292 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700293 ret = chdir(prevdir);
Lev Walkin397d59d2006-07-13 11:11:33 +0000294 assert(ret == 0);
295 assert(fp);
296
297 rd = fread(fbuf, 1, sizeof(fbuf), fp);
298 fclose(fp);
299
Lev Walkin5b63acf2014-01-14 01:48:37 -0800300 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
Lev Walkin397d59d2006-07-13 11:11:33 +0000301
302 process_XER_data(expectation, fbuf, rd);
303
304 return 1;
305}
306
307int
308main() {
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 Walkin2a744a72013-03-27 01:56:23 -0700316 if(str && strncmp(str, "data-70-", 8) == 0) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000317 process(str);
Lev Walkin2a744a72013-03-27 01:56:23 -0700318 return 0;
319 }
Lev Walkin397d59d2006-07-13 11:11:33 +0000320
Lev Walkine0d321a2014-09-11 01:28:57 -0700321 dir = opendir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000322 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