File: enumcheck.c

package info (click to toggle)
libreswan 4.3-1%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,688 kB
  • sloc: ansic: 108,293; sh: 25,973; xml: 11,756; python: 10,230; makefile: 1,580; javascript: 1,353; yacc: 825; sed: 647; perl: 584; lex: 159; awk: 156
file content (369 lines) | stat: -rw-r--r-- 10,352 bytes parent folder | download
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
#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"

#define PREFIX "         "

unsigned errors;

enum enum_name_expectation {
	OPTIONAL,
	PRESENT,
	ABSENT,
};

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

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

	/*
	 * 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)) {
			printf("OK\n");
		} else {
			printf("ERROR\n");
			errors++;
		}
	}

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

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

	if (strchr(name, '(') != NULL) {
		char *clone = clone_str(name, "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);
	const char *short_name = enum_short_name(enum_test, i);
	printf("%s ", short_name);
	if (short_name == NULL) {
		printf("ERROR\n");
		errors++;
		return;
	} else {
		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)) {
			printf("OK\n");
		} else {
			printf("ERROR\n");
			errors++;
		}
	}

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

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

	if (strchr(short_name, '(') != NULL) {
		char *trunc_short_name = clone_str(short_name, "trunc_short_name");
		*strchr(trunc_short_name, '(') = '\0';
		printf(PREFIX "match %s [short+trunc]: ", trunc_short_name);
		int e = enum_match(enum_test, shunk1(trunc_short_name));
		pfree(trunc_short_name);
		if (e != i) {
			printf("%d ERROR\n", e);
			errors++;
		} else {
			printf("OK\n");
		}
	}
}

static void test_enum_range(char *enumname, enum_names *enum_test, int floor, int long roof)
{
	printf("  %s:\n", enumname);
	for (int i = floor; i < roof; i++) {
		test_enum(enum_test, i, OPTIONAL);
	}
	printf("\n");
}

static void test_enums(char *enumname, enum_names *enum_test)
{
	printf("  %s:\n", enumname);
	int first = -1;
	int last = -1;
	for (int i = next_enum(enum_test, -1);
	     i >= 0; i = next_enum(enum_test, i)) {
		if (first < 0) {
			first = i;
		} else if (i <= first) {
			printf("enum %d <= first %d\n", i, first);
		}
		if (i <= last) {
			printf("enum %d <= last %d\n", i, last);
		}
		last = i;
		test_enum(enum_test, i, PRESENT);
	}
	test_enum(enum_test, last + 1, ABSENT);
	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_table %lu: ", table);
		if (en == enum_enum_table(een, table)) {
			printf("OK\n");
		} else {
			printf("ERROR\n");
			errors++;
		}
	}

	printf(PREFIX "enum_enum_name %lu %lu: ", table, val);
	const char *name = enum_enum_name(een, table, val);
	printf("%s ", name == NULL ? "NULL" : name);
	if ((val_ok) == (name != NULL)) {
		printf("OK\n");
	} else {
		printf("ERROR\n");
		errors++;
	}

	printf(PREFIX "enum_name table %lu: ", val);
	if (en == NULL) {
		printf("N/A\n");
	} else if (name == enum_name(en, val)) {
		printf("OK\n");
	} else {
		printf("ERROR\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));
		/* ??? clang says that name might be NULL */
		if (val_ok && name == NULL) {
			printf("name == NULL\n");
		} else if (val_ok && hunk_streq(s, name)) {
			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));
		if (val_ok && hunk_streq(s, enum_short_name(en, val))) {
			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[])
{
	struct logger *logger = tool_init_log(argv[0]);

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

	printf("pluto enum_names:\n\n");
	test_enums("connection_kind_names", &connection_kind_names);
	test_enums("certpolicy_type_names", &certpolicy_type_names);

	printf("IETF registry enum_names:\n\n");
	test_enum_range("version_names", &version_names, 0, 256);
	test_enums("doi_names", &doi_names);
	test_enums("ikev1_payload_names", &ikev1_payload_names);
	test_enums("ikev2_payload_names", &ikev2_payload_names);
	test_enums("payload_names_ikev1orv2", &payload_names_ikev1orv2);
	test_enums("ikev1_exchange_names", &ikev1_exchange_names);
	test_enums("ikev2_exchange_names", &ikev2_exchange_names);
	test_enums("exchange_names_ikev1orv2", &exchange_names_ikev1orv2);
	test_enums("ikev1_protocol_names", &ikev1_protocol_names);
	test_enums("isakmp_transformid_names", &isakmp_transformid_names);
	test_enums("ah_transformid_names", &ah_transformid_names);
	test_enums("esp_transformid_names", &esp_transformid_names);
	test_enums("ipcomp_transformid_names", &ipcomp_transformid_names);
	test_enum_range("oakley_attr_names", &oakley_attr_names, 0, 256);
	test_enum_range("ipsec_attr_names", &ipsec_attr_names, 0, 256);
	test_enums("sa_lifetime_names", &sa_lifetime_names);
	test_enums("oakley_lifetime_names", &oakley_lifetime_names);
	test_enum_range("oakley_auth_names", &oakley_auth_names, 0, 256);
	test_enum_range("oakley_enc_names", &oakley_enc_names, 0, 256);
	test_enums("oakley_hash_names", &oakley_hash_names);
	test_enums("oakley_group_names", &oakley_group_names);
	test_enum_range("ikev1_notify_names", &ikev1_notify_names, 0, 16384);
	test_enum_range("ikev2_notify_names", &ikev2_notify_names, 0, 16384);
	test_enums("ikev2_ts_type_names", &ikev2_ts_type_names);
	test_enums("ikev2_cp_type_names", &ikev2_cp_type_names);
	test_enums("ikev2_cp_attribute_type_names", &ikev2_cp_attribute_type_names);
	test_enums("pkk_names", &pkk_names);
	test_enum_range("enc_mode_names", &enc_mode_names, 0, 256);
	test_enums("auth_alg_names", &auth_alg_names);
	test_enums("xauth_type_names", &xauth_type_names);
	test_enum_range("xauth_attr_names", &xauth_attr_names, 0, 256);
	test_enums("attr_msg_type_names", &attr_msg_type_names);
	test_enums("ikev2_proposal_protocol_id_names", &ikev2_proposal_protocol_id_names);
	test_enums("ikev2_delete_protocol_id_names", &ikev2_delete_protocol_id_names);
	test_enums("ikev2_notify_protocol_id_names", &ikev2_notify_protocol_id_names);
	test_enums("ikev2_auth_names", &ikev2_auth_names);
	test_enum_range("ikev2_trans_type_encr_names", &ikev2_trans_type_encr_names, 0, 256);
	test_enums("ikev2_trans_type_prf_names", &ikev2_trans_type_prf_names);
	test_enums("ikev2_trans_type_integ_names", &ikev2_trans_type_integ_names);
	test_enums("ikev2_trans_type_esn_names", &ikev2_trans_type_esn_names);
	test_enums("ikev2_trans_type_names", &ikev2_trans_type_names);
	test_enum_range("ikev2_trans_attr_descs", &ikev2_trans_attr_descs, 0, 256);
	test_enums("ike_cert_type_names", &ike_cert_type_names);
	test_enums("ikev2_cert_type_names", &ikev2_cert_type_names);
	test_enum_range("modecfg_attr_names", &modecfg_attr_names, 0, 256);
	test_enum_range("ike_idtype_names_extended", &ike_idtype_names_extended, -10, 0);
	test_enum_range("ike_idtype_names_extended", &ike_idtype_names_extended0, 0, 256);
	test_enums("ike_idtype_names", &ike_idtype_names);
	test_enums("ikev2_idtype_names", &ikev2_idtype_names);

	/*
	 * 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");

	report_leaks(logger);
	exit(errors > 0 ? 1 : 0);
}