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> |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 4 | #include <fmt/chrono.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <netinet/in.h> |
| 6 | |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 7 | #include <charconv> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 8 | #include <cstddef> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 9 | #include <cstring> |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 10 | #include <stdexcept> |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <string_view> |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
| 17 | |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace network |
| 21 | { |
| 22 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 23 | using namespace std::literals; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 24 | using InternalFailure = |
| 25 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 26 | class TestUtil : public testing::Test |
| 27 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | public: |
| 29 | TestUtil() |
| 30 | { |
| 31 | // Empty |
| 32 | } |
Ratan Gupta | 594123d | 2017-05-16 22:48:35 +0530 | [diff] [blame] | 33 | }; |
| 34 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 35 | TEST_F(TestUtil, AddrFromBuf) |
| 36 | { |
| 37 | std::string tooSmall(1, 'a'); |
| 38 | std::string tooLarge(24, 'a'); |
| 39 | |
| 40 | struct in_addr ip1; |
| 41 | EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1)); |
| 42 | std::string_view buf1(reinterpret_cast<char*>(&ip1), sizeof(ip1)); |
| 43 | InAddrAny res1 = addrFromBuf(AF_INET, buf1); |
| 44 | EXPECT_EQ(0, memcmp(&ip1, &std::get<struct in_addr>(res1), sizeof(ip1))); |
| 45 | EXPECT_THROW(addrFromBuf(AF_INET, tooSmall), std::runtime_error); |
| 46 | EXPECT_THROW(addrFromBuf(AF_INET, tooLarge), std::runtime_error); |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 47 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf1), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 48 | |
| 49 | struct in6_addr ip2; |
| 50 | EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2)); |
| 51 | std::string_view buf2(reinterpret_cast<char*>(&ip2), sizeof(ip2)); |
| 52 | InAddrAny res2 = addrFromBuf(AF_INET6, buf2); |
| 53 | EXPECT_EQ(0, memcmp(&ip2, &std::get<struct in6_addr>(res2), sizeof(ip2))); |
| 54 | EXPECT_THROW(addrFromBuf(AF_INET6, tooSmall), std::runtime_error); |
| 55 | EXPECT_THROW(addrFromBuf(AF_INET6, tooLarge), std::runtime_error); |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 56 | EXPECT_THROW(addrFromBuf(AF_UNSPEC, buf2), std::invalid_argument); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 59 | TEST_F(TestUtil, IpValidation) |
| 60 | { |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 61 | EXPECT_TRUE(isValidIP(AF_INET, "0.0.0.0")); |
| 62 | EXPECT_TRUE(isValidIP("0.0.0.0")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 63 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 64 | EXPECT_TRUE(isValidIP(AF_INET, "9.3.185.83")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 65 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 66 | EXPECT_FALSE(isValidIP(AF_INET, "9.3.185.a")); |
| 67 | EXPECT_FALSE(isValidIP("9.3.185.a")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 68 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 69 | EXPECT_FALSE(isValidIP(AF_INET, "9.3.a.83")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 70 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 71 | EXPECT_FALSE(isValidIP(AF_INET, "x.x.x.x")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 72 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:0:0:0")); |
| 74 | EXPECT_TRUE(isValidIP("0:0:0:0:0:0:0:0")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 75 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(isValidIP(AF_INET6, "1:0:0:0:0:0:0:8")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 77 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(isValidIP(AF_INET6, "1::8")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 79 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(isValidIP(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 81 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(isValidIP(AF_INET6, "::ffff:204.152.189.116")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 83 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(isValidIP(AF_INET6, "a:0:0:0:0:FFFF:204.152.189.116")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 85 | |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(isValidIP(AF_INET6, "1::8")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(TestUtil, PrefixValidation) |
| 90 | { |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(isValidPrefix(AF_INET, 0)); |
| 92 | EXPECT_TRUE(isValidPrefix(AF_INET, 1)); |
| 93 | EXPECT_TRUE(isValidPrefix(AF_INET, 32)); |
| 94 | EXPECT_FALSE(isValidPrefix(AF_INET, 33)); |
| 95 | EXPECT_FALSE(isValidPrefix(AF_INET, 64)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 96 | |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(isValidPrefix(AF_INET6, 0)); |
| 98 | EXPECT_TRUE(isValidPrefix(AF_INET6, 1)); |
| 99 | EXPECT_TRUE(isValidPrefix(AF_INET6, 53)); |
| 100 | EXPECT_TRUE(isValidPrefix(AF_INET6, 64)); |
| 101 | EXPECT_TRUE(isValidPrefix(AF_INET6, 128)); |
| 102 | EXPECT_FALSE(isValidPrefix(AF_INET6, 129)); |
| 103 | EXPECT_FALSE(isValidPrefix(AF_INET6, 177)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 104 | |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 105 | EXPECT_THROW(isValidPrefix(AF_UNSPEC, 1), std::invalid_argument); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 106 | } |
| 107 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 108 | TEST_F(TestUtil, InterfaceToUbootEthAddr) |
| 109 | { |
| 110 | EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("et")); |
| 111 | EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth")); |
| 112 | EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("sit0")); |
| 113 | EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("ethh0")); |
| 114 | EXPECT_EQ(std::nullopt, interfaceToUbootEthAddr("eth0h")); |
| 115 | EXPECT_EQ("ethaddr", interfaceToUbootEthAddr("eth0")); |
| 116 | EXPECT_EQ("eth1addr", interfaceToUbootEthAddr("eth1")); |
| 117 | EXPECT_EQ("eth5addr", interfaceToUbootEthAddr("eth5")); |
| 118 | EXPECT_EQ("eth28addr", interfaceToUbootEthAddr("eth28")); |
| 119 | } |
| 120 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 121 | namespace mac_address |
| 122 | { |
| 123 | |
| 124 | TEST(MacFromString, Bad) |
| 125 | { |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 126 | EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::invalid_argument); |
| 127 | EXPECT_THROW(fromString("00:00:00:00:00"), std::invalid_argument); |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 128 | EXPECT_THROW(fromString("00:00:00:00:00:"), std::invalid_argument); |
| 129 | EXPECT_THROW(fromString("00:00:00:00::00"), std::invalid_argument); |
| 130 | EXPECT_THROW(fromString(":00:00:00:00:00"), std::invalid_argument); |
| 131 | EXPECT_THROW(fromString("00::00:00:00:00"), std::invalid_argument); |
| 132 | EXPECT_THROW(fromString(":::::"), std::invalid_argument); |
| 133 | EXPECT_THROW(fromString("00:0:0:0:0"), std::invalid_argument); |
| 134 | EXPECT_THROW(fromString("00:00:00:00:00:00:00"), std::invalid_argument); |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 135 | EXPECT_THROW(fromString(""), std::invalid_argument); |
Potin Lai | da0b1d4 | 2021-12-26 20:08:20 +0800 | [diff] [blame] | 136 | EXPECT_THROW(fromString("123456789XYZ"), std::invalid_argument); |
| 137 | EXPECT_THROW(fromString("123456789AB"), std::invalid_argument); |
| 138 | EXPECT_THROW(fromString("123456789ABCD"), std::invalid_argument); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST(MacFromString, Valid) |
| 142 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 143 | EXPECT_EQ((ether_addr{}), fromString("00:00:00:00:00:00")); |
| 144 | EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}), |
| 145 | fromString("FF:EE:DD:cc:bb:aa")); |
| 146 | EXPECT_EQ((ether_addr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}), |
| 147 | fromString("0:1:2:3:4:5")); |
| 148 | EXPECT_EQ((ether_addr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}), |
| 149 | fromString("0123456789AB")); |
| 150 | EXPECT_EQ((ether_addr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}), |
| 151 | fromString("FFEEDDccbbaa")); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST(MacIsEmpty, True) |
| 155 | { |
| 156 | EXPECT_TRUE(isEmpty({})); |
| 157 | } |
| 158 | |
| 159 | TEST(MacIsEmpty, False) |
| 160 | { |
| 161 | EXPECT_FALSE(isEmpty(fromString("01:00:00:00:00:00"))); |
| 162 | EXPECT_FALSE(isEmpty(fromString("00:00:00:10:00:00"))); |
| 163 | EXPECT_FALSE(isEmpty(fromString("00:00:00:00:00:01"))); |
| 164 | } |
| 165 | |
| 166 | TEST(MacIsMulticast, True) |
| 167 | { |
| 168 | EXPECT_TRUE(isMulticast(fromString("ff:ff:ff:ff:ff:ff"))); |
| 169 | EXPECT_TRUE(isMulticast(fromString("01:00:00:00:00:00"))); |
| 170 | } |
| 171 | |
| 172 | TEST(MacIsMulticast, False) |
| 173 | { |
| 174 | EXPECT_FALSE(isMulticast(fromString("00:11:22:33:44:55"))); |
| 175 | EXPECT_FALSE(isMulticast(fromString("FE:11:22:33:44:55"))); |
| 176 | } |
| 177 | |
| 178 | TEST(MacIsUnicast, True) |
| 179 | { |
| 180 | EXPECT_TRUE(isUnicast(fromString("00:11:22:33:44:55"))); |
| 181 | EXPECT_TRUE(isUnicast(fromString("FE:11:22:33:44:55"))); |
| 182 | } |
| 183 | |
| 184 | TEST(MacIsUnicast, False) |
| 185 | { |
| 186 | EXPECT_FALSE(isUnicast(fromString("00:00:00:00:00:00"))); |
| 187 | EXPECT_FALSE(isUnicast(fromString("01:00:00:00:00:00"))); |
| 188 | EXPECT_FALSE(isUnicast(fromString("ff:ff:ff:ff:ff:ff"))); |
| 189 | } |
| 190 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 191 | TEST(IgnoredInterfaces, Empty) |
| 192 | { |
| 193 | auto ret = internal::parseInterfaces({}); |
| 194 | EXPECT_TRUE(ret.empty()); |
| 195 | |
| 196 | ret = internal::parseInterfaces(" , ,, "); |
| 197 | EXPECT_TRUE(ret.empty()); |
| 198 | } |
| 199 | |
| 200 | TEST(IgnoredInterfaces, NotEmpty) |
| 201 | { |
| 202 | using ::testing::ContainerEq; |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 203 | std::unordered_set<std::string_view> expected = {"eth0"}; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 204 | auto ret = internal::parseInterfaces("eth0"); |
| 205 | EXPECT_THAT(ret, ContainerEq(expected)); |
| 206 | |
| 207 | expected = {"eth0", "eth1", "bond1", "usb0"}; |
| 208 | ret = internal::parseInterfaces(" ,eth0, eth1 ,bond1, usb0,,"); |
| 209 | EXPECT_THAT(ret, ContainerEq(expected)); |
| 210 | } |
| 211 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 212 | } // namespace mac_address |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 213 | } // namespace network |
| 214 | } // namespace phosphor |