File: ip_said_check.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 (130 lines) | stat: -rw-r--r-- 4,236 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
/* ip_said tests, for libreswan
 *
 * Copyright (C) 2000  Henry Spencer.
 * Copyright (C) 2019 Andrew Cagney
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 * License for more details.
 *
 */

#include <stdio.h>
#include <string.h>

#include "ip_said.h"
#include "ipcheck.h"

static void check_str_said(void)
{
	static const struct test {
		char *in;
		char *out;	/* NULL means error expected */
		bool fudge;
	} tests[] = {
		{ "esp257@1.2.3.0", "esp.101@1.2.3.0", false, },
		{ "ah0x20@1.2.3.4", "ah.20@1.2.3.4", false, },
		{ "tun20@1.2.3.4", "tun.14@1.2.3.4", false, },
		{ "comp20@1.2.3.4", "comp.14@1.2.3.4", false, },
		{ "esp257@::1", "esp:101@::1", false, },
		{ "esp257@0bc:12de::1", "esp:101@bc:12de::1", false, },
		{ "esp78@1049:1::8007:2040", "esp:4e@1049:1::8007:2040", false, },
		{ "esp0x78@1049:1::8007:2040", "esp:78@1049:1::8007:2040", false, },
		{ "ah78@1049:1::8007:2040", "ah:4e@1049:1::8007:2040", false, },
		{ "ah0x78@1049:1::8007:2040", "ah:78@1049:1::8007:2040", false, },
		{ "tun78@1049:1::8007:2040", "tun:4e@1049:1::8007:2040", false, },
		{ "tun0x78@1049:1::8007:2040", "tun:78@1049:1::8007:2040", false, },
		{ "duk99@3ffe:370:400:ff::9001:3001", NULL, false, },
		{ "esp78x@1049:1::8007:2040", NULL, false, },
		{ "esp0x78@1049:1:0xfff::8007:2040", NULL, false, },
		{ "es78@1049:1::8007:2040", NULL, false, },
		{ "", NULL, false, },
		{ "_", NULL, false, },
		{ "ah2.2", NULL, false, },
		{ "goo2@1.2.3.4", NULL, false, },
		{ "esp9@1.2.3.4", "esp.9@1.2.3.4", false, },
		{ "espp9@1.2.3.4", NULL, false, },
		{ "es9@1.2.3.4", NULL, false, },
		{ "ah@1.2.3.4", NULL, false, },
		{ "esp7x7@1.2.3.4", NULL, false, },
		{ "esp77@1.0x2.3.4", NULL, false, },
		{ PASSTHROUGHNAME, PASSTHROUGH4NAME, false, },
		{ PASSTHROUGH6NAME, PASSTHROUGH6NAME, false, },
		{ "%pass", "%pass", false, },
		{ "int256@0.0.0.0", "%pass", false, },
		{ "%drop", "%drop", false, },
		{ "int257@0.0.0.0", "%drop", false, },
		{ "%reject", "%reject", false, },
		{ "int258@0.0.0.0", "%reject", false, },
		{ "%hold", "%hold", false, },
		{ "int259@0.0.0.0", "%hold", false, },
		{ "%trap", "%trap", false, },
		{ "int260@0.0.0.0", "%trap", false, },
		{ "%trapsubnet", "%trapsubnet", false, },
		{ "int261@0.0.0.0", "%trapsubnet", false, },
		/* was "int.106@0.0.0.0" */
		{ "int262@0.0.0.0", "%unk-262", false, },
		{ "esp9@1.2.3.4", "unk.9@1.2.3.4", .fudge = true, },
		{ "unk77.9@1.2.3.4", NULL, false, },

		/* buffer size? */
		{ "esp.3a7292a2@192.1.2.24", "esp.3a7292a2@192.1.2.24", false, },
		{ "esp:3a7292a2@1000:2000:3000:4000:5000:6000:7000:8000", "esp:3a7292a2@1000:2000:3000:4000:5000:6000:7000:8000", false, },
	};

#define PRINT_SA(FILE, FMT, ...)					\
	PRINT(FILE, " '%s' fudge: %s"FMT,				\
	      t->in, bool_str(t->fudge),##__VA_ARGS__);

#define FAIL_SA(FMT, ...)						\
	{								\
		fails++;						\
		PRINT_SA(stderr, " "FMT" ("PRI_WHERE")",##__VA_ARGS__,	\
			 pri_where(HERE));				\
		continue;						\
	}

	for (size_t ti = 0; ti < elemsof(tests); ti++) {
		const struct test *t = &tests[ti];
		PRINT_SA(stdout, "");

		/* convert it *to* internal format */
		ip_said sa;
		err_t err = ttosa(t->in, strlen(t->in), &sa);
		if (err != NULL) {
			if (t->out != NULL) {
				FAIL_SA("ttosa() unexpectedly failed: %s", err);
			} else {
				/* all is good */
				continue;
			}
		} else if (t->out == NULL) {
			FAIL_SA("ttosa() unexpectedly succeeded");
		}

		if (t->fudge) {
			sa.proto = NULL;
		}

		/* now convert it back */
		said_buf buf;
		const char *out = str_said(&sa, &buf);
		if (out == NULL) {
			FAIL_SA("str_said() failed");
		} else if (!strcaseeq(t->out, out)) {
			FAIL_SA("str_said() returned '%s', expected '%s'",
				out, t->out);
		}
	}
}

void ip_said_check(void)
{
	check_str_said();
}