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> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 13 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | |
| 19 | namespace network |
| 20 | { |
Andrew Jeffery | 4aa1378 | 2018-03-07 14:04:50 +1030 | [diff] [blame] | 21 | sdbusplus::bus::bus bus(sdbusplus::bus::new_default()); |
Manojkiran Eda | 1b5ec9c | 2020-06-13 15:29:28 +0530 | [diff] [blame] | 22 | extern std::unique_ptr<MockManager> manager; |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 23 | extern std::unique_ptr<Timer> refreshObjectTimer; |
| 24 | extern std::unique_ptr<Timer> restartTimer; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 25 | EventPtr eventPtr = nullptr; |
| 26 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 27 | class TestRtNetlink : public testing::Test |
| 28 | { |
| 29 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | public: |
| 31 | std::string confDir; |
| 32 | phosphor::Descriptor smartSock; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 33 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | TestRtNetlink() |
| 35 | { |
| 36 | manager = |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 37 | std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | 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 Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 48 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 49 | ~TestRtNetlink() |
| 50 | { |
| 51 | if (confDir.empty()) |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 52 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | fs::remove_all(confDir); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 54 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 56 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 57 | void setConfDir() |
| 58 | { |
| 59 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 60 | confDir = mkdtemp(tmp); |
| 61 | manager->setConfDir(confDir); |
| 62 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 63 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 64 | void createNetLinkSocket() |
| 65 | { |
| 66 | // RtnetLink socket |
| 67 | auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); |
| 68 | smartSock.set(fd); |
| 69 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 70 | }; |
| 71 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 72 | TEST_F(TestRtNetlink, WithSingleInterface) |
| 73 | { |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 74 | using namespace std::chrono; |
William A. Kennington III | 862275a | 2019-04-22 20:37:08 -0700 | [diff] [blame] | 75 | mock_clear(); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 76 | // Adds the following ip in the getifaddrs list. |
William A. Kennington III | ebb1ad0 | 2019-04-21 18:02:49 -0700 | [diff] [blame] | 77 | mock_addIF("igb5", 6); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 78 | mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 79 | 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 Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 91 | EXPECT_EQ(false, manager->hasInterface("igb5")); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 92 | // Send the request |
| 93 | send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0); |
| 94 | |
Andrew Jeffery | c5ae81e | 2018-03-07 15:06:14 +1030 | [diff] [blame] | 95 | int i = 3; |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 96 | while (i--) |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 97 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 98 | // wait for timer to expire |
| 99 | std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout)); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 100 | sd_event_run(eventPtr.get(), 10); |
| 101 | }; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 102 | |
Patrick Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 103 | EXPECT_EQ(true, manager->hasInterface("igb5")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 104 | } |
| 105 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 106 | } // namespace network |
| 107 | } // namespace phosphor |