Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 2 | #include "ipaddress.hpp" |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 3 | #include "mock_network_manager.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include "mock_syscall.hpp" |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 6 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 8 | #include <net/if.h> |
| 9 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 12 | #include <exception> |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 13 | #include <fstream> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 15 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 16 | |
| 17 | #include <gtest/gtest.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 18 | |
| 19 | namespace phosphor |
| 20 | { |
| 21 | namespace network |
| 22 | { |
| 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestEthernetInterface : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t bus; |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 28 | std::string confDir; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 29 | MockManager manager; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 30 | MockEthernetInterface interface; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | TestEthernetInterface() : |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 32 | bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()), |
| 33 | manager(bus, "/xyz/openbmc_test/network", confDir), |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 34 | interface(makeInterface(bus, manager)) |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 38 | |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 39 | static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
| 40 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 41 | static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus, |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 42 | MockManager& manager) |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 43 | { |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 44 | mock_clear(); |
William A. Kennington III | cb64b99 | 2019-04-21 18:45:07 -0700 | [diff] [blame] | 45 | mock_addIF("test0", 1, mac); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 46 | return {bus, |
| 47 | "/xyz/openbmc_test/network/test0", |
| 48 | config::Parser(), |
| 49 | EthernetInterface::DHCPConf::none, |
| 50 | manager, |
| 51 | true}; |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 54 | int countIPObjects() |
| 55 | { |
| 56 | return interface.getAddresses().size(); |
| 57 | } |
| 58 | |
| 59 | bool isIPObjectExist(const std::string& ipaddress) |
| 60 | { |
| 61 | auto address = interface.getAddresses().find(ipaddress); |
| 62 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 63 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 64 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 65 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | return true; |
| 67 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 68 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 69 | bool deleteIPObject(const std::string& ipaddress) |
| 70 | { |
| 71 | auto address = interface.getAddresses().find(ipaddress); |
| 72 | if (address == interface.getAddresses().end()) |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 73 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 74 | return false; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 75 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 76 | address->second->delete_(); |
| 77 | return true; |
| 78 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 81 | const std::string& gateway, |
| 82 | IP::AddressOrigin origin) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | { |
| 84 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 85 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 86 | return interface.generateObjectPath(addressType, ipaddress, subnetMask, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 87 | gateway, origin); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void createIPObject(IP::Protocol addressType, const std::string& ipaddress, |
| 91 | uint8_t subnetMask, const std::string& gateway) |
| 92 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 93 | interface.ip(addressType, ipaddress, subnetMask, gateway); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | } |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 98 | { |
| 99 | EXPECT_EQ(countIPObjects(), 0); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 100 | EXPECT_EQ(mac_address::toString(mac), interface.macAddress()); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 104 | { |
| 105 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 106 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 107 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 111 | { |
| 112 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 113 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 114 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 115 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 116 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 120 | { |
| 121 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 122 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 123 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 124 | deleteIPObject("10.10.10.10"); |
| 125 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 126 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 130 | { |
| 131 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 132 | } |
| 133 | |
| 134 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 135 | { |
| 136 | std::string ipaddress = "10.10.10.10"; |
| 137 | uint8_t prefix = 16; |
| 138 | std::string gateway = "10.10.10.1"; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 139 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 140 | |
| 141 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 142 | std::stringstream hexId; |
| 143 | |
| 144 | std::string hashString = ipaddress; |
| 145 | hashString += std::to_string(prefix); |
| 146 | hashString += gateway; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 147 | hashString += convertForMessage(origin); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 148 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 149 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 150 | expectedObjectPath += hexId.str(); |
| 151 | |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 152 | EXPECT_EQ(expectedObjectPath, |
| 153 | getObjectPath(ipaddress, prefix, gateway, origin)); |
| 154 | |
| 155 | origin = IP::AddressOrigin::DHCP; |
| 156 | EXPECT_NE(expectedObjectPath, |
| 157 | getObjectPath(ipaddress, prefix, gateway, origin)); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 158 | } |
| 159 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 160 | TEST_F(TestEthernetInterface, addStaticNameServers) |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 161 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 162 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 163 | EXPECT_CALL(manager, reloadConfigs()); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 164 | interface.staticNameServers(servers); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 165 | fs::path filePath = confDir; |
| 166 | filePath /= "00-bmc-test0.network"; |
| 167 | config::Parser parser(filePath.string()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 168 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 09f6157 | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 171 | TEST_F(TestEthernetInterface, getDynamicNameServers) |
| 172 | { |
| 173 | ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; |
| 174 | EXPECT_CALL(interface, getNameServerFromResolvd()) |
| 175 | .WillRepeatedly(testing::Return(servers)); |
| 176 | EXPECT_EQ(interface.getNameServerFromResolvd(), servers); |
| 177 | } |
| 178 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 179 | TEST_F(TestEthernetInterface, addNTPServers) |
| 180 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 181 | ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 182 | EXPECT_CALL(manager, reloadConfigs()); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 183 | interface.ntpServers(servers); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 184 | fs::path filePath = confDir; |
| 185 | filePath /= "00-bmc-test0.network"; |
| 186 | config::Parser parser(filePath.string()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 187 | EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 190 | TEST_F(TestEthernetInterface, addGateway) |
| 191 | { |
| 192 | std::string gateway = "10.3.3.3"; |
| 193 | interface.defaultGateway(gateway); |
| 194 | EXPECT_EQ(interface.defaultGateway(), gateway); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 195 | interface.defaultGateway(""); |
| 196 | EXPECT_EQ(interface.defaultGateway(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST_F(TestEthernetInterface, addGateway6) |
| 200 | { |
| 201 | std::string gateway6 = "ffff:ffff:ffff:fe80::1"; |
| 202 | interface.defaultGateway6(gateway6); |
| 203 | EXPECT_EQ(interface.defaultGateway6(), gateway6); |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 204 | interface.defaultGateway6(""); |
| 205 | EXPECT_EQ(interface.defaultGateway6(), ""); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 208 | } // namespace network |
| 209 | } // namespace phosphor |