blob: d97b182b3d829f3eea24d799f7b0b9bd2aad3ebb [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"
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 <phosphor-logging/elog-errors.hpp>
13#include <sdbusplus/bus.hpp>
14
15#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053016
17namespace phosphor
18{
19namespace network
20{
21
Ratan Gupta255d5142017-08-10 09:02:08 +053022namespace fs = std::experimental::filesystem;
23
Ratan Gupta8ab17922017-05-25 13:07:05 +053024class TestNetworkManager : public testing::Test
25{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
27 sdbusplus::bus::bus bus;
28 Manager manager;
29 std::string confDir;
30 TestNetworkManager() :
31 bus(sdbusplus::bus::new_default()),
32 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
33 {
34 setConfDir();
35 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053036
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 ~TestNetworkManager()
38 {
39 if (confDir != "")
Ratan Gupta8ab17922017-05-25 13:07:05 +053040 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 fs::remove_all(confDir);
Ratan Gupta255d5142017-08-10 09:02:08 +053042 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053044
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 void setConfDir()
46 {
47 char tmp[] = "/tmp/NetworkManager.XXXXXX";
48 confDir = mkdtemp(tmp);
49 manager.setConfDir(confDir);
50 }
Ratan Gupta255d5142017-08-10 09:02:08 +053051
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 void createInterfaces()
53 {
54 manager.createInterfaces();
55 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053056
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 int getSize()
58 {
59 return manager.interfaces.size();
60 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053061
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 bool isInterfaceAdded(std::string intf)
63 {
64 return manager.interfaces.find(intf) != manager.interfaces.end()
65 ? true
66 : false;
67 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053068};
69
70// getifaddrs will not return any interface
71TEST_F(TestNetworkManager, NoInterface)
72{
73 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
74 bool caughtException = false;
75 try
76 {
77 createInterfaces();
78 }
79 catch (InternalFailure& e)
80 {
81 caughtException = true;
82 }
83
84 EXPECT_EQ(true, caughtException);
85}
86
87// getifaddrs returns single interface.
88TEST_F(TestNetworkManager, WithSingleInterface)
89{
90 bool caughtException = false;
91 try
92 {
93 // Adds the following ip in the getifaddrs list.
94 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
95 IFF_UP | IFF_RUNNING);
96
97 // Now create the interfaces which will call the mocked getifaddrs
98 // which returns the above interface detail.
99 createInterfaces();
100 EXPECT_EQ(1, getSize());
101 EXPECT_EQ(true, isInterfaceAdded("igb1"));
102 }
103 catch (std::exception& e)
104 {
105 caughtException = true;
106 }
107 EXPECT_EQ(false, caughtException);
108}
109
110// getifaddrs returns two interfaces.
111TEST_F(TestNetworkManager, WithMultipleInterfaces)
112{
113 try
114 {
115 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
116 IFF_UP | IFF_RUNNING);
117
118 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
119 IFF_UP | IFF_RUNNING);
120
121 createInterfaces();
122 EXPECT_EQ(2, getSize());
123 EXPECT_EQ(true, isInterfaceAdded("igb0"));
124 }
125 catch (std::exception& e)
126 {
127 }
128}
129
Gunnar Mills57d9c502018-09-14 14:42:34 -0500130} // namespace network
131} // namespace phosphor