]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-netlink-manual.c
tests: add helper to unify skipping a test and exiting
[thirdparty/systemd.git] / src / test / test-netlink-manual.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <arpa/inet.h>
4 #include <libkmod.h>
5 #include <linux/ip.h>
6 #include <net/if.h>
7 #include <linux/if_tunnel.h>
8
9 #include "sd-netlink.h"
10
11 #include "macro.h"
12 #include "module-util.h"
13 #include "tests.h"
14 #include "util.h"
15
16 static int load_module(const char *mod_name) {
17 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
18 _cleanup_(kmod_module_unref_listp) struct kmod_list *list = NULL;
19 struct kmod_list *l;
20 int r;
21
22 ctx = kmod_new(NULL, NULL);
23 if (!ctx)
24 return log_oom();
25
26 r = kmod_module_new_from_lookup(ctx, mod_name, &list);
27 if (r < 0)
28 return r;
29
30 kmod_list_foreach(l, list) {
31 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
32
33 mod = kmod_module_get_module(l);
34
35 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
36 if (r > 0)
37 r = -EINVAL;
38 }
39
40 return r;
41 }
42
43 static int test_tunnel_configure(sd_netlink *rtnl) {
44 int r;
45 sd_netlink_message *m, *n;
46 struct in_addr local, remote;
47
48 /* skip test if module cannot be loaded */
49 r = load_module("ipip");
50 if (r < 0) {
51 log_info_errno(r, "Skipping tests: failed to load module 'ipip': %m");
52 return EXIT_TEST_SKIP;
53 }
54
55 r = load_module("sit");
56 if (r < 0) {
57 log_info_errno(r, "Skipping tests: failed to load module 'sit': %m");
58 return EXIT_TEST_SKIP;
59 }
60
61 if (getuid() != 0)
62 return log_tests_skipped("not root");
63
64 /* IPIP tunnel */
65 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
66 assert_se(m);
67
68 assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
69 assert_se(sd_netlink_message_append_u32(m, IFLA_MTU, 1234)>= 0);
70
71 assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
72
73 assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
74
75 inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
76 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
77
78 inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
79 assert_se(sd_netlink_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
80
81 assert_se(sd_netlink_message_close_container(m) >= 0);
82 assert_se(sd_netlink_message_close_container(m) >= 0);
83
84 assert_se(sd_netlink_call(rtnl, m, -1, 0) == 1);
85
86 assert_se((m = sd_netlink_message_unref(m)) == NULL);
87
88 /* sit */
89 assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
90 assert_se(n);
91
92 assert_se(sd_netlink_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
93 assert_se(sd_netlink_message_append_u32(n, IFLA_MTU, 1234)>= 0);
94
95 assert_se(sd_netlink_message_open_container(n, IFLA_LINKINFO) >= 0);
96
97 assert_se(sd_netlink_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
98
99 assert_se(sd_netlink_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
100
101 inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
102 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
103
104 inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
105 assert_se(sd_netlink_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
106
107 assert_se(sd_netlink_message_close_container(n) >= 0);
108 assert_se(sd_netlink_message_close_container(n) >= 0);
109
110 assert_se(sd_netlink_call(rtnl, n, -1, 0) == 1);
111
112 assert_se((n = sd_netlink_message_unref(n)) == NULL);
113
114 return EXIT_SUCCESS;
115 }
116
117 int main(int argc, char *argv[]) {
118 sd_netlink *rtnl;
119 int r;
120
121 assert_se(sd_netlink_open(&rtnl) >= 0);
122 assert_se(rtnl);
123
124 r = test_tunnel_configure(rtnl);
125
126 assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
127
128 return r;
129 }