blob: b680be79cf27f441ff90127d6f524fc96b7aacf0 [file] [log] [blame]
William A. Kennington IIIce80c3f2018-07-22 14:59:13 -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/io.hpp>
9#include <sdeventplus/test/sdevent.hpp>
10#include <systemd/sd-event.h>
11#include <type_traits>
12#include <utility>
13
14namespace sdeventplus
15{
16namespace source
17{
18namespace
19{
20
21using testing::DoAll;
22using testing::Return;
23using testing::SaveArg;
24using testing::SetArgPointee;
25
26using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
27
28class IOTest : public testing::Test
29{
30 protected:
31 testing::StrictMock<test::SdEventMock> mock;
32 sd_event_source* const expected_source =
33 reinterpret_cast<sd_event_source*>(1234);
34 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
35 UniqueEvent event = make_event(expected_event);
36
37 UniqueEvent make_event(sd_event* event)
38 {
39 auto deleter = [this, event](Event* e) {
40 EXPECT_CALL(this->mock, sd_event_unref(event))
41 .WillOnce(Return(nullptr));
42 delete e;
43 };
44 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
45 }
46
47 void expect_destruct()
48 {
49 {
50 testing::InSequence sequence;
51 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
52 SD_EVENT_OFF))
53 .WillOnce(Return(0));
54 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
55 .WillOnce(Return(nullptr));
56 }
57 EXPECT_CALL(mock, sd_event_unref(expected_event))
58 .WillOnce(Return(nullptr));
59 }
60};
61
62TEST_F(IOTest, ConstructSuccess)
63{
64 const int fd = 10;
65 const uint32_t events = EPOLLIN | EPOLLET;
66
67 EXPECT_CALL(mock, sd_event_ref(expected_event))
68 .WillOnce(Return(expected_event));
69 sd_event_io_handler_t handler;
70 EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd, events,
71 testing::_, nullptr))
72 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<4>(&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 int return_fd;
79 uint32_t return_revents;
80 IO::Callback callback = [&](IO&, int fd, uint32_t revents) {
81 return_fd = fd;
82 return_revents = revents;
83 completions++;
84 };
85 IO io(*event, fd, events, std::move(callback));
86 EXPECT_FALSE(callback);
87 EXPECT_EQ(&io, userdata);
88 EXPECT_EQ(0, completions);
89
90 EXPECT_EQ(0, handler(nullptr, 5, EPOLLIN, &io));
91 EXPECT_EQ(1, completions);
92 EXPECT_EQ(5, return_fd);
93 EXPECT_EQ(EPOLLIN, return_revents);
94
95 expect_destruct();
96}
97
98TEST_F(IOTest, ConstructError)
99{
100 const int fd = 10;
101 const uint32_t events = EPOLLIN | EPOLLET;
102
103 EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd, events,
104 testing::_, nullptr))
105 .WillOnce(Return(-EINVAL));
106 int completions = 0;
107 IO::Callback callback = [&completions](IO&, int, uint32_t) {
108 completions++;
109 };
110 EXPECT_THROW(IO(*event, fd, events, std::move(callback)), SdEventError);
111 EXPECT_TRUE(callback);
112 EXPECT_EQ(0, completions);
113}
114
115class IOMethodTest : public IOTest
116{
117 protected:
118 std::unique_ptr<IO> io;
119
120 void SetUp()
121 {
122 const int fd = 7;
123 const int events = EPOLLOUT;
124
125 EXPECT_CALL(mock, sd_event_ref(expected_event))
126 .WillOnce(Return(expected_event));
127 EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd,
128 events, testing::_, nullptr))
129 .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
130 EXPECT_CALL(mock,
131 sd_event_source_set_userdata(expected_source, testing::_))
132 .WillOnce(Return(nullptr));
133 io =
134 std::make_unique<IO>(*event, fd, events, [](IO&, int, uint32_t) {});
135 }
136
137 void TearDown()
138 {
139 expect_destruct();
140 io.reset();
141 }
142};
143
144TEST_F(IOMethodTest, GetFdSuccess)
145{
146 const int fd = 5;
147 EXPECT_CALL(mock, sd_event_source_get_io_fd(expected_source))
148 .WillOnce(Return(fd));
149 EXPECT_EQ(fd, io->get_fd());
150}
151
152TEST_F(IOMethodTest, GetFdError)
153{
154 EXPECT_CALL(mock, sd_event_source_get_io_fd(expected_source))
155 .WillOnce(Return(-EINVAL));
156 EXPECT_THROW(io->get_fd(), SdEventError);
157}
158
159TEST_F(IOMethodTest, SetFdSuccess)
160{
161 const int fd = 5;
162 EXPECT_CALL(mock, sd_event_source_set_io_fd(expected_source, fd))
163 .WillOnce(Return(0));
164 io->set_fd(fd);
165}
166
167TEST_F(IOMethodTest, SetFdError)
168{
169 const int fd = 3;
170 EXPECT_CALL(mock, sd_event_source_set_io_fd(expected_source, fd))
171 .WillOnce(Return(-EINVAL));
172 EXPECT_THROW(io->set_fd(fd), SdEventError);
173}
174
William A. Kennington IIIce80c3f2018-07-22 14:59:13 -0700175TEST_F(IOMethodTest, GetEventsSuccess)
176{
177 const uint32_t events = EPOLLIN | EPOLLOUT;
178 EXPECT_CALL(mock,
179 sd_event_source_get_io_events(expected_source, testing::_))
180 .WillOnce(DoAll(SetArgPointee<1>(events), Return(0)));
181 EXPECT_EQ(events, io->get_events());
182}
183
184TEST_F(IOMethodTest, GetEventsError)
185{
186 EXPECT_CALL(mock,
187 sd_event_source_get_io_events(expected_source, testing::_))
188 .WillOnce(Return(-EINVAL));
189 EXPECT_THROW(io->get_events(), SdEventError);
190}
191
192TEST_F(IOMethodTest, SetEventsSuccess)
193{
194 const uint32_t events = EPOLLIN | EPOLLOUT;
195 EXPECT_CALL(mock, sd_event_source_set_io_events(expected_source, events))
196 .WillOnce(Return(0));
197 io->set_events(events);
198}
199
200TEST_F(IOMethodTest, SetEventsError)
201{
202 const uint32_t events = EPOLLIN | EPOLLOUT;
203 EXPECT_CALL(mock, sd_event_source_set_io_events(expected_source, events))
204 .WillOnce(Return(-EINVAL));
205 EXPECT_THROW(io->set_events(events), SdEventError);
206}
207
208TEST_F(IOMethodTest, GetREventsSuccess)
209{
210 const uint32_t revents = EPOLLOUT;
211 EXPECT_CALL(mock,
212 sd_event_source_get_io_revents(expected_source, testing::_))
213 .WillOnce(DoAll(SetArgPointee<1>(revents), Return(0)));
214 EXPECT_EQ(revents, io->get_revents());
215}
216
217TEST_F(IOMethodTest, GetREventsError)
218{
219 EXPECT_CALL(mock,
220 sd_event_source_get_io_revents(expected_source, testing::_))
221 .WillOnce(Return(-EINVAL));
222 EXPECT_THROW(io->get_revents(), SdEventError);
223}
224
225} // namespace
226} // namespace source
227} // namespace sdeventplus