blob: 911a1bddf65bd6580d98d0a062f039faa7b94027 [file] [log] [blame]
Ratan Gupta35297172018-11-28 18:40:16 +05301#include "mock_network_manager.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05302#include "mock_syscall.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05003#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05304
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05306#include <net/if.h>
7#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05308#include <stdlib.h>
9
Ratan Gupta8ab17922017-05-25 13:07:05 +053010#include <exception>
Ratan Gupta255d5142017-08-10 09:02:08 +053011#include <experimental/filesystem>
Gunnar Mills57d9c502018-09-14 14:42:34 -050012#include <sdbusplus/bus.hpp>
13
14#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053015
16namespace phosphor
17{
18namespace network
19{
20
Ratan Gupta35297172018-11-28 18:40:16 +053021std::unique_ptr<Timer> refreshObjectTimer = nullptr;
22std::unique_ptr<Timer> restartTimer = nullptr;
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{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 public:
29 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053030 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 std::string confDir;
32 TestNetworkManager() :
33 bus(sdbusplus::bus::new_default()),
34 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
35 {
36 setConfDir();
37 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053038
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 ~TestNetworkManager()
40 {
41 if (confDir != "")
Ratan Gupta8ab17922017-05-25 13:07:05 +053042 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 fs::remove_all(confDir);
Ratan Gupta255d5142017-08-10 09:02:08 +053044 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053046
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 void setConfDir()
48 {
49 char tmp[] = "/tmp/NetworkManager.XXXXXX";
50 confDir = mkdtemp(tmp);
51 manager.setConfDir(confDir);
52 }
Ratan Gupta255d5142017-08-10 09:02:08 +053053
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 void createInterfaces()
55 {
56 manager.createInterfaces();
57 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053058
Gunnar Mills57d9c502018-09-14 14:42:34 -050059 int getSize()
60 {
61 return manager.interfaces.size();
62 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053063
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 bool isInterfaceAdded(std::string intf)
65 {
66 return manager.interfaces.find(intf) != manager.interfaces.end()
67 ? true
68 : false;
69 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053070};
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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500132} // namespace network
133} // namespace phosphor