blob: b11100fe71d95d861a47970c98349c5a4d86aaeb [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>
William A. Kennington III84bfe672022-07-13 14:15:30 -070013#include <testutil.hpp>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060014
Gunnar Mills57d9c502018-09-14 14:42:34 -050015#include <gtest/gtest.h>
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060016
17namespace phosphor
18{
19
20namespace network
21{
Patrick Williamsc38b0712022-07-22 19:26:54 -050022sdbusplus::bus_t bus(sdbusplus::bus::new_default());
Manojkiran Eda1b5ec9c2020-06-13 15:29:28 +053023extern std::unique_ptr<MockManager> manager;
Ratan Gupta35297172018-11-28 18:40:16 +053024extern std::unique_ptr<Timer> refreshObjectTimer;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060025EventPtr eventPtr = nullptr;
26
William A. Kennington III84bfe672022-07-13 14:15:30 -070027class TestRtNetlink : public TestWithTmp
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060028{
29
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 public:
William A. Kennington III32eef712022-01-24 17:03:03 -080031 std::optional<rtnetlink::Server> svr;
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060032
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 TestRtNetlink()
34 {
William A. Kennington III84bfe672022-07-13 14:15:30 -070035 manager = std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd",
36 CaseTmpDir());
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 sd_event* events;
38 sd_event_default(&events);
39 eventPtr.reset(events);
40 events = nullptr;
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 initializeTimers();
42 createNetLinkSocket();
43 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
William A. Kennington III32eef712022-01-24 17:03:03 -080044 svr.emplace(eventPtr);
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 }
William A. Kennington III32eef712022-01-24 17:03:03 -080046
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 void createNetLinkSocket()
48 {
49 // RtnetLink socket
50 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
51 smartSock.set(fd);
52 }
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060053};
54
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060055TEST_F(TestRtNetlink, WithSingleInterface)
56{
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060057 using namespace std::chrono;
William A. Kennington III862275a2019-04-22 20:37:08 -070058 mock_clear();
Andrew Jeffery96281222018-03-06 21:48:11 +103059 // Adds the following ip in the getifaddrs list.
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070060 mock_addIF("igb5", /*idx=*/6);
61 mock_addIP("igb5", "127.0.0.1", "255.255.255.128");
Andrew Jeffery96281222018-03-06 21:48:11 +103062 constexpr auto BUFSIZE = 4096;
63 std::array<char, BUFSIZE> msgBuf = {0};
64
65 // point the header and the msg structure pointers into the buffer.
66 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
67 // Length of message
68 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
69 nlMsg->nlmsg_type = RTM_GETADDR;
70 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
71 nlMsg->nlmsg_seq = 0;
72 nlMsg->nlmsg_pid = getpid();
73
Patrick Venture04c3d712019-01-07 13:49:16 -080074 EXPECT_EQ(false, manager->hasInterface("igb5"));
Andrew Jeffery96281222018-03-06 21:48:11 +103075 // Send the request
William A. Kennington III32eef712022-01-24 17:03:03 -080076 send(svr->getSock(), nlMsg, nlMsg->nlmsg_len, 0);
Andrew Jeffery96281222018-03-06 21:48:11 +103077
Andrew Jefferyc5ae81e2018-03-07 15:06:14 +103078 int i = 3;
Andrew Jeffery96281222018-03-06 21:48:11 +103079 while (i--)
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060080 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 // wait for timer to expire
82 std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout));
Andrew Jeffery96281222018-03-06 21:48:11 +103083 sd_event_run(eventPtr.get(), 10);
84 };
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060085
Patrick Venture04c3d712019-01-07 13:49:16 -080086 EXPECT_EQ(true, manager->hasInterface("igb5"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060087}
88
Gunnar Mills57d9c502018-09-14 14:42:34 -050089} // namespace network
90} // namespace phosphor