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 <stdplus/gtest/tmp.hpp>
13 #include <xyz/openbmc_project/Common/error.hpp>
14 
15 #include <gtest/gtest.h>
16 
17 namespace phosphor
18 {
19 namespace network
20 {
21 
22 namespace fs = std::filesystem;
23 
24 class TestNetworkManager : public stdplus::gtest::TestWithTmp
25 {
26   public:
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     }
34 
35     void createInterfaces()
36     {
37         manager.createInterfaces();
38     }
39 };
40 
41 // getifaddrs will not return any interface
42 TEST_F(TestNetworkManager, NoInterface)
43 {
44     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
45     EXPECT_THROW(createInterfaces(), InternalFailure);
46 }
47 // getifaddrs returns single interface.
48 TEST_F(TestNetworkManager, WithSingleInterface)
49 {
50     mock_clear();
51 
52     // Adds the following ip in the getifaddrs list.
53     mock_addIF("igb1", 2);
54     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
55 
56     // Now create the interfaces which will call the mocked getifaddrs
57     // which returns the above interface detail.
58     createInterfaces();
59     EXPECT_EQ(1, manager.getInterfaceCount());
60     EXPECT_EQ(true, manager.hasInterface("igb1"));
61 }
62 
63 // getifaddrs returns two interfaces.
64 TEST_F(TestNetworkManager, WithMultipleInterfaces)
65 {
66     mock_clear();
67 
68     mock_addIF("igb0", 1);
69     mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
70 
71     mock_addIF("igb1", 2);
72     mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
73 
74     createInterfaces();
75     EXPECT_EQ(2, manager.getInterfaceCount());
76     EXPECT_EQ(true, manager.hasInterface("igb0"));
77     EXPECT_EQ(true, manager.hasInterface("igb1"));
78 }
79 } // namespace network
80 } // namespace phosphor
81