1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "sd-device.h"
4 
5 #include "arphrd-util.h"
6 #include "ether-addr-util.h"
7 #include "parse-util.h"
8 #include "tests.h"
9 #include "udev-netlink.h"
10 
test_link_info_one(sd_netlink * rtnl,int ifindex)11 static void test_link_info_one(sd_netlink *rtnl, int ifindex) {
12         _cleanup_(link_info_clear) LinkInfo info = LINK_INFO_NULL;
13         _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *dev_with_netlink = NULL;
14         const char *s, *t;
15         unsigned u;
16 
17         log_debug("/* %s(ifindex=%i) */", __func__, ifindex);
18 
19         assert_se(link_info_get(&rtnl, ifindex, &info) >= 0);
20         assert_se(sd_device_new_from_ifindex(&dev, ifindex) >= 0);
21         assert_se(sd_device_new_from_ifindex(&dev_with_netlink, ifindex) >= 0);
22 
23         /* check iftype */
24         log_debug("iftype: %"PRIu16" (%s)", info.iftype, strna(arphrd_to_name(info.iftype)));
25         assert_se(sd_device_get_sysattr_value(dev, "type", &s) >= 0);
26         assert_se(safe_atou(s, &u) >= 0);
27         assert_se(u == info.iftype);
28         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "type", &s) >= 0);
29         assert_se(safe_atou(s, &u) >= 0);
30         assert_se(u == info.iftype);
31 
32         /* check hardware address length */
33         log_debug("hardware address length: %zu", info.hw_addr.length);
34         assert_se(sd_device_get_sysattr_value(dev, "addr_len", &s) >= 0);
35         assert_se(safe_atou(s, &u) >= 0);
36         assert_se(u == info.hw_addr.length);
37         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "addr_len", &s) >= 0);
38         assert_se(safe_atou(s, &u) >= 0);
39         assert_se(u == info.hw_addr.length);
40 
41         /* check hardware address */
42         log_debug("hardware address: %s", HW_ADDR_TO_STR(&info.hw_addr));
43         assert_se(sd_device_get_sysattr_value(dev, "address", &s) >= 0);
44         assert_se(streq(s, HW_ADDR_TO_STR(&info.hw_addr)));
45         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "address", &s) >= 0);
46         assert_se(streq(s, HW_ADDR_TO_STR(&info.hw_addr)));
47 
48         /* check broadcast address */
49         log_debug("broadcast address: %s", HW_ADDR_TO_STR(&info.broadcast));
50         assert_se(sd_device_get_sysattr_value(dev, "broadcast", &s) >= 0);
51         assert_se(streq(s, HW_ADDR_TO_STR(&info.broadcast)));
52         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "broadcast", &s) >= 0);
53         assert_se(streq(s, HW_ADDR_TO_STR(&info.broadcast)));
54 
55         /* check ifname */
56         log_debug("ifname: %s", info.ifname);
57         assert_se(sd_device_get_sysname(dev, &s) >= 0);
58         assert_se(streq(s, info.ifname));
59         assert_se(sd_device_get_sysname(dev_with_netlink, &s) >= 0);
60         assert_se(streq(s, info.ifname));
61 
62         /* check mtu */
63         log_debug("mtu: %"PRIu32, info.mtu);
64         assert_se(sd_device_get_sysattr_value(dev, "mtu", &s) >= 0);
65         assert_se(safe_atou(s, &u) >= 0);
66         assert_se(u == info.mtu);
67         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "mtu", &s) >= 0);
68         assert_se(safe_atou(s, &u) >= 0);
69         assert_se(u == info.mtu);
70 
71         /* check iflink */
72         log_debug("iflink: %"PRIu32, info.iflink);
73         assert_se(sd_device_get_sysattr_value(dev, "iflink", &s) >= 0);
74         assert_se(safe_atou(s, &u) >= 0);
75         assert_se(u == info.iflink);
76         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "iflink", &s) >= 0);
77         assert_se(safe_atou(s, &u) >= 0);
78         assert_se(u == info.iflink);
79 
80         /* check link_mode */
81         log_debug("link_mode: %"PRIu8, info.link_mode);
82         assert_se(sd_device_get_sysattr_value(dev, "link_mode", &s) >= 0);
83         assert_se(safe_atou(s, &u) >= 0);
84         assert_se(u == info.link_mode);
85         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "link_mode", &s) >= 0);
86         assert_se(safe_atou(s, &u) >= 0);
87         assert_se(u == info.link_mode);
88 
89         /* check ifalias */
90         log_debug("ifalias: %s", strna(info.ifalias));
91         assert_se(sd_device_get_sysattr_value(dev, "ifalias", &s) >= 0);
92         assert_se(streq(s, strempty(info.ifalias)));
93         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "ifalias", &s) >= 0);
94         assert_se(streq(s, strempty(info.ifalias)));
95 
96         /* check netdev_group */
97         log_debug("netdev_group: %"PRIu32, info.group);
98         assert_se(sd_device_get_sysattr_value(dev, "netdev_group", &s) >= 0);
99         assert_se(safe_atou(s, &u) >= 0);
100         assert_se(u == info.group);
101         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "netdev_group", &s) >= 0);
102         assert_se(safe_atou(s, &u) >= 0);
103         assert_se(u == info.group);
104 
105         /* check phys_port_id */
106         log_debug("phys_port_id: (%s)",
107                   info.phys_port_id_supported ? "supported" : "unsupported");
108         s = t = NULL;
109         (void) sd_device_get_sysattr_value(dev, "phys_port_id", &s);
110         (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_port_id", &t);
111         assert_se(streq_ptr(s, t));
112 
113         /* check phys_switch_id */
114         log_debug("phys_switch_id: (%s)",
115                   info.phys_switch_id_supported ? "supported" : "unsupported");
116         s = t = NULL;
117         (void) sd_device_get_sysattr_value(dev, "phys_switch_id", &s);
118         (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_switch_id", &t);
119         assert_se(streq_ptr(s, t));
120 
121         /* check phys_port_name */
122         log_debug("phys_port_name: %s (%s)",
123                   strna(info.phys_port_name),
124                   info.phys_port_name_supported ? "supported" : "unsupported");
125         s = t = NULL;
126         (void) sd_device_get_sysattr_value(dev, "phys_port_name", &s);
127         (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_port_name", &t);
128         assert_se(streq_ptr(s, t));
129         if (info.phys_port_name_supported) {
130                 assert_se(streq_ptr(s, info.phys_port_name));
131                 assert_se(streq_ptr(t, info.phys_port_name));
132         }
133 }
134 
TEST(link_info_get)135 TEST(link_info_get) {
136         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
137         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
138 
139         assert_se(sd_netlink_open(&rtnl) >= 0);
140 
141         assert_se(sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0) >= 0);
142         assert_se(sd_netlink_message_request_dump(req, true) >= 0);
143         assert_se(sd_netlink_call(rtnl, req, 0, &reply) >= 0);
144 
145         for (sd_netlink_message *reply_one = reply; reply_one; reply_one = sd_netlink_message_next(reply_one)) {
146                 uint16_t nlmsg_type;
147                 int ifindex;
148 
149                 assert_se(sd_netlink_message_get_type(reply_one, &nlmsg_type) >= 0);
150                 assert_se(nlmsg_type == RTM_NEWLINK);
151                 assert_se(sd_rtnl_message_link_get_ifindex(reply_one, &ifindex) >= 0);
152 
153                 test_link_info_one(rtnl, ifindex);
154         }
155 }
156 
157 DEFINE_TEST_MAIN(LOG_DEBUG);
158