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