1 #include "mock_syscall.hpp" 2 #include "network_manager.hpp" 3 #include "timer.hpp" 4 #include "xyz/openbmc_project/Common/error.hpp" 5 6 #include <arpa/inet.h> 7 #include <net/if.h> 8 #include <netinet/in.h> 9 #include <stdlib.h> 10 11 #include <exception> 12 #include <experimental/filesystem> 13 #include <phosphor-logging/elog-errors.hpp> 14 #include <sdbusplus/bus.hpp> 15 16 #include <gtest/gtest.h> 17 18 namespace phosphor 19 { 20 namespace network 21 { 22 23 namespace fs = std::experimental::filesystem; 24 25 class TestNetworkManager : public testing::Test 26 { 27 public: 28 sdbusplus::bus::bus bus; 29 Manager manager; 30 std::string confDir; 31 TestNetworkManager() : 32 bus(sdbusplus::bus::new_default()), 33 manager(bus, "/xyz/openbmc_test/abc", "/tmp") 34 { 35 setConfDir(); 36 } 37 38 ~TestNetworkManager() 39 { 40 if (confDir != "") 41 { 42 fs::remove_all(confDir); 43 } 44 } 45 46 void setConfDir() 47 { 48 char tmp[] = "/tmp/NetworkManager.XXXXXX"; 49 confDir = mkdtemp(tmp); 50 manager.setConfDir(confDir); 51 } 52 53 void createInterfaces() 54 { 55 manager.createInterfaces(); 56 } 57 58 int getSize() 59 { 60 return manager.interfaces.size(); 61 } 62 63 bool isInterfaceAdded(std::string intf) 64 { 65 return manager.interfaces.find(intf) != manager.interfaces.end() 66 ? true 67 : false; 68 } 69 }; 70 71 // getifaddrs will not return any interface 72 TEST_F(TestNetworkManager, NoInterface) 73 { 74 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 75 bool caughtException = false; 76 try 77 { 78 createInterfaces(); 79 } 80 catch (InternalFailure& e) 81 { 82 caughtException = true; 83 } 84 85 EXPECT_EQ(true, caughtException); 86 } 87 88 // getifaddrs returns single interface. 89 TEST_F(TestNetworkManager, WithSingleInterface) 90 { 91 bool caughtException = false; 92 try 93 { 94 // Adds the following ip in the getifaddrs list. 95 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", 96 IFF_UP | IFF_RUNNING); 97 98 // Now create the interfaces which will call the mocked getifaddrs 99 // which returns the above interface detail. 100 createInterfaces(); 101 EXPECT_EQ(1, getSize()); 102 EXPECT_EQ(true, isInterfaceAdded("igb1")); 103 } 104 catch (std::exception& e) 105 { 106 caughtException = true; 107 } 108 EXPECT_EQ(false, caughtException); 109 } 110 111 // getifaddrs returns two interfaces. 112 TEST_F(TestNetworkManager, WithMultipleInterfaces) 113 { 114 try 115 { 116 mock_addIP("igb0", "192.0.2.2", "255.255.255.128", 117 IFF_UP | IFF_RUNNING); 118 119 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", 120 IFF_UP | IFF_RUNNING); 121 122 createInterfaces(); 123 EXPECT_EQ(2, getSize()); 124 EXPECT_EQ(true, isInterfaceAdded("igb0")); 125 } 126 catch (std::exception& e) 127 { 128 } 129 } 130 131 } // namespace network 132 } // namespace phosphor 133