blob: cb5d5201e924c24e7890daca3277ea7bc1e453fe [file] [log] [blame]
Lev Walkina85b37d2005-02-18 10:13:44 +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 compared with the corresponding .out file.
Lev Walkina85b37d2005-02-18 10:13:44 +00006 */
Lev Walkin3234ecc2005-02-14 17:20:23 +00007#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 Walkin3234ecc2005-02-14 17:20:23 +000012#include <string.h>
13#include <dirent.h>
Lev Walkin2a744a72013-03-27 01:56:23 -070014#include <ctype.h> /* for isspace(3) */
Lev Walkin3234ecc2005-02-14 17:20:23 +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 Walkinbc09dd42017-10-19 02:16:35 -070028#ifdef ENABLE_LIBFUZZER
29
30int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
31int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
32 PDU_t *st = 0;
33 asn_dec_rval_t rval;
34 rval = asn_decode(0, ATS_BASIC_XER, &asn_DEF_PDU, (void **)&st, Data, Size);
35 assert(rval.consumed <= Size);
36 ASN_STRUCT_FREE(asn_DEF_PDU, st);
37 return 0;
38}
39
40#else
41
Lev Walkin3234ecc2005-02-14 17:20:23 +000042enum expectation {
43 EXP_OK, /* Encoding/decoding must succeed */
44 EXP_BROKEN, /* Decoding must fail */
Lev Walkin3234ecc2005-02-14 17:20:23 +000045 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
46};
47
48static unsigned char buf[4096];
49static int buf_offset;
50
51static int
52_buf_writer(const void *buffer, size_t size, void *app_key) {
53 unsigned char *b, *bend;
54 (void)app_key;
55 assert(buf_offset + size < sizeof(buf));
56 memcpy(buf + buf_offset, buffer, size);
57 b = buf + buf_offset;
58 bend = b + size;
Lev Walkin41ffcac2005-02-18 14:26:30 +000059 fprintf(stderr, "=> [");
Lev Walkin2a744a72013-03-27 01:56:23 -070060 for(; b < bend; b++)
61 fprintf(stderr, "%c", *b);
Lev Walkin1715b322013-03-28 04:02:13 -070062 fprintf(stderr, "]:%zd\n", size);
Lev Walkin3234ecc2005-02-14 17:20:23 +000063 buf_offset += size;
64 return 0;
65}
66
Lev Walkina85b37d2005-02-18 10:13:44 +000067static void
Lev Walkin74898072017-09-18 00:56:06 -070068save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
69 asn_enc_rval_t rval; /* Return value */
Lev Walkin3234ecc2005-02-14 17:20:23 +000070
Lev Walkin74898072017-09-18 00:56:06 -070071 buf_offset = 0;
Lev Walkina85b37d2005-02-18 10:13:44 +000072
Lev Walkin74898072017-09-18 00:56:06 -070073 rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
74
75 if (rval.encoded == -1) {
Lev Walkin3234ecc2005-02-14 17:20:23 +000076 fprintf(stderr,
77 "Cannot encode %s: %s\n",
78 rval.failed_type->name, strerror(errno));
79 assert(rval.encoded != -1);
Lev Walkina85b37d2005-02-18 10:13:44 +000080 return;
Lev Walkin74898072017-09-18 00:56:06 -070081 }
Lev Walkin3234ecc2005-02-14 17:20:23 +000082
Lev Walkin4fe28822017-09-18 02:57:34 -070083 fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zu\n", buf_offset, rval.encoded);
Lev Walkin74898072017-09-18 00:56:06 -070084
Lev Walkin4fe28822017-09-18 02:57:34 -070085 assert(buf_offset == rval.encoded);
Lev Walkin3234ecc2005-02-14 17:20:23 +000086}
87
88static PDU_t *
Lev Walkin74898072017-09-18 00:56:06 -070089load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
Lev Walkin3234ecc2005-02-14 17:20:23 +000090 asn_dec_rval_t rval;
91 PDU_t *st = 0;
Lev Walkinb7c40b62013-03-28 05:54:12 -070092 size_t csize = 1;
Lev Walkin3234ecc2005-02-14 17:20:23 +000093
Lev Walkin41ffcac2005-02-18 14:26:30 +000094 if(getenv("INITIAL_CHUNK_SIZE"))
95 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
Lev Walkin3234ecc2005-02-14 17:20:23 +000096
97 /* Perform multiple iterations with multiple chunks sizes */
Lev Walkin41ffcac2005-02-18 14:26:30 +000098 for(; csize < 20; csize += 1) {
Lev Walkin3234ecc2005-02-14 17:20:23 +000099 int fbuf_offset = 0;
100 int fbuf_left = size;
101 int fbuf_chunk = csize;
102
Lev Walkinb7c40b62013-03-28 05:54:12 -0700103 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
Lev Walkin41ffcac2005-02-18 14:26:30 +0000104 size, csize);
105
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800106 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000107 st = 0;
108
109 do {
Lev Walkin15d38f42014-09-17 01:19:31 -0700110 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
Lev Walkin41ffcac2005-02-18 14:26:30 +0000111 fbuf_offset,
112 fbuf_chunk < fbuf_left
113 ? fbuf_chunk : fbuf_left,
114 fbuf_left);
Lev Walkin15d38f42014-09-17 01:19:31 -0700115#ifdef EMIT_ASN_DEBUG
Lev Walkin41ffcac2005-02-18 14:26:30 +0000116 if(st) {
117 fprintf(stderr, "=== currently ===\n");
118 asn_fprint(stderr, &asn_DEF_PDU, st);
119 fprintf(stderr, "=== end ===\n");
120 }
Lev Walkin15d38f42014-09-17 01:19:31 -0700121#endif
Lev Walkin74898072017-09-18 00:56:06 -0700122 rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
Lev Walkin3234ecc2005-02-14 17:20:23 +0000123 fbuf + fbuf_offset,
124 fbuf_chunk < fbuf_left
Lev Walkina85b37d2005-02-18 10:13:44 +0000125 ? fbuf_chunk : fbuf_left);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000126 fbuf_offset += rval.consumed;
127 fbuf_left -= rval.consumed;
128 if(rval.code == RC_WMORE)
129 fbuf_chunk += 1; /* Give little more */
130 else
131 fbuf_chunk = csize; /* Back off */
132 } while(fbuf_left && rval.code == RC_WMORE);
133
134 if(expectation != EXP_BROKEN) {
135 assert(rval.code == RC_OK);
Lev Walkin74898072017-09-18 00:56:06 -0700136 if(syntax == ATS_BER) {
Lev Walkin5b63acf2014-01-14 01:48:37 -0800137 assert(fbuf_offset == (ssize_t)size);
Lev Walkina85b37d2005-02-18 10:13:44 +0000138 } else {
Lev Walkina75b2472017-09-18 00:38:43 -0700139 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
Lev Walkin3234ecc2005-02-14 17:20:23 +0000140 && fbuf[size - 1] == '\n')
Lev Walkin5b63acf2014-01-14 01:48:37 -0800141 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
Lev Walkin3234ecc2005-02-14 17:20:23 +0000142 && fbuf[size - 2] == '\r'
143 && fbuf[size - 1] == '\n')
Lev Walkina85b37d2005-02-18 10:13:44 +0000144 );
145 }
Lev Walkin3234ecc2005-02-14 17:20:23 +0000146 } else {
147 assert(rval.code != RC_OK);
148 fprintf(stderr, "Failed, but this was expected\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800149 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000150 st = 0; /* ignore leak for now */
151 }
152 }
153
154 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
155 return st;
156}
157
Lev Walkina85b37d2005-02-18 10:13:44 +0000158static int
Lev Walkin5b63acf2014-01-14 01:48:37 -0800159xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
160 char *obuf = obufp;
161 char *nbuf = nbufp;
Lev Walkina85b37d2005-02-18 10:13:44 +0000162 char *oend = obuf + osize;
163 char *nend = nbuf + nsize;
164
165 if((osize && !nsize) || (!osize && nsize))
166 return 0; /* not equal apriori */
167
168 while(1) {
169 while(obuf < oend && isspace(*obuf)) obuf++;
170 while(nbuf < nend && isspace(*nbuf)) nbuf++;
171
172 if(obuf == oend || nbuf == nend) {
173 if(obuf == oend && nbuf == nend)
174 break;
175 fprintf(stderr, "%s data in reconstructed encoding\n",
176 (obuf == oend) ? "More" : "Less");
177 return 0;
178 }
179
180 if(*obuf != *nbuf) {
181 printf("%c%c != %c%c\n",
182 obuf[0], obuf[1],
183 nbuf[0], nbuf[1]);
184 return 0;
185 }
186 obuf++, nbuf++;
187 }
188
189 return 1;
190}
Lev Walkin3234ecc2005-02-14 17:20:23 +0000191
192static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800193process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin3234ecc2005-02-14 17:20:23 +0000194 PDU_t *st;
Lev Walkin3234ecc2005-02-14 17:20:23 +0000195
Lev Walkin74898072017-09-18 00:56:06 -0700196 st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000197 if(!st) return;
198
Lev Walkina85b37d2005-02-18 10:13:44 +0000199 /* Save and re-load as DER */
Lev Walkin74898072017-09-18 00:56:06 -0700200 save_object_as(st, ATS_DER);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300201 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin74898072017-09-18 00:56:06 -0700202 st = load_object_from(expectation, buf, buf_offset, ATS_BER);
Lev Walkina85b37d2005-02-18 10:13:44 +0000203 assert(st);
204
Lev Walkin74898072017-09-18 00:56:06 -0700205 save_object_as(st, ATS_BASIC_XER);
Lev Walkina85b37d2005-02-18 10:13:44 +0000206 fprintf(stderr, "=== original ===\n");
207 fwrite(fbuf, 1, size, stderr);
208 fprintf(stderr, "=== re-encoded ===\n");
209 fwrite(buf, 1, buf_offset, stderr);
210 fprintf(stderr, "=== end ===\n");
Lev Walkin3234ecc2005-02-14 17:20:23 +0000211
212 switch(expectation) {
Lev Walkin3234ecc2005-02-14 17:20:23 +0000213 case EXP_DIFFERENT:
Lev Walkina85b37d2005-02-18 10:13:44 +0000214 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
Lev Walkin3234ecc2005-02-14 17:20:23 +0000215 break;
216 case EXP_BROKEN:
Lev Walkina85b37d2005-02-18 10:13:44 +0000217 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
Lev Walkin3234ecc2005-02-14 17:20:23 +0000218 break;
219 case EXP_OK:
Lev Walkina85b37d2005-02-18 10:13:44 +0000220 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
Lev Walkin3234ecc2005-02-14 17:20:23 +0000221 break;
222 }
223
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800224 ASN_STRUCT_FREE(asn_DEF_PDU, st);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000225}
226
227/*
228 * Decode the .der files and try to regenerate them.
229 */
230static int
231process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700232 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800233 unsigned char fbuf[4096];
Lev Walkin3234ecc2005-02-14 17:20:23 +0000234 char *ext = strrchr(fname, '.');
235 enum expectation expectation;
236 int ret;
237 int rd;
238 FILE *fp;
239
240 if(ext == 0 || strcmp(ext, ".in"))
241 return 0;
242
243 switch(ext[-1]) {
244 case 'B': /* The file is intentionally broken */
245 expectation = EXP_BROKEN; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700246 case 'X':
Lev Walkin3234ecc2005-02-14 17:20:23 +0000247 case 'D': /* Reconstructing should yield different data */
248 expectation = EXP_DIFFERENT; break;
Lev Walkin2a744a72013-03-27 01:56:23 -0700249 case 'E':
Lev Walkin3234ecc2005-02-14 17:20:23 +0000250 default:
251 expectation = EXP_OK; break;
252 }
253
254 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
255
Lev Walkine0d321a2014-09-11 01:28:57 -0700256 getcwd(prevdir, sizeof(prevdir));
257 ret = chdir(SRCDIR_S "/data-70");
Lev Walkin3234ecc2005-02-14 17:20:23 +0000258 assert(ret == 0);
259 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700260 ret = chdir(prevdir);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000261 assert(ret == 0);
262 assert(fp);
263
264 rd = fread(fbuf, 1, sizeof(fbuf), fp);
265 fclose(fp);
266
Lev Walkin5b63acf2014-01-14 01:48:37 -0800267 assert(rd > 0 && (size_t)rd < sizeof(fbuf)); /* expect small files */
Lev Walkin3234ecc2005-02-14 17:20:23 +0000268
Lev Walkina85b37d2005-02-18 10:13:44 +0000269 process_XER_data(expectation, fbuf, rd);
Lev Walkin3234ecc2005-02-14 17:20:23 +0000270
271 return 1;
272}
273
274int
275main() {
276 DIR *dir;
277 struct dirent *dent;
278 int processed_files = 0;
279 char *str;
280
Lev Walkin3234ecc2005-02-14 17:20:23 +0000281 /* Process a specific test file */
282 str = getenv("DATA_70_FILE");
Lev Walkin2a744a72013-03-27 01:56:23 -0700283 if(str && strncmp(str, "data-70-", 8) == 0)
Lev Walkin3234ecc2005-02-14 17:20:23 +0000284 process(str);
285
Lev Walkine0d321a2014-09-11 01:28:57 -0700286 dir = opendir(SRCDIR_S "/data-70");
Lev Walkina85b37d2005-02-18 10:13:44 +0000287 assert(dir);
288
289 /*
290 * Process each file in that directory.
291 */
Lev Walkin3234ecc2005-02-14 17:20:23 +0000292 while((dent = readdir(dir))) {
293 if(strncmp(dent->d_name, "data-70-", 8) == 0)
294 if(process(dent->d_name))
295 processed_files++;
296 }
297
298 assert(processed_files);
299 closedir(dir);
300
301 return 0;
302}
303
Lev Walkinc8c286a2017-09-18 03:19:37 -0700304#endif