blob: 8413c013e4a4626dd7295ac5f218be351f7cc00e [file] [log] [blame]
Ratan Gupta8ab17922017-05-25 13:07:05 +05301#include "network_manager.hpp"
2#include "mock_syscall.hpp"
3
4#include "xyz/openbmc_project/Common/error.hpp"
5#include <phosphor-logging/elog-errors.hpp>
Ratan Guptae05083a2017-09-16 07:12:11 +05306#include "timer.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05307
8#include <gtest/gtest.h>
9#include <sdbusplus/bus.hpp>
10
11#include <net/if.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
Ratan Gupta255d5142017-08-10 09:02:08 +053014#include <stdlib.h>
15
Ratan Gupta8ab17922017-05-25 13:07:05 +053016#include <exception>
Ratan Gupta255d5142017-08-10 09:02:08 +053017#include <experimental/filesystem>
Ratan Gupta8ab17922017-05-25 13:07:05 +053018
19namespace phosphor
20{
21namespace network
22{
23
Ratan Gupta16f12882017-09-22 18:26:11 +053024std::unique_ptr<phosphor::network::Timer> refreshObjectTimer = nullptr;
25std::unique_ptr<phosphor::network::Timer> restartTimer = nullptr;
26
Ratan Gupta255d5142017-08-10 09:02:08 +053027namespace fs = std::experimental::filesystem;
28
Ratan Gupta8ab17922017-05-25 13:07:05 +053029class TestNetworkManager : public testing::Test
30{
31 public:
32
33 sdbusplus::bus::bus bus;
34 Manager manager;
Ratan Gupta255d5142017-08-10 09:02:08 +053035 std::string confDir;
Ratan Gupta8ab17922017-05-25 13:07:05 +053036 TestNetworkManager()
37 : bus(sdbusplus::bus::new_default()),
Ratan Gupta255d5142017-08-10 09:02:08 +053038 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
Ratan Gupta8ab17922017-05-25 13:07:05 +053039 {
Ratan Gupta255d5142017-08-10 09:02:08 +053040 setConfDir();
41 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053042
Ratan Gupta255d5142017-08-10 09:02:08 +053043 ~TestNetworkManager()
44 {
45 if(confDir != "")
46 {
47 fs::remove_all(confDir);
48 }
49 }
50
51 void setConfDir()
52 {
53 char tmp[] = "/tmp/NetworkManager.XXXXXX";
54 confDir = mkdtemp(tmp);
55 manager.setConfDir(confDir);
Ratan Gupta8ab17922017-05-25 13:07:05 +053056 }
57
58 void createInterfaces()
59 {
60 manager.createInterfaces();
61 }
62
63 int getSize()
64 {
65 return manager.interfaces.size();
66 }
67
68 bool isInterfaceAdded(std::string intf)
69 {
70 return manager.interfaces.find(intf) != manager.interfaces.end() ?
71 true :
72 false;
73 }
74};
75
76// getifaddrs will not return any interface
77TEST_F(TestNetworkManager, NoInterface)
78{
79 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
80 bool caughtException = false;
81 try
82 {
83 createInterfaces();
84 }
85 catch (InternalFailure& e)
86 {
87 caughtException = true;
88 }
89
90 EXPECT_EQ(true, caughtException);
91}
92
93// getifaddrs returns single interface.
94TEST_F(TestNetworkManager, WithSingleInterface)
95{
96 bool caughtException = false;
97 try
98 {
99 // Adds the following ip in the getifaddrs list.
100 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
101 IFF_UP | IFF_RUNNING);
102
103 // Now create the interfaces which will call the mocked getifaddrs
104 // which returns the above interface detail.
105 createInterfaces();
106 EXPECT_EQ(1, getSize());
107 EXPECT_EQ(true, isInterfaceAdded("igb1"));
108 }
109 catch (std::exception& e)
110 {
111 caughtException = true;
112 }
113 EXPECT_EQ(false, caughtException);
114}
115
116// getifaddrs returns two interfaces.
117TEST_F(TestNetworkManager, WithMultipleInterfaces)
118{
119 try
120 {
121 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
122 IFF_UP | IFF_RUNNING);
123
124 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
125 IFF_UP | IFF_RUNNING);
126
127 createInterfaces();
128 EXPECT_EQ(2, getSize());
129 EXPECT_EQ(true, isInterfaceAdded("igb0"));
130 }
131 catch (std::exception& e)
132 {
133 }
134}
135
136}// namespce network
137}// namespace phosphor