blob: 2776bcf226cb10e22a5aa637a8b0bd18ba4b3295 [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 Gupta255d5142017-08-10 09:02:08 +053024namespace fs = std::experimental::filesystem;
25
Ratan Gupta8ab17922017-05-25 13:07:05 +053026class TestNetworkManager : public testing::Test
27{
28 public:
29
30 sdbusplus::bus::bus bus;
31 Manager manager;
Ratan Gupta255d5142017-08-10 09:02:08 +053032 std::string confDir;
Ratan Gupta8ab17922017-05-25 13:07:05 +053033 TestNetworkManager()
34 : bus(sdbusplus::bus::new_default()),
Ratan Gupta255d5142017-08-10 09:02:08 +053035 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
Ratan Gupta8ab17922017-05-25 13:07:05 +053036 {
Ratan Gupta255d5142017-08-10 09:02:08 +053037 setConfDir();
38 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053039
Ratan Gupta255d5142017-08-10 09:02:08 +053040 ~TestNetworkManager()
41 {
42 if(confDir != "")
43 {
44 fs::remove_all(confDir);
45 }
46 }
47
48 void setConfDir()
49 {
50 char tmp[] = "/tmp/NetworkManager.XXXXXX";
51 confDir = mkdtemp(tmp);
52 manager.setConfDir(confDir);
Ratan Gupta8ab17922017-05-25 13:07:05 +053053 }
54
55 void createInterfaces()
56 {
57 manager.createInterfaces();
58 }
59
60 int getSize()
61 {
62 return manager.interfaces.size();
63 }
64
65 bool isInterfaceAdded(std::string intf)
66 {
67 return manager.interfaces.find(intf) != manager.interfaces.end() ?
68 true :
69 false;
70 }
71};
72
73// getifaddrs will not return any interface
74TEST_F(TestNetworkManager, NoInterface)
75{
76 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
77 bool caughtException = false;
78 try
79 {
80 createInterfaces();
81 }
82 catch (InternalFailure& e)
83 {
84 caughtException = true;
85 }
86
87 EXPECT_EQ(true, caughtException);
88}
89
90// getifaddrs returns single interface.
91TEST_F(TestNetworkManager, WithSingleInterface)
92{
93 bool caughtException = false;
94 try
95 {
96 // Adds the following ip in the getifaddrs list.
97 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
98 IFF_UP | IFF_RUNNING);
99
100 // Now create the interfaces which will call the mocked getifaddrs
101 // which returns the above interface detail.
102 createInterfaces();
103 EXPECT_EQ(1, getSize());
104 EXPECT_EQ(true, isInterfaceAdded("igb1"));
105 }
106 catch (std::exception& e)
107 {
108 caughtException = true;
109 }
110 EXPECT_EQ(false, caughtException);
111}
112
113// getifaddrs returns two interfaces.
114TEST_F(TestNetworkManager, WithMultipleInterfaces)
115{
116 try
117 {
118 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
119 IFF_UP | IFF_RUNNING);
120
121 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
122 IFF_UP | IFF_RUNNING);
123
124 createInterfaces();
125 EXPECT_EQ(2, getSize());
126 EXPECT_EQ(true, isInterfaceAdded("igb0"));
127 }
128 catch (std::exception& e)
129 {
130 }
131}
132
133}// namespce network
134}// namespace phosphor