blob: b68aa448062f6af9bb776937941da87ee63a8bdb [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;
Patrick Ventured94d23e2019-01-07 13:05:23 -080030 Manager 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};
59
60// getifaddrs will not return any interface
61TEST_F(TestNetworkManager, NoInterface)
62{
63 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
64 bool caughtException = false;
65 try
66 {
67 createInterfaces();
68 }
69 catch (InternalFailure& e)
70 {
71 caughtException = true;
72 }
73
74 EXPECT_EQ(true, caughtException);
75}
76
77// getifaddrs returns single interface.
78TEST_F(TestNetworkManager, WithSingleInterface)
79{
80 bool caughtException = false;
81 try
82 {
83 // Adds the following ip in the getifaddrs list.
84 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
85 IFF_UP | IFF_RUNNING);
86
87 // Now create the interfaces which will call the mocked getifaddrs
88 // which returns the above interface detail.
89 createInterfaces();
Patrick Ventured94d23e2019-01-07 13:05:23 -080090 EXPECT_EQ(1, manager.getInterfaceCount());
91 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053092 }
93 catch (std::exception& e)
94 {
95 caughtException = true;
96 }
97 EXPECT_EQ(false, caughtException);
98}
99
100// getifaddrs returns two interfaces.
101TEST_F(TestNetworkManager, WithMultipleInterfaces)
102{
103 try
104 {
105 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
106 IFF_UP | IFF_RUNNING);
107
108 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
109 IFF_UP | IFF_RUNNING);
110
111 createInterfaces();
Patrick Ventured94d23e2019-01-07 13:05:23 -0800112 EXPECT_EQ(2, manager.getInterfaceCount());
113 EXPECT_EQ(true, manager.hasInterface("igb0"));
Ratan Gupta8ab17922017-05-25 13:07:05 +0530114 }
115 catch (std::exception& e)
116 {
117 }
118}
119
Gunnar Mills57d9c502018-09-14 14:42:34 -0500120} // namespace network
121} // namespace phosphor