Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 3 | #include <netinet/in.h> |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace network |
| 10 | { |
| 11 | |
| 12 | class TestUtil : public testing::Test |
| 13 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | public: |
| 15 | TestUtil() |
| 16 | { |
| 17 | // Empty |
| 18 | } |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 19 | }; |
| 20 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 21 | TEST_F(TestUtil, IpValidation) |
| 22 | { |
| 23 | std::string ipaddress = "0.0.0.0"; |
| 24 | EXPECT_EQ(true, isValidIP(AF_INET, ipaddress)); |
| 25 | |
| 26 | ipaddress = "9.3.185.83"; |
| 27 | EXPECT_EQ(true, isValidIP(AF_INET, ipaddress)); |
| 28 | |
| 29 | ipaddress = "9.3.185.a"; |
| 30 | EXPECT_EQ(false, isValidIP(AF_INET, ipaddress)); |
| 31 | |
| 32 | ipaddress = "9.3.a.83"; |
| 33 | EXPECT_EQ(false, isValidIP(AF_INET, ipaddress)); |
| 34 | |
| 35 | ipaddress = "x.x.x.x"; |
| 36 | EXPECT_EQ(false, isValidIP(AF_INET, ipaddress)); |
| 37 | |
| 38 | ipaddress = "0:0:0:0:0:0:0:0"; |
| 39 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 40 | |
| 41 | ipaddress = "1:0:0:0:0:0:0:8"; |
| 42 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 43 | |
| 44 | ipaddress = "1::8"; |
| 45 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 46 | |
| 47 | ipaddress = "0:0:0:0:0:FFFF:204.152.189.116"; |
| 48 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 49 | |
| 50 | ipaddress = "::ffff:204.152.189.116"; |
| 51 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 52 | |
| 53 | ipaddress = "a:0:0:0:0:FFFF:204.152.189.116"; |
| 54 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
| 55 | |
| 56 | ipaddress = "1::8"; |
| 57 | EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_F(TestUtil, PrefixValidation) |
| 61 | { |
| 62 | uint8_t prefixLength = 1; |
| 63 | EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength)); |
| 64 | |
| 65 | prefixLength = 32; |
| 66 | EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength)); |
| 67 | |
| 68 | prefixLength = 0; |
| 69 | EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength)); |
| 70 | |
| 71 | prefixLength = 33; |
| 72 | EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength)); |
| 73 | |
| 74 | prefixLength = 33; |
| 75 | EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength)); |
| 76 | |
| 77 | prefixLength = 65; |
| 78 | EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 81 | TEST_F(TestUtil, MacValidation) |
| 82 | { |
| 83 | std::string macaddress = "00:00:00:00:00:00"; |
| 84 | EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); |
| 85 | |
| 86 | macaddress = "F6:C6:E6:6:B0:D3"; |
| 87 | EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); |
| 88 | |
| 89 | macaddress = "F6:C6:E6:06:B0:D3"; |
| 90 | EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress)); |
| 91 | |
| 92 | macaddress = "hh:HH:HH:hh:HH:yy"; |
| 93 | EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); |
| 94 | |
| 95 | macaddress = "hhh:GGG:iii:jjj:kkk:lll"; |
| 96 | EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 97 | } |
| 98 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 99 | TEST_F(TestUtil, ConvertV4MasktoPrefix) |
| 100 | { |
| 101 | std::string mask = "255.255.255.0"; |
| 102 | uint8_t prefix = toCidr(AF_INET, mask); |
| 103 | EXPECT_EQ(prefix, 24); |
| 104 | |
| 105 | mask = "255.255.0.0"; |
| 106 | prefix = toCidr(AF_INET, mask); |
| 107 | EXPECT_EQ(prefix, 16); |
| 108 | |
| 109 | mask = "255.0.0.0"; |
| 110 | prefix = toCidr(AF_INET, mask); |
| 111 | EXPECT_EQ(prefix, 8); |
| 112 | |
| 113 | mask = "255.224.0.0"; |
| 114 | prefix = toCidr(AF_INET, mask); |
| 115 | EXPECT_EQ(prefix, 11); |
| 116 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 117 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 118 | mask = "255.0.255.0"; |
| 119 | prefix = toCidr(AF_INET, mask); |
| 120 | EXPECT_EQ(prefix, 0); |
| 121 | } |
| 122 | |
| 123 | TEST_F(TestUtil, convertV6MasktoPrefix) |
| 124 | { |
| 125 | std::string mask = "ffff:ffff::"; |
| 126 | uint8_t prefix = toCidr(AF_INET6, mask); |
| 127 | EXPECT_EQ(prefix, 32); |
| 128 | |
| 129 | mask = "ffff:ffff:ffff::"; |
| 130 | prefix = toCidr(AF_INET6, mask); |
| 131 | EXPECT_EQ(prefix, 48); |
| 132 | |
| 133 | mask = "ffff:ffff:fc00::"; |
| 134 | prefix = toCidr(AF_INET6, mask); |
| 135 | EXPECT_EQ(prefix, 38); |
| 136 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 137 | // Invalid Mask |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 138 | mask = "ffff:0fff::"; |
| 139 | prefix = toCidr(AF_INET6, mask); |
| 140 | EXPECT_EQ(prefix, 0); |
| 141 | } |
| 142 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 143 | TEST_F(TestUtil, isLinkLocaladdress) |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 144 | { |
| 145 | std::string ipaddress = "fe80:fec0::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 146 | EXPECT_TRUE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 147 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 148 | ipaddress = "2000:fe80:789::"; |
| 149 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 150 | |
| 151 | ipaddress = "2000:fe80::"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 152 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
| 153 | |
| 154 | ipaddress = "169.254.3.3"; |
| 155 | EXPECT_TRUE(isLinkLocalIP(ipaddress)); |
| 156 | |
| 157 | ipaddress = "3.169.254.3"; |
| 158 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
| 159 | |
| 160 | ipaddress = "3.3.169.254"; |
| 161 | EXPECT_FALSE(isLinkLocalIP(ipaddress)); |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | TEST_F(TestUtil, convertPrefixToMask) |
| 165 | { |
| 166 | std::string mask = toMask(AF_INET, 24); |
| 167 | EXPECT_EQ(mask, "255.255.255.0"); |
| 168 | |
| 169 | mask = toMask(AF_INET, 8); |
| 170 | EXPECT_EQ(mask, "255.0.0.0"); |
| 171 | |
| 172 | mask = toMask(AF_INET, 27); |
| 173 | EXPECT_EQ(mask, "255.255.255.224"); |
| 174 | } |
| 175 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 176 | TEST_F(TestUtil, getNetworkAddress) |
| 177 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 178 | std::string address = getNetworkID(AF_INET, "9.3.23.251", 24); |
| 179 | EXPECT_EQ("9.3.23.0", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 180 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 181 | address = getNetworkID(AF_INET, "9.3.23.251", 25); |
| 182 | EXPECT_EQ("9.3.23.128", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 183 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 184 | address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64); |
| 185 | EXPECT_EQ("2001:db8:abcd:dd12::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 186 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 187 | address = getNetworkID(AF_INET, "a.b.c.d", 25); |
| 188 | EXPECT_EQ("", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 189 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 190 | address = getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64); |
| 191 | EXPECT_EQ("", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 192 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 193 | address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64); |
| 194 | EXPECT_EQ("fe80::", address); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 195 | } |
| 196 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 197 | } // namespace network |
| 198 | } // namespace phosphor |