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