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     MockManager 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 // getifaddrs returns single interface.
67 TEST_F(TestNetworkManager, WithSingleInterface)
68 {
69     mock_clear();
70 
71     // Adds the following ip in the getifaddrs list.
72     mock_addIF("igb1", 2);
73     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
74 
75     // Now create the interfaces which will call the mocked getifaddrs
76     // which returns the above interface detail.
77     createInterfaces();
78     EXPECT_EQ(1, manager.getInterfaceCount());
79     EXPECT_EQ(true, manager.hasInterface("igb1"));
80 }
81 
82 // getifaddrs returns two interfaces.
83 TEST_F(TestNetworkManager, WithMultipleInterfaces)
84 {
85     mock_clear();
86 
87     mock_addIF("igb0", 1);
88     mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
89 
90     mock_addIF("igb1", 2);
91     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
92 
93     createInterfaces();
94     EXPECT_EQ(2, manager.getInterfaceCount());
95     EXPECT_EQ(true, manager.hasInterface("igb0"));
96     EXPECT_EQ(true, manager.hasInterface("igb1"));
97 }
98 } // namespace network
99 } // namespace phosphor
100