chiark / gitweb /
fsckd: the error code is actually returned in 'fd'
[elogind.git] / src / test / test-rtnl-manual.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Susant Sahani
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <arpa/inet.h>
23 #include <net/if.h>
24 #include <linux/ip.h>
25 #include <linux/if_tunnel.h>
26 #include <libkmod.h>
27
28 #include "util.h"
29 #include "macro.h"
30 #include "sd-rtnl.h"
31
32 static int load_module(const char *mod_name) {
33         struct kmod_ctx *ctx;
34         struct kmod_list *list = NULL, *l;
35         int r;
36
37         ctx = kmod_new(NULL, NULL);
38         if (!ctx) {
39                 kmod_unref(ctx);
40                 return -ENOMEM;
41         }
42
43         r = kmod_module_new_from_lookup(ctx, mod_name, &list);
44         if (r < 0)
45                 return -1;
46
47         kmod_list_foreach(l, list) {
48                 struct kmod_module *mod = kmod_module_get_module(l);
49
50                 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
51                 if (r >= 0)
52                         r = 0;
53                 else
54                         r = -1;
55
56                 kmod_module_unref(mod);
57         }
58
59         kmod_module_unref_list(list);
60         kmod_unref(ctx);
61
62         return r;
63 }
64
65 static int test_tunnel_configure(sd_rtnl *rtnl) {
66         int r;
67         sd_rtnl_message *m, *n;
68         struct in_addr local, remote;
69
70         /* skip test if module cannot be loaded */
71         r = load_module("ipip");
72         if(r < 0)
73                 return EXIT_TEST_SKIP;
74
75         if(getuid() != 0)
76                 return EXIT_TEST_SKIP;
77
78         /* IPIP tunnel */
79         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
80         assert_se(m);
81
82         assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
83         assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, 1234)>= 0);
84
85         assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
86
87         assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
88
89         inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
90         assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
91
92         inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
93         assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
94
95         assert_se(sd_rtnl_message_close_container(m) >= 0);
96         assert_se(sd_rtnl_message_close_container(m) >= 0);
97
98         assert_se(sd_rtnl_call(rtnl, m, -1, 0) == 1);
99
100         assert_se((m = sd_rtnl_message_unref(m)) == NULL);
101
102         r = load_module("sit");
103         if(r < 0)
104                 return EXIT_TEST_SKIP;
105
106         /* sit */
107         assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
108         assert_se(n);
109
110         assert_se(sd_rtnl_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
111         assert_se(sd_rtnl_message_append_u32(n, IFLA_MTU, 1234)>= 0);
112
113         assert_se(sd_rtnl_message_open_container(n, IFLA_LINKINFO) >= 0);
114
115         assert_se(sd_rtnl_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
116
117         assert_se(sd_rtnl_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
118
119         inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
120         assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
121
122         inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
123         assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
124
125         assert_se(sd_rtnl_message_close_container(n) >= 0);
126         assert_se(sd_rtnl_message_close_container(n) >= 0);
127
128         assert_se(sd_rtnl_call(rtnl, n, -1, 0) == 1);
129
130         assert_se((m = sd_rtnl_message_unref(n)) == NULL);
131
132         return EXIT_SUCCESS;
133 }
134
135 int main(int argc, char *argv[]) {
136         sd_rtnl *rtnl;
137         int r;
138
139         assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
140         assert_se(rtnl);
141
142         r = test_tunnel_configure(rtnl);
143
144         assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
145
146         return r;
147 }