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()); |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 22 | std::unique_ptr<MockManager> manager = nullptr; |
| 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 | |
| 27 | /** @brief refresh the network objects. */ |
| 28 | void refreshObjects() |
| 29 | { |
| 30 | |
| 31 | if (manager) |
| 32 | { |
| 33 | manager->createChildObjects(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void initializeTimers() |
| 38 | { |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 39 | refreshObjectTimer = std::make_unique<Timer>( |
| 40 | sdeventplus::Event::get_default(), std::bind(refreshObjects)); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | class TestRtNetlink : public testing::Test |
| 44 | { |
| 45 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | public: |
| 47 | std::string confDir; |
| 48 | phosphor::Descriptor smartSock; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 49 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 50 | TestRtNetlink() |
| 51 | { |
| 52 | manager = |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 53 | std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp"); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 54 | sd_event* events; |
| 55 | sd_event_default(&events); |
| 56 | eventPtr.reset(events); |
| 57 | events = nullptr; |
| 58 | setConfDir(); |
| 59 | initializeTimers(); |
| 60 | createNetLinkSocket(); |
| 61 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 62 | rtnetlink::Server svr(eventPtr, smartSock); |
| 63 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 64 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 65 | ~TestRtNetlink() |
| 66 | { |
| 67 | if (confDir.empty()) |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 68 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 69 | fs::remove_all(confDir); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 70 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 71 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 72 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | void setConfDir() |
| 74 | { |
| 75 | char tmp[] = "/tmp/NetworkManager.XXXXXX"; |
| 76 | confDir = mkdtemp(tmp); |
| 77 | manager->setConfDir(confDir); |
| 78 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | void createNetLinkSocket() |
| 81 | { |
| 82 | // RtnetLink socket |
| 83 | auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); |
| 84 | smartSock.set(fd); |
| 85 | } |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 86 | }; |
| 87 | |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 88 | TEST_F(TestRtNetlink, WithSingleInterface) |
| 89 | { |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 90 | using namespace std::chrono; |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 91 | // Adds the following ip in the getifaddrs list. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | 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] | 93 | constexpr auto BUFSIZE = 4096; |
| 94 | std::array<char, BUFSIZE> msgBuf = {0}; |
| 95 | |
| 96 | // point the header and the msg structure pointers into the buffer. |
| 97 | auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data()); |
| 98 | // Length of message |
| 99 | nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg)); |
| 100 | nlMsg->nlmsg_type = RTM_GETADDR; |
| 101 | nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; |
| 102 | nlMsg->nlmsg_seq = 0; |
| 103 | nlMsg->nlmsg_pid = getpid(); |
| 104 | |
Patrick Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 105 | EXPECT_EQ(false, manager->hasInterface("igb5")); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 106 | // Send the request |
| 107 | send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0); |
| 108 | |
Andrew Jeffery | c5ae81e | 2018-03-07 15:06:14 +1030 | [diff] [blame] | 109 | int i = 3; |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 110 | while (i--) |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 111 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 112 | // wait for timer to expire |
| 113 | std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout)); |
Andrew Jeffery | 9628122 | 2018-03-06 21:48:11 +1030 | [diff] [blame] | 114 | sd_event_run(eventPtr.get(), 10); |
| 115 | }; |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 116 | |
Patrick Venture | 04c3d71 | 2019-01-07 13:49:16 -0800 | [diff] [blame] | 117 | EXPECT_EQ(true, manager->hasInterface("igb5")); |
Nagaraju Goruganti | 067ca2d | 2018-01-17 01:12:00 -0600 | [diff] [blame] | 118 | } |
| 119 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 120 | } // namespace network |
| 121 | } // namespace phosphor |