blob: effd7a3743086b7562ae6b3b2e1d8c468f37e938 [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <signal.h>
2#include <systemd/sd-event.h>
3
William A. Kennington IIIa3a38502018-07-22 18:15:59 -07004#include <sdeventplus/event.hpp>
5#include <sdeventplus/exception.hpp>
6#include <sdeventplus/source/signal.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 IIIa3a38502018-07-22 18:15:59 -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 IIIa3a38502018-07-22 18:15:59 -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 IIIa3a38502018-07-22 18:15:59 -070028using testing::SaveArg;
29using testing::SetArgPointee;
30
31using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
32
33class SignalTest : 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 IIIa3a38502018-07-22 18:15:59 -070056 EXPECT_CALL(mock, sd_event_unref(expected_event))
57 .WillOnce(Return(nullptr));
58 }
59};
60
61TEST_F(SignalTest, ConstructSuccess)
62{
63 const int sig = SIGALRM;
64
65 EXPECT_CALL(mock, sd_event_ref(expected_event))
66 .WillOnce(Return(expected_event));
67 sd_event_signal_handler_t handler;
68 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
69 testing::_, nullptr))
70 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<3>(&handler),
71 Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070072 sd_event_destroy_t destroy;
William A. Kennington IIIa3a38502018-07-22 18:15:59 -070073 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070074 {
75 testing::InSequence seq;
76 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
77 testing::_))
78 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
79 EXPECT_CALL(mock,
80 sd_event_source_set_userdata(expected_source, testing::_))
81 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
82 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
83 .WillRepeatedly(ReturnPointee(&userdata));
84 }
William A. Kennington IIIa3a38502018-07-22 18:15:59 -070085 int completions = 0;
86 const struct signalfd_siginfo* return_si;
Patrick Williamsa8c11e32023-05-10 07:50:56 -050087 Signal::Callback callback =
88 [&](Signal&, const struct signalfd_siginfo* si) {
William A. Kennington IIIa3a38502018-07-22 18:15:59 -070089 return_si = si;
90 completions++;
91 };
92 Signal signal(*event, sig, std::move(callback));
93 EXPECT_FALSE(callback);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070094 EXPECT_NE(&signal, userdata);
William A. Kennington IIIa3a38502018-07-22 18:15:59 -070095 EXPECT_EQ(0, completions);
96
97 const struct signalfd_siginfo* expected_si =
98 reinterpret_cast<struct signalfd_siginfo*>(865);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070099 EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700100 EXPECT_EQ(1, completions);
101 EXPECT_EQ(expected_si, return_si);
102
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700103 signal.set_callback(std::bind([]() {}));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700104 EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700105 EXPECT_EQ(1, completions);
106
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700107 expect_destruct();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700108 destroy(userdata);
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700109}
110
111TEST_F(SignalTest, ConstructError)
112{
113 const int sig = SIGALRM;
114
115 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
116 testing::_, nullptr))
117 .WillOnce(Return(-EINVAL));
118 int completions = 0;
Patrick Williamsa8c11e32023-05-10 07:50:56 -0500119 Signal::Callback callback =
120 [&completions](Signal&, const struct signalfd_siginfo*) {
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700121 completions++;
122 };
123 EXPECT_THROW(Signal(*event, sig, std::move(callback)), SdEventError);
124 EXPECT_TRUE(callback);
125 EXPECT_EQ(0, completions);
126}
127
128class SignalMethodTest : public SignalTest
129{
130 protected:
131 std::unique_ptr<Signal> signal;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700132 sd_event_destroy_t destroy;
133 void* userdata;
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700134
135 void SetUp()
136 {
137 const int sig = SIGINT;
138
139 EXPECT_CALL(mock, sd_event_ref(expected_event))
140 .WillOnce(Return(expected_event));
141 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
142 testing::_, nullptr))
143 .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700144 {
145 testing::InSequence seq;
146 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(
147 expected_source, testing::_))
148 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
149 EXPECT_CALL(
150 mock, sd_event_source_set_userdata(expected_source, testing::_))
151 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
152 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
153 .WillRepeatedly(ReturnPointee(&userdata));
154 }
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700155 signal = std::make_unique<Signal>(
156 *event, sig, [](Signal&, const struct signalfd_siginfo*) {});
157 }
158
159 void TearDown()
160 {
161 expect_destruct();
162 signal.reset();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700163 destroy(userdata);
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700164 }
165};
166
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700167TEST_F(SignalMethodTest, Copy)
168{
169 EXPECT_CALL(mock, sd_event_ref(expected_event))
170 .WillOnce(Return(expected_event));
171 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
172 .WillOnce(Return(expected_source));
173 auto signal2 = std::make_unique<Signal>(*signal);
174 {
175 EXPECT_CALL(mock, sd_event_ref(expected_event))
176 .WillOnce(Return(expected_event));
177 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
178 .WillOnce(Return(expected_source));
179 Signal signal3(*signal);
180
181 expect_destruct();
182 EXPECT_CALL(mock, sd_event_ref(expected_event))
183 .WillOnce(Return(expected_event));
184 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
185 .WillOnce(Return(expected_source));
186 *signal2 = signal3;
187
188 expect_destruct();
189 }
190
191 // Delete the original signal
192 signal2.swap(signal);
193 expect_destruct();
194 signal2.reset();
195
196 // Make sure our new copy can still access data
197 signal->set_callback(nullptr);
198}
199
William A. Kennington IIIa3a38502018-07-22 18:15:59 -0700200TEST_F(SignalMethodTest, GetSignalSuccess)
201{
202 const int sig = SIGTERM;
203 EXPECT_CALL(mock, sd_event_source_get_signal(expected_source))
204 .WillOnce(Return(sig));
205 EXPECT_EQ(sig, signal->get_signal());
206}
207
208TEST_F(SignalMethodTest, GetSignalError)
209{
210 EXPECT_CALL(mock, sd_event_source_get_signal(expected_source))
211 .WillOnce(Return(-EINVAL));
212 EXPECT_THROW(signal->get_signal(), SdEventError);
213}
214
215} // namespace
216} // namespace source
217} // namespace sdeventplus