aboutsummaryrefslogtreecommitdiffstats
path: root/libasn1fix/check_fixer.c
blob: 18633241560abaf8f35782ca2115e2c0ed6b08a2 (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
373
374
375
376
377
378
379
#undef	NDEBUG
#include "asn1fix_internal.h"

#ifdef	_WIN32
#include <io.h>
#include <direct.h>
#define	chdir _chdir
#else
#include <dirent.h>
#include <sysexits.h>
#endif
#include <errno.h>

#include "asn1fix.h"

#ifndef TOP_SRCDIR
#define TOP_SRCDIR_S    ".."
#else
#define STRINGIFY_MACRO2(x)  #x
#define STRINGIFY_MACRO(x)  STRINGIFY_MACRO2(x)
#define TOP_SRCDIR_S    STRINGIFY_MACRO(TOP_SRCDIR)
#endif

static int check(int skeletons_hier_level, const char *fname,
	enum asn1p_flags parser_flags,
	enum asn1f_flags fixer_flags);
static int post_fix_check(asn1p_t *asn);
static int post_fix_check_element(asn1p_module_t *mod, asn1p_expr_t *expr);

int
main(int ac, char **av) {
#ifdef	_WIN32
	intptr_t dir;
	struct _finddata_t c_file;
#else
	struct dirent *dp;
	DIR *dir;
#endif
	int failed = 0;
	int completed = 0;
	enum asn1p_flags parser_flags = A1P_NOFLAGS;
	enum asn1f_flags fixer_flags  = A1F_NOFLAGS;
	const char *filename;
    int skeletons_hier_level = 1;
	size_t len;
	int ret;

	/*
	 * Just in case when one decides that some flags better be
	 * enabled during `ASN1_FIXER_FLAGS=1 make check` or some
	 * similar usage.
	 */
	if(getenv("ASN1_PARSER_FLAGS"))
		parser_flags = atoi(getenv("ASN1_PARSER_FLAGS"));
	if(getenv("ASN1_FIXER_FLAGS"))
		fixer_flags = atoi(getenv("ASN1_FIXER_FLAGS"));

	/*
	 * Go into a directory with tests.
	 */
	if(ac <= 1) {
        const char *asn1_tests_dir = getenv("ASN1_TESTS_DIR");
        if(!asn1_tests_dir)
            asn1_tests_dir = TOP_SRCDIR_S "/tests";
        fprintf(stderr, "Testing in %s...\n", asn1_tests_dir);
        ret = chdir(asn1_tests_dir);
        if(ret == -1)
            fprintf(stderr, "%s: %s\n", asn1_tests_dir, strerror(errno));
        assert(ret == 0);
        /* For some reasons, tests could be hidden under extra tests dir. */
        if(chdir("tests") == 0) {
            skeletons_hier_level++;
        }
#ifdef	_WIN32
		dir = _findfirst("*.asn1", &c_file);
		assert(dir != -1L);
#else
		dir = opendir(".");
		assert(dir);
#endif	/* _WIN32 */
	} else {
		dir = 0;
	}

	/*
	 * Scan every *.asn1 file and try to parse and fix it.
	 */
	if(dir) {
#ifdef	_WIN32
		do {
			filename = c_file.name;
#else
		while((dp = readdir(dir))) {
			filename = dp->d_name;
#endif	/* _WIN32 */
			len = strlen(filename);
			if(len <= 5 || strcmp(filename + len - 5, ".asn1"))
				continue;
			ret = check(skeletons_hier_level, filename, parser_flags, fixer_flags);
			if(ret) {
				fprintf(stderr, "FAILED: %s\n",
					filename);
				failed++;
			}
			completed++;
#ifdef	_WIN32
		} while(_findnext(dir, &c_file) == 0);
		_findclose(dir);
#else
		}
		closedir(dir);
#endif	/* _WIN32 */


		fprintf(stderr,
			"Tests COMPLETED: %d\n"
			"Tests FAILED:    %d\n"
			,
			completed, failed
		);
	} else {
		int i;
		for(i = 1; i < ac; i++) {
			ret = check(skeletons_hier_level, av[i], parser_flags, fixer_flags);
			if(ret) {
				fprintf(stderr, "FAILED: %s\n", av[i]);
				failed++;
			}
			completed++;
		}
	}

	if(completed == 0) {
		fprintf(stderr, "No tests defined?!\n");
		exit(EX_NOINPUT);
	}

	if(failed)
		exit(EX_DATAERR);
	return 0;
}

static int
check(int skeletons_hier_level, const char *fname,
		enum asn1p_flags parser_flags,
		enum asn1f_flags fixer_flags) {
	asn1p_t *asn;
	int expected_parseable;		/* Is it expected to be parseable? */
	int expected_fix_code;		/* What code a fixer must return */
	int r_value = 0;

	/*
	 * Figure out how the processing should go by inferring
	 * expectations from the file name.
	 */
	if(strstr(fname, "-OK.")) {
		expected_parseable = 1;
		expected_fix_code  = 0;
	} else if(strstr(fname, "-NP.")) {
		expected_parseable = 0;
		expected_fix_code  = 123;	/* Does not matter */
	} else if(strstr(fname, "-SE.")) {
		expected_parseable = 1;
		expected_fix_code  = -1;	/* Semantically incorrect */
	} else if(strstr(fname, "-SW.")) {
		expected_parseable = 1;
		expected_fix_code  = 1;	/* Semantically suspicious */
	} else {
		fprintf(stderr, "%s: Invalid file name format\n", fname);
		return -1;
	}

	/* Flag modifiers */
	if(strstr(fname, "-blessSize-"))
		fixer_flags |= A1F_EXTENDED_SizeConstraint;

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

	/*
	 * Perform low-level parsing.
	 */
	if(!expected_parseable)
		fprintf(stderr, "Expecting error...\n");
	asn = asn1p_parse_file(fname, parser_flags);
	if(asn == NULL) {
		if(expected_parseable) {
			fprintf(stderr, "Cannot parse file \"%s\"\n", fname);
			r_value = -1;
		} else {
			fprintf(stderr,
				"Previous error is EXPECTED, no worry\n");
		}
	} else if(!expected_parseable) {
		fprintf(stderr,
			"The file \"%s\" is not expected to be parseable, "
			"yet parsing was successfull!\n", fname);
		r_value = -1;
	}
	if(!asn) return r_value;

	if(r_value == 0) {
		asn1p_t *std_asn;
		std_asn = asn1p_parse_file(skeletons_hier_level == 1
                ? "../skeletons/standard-modules/ASN1C-UsefulInformationObjectClasses.asn1"
                : "../../skeletons/standard-modules/ASN1C-UsefulInformationObjectClasses.asn1"
                , A1P_NOFLAGS);
		if(std_asn) {
			asn1p_module_t *mod;
			while((mod = TQ_REMOVE(&(std_asn->modules), mod_next))) {
				mod->_tags |= MT_STANDARD_MODULE;
				TQ_ADD(&(asn->modules), mod, mod_next);
			}
			asn1p_delete(std_asn);
		}
	}

	/*
	 * Perform semantical checks and fixes.
	 */
	if(r_value == 0) {
		int ret;

		if(expected_fix_code)
			fprintf(stderr, "Expecting some problems...\n");

		ret = asn1f_process(asn, fixer_flags, 0);
		if(ret) {
			if(ret == expected_fix_code) {
				fprintf(stderr,
					"Previous error is EXPECTED, "
					"no worry\n");
			} else {
				fprintf(stderr,
					"Cannot process file \"%s\": %d\n",
					fname, ret);
				r_value = -1;
		}
		} else if(ret != expected_fix_code) {
			fprintf(stderr,
				"File \"%s\" is expected "
				"to be semantically incorrect, "
				"yet processing was successful!\n",
				fname);
			r_value = -1;
		}
	}

	/*
	 * Check validity of some values, if grammar has special
	 * instructions for that.
	 */
	if(r_value == 0) {
		if(post_fix_check(asn))
			r_value = -1;
	}

	/*
	 * Destroy the asn.
	 */
#ifdef	CLEAN_EVERYTHING
	asn1p_delete(asn);
#endif

	return r_value;
}


static int
post_fix_check(asn1p_t *asn) {
	asn1p_module_t *mod;
	asn1p_expr_t *expr;
	int r_value = 0;

	TQ_FOR(mod, &(asn->modules), mod_next) {
		TQ_FOR(expr, &(mod->members), next) {
			assert(expr->Identifier);
			if(strncmp(expr->Identifier, "check-", 6) == 0) {
				if(post_fix_check_element(mod, expr))
					r_value = -1;
			}
		}
	}

	return r_value;
}


static int
post_fix_check_element(asn1p_module_t *mod, asn1p_expr_t *check_expr) {
	asn1p_expr_t *expr = NULL;
	char *name;
	asn1p_value_t *value;

	if(check_expr->expr_type != ASN_BASIC_INTEGER
	|| check_expr->meta_type != AMT_VALUE) {
		fprintf(stderr,
			"CHECKER: Unsupported type of \"%s\" value: "
			"%d at line %d of %s\n",
			check_expr->Identifier,
			check_expr->expr_type,
			check_expr->_lineno,
			mod->source_file_name
		);
		return -1;
	}

	assert(check_expr->meta_type == AMT_VALUE);

	value = check_expr->value;
	if(value == NULL || value->type != ATV_INTEGER) {
		fprintf(stderr,
			"CHECKER: Unsupported value type of \"%s\": "
			"%d at line %d of %s\n",
			check_expr->Identifier,
			value?(signed)value->type:-1,
			expr->_lineno,
			mod->source_file_name
		);
		return -1;
	}

	name = check_expr->Identifier + sizeof("check-") - 1;

	/*
	 * Scan in search for the original.
	 */
	TQ_FOR(expr, &(mod->members), next) {
		if(strcmp(expr->Identifier, name) == 0)
			break;
	}

	if(expr == NULL) {
		fprintf(stderr,
			"CHECKER: Value \"%s\" requested by "
			"\"check-%s\" at line %d of %s is not found!\n",
			name, name, check_expr->_lineno,
			mod->source_file_name
		);
		return -1;
	}

	if(0 && expr->expr_type != check_expr->expr_type) {
		fprintf(stderr,
			"CHECKER: Value type of \"%s\" (=%d) at line %d "
			"does not have desired type %d as requested by "
			"\"check-%s\" in %s\n",
			expr->Identifier,
			expr->expr_type,
			expr->_lineno,
			check_expr->expr_type,
			name,
			mod->source_file_name
		);
		return -1;
	}

	if(expr->value == NULL
	|| expr->value->type != value->type) {
		fprintf(stderr,
			"CHECKER: Value of \"%s\" (\"%s\", type=%d) at line %d "
			"does not have desired type %d as requested by "
			"\"check-%s\" in %s\n",
			expr->Identifier,
			asn1f_printable_value(expr->value),
			expr->value->type,
			expr->_lineno,
			value->type,
			name,
			mod->source_file_name
		);
		return -1;
	}

	assert(value->type = ATV_INTEGER);

	return 0;
}