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