blob: 167c903e1ec81a49d0bc0732d1a002ee7ca60c3b [file] [log] [blame]
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +05301#include "types.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002#include "watch.hpp"
3
4#include <experimental/filesystem>
5#include <fstream>
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +05306
7#include <gtest/gtest.h>
8
Andrew Jeffery6b371a12018-03-13 22:38:17 +10309static constexpr auto TRIGGER_FILE = "/tmp/" __BASE_FILE__ "netif_state";
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053010
11namespace fs = std::experimental::filesystem;
12
13class WatchTest : public ::testing::Test
14{
Gunnar Mills57d9c502018-09-14 14:42:34 -050015 public:
16 // systemd event handler
17 sd_event* events;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053018
Gunnar Mills57d9c502018-09-14 14:42:34 -050019 // Need this so that events can be initialized.
20 int rc;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053021
Gunnar Mills57d9c502018-09-14 14:42:34 -050022 // Gets called as part of each TEST_F construction
23 WatchTest() : rc(sd_event_default(&events)), eventPtr(events)
24 {
25 // Create a file containing DNS entries like in netif/state
26 std::ofstream file(TRIGGER_FILE);
27 file << "";
28
29 // Check for successful creation of
30 // event handler
31 EXPECT_GE(rc, 0);
32 }
33
34 // Gets called as part of each TEST_F destruction
35 ~WatchTest()
36 {
37 if (fs::exists(TRIGGER_FILE))
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053038 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 fs::remove(TRIGGER_FILE);
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053040 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 }
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053042
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 // unique_ptr for sd_event
44 phosphor::network::EventPtr eventPtr;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053045
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 // Count of callback invocation
47 int count = 0;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053048
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 // This is supposed to get hit twice
50 // Once at the beginning to see if there is anything
51 // and the second time when the data is fired.
52 void callBackHandler(const fs::path& file)
53 {
54 count++;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053055
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 // Expect that the file is what we wanted
57 EXPECT_EQ(file, TRIGGER_FILE);
58 }
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053059};
60
61/** @brief Makes sure that the inotify event is fired
62 */
63TEST_F(WatchTest, validateEventNotification)
64{
65 // Create a watch object and register the handler
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 phosphor::network::inotify::Watch watch(
67 eventPtr, TRIGGER_FILE,
68 std::bind(&WatchTest::callBackHandler, this, std::placeholders::_1));
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053069
70 // Reading the event post subscription
71 callBackHandler(TRIGGER_FILE);
72
73 // Callback function must have hit by now
74 EXPECT_EQ(1, count);
75
76 // Make a run and see that no changes
77 sd_event_run(eventPtr.get(), 10);
78 EXPECT_EQ(1, count);
79
80 // Pump the data and get notification
81 {
82 std::ofstream file(TRIGGER_FILE);
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 file << "DNS=1.2.3.4\n";
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053084 }
85
86 sd_event_run(eventPtr.get(), 10);
87 EXPECT_EQ(2, count);
88}