blob: 203019dccbb519c9a3033dac5233c5da86337edd [file] [log] [blame]
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -06001#include "types.hpp"
2#include "rtnetlink_server.hpp"
3#include "network_manager.hpp"
4#include "mock_syscall.hpp"
5#include "timer.hpp"
6#include "types.hpp"
7
8
9#include <gtest/gtest.h>
10#include <sdbusplus/bus.hpp>
11
12#include <net/if.h>
13#include <linux/rtnetlink.h>
14
15namespace phosphor
16{
17
18namespace network
19{
Andrew Jeffery4aa13782018-03-07 14:04:50 +103020sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060021std::unique_ptr<Manager> manager = nullptr;
22std::unique_ptr<Timer> refreshObjectTimer = nullptr;
23std::unique_ptr<Timer> restartTimer = nullptr;
24EventPtr eventPtr = nullptr;
25
26/** @brief refresh the network objects. */
27void refreshObjects()
28{
29
30 if (manager)
31 {
32 manager->createChildObjects();
33 }
34}
35
36void initializeTimers()
37{
38 std::function<void()> refreshFunc(
39 std::bind(&refreshObjects));
40
41 refreshObjectTimer =
42 std::make_unique<Timer>(refreshFunc);
43
44}
45
46class TestRtNetlink : public testing::Test
47{
48
49 public:
50 std::string confDir;
51 phosphor::Descriptor smartSock;
52
53
54
55 TestRtNetlink()
56 {
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060057 manager =
58 std::make_unique<Manager>(bus,
59 "/xyz/openbmc_test/bcd",
60 "/tmp");
61 sd_event* events;
62 sd_event_default(&events);
63 eventPtr.reset(events);
64 events = nullptr;
65 setConfDir();
66 initializeTimers();
67 createNetLinkSocket();
68 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
69 rtnetlink::Server svr(eventPtr, smartSock);
70 }
71
72 ~TestRtNetlink()
73 {
74 if (confDir.empty())
75 {
76 fs::remove_all(confDir);
77 }
78 }
79
80 void setConfDir()
81 {
Andrew Jeffery6b371a12018-03-13 22:38:17 +103082 char tmp[] = "/tmp/NetworkManager.XXXXXX";
83 confDir = mkdtemp(tmp);
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060084 manager->setConfDir(confDir);
85 }
86
87 bool isInterfaceAdded(std::string intf)
88 {
89 return manager->interfaces.find(intf) != manager->interfaces.end()?
90 true :
91 false;
92 }
93
94 void createNetLinkSocket()
95 {
96 //RtnetLink socket
97 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK,
98 NETLINK_ROUTE);
99 smartSock.set(fd);
100 }
101};
102
103
104TEST_F(TestRtNetlink, WithSingleInterface)
105{
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600106 using namespace std::chrono;
Andrew Jeffery96281222018-03-06 21:48:11 +1030107 // Adds the following ip in the getifaddrs list.
108 mock_addIP("igb5", "127.0.0.1", "255.255.255.128",
109 IFF_UP | IFF_RUNNING);
110 constexpr auto BUFSIZE = 4096;
111 std::array<char, BUFSIZE> msgBuf = {0};
112
113 // point the header and the msg structure pointers into the buffer.
114 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
115 // Length of message
116 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
117 nlMsg->nlmsg_type = RTM_GETADDR;
118 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
119 nlMsg->nlmsg_seq = 0;
120 nlMsg->nlmsg_pid = getpid();
121
122 EXPECT_EQ(false, isInterfaceAdded("igb5"));
123 // Send the request
124 send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0);
125
Andrew Jefferyc5ae81e2018-03-07 15:06:14 +1030126 int i = 3;
Andrew Jeffery96281222018-03-06 21:48:11 +1030127 while (i--)
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600128 {
Andrew Jeffery96281222018-03-06 21:48:11 +1030129 //wait for timer to expire
130 std::this_thread::sleep_for(
131 std::chrono::milliseconds(refreshTimeout));
132 sd_event_run(eventPtr.get(), 10);
133 };
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600134
Andrew Jeffery96281222018-03-06 21:48:11 +1030135 EXPECT_EQ(true, isInterfaceAdded("igb5"));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -0600136}
137
138}// namespce network
139}// namespace phosphor