Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 1 | #include "types.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 2 | #include "watch.hpp" |
| 3 | |
| 4 | #include <experimental/filesystem> |
| 5 | #include <fstream> |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
Andrew Jeffery | 6b371a1 | 2018-03-13 22:38:17 +1030 | [diff] [blame] | 9 | static constexpr auto TRIGGER_FILE = "/tmp/" __BASE_FILE__ "netif_state"; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 10 | |
| 11 | namespace fs = std::experimental::filesystem; |
| 12 | |
| 13 | class WatchTest : public ::testing::Test |
| 14 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 15 | public: |
| 16 | // systemd event handler |
| 17 | sd_event* events; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 18 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 19 | // Need this so that events can be initialized. |
| 20 | int rc; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 21 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 22 | // 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 Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 38 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | fs::remove(TRIGGER_FILE); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 40 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 41 | } |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 42 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | // unique_ptr for sd_event |
| 44 | phosphor::network::EventPtr eventPtr; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 45 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | // Count of callback invocation |
| 47 | int count = 0; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 48 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 49 | // 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 Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 55 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 56 | // Expect that the file is what we wanted |
| 57 | EXPECT_EQ(file, TRIGGER_FILE); |
| 58 | } |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /** @brief Makes sure that the inotify event is fired |
| 62 | */ |
| 63 | TEST_F(WatchTest, validateEventNotification) |
| 64 | { |
| 65 | // Create a watch object and register the handler |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | phosphor::network::inotify::Watch watch( |
| 67 | eventPtr, TRIGGER_FILE, |
| 68 | std::bind(&WatchTest::callBackHandler, this, std::placeholders::_1)); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 69 | |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | file << "DNS=1.2.3.4\n"; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | sd_event_run(eventPtr.get(), 10); |
| 87 | EXPECT_EQ(2, count); |
| 88 | } |