blob: 1c0a116183101e43e99c130e48e115c6287eb563 [file] [log] [blame]
Ratan Gupta8ab17922017-05-25 13:07:05 +05301#include "mock_syscall.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002#include "network_manager.hpp"
Ratan Guptae05083a2017-09-16 07:12:11 +05303#include "timer.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05305
Gunnar Mills57d9c502018-09-14 14:42:34 -05006#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05307#include <net/if.h>
8#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05309#include <stdlib.h>
10
Ratan Gupta8ab17922017-05-25 13:07:05 +053011#include <exception>
Ratan Gupta255d5142017-08-10 09:02:08 +053012#include <experimental/filesystem>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013#include <phosphor-logging/elog-errors.hpp>
14#include <sdbusplus/bus.hpp>
15
16#include <gtest/gtest.h>
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{
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 public:
28 sdbusplus::bus::bus bus;
29 Manager manager;
30 std::string confDir;
31 TestNetworkManager() :
32 bus(sdbusplus::bus::new_default()),
33 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
34 {
35 setConfDir();
36 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053037
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 ~TestNetworkManager()
39 {
40 if (confDir != "")
Ratan Gupta8ab17922017-05-25 13:07:05 +053041 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 fs::remove_all(confDir);
Ratan Gupta255d5142017-08-10 09:02:08 +053043 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053045
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 void setConfDir()
47 {
48 char tmp[] = "/tmp/NetworkManager.XXXXXX";
49 confDir = mkdtemp(tmp);
50 manager.setConfDir(confDir);
51 }
Ratan Gupta255d5142017-08-10 09:02:08 +053052
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 void createInterfaces()
54 {
55 manager.createInterfaces();
56 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053057
Gunnar Mills57d9c502018-09-14 14:42:34 -050058 int getSize()
59 {
60 return manager.interfaces.size();
61 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053062
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 bool isInterfaceAdded(std::string intf)
64 {
65 return manager.interfaces.find(intf) != manager.interfaces.end()
66 ? true
67 : false;
68 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053069};
70
71// getifaddrs will not return any interface
72TEST_F(TestNetworkManager, NoInterface)
73{
74 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
75 bool caughtException = false;
76 try
77 {
78 createInterfaces();
79 }
80 catch (InternalFailure& e)
81 {
82 caughtException = true;
83 }
84
85 EXPECT_EQ(true, caughtException);
86}
87
88// getifaddrs returns single interface.
89TEST_F(TestNetworkManager, WithSingleInterface)
90{
91 bool caughtException = false;
92 try
93 {
94 // Adds the following ip in the getifaddrs list.
95 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
96 IFF_UP | IFF_RUNNING);
97
98 // Now create the interfaces which will call the mocked getifaddrs
99 // which returns the above interface detail.
100 createInterfaces();
101 EXPECT_EQ(1, getSize());
102 EXPECT_EQ(true, isInterfaceAdded("igb1"));
103 }
104 catch (std::exception& e)
105 {
106 caughtException = true;
107 }
108 EXPECT_EQ(false, caughtException);
109}
110
111// getifaddrs returns two interfaces.
112TEST_F(TestNetworkManager, WithMultipleInterfaces)
113{
114 try
115 {
116 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
117 IFF_UP | IFF_RUNNING);
118
119 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
120 IFF_UP | IFF_RUNNING);
121
122 createInterfaces();
123 EXPECT_EQ(2, getSize());
124 EXPECT_EQ(true, isInterfaceAdded("igb0"));
125 }
126 catch (std::exception& e)
127 {
128 }
129}
130
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131} // namespace network
132} // namespace phosphor