NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / tests / tests-c-compiler / check-src / check-70.-fwide-types.c
1 /*
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
5  * stream is checked against rules specified in ../data-70/README file.
6  */
7 #undef  NDEBUG
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <unistd.h>     /* for chdir(2), getcwd(3) */
12 #include <string.h>
13 #include <ctype.h>
14 #include <dirent.h>
15 #include <assert.h>
16 #include <errno.h>
17
18 #include <PDU.h>
19
20 #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
28 #ifdef ENABLE_LIBFUZZER
29
30 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
31 int 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
42 enum expectation {
43         EXP_OK,         /* Encoding/decoding must succeed */
44         EXP_CXER_EXACT, /* Encoding/decoding using CXER must be exact */
45         EXP_CXER_DIFF,  /* Encoding/decoding using CXER must be different */
46         EXP_BROKEN,     /* Decoding must fail */
47         EXP_DIFFERENT,  /* Reconstruction will yield different encoding */
48 };
49
50 static unsigned char buf[4096];
51 static int buf_offset;
52
53 static int
54 _buf_writer(const void *buffer, size_t size, void *app_key) {
55         unsigned char *b, *bend;
56         (void)app_key;
57         assert(buf_offset + size < sizeof(buf));
58         memcpy(buf + buf_offset, buffer, size);
59         b = buf + buf_offset;
60         bend = b + size;
61         fprintf(stderr, "=> [");
62         for(; b < bend; b++) {
63                 if(*b >= 32 && *b < 127 && *b != '%')
64                         fprintf(stderr, "%c", *b);
65                 else
66                         fprintf(stderr, "%%%02x", *b);
67         }
68         fprintf(stderr, "]:%zd\n", size);
69         buf_offset += size;
70         return 0;
71 }
72
73 static void
74 save_object_as(PDU_t *st, enum asn_transfer_syntax syntax) {
75     asn_enc_rval_t rval; /* Return value */
76
77     buf_offset = 0;
78
79     rval = asn_encode(0, syntax, &asn_DEF_PDU, st, _buf_writer, 0);
80
81     if (rval.encoded == -1) {
82         fprintf(stderr,
83             "Cannot encode %s: %s\n",
84             rval.failed_type->name, strerror(errno));
85         assert(rval.encoded != -1);
86         return;
87     }
88
89     fprintf(stderr, "SAVED OBJECT IN SIZE %d/%zd\n", buf_offset, rval.encoded);
90
91     assert(buf_offset == rval.encoded);
92 }
93
94 static PDU_t *
95 load_object_from(enum expectation expectation, unsigned char *fbuf, size_t size, enum asn_transfer_syntax syntax) {
96         asn_dec_rval_t rval;
97         PDU_t *st = 0;
98         size_t csize = 1;
99
100         if(getenv("INITIAL_CHUNK_SIZE"))
101                 csize = atoi(getenv("INITIAL_CHUNK_SIZE"));
102
103         /* Perform multiple iterations with multiple chunks sizes */
104         for(; csize < 20; csize += 1) {
105                 int fbuf_offset = 0;
106                 int fbuf_left = size;
107                 int fbuf_chunk = csize;
108
109                 fprintf(stderr, "LOADING OBJECT OF SIZE %zd, chunks %zd\n",
110                         size, csize);
111
112                 if(st) ASN_STRUCT_FREE(asn_DEF_PDU, st);
113                 st = 0;
114
115                 do {
116                         ASN_DEBUG("Decoding bytes %d..%d (left %d)",
117                                 fbuf_offset,
118                                         fbuf_chunk < fbuf_left
119                                                 ? fbuf_chunk : fbuf_left,
120                                         fbuf_left);
121 #ifdef  ASN_EMIT_DEBUG
122                         if(st) {
123                                 fprintf(stderr, "=== currently ===\n");
124                                 asn_fprint(stderr, &asn_DEF_PDU, st);
125                                 fprintf(stderr, "=== end ===\n");
126                         }
127 #endif
128                         rval = asn_decode(0, syntax, &asn_DEF_PDU, (void **)&st,
129                                 fbuf + fbuf_offset,
130                                         fbuf_chunk < fbuf_left 
131                                         ? fbuf_chunk : fbuf_left);
132                         fbuf_offset += rval.consumed;
133                         fbuf_left -= rval.consumed;
134                         if(rval.code == RC_WMORE)
135                                 fbuf_chunk += 1;        /* Give little more */
136                         else
137                                 fbuf_chunk = csize;     /* Back off */
138                 } while(fbuf_left && rval.code == RC_WMORE);
139
140                 if(expectation != EXP_BROKEN) {
141                         assert(rval.code == RC_OK);
142                         if(syntax == ATS_BER) {
143                                 assert(fbuf_offset == (ssize_t)size);
144                         } else {
145                                 assert((fbuf_offset + 1 /* "\n" */  == (ssize_t)size
146                                         && fbuf[size - 1] == '\n')
147                                 || (fbuf_offset + 2 /* "\r\n" */  == (ssize_t)size
148                                         && fbuf[size - 2] == '\r'
149                                         && fbuf[size - 1] == '\n')
150                                 );
151                         }
152                 } else {
153                         assert(rval.code != RC_OK);
154                         fprintf(stderr, "Failed, but this was expected\n");
155                         ASN_STRUCT_FREE(asn_DEF_PDU, st);
156                         st = 0; /* ignore leak for now */
157                 }
158         }
159
160         if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
161         return st;
162 }
163
164 static int
165 xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
166     char *obuf = obufp;
167     char *nbuf = nbufp;
168         char *oend = obuf + osize;
169         char *nend = nbuf + nsize;
170
171         if((osize && !nsize) || (!osize && nsize))
172                 return 0;       /* not equal apriori */
173
174         while(1) {
175                 while(obuf < oend && isspace(*obuf)) obuf++;
176                 while(nbuf < nend && isspace(*nbuf)) nbuf++;
177
178                 if(obuf == oend || nbuf == nend) {
179                         if(obuf == oend && nbuf == nend)
180                                 break;
181                         fprintf(stderr, "%s data in reconstructed encoding\n",
182                                 (obuf == oend) ? "More" : "Less");
183                         return 0;
184                 }
185
186                 if(*obuf != *nbuf) {
187                         printf("%c%c != %c%c\n",
188                                 obuf[0], obuf[1],
189                                 nbuf[0], nbuf[1]);
190                         return 0;
191                 }
192                 obuf++, nbuf++;
193         }
194
195         return 1;
196 }
197
198 static void
199 process_XER_data(enum expectation expectation, unsigned char *fbuf, size_t size) {
200         PDU_t *st;
201
202         st = load_object_from(expectation, fbuf, size, ATS_BASIC_XER);
203         if(!st) return;
204
205         /* Save and re-load as DER */
206         save_object_as(st, ATS_DER);
207         ASN_STRUCT_FREE(asn_DEF_PDU, st);
208         st = load_object_from(expectation, buf, buf_offset, ATS_BER);
209         assert(st);
210
211         save_object_as(st,
212                         (expectation == EXP_CXER_EXACT
213                         || expectation == EXP_CXER_DIFF)
214                         ? ATS_CANONICAL_XER : ATS_BASIC_XER);
215         fprintf(stderr, "=== original ===\n");
216         fwrite(fbuf, 1, size, stderr);
217         fprintf(stderr, "=== re-encoded ===\n");
218         fwrite(buf, 1, buf_offset, stderr);
219         fprintf(stderr, "=== end ===\n");
220
221         switch(expectation) {
222         case EXP_DIFFERENT:
223                 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
224                 break;
225         case EXP_BROKEN:
226                 assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
227                 break;
228         case EXP_CXER_EXACT:
229                 buf[buf_offset++] = '\n';
230                 assert((ssize_t)size == buf_offset);
231                 assert(memcmp(fbuf, buf, size) == 0);
232                 break;
233         case EXP_CXER_DIFF:
234                 buf[buf_offset++] = '\n';
235                 assert((ssize_t)size != buf_offset
236                         || memcmp(fbuf, buf, size));
237                 break;
238         case EXP_OK:
239                 assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
240                 break;
241         }
242
243         ASN_STRUCT_FREE(asn_DEF_PDU, st);
244 }
245
246 /*
247  * Decode the .der files and try to regenerate them.
248  */
249 static int
250 process(const char *fname) {
251         char prevdir[256];
252         unsigned char fbuf[4096];
253         char *ext = strrchr(fname, '.');
254         enum expectation expectation;
255         char *cwd;
256         int ret;
257         int rd;
258         FILE *fp;
259
260         if(ext == 0 || strcmp(ext, ".in"))
261                 return 0;
262
263         switch(ext[-1]) {
264         case 'B':       /* The file is intentionally broken */
265                 expectation = EXP_BROKEN; break;
266         case 'D':       /* Reconstructing should yield different data */
267                 expectation = EXP_DIFFERENT; break;
268         case 'E':       /* Byte to byte exact reconstruction */
269                 expectation = EXP_CXER_EXACT; break;
270         case 'X':       /* Should fail byte-to-byte comparison */
271                 expectation = EXP_CXER_DIFF; break;
272         default:
273                 expectation = EXP_OK; break;
274         }
275
276         fprintf(stderr, "\nProcessing file [../%s]\n", fname);
277
278         cwd = getcwd(prevdir, sizeof(prevdir));
279         assert(cwd != NULL);
280         ret = chdir(SRCDIR_S "/data-70");
281         assert(ret == 0);
282         fp = fopen(fname, "r");
283         ret = chdir(prevdir);
284         assert(ret == 0);
285         assert(fp);
286
287         rd = fread(fbuf, 1, sizeof(fbuf), fp);
288         fclose(fp);
289
290         assert(rd < (ssize_t)sizeof(fbuf));     /* expect small files */
291
292         process_XER_data(expectation, fbuf, rd);
293
294         return 1;
295 }
296
297 int
298 main() {
299         DIR *dir;
300         struct dirent *dent;
301         int processed_files = 0;
302         char *str;
303
304         /* Process a specific test file */
305         str = getenv("DATA_70_FILE");
306         if(str && strncmp(str, "data-70-", 8) == 0) {
307                 process(str);
308                 return 0;
309         }
310
311         dir = opendir(SRCDIR_S "/data-70");
312         assert(dir);
313
314         /*
315          * Process each file in that directory.
316          */
317         while((dent = readdir(dir))) {
318                 if(strncmp(dent->d_name, "data-70-", 8) == 0)
319                         if(process(dent->d_name))
320                                 processed_files++;
321         }
322
323         assert(processed_files);
324         closedir(dir);
325
326         return 0;
327 }
328
329 #endif