1 #include "config_parser.hpp" 2 #include "mock_network_manager.hpp" 3 #include "mock_syscall.hpp" 4 5 #include <arpa/inet.h> 6 #include <net/if.h> 7 #include <netinet/in.h> 8 #include <stdlib.h> 9 10 #include <filesystem> 11 #include <sdbusplus/bus.hpp> 12 #include <stdplus/gtest/tmp.hpp> 13 14 #include <gtest/gtest.h> 15 16 namespace phosphor 17 { 18 namespace network 19 { 20 21 using ::testing::Key; 22 using ::testing::UnorderedElementsAre; 23 24 class TestNetworkManager : public stdplus::gtest::TestWithTmp 25 { 26 protected: 27 sdbusplus::bus_t bus; 28 MockManager manager; 29 TestNetworkManager() : 30 bus(sdbusplus::bus::new_default()), 31 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) 32 { 33 system::mock_clear(); 34 } 35 36 void createInterfaces() 37 { 38 manager.createInterfaces(); 39 } 40 41 void deleteVLAN(std::string_view ifname) 42 { 43 manager.interfaces.find(ifname)->second->vlan->delete_(); 44 } 45 }; 46 47 // getifaddrs will not return any interface 48 TEST_F(TestNetworkManager, NoInterface) 49 { 50 createInterfaces(); 51 EXPECT_TRUE(manager.interfaces.empty()); 52 } 53 // getifaddrs returns single interface. 54 TEST_F(TestNetworkManager, WithSingleInterface) 55 { 56 // Adds the following ip in the getifaddrs list. 57 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); 58 59 // Now create the interfaces which will call the mocked getifaddrs 60 // which returns the above interface detail. 61 createInterfaces(); 62 EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1"))); 63 } 64 65 // getifaddrs returns two interfaces. 66 TEST_F(TestNetworkManager, WithMultipleInterfaces) 67 { 68 system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"}); 69 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"}); 70 71 createInterfaces(); 72 EXPECT_THAT(manager.interfaces, 73 UnorderedElementsAre(Key("igb0"), Key("igb1"))); 74 } 75 76 TEST_F(TestNetworkManager, WithVLAN) 77 { 78 EXPECT_THROW(manager.vlan("", 8000), std::exception); 79 EXPECT_THROW(manager.vlan("", 0), std::exception); 80 EXPECT_THROW(manager.vlan("eth0", 2), std::exception); 81 82 system::mock_addIF({.idx = 1, .flags = 0, .name = "eth0"}); 83 manager.createInterfaces(); 84 EXPECT_NO_THROW(manager.vlan("eth0", 2)); 85 EXPECT_NO_THROW(manager.vlan("eth0", 4094)); 86 EXPECT_THAT( 87 manager.interfaces, 88 UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094"))); 89 auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2"); 90 auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094"); 91 EXPECT_TRUE(std::filesystem::is_regular_file(netdev1)); 92 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); 93 94 deleteVLAN("eth0.2"); 95 EXPECT_THAT(manager.interfaces, 96 UnorderedElementsAre(Key("eth0"), Key("eth0.4094"))); 97 EXPECT_FALSE(std::filesystem::is_regular_file(netdev1)); 98 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); 99 } 100 101 } // namespace network 102 } // namespace phosphor 103