chiark / gitweb /
shared: utf8 - support ucs4 -> utf8
[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 <netinet/ether.h>
23 #include <arpa/inet.h>
24 #include <net/if.h>
25 #include <linux/ip.h>
26 #include <linux/if_tunnel.h>
27 #include <libkmod.h>
28
29 #include "util.h"
30 #include "macro.h"
31 #include "sd-rtnl.h"
32 #include "socket-util.h"
33 #include "rtnl-util.h"
34 #include "event-util.h"
35 #include "rtnl-internal.h"
36
37 static int load_module(const char *mod_name) {
38         struct kmod_ctx *ctx;
39         struct kmod_list *list = NULL, *l;
40         int r;
41
42         ctx = kmod_new(NULL, NULL);
43         if (!ctx) {
44                 kmod_unref(ctx);
45                 return -ENOMEM;
46         }
47
48         r = kmod_module_new_from_lookup(ctx, mod_name, &list);
49         if (r < 0)
50                 return -1;
51
52         kmod_list_foreach(l, list) {
53                 struct kmod_module *mod = kmod_module_get_module(l);
54
55                 r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
56                 if (r >= 0)
57                         r = 0;
58                 else
59                         r = -1;
60
61                 kmod_module_unref(mod);
62         }
63
64         kmod_module_unref_list(list);
65         kmod_unref(ctx);
66
67         return r;
68 }
69
70 static int test_tunnel_configure(sd_rtnl *rtnl) {
71         int r;
72         sd_rtnl_message *m, *n;
73         struct in_addr local, remote;
74
75         /* skip test if module cannot be loaded */
76         r = load_module("ipip");
77         if(r < 0)
78                 return EXIT_TEST_SKIP;
79
80         if(getuid() != 0)
81                 return EXIT_TEST_SKIP;
82
83         /* IPIP tunnel */
84         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
85         assert_se(m);
86
87         assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
88         assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, 1234)>= 0);
89
90         assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
91
92         assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);
93
94         inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
95         assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
96
97         inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
98         assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
99
100         assert_se(sd_rtnl_message_close_container(m) >= 0);
101         assert_se(sd_rtnl_message_close_container(m) >= 0);
102
103         assert_se(sd_rtnl_call(rtnl, m, -1, 0) == 1);
104
105         assert_se((m = sd_rtnl_message_unref(m)) == NULL);
106
107         r = load_module("sit");
108         if(r < 0)
109                 return EXIT_TEST_SKIP;
110
111         /* sit */
112         assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
113         assert_se(n);
114
115         assert_se(sd_rtnl_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
116         assert_se(sd_rtnl_message_append_u32(n, IFLA_MTU, 1234)>= 0);
117
118         assert_se(sd_rtnl_message_open_container(n, IFLA_LINKINFO) >= 0);
119
120         assert_se(sd_rtnl_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);
121
122         assert_se(sd_rtnl_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);
123
124         inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
125         assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);
126
127         inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
128         assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);
129
130         assert_se(sd_rtnl_message_close_container(n) >= 0);
131         assert_se(sd_rtnl_message_close_container(n) >= 0);
132
133         assert_se(sd_rtnl_call(rtnl, n, -1, 0) == 1);
134
135         assert_se((m = sd_rtnl_message_unref(n)) == NULL);
136
137         return EXIT_SUCCESS;
138 }
139
140 int main(int argc, char *argv[]) {
141         sd_rtnl *rtnl;
142         int r;
143
144         assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
145         assert_se(rtnl);
146
147         r = test_tunnel_configure(rtnl);
148
149         assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
150
151         return r;
152 }