blob: 52008ba764beb584916baf084023016ab472335a [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{
20
21std::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 {
57 sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
58 manager =
59 std::make_unique<Manager>(bus,
60 "/xyz/openbmc_test/bcd",
61 "/tmp");
62 sd_event* events;
63 sd_event_default(&events);
64 eventPtr.reset(events);
65 events = nullptr;
66 setConfDir();
67 initializeTimers();
68 createNetLinkSocket();
69 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
70 rtnetlink::Server svr(eventPtr, smartSock);
71 }
72
73 ~TestRtNetlink()
74 {
75 if (confDir.empty())
76 {
77 fs::remove_all(confDir);
78 }
79 }
80
81 void setConfDir()
82 {
83 confDir = "/tmp/NetworkManager.YYYY";
84 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{
106 bool caughtException = false;
107 using namespace std::chrono;
108 try
109 {
110 // Adds the following ip in the getifaddrs list.
111 mock_addIP("igb5", "127.0.0.1", "255.255.255.128",
112 IFF_UP | IFF_RUNNING);
113 constexpr auto BUFSIZE = 4096;
114 std::array<char, BUFSIZE> msgBuf = {0};
115
116 // point the header and the msg structure pointers into the buffer.
117 auto nlMsg = reinterpret_cast<nlmsghdr*>(msgBuf.data());
118 // Length of message
119 nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
120 nlMsg->nlmsg_type = RTM_GETADDR;
121 nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
122 nlMsg->nlmsg_seq = 0;
123 nlMsg->nlmsg_pid = getpid();
124
125 EXPECT_EQ(false, isInterfaceAdded("igb5"));
126 // Send the request
127 send(smartSock(), nlMsg, nlMsg->nlmsg_len, 0);
128
129 int i = 2;
130 while (i--)
131 {
132 //wait for timer to expire
133 std::this_thread::sleep_for(
134 std::chrono::milliseconds(refreshTimeout));
135 sd_event_run(eventPtr.get(), 10);
136 };
137
138 EXPECT_EQ(true, isInterfaceAdded("igb5"));
139 }
140 catch (std::exception& e)
141 {
142 caughtException = true;
143 }
144 EXPECT_EQ(false, caughtException);
145}
146
147}// namespce network
148}// namespace phosphor