1 #include "mock_network_manager.hpp" 2 #include "mock_syscall.hpp" 3 4 #include <arpa/inet.h> 5 #include <net/if.h> 6 #include <netinet/in.h> 7 #include <stdlib.h> 8 9 #include <exception> 10 #include <experimental/filesystem> 11 #include <sdbusplus/bus.hpp> 12 #include <xyz/openbmc_project/Common/error.hpp> 13 14 #include <gtest/gtest.h> 15 16 namespace phosphor 17 { 18 namespace network 19 { 20 21 std::unique_ptr<Timer> refreshObjectTimer = nullptr; 22 std::unique_ptr<Timer> restartTimer = nullptr; 23 24 namespace fs = std::experimental::filesystem; 25 26 class TestNetworkManager : public testing::Test 27 { 28 public: 29 sdbusplus::bus::bus bus; 30 Manager manager; 31 std::string confDir; 32 TestNetworkManager() : 33 bus(sdbusplus::bus::new_default()), 34 manager(bus, "/xyz/openbmc_test/abc", "/tmp") 35 { 36 setConfDir(); 37 } 38 39 ~TestNetworkManager() 40 { 41 if (confDir != "") 42 { 43 fs::remove_all(confDir); 44 } 45 } 46 47 void setConfDir() 48 { 49 char tmp[] = "/tmp/NetworkManager.XXXXXX"; 50 confDir = mkdtemp(tmp); 51 manager.setConfDir(confDir); 52 } 53 54 void createInterfaces() 55 { 56 manager.createInterfaces(); 57 } 58 }; 59 60 // getifaddrs will not return any interface 61 TEST_F(TestNetworkManager, NoInterface) 62 { 63 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 64 EXPECT_THROW(createInterfaces(), InternalFailure); 65 } 66 67 // getifaddrs returns single interface. 68 TEST_F(TestNetworkManager, WithSingleInterface) 69 { 70 mock_clear(); 71 72 // Adds the following ip in the getifaddrs list. 73 mock_addIF("igb1", 2); 74 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING); 75 76 // Now create the interfaces which will call the mocked getifaddrs 77 // which returns the above interface detail. 78 createInterfaces(); 79 EXPECT_EQ(1, manager.getInterfaceCount()); 80 EXPECT_EQ(true, manager.hasInterface("igb1")); 81 } 82 83 // getifaddrs returns two interfaces. 84 TEST_F(TestNetworkManager, WithMultipleInterfaces) 85 { 86 mock_clear(); 87 88 mock_addIF("igb0", 1); 89 mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING); 90 91 mock_addIF("igb1", 2); 92 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING); 93 94 createInterfaces(); 95 EXPECT_EQ(2, manager.getInterfaceCount()); 96 EXPECT_EQ(true, manager.hasInterface("igb0")); 97 EXPECT_EQ(true, manager.hasInterface("igb1")); 98 } 99 100 } // namespace network 101 } // namespace phosphor 102