Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame^] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 2 | #include "network_manager.hpp" |
| 3 | #include "mock_syscall.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 4 | #include "ipaddress.hpp" |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 9 | #include <arpa/inet.h> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 10 | #include <net/if.h> |
| 11 | #include <netinet/in.h> |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 14 | #include <exception> |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame^] | 15 | #include <fstream> |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | |
| 22 | class TestEthernetInterface : public testing::Test |
| 23 | { |
| 24 | public: |
| 25 | |
| 26 | sdbusplus::bus::bus bus; |
| 27 | Manager manager; |
| 28 | EthernetInterface interface; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 29 | std::string confDir; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 30 | TestEthernetInterface() |
| 31 | : bus(sdbusplus::bus::new_default()), |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 32 | manager(bus, "/xyz/openbmc_test/network", "/tmp/"), |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 33 | interface(bus, "/xyz/openbmc_test/network/test0", false, manager) |
| 34 | |
| 35 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 36 | setConfDir(); |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 37 | } |
| 38 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 39 | void setConfDir() |
| 40 | { |
| 41 | char tmp[] = "/tmp/EthernetInterface.XXXXXX"; |
| 42 | confDir = mkdtemp(tmp); |
| 43 | manager.setConfDir(confDir); |
| 44 | } |
| 45 | |
| 46 | ~TestEthernetInterface() |
| 47 | { |
| 48 | if(confDir != "") |
| 49 | { |
| 50 | fs::remove_all(confDir); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 55 | int countIPObjects() |
| 56 | { |
| 57 | return interface.getAddresses().size(); |
| 58 | } |
| 59 | |
| 60 | bool isIPObjectExist(const std::string& ipaddress) |
| 61 | { |
| 62 | auto address = interface.getAddresses().find(ipaddress); |
| 63 | if (address == interface.getAddresses().end()) |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | bool deleteIPObject(const std::string& ipaddress) |
| 72 | { |
| 73 | auto address = interface.getAddresses().find(ipaddress); |
| 74 | if (address == interface.getAddresses().end()) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | address->second->delete_(); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | std::string getObjectPath(const std::string& ipaddress, |
| 83 | uint8_t subnetMask, |
| 84 | const std::string& gateway) |
| 85 | { |
| 86 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 87 | |
| 88 | return interface.generateObjectPath(addressType, |
| 89 | ipaddress, |
| 90 | subnetMask, |
| 91 | gateway); |
| 92 | } |
| 93 | |
| 94 | void createIPObject(IP::Protocol addressType, |
| 95 | const std::string& ipaddress, |
| 96 | uint8_t subnetMask, |
| 97 | const std::string& gateway) |
| 98 | { |
| 99 | interface.iP(addressType, |
| 100 | ipaddress, |
| 101 | subnetMask, |
| 102 | gateway |
| 103 | ); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | }; |
| 108 | |
| 109 | TEST_F(TestEthernetInterface, NoIPaddress) |
| 110 | { |
| 111 | EXPECT_EQ(countIPObjects(), 0); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | TEST_F(TestEthernetInterface, AddIPAddress) |
| 116 | { |
| 117 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 118 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 119 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | TEST_F(TestEthernetInterface, AddMultipleAddress) |
| 124 | { |
| 125 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 126 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 127 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 128 | EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); |
| 129 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | TEST_F(TestEthernetInterface, DeleteIPAddress) |
| 134 | { |
| 135 | IP::Protocol addressType = IP::Protocol::IPv4; |
| 136 | createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); |
| 137 | createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); |
| 138 | deleteIPObject("10.10.10.10"); |
| 139 | EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); |
| 140 | EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) |
| 145 | { |
| 146 | EXPECT_EQ(false, deleteIPObject("10.10.10.10")); |
| 147 | } |
| 148 | |
| 149 | TEST_F(TestEthernetInterface, CheckObjectPath) |
| 150 | { |
| 151 | std::string ipaddress = "10.10.10.10"; |
| 152 | uint8_t prefix = 16; |
| 153 | std::string gateway = "10.10.10.1"; |
| 154 | |
| 155 | std::string expectedObjectPath = "/xyz/openbmc_test/network/test0/ipv4/"; |
| 156 | std::stringstream hexId; |
| 157 | |
| 158 | std::string hashString = ipaddress; |
| 159 | hashString += std::to_string(prefix); |
| 160 | hashString += gateway; |
| 161 | |
| 162 | |
| 163 | hexId << std::hex << ((std::hash<std::string> {}( |
| 164 | hashString)) & 0xFFFFFFFF); |
| 165 | expectedObjectPath += hexId.str(); |
| 166 | |
| 167 | EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, |
| 168 | prefix, |
| 169 | gateway)); |
| 170 | } |
| 171 | |
Ratan Gupta | c9645fe | 2017-08-23 16:53:52 +0530 | [diff] [blame^] | 172 | TEST_F(TestEthernetInterface, addNTPServers) |
| 173 | { |
| 174 | ServerList servers = {"10.1.1.1","10.2.2.2","10.3.3.3"}; |
| 175 | interface.nTPServers(servers); |
| 176 | fs::path filePath = confDir; |
| 177 | filePath /= "00-bmc-test0.network"; |
| 178 | config::Parser parser(filePath.string()); |
| 179 | auto values = parser.getValues("Network", "NTP"); |
| 180 | EXPECT_EQ(servers , values); |
| 181 | } |
| 182 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 183 | }// namespce network |
| 184 | }// namespace phosphor |