blob: 0928433974b924b48dbe1495d21f4937a3aadc4f [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <sys/wait.h>
2#include <systemd/sd-event.h>
3
William A. Kennington III571fdf92018-07-22 18:16:22 -07004#include <sdeventplus/event.hpp>
5#include <sdeventplus/exception.hpp>
6#include <sdeventplus/source/child.hpp>
7#include <sdeventplus/test/sdevent.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05008
9#include <cerrno>
10#include <functional>
11#include <memory>
William A. Kennington III571fdf92018-07-22 18:16:22 -070012#include <type_traits>
13#include <utility>
14
Patrick Williamsa8c11e32023-05-10 07:50:56 -050015#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
William A. Kennington III571fdf92018-07-22 18:16:22 -070018namespace sdeventplus
19{
20namespace source
21{
22namespace
23{
24
25using testing::DoAll;
26using testing::Return;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070027using testing::ReturnPointee;
William A. Kennington III571fdf92018-07-22 18:16:22 -070028using testing::SaveArg;
29using testing::SetArgPointee;
30
31using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
32
33class 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 III5320b1f2019-03-29 20:00:37 -070054 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
55 .WillOnce(Return(nullptr));
William A. Kennington III571fdf92018-07-22 18:16:22 -070056 EXPECT_CALL(mock, sd_event_unref(expected_event))
57 .WillOnce(Return(nullptr));
58 }
59};
60
61TEST_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 III5320b1f2019-03-29 20:00:37 -070073 sd_event_destroy_t destroy;
William A. Kennington III571fdf92018-07-22 18:16:22 -070074 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070075 {
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 III571fdf92018-07-22 18:16:22 -070086 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 III5320b1f2019-03-29 20:00:37 -070094 EXPECT_NE(&child, userdata);
William A. Kennington III571fdf92018-07-22 18:16:22 -070095 EXPECT_EQ(0, completions);
96
97 const siginfo_t* expected_si = reinterpret_cast<siginfo_t*>(865);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070098 EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
William A. Kennington III571fdf92018-07-22 18:16:22 -070099 EXPECT_EQ(1, completions);
100 EXPECT_EQ(expected_si, return_si);
101
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700102 child.set_callback(std::bind([]() {}));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700103 EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700104 EXPECT_EQ(1, completions);
105
William A. Kennington III571fdf92018-07-22 18:16:22 -0700106 expect_destruct();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700107 destroy(userdata);
William A. Kennington III571fdf92018-07-22 18:16:22 -0700108}
109
110TEST_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
128class ChildMethodTest : public ChildTest
129{
130 protected:
131 std::unique_ptr<Child> child;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700132 sd_event_destroy_t destroy;
133 void* userdata;
William A. Kennington III571fdf92018-07-22 18:16:22 -0700134
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 III5320b1f2019-03-29 20:00:37 -0700145 {
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 III571fdf92018-07-22 18:16:22 -0700156 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 III5320b1f2019-03-29 20:00:37 -0700164 destroy(userdata);
William A. Kennington III571fdf92018-07-22 18:16:22 -0700165 }
166};
167
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700168TEST_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 III571fdf92018-07-22 18:16:22 -0700201TEST_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
210TEST_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