blob: c99aa6b50aac7fd1ecaf4025937b235f30b8da5d [file] [log] [blame]
William A. Kennington IIIa3a38502018-07-22 18:15:59 -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/signal.hpp>
9#include <sdeventplus/test/sdevent.hpp>
10#include <signal.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;
24using testing::SaveArg;
25using testing::SetArgPointee;
26
27using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
28
29class SignalTest : public testing::Test
30{
31 protected:
32 testing::StrictMock<test::SdEventMock> mock;
33 sd_event_source* const expected_source =
34 reinterpret_cast<sd_event_source*>(1234);
35 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
36 UniqueEvent event = make_event(expected_event);
37
38 UniqueEvent make_event(sd_event* event)
39 {
40 auto deleter = [this, event](Event* e) {
41 EXPECT_CALL(this->mock, sd_event_unref(event))
42 .WillOnce(Return(nullptr));
43 delete e;
44 };
45 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
46 }
47
48 void expect_destruct()
49 {
50 {
51 testing::InSequence sequence;
52 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
53 SD_EVENT_OFF))
54 .WillOnce(Return(0));
55 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
56 .WillOnce(Return(nullptr));
57 }
58 EXPECT_CALL(mock, sd_event_unref(expected_event))
59 .WillOnce(Return(nullptr));
60 }
61};
62
63TEST_F(SignalTest, ConstructSuccess)
64{
65 const int sig = SIGALRM;
66
67 EXPECT_CALL(mock, sd_event_ref(expected_event))
68 .WillOnce(Return(expected_event));
69 sd_event_signal_handler_t handler;
70 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
71 testing::_, nullptr))
72 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<3>(&handler),
73 Return(0)));
74 void* userdata;
75 EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
76 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
77 int completions = 0;
78 const struct signalfd_siginfo* return_si;
79 Signal::Callback callback = [&](Signal&,
80 const struct signalfd_siginfo* si) {
81 return_si = si;
82 completions++;
83 };
84 Signal signal(*event, sig, std::move(callback));
85 EXPECT_FALSE(callback);
86 EXPECT_EQ(&signal, userdata);
87 EXPECT_EQ(0, completions);
88
89 const struct signalfd_siginfo* expected_si =
90 reinterpret_cast<struct signalfd_siginfo*>(865);
91 EXPECT_EQ(0, handler(nullptr, expected_si, &signal));
92 EXPECT_EQ(1, completions);
93 EXPECT_EQ(expected_si, return_si);
94
95 expect_destruct();
96}
97
98TEST_F(SignalTest, ConstructError)
99{
100 const int sig = SIGALRM;
101
102 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
103 testing::_, nullptr))
104 .WillOnce(Return(-EINVAL));
105 int completions = 0;
106 Signal::Callback callback = [&completions](Signal&,
107 const struct signalfd_siginfo*) {
108 completions++;
109 };
110 EXPECT_THROW(Signal(*event, sig, std::move(callback)), SdEventError);
111 EXPECT_TRUE(callback);
112 EXPECT_EQ(0, completions);
113}
114
115class SignalMethodTest : public SignalTest
116{
117 protected:
118 std::unique_ptr<Signal> signal;
119
120 void SetUp()
121 {
122 const int sig = SIGINT;
123
124 EXPECT_CALL(mock, sd_event_ref(expected_event))
125 .WillOnce(Return(expected_event));
126 EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
127 testing::_, nullptr))
128 .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
129 EXPECT_CALL(mock,
130 sd_event_source_set_userdata(expected_source, testing::_))
131 .WillOnce(Return(nullptr));
132 signal = std::make_unique<Signal>(
133 *event, sig, [](Signal&, const struct signalfd_siginfo*) {});
134 }
135
136 void TearDown()
137 {
138 expect_destruct();
139 signal.reset();
140 }
141};
142
143TEST_F(SignalMethodTest, GetSignalSuccess)
144{
145 const int sig = SIGTERM;
146 EXPECT_CALL(mock, sd_event_source_get_signal(expected_source))
147 .WillOnce(Return(sig));
148 EXPECT_EQ(sig, signal->get_signal());
149}
150
151TEST_F(SignalMethodTest, GetSignalError)
152{
153 EXPECT_CALL(mock, sd_event_source_get_signal(expected_source))
154 .WillOnce(Return(-EINVAL));
155 EXPECT_THROW(signal->get_signal(), SdEventError);
156}
157
158} // namespace
159} // namespace source
160} // namespace sdeventplus