blob: c6a5f8ffa2aba8f164aada2b5985b55d7c4f3b15 [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;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060024EventPtr eventPtr = nullptr;
25
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060026class TestRtNetlink : public testing::Test
27{
28
Gunnar Mills57d9c502018-09-14 14:42:34 -050029 public:
30 std::string confDir;
31 phosphor::Descriptor smartSock;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060032
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 TestRtNetlink()
34 {
35 manager =
Ratan Gupta35297172018-11-28 18:40:16 +053036 std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp");
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 sd_event* events;
38 sd_event_default(&events);
39 eventPtr.reset(events);
40 events = nullptr;
41 setConfDir();
42 initializeTimers();
43 createNetLinkSocket();
44 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
45 rtnetlink::Server svr(eventPtr, smartSock);
46 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060047
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 ~TestRtNetlink()
49 {
50 if (confDir.empty())
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060051 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 fs::remove_all(confDir);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060053 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060055
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 void setConfDir()
57 {
58 char tmp[] = "/tmp/NetworkManager.XXXXXX";
59 confDir = mkdtemp(tmp);
60 manager->setConfDir(confDir);
61 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060062
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 void createNetLinkSocket()
64 {
65 // RtnetLink socket
66 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
67 smartSock.set(fd);
68 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060069};
70
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060071TEST_F(TestRtNetlink, WithSingleInterface)
72{
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060073 using namespace std::chrono;
William A. Kennington III862275a2019-04-22 20:37:08 -070074 mock_clear();
Andrew Jeffery96281222018-03-06 21:48:11 +103075 // Adds the following ip in the getifaddrs list.
William A. Kennington IIIebb1ad02019-04-21 18:02:49 -070076 mock_addIF("igb5", 6);
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING);
Andrew Jeffery96281222018-03-06 21:48:11 +103078 constexpr auto BUFSIZE = 4096;
79 std::array<char, BUFSIZE> msgBuf = {0};
80
81 // point the header and the msg structure pointers into the buffer.
82 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
83 // Length of message
84 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
85 nlMsg->nlmsg_type = RTM_GETADDR;
86 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
87 nlMsg->nlmsg_seq = 0;
88 nlMsg->nlmsg_pid = getpid();
89
Patrick Venture04c3d712019-01-07 13:49:16 -080090 EXPECT_EQ(false, manager->hasInterface("igb5"));
Andrew Jeffery96281222018-03-06 21:48:11 +103091 // Send the request
92 send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0);
93
Andrew Jefferyc5ae81e2018-03-07 15:06:14 +103094 int i = 3;
Andrew Jeffery96281222018-03-06 21:48:11 +103095 while (i--)
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060096 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 // wait for timer to expire
98 std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout));
Andrew Jeffery96281222018-03-06 21:48:11 +103099 sd_event_run(eventPtr.get(), 10);
100 };
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600101
Patrick Venture04c3d712019-01-07 13:49:16 -0800102 EXPECT_EQ(true, manager->hasInterface("igb5"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600103}
104
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105} // namespace network
106} // namespace phosphor