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