Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 1 | #include "mock_network_manager.hpp" |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 2 | #include "mock_syscall.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 3 | #include "rtnetlink_server.hpp" |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 4 | #include "types.hpp" |
| 5 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 6 | #include <linux/rtnetlink.h> |
| 7 | #include <net/if.h> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 8 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 9 | #include <chrono> |
| 10 | #include <functional> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 12 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 13 | #include <testutil.hpp> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 14 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | |
| 20 | namespace network |
| 21 | { |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 22 | sdbusplus::bus_t bus(sdbusplus::bus::new_default()); |
Manojkiran Eda | 1b5ec9c | 2020-06-13 15:29:28 +0530 | [diff] [blame] | 23 | extern std::unique_ptr<MockManager> manager; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 24 | extern std::unique_ptr<Timer> refreshObjectTimer; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 25 | EventPtr eventPtr = nullptr; |
| 26 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 27 | class TestRtNetlink : public TestWithTmp |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 28 | { |
| 29 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | public: |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 31 | std::optional<rtnetlink::Server> svr; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 32 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 33 | TestRtNetlink() |
| 34 | { |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 35 | manager = std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", |
| 36 | CaseTmpDir()); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | sd_event* events; |
| 38 | sd_event_default(&events); |
| 39 | eventPtr.reset(events); |
| 40 | events = nullptr; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 41 | initializeTimers(); |
| 42 | createNetLinkSocket(); |
| 43 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 44 | svr.emplace(eventPtr); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 45 | } |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 46 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 47 | void createNetLinkSocket() |
| 48 | { |
| 49 | // RtnetLink socket |
| 50 | auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); |
| 51 | smartSock.set(fd); |
| 52 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 53 | }; |
| 54 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 55 | TEST_F(TestRtNetlink, WithSingleInterface) |
| 56 | { |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 57 | using namespace std::chrono; |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 58 | mock_clear(); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 59 | // Adds the following ip in the getifaddrs list. |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 60 | mock_addIF("igb5", /*idx=*/6); |
| 61 | mock_addIP("igb5", "127.0.0.1", "255.255.255.128"); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 62 | 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 Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 74 | EXPECT_EQ(false, manager->hasInterface("igb5")); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 75 | // Send the request |
William A. Kennington III | 32eef71 | 2022-01-24 17:03:03 -0800 | [diff] [blame] | 76 | send(svr->getSock(), nlMsg, nlMsg->nlmsg_len, 0); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 77 | |
Andrew Jeffery | c5ae81e | 2018-03-07 15:06:14 +1030 | [diff] [blame] | 78 | int i = 3; |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 79 | while (i--) |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 80 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 81 | // wait for timer to expire |
| 82 | std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout)); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 83 | sd_event_run(eventPtr.get(), 10); |
| 84 | }; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 85 | |
Patrick Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 86 | EXPECT_EQ(true, manager->hasInterface("igb5")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 87 | } |
| 88 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 89 | } // namespace network |
| 90 | } // namespace phosphor |