blob: cfbab77383ccb35f63b684ffcf3fdcec296c1c6c [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include "util.hpp"
2
William A. Kennington III5058f572019-01-30 17:18:14 -08003#include <arpa/inet.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include <netinet/in.h>
5
William A. Kennington IIId27410f2019-01-30 17:15:43 -08006#include <cstddef>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07007#include <cstring>
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -07008#include <stdexcept>
William A. Kennington III12beaad2020-06-13 19:30:41 -07009#include <stdplus/raw.hpp>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070010#include <string>
11#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +053012#include <xyz/openbmc_project/Common/error.hpp>
13
Lei YU307554e2021-03-18 14:56:50 +080014#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050015#include <gtest/gtest.h>
16
Ratan Gupta594123d2017-05-16 22:48:35 +053017namespace phosphor
18{
19namespace network
20{
21
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070022using namespace std::literals;
Ratan Gupta35297172018-11-28 18:40:16 +053023using InternalFailure =
24 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Ratan Gupta594123d2017-05-16 22:48:35 +053025class TestUtil : public testing::Test
26{
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 public:
28 TestUtil()
29 {
30 // Empty
31 }
Ratan Gupta594123d2017-05-16 22:48:35 +053032};
33
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080034TEST_F(TestUtil, AddrFromBuf)
35{
36 std::string tooSmall(1, 'a');
37 std::string tooLarge(24, 'a');
38
39 struct in_addr ip1;
40 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
41 std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1));
42 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
43 EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1)));
44 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
45 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070046 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080047
48 struct in6_addr ip2;
49 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
50 std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2));
51 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
52 EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2)));
53 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
54 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070055 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080056}
57
William A. Kennington III5058f572019-01-30 17:18:14 -080058TEST_F(TestUtil, IpToString)
59{
60 struct in_addr ip1;
61 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
Alexander Filippov983da552021-02-08 15:26:54 +030062 EXPECT_EQ("192.168.10.1", toString(ip1));
William A. Kennington III5058f572019-01-30 17:18:14 -080063 EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
64
65 struct in6_addr ip2;
66 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
Alexander Filippov983da552021-02-08 15:26:54 +030067 EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(ip2));
William A. Kennington III5058f572019-01-30 17:18:14 -080068 EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
William A. Kennington III5058f572019-01-30 17:18:14 -080069}
70
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060071TEST_F(TestUtil, IpValidation)
72{
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070073 EXPECT_TRUE(isValidIP(AF_INET, "0.0.0.0"));
74 EXPECT_TRUE(isValidIP("0.0.0.0"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060075
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070076 EXPECT_TRUE(isValidIP(AF_INET, "9.3.185.83"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060077
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070078 EXPECT_FALSE(isValidIP(AF_INET, "9.3.185.a"));
79 EXPECT_FALSE(isValidIP("9.3.185.a"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060080
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070081 EXPECT_FALSE(isValidIP(AF_INET, "9.3.a.83"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060082
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070083 EXPECT_FALSE(isValidIP(AF_INET, "x.x.x.x"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060084
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070085 EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:0:0:0"));
86 EXPECT_TRUE(isValidIP("0:0:0:0:0:0:0:0"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060087
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070088 EXPECT_TRUE(isValidIP(AF_INET6, "1:0:0:0:0:0:0:8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060089
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070090 EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060091
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070092 EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060093
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070094 EXPECT_TRUE(isValidIP(AF_INET6, "::ffff:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060095
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070096 EXPECT_TRUE(isValidIP(AF_INET6, "a:0:0:0:0:FFFF:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060097
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070098 EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060099}
100
101TEST_F(TestUtil, PrefixValidation)
102{
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700103 EXPECT_TRUE(isValidPrefix(AF_INET, 0));
104 EXPECT_TRUE(isValidPrefix(AF_INET, 1));
105 EXPECT_TRUE(isValidPrefix(AF_INET, 32));
106 EXPECT_FALSE(isValidPrefix(AF_INET, 33));
107 EXPECT_FALSE(isValidPrefix(AF_INET, 64));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600108
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700109 EXPECT_TRUE(isValidPrefix(AF_INET6, 0));
110 EXPECT_TRUE(isValidPrefix(AF_INET6, 1));
111 EXPECT_TRUE(isValidPrefix(AF_INET6, 53));
112 EXPECT_TRUE(isValidPrefix(AF_INET6, 64));
113 EXPECT_TRUE(isValidPrefix(AF_INET6, 128));
114 EXPECT_FALSE(isValidPrefix(AF_INET6, 129));
115 EXPECT_FALSE(isValidPrefix(AF_INET6, 177));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600116
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700117 EXPECT_THROW(isValidPrefix(AF_UNSPEC, 1), std::invalid_argument);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600118}
119
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700120TEST_F(TestUtil, InterfaceToUbootEthAddr)
121{
122 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et"));
123 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth"));
124 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0"));
125 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0"));
126 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h"));
127 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0"));
128 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1"));
129 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5"));
130 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28"));
131}
132
William A. Kennington III1137a972019-04-20 20:49:58 -0700133namespace mac_address
134{
135
136TEST(MacFromString, Bad)
137{
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600138 EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::invalid_argument);
139 EXPECT_THROW(fromString("00:00:00:00:00"), std::invalid_argument);
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700140 EXPECT_THROW(fromString("00:00:00:00:00:"), std::invalid_argument);
141 EXPECT_THROW(fromString("00:00:00:00::00"), std::invalid_argument);
142 EXPECT_THROW(fromString(":00:00:00:00:00"), std::invalid_argument);
143 EXPECT_THROW(fromString("00::00:00:00:00"), std::invalid_argument);
144 EXPECT_THROW(fromString(":::::"), std::invalid_argument);
145 EXPECT_THROW(fromString("00:0:0:0:0"), std::invalid_argument);
146 EXPECT_THROW(fromString("00:00:00:00:00:00:00"), std::invalid_argument);
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600147 EXPECT_THROW(fromString(""), std::invalid_argument);
Potin Laida0b1d42021-12-26 20:08:20 +0800148 EXPECT_THROW(fromString("123456789XYZ"), std::invalid_argument);
149 EXPECT_THROW(fromString("123456789AB"), std::invalid_argument);
150 EXPECT_THROW(fromString("123456789ABCD"), std::invalid_argument);
William A. Kennington III1137a972019-04-20 20:49:58 -0700151}
152
153TEST(MacFromString, Valid)
154{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700155 EXPECT_TRUE(
156 stdplus::raw::equal(ether_addr{}, fromString("00:00:00:00:00:00")));
157 EXPECT_TRUE(
158 stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
159 fromString("FF:EE:DD:cc:bb:aa")));
160 EXPECT_TRUE(
161 stdplus::raw::equal(ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
162 fromString("0:1:2:3:4:5")));
Potin Laida0b1d42021-12-26 20:08:20 +0800163 EXPECT_TRUE(
164 stdplus::raw::equal(ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab},
165 fromString("0123456789AB")));
166 EXPECT_TRUE(
167 stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
168 fromString("FFEEDDccbbaa")));
William A. Kennington III1137a972019-04-20 20:49:58 -0700169}
170
171TEST(MacToString, Valid)
172{
173 EXPECT_EQ("11:22:33:44:55:66",
174 toString({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
Karthick Sundarrajandbd328d2019-11-20 15:19:08 -0800175 EXPECT_EQ("01:02:03:04:05:67",
176 toString({0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
177 EXPECT_EQ("00:00:00:00:00:00",
178 toString({0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
William A. Kennington III1137a972019-04-20 20:49:58 -0700179}
180
181TEST(MacIsEmpty, True)
182{
183 EXPECT_TRUE(isEmpty({}));
184}
185
186TEST(MacIsEmpty, False)
187{
188 EXPECT_FALSE(isEmpty(fromString("01:00:00:00:00:00")));
189 EXPECT_FALSE(isEmpty(fromString("00:00:00:10:00:00")));
190 EXPECT_FALSE(isEmpty(fromString("00:00:00:00:00:01")));
191}
192
193TEST(MacIsMulticast, True)
194{
195 EXPECT_TRUE(isMulticast(fromString("ff:ff:ff:ff:ff:ff")));
196 EXPECT_TRUE(isMulticast(fromString("01:00:00:00:00:00")));
197}
198
199TEST(MacIsMulticast, False)
200{
201 EXPECT_FALSE(isMulticast(fromString("00:11:22:33:44:55")));
202 EXPECT_FALSE(isMulticast(fromString("FE:11:22:33:44:55")));
203}
204
205TEST(MacIsUnicast, True)
206{
207 EXPECT_TRUE(isUnicast(fromString("00:11:22:33:44:55")));
208 EXPECT_TRUE(isUnicast(fromString("FE:11:22:33:44:55")));
209}
210
211TEST(MacIsUnicast, False)
212{
213 EXPECT_FALSE(isUnicast(fromString("00:00:00:00:00:00")));
214 EXPECT_FALSE(isUnicast(fromString("01:00:00:00:00:00")));
215 EXPECT_FALSE(isUnicast(fromString("ff:ff:ff:ff:ff:ff")));
216}
217
Lei YU307554e2021-03-18 14:56:50 +0800218TEST(IgnoredInterfaces, Empty)
219{
220 auto ret = internal::parseInterfaces({});
221 EXPECT_TRUE(ret.empty());
222
223 ret = internal::parseInterfaces(" , ,, ");
224 EXPECT_TRUE(ret.empty());
225}
226
227TEST(IgnoredInterfaces, NotEmpty)
228{
229 using ::testing::ContainerEq;
William A. Kennington III95530ec2022-08-19 01:44:39 -0700230 std::unordered_set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +0800231 auto ret = internal::parseInterfaces("eth0");
232 EXPECT_THAT(ret, ContainerEq(expected));
233
234 expected = {"eth0", "eth1", "bond1", "usb0"};
235 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,");
236 EXPECT_THAT(ret, ContainerEq(expected));
237}
238
William A. Kennington III1137a972019-04-20 20:49:58 -0700239} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240} // namespace network
241} // namespace phosphor