OpenVPN
test_networking.c
Go to the documentation of this file.
1#include "config.h"
2#include "syshead.h"
3#include "networking.h"
4
5#include <setjmp.h>
6#include <cmocka.h>
7
8static char *iface = "ovpn-dummy0";
9
10static int
12{
13 printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");
14
15 return net_iface_up(NULL, iface, up);
16}
17
18static int
19net__iface_new(const char *name, const char *type)
20{
21 return net_iface_new(NULL, name, type, NULL);
22}
23
24static int
25net__iface_type(const char *name, const char *type)
26{
27 char ret_type[IFACE_TYPE_LEN_MAX];
28 int ret = net_iface_type(NULL, name, ret_type);
29 if (ret == 0)
30 {
31 assert_string_equal(type, ret_type);
32 }
33
34 return ret;
35}
36
37static int
38net__iface_del(const char *name)
39{
40 return net_iface_del(NULL, name);
41}
42
43static int
45{
46 printf("CMD: ip link set %s mtu %d\n", iface, mtu);
47
48 return net_iface_mtu_set(NULL, iface, mtu);
49}
50
51static int
52net__addr_v4_add(const char *addr_str, int prefixlen)
53{
54 in_addr_t addr;
55 int ret;
56
57 ret = inet_pton(AF_INET, addr_str, &addr);
58 if (ret != 1)
59 {
60 return -1;
61 }
62
63 addr = ntohl(addr);
64
65 printf("CMD: ip addr add %s/%d dev %s broadcast +\n", addr_str, prefixlen, iface);
66
67 return net_addr_v4_add(NULL, iface, &addr, prefixlen);
68}
69
70static int
71net__addr_v6_add(const char *addr_str, int prefixlen)
72{
73 struct in6_addr addr;
74 int ret;
75
76 ret = inet_pton(AF_INET6, addr_str, &addr);
77 if (ret != 1)
78 {
79 return -1;
80 }
81
82 printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
83
84 return net_addr_v6_add(NULL, iface, &addr, prefixlen);
85}
86
87static int
88net__route_v4_add(const char *dst_str, int prefixlen, int metric)
89{
90 in_addr_t dst;
91 int ret;
92
93 if (!dst_str)
94 {
95 return -1;
96 }
97
98 ret = inet_pton(AF_INET, dst_str, &dst);
99 if (ret != 1)
100 {
101 return -1;
102 }
103
104 dst = ntohl(dst);
105
106 printf("CMD: ip route add %s/%d dev %s", dst_str, prefixlen, iface);
107 if (metric > 0)
108 {
109 printf(" metric %d", metric);
110 }
111 printf("\n");
112
113 return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
114}
115
116static int
117net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
118{
119 in_addr_t dst, gw;
120 int ret;
121
122 if (!dst_str || !gw_str)
123 {
124 return -1;
125 }
126
127 ret = inet_pton(AF_INET, dst_str, &dst);
128 if (ret != 1)
129 {
130 return -1;
131 }
132
133 ret = inet_pton(AF_INET, gw_str, &gw);
134 if (ret != 1)
135 {
136 return -1;
137 }
138
139 dst = ntohl(dst);
140 gw = ntohl(gw);
141
142 printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface, gw_str);
143 if (metric > 0)
144 {
145 printf(" metric %d", metric);
146 }
147 printf("\n");
148
149 return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
150}
151
152static int
153net__route_v6_add(const char *dst_str, int prefixlen, int metric)
154{
155 struct in6_addr dst;
156 int ret;
157
158 if (!dst_str)
159 {
160 return -1;
161 }
162
163 ret = inet_pton(AF_INET6, dst_str, &dst);
164 if (ret != 1)
165 {
166 return -1;
167 }
168
169 printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface);
170 if (metric > 0)
171 {
172 printf(" metric %d", metric);
173 }
174 printf("\n");
175
176 return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
177}
178
179static int
180net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
181{
182 struct in6_addr dst, gw;
183 int ret;
184
185 if (!dst_str || !gw_str)
186 {
187 return -1;
188 }
189
190 ret = inet_pton(AF_INET6, dst_str, &dst);
191 if (ret != 1)
192 {
193 return -1;
194 }
195
196 ret = inet_pton(AF_INET6, gw_str, &gw);
197 if (ret != 1)
198 {
199 return -1;
200 }
201
202 printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen, iface, gw_str);
203 if (metric > 0)
204 {
205 printf(" metric %d", metric);
206 }
207 printf("\n");
208
209 return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
210}
211
212static void
213usage(char *name)
214{
215 printf("Usage: %s <0-8>\n", name);
216}
217
218int
219main(int argc, char *argv[])
220{
221 int test;
222
223 if (argc < 2)
224 {
225 usage(argv[0]);
226 return -1;
227 }
228
229 /* the t_net script can use this command to perform a dry-run test */
230 if (strcmp(argv[1], "test") == 0)
231 {
232 return 0;
233 }
234
235 if (argc > 3)
236 {
237 iface = argv[2];
238 }
239
240 test = atoi(argv[1]);
241 switch (test)
242 {
243 case 0:
244 return net__iface_up(true);
245
246 case 1:
247 return net__iface_mtu_set(1281);
248
249 case 2:
250 return net__addr_v4_add("10.255.255.1", 24);
251
252 case 3:
253 return net__addr_v6_add("2001::1", 64);
254
255 case 4:
256 return net__route_v4_add("11.11.11.0", 24, 0);
257
258 case 5:
259 return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0);
260
261 case 6:
262 return net__route_v6_add("2001:babe:cafe:babe::", 64, 600);
263
264 case 7:
265 return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600);
266
267 /* following tests are standalone and do not print any CMD= */
268 case 8:
269 assert_int_equal(net__iface_new("dummy0815", "dummy"), 0);
270 assert_int_equal(net__iface_type("dummy0815", "dummy"), 0);
271 assert_int_equal(net__iface_del("dummy0815"), 0);
272 assert_int_equal(net__iface_type("dummy0815", NULL), -ENODEV);
273 return 0;
274
275 default:
276 printf("invalid test: %d\n", test);
277 break;
278 }
279
280 usage(argv[0]);
281 return -1;
282}
#define IFACE_TYPE_LEN_MAX
Definition networking.h:25
int main(void)
void usage(void)
Definition options.c:4863
Definition argv.h:35
static int net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
static int net__addr_v6_add(const char *addr_str, int prefixlen)
static int net__iface_mtu_set(int mtu)
static int net__route_v4_add(const char *dst_str, int prefixlen, int metric)
static int net__iface_del(const char *name)
static int net__iface_new(const char *name, const char *type)
static int net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
static int net__route_v6_add(const char *dst_str, int prefixlen, int metric)
static int net__addr_v4_add(const char *addr_str, int prefixlen)
static int net__iface_type(const char *name, const char *type)
static char * iface
static int net__iface_up(bool up)