blob: 4a50a26b9a46eae35420767c7dbe7db60bfc2bfa [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 III12beaad2020-06-13 19:30:41 -07008#include <stdplus/raw.hpp>
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -07009#include <string>
10#include <string_view>
Ratan Gupta35297172018-11-28 18:40:16 +053011#include <xyz/openbmc_project/Common/error.hpp>
12
Lei YU307554e2021-03-18 14:56:50 +080013#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050014#include <gtest/gtest.h>
15
Ratan Gupta594123d2017-05-16 22:48:35 +053016namespace phosphor
17{
18namespace network
19{
20
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -070021using namespace std::literals;
Ratan Gupta35297172018-11-28 18:40:16 +053022using InternalFailure =
23 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Ratan Gupta594123d2017-05-16 22:48:35 +053024class TestUtil : public testing::Test
25{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
27 TestUtil()
28 {
29 // Empty
30 }
Ratan Gupta594123d2017-05-16 22:48:35 +053031};
32
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080033TEST_F(TestUtil, AddrFromBuf)
34{
35 std::string tooSmall(1, 'a');
36 std::string tooLarge(24, 'a');
37
38 struct in_addr ip1;
39 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
40 std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1));
41 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
42 EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1)));
43 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
44 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
45 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::runtime_error);
46
47 struct in6_addr ip2;
48 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
49 std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2));
50 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
51 EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2)));
52 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
53 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
54 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::runtime_error);
55}
56
William A. Kennington III5058f572019-01-30 17:18:14 -080057TEST_F(TestUtil, IpToString)
58{
59 struct in_addr ip1;
60 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
Alexander Filippov983da552021-02-08 15:26:54 +030061 EXPECT_EQ("192.168.10.1", toString(ip1));
William A. Kennington III5058f572019-01-30 17:18:14 -080062 EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
63
64 struct in6_addr ip2;
65 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
Alexander Filippov983da552021-02-08 15:26:54 +030066 EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(ip2));
William A. Kennington III5058f572019-01-30 17:18:14 -080067 EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
William A. Kennington III5058f572019-01-30 17:18:14 -080068}
69
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060070TEST_F(TestUtil, IpValidation)
71{
72 std::string ipaddress = "0.0.0.0";
73 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
74
75 ipaddress = "9.3.185.83";
76 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
77
78 ipaddress = "9.3.185.a";
79 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
80
81 ipaddress = "9.3.a.83";
82 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
83
84 ipaddress = "x.x.x.x";
85 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
86
87 ipaddress = "0:0:0:0:0:0:0:0";
88 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
89
90 ipaddress = "1:0:0:0:0:0:0:8";
91 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
92
93 ipaddress = "1::8";
94 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
95
96 ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
97 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
98
99 ipaddress = "::ffff:204.152.189.116";
100 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
101
102 ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
103 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
104
105 ipaddress = "1::8";
106 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600107}
108
109TEST_F(TestUtil, PrefixValidation)
110{
111 uint8_t prefixLength = 1;
112 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
113
114 prefixLength = 32;
115 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
116
117 prefixLength = 0;
118 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
119
120 prefixLength = 33;
121 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
122
123 prefixLength = 33;
124 EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
125
126 prefixLength = 65;
127 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600128}
129
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700130TEST_F(TestUtil, InterfaceToUbootEthAddr)
131{
132 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et"));
133 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth"));
134 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0"));
135 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0"));
136 EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h"));
137 EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0"));
138 EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1"));
139 EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5"));
140 EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28"));
141}
142
William A. Kennington III1137a972019-04-20 20:49:58 -0700143namespace mac_address
144{
145
146TEST(MacFromString, Bad)
147{
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600148 EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::invalid_argument);
149 EXPECT_THROW(fromString("00:00:00:00:00"), std::invalid_argument);
150 EXPECT_THROW(fromString(""), std::invalid_argument);
Potin Laida0b1d42021-12-26 20:08:20 +0800151 EXPECT_THROW(fromString("123456789XYZ"), std::invalid_argument);
152 EXPECT_THROW(fromString("123456789AB"), std::invalid_argument);
153 EXPECT_THROW(fromString("123456789ABCD"), std::invalid_argument);
William A. Kennington III1137a972019-04-20 20:49:58 -0700154}
155
156TEST(MacFromString, Valid)
157{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700158 EXPECT_TRUE(
159 stdplus::raw::equal(ether_addr{}, fromString("00:00:00:00:00:00")));
160 EXPECT_TRUE(
161 stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
162 fromString("FF:EE:DD:cc:bb:aa")));
163 EXPECT_TRUE(
164 stdplus::raw::equal(ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05},
165 fromString("0:1:2:3:4:5")));
Potin Laida0b1d42021-12-26 20:08:20 +0800166 EXPECT_TRUE(
167 stdplus::raw::equal(ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab},
168 fromString("0123456789AB")));
169 EXPECT_TRUE(
170 stdplus::raw::equal(ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa},
171 fromString("FFEEDDccbbaa")));
William A. Kennington III1137a972019-04-20 20:49:58 -0700172}
173
174TEST(MacToString, Valid)
175{
176 EXPECT_EQ("11:22:33:44:55:66",
177 toString({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}));
Karthick Sundarrajandbd328d2019-11-20 15:19:08 -0800178 EXPECT_EQ("01:02:03:04:05:67",
179 toString({0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
180 EXPECT_EQ("00:00:00:00:00:00",
181 toString({0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
William A. Kennington III1137a972019-04-20 20:49:58 -0700182}
183
184TEST(MacIsEmpty, True)
185{
186 EXPECT_TRUE(isEmpty({}));
187}
188
189TEST(MacIsEmpty, False)
190{
191 EXPECT_FALSE(isEmpty(fromString("01:00:00:00:00:00")));
192 EXPECT_FALSE(isEmpty(fromString("00:00:00:10:00:00")));
193 EXPECT_FALSE(isEmpty(fromString("00:00:00:00:00:01")));
194}
195
196TEST(MacIsMulticast, True)
197{
198 EXPECT_TRUE(isMulticast(fromString("ff:ff:ff:ff:ff:ff")));
199 EXPECT_TRUE(isMulticast(fromString("01:00:00:00:00:00")));
200}
201
202TEST(MacIsMulticast, False)
203{
204 EXPECT_FALSE(isMulticast(fromString("00:11:22:33:44:55")));
205 EXPECT_FALSE(isMulticast(fromString("FE:11:22:33:44:55")));
206}
207
208TEST(MacIsUnicast, True)
209{
210 EXPECT_TRUE(isUnicast(fromString("00:11:22:33:44:55")));
211 EXPECT_TRUE(isUnicast(fromString("FE:11:22:33:44:55")));
212}
213
214TEST(MacIsUnicast, False)
215{
216 EXPECT_FALSE(isUnicast(fromString("00:00:00:00:00:00")));
217 EXPECT_FALSE(isUnicast(fromString("01:00:00:00:00:00")));
218 EXPECT_FALSE(isUnicast(fromString("ff:ff:ff:ff:ff:ff")));
219}
220
Lei YU307554e2021-03-18 14:56:50 +0800221TEST(IgnoredInterfaces, Empty)
222{
223 auto ret = internal::parseInterfaces({});
224 EXPECT_TRUE(ret.empty());
225
226 ret = internal::parseInterfaces(" , ,, ");
227 EXPECT_TRUE(ret.empty());
228}
229
230TEST(IgnoredInterfaces, NotEmpty)
231{
232 using ::testing::ContainerEq;
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700233 std::set<std::string_view> expected = {"eth0"};
Lei YU307554e2021-03-18 14:56:50 +0800234 auto ret = internal::parseInterfaces("eth0");
235 EXPECT_THAT(ret, ContainerEq(expected));
236
237 expected = {"eth0", "eth1", "bond1", "usb0"};
238 ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,");
239 EXPECT_THAT(ret, ContainerEq(expected));
240}
241
William A. Kennington III1137a972019-04-20 20:49:58 -0700242} // namespace mac_address
Gunnar Mills57d9c502018-09-14 14:42:34 -0500243} // namespace network
244} // namespace phosphor