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