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