blob: bd78c2f8337c02d082683559eeaf87307190cd70 [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
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800124 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000125 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 {
Lev Walkina75b2472017-09-18 00:38:43 -0700157 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000158 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800159 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkin397d59d2006-07-13 11:11:33 +0000160 && fbuf[size - 2] == '\r'
161 && fbuf[size - 1] == '\n')
162 );
163 }
164 } else {
165 assert(rval.code != RC_OK);
166 fprintf(stderr, "Failed, but this was expected\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800167 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000168 st = 0; /* ignore leak for now */
169 }
170 }
171
172 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
173 return st;
174}
175
176static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800177xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
178 char *obuf = obufp;
179 char *nbuf = nbufp;
Lev Walkin397d59d2006-07-13 11:11:33 +0000180 char *oend = obuf + osize;
181 char *nend = nbuf + nsize;
182
183 if((osize && !nsize) || (!osize && nsize))
184 return 0; /* not equal apriori */
185
186 while(1) {
187 while(obuf < oend && isspace(*obuf)) obuf++;
188 while(nbuf < nend && isspace(*nbuf)) nbuf++;
189
190 if(obuf == oend || nbuf == nend) {
191 if(obuf == oend && nbuf == nend)
192 break;
193 fprintf(stderr, "%s data in reconstructed encoding\n",
194 (obuf == oend) ? "More" : "Less");
195 return 0;
196 }
197
198 if(*obuf != *nbuf) {
199 printf("%c%c != %c%c\n",
200 obuf[0], obuf[1],
201 nbuf[0], nbuf[1]);
202 return 0;
203 }
204 obuf++, nbuf++;
205 }
206
207 return 1;
208}
209
210static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800211process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000212 PDU_t *st;
213
214 st = load_object_from(expectation, fbuf, size, AS_XER);
215 if(!st) return;
216
217 /* Save and re-load as DER */
218 save_object_as(st, AS_DER);
219 st = load_object_from(expectation, buf, buf_offset, AS_DER);
220 assert(st);
221
Lev Walkin2a744a72013-03-27 01:56:23 -0700222 save_object_as(st,
223 (expectation == EXP_CXER_EXACT
224 || expectation == EXP_CXER_DIFF)
225 ? AS_CXER : AS_XER);
Lev Walkin397d59d2006-07-13 11:11:33 +0000226 fprintf(stderr, "=== original ===\n");
227 fwrite(fbuf, 1, size, stderr);
228 fprintf(stderr, "=== re-encoded ===\n");
229 fwrite(buf, 1, buf_offset, stderr);
230 fprintf(stderr, "=== end ===\n");
231
232 switch(expectation) {
233 case EXP_DIFFERENT:
234 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
235 break;
236 case EXP_BROKEN:
237 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
238 break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700239 case EXP_CXER_EXACT:
240 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800241 assert((ssize_t)size == buf_offset);
Lev Walkin2a744a72013-03-27 01:56:23 -0700242 assert(memcmp(fbuf, buf, size) == 0);
243 break;
244 case EXP_CXER_DIFF:
245 buf[buf_offset++] = '\n';
Lev Walkin5b63acf2014-01-14 01:48:37 -0800246 assert((ssize_t)size != buf_offset
Lev Walkin2a744a72013-03-27 01:56:23 -0700247 || memcmp(fbuf, buf, size));
248 break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000249 case EXP_OK:
250 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
251 break;
252 }
253
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800254 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin397d59d2006-07-13 11:11:33 +0000255}
256
257/*
258 * Decode the .der files and try to regenerate them.
259 */
260static int
261process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700262 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800263 unsigned char fbuf[4096];
Lev Walkin397d59d2006-07-13 11:11:33 +0000264 char *ext = strrchr(fname, '.');
265 enum expectation expectation;
266 int ret;
267 int rd;
268 FILE *fp;
269
270 if(ext == 0 || strcmp(ext, ".in"))
271 return 0;
272
273 switch(ext[-1]) {
274 case 'B': /* The file is intentionally broken */
275 expectation = EXP_BROKEN; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000276 case 'D': /* Reconstructing should yield different data */
277 expectation = EXP_DIFFERENT; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700278 case 'E': /* Byte to byte exact reconstruction */
279 expectation = EXP_CXER_EXACT; break;
280 case 'X': /* Should fail byte-to-byte comparison */
281 expectation = EXP_CXER_DIFF; break;
Lev Walkin397d59d2006-07-13 11:11:33 +0000282 default:
283 expectation = EXP_OK; break;
284 }
285
286 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
287
Lev Walkine0d321a2014-09-11 01:28:57 -0700288 getcwd(prevdir, sizeof(prevdir));
289 ret = chdir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000290 assert(ret == 0);
291 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700292 ret = chdir(prevdir);
Lev Walkin397d59d2006-07-13 11:11:33 +0000293 assert(ret == 0);
294 assert(fp);
295
296 rd = fread(fbuf, 1, sizeof(fbuf), fp);
297 fclose(fp);
298
Lev Walkin5b63acf2014-01-14 01:48:37 -0800299 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
Lev Walkin397d59d2006-07-13 11:11:33 +0000300
301 process_XER_data(expectation, fbuf, rd);
302
303 return 1;
304}
305
306int
307main() {
308 DIR *dir;
309 struct dirent *dent;
310 int processed_files = 0;
311 char *str;
312
313 /* Process a specific test file */
314 str = getenv("DATA_70_FILE");
Lev Walkin2a744a72013-03-27 01:56:23 -0700315 if(str && strncmp(str, "data-70-", 8) == 0) {
Lev Walkin397d59d2006-07-13 11:11:33 +0000316 process(str);
Lev Walkin2a744a72013-03-27 01:56:23 -0700317 return 0;
318 }
Lev Walkin397d59d2006-07-13 11:11:33 +0000319
Lev Walkine0d321a2014-09-11 01:28:57 -0700320 dir = opendir(SRCDIR_S "/data-70");
Lev Walkin397d59d2006-07-13 11:11:33 +0000321 assert(dir);
322
323 /*
324 * Process each file in that directory.
325 */
326 while((dent = readdir(dir))) {
327 if(strncmp(dent->d_name, "data-70-", 8) == 0)
328 if(process(dent->d_name))
329 processed_files++;
330 }
331
332 assert(processed_files);
333 closedir(dir);
334
335 return 0;
336}
337