2
#include "asn1fix_internal.h"
16
static int check(const char *fname,
17
enum asn1p_flags parser_flags,
18
enum asn1f_flags fixer_flags);
19
static int post_fix_check(asn1p_t *asn);
20
static int post_fix_check_element(asn1p_module_t *mod, asn1p_expr_t *expr);
23
main(int ac, char **av) {
26
struct _finddata_t c_file;
33
enum asn1p_flags parser_flags = A1P_NOFLAGS;
34
enum asn1f_flags fixer_flags = A1F_NOFLAGS;
40
* Just in case when one decides that some flags better be
41
* enabled during `ASN1_FIXER_FLAGS=1 make check` or some
44
if(getenv("ASN1_PARSER_FLAGS"))
45
parser_flags = atoi(getenv("ASN1_PARSER_FLAGS"));
46
if(getenv("ASN1_FIXER_FLAGS"))
47
fixer_flags = atoi(getenv("ASN1_FIXER_FLAGS"));
50
* Go into a directory with tests.
53
fprintf(stderr, "Testing in ./tests...\n");
54
ret = chdir("../tests");
57
dir = _findfirst("*.asn1", &c_file);
68
* Scan every *.asn1 file and try to parse and fix it.
73
filename = c_file.name;
75
while((dp = readdir(dir))) {
76
filename = dp->d_name;
78
len = strlen(filename);
79
if(len && strcmp(filename + len - 5, ".asn1") == 0) {
80
ret = check(filename, parser_flags,fixer_flags);
82
fprintf(stderr, "FAILED: %s\n",
89
} while(_findnext(dir, &c_file) == 0);
98
"Tests COMPLETED: %d\n"
105
for(i = 1; i < ac; i++) {
106
ret = check(av[i], parser_flags, fixer_flags);
108
fprintf(stderr, "FAILED: %s\n", av[i]);
116
fprintf(stderr, "No tests defined?!\n");
126
check(const char *fname,
127
enum asn1p_flags parser_flags,
128
enum asn1f_flags fixer_flags) {
130
int expected_parseable; /* Is it expected to be parseable? */
131
int expected_fix_code; /* What code a fixer must return */
135
* Figure out how the processing should go by inferring
136
* expectations from the file name.
138
if(strstr(fname, "-OK.")) {
139
expected_parseable = 1;
140
expected_fix_code = 0;
141
} else if(strstr(fname, "-NP.")) {
142
expected_parseable = 0;
143
expected_fix_code = 123; /* Does not matter */
144
} else if(strstr(fname, "-SE.")) {
145
expected_parseable = 1;
146
expected_fix_code = -1; /* Semantically incorrect */
147
} else if(strstr(fname, "-SW.")) {
148
expected_parseable = 1;
149
expected_fix_code = 1; /* Semantically suspicious */
151
fprintf(stderr, "%s: Invalid file name format\n", fname);
155
fprintf(stderr, "[=> %s]\n", fname);
158
* Perform low-level parsing.
160
if(!expected_parseable)
161
fprintf(stderr, "Expecting error...\n");
162
asn = asn1p_parse_file(fname, parser_flags);
164
if(expected_parseable) {
165
fprintf(stderr, "Cannot parse file \"%s\"\n", fname);
169
"Previous error is EXPECTED, no worry\n");
171
} else if(!expected_parseable) {
173
"The file \"%s\" is not expected to be parseable, "
174
"yet parsing was successfull!\n", fname);
179
* Perform semantical checks and fixes.
181
if(asn && r_value == 0) {
184
if(expected_fix_code)
185
fprintf(stderr, "Expecting some problems...\n");
187
ret = asn1f_process(asn, fixer_flags, 0);
189
if(ret == expected_fix_code) {
191
"Previous error is EXPECTED, "
195
"Cannot process file \"%s\": %d\n",
199
} else if(ret != expected_fix_code) {
201
"File \"%s\" is expected "
202
"to be semantically incorrect, "
203
"yet processing was successful!\n",
210
* Check validity of some values, if grammar has special
211
* instructions for that.
213
if(asn && r_value == 0) {
214
if(post_fix_check(asn))
219
* TODO: destroy the asn.
227
post_fix_check(asn1p_t *asn) {
232
TQ_FOR(mod, &(asn->modules), mod_next) {
233
TQ_FOR(expr, &(mod->members), next) {
234
assert(expr->Identifier);
235
if(strncmp(expr->Identifier, "check-", 6) == 0) {
236
if(post_fix_check_element(mod, expr))
247
post_fix_check_element(asn1p_module_t *mod, asn1p_expr_t *check_expr) {
248
asn1p_expr_t *expr = NULL;
250
asn1p_value_t *value;
252
if(check_expr->expr_type != ASN_BASIC_INTEGER
253
|| check_expr->meta_type != AMT_VALUE) {
255
"CHECKER: Unsupported type of \"%s\" value: "
256
"%d at line %d of %s\n",
257
check_expr->Identifier,
258
check_expr->expr_type,
260
mod->source_file_name
265
assert(check_expr->meta_type == AMT_VALUE);
267
value = check_expr->value;
268
if(value == NULL || value->type != ATV_INTEGER) {
270
"CHECKER: Unsupported value type of \"%s\": "
271
"%d at line %d of %s\n",
272
check_expr->Identifier,
273
value?(signed)value->type:-1,
275
mod->source_file_name
280
name = check_expr->Identifier + sizeof("check-") - 1;
283
* Scan in search for the original.
285
TQ_FOR(expr, &(mod->members), next) {
286
if(strcmp(expr->Identifier, name) == 0)
292
"CHECKER: Value \"%s\" requested by "
293
"\"check-%s\" at line %d of %s is not found!\n",
294
name, name, check_expr->_lineno,
295
mod->source_file_name
300
if(0 && expr->expr_type != check_expr->expr_type) {
302
"CHECKER: Value type of \"%s\" (=%d) at line %d "
303
"does not have desired type %d as requested by "
304
"\"check-%s\" in %s\n",
308
check_expr->expr_type,
310
mod->source_file_name
315
if(expr->value == NULL
316
|| expr->value->type != value->type) {
318
"CHECKER: Value of \"%s\" (\"%s\", type=%d) at line %d "
319
"does not have desired type %d as requested by "
320
"\"check-%s\" in %s\n",
322
asn1f_printable_value(expr->value),
327
mod->source_file_name
332
assert(value->type = ATV_INTEGER);