blob: a79bc7765101f8ab98e1dec42b9bc02054d1c269 [file] [log] [blame]
William A. Kennington IIIfd862be2022-10-09 18:40:55 -07001#include "system_queries.hpp"
2
3#include <linux/rtnetlink.h>
4#include <net/if.h>
5
6#include <stdplus/raw.hpp>
7
8#include <gtest/gtest.h>
9
10using std::literals::string_view_literals::operator""sv;
11
12namespace phosphor::network::system
13{
14namespace detail
15{
16
17TEST(ParseInterface, NotLinkType)
18{
19 nlmsghdr hdr{};
20 hdr.nlmsg_type = RTM_NEWADDR;
21
22 EXPECT_THROW(parseInterface(hdr, ""), std::runtime_error);
23}
24
25TEST(ParseInterface, SmallMsg)
26{
27 nlmsghdr hdr{};
28 hdr.nlmsg_type = RTM_NEWLINK;
29 auto data = "1"sv;
30
31 EXPECT_THROW(parseInterface(hdr, data), std::runtime_error);
32}
33
34TEST(ParseInterface, NoAttrs)
35{
36 nlmsghdr hdr{};
37 hdr.nlmsg_type = RTM_NEWLINK;
38 struct
39 {
40 ifinfomsg hdr __attribute__((aligned(NLMSG_ALIGNTO)));
41 } msg;
42 msg.hdr.ifi_index = 1;
43 msg.hdr.ifi_flags = 2;
44 auto data = stdplus::raw::asView<char>(msg);
45
46 auto info = parseInterface(hdr, data);
47 auto expected = InterfaceInfo{.idx = 1, .flags = 2};
48 EXPECT_EQ(info, expected);
49}
50
51TEST(ParseInterface, AllAttrs)
52{
53 nlmsghdr hdr{};
54 hdr.nlmsg_type = RTM_NEWLINK;
55 struct
56 {
57 ifinfomsg hdr __attribute__((aligned(NLMSG_ALIGNTO)));
58 rtattr addr_hdr __attribute__((aligned((RTA_ALIGNTO))));
59 char addr[6]
60 __attribute__((aligned((RTA_ALIGNTO)))) = {0, 1, 2, 3, 4, 5};
61 rtattr name_hdr __attribute__((aligned((RTA_ALIGNTO))));
62 char name[5] __attribute__((aligned((RTA_ALIGNTO)))) = "eth0";
63 rtattr mtu_hdr __attribute__((aligned((RTA_ALIGNTO))));
64 unsigned mtu __attribute__((aligned((RTA_ALIGNTO)))) = 50;
65 } msg;
66 msg.hdr.ifi_index = 1;
67 msg.hdr.ifi_flags = 2;
68 msg.addr_hdr.rta_type = IFLA_ADDRESS;
69 msg.addr_hdr.rta_len = RTA_LENGTH(sizeof(msg.addr));
70 msg.name_hdr.rta_type = IFLA_IFNAME;
71 msg.name_hdr.rta_len = RTA_LENGTH(sizeof(msg.name));
72 msg.mtu_hdr.rta_type = IFLA_MTU;
73 msg.mtu_hdr.rta_len = RTA_LENGTH(sizeof(msg.mtu));
74 auto data = stdplus::raw::asView<char>(msg);
75
76 auto info = parseInterface(hdr, data);
77 auto expected = InterfaceInfo{.idx = 1,
78 .flags = 2,
79 .name = "eth0",
80 .mac = ether_addr{0, 1, 2, 3, 4, 5},
81 .mtu = 50};
82 EXPECT_EQ(info, expected);
83}
84
85TEST(ValidateNewInterface, Loopback)
86{
87 InterfaceInfo info;
88 info.flags = IFF_LOOPBACK | IFF_RUNNING;
89 EXPECT_FALSE(validateNewInterface(info));
90}
91
92TEST(ValidateNewInterface, NoName)
93{
94 EXPECT_THROW(validateNewInterface(InterfaceInfo{}), std::invalid_argument);
95}
96
97TEST(ValidateNewInterface, IgnoredInterface)
98{
99 InterfaceInfo info;
100 setenv("IGNORED_INTERFACES", "ign", true);
101 info.name = "ign";
102 info.flags = IFF_RUNNING;
103 EXPECT_FALSE(validateNewInterface(info));
104}
105
106TEST(ValidateNewInterface, Valid)
107{
108 InterfaceInfo info;
109 info.name = "eth0";
110 info.flags = 0;
111 EXPECT_TRUE(validateNewInterface(info));
112}
113
William A. Kennington III6a923632022-11-06 18:17:33 -0800114TEST(ValidateNewAddr, Filtering)
115{
116 AddressInfo info = {};
117 EXPECT_TRUE(validateNewAddr(info, {}));
118
119 info.ifidx = 2;
120 EXPECT_TRUE(validateNewAddr(info, {}));
121 EXPECT_TRUE(validateNewAddr(info, {.ifidx = 2}));
122 EXPECT_FALSE(validateNewAddr(info, {.ifidx = 3}));
123}
124
William A. Kennington IIIa8426902022-11-07 15:37:41 -0800125TEST(ValidateNewNeigh, Filtering)
126{
127 NeighborInfo info = {};
128 EXPECT_TRUE(validateNewNeigh(info, {}));
129
130 info.ifidx = 2;
131 EXPECT_TRUE(validateNewNeigh(info, {}));
132 EXPECT_TRUE(validateNewNeigh(info, {.ifidx = 2}));
133 EXPECT_FALSE(validateNewNeigh(info, {.ifidx = 3}));
134}
135
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700136} // namespace detail
137} // namespace phosphor::network::system