blob: d2be390161baf6f7cc25dcf5ac7798b11d6136ff [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>
Ratan Gupta35297172018-11-28 18:40:16 +05307#include <xyz/openbmc_project/Common/error.hpp>
8
Gunnar Mills57d9c502018-09-14 14:42:34 -05009#include <gtest/gtest.h>
10
Ratan Gupta594123d2017-05-16 22:48:35 +053011namespace phosphor
12{
13namespace network
14{
15
Ratan Gupta35297172018-11-28 18:40:16 +053016using InternalFailure =
17 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Ratan Gupta594123d2017-05-16 22:48:35 +053018class TestUtil : public testing::Test
19{
Gunnar Mills57d9c502018-09-14 14:42:34 -050020 public:
21 TestUtil()
22 {
23 // Empty
24 }
Ratan Gupta594123d2017-05-16 22:48:35 +053025};
26
William A. Kennington IIId27410f2019-01-30 17:15:43 -080027TEST_F(TestUtil, ToHex)
28{
29 EXPECT_EQ('E', mac_address::internal::toHex(std::byte(0xfe)));
30 EXPECT_EQ('A', mac_address::internal::toHex(std::byte(10)));
31 EXPECT_EQ('4', mac_address::internal::toHex(std::byte(4)));
32}
33
William A. Kennington IIIa14879e2019-02-01 21:43:11 -080034TEST_F(TestUtil, MacFromBuf)
35{
36 std::string tooSmall(1, 'a');
37 std::string tooLarge(24, 'a');
38 std::string buf{'\x00', '\xde', '\xad', '\x00', '\xbe', '\xef'};
39
40 MacAddr mac = mac_address::fromBuf(buf);
41 EXPECT_EQ(0, memcmp(buf.data(), mac.data(), buf.size()));
42
43 EXPECT_THROW(mac_address::fromBuf(tooSmall), std::runtime_error);
44 EXPECT_THROW(mac_address::fromBuf(tooLarge), std::runtime_error);
45}
46
William A. Kennington IIId27410f2019-01-30 17:15:43 -080047TEST_F(TestUtil, MacToString)
48{
49 MacAddr mac1{
50 std::byte(0x00), std::byte(0xDE), std::byte(0xAD),
51 std::byte(0x00), std::byte(0xBE), std::byte(0xEF),
52 };
53 EXPECT_EQ("00:DE:AD:00:BE:EF", mac_address::toString(mac1));
54 MacAddr mac2{
55 std::byte(0x70), std::byte(0xFF), std::byte(0x84),
56 std::byte(0x09), std::byte(0x35), std::byte(0x09),
57 };
58 EXPECT_EQ("70:FF:84:09:35:09", mac_address::toString(mac2));
59}
60
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080061TEST_F(TestUtil, AddrFromBuf)
62{
63 std::string tooSmall(1, 'a');
64 std::string tooLarge(24, 'a');
65
66 struct in_addr ip1;
67 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
68 std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1));
69 InAddrAny res1 = addrFromBuf(AF_INET, buf1);
70 EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1)));
71 EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error);
72 EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error);
73 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::runtime_error);
74
75 struct in6_addr ip2;
76 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
77 std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2));
78 InAddrAny res2 = addrFromBuf(AF_INET6, buf2);
79 EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2)));
80 EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error);
81 EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error);
82 EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::runtime_error);
83}
84
William A. Kennington III5058f572019-01-30 17:18:14 -080085TEST_F(TestUtil, IpToString)
86{
87 struct in_addr ip1;
88 EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
89 EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
90
91 struct in6_addr ip2;
92 EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
93 EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
94
95 InAddrAny ip3;
96 try
97 {
98 struct E
99 {
100 operator struct in6_addr()
101 {
102 throw 1;
103 }
104 };
105 ip3.emplace<struct in6_addr>(E());
106 EXPECT_TRUE(false);
107 }
108 catch (...)
109 {
110 }
111 EXPECT_THROW(toString(ip3), std::runtime_error);
112}
113
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600114TEST_F(TestUtil, IpValidation)
115{
116 std::string ipaddress = "0.0.0.0";
117 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
118
119 ipaddress = "9.3.185.83";
120 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
121
122 ipaddress = "9.3.185.a";
123 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
124
125 ipaddress = "9.3.a.83";
126 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
127
128 ipaddress = "x.x.x.x";
129 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
130
131 ipaddress = "0:0:0:0:0:0:0:0";
132 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
133
134 ipaddress = "1:0:0:0:0:0:0:8";
135 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
136
137 ipaddress = "1::8";
138 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
139
140 ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
141 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
142
143 ipaddress = "::ffff:204.152.189.116";
144 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
145
146 ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
147 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
148
149 ipaddress = "1::8";
150 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600151}
152
153TEST_F(TestUtil, PrefixValidation)
154{
155 uint8_t prefixLength = 1;
156 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
157
158 prefixLength = 32;
159 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
160
161 prefixLength = 0;
162 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
163
164 prefixLength = 33;
165 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
166
167 prefixLength = 33;
168 EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
169
170 prefixLength = 65;
171 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600172}
173
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600174TEST_F(TestUtil, MacValidation)
175{
176 std::string macaddress = "00:00:00:00:00:00";
177 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
178
179 macaddress = "F6:C6:E6:6:B0:D3";
William A. Kennington III4966e962019-04-08 01:58:10 -0700180 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600181
182 macaddress = "F6:C6:E6:06:B0:D3";
183 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
184
185 macaddress = "hh:HH:HH:hh:HH:yy";
186 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
187
188 macaddress = "hhh:GGG:iii:jjj:kkk:lll";
189 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600190}
191
Ratan Gupta594123d2017-05-16 22:48:35 +0530192TEST_F(TestUtil, ConvertV4MasktoPrefix)
193{
194 std::string mask = "255.255.255.0";
195 uint8_t prefix = toCidr(AF_INET, mask);
196 EXPECT_EQ(prefix, 24);
197
198 mask = "255.255.0.0";
199 prefix = toCidr(AF_INET, mask);
200 EXPECT_EQ(prefix, 16);
201
202 mask = "255.0.0.0";
203 prefix = toCidr(AF_INET, mask);
204 EXPECT_EQ(prefix, 8);
205
206 mask = "255.224.0.0";
207 prefix = toCidr(AF_INET, mask);
208 EXPECT_EQ(prefix, 11);
209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530211 mask = "255.0.255.0";
212 prefix = toCidr(AF_INET, mask);
213 EXPECT_EQ(prefix, 0);
214}
215
216TEST_F(TestUtil, convertV6MasktoPrefix)
217{
218 std::string mask = "ffff:ffff::";
219 uint8_t prefix = toCidr(AF_INET6, mask);
220 EXPECT_EQ(prefix, 32);
221
222 mask = "ffff:ffff:ffff::";
223 prefix = toCidr(AF_INET6, mask);
224 EXPECT_EQ(prefix, 48);
225
226 mask = "ffff:ffff:fc00::";
227 prefix = toCidr(AF_INET6, mask);
228 EXPECT_EQ(prefix, 38);
229
Gunnar Mills57d9c502018-09-14 14:42:34 -0500230 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530231 mask = "ffff:0fff::";
232 prefix = toCidr(AF_INET6, mask);
233 EXPECT_EQ(prefix, 0);
234}
235
Ratan Gupta11cef802017-05-29 08:41:48 +0530236TEST_F(TestUtil, isLinkLocaladdress)
Ratan Gupta594123d2017-05-16 22:48:35 +0530237{
238 std::string ipaddress = "fe80:fec0::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500239 EXPECT_TRUE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530240
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500241 ipaddress = "2000:fe80:789::";
242 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530243
244 ipaddress = "2000:fe80::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500245 EXPECT_FALSE(isLinkLocalIP(ipaddress));
246
247 ipaddress = "169.254.3.3";
248 EXPECT_TRUE(isLinkLocalIP(ipaddress));
249
250 ipaddress = "3.169.254.3";
251 EXPECT_FALSE(isLinkLocalIP(ipaddress));
252
253 ipaddress = "3.3.169.254";
254 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530255}
256
257TEST_F(TestUtil, convertPrefixToMask)
258{
259 std::string mask = toMask(AF_INET, 24);
260 EXPECT_EQ(mask, "255.255.255.0");
261
262 mask = toMask(AF_INET, 8);
263 EXPECT_EQ(mask, "255.0.0.0");
264
265 mask = toMask(AF_INET, 27);
266 EXPECT_EQ(mask, "255.255.255.224");
267}
268
Ratan Gupta11cef802017-05-29 08:41:48 +0530269TEST_F(TestUtil, getNetworkAddress)
270{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500271 std::string address = getNetworkID(AF_INET, "9.3.23.251", 24);
272 EXPECT_EQ("9.3.23.0", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530273
Gunnar Mills57d9c502018-09-14 14:42:34 -0500274 address = getNetworkID(AF_INET, "9.3.23.251", 25);
275 EXPECT_EQ("9.3.23.128", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530276
Gunnar Mills57d9c502018-09-14 14:42:34 -0500277 address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64);
278 EXPECT_EQ("2001:db8:abcd:dd12::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530279
Ratan Gupta35297172018-11-28 18:40:16 +0530280 EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure);
Ratan Gupta11cef802017-05-29 08:41:48 +0530281
Ratan Gupta35297172018-11-28 18:40:16 +0530282 EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64),
283 InternalFailure);
Ratan Gupta11cef802017-05-29 08:41:48 +0530284
Gunnar Mills57d9c502018-09-14 14:42:34 -0500285 address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64);
286 EXPECT_EQ("fe80::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530287}
288
Gunnar Mills57d9c502018-09-14 14:42:34 -0500289} // namespace network
290} // namespace phosphor