Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame^] | 1 | #include <sys/wait.h> |
| 2 | #include <systemd/sd-event.h> |
| 3 | |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 4 | #include <sdeventplus/event.hpp> |
| 5 | #include <sdeventplus/exception.hpp> |
| 6 | #include <sdeventplus/source/child.hpp> |
| 7 | #include <sdeventplus/test/sdevent.hpp> |
Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame^] | 8 | |
| 9 | #include <cerrno> |
| 10 | #include <functional> |
| 11 | #include <memory> |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 12 | #include <type_traits> |
| 13 | #include <utility> |
| 14 | |
Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame^] | 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 18 | namespace sdeventplus |
| 19 | { |
| 20 | namespace source |
| 21 | { |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | using testing::DoAll; |
| 26 | using testing::Return; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 27 | using testing::ReturnPointee; |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 28 | using testing::SaveArg; |
| 29 | using testing::SetArgPointee; |
| 30 | |
| 31 | using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>; |
| 32 | |
| 33 | class ChildTest : public testing::Test |
| 34 | { |
| 35 | protected: |
| 36 | testing::StrictMock<test::SdEventMock> mock; |
| 37 | sd_event_source* const expected_source = |
| 38 | reinterpret_cast<sd_event_source*>(1234); |
| 39 | sd_event* const expected_event = reinterpret_cast<sd_event*>(2345); |
| 40 | UniqueEvent event = make_event(expected_event); |
| 41 | |
| 42 | UniqueEvent make_event(sd_event* event) |
| 43 | { |
| 44 | auto deleter = [this, event](Event* e) { |
| 45 | EXPECT_CALL(this->mock, sd_event_unref(event)) |
| 46 | .WillOnce(Return(nullptr)); |
| 47 | delete e; |
| 48 | }; |
| 49 | return UniqueEvent(new Event(event, std::false_type(), &mock), deleter); |
| 50 | } |
| 51 | |
| 52 | void expect_destruct() |
| 53 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 54 | EXPECT_CALL(mock, sd_event_source_unref(expected_source)) |
| 55 | .WillOnce(Return(nullptr)); |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 56 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 57 | .WillOnce(Return(nullptr)); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | TEST_F(ChildTest, ConstructSuccess) |
| 62 | { |
| 63 | const pid_t pid = 50; |
| 64 | const int options = WEXITED; |
| 65 | |
| 66 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 67 | .WillOnce(Return(expected_event)); |
| 68 | sd_event_child_handler_t handler; |
| 69 | EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid, |
| 70 | options, testing::_, nullptr)) |
| 71 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<4>(&handler), |
| 72 | Return(0))); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 73 | sd_event_destroy_t destroy; |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 74 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 75 | { |
| 76 | testing::InSequence seq; |
| 77 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 78 | testing::_)) |
| 79 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 80 | EXPECT_CALL(mock, |
| 81 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 82 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 83 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 84 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 85 | } |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 86 | int completions = 0; |
| 87 | const siginfo_t* return_si; |
| 88 | Child::Callback callback = [&](Child&, const siginfo_t* si) { |
| 89 | return_si = si; |
| 90 | completions++; |
| 91 | }; |
| 92 | Child child(*event, pid, options, std::move(callback)); |
| 93 | EXPECT_FALSE(callback); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 94 | EXPECT_NE(&child, userdata); |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 95 | EXPECT_EQ(0, completions); |
| 96 | |
| 97 | const siginfo_t* expected_si = reinterpret_cast<siginfo_t*>(865); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 98 | EXPECT_EQ(0, handler(nullptr, expected_si, userdata)); |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 99 | EXPECT_EQ(1, completions); |
| 100 | EXPECT_EQ(expected_si, return_si); |
| 101 | |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 102 | child.set_callback(std::bind([]() {})); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 103 | EXPECT_EQ(0, handler(nullptr, expected_si, userdata)); |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 104 | EXPECT_EQ(1, completions); |
| 105 | |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 106 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 107 | destroy(userdata); |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST_F(ChildTest, ConstructError) |
| 111 | { |
| 112 | const pid_t pid = 50; |
| 113 | const int options = WEXITED; |
| 114 | |
| 115 | EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid, |
| 116 | options, testing::_, nullptr)) |
| 117 | .WillOnce(Return(-EINVAL)); |
| 118 | int completions = 0; |
| 119 | Child::Callback callback = [&completions](Child&, const siginfo_t*) { |
| 120 | completions++; |
| 121 | }; |
| 122 | EXPECT_THROW(Child(*event, pid, options, std::move(callback)), |
| 123 | SdEventError); |
| 124 | EXPECT_TRUE(callback); |
| 125 | EXPECT_EQ(0, completions); |
| 126 | } |
| 127 | |
| 128 | class ChildMethodTest : public ChildTest |
| 129 | { |
| 130 | protected: |
| 131 | std::unique_ptr<Child> child; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 132 | sd_event_destroy_t destroy; |
| 133 | void* userdata; |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 134 | |
| 135 | void SetUp() |
| 136 | { |
| 137 | const pid_t pid = 50; |
| 138 | const int options = WEXITED; |
| 139 | |
| 140 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 141 | .WillOnce(Return(expected_event)); |
| 142 | EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid, |
| 143 | options, testing::_, nullptr)) |
| 144 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0))); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 145 | { |
| 146 | testing::InSequence seq; |
| 147 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback( |
| 148 | expected_source, testing::_)) |
| 149 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 150 | EXPECT_CALL( |
| 151 | mock, sd_event_source_set_userdata(expected_source, testing::_)) |
| 152 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 153 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 154 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 155 | } |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 156 | child = std::make_unique<Child>(*event, pid, options, |
| 157 | [](Child&, const siginfo_t*) {}); |
| 158 | } |
| 159 | |
| 160 | void TearDown() |
| 161 | { |
| 162 | expect_destruct(); |
| 163 | child.reset(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 164 | destroy(userdata); |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | |
William A. Kennington III | 56dc78b | 2019-03-29 21:09:13 -0700 | [diff] [blame] | 168 | TEST_F(ChildMethodTest, Copy) |
| 169 | { |
| 170 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 171 | .WillOnce(Return(expected_event)); |
| 172 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 173 | .WillOnce(Return(expected_source)); |
| 174 | auto child2 = std::make_unique<Child>(*child); |
| 175 | { |
| 176 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 177 | .WillOnce(Return(expected_event)); |
| 178 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 179 | .WillOnce(Return(expected_source)); |
| 180 | Child child3(*child); |
| 181 | |
| 182 | expect_destruct(); |
| 183 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 184 | .WillOnce(Return(expected_event)); |
| 185 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 186 | .WillOnce(Return(expected_source)); |
| 187 | *child2 = child3; |
| 188 | |
| 189 | expect_destruct(); |
| 190 | } |
| 191 | |
| 192 | // Delete the original child |
| 193 | child2.swap(child); |
| 194 | expect_destruct(); |
| 195 | child2.reset(); |
| 196 | |
| 197 | // Make sure our new copy can still access data |
| 198 | child->set_callback(nullptr); |
| 199 | } |
| 200 | |
William A. Kennington III | 571fdf9 | 2018-07-22 18:16:22 -0700 | [diff] [blame] | 201 | TEST_F(ChildMethodTest, GetPidSuccess) |
| 202 | { |
| 203 | const pid_t pid = 32; |
| 204 | EXPECT_CALL(mock, |
| 205 | sd_event_source_get_child_pid(expected_source, testing::_)) |
| 206 | .WillOnce(DoAll(SetArgPointee<1>(pid), Return(0))); |
| 207 | EXPECT_EQ(pid, child->get_pid()); |
| 208 | } |
| 209 | |
| 210 | TEST_F(ChildMethodTest, GetPidError) |
| 211 | { |
| 212 | EXPECT_CALL(mock, |
| 213 | sd_event_source_get_child_pid(expected_source, testing::_)) |
| 214 | .WillOnce(Return(-EINVAL)); |
| 215 | EXPECT_THROW(child->get_pid(), SdEventError); |
| 216 | } |
| 217 | |
| 218 | } // namespace |
| 219 | } // namespace source |
| 220 | } // namespace sdeventplus |