blob: f1cfa64a32f1f4a4c9b189720524019adb89d9b8 [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"
Ratan Gupta8ab17922017-05-25 13:07:05 +05303
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05305#include <net/if.h>
6#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05307#include <stdlib.h>
8
Ratan Gupta8ab17922017-05-25 13:07:05 +05309#include <exception>
Ratan Gupta255d5142017-08-10 09:02:08 +053010#include <experimental/filesystem>
Patrick Venture3f19a512019-01-07 13:28:57 -080011#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080012#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013
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:
Patrick Venture3f19a512019-01-07 13:28:57 -080029 sdbusplus::SdBusMock sdbus_mock;
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 sdbusplus::bus::bus bus;
Patrick Ventured94d23e2019-01-07 13:05:23 -080031 Manager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 std::string confDir;
33 TestNetworkManager() :
Patrick Venture3f19a512019-01-07 13:28:57 -080034 bus(sdbusplus::get_mocked_new(&sdbus_mock)),
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 manager(bus, "/xyz/openbmc_test/abc", "/tmp")
36 {
37 setConfDir();
38 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 ~TestNetworkManager()
41 {
42 if (confDir != "")
Ratan Gupta8ab17922017-05-25 13:07:05 +053043 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 fs::remove_all(confDir);
Ratan Gupta255d5142017-08-10 09:02:08 +053045 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053047
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 void setConfDir()
49 {
50 char tmp[] = "/tmp/NetworkManager.XXXXXX";
51 confDir = mkdtemp(tmp);
52 manager.setConfDir(confDir);
53 }
Ratan Gupta255d5142017-08-10 09:02:08 +053054
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 void createInterfaces()
56 {
57 manager.createInterfaces();
58 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053059};
60
61// getifaddrs will not return any interface
62TEST_F(TestNetworkManager, NoInterface)
63{
64 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Venture8fe594c2019-01-07 13:24:12 -080065 EXPECT_THROW(createInterfaces(), InternalFailure);
Ratan Gupta8ab17922017-05-25 13:07:05 +053066}
67
68// getifaddrs returns single interface.
69TEST_F(TestNetworkManager, WithSingleInterface)
70{
71 bool caughtException = false;
72 try
73 {
74 // Adds the following ip in the getifaddrs list.
75 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
76 IFF_UP | IFF_RUNNING);
77
78 // Now create the interfaces which will call the mocked getifaddrs
79 // which returns the above interface detail.
80 createInterfaces();
Patrick Ventured94d23e2019-01-07 13:05:23 -080081 EXPECT_EQ(1, manager.getInterfaceCount());
82 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053083 }
84 catch (std::exception& e)
85 {
86 caughtException = true;
87 }
88 EXPECT_EQ(false, caughtException);
89}
90
91// getifaddrs returns two interfaces.
92TEST_F(TestNetworkManager, WithMultipleInterfaces)
93{
94 try
95 {
96 mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
97 IFF_UP | IFF_RUNNING);
98
99 mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
100 IFF_UP | IFF_RUNNING);
101
102 createInterfaces();
Patrick Ventured94d23e2019-01-07 13:05:23 -0800103 EXPECT_EQ(2, manager.getInterfaceCount());
104 EXPECT_EQ(true, manager.hasInterface("igb0"));
Ratan Gupta8ab17922017-05-25 13:07:05 +0530105 }
106 catch (std::exception& e)
107 {
108 }
109}
110
Gunnar Mills57d9c502018-09-14 14:42:34 -0500111} // namespace network
112} // namespace phosphor