blob: 9747cf913776d4ce70b8f824af569da59d9ebe33 [file] [log] [blame]
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -07001#include "ipaddress.hpp"
2#include "util.hpp"
3
4#include <arpa/inet.h>
5#include <linux/netlink.h>
6#include <linux/rtnetlink.h>
7
8#include <cstring>
9#include <stdexcept>
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -070010#include <string>
11#include <vector>
12
13#include <gtest/gtest.h>
14
15namespace phosphor
16{
17namespace network
18{
19namespace detail
20{
21
22TEST(ParseAddress, NotAddressType)
23{
24 nlmsghdr hdr{};
25 hdr.nlmsg_type = RTM_NEWLINK;
26 AddressFilter filter;
27
28 std::vector<AddressInfo> addresses;
29 EXPECT_THROW(parseAddress(filter, hdr, "", addresses), std::runtime_error);
30 EXPECT_EQ(0, addresses.size());
31}
32
33TEST(ParseAddress, SmallMsg)
34{
35 nlmsghdr hdr{};
36 hdr.nlmsg_type = RTM_NEWADDR;
37 std::string data = "1";
38 AddressFilter filter;
39
40 std::vector<AddressInfo> addresses;
41 EXPECT_THROW(parseAddress(filter, hdr, data, addresses),
42 std::runtime_error);
43 EXPECT_EQ(0, addresses.size());
44}
45
46TEST(ParseAddress, NoAttrs)
47{
48 nlmsghdr hdr{};
49 hdr.nlmsg_type = RTM_NEWADDR;
50 ifaddrmsg msg{};
51 msg.ifa_family = AF_INET;
52 msg.ifa_prefixlen = 24;
53 msg.ifa_index = 1;
54 msg.ifa_scope = RT_SCOPE_UNIVERSE;
55 std::string data;
56 data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
57 AddressFilter filter;
58
59 std::vector<AddressInfo> addresses;
60 EXPECT_THROW(parseAddress(filter, hdr, data, addresses),
61 std::runtime_error);
62 EXPECT_EQ(0, addresses.size());
63}
64
65TEST(ParseAddress, NoAddress)
66{
67 nlmsghdr hdr{};
68 hdr.nlmsg_type = RTM_NEWADDR;
69 ifaddrmsg msg{};
70 msg.ifa_family = AF_INET;
71 msg.ifa_prefixlen = 24;
72 msg.ifa_index = 1;
73 msg.ifa_scope = RT_SCOPE_UNIVERSE;
74 in_addr addr{};
75 rtattr local{};
76 constexpr auto len = RTA_LENGTH(sizeof(addr));
77 local.rta_len = len;
78 local.rta_type = IFA_LOCAL;
79 char localbuf[RTA_ALIGN(len)];
80 std::memset(localbuf, '\0', sizeof(localbuf));
81 std::memcpy(localbuf, &local, sizeof(local));
82 std::memcpy(RTA_DATA(localbuf), &addr, sizeof(addr));
83 std::string data;
84 data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
85 data.append(reinterpret_cast<char*>(&localbuf), sizeof(localbuf));
86 AddressFilter filter;
87
88 std::vector<AddressInfo> addresses;
89 EXPECT_THROW(parseAddress(filter, hdr, data, addresses),
90 std::runtime_error);
91 EXPECT_EQ(0, addresses.size());
92}
93
94TEST(ParseAddress, FilterInterface)
95{
96 nlmsghdr hdr{};
97 hdr.nlmsg_type = RTM_NEWADDR;
98 ifaddrmsg msg{};
99 msg.ifa_family = AF_INET;
100 msg.ifa_prefixlen = 24;
101 msg.ifa_index = 2;
102 msg.ifa_scope = RT_SCOPE_UNIVERSE;
103 in_addr addr;
104 ASSERT_EQ(1, inet_pton(msg.ifa_family, "192.168.10.1", &addr));
105 rtattr address{};
106 constexpr auto len = RTA_LENGTH(sizeof(addr));
107 address.rta_len = len;
108 address.rta_type = IFA_ADDRESS;
109 char addressbuf[RTA_ALIGN(len)];
110 std::memset(addressbuf, '\0', sizeof(addressbuf));
111 std::memcpy(addressbuf, &address, sizeof(address));
112 std::memcpy(RTA_DATA(addressbuf), &addr, sizeof(addr));
113 std::string data;
114 data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
115 data.append(reinterpret_cast<char*>(&addressbuf), sizeof(addressbuf));
116 AddressFilter filter;
117
118 std::vector<AddressInfo> addresses;
119 filter.interface = 1;
120 parseAddress(filter, hdr, data, addresses);
121 EXPECT_EQ(0, addresses.size());
122 filter.interface = 2;
123 parseAddress(filter, hdr, data, addresses);
124 EXPECT_EQ(1, addresses.size());
125 EXPECT_EQ(msg.ifa_index, addresses[0].interface);
126 EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
127 EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700128 EXPECT_EQ(addr, addresses[0].address);
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -0700129}
130
131TEST(ParseNeighbor, FilterScope)
132{
133 nlmsghdr hdr{};
134 hdr.nlmsg_type = RTM_NEWADDR;
135 ifaddrmsg msg{};
136 msg.ifa_family = AF_INET;
137 msg.ifa_prefixlen = 24;
138 msg.ifa_index = 2;
139 msg.ifa_scope = RT_SCOPE_SITE;
140 in_addr addr;
141 ASSERT_EQ(1, inet_pton(msg.ifa_family, "192.168.10.1", &addr));
142 rtattr address{};
143 constexpr auto len = RTA_LENGTH(sizeof(addr));
144 address.rta_len = len;
145 address.rta_type = IFA_ADDRESS;
146 char addressbuf[RTA_ALIGN(len)];
147 std::memset(addressbuf, '\0', sizeof(addressbuf));
148 std::memcpy(addressbuf, &address, sizeof(address));
149 std::memcpy(RTA_DATA(addressbuf), &addr, sizeof(addr));
150 std::string data;
151 data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
152 data.append(reinterpret_cast<char*>(&addressbuf), sizeof(addressbuf));
153 AddressFilter filter;
154
155 std::vector<AddressInfo> addresses;
156 filter.scope = RT_SCOPE_UNIVERSE;
157 parseAddress(filter, hdr, data, addresses);
158 EXPECT_EQ(0, addresses.size());
159 filter.scope = RT_SCOPE_SITE;
160 parseAddress(filter, hdr, data, addresses);
161 EXPECT_EQ(1, addresses.size());
162 EXPECT_EQ(msg.ifa_index, addresses[0].interface);
163 EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
164 EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700165 EXPECT_EQ(addr, addresses[0].address);
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -0700166}
167
168TEST(ParseNeighbor, NoFilter)
169{
170 nlmsghdr hdr{};
171 hdr.nlmsg_type = RTM_NEWADDR;
172 ifaddrmsg msg{};
173 msg.ifa_family = AF_INET6;
174 msg.ifa_prefixlen = 24;
175 msg.ifa_index = 1;
176 msg.ifa_scope = RT_SCOPE_UNIVERSE;
177 in6_addr addr;
178 ASSERT_EQ(1, inet_pton(msg.ifa_family, "fd00::2", &addr));
179 rtattr address{};
180 constexpr auto len = RTA_LENGTH(sizeof(addr));
181 address.rta_len = len;
182 address.rta_type = IFA_ADDRESS;
183 char addressbuf[RTA_ALIGN(len)];
184 std::memset(addressbuf, '\0', sizeof(addressbuf));
185 std::memcpy(addressbuf, &address, sizeof(address));
186 std::memcpy(RTA_DATA(addressbuf), &addr, sizeof(addr));
187 std::string data;
188 data.append(reinterpret_cast<char*>(&msg), sizeof(msg));
189 data.append(reinterpret_cast<char*>(&addressbuf), sizeof(addressbuf));
190 AddressFilter filter;
191
192 std::vector<AddressInfo> addresses;
193 parseAddress(filter, hdr, data, addresses);
194 EXPECT_EQ(1, addresses.size());
195 EXPECT_EQ(msg.ifa_index, addresses[0].interface);
196 EXPECT_EQ(msg.ifa_scope, addresses[0].scope);
197 EXPECT_EQ(msg.ifa_prefixlen, addresses[0].prefix);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700198 EXPECT_EQ(addr, addresses[0].address);
William A. Kennington IIIc2e5e0e2019-04-22 01:26:06 -0700199}
200
201} // namespace detail
202} // namespace network
203} // namespace phosphor