Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 3 | #include <arpa/inet.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include <netinet/in.h> |
| 5 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 6 | #include <cstddef> |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> |
| 8 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 16 | using InternalFailure = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 18 | class TestUtil : public testing::Test |
| 19 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 20 | public: |
| 21 | TestUtil() |
| 22 | { |
| 23 | // Empty |
| 24 | } |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 25 | }; |
| 26 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 27 | TEST_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 III | a14879e | 2019-02-01 21:43:11 -0800 | [diff] [blame] | 34 | TEST_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 III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 47 | TEST_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 III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 61 | TEST_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 III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 85 | TEST_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 III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 96 | TEST_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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TEST_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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 156 | TEST_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 III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 162 | EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 163 | |
| 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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 172 | } |
| 173 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 174 | TEST_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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 192 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 193 | mask = "255.0.255.0"; |
| 194 | prefix = toCidr(AF_INET, mask); |
| 195 | EXPECT_EQ(prefix, 0); |
| 196 | } |
| 197 | |
| 198 | TEST_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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 212 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 213 | mask = "ffff:0fff::"; |
| 214 | prefix = toCidr(AF_INET6, mask); |
| 215 | EXPECT_EQ(prefix, 0); |
| 216 | } |
| 217 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 218 | TEST_F(TestUtil, isLinkLocaladdress) |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 219 | { |
| 220 | std::string ipaddress = "fe80:fec0::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 221 | EXPECT_TRUE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 222 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 223 | ipaddress = "2000:fe80:789::"; |
| 224 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 225 | |
| 226 | ipaddress = "2000:fe80::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 227 | 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 Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST_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 Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 251 | TEST_F(TestUtil, getNetworkAddress) |
| 252 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 253 | std::string address = getNetworkID(AF_INET, "9.3.23.251", 24); |
| 254 | EXPECT_EQ("9.3.23.0", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 255 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 256 | address = getNetworkID(AF_INET, "9.3.23.251", 25); |
| 257 | EXPECT_EQ("9.3.23.128", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 258 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 259 | address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64); |
| 260 | EXPECT_EQ("2001:db8:abcd:dd12::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 261 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 262 | EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 263 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 264 | EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64), |
| 265 | InternalFailure); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 266 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 267 | address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64); |
| 268 | EXPECT_EQ("fe80::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 271 | } // namespace network |
| 272 | } // namespace phosphor |