blob: 64ca87dfdb86fc434463ea6f43b3ffffec9dded8 [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>
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070011#include <sdbusplus/bus.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:
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() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070033 bus(sdbusplus::bus::new_default()),
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 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;
Patrick Venture8fe594c2019-01-07 13:24:12 -080064 EXPECT_THROW(createInterfaces(), InternalFailure);
Ratan Gupta8ab17922017-05-25 13:07:05 +053065}
66
67// getifaddrs returns single interface.
68TEST_F(TestNetworkManager, WithSingleInterface)
69{
William A. Kennington III8cff3802019-04-21 20:17:27 -070070 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070071
William A. Kennington III8cff3802019-04-21 20:17:27 -070072 // Adds the following ip in the getifaddrs list.
73 mock_addIF("igb1", 2);
74 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053075
William A. Kennington III8cff3802019-04-21 20:17:27 -070076 // Now create the interfaces which will call the mocked getifaddrs
77 // which returns the above interface detail.
78 createInterfaces();
79 EXPECT_EQ(1, manager.getInterfaceCount());
80 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053081}
82
83// getifaddrs returns two interfaces.
84TEST_F(TestNetworkManager, WithMultipleInterfaces)
85{
William A. Kennington III8cff3802019-04-21 20:17:27 -070086 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070087
William A. Kennington III8cff3802019-04-21 20:17:27 -070088 mock_addIF("igb0", 1);
89 mock_addIP("igb0", "192.0.2.2", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053090
William A. Kennington III8cff3802019-04-21 20:17:27 -070091 mock_addIF("igb1", 2);
92 mock_addIP("igb1", "192.0.2.3", "255.255.255.128", IFF_UP | IFF_RUNNING);
Ratan Gupta8ab17922017-05-25 13:07:05 +053093
William A. Kennington III8cff3802019-04-21 20:17:27 -070094 createInterfaces();
95 EXPECT_EQ(2, manager.getInterfaceCount());
96 EXPECT_EQ(true, manager.hasInterface("igb0"));
97 EXPECT_EQ(true, manager.hasInterface("igb1"));
Ratan Gupta8ab17922017-05-25 13:07:05 +053098}
99
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100} // namespace network
101} // namespace phosphor