]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-rtnl/test-rtnl.c
udev: link-config - add proper parsing
[thirdparty/systemd.git] / src / libsystemd-rtnl / test-rtnl.c
CommitLineData
65f568bb
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
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
24#include "util.h"
25#include "macro.h"
26#include "sd-rtnl.h"
27#include "socket-util.h"
28
29static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
30 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
31 __u16 type;
32 const char *mac = "98:fe:94:3f:c6:18", *name = "test";
33 unsigned int mtu = 1450;
34 void *data;
35
36 /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
37 assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &message) >= 0);
38 assert(sd_rtnl_message_append(message, IFLA_IFNAME, name) >= 0);
39 assert(sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
40 assert(sd_rtnl_message_append(message, IFLA_MTU, &mtu) >= 0);
41
42 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
43 assert(type == IFLA_IFNAME);
44 assert(streq(name, (char *) data));
45
46 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
47 assert(type == IFLA_ADDRESS);
48 assert(streq(mac, ether_ntoa(data)));
49
50 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
51 assert(type == IFLA_MTU);
52 assert(mtu == *(unsigned int *) data);
53
65f568bb
TG
54 assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 2 * USEC_PER_SEC, NULL) == 0);
55}
56
57int main(void) {
58 sd_rtnl *rtnl;
59 sd_rtnl_message *m;
60 sd_rtnl_message *r;
61 void *data;
62 int if_loopback;
63 __u16 type;
64 unsigned int mtu = 0;
65 unsigned int *mtu_reply;
66
67 assert(sd_rtnl_open(0, &rtnl) >= 0);
68 assert(rtnl);
69
70 if_loopback = (int) if_nametoindex("lo");
71 assert(if_loopback > 0);
72
73 test_link_configure(rtnl, if_loopback);
74
75 assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
76 assert(m);
77
78 assert(sd_rtnl_message_get_type(m, &type) >= 0);
79 assert(type == RTM_GETLINK);
80
81 assert(sd_rtnl_message_read(m, &type, &data) == 0);
82
83 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 100000000, &r) >= 0);
84 assert(sd_rtnl_message_get_type(r, &type) >= 0);
85 assert(type == RTM_NEWLINK);
86
87 assert(sd_rtnl_message_read(m, &type, data) == 0);
88 assert((r = sd_rtnl_message_unref(r)) == NULL);
89
90 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) == -EPERM);
91 assert((m = sd_rtnl_message_unref(m)) == NULL);
92 assert((r = sd_rtnl_message_unref(r)) == NULL);
93
94 assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
95 assert(m);
96
97 assert(sd_rtnl_message_append(m, IFLA_MTU, &mtu) >= 0);
98 assert(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1);
99
100 assert(type == IFLA_MTU);
101 assert(*mtu_reply == 0);
102
103 assert(sd_rtnl_message_read(m, &type, data) == 0);
104
105 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) >= 0);
106 while (sd_rtnl_message_read(r, &type, &data)) {
107 switch (type) {
eab97a46
TG
108// case IFLA_MTU:
109// assert(*(unsigned int *) data == 65536);
110// break;;
111// case IFLA_QDISC:
112// assert(streq((char *) data, "noqueue"));
113// break;;
65f568bb
TG
114 case IFLA_IFNAME:
115 assert(streq((char *) data, "lo"));
116 break;;
65f568bb
TG
117 }
118 }
119
120 assert((m = sd_rtnl_message_unref(m)) == NULL);
121 assert((r = sd_rtnl_message_unref(r)) == NULL);
122 assert((rtnl = sd_rtnl_unref(rtnl)) == NULL);
123
124 return EXIT_SUCCESS;
125}