blob: 47ccecf9b89a358f03bef363801434ecaea813e8 [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());
Manojkiran Eda1b5ec9c2020-06-13 15:29:28 +053022extern std::unique_ptr<MockManager> manager;
Ratan Gupta35297172018-11-28 18:40:16 +053023extern std::unique_ptr<Timer> refreshObjectTimer;
24extern std::unique_ptr<Timer> restartTimer;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060025EventPtr eventPtr = nullptr;
26
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060027class TestRtNetlink : public testing::Test
28{
29
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 public:
31 std::string confDir;
32 phosphor::Descriptor smartSock;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060033
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 TestRtNetlink()
35 {
36 manager =
Ratan Gupta35297172018-11-28 18:40:16 +053037 std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp");
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 sd_event* events;
39 sd_event_default(&events);
40 eventPtr.reset(events);
41 events = nullptr;
42 setConfDir();
43 initializeTimers();
44 createNetLinkSocket();
45 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
46 rtnetlink::Server svr(eventPtr, smartSock);
47 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060048
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 ~TestRtNetlink()
50 {
51 if (confDir.empty())
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060052 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 fs::remove_all(confDir);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060054 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060056
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 void setConfDir()
58 {
59 char tmp[] = "/tmp/NetworkManager.XXXXXX";
60 confDir = mkdtemp(tmp);
61 manager->setConfDir(confDir);
62 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060063
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 void createNetLinkSocket()
65 {
66 // RtnetLink socket
67 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
68 smartSock.set(fd);
69 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060070};
71
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060072TEST_F(TestRtNetlink, WithSingleInterface)
73{
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060074 using namespace std::chrono;
William A. Kennington III862275a2019-04-22 20:37:08 -070075 mock_clear();
Andrew Jeffery96281222018-03-06 21:48:11 +103076 // Adds the following ip in the getifaddrs list.
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070077 mock_addIF("igb5", 6);
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING);
Andrew Jeffery96281222018-03-06 21:48:11 +103079 constexpr auto BUFSIZE = 4096;
80 std::array<char, BUFSIZE> msgBuf = {0};
81
82 // point the header and the msg structure pointers into the buffer.
83 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
84 // Length of message
85 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
86 nlMsg->nlmsg_type = RTM_GETADDR;
87 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
88 nlMsg->nlmsg_seq = 0;
89 nlMsg->nlmsg_pid = getpid();
90
Patrick Venture04c3d712019-01-07 13:49:16 -080091 EXPECT_EQ(false, manager->hasInterface("igb5"));
Andrew Jeffery96281222018-03-06 21:48:11 +103092 // Send the request
93 send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0);
94
Andrew Jefferyc5ae81e2018-03-07 15:06:14 +103095 int i = 3;
Andrew Jeffery96281222018-03-06 21:48:11 +103096 while (i--)
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060097 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050098 // wait for timer to expire
99 std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout));
Andrew Jeffery96281222018-03-06 21:48:11 +1030100 sd_event_run(eventPtr.get(), 10);
101 };
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600102
Patrick Venture04c3d712019-01-07 13:49:16 -0800103 EXPECT_EQ(true, manager->hasInterface("igb5"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600104}
105
Gunnar Mills57d9c502018-09-14 14:42:34 -0500106} // namespace network
107} // namespace phosphor