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