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
5 * stream is compared with the corresponding .out file.
10 #include <sys/types.h>
11 #include <unistd.h> /* for chdir(2), getcwd(3) */
14 #include <ctype.h> /* for isspace(3) */
23 #define STRINGIFY_MACRO2(x) #x
24 #define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
25 #define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
28 #ifdef ENABLE_LIBFUZZER
30 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
31 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
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);
43 EXP_OK, /* Encoding/decoding must succeed */
44 EXP_BROKEN, /* Decoding must fail */
45 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
48 static unsigned char buf[4096];
49 static int buf_offset;
52 _buf_writer(const void *buffer, size_t size, void *app_key) {
53 unsigned char *b, *bend;
55 assert(buf_offset + size < sizeof(buf));
56 memcpy(buf + buf_offset, buffer, size);
59 fprintf(stderr, "=> [");
61 fprintf(stderr, "%c", *b);
62 fprintf(stderr, "]:%zd\n", size);
68 save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
69 asn_enc_rval_t rval; /* Return value */
73 rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
75 if (rval.encoded == -1) {
77 "Cannot encode %s: %s\n",
78 rval.failed_type->name, strerror(errno));
79 assert(rval.encoded != -1);
83 fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zu\n", buf_offset, rval.encoded);
85 assert(buf_offset == rval.encoded);
89 load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
94 if(getenv("INITIAL_CHUNK_SIZE"))
95 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
97 /* Perform multiple iterations with multiple chunks sizes */
98 for(; csize < 20; csize += 1) {
100 int fbuf_left = size;
101 int fbuf_chunk = csize;
103 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
106 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
110 ASN_DEBUG("Decoding bytes %d..%d (left %d)",
112 fbuf_chunk < fbuf_left
113 ? fbuf_chunk : fbuf_left,
115 #ifdef ASN_EMIT_DEBUG
117 fprintf(stderr, "=== currently ===\n");
118 asn_fprint(stderr, &asn_DEF_PDU, st);
119 fprintf(stderr, "=== end ===\n");
122 rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
124 fbuf_chunk < fbuf_left
125 ? fbuf_chunk : fbuf_left);
126 fbuf_offset += rval.consumed;
127 fbuf_left -= rval.consumed;
128 if(rval.code == RC_WMORE)
129 fbuf_chunk += 1; /* Give little more */
131 fbuf_chunk = csize; /* Back off */
132 } while(fbuf_left && rval.code == RC_WMORE);
134 if(expectation != EXP_BROKEN) {
135 assert(rval.code == RC_OK);
136 if(syntax == ATS_BER) {
137 assert(fbuf_offset == (ssize_t)size);
139 assert((fbuf_offset + 1 /* "\n" */ == (ssize_t)size
140 && fbuf[size - 1] == '\n')
141 || (fbuf_offset + 2 /* "\r\n" */ == (ssize_t)size
142 && fbuf[size - 2] == '\r'
143 && fbuf[size - 1] == '\n')
147 assert(rval.code != RC_OK);
148 fprintf(stderr, "Failed, but this was expected\n");
149 ASN_STRUCT_FREE(asn_DEF_PDU, st);
150 st = 0; /* ignore leak for now */
154 if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
159 xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
162 char *oend = obuf + osize;
163 char *nend = nbuf + nsize;
165 if((osize && !nsize) || (!osize && nsize))
166 return 0; /* not equal apriori */
169 while(obuf < oend && isspace(*obuf)) obuf++;
170 while(nbuf < nend && isspace(*nbuf)) nbuf++;
172 if(obuf == oend || nbuf == nend) {
173 if(obuf == oend && nbuf == nend)
175 fprintf(stderr, "%s data in reconstructed encoding\n",
176 (obuf == oend) ? "More" : "Less");
181 printf("%c%c != %c%c\n",
193 process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
196 st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
199 /* Save and re-load as DER */
200 save_object_as(st, ATS_DER);
201 ASN_STRUCT_FREE(asn_DEF_PDU, st);
202 st = load_object_from(expectation, buf, buf_offset, ATS_BER);
205 save_object_as(st, ATS_BASIC_XER);
206 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");
212 switch(expectation) {
214 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
217 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
220 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
224 ASN_STRUCT_FREE(asn_DEF_PDU, st);
228 * Decode the .der files and try to regenerate them.
231 process(const char *fname) {
233 unsigned char fbuf[4096];
234 char *ext = strrchr(fname, '.');
235 enum expectation expectation;
241 if(ext == 0 || strcmp(ext, ".in"))
245 case 'B': /* The file is intentionally broken */
246 expectation = EXP_BROKEN; break;
248 case 'D': /* Reconstructing should yield different data */
249 expectation = EXP_DIFFERENT; break;
252 expectation = EXP_OK; break;
255 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
257 cwd = getcwd(prevdir, sizeof(prevdir));
259 ret = chdir(SRCDIR_S "/data-70");
261 fp = fopen(fname, "r");
262 ret = chdir(prevdir);
266 rd = fread(fbuf, 1, sizeof(fbuf), fp);
269 assert(rd > 0 && (size_t)rd < sizeof(fbuf)); /* expect small files */
271 process_XER_data(expectation, fbuf, rd);
280 int processed_files = 0;
283 /* Process a specific test file */
284 str = getenv("DATA_70_FILE");
285 if(str && strncmp(str, "data-70-", 8) == 0)
288 dir = opendir(SRCDIR_S "/data-70");
292 * Process each file in that directory.
294 while((dent = readdir(dir))) {
295 if(strncmp(dent->d_name, "data-70-", 8) == 0)
296 if(process(dent->d_name))
300 assert(processed_files);