aboutsummaryrefslogtreecommitdiffstats
path: root/asn1c/tests/check-src/check-126.-gen-PER.c
blob: 595a1dbf1031bbc103141e29c3dd2f9f6482c52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Mode of operation:
 * Each of the *.in files is XER-decoded, then converted into PER,
 * compared with *.out file, then decoded and compared with the original.
 */
#undef	NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>	/* for chdir(2) */
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>

#include <PDU.h>

#ifndef SRCDIR
#define SRCDIR_S ".."
#else
#define STRINGIFY_MACRO2(x) #x
#define STRINGIFY_MACRO(x)  STRINGIFY_MACRO2(x)
#define SRCDIR_S    STRINGIFY_MACRO(SRCDIR)
#endif

static unsigned char buf[4096];
static int buf_offset;

static int
_buf_writer(const void *buffer, size_t size, void *app_key) {
	unsigned char *b, *bend;
	(void)app_key;
	assert(buf_offset + size < sizeof(buf));
	memcpy(buf + buf_offset, buffer, size);
	b = buf + buf_offset;
	bend = b + size;
	fprintf(stderr, "=> [");
	for(; b < bend; b++) {
		if(*b >= 32 && *b < 127 && *b != '%')
			fprintf(stderr, "%c", *b);
		else
			fprintf(stderr, "%%%02x", *b);
	}
	fprintf(stderr, "]:%zd\n", size);
	buf_offset += size;
	return 0;
}

enum enctype {
	AS_PER,
	AS_DER,
	AS_XER,
};

static void
save_object_as(PDU_t *st, enum enctype how) {
	asn_enc_rval_t rval; /* Return value */

	buf_offset = 0;

	/*
	 * Save object using specified method.
	 */
	switch(how) {
	case AS_PER:
		rval = uper_encode(&asn_DEF_PDU, st, _buf_writer, 0);
		assert(rval.encoded > 0);
		fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
		return;
	case AS_DER:
		rval = der_encode(&asn_DEF_PDU, st,
			_buf_writer, 0);
		break;
	case AS_XER:
		rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
			_buf_writer, 0);
		break;
	}

	if (rval.encoded == -1) {
		fprintf(stderr,
			"Cannot encode %s: %s\n",
			rval.failed_type->name, strerror(errno));
		assert(rval.encoded != -1);
		return;
	}

	fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
}

static PDU_t *
load_object_from(const char *fname, unsigned char *fbuf, size_t size, enum enctype how, int mustfail) {
	asn_dec_rval_t rval;
	PDU_t *st = 0;
	ssize_t csize = 1;

	if(getenv("INITIAL_CHUNK_SIZE"))
		csize = atoi(getenv("INITIAL_CHUNK_SIZE"));

	/* Perform multiple iterations with multiple chunks sizes */
	for(; csize < 20; csize += 1) {
		int fbuf_offset = 0;
		int fbuf_left = size;
		int fbuf_chunk = csize;

		fprintf(stderr, "LOADING OBJECT OF SIZE %zd FROM [%s] as %s,"
			" chunks %zd\n",
			size, fname, how==AS_PER?"PER":"XER", csize);

		if(st) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
		st = 0;

		do {
			fprintf(stderr, "\nDecoding bytes %d..%d (left %d) [%s]\n",
				fbuf_offset,
					fbuf_chunk < fbuf_left
						? fbuf_chunk : fbuf_left,
					fbuf_left,
				fname);
			if(st) {
				fprintf(stderr, "=== currently ===\n");
				asn_fprint(stderr, &asn_DEF_PDU, st);
				fprintf(stderr, "=== end ===\n");
			}
			switch(how) {
			case AS_XER:
				rval = xer_decode(0, &asn_DEF_PDU, (void **)&st,
					fbuf + fbuf_offset,
					fbuf_chunk < fbuf_left 
					? fbuf_chunk : fbuf_left);
				break;
			case AS_DER:
				assert(0);
				break;
			case AS_PER:
				rval = uper_decode(0, &asn_DEF_PDU,
					(void **)&st, fbuf + fbuf_offset,
					fbuf_chunk < fbuf_left 
					? fbuf_chunk : fbuf_left, 0, 0);
				if(rval.code == RC_WMORE) {
					if(fbuf_chunk == fbuf_left) {
						fprintf(stderr, "-> PER decode error (%zd bits of %zd bytes (c=%d,l=%d)) \n", rval.consumed, size, fbuf_chunk, fbuf_left);
						rval.code = RC_FAIL;
						rval.consumed += 7;
						rval.consumed /= 8;
						if(mustfail) {
							fprintf(stderr, "-> (this was expected failure)\n");
							return 0;
						}
					} else {
						rval.consumed = 0; /* Not restartable */
						ASN_STRUCT_FREE(asn_DEF_PDU, st);
						st = 0;
						fprintf(stderr, "-> PER wants more\n");
					}
				} else {
					fprintf(stderr, "-> PER ret %d/%zd mf=%d\n",
						rval.code, rval.consumed, mustfail);
					/* uper_decode() returns _bits_ */
					rval.consumed += 7;
					rval.consumed /= 8;
					if((mustfail?1:0) == (rval.code == RC_FAIL)) {
						if(mustfail) {
							fprintf(stderr, "-> (this was expected failure)\n");
							return 0;
						}
					} else {
						fprintf(stderr, "-> (unexpected %s)\n", mustfail ? "success" : "failure");
						rval.code = RC_FAIL;
					}
				}
				break;
			}
			fbuf_offset += rval.consumed;
			fbuf_left -= rval.consumed;
			if(rval.code == RC_WMORE)
				fbuf_chunk += 1;	/* Give little more */
			else
				fbuf_chunk = csize;	/* Back off */
		} while(fbuf_left && rval.code == RC_WMORE);

		assert(rval.code == RC_OK);
		if(how == AS_PER) {
			fprintf(stderr, "[left %d, off %d, size %zd]\n",
				fbuf_left, fbuf_offset, size);
			assert(fbuf_offset == (ssize_t)size);
		} else {
			assert(fbuf_offset - size < 2
			|| (fbuf_offset + 1 /* "\n" */  == (ssize_t)size
				&& fbuf[size - 1] == '\n')
			|| (fbuf_offset + 2 /* "\r\n" */  == (ssize_t)size
				&& fbuf[size - 2] == '\r'
				&& fbuf[size - 1] == '\n')
			);
		}
	}

	if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
	return st;
}

static int
xer_encoding_equal(void *obufp, size_t osize, void *nbufp, size_t nsize) {
    char *obuf = obufp;
    char *nbuf = nbufp;
	char *oend = obuf + osize;
	char *nend = nbuf + nsize;

	if((osize && !nsize) || (!osize && nsize))
		return 0;	/* not equal apriori */

	while(1) {
		while(obuf < oend && isspace(*obuf)) obuf++;
		while(nbuf < nend && isspace(*nbuf)) nbuf++;

		if(obuf == oend || nbuf == nend) {
			if(obuf == oend && nbuf == nend)
				break;
			fprintf(stderr, "%s data in reconstructed encoding\n",
				(obuf == oend) ? "More" : "Less");
			return 0;
		}

		if(*obuf != *nbuf) {
			printf("%c%c != %c%c\n",
				obuf[0], obuf[1],
				nbuf[0], nbuf[1]);
			return 0;
		}
		obuf++, nbuf++;
	}

	return 1;
}

static void
compare_with_data_out(const char *fname, void *datap, size_t size) {
    char *data = datap;
	char outName[sizeof(SRCDIR_S) + 256];
	unsigned char fbuf[1024];
	size_t rd;
	FILE *f;
	char lastChar;
	int mustfail, compare;

	sprintf(outName, SRCDIR_S "/data-126/%s", fname);
	strcpy(outName + strlen(outName) - 3, ".out");

	fprintf(stderr, "Comparing PER output with [%s]\n", outName);

	lastChar = outName[strlen(outName)-5];
	mustfail = lastChar == 'P';
	compare = lastChar != 'C';

	if((compare && !mustfail) && getenv("REGENERATE")) {
		f = fopen(outName, "w");
		fwrite(data, 1, size, f);
		fclose(f);
	} else {
		f = fopen(outName, "r");
		assert(f);
		rd = fread(fbuf, 1, sizeof(fbuf), f);
		assert(rd);
		fclose(f);

		fprintf(stderr, "Trying to decode [%s]\n", outName);
		load_object_from(outName, fbuf, rd, AS_PER, mustfail);
		if(mustfail) return;

		if(compare) {
			assert(rd == (size_t)size);
			assert(memcmp(fbuf, data, rd) == 0);
			fprintf(stderr, "XER->PER recoding .in->.out match.\n");
		} else {
			assert(rd != (size_t)size || memcmp(fbuf, data, rd));
			fprintf(stderr, "XER->PER recoding .in->.out diverge.\n");
		}
	}
}

static void
process_XER_data(const char *fname, unsigned char *fbuf, size_t size) {
	PDU_t *st;

	st = load_object_from(fname, fbuf, size, AS_XER, 0);
	if(!st) return;

	/* Save and re-load as PER */
	save_object_as(st, AS_PER);
	compare_with_data_out(fname, buf, buf_offset);
	st = load_object_from("buffer", buf, buf_offset, AS_PER, 0);
	assert(st);

	save_object_as(st, AS_XER);
	fprintf(stderr, "=== original ===\n");
	fwrite(fbuf, 1, size, stderr);
	fprintf(stderr, "=== re-encoded ===\n");
	fwrite(buf, 1, buf_offset, stderr);
	fprintf(stderr, "=== end ===\n");

	if(fname[strlen(fname) - 4] == 'X')
		assert(!xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));
	else
		assert(xer_encoding_equal((char *)fbuf, size, (char *)buf, buf_offset));

	asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
}

/*
 * Decode the .der files and try to regenerate them.
 */
static int
process(const char *fname) {
	unsigned char fbuf[sizeof(SRCDIR_S) + 4096];
	char *ext = strrchr(fname, '.');
	int rd;
	FILE *fp;

	if(ext == 0 || strcmp(ext, ".in"))
		return 0;

	fprintf(stderr, "\nProcessing file [../%s]\n", fname);

	snprintf((char *)fbuf, sizeof(fbuf), SRCDIR_S "/data-126/%s", fname);
	fp = fopen((char *)fbuf, "r");
	assert(fp);

	rd = fread(fbuf, 1, sizeof(fbuf), fp);
	fclose(fp);

	assert((size_t)rd < sizeof(fbuf));	/* expect small files */

	process_XER_data(fname, fbuf, rd);

	fprintf(stderr, "Finished [%s]\n", fname);

	return 1;
}

int
main() {
	DIR *dir;
	struct dirent *dent;
	int processed_files = 0;
	char *str;

	/* Process a specific test file */
	str = getenv("DATA_126_FILE");
	if(str && strncmp(str, "data-126-", 9) == 0) {
		process(str);
		return 0;
	}

	dir = opendir(SRCDIR_S "/data-126");
	assert(dir);

	/*
	 * Process each file in that directory.
	 */
	while((dent = readdir(dir))) {
		if(strncmp(dent->d_name, "data-126-", 9) == 0)
			if(process(dent->d_name))
				processed_files++;
	}

	assert(processed_files);
	closedir(dir);

	return 0;
}