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 <sdbusplus/bus.hpp> 10 #include <stdplus/gtest/tmp.hpp> 11 12 #include <gtest/gtest.h> 13 14 namespace phosphor 15 { 16 namespace network 17 { 18 19 using ::testing::Key; 20 using ::testing::UnorderedElementsAre; 21 22 class TestNetworkManager : public stdplus::gtest::TestWithTmp 23 { 24 public: 25 sdbusplus::bus_t bus; 26 MockManager manager; 27 TestNetworkManager() : 28 bus(sdbusplus::bus::new_default()), 29 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) 30 { 31 system::mock_clear(); 32 } 33 34 void createInterfaces() 35 { 36 manager.createInterfaces(); 37 } 38 }; 39 40 // getifaddrs will not return any interface 41 TEST_F(TestNetworkManager, NoInterface) 42 { 43 createInterfaces(); 44 EXPECT_TRUE(manager.getInterfaces().empty()); 45 } 46 // getifaddrs returns single interface. 47 TEST_F(TestNetworkManager, WithSingleInterface) 48 { 49 // Adds the following ip in the getifaddrs list. 50 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); 51 52 // Now create the interfaces which will call the mocked getifaddrs 53 // which returns the above interface detail. 54 createInterfaces(); 55 EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1"))); 56 } 57 58 // getifaddrs returns two interfaces. 59 TEST_F(TestNetworkManager, WithMultipleInterfaces) 60 { 61 system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"}); 62 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); 63 64 createInterfaces(); 65 EXPECT_THAT(manager.getInterfaces(), 66 UnorderedElementsAre(Key("igb0"), Key("igb1"))); 67 } 68 } // namespace network 69 } // namespace phosphor 70