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))); |
| 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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 114 | TEST_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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 172 | } |
| 173 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 174 | TEST_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"; |
| 180 | EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); |
| 181 | |
| 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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 190 | } |
| 191 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 192 | TEST_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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 210 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 211 | mask = "255.0.255.0"; |
| 212 | prefix = toCidr(AF_INET, mask); |
| 213 | EXPECT_EQ(prefix, 0); |
| 214 | } |
| 215 | |
| 216 | TEST_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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 230 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 231 | mask = "ffff:0fff::"; |
| 232 | prefix = toCidr(AF_INET6, mask); |
| 233 | EXPECT_EQ(prefix, 0); |
| 234 | } |
| 235 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 236 | TEST_F(TestUtil, isLinkLocaladdress) |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 237 | { |
| 238 | std::string ipaddress = "fe80:fec0::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 239 | EXPECT_TRUE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 240 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 241 | ipaddress = "2000:fe80:789::"; |
| 242 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 243 | |
| 244 | ipaddress = "2000:fe80::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 245 | 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 Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | TEST_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 Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 269 | TEST_F(TestUtil, getNetworkAddress) |
| 270 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 271 | std::string address = getNetworkID(AF_INET, "9.3.23.251", 24); |
| 272 | EXPECT_EQ("9.3.23.0", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 273 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 274 | address = getNetworkID(AF_INET, "9.3.23.251", 25); |
| 275 | EXPECT_EQ("9.3.23.128", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 276 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 277 | address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64); |
| 278 | EXPECT_EQ("2001:db8:abcd:dd12::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 279 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 280 | EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 281 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 282 | EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64), |
| 283 | InternalFailure); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 284 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 285 | address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64); |
| 286 | EXPECT_EQ("fe80::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 287 | } |
| 288 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 289 | } // namespace network |
| 290 | } // namespace phosphor |