File: ip_said_check.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 (107 lines) | stat: -rw-r--r-- 3,617 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
/* 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__ttosaid__str_said(void)
{
	static const struct test {
		int line;
		char *in;
		char *out;	/* NULL means error expected */
	} tests[] = {
		/* all known prefixes */
		{ LN, "icmp.1@1.2.3.0", "icmp.1@1.2.3.0", },
		{ LN, "tun.4@1.2.3.0", "tun.4@1.2.3.0", },
		{ LN, "tcp.6@1.2.3.0", "tcp.6@1.2.3.0", },
		{ LN, "udp.17@1.2.3.0", "udp.17@1.2.3.0", },
		{ LN, "esp.50@1.2.3.0", "esp.50@1.2.3.0", },
		{ LN, "ah.51@1.2.3.0", "ah.51@1.2.3.0", },
		{ LN, "comp.108@1.2.3.0", "comp.108@1.2.3.0", },
		/* number conversion */
		{ LN, "tun20@1.2.3.4", "tun.14@1.2.3.4", },
		{ LN, "esp257@1.2.3.0", "esp.101@1.2.3.0", },
		{ LN, "ah0x20@1.2.3.4", "ah.20@1.2.3.4", },
		{ LN, "comp20@1.2.3.4", "comp.14@1.2.3.4", },
		{ LN, "esp257@::1", "esp:101@::1", },
		{ LN, "esp257@0bc:12de::1", "esp:101@bc:12de::1", },
		{ LN, "esp78@1049:1::8007:2040", "esp:4e@1049:1::8007:2040", },
		{ LN, "esp0x78@1049:1::8007:2040", "esp:78@1049:1::8007:2040", },
		{ LN, "ah78@1049:1::8007:2040", "ah:4e@1049:1::8007:2040", },
		{ LN, "ah0x78@1049:1::8007:2040", "ah:78@1049:1::8007:2040", },
		{ LN, "tun78@1049:1::8007:2040", "tun:4e@1049:1::8007:2040", },
		{ LN, "tun0x78@1049:1::8007:2040", "tun:78@1049:1::8007:2040", },
		{ LN, "duk99@3ffe:370:400:ff::9001:3001", NULL, },
		{ LN, "esp78x@1049:1::8007:2040", NULL, },
		{ LN, "esp0x78@1049:1:0xfff::8007:2040", NULL, },
		{ LN, "es78@1049:1::8007:2040", NULL, },
		{ LN, "", NULL, },
		{ LN, "_", NULL, },
		{ LN, "ah2.2", NULL, },
		{ LN, "goo2@1.2.3.4", NULL, },
		{ LN, "esp9@1.2.3.4", "esp.9@1.2.3.4", },
		{ LN, "espp9@1.2.3.4", NULL, },
		{ LN, "es9@1.2.3.4", NULL, },
		{ LN, "ah@1.2.3.4", NULL, },
		{ LN, "esp7x7@1.2.3.4", NULL, },
		{ LN, "esp77@1.0x02.0003.4", "esp.4d@1.2.3.4", },
		{ LN, "esp77@1.0x0g.3.4", NULL, },
		{ LN, PASSTHROUGHNAME, PASSTHROUGH4NAME, },
		{ LN, PASSTHROUGH6NAME, PASSTHROUGH6NAME, },

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

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

		/* convert it *to* internal format */
		ip_said sa;
		diag_t d = ttosaid(shunk1(t->in), &sa);
		if (d != NULL) {
			if (t->out != NULL) {
				DIAG_FAIL(&d, "ttosaid(%s) unexpectedly failed: ", t->in);
			} else {
				/* all is good */
				pfree_diag(&d);
				continue;
			}
		} else if (t->out == NULL) {
			FAIL("ttosa(%s) unexpectedly succeeded", t->in);
		}

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

void ip_said_check(void)
{
	check__ttosaid__str_said();
}