blob: 4e79394048d2ae5598520d88472af026d6559dcb [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)));
William A. Kennington III5058f572019-01-30 17:18:14 -080094}
95
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060096TEST_F(TestUtil, IpValidation)
97{
98 std::string ipaddress = "0.0.0.0";
99 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
100
101 ipaddress = "9.3.185.83";
102 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
103
104 ipaddress = "9.3.185.a";
105 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
106
107 ipaddress = "9.3.a.83";
108 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
109
110 ipaddress = "x.x.x.x";
111 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
112
113 ipaddress = "0:0:0:0:0:0:0:0";
114 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
115
116 ipaddress = "1:0:0:0:0:0:0:8";
117 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
118
119 ipaddress = "1::8";
120 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
121
122 ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
123 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
124
125 ipaddress = "::ffff:204.152.189.116";
126 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
127
128 ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
129 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
130
131 ipaddress = "1::8";
132 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600133}
134
135TEST_F(TestUtil, PrefixValidation)
136{
137 uint8_t prefixLength = 1;
138 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
139
140 prefixLength = 32;
141 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
142
143 prefixLength = 0;
144 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
145
146 prefixLength = 33;
147 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
148
149 prefixLength = 33;
150 EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
151
152 prefixLength = 65;
153 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600154}
155
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600156TEST_F(TestUtil, MacValidation)
157{
158 std::string macaddress = "00:00:00:00:00:00";
159 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
160
161 macaddress = "F6:C6:E6:6:B0:D3";
William A. Kennington III4966e962019-04-08 01:58:10 -0700162 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600163
164 macaddress = "F6:C6:E6:06:B0:D3";
165 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
166
167 macaddress = "hh:HH:HH:hh:HH:yy";
168 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
169
170 macaddress = "hhh:GGG:iii:jjj:kkk:lll";
171 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600172}
173
Ratan Gupta594123d2017-05-16 22:48:35 +0530174TEST_F(TestUtil, ConvertV4MasktoPrefix)
175{
176 std::string mask = "255.255.255.0";
177 uint8_t prefix = toCidr(AF_INET, mask);
178 EXPECT_EQ(prefix, 24);
179
180 mask = "255.255.0.0";
181 prefix = toCidr(AF_INET, mask);
182 EXPECT_EQ(prefix, 16);
183
184 mask = "255.0.0.0";
185 prefix = toCidr(AF_INET, mask);
186 EXPECT_EQ(prefix, 8);
187
188 mask = "255.224.0.0";
189 prefix = toCidr(AF_INET, mask);
190 EXPECT_EQ(prefix, 11);
191
Gunnar Mills57d9c502018-09-14 14:42:34 -0500192 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530193 mask = "255.0.255.0";
194 prefix = toCidr(AF_INET, mask);
195 EXPECT_EQ(prefix, 0);
196}
197
198TEST_F(TestUtil, convertV6MasktoPrefix)
199{
200 std::string mask = "ffff:ffff::";
201 uint8_t prefix = toCidr(AF_INET6, mask);
202 EXPECT_EQ(prefix, 32);
203
204 mask = "ffff:ffff:ffff::";
205 prefix = toCidr(AF_INET6, mask);
206 EXPECT_EQ(prefix, 48);
207
208 mask = "ffff:ffff:fc00::";
209 prefix = toCidr(AF_INET6, mask);
210 EXPECT_EQ(prefix, 38);
211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530213 mask = "ffff:0fff::";
214 prefix = toCidr(AF_INET6, mask);
215 EXPECT_EQ(prefix, 0);
216}
217
Ratan Gupta11cef802017-05-29 08:41:48 +0530218TEST_F(TestUtil, isLinkLocaladdress)
Ratan Gupta594123d2017-05-16 22:48:35 +0530219{
220 std::string ipaddress = "fe80:fec0::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500221 EXPECT_TRUE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530222
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500223 ipaddress = "2000:fe80:789::";
224 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530225
226 ipaddress = "2000:fe80::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500227 EXPECT_FALSE(isLinkLocalIP(ipaddress));
228
229 ipaddress = "169.254.3.3";
230 EXPECT_TRUE(isLinkLocalIP(ipaddress));
231
232 ipaddress = "3.169.254.3";
233 EXPECT_FALSE(isLinkLocalIP(ipaddress));
234
235 ipaddress = "3.3.169.254";
236 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530237}
238
239TEST_F(TestUtil, convertPrefixToMask)
240{
241 std::string mask = toMask(AF_INET, 24);
242 EXPECT_EQ(mask, "255.255.255.0");
243
244 mask = toMask(AF_INET, 8);
245 EXPECT_EQ(mask, "255.0.0.0");
246
247 mask = toMask(AF_INET, 27);
248 EXPECT_EQ(mask, "255.255.255.224");
249}
250
Ratan Gupta11cef802017-05-29 08:41:48 +0530251TEST_F(TestUtil, getNetworkAddress)
252{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 std::string address = getNetworkID(AF_INET, "9.3.23.251", 24);
254 EXPECT_EQ("9.3.23.0", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530255
Gunnar Mills57d9c502018-09-14 14:42:34 -0500256 address = getNetworkID(AF_INET, "9.3.23.251", 25);
257 EXPECT_EQ("9.3.23.128", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530258
Gunnar Mills57d9c502018-09-14 14:42:34 -0500259 address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64);
260 EXPECT_EQ("2001:db8:abcd:dd12::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530261
Ratan Gupta35297172018-11-28 18:40:16 +0530262 EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure);
Ratan Gupta11cef802017-05-29 08:41:48 +0530263
Ratan Gupta35297172018-11-28 18:40:16 +0530264 EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64),
265 InternalFailure);
Ratan Gupta11cef802017-05-29 08:41:48 +0530266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64);
268 EXPECT_EQ("fe80::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530269}
270
Gunnar Mills57d9c502018-09-14 14:42:34 -0500271} // namespace network
272} // namespace phosphor