File: enumcheck.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (443 lines) | stat: -rw-r--r-- 10,324 bytes parent folder | download | duplicates (2)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "constants.h"
#include "lswalloc.h"
#include "lswtool.h"
#include "jambuf.h"
#include "passert.h"
#include "pexpect.h"
#include "enum_names.h"

#define PREFIX "         "

unsigned errors;

enum enum_name_expectation {
	OPTIONAL,
	PRESENT,
	ABSENT,
};

struct clash {
	int e;
	int short_match; /* returned by match(short_name) */
};

static void test_enum(enum_names *enum_test, int i,
		      enum enum_name_expectation expect,
		      const struct clash *clash)
{
	char scratch[100];

	/* find a name, if any, for this value */
	enum_buf name;
	bool found = enum_long(enum_test, i, &name);
	switch (expect) {
	case OPTIONAL:
		if (!found) {
			return;
		}
		break;
	case PRESENT:
		if (!found) {
			printf("name for %d missing (should be present)\n", i);
			return;
		}
		break;
	case ABSENT:
		if (found) {
			printf("name for %d present (should be absent)\n", i);
		}
		return;
	}
	printf("  %3d -> %s\n", i, name.buf);

	/*
	 * So that it is easy to see what was tested, print something
	 * for every comparison.
	 */

	if (i < 0) {
		/* we are cheating: don't do the other checks */
		return;
	}

	{
		printf(PREFIX "jam_enum %d: ", i);
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_enum(&buf, enum_test, i);
		shunk_t s = jambuf_as_shunk(&buf);
		printf(""PRI_SHUNK" ", pri_shunk(s));
		if (hunk_streq(s, name.buf)) {
			printf("OK\n");
		} else {
			printf("ERROR\n");
			errors++;
		}
	}

	{
		printf(PREFIX "search %s: ", name.buf);
		int e = enum_search(enum_test, name.buf);
		if (e != i) {
			printf("%d ERROR\n", e);
			errors++;
		} else {
			printf("OK\n");
		}
	}

	{
		printf(PREFIX "match %s: ", name.buf);
		int e = enum_match(enum_test, shunk1(name.buf));
		if (e != i) {
			printf("%d ERROR\n", e);
			errors++;
		} else {
			printf("OK\n");
		}
	}

	if (strchr(name.buf, '(') != NULL) {
		char *clone = clone_str(name.buf, "trunc_name");
		shunk_t trunc_name = shunk2(clone, strcspn(clone, "("));
		passert(clone[trunc_name.len] == '(');
		clone[trunc_name.len] = '*';
		printf(PREFIX "match "PRI_SHUNK" [trunc]: ",
		       pri_shunk(trunc_name));
		int e = enum_match(enum_test, trunc_name);
		pfree(clone);
		if (e != i) {
			printf("%d ERROR\n", e);
			errors++;
		} else {
			printf("OK\n");
		}
	}

	printf(PREFIX "short_name %d: ", i);
	enum_buf short_name;
	if (!enum_name_short(enum_test, i, &short_name)) {
		printf("ERROR\n");
		errors++;
		return;
	}
	printf("%s ", short_name.buf);
	printf("OK\n");

	{
		printf(PREFIX "jam_enum_short %d: ", i);
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_enum_short(&buf, enum_test, i);
		shunk_t s = jambuf_as_shunk(&buf);
		printf(""PRI_SHUNK" ", pri_shunk(s));
		if (hunk_streq(s, short_name.buf)) {
			printf("OK\n");
		} else {
			printf("ERROR\n");
			errors++;
		}
	}

	{
		printf(PREFIX "jam_enum_human %d: ", i);
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_enum_human(&buf, enum_test, i);
		shunk_t s = jambuf_as_shunk(&buf);
		printf(""PRI_SHUNK" ", pri_shunk(s));
		if (strchr(short_name.buf, '_') == NULL) {
			if (hunk_strcaseeq(s, short_name.buf)) {
				printf("OK\n");
			} else {
				printf("ERROR\n");
				errors++;
			}
		} else {
			printf("OK\n"); /* what can be checked? */
		}
	}


	if (streq(short_name.buf, name.buf)) {
		/* remaining tests redundant */
		return;
	}

	{
		printf(PREFIX "match %s [short]: ", short_name.buf);
		int e = enum_match(enum_test, shunk1(short_name.buf));
		int short_match = (clash == NULL ? -1 : clash->short_match);
		if (short_match >= 0 && e == short_match) {
			printf("OK (clashed with %d)\n", short_match);
		} else if (short_match >= 0) {
			printf("%d ERROR (should clash with %d)\n", e, short_match);
			errors++;
		} else if (e == i) {
			printf("OK\n");
		} else {
			printf("%d ERROR\n", e);
			errors++;
		}
	}

	const char *bra = strchr(short_name.buf, '(');
	if (bra != NULL) {
		int tsl = bra - short_name.buf;
		printf(PREFIX "match %.*s [short+trunc]: ", tsl, short_name.buf);
		int e = enum_match(enum_test, shunk2(short_name.buf, tsl));
		if (e != i) {
			printf("%d ERROR\n", e);
			errors++;
		} else {
			printf("OK\n");
		}
	}
}

struct bounds {
	long int floor;
	long int roof;
};

static struct bounds enum_bounds(const struct enum_names *en)
{
	/* find lower and upper bounds; might contain gaps */
	long int first = next_enum(en, -1);
	struct bounds b = {
		.floor = first,
		.roof = first+1,
	};
	for (int e = next_enum(en, first);
	     e >= 0; e = next_enum(en, e)) {
		b.roof = e+1;
	}
	return b;
}

static void test_enum_range(char *enumname, enum_names *enum_test, long int floor, long int roof)
{
	struct bounds b = enum_bounds(enum_test);
	printf("  %s: ", enumname);
	printf("[%ld", b.floor);
	if (b.floor != floor) {
		printf("(%ld)", floor);
	}
	printf("..");
	printf("%ld", b.roof);
	if (b.roof != roof) {
		printf("(%ld)", roof);
	}
	printf(")\n");
	for (int i = floor; i < roof; i++) {
		test_enum(enum_test, i, OPTIONAL, NULL);
	}
	printf("\n");
}

static void test_enums(const char *enumname, enum_names *enum_test, const struct clash *clashes)
{
	struct bounds b = enum_bounds(enum_test);
	printf("  %s: [%ld..%ld)\n", enumname, b.floor, b.roof);

	/* test those next_enum() returns */
	int clashed = 0;
	for (int i = next_enum(enum_test, -1);
	     i >= 0; i = next_enum(enum_test, i)) {
		const struct clash *clash = NULL;
		for (const struct clash *c = clashes; c != NULL && c->e >= 0; c++) {
 			if (c->e == i) {
				clashed++;
				clash = c;
 				break;
 			}
 		}
		test_enum(enum_test, i, PRESENT, clash);
	}

	test_enum(enum_test, b.floor-1, ABSENT, NULL);
	test_enum(enum_test, b.roof, ABSENT, NULL);

	for (const struct clash *c = clashes; c != NULL && c->e >= 0; c++) {
		clashed--;
	}
	if (clashed != 0) {
		printf("    ERROR missing clashes %d\n", clashed);
		errors++;
	}

	/* check tables are not empty */
	unsigned level = 0;
	for (const struct enum_names *en = enum_test; en != NULL; en = en->en_next_range) {
		unsigned count = 0;
		for (unsigned i = 0; i < en->en_checklen; i++) {
			if (en->en_names[i] != NULL) {
				count++;
			}
		}
		/*
		 * The goal is to catch an array with an entry at 0
		 * and another at 64000 say.  10% is pretty arbitrary.
		 */
		if (count * 10 < en->en_checklen) {
			llog_pexpect(&global_logger, HERE,
				     "enum table %s at level %u of size %zu only contains %u names",
				     enumname, level, en->en_checklen, count);
		}
		level++;
	}

	printf("\n");
}

static void test_enum_enum(const char *title, enum_enum_names *een,
			   unsigned long table, enum_names *en,
			   unsigned long val, bool val_ok)
{
	char scratch[100];

	printf("%s:\n", title);

	printf(PREFIX "enum_enum_name %lu %lu: ", table, val);
	enum_buf name;
	bool name_ok = enum_enum_name(een, table, val, &name);
	printf("%s ", (name_ok ? name.buf : "NULL"));
	if (name_ok == val_ok) {
		printf("OK\n");
	} else {
		printf("ERROR\n");
		errors++;
	}

	printf(PREFIX "enum_name table %lu: ", val);
	if (en == NULL) {
		printf("N/A\n");
	} else {
		enum_buf n;
		if (enum_name(en, val, &n)) {
			/*
			 * Should point to the same static string.
			 * name.buf and n.buf are never NULL.
			 */
			if (name.buf == n.buf) {
				printf("OK\n");
			} else {
				printf("ERROR (pointer)\n");
				errors++;
			}
		} else if (!name_ok) {
			printf("OK\n");
		} else {
			printf("ERROR (lookup)\n");
			errors++;
		}
	}

	{
		printf(PREFIX "jam_enum_enum %lu %lu: ", table, val);
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_enum_enum(&buf, een, table, val);
		shunk_t s = jambuf_as_shunk(&buf);
		printf(""PRI_SHUNK" ", pri_shunk(s));
		if (val_ok && hunk_streq(s, name.buf)) {
			printf("OK\n");
		} else if (s.len > 0) {
			printf("OK\n");
		} else {
			printf("ERROR [empty]\n");
			errors++;
		}
	}

	{
		printf(PREFIX "jam_enum_enum_short %lu %lu: ", table, val);
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_enum_enum_short(&buf, een, table, val);
		shunk_t s = jambuf_as_shunk(&buf);
		printf(""PRI_SHUNK" ", pri_shunk(s));
		enum_buf enb;
		if (val_ok && hunk_streq(s, str_enum_short(en, val, &enb))) {
			printf("OK\n");
		} else if (s.len > 0) {
			printf("OK\n");
		} else {
			printf("ERROR [empty]\n");
			errors++;
		}
	}
}

static void test_enum_lset(const char *name, const enum_names *en, lset_t val)
{
	printf("  %s "PRI_LSET":\n", name, val);
	printf("\t<<");
	{
		char scratch[100];
		struct jambuf buf = ARRAY_AS_JAMBUF(scratch);
		jam_lset_short(&buf, en, "+", val);
		printf(PRI_SHUNK, pri_shunk(jambuf_as_shunk(&buf)));
	}
	printf(">>");
}

int main(int argc UNUSED, char *argv[])
{
	leak_detective = true;
	struct logger *logger = tool_logger(argc, argv);

	/* don't hold back */
	setbuf(stdout, NULL);

	init_enum_names();

	for (const struct enum_names_check *c = enum_names_checklist;
	     c->name != NULL && c->enum_names != NULL; c++) {
		if (c->enum_names == &encapsulation_mode_names) {
			test_enum_range("encapsulation_mode_names", &encapsulation_mode_names, 0, 256);
		} else if (c->enum_names == &event_type_names) {
			static const struct clash clash[] = {
				{ EVENT_v1_EXPIRE, EVENT_v2_EXPIRE, },
				{ EVENT_v1_DISCARD, EVENT_v2_DISCARD, },
				{ EVENT_v1_REPLACE, EVENT_v2_REPLACE, },
				{ EVENT_v1_RETRANSMIT, EVENT_v2_RETRANSMIT, },
				{ EVENT_v1_NAT_KEEPALIVE, EVENT_v2_NAT_KEEPALIVE, },
				{ -1, -1, },
			};
			test_enums("event_type_names", c->enum_names, clash);
		} else {
			test_enums(c->name, c->enum_names, NULL);
		}
	}

	/*
	 * Some hard-wired checks of enum_enum_name.  If a lookup
	 * should fail, pass NULL for the enum table.
	 */
	test_enum_enum("IKEv2 transforms", &v2_transform_ID_enums,
		       IKEv2_TRANS_TYPE_ENCR, &ikev2_trans_type_encr_names,
		       IKEv2_ENCR_3DES, true);
	test_enum_enum("IKEv2 transforms", &v2_transform_ID_enums,
		       IKEv2_TRANS_TYPE_ROOF, NULL,
		       1, false);
	test_enum_enum("IKEv2 transforms", &v2_transform_ID_enums,
		       IKEv2_TRANS_TYPE_PRF, &ikev2_trans_type_prf_names,
		       IKEv2_PRF_INVALID, false);
	printf("\n");

	printf("jam_enum_lset_short:\n\n");
	test_enum_lset("debug", &debug_names, DBG_CRYPT|DBG_CPU_USAGE);
	printf("\n");

	if (report_leaks(logger)) {
		errors++;
	}

	if (errors > 0) {
		fprintf(stderr, "TOTAL FAILURES: %d\n", errors);
		return 1;
	}

	return 0;
}