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 <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 namespace fs = std::filesystem;
22 
23 class TestNetworkManager : public testing::Test
24 {
25   public:
26     sdbusplus::bus::bus bus;
27     MockManager manager;
28     std::string confDir;
29     TestNetworkManager() :
30         bus(sdbusplus::bus::new_default()),
31         manager(bus, "/xyz/openbmc_test/abc", "/tmp")
32     {
33         setConfDir();
34     }
35 
36     ~TestNetworkManager()
37     {
38         if (confDir != "")
39         {
40             fs::remove_all(confDir);
41         }
42     }
43 
44     void setConfDir()
45     {
46         char tmp[] = "/tmp/NetworkManager.XXXXXX";
47         confDir = mkdtemp(tmp);
48         manager.setConfDir(confDir);
49     }
50 
51     void createInterfaces()
52     {
53         manager.createInterfaces();
54     }
55 };
56 
57 // getifaddrs will not return any interface
58 TEST_F(TestNetworkManager, NoInterface)
59 {
60     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
61     EXPECT_THROW(createInterfaces(), InternalFailure);
62 }
63 // getifaddrs returns single interface.
64 TEST_F(TestNetworkManager, WithSingleInterface)
65 {
66     mock_clear();
67 
68     // Adds the following ip in the getifaddrs list.
69     mock_addIF("igb1", 2);
70     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
71 
72     // Now create the interfaces which will call the mocked getifaddrs
73     // which returns the above interface detail.
74     createInterfaces();
75     EXPECT_EQ(1, manager.getInterfaceCount());
76     EXPECT_EQ(true, manager.hasInterface("igb1"));
77 }
78 
79 // getifaddrs returns two interfaces.
80 TEST_F(TestNetworkManager, WithMultipleInterfaces)
81 {
82     mock_clear();
83 
84     mock_addIF("igb0", 1);
85     mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
86 
87     mock_addIF("igb1", 2);
88     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
89 
90     createInterfaces();
91     EXPECT_EQ(2, manager.getInterfaceCount());
92     EXPECT_EQ(true, manager.hasInterface("igb0"));
93     EXPECT_EQ(true, manager.hasInterface("igb1"));
94 }
95 } // namespace network
96 } // namespace phosphor
97