blob: 39f5c39566c961a4cda7a3a12984a813297490b4 [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>
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07004#include <fmt/chrono.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <netinet/in.h>
6
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07007#include <charconv>
William A. Kennington IIId27410f2019-01-30 17:15:43 -08008#include <cstddef>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07009#include <cstring>
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070010#include <stdexcept>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070011#include <string>
12#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +053013#include <xyz/openbmc_project/Common/error.hpp>
14
Lei YU307554e2021-03-18 14:56:50 +080015#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050016#include <gtest/gtest.h>
17
Ratan Gupta594123d2017-05-16 22:48:35 +053018namespace phosphor
19{
20namespace network
21{
22
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070023using namespace std::literals;
Ratan Gupta35297172018-11-28 18:40:16 +053024using InternalFailure =
25 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Ratan Gupta594123d2017-05-16 22:48:35 +053026class TestUtil : public testing::Test
27{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 public:
29 TestUtil()
30 {
31 // Empty
32 }
Ratan Gupta594123d2017-05-16 22:48:35 +053033};
34
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080035TEST_F(TestUtil, AddrFromBuf)
36{
37 std::string tooSmall(1, 'a');
38 std::string tooLarge(24, 'a');
39
40 struct in_addr ip1;
41 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
42 std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1));
43 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
44 EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1)));
45 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
46 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070047 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080048
49 struct in6_addr ip2;
50 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
51 std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2));
52 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
53 EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2)));
54 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
55 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
William A. Kennington III97b5dc62022-10-07 14:01:29 -070056 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080057}
58
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060059TEST_F(TestUtil, IpValidation)
60{
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070061 EXPECT_TRUE(isValidIP(AF_INET, "0.0.0.0"));
62 EXPECT_TRUE(isValidIP("0.0.0.0"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060063
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070064 EXPECT_TRUE(isValidIP(AF_INET, "9.3.185.83"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060065
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070066 EXPECT_FALSE(isValidIP(AF_INET, "9.3.185.a"));
67 EXPECT_FALSE(isValidIP("9.3.185.a"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060068
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070069 EXPECT_FALSE(isValidIP(AF_INET, "9.3.a.83"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060070
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070071 EXPECT_FALSE(isValidIP(AF_INET, "x.x.x.x"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060072
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070073 EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:0:0:0"));
74 EXPECT_TRUE(isValidIP("0:0:0:0: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_INET6, "1:0:0:0:0:0:0:8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060077
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070078 EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060079
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070080 EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060081
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070082 EXPECT_TRUE(isValidIP(AF_INET6, "::ffff:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060083
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070084 EXPECT_TRUE(isValidIP(AF_INET6, "a:0:0:0:0:FFFF:204.152.189.116"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060085
William A. Kennington IIIff12acb2022-10-07 19:06:56 -070086 EXPECT_TRUE(isValidIP(AF_INET6, "1::8"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060087}
88
89TEST_F(TestUtil, PrefixValidation)
90{
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070091 EXPECT_TRUE(isValidPrefix(AF_INET, 0));
92 EXPECT_TRUE(isValidPrefix(AF_INET, 1));
93 EXPECT_TRUE(isValidPrefix(AF_INET, 32));
94 EXPECT_FALSE(isValidPrefix(AF_INET, 33));
95 EXPECT_FALSE(isValidPrefix(AF_INET, 64));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060096
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -070097 EXPECT_TRUE(isValidPrefix(AF_INET6, 0));
98 EXPECT_TRUE(isValidPrefix(AF_INET6, 1));
99 EXPECT_TRUE(isValidPrefix(AF_INET6, 53));
100 EXPECT_TRUE(isValidPrefix(AF_INET6, 64));
101 EXPECT_TRUE(isValidPrefix(AF_INET6, 128));
102 EXPECT_FALSE(isValidPrefix(AF_INET6, 129));
103 EXPECT_FALSE(isValidPrefix(AF_INET6, 177));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600104
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700105 EXPECT_THROW(isValidPrefix(AF_UNSPEC, 1), std::invalid_argument);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600106}
107
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700108TEST_F(TestUtil, InterfaceToUbootEthAddr)
109{
110 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et"));
111 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth"));
112 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0"));
113 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0"));
114 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h"));
115 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0"));
116 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1"));
117 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5"));
118 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28"));
119}
120
William A. Kennington III1137a972019-04-20 20:49:58 -0700121namespace mac_address
122{
123
124TEST(MacFromString, Bad)
125{
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600126 EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::invalid_argument);
127 EXPECT_THROW(fromString("00:00:00:00:00"), std::invalid_argument);
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700128 EXPECT_THROW(fromString("00:00:00:00:00:"), std::invalid_argument);
129 EXPECT_THROW(fromString("00:00:00:00::00"), std::invalid_argument);
130 EXPECT_THROW(fromString(":00:00:00:00:00"), std::invalid_argument);
131 EXPECT_THROW(fromString("00::00:00:00:00"), std::invalid_argument);
132 EXPECT_THROW(fromString(":::::"), std::invalid_argument);
133 EXPECT_THROW(fromString("00:0:0:0:0"), std::invalid_argument);
134 EXPECT_THROW(fromString("00:00:00:00:00:00:00"), std::invalid_argument);
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600135 EXPECT_THROW(fromString(""), std::invalid_argument);
Potin Laida0b1d42021-12-26 20:08:20 +0800136 EXPECT_THROW(fromString("123456789XYZ"), std::invalid_argument);
137 EXPECT_THROW(fromString("123456789AB"), std::invalid_argument);
138 EXPECT_THROW(fromString("123456789ABCD"), std::invalid_argument);
William A. Kennington III1137a972019-04-20 20:49:58 -0700139}
140
141TEST(MacFromString, Valid)
142{
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700143 EXPECT_EQ((ether_addr{}), fromString("00:00:00:00:00:00"));
144 EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
145 fromString("FF:EE:DD:cc:bb:aa"));
146 EXPECT_EQ((ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}),
147 fromString("0:1:2:3:4:5"));
148 EXPECT_EQ((ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}),
149 fromString("0123456789AB"));
150 EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}),
151 fromString("FFEEDDccbbaa"));
William A. Kennington III1137a972019-04-20 20:49:58 -0700152}
153
154TEST(MacIsEmpty, True)
155{
156 EXPECT_TRUE(isEmpty({}));
157}
158
159TEST(MacIsEmpty, False)
160{
161 EXPECT_FALSE(isEmpty(fromString("01:00:00:00:00:00")));
162 EXPECT_FALSE(isEmpty(fromString("00:00:00:10:00:00")));
163 EXPECT_FALSE(isEmpty(fromString("00:00:00:00:00:01")));
164}
165
166TEST(MacIsMulticast, True)
167{
168 EXPECT_TRUE(isMulticast(fromString("ff:ff:ff:ff:ff:ff")));
169 EXPECT_TRUE(isMulticast(fromString("01:00:00:00:00:00")));
170}
171
172TEST(MacIsMulticast, False)
173{
174 EXPECT_FALSE(isMulticast(fromString("00:11:22:33:44:55")));
175 EXPECT_FALSE(isMulticast(fromString("FE:11:22:33:44:55")));
176}
177
178TEST(MacIsUnicast, True)
179{
180 EXPECT_TRUE(isUnicast(fromString("00:11:22:33:44:55")));
181 EXPECT_TRUE(isUnicast(fromString("FE:11:22:33:44:55")));
182}
183
184TEST(MacIsUnicast, False)
185{
186 EXPECT_FALSE(isUnicast(fromString("00:00:00:00:00:00")));
187 EXPECT_FALSE(isUnicast(fromString("01:00:00:00:00:00")));
188 EXPECT_FALSE(isUnicast(fromString("ff:ff:ff:ff:ff:ff")));
189}
190
Lei YU307554e2021-03-18 14:56:50 +0800191TEST(IgnoredInterfaces, Empty)
192{
193 auto ret = internal::parseInterfaces({});
194 EXPECT_TRUE(ret.empty());
195
196 ret = internal::parseInterfaces(" , ,, ");
197 EXPECT_TRUE(ret.empty());
198}
199
200TEST(IgnoredInterfaces, NotEmpty)
201{
202 using ::testing::ContainerEq;
William A. Kennington III95530ec2022-08-19 01:44:39 -0700203 std::unordered_set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +0800204 auto ret = internal::parseInterfaces("eth0");
205 EXPECT_THAT(ret, ContainerEq(expected));
206
207 expected = {"eth0", "eth1", "bond1", "usb0"};
208 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,");
209 EXPECT_THAT(ret, ContainerEq(expected));
210}
211
William A. Kennington III1137a972019-04-20 20:49:58 -0700212} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213} // namespace network
214} // namespace phosphor