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 using ::testing::Key;
23 using ::testing::UnorderedElementsAre;
24 namespace fs = std::filesystem;
25 
26 class TestNetworkManager : public stdplus::gtest::TestWithTmp
27 {
28   public:
29     sdbusplus::bus_t bus;
30     MockManager manager;
31     TestNetworkManager() :
32         bus(sdbusplus::bus::new_default()),
33         manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
34     {
35     }
36 
37     void createInterfaces()
38     {
39         manager.createInterfaces();
40     }
41 };
42 
43 // getifaddrs will not return any interface
44 TEST_F(TestNetworkManager, NoInterface)
45 {
46     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
47     EXPECT_THROW(createInterfaces(), InternalFailure);
48 }
49 // getifaddrs returns single interface.
50 TEST_F(TestNetworkManager, WithSingleInterface)
51 {
52     mock_clear();
53 
54     // Adds the following ip in the getifaddrs list.
55     mock_addIF("igb1", /*idx=*/2);
56     mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
57 
58     // Now create the interfaces which will call the mocked getifaddrs
59     // which returns the above interface detail.
60     createInterfaces();
61     EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1")));
62 }
63 
64 // getifaddrs returns two interfaces.
65 TEST_F(TestNetworkManager, WithMultipleInterfaces)
66 {
67     mock_clear();
68 
69     mock_addIF("igb0", /*idx=*/1);
70     mock_addIP("igb0", "192.0.2.2", "255.255.255.128");
71     mock_addIF("igb1", /*idx=*/2);
72     mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
73 
74     createInterfaces();
75     EXPECT_THAT(manager.getInterfaces(),
76                 UnorderedElementsAre(Key("igb0"), Key("igb1")));
77 }
78 } // namespace network
79 } // namespace phosphor
80