summaryrefslogtreecommitdiffstats
path: root/test/test_network_manager.cpp
blob: 911a1bddf65bd6580d98d0a062f039faa7b94027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "mock_network_manager.hpp"
#include "mock_syscall.hpp"
#include "xyz/openbmc_project/Common/error.hpp"

#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdlib.h>

#include <exception>
#include <experimental/filesystem>
#include <sdbusplus/bus.hpp>

#include <gtest/gtest.h>

namespace phosphor
{
namespace network
{

std::unique_ptr<Timer> refreshObjectTimer = nullptr;
std::unique_ptr<Timer> restartTimer = nullptr;

namespace fs = std::experimental::filesystem;

class TestNetworkManager : public testing::Test
{
  public:
    sdbusplus::bus::bus bus;
    MockManager manager;
    std::string confDir;
    TestNetworkManager() :
        bus(sdbusplus::bus::new_default()),
        manager(bus, "/xyz/openbmc_test/abc", "/tmp")
    {
        setConfDir();
    }

    ~TestNetworkManager()
    {
        if (confDir != "")
        {
            fs::remove_all(confDir);
        }
    }

    void setConfDir()
    {
        char tmp[] = "/tmp/NetworkManager.XXXXXX";
        confDir = mkdtemp(tmp);
        manager.setConfDir(confDir);
    }

    void createInterfaces()
    {
        manager.createInterfaces();
    }

    int getSize()
    {
        return manager.interfaces.size();
    }

    bool isInterfaceAdded(std::string intf)
    {
        return manager.interfaces.find(intf) != manager.interfaces.end()
                   ? true
                   : false;
    }
};

// getifaddrs will not return any interface
TEST_F(TestNetworkManager, NoInterface)
{
    using namespace sdbusplus::xyz::openbmc_project::Common::Error;
    bool caughtException = false;
    try
    {
        createInterfaces();
    }
    catch (InternalFailure& e)
    {
        caughtException = true;
    }

    EXPECT_EQ(true, caughtException);
}

// getifaddrs returns single interface.
TEST_F(TestNetworkManager, WithSingleInterface)
{
    bool caughtException = false;
    try
    {
        // Adds the following ip in the getifaddrs list.
        mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
                   IFF_UP | IFF_RUNNING);

        // Now create the interfaces which will call the mocked getifaddrs
        // which returns the above interface detail.
        createInterfaces();
        EXPECT_EQ(1, getSize());
        EXPECT_EQ(true, isInterfaceAdded("igb1"));
    }
    catch (std::exception& e)
    {
        caughtException = true;
    }
    EXPECT_EQ(false, caughtException);
}

// getifaddrs returns two interfaces.
TEST_F(TestNetworkManager, WithMultipleInterfaces)
{
    try
    {
        mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
                   IFF_UP | IFF_RUNNING);

        mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
                   IFF_UP | IFF_RUNNING);

        createInterfaces();
        EXPECT_EQ(2, getSize());
        EXPECT_EQ(true, isInterfaceAdded("igb0"));
    }
    catch (std::exception& e)
    {
    }
}

} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud