blob: 92846005ed71d8468ecdd902db39bed68e3bd125 [file] [log] [blame]
Ratan Gupta35297172018-11-28 18:40:16 +05301#include "mock_network_manager.hpp"
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -06002#include "mock_syscall.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05003#include "rtnetlink_server.hpp"
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -06004#include "types.hpp"
5
Gunnar Mills57d9c502018-09-14 14:42:34 -05006#include <linux/rtnetlink.h>
7#include <net/if.h>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -06008
William A. Kennington III3a70fa22018-09-20 18:48:20 -07009#include <chrono>
10#include <functional>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060011#include <sdbusplus/bus.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070012#include <sdeventplus/event.hpp>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060013
Gunnar Mills57d9c502018-09-14 14:42:34 -050014#include <gtest/gtest.h>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060015
16namespace phosphor
17{
18
19namespace network
20{
Andrew Jeffery4aa13782018-03-07 14:04:50 +103021sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
Ratan Gupta35297172018-11-28 18:40:16 +053022std::unique_ptr<MockManager> manager = nullptr;
23extern std::unique_ptr<Timer> refreshObjectTimer;
24extern std::unique_ptr<Timer> restartTimer;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060025EventPtr eventPtr = nullptr;
26
27/** @brief refresh the network objects. */
28void refreshObjects()
29{
30
31 if (manager)
32 {
33 manager->createChildObjects();
34 }
35}
36
37void initializeTimers()
38{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070039 refreshObjectTimer = std::make_unique<Timer>(
40 sdeventplus::Event::get_default(), std::bind(refreshObjects));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060041}
42
43class TestRtNetlink : public testing::Test
44{
45
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 public:
47 std::string confDir;
48 phosphor::Descriptor smartSock;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060049
Gunnar Mills57d9c502018-09-14 14:42:34 -050050 TestRtNetlink()
51 {
52 manager =
Ratan Gupta35297172018-11-28 18:40:16 +053053 std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp");
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 sd_event* events;
55 sd_event_default(&events);
56 eventPtr.reset(events);
57 events = nullptr;
58 setConfDir();
59 initializeTimers();
60 createNetLinkSocket();
61 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
62 rtnetlink::Server svr(eventPtr, smartSock);
63 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060064
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 ~TestRtNetlink()
66 {
67 if (confDir.empty())
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060068 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 fs::remove_all(confDir);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060070 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060072
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 void setConfDir()
74 {
75 char tmp[] = "/tmp/NetworkManager.XXXXXX";
76 confDir = mkdtemp(tmp);
77 manager->setConfDir(confDir);
78 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060079
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 bool isInterfaceAdded(std::string intf)
81 {
82 return manager->interfaces.find(intf) != manager->interfaces.end()
83 ? true
84 : false;
85 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060086
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 void createNetLinkSocket()
88 {
89 // RtnetLink socket
90 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
91 smartSock.set(fd);
92 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060093};
94
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060095TEST_F(TestRtNetlink, WithSingleInterface)
96{
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060097 using namespace std::chrono;
Andrew Jeffery96281222018-03-06 21:48:11 +103098 // Adds the following ip in the getifaddrs list.
Gunnar Mills57d9c502018-09-14 14:42:34 -050099 mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING);
Andrew Jeffery96281222018-03-06 21:48:11 +1030100 constexpr auto BUFSIZE = 4096;
101 std::array<char, BUFSIZE> msgBuf = {0};
102
103 // point the header and the msg structure pointers into the buffer.
104 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
105 // Length of message
106 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
107 nlMsg->nlmsg_type = RTM_GETADDR;
108 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
109 nlMsg->nlmsg_seq = 0;
110 nlMsg->nlmsg_pid = getpid();
111
112 EXPECT_EQ(false, isInterfaceAdded("igb5"));
113 // Send the request
114 send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0);
115
Andrew Jefferyc5ae81e2018-03-07 15:06:14 +1030116 int i = 3;
Andrew Jeffery96281222018-03-06 21:48:11 +1030117 while (i--)
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600118 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119 // wait for timer to expire
120 std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout));
Andrew Jeffery96281222018-03-06 21:48:11 +1030121 sd_event_run(eventPtr.get(), 10);
122 };
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600123
Andrew Jeffery96281222018-03-06 21:48:11 +1030124 EXPECT_EQ(true, isInterfaceAdded("igb5"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600125}
126
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127} // namespace network
128} // namespace phosphor