blob: bc0eec09bc0201ae5e7cc550c48059dad56d8b96 [file] [log] [blame]
William A. Kennington III48c42752018-07-17 14:40:14 -07001#include <cerrno>
William A. Kennington III0a816c52018-07-17 14:40:14 -07002#include <gmock/gmock.h>
3#include <gtest/gtest.h>
William A. Kennington III715c72f2018-07-17 14:40:14 -07004#include <memory>
5#include <sdeventplus/event.hpp>
William A. Kennington III65db8632018-07-17 14:40:14 -07006#include <sdeventplus/exception.hpp>
William A. Kennington III0a816c52018-07-17 14:40:14 -07007#include <sdeventplus/internal/sdevent.hpp>
8#include <sdeventplus/source/base.hpp>
9#include <sdeventplus/test/sdevent.hpp>
William A. Kennington III65db8632018-07-17 14:40:14 -070010#include <string>
William A. Kennington III715c72f2018-07-17 14:40:14 -070011#include <systemd/sd-event.h>
William A. Kennington III0a816c52018-07-17 14:40:14 -070012#include <type_traits>
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -070013#include <utility>
William A. Kennington III0a816c52018-07-17 14:40:14 -070014
15namespace sdeventplus
16{
17namespace source
18{
19namespace
20{
21
William A. Kennington III65db8632018-07-17 14:40:14 -070022using testing::DoAll;
William A. Kennington III0a816c52018-07-17 14:40:14 -070023using testing::Return;
William A. Kennington III48c42752018-07-17 14:40:14 -070024using testing::SaveArg;
William A. Kennington III65db8632018-07-17 14:40:14 -070025using testing::SetArgPointee;
William A. Kennington III0a816c52018-07-17 14:40:14 -070026
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070027using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
28
William A. Kennington III0a816c52018-07-17 14:40:14 -070029class BaseImpl : public Base
30{
31 public:
William A. Kennington III715c72f2018-07-17 14:40:14 -070032 BaseImpl(const Event& event, sd_event_source* source) : Base(event, source)
William A. Kennington III0a816c52018-07-17 14:40:14 -070033 {
34 }
William A. Kennington III715c72f2018-07-17 14:40:14 -070035 BaseImpl(const Event& event, sd_event_source* source, std::false_type) :
36 Base(event, source, std::false_type())
William A. Kennington III0a816c52018-07-17 14:40:14 -070037 {
38 }
William A. Kennington IIIe0f1d192018-07-17 17:00:56 -070039
40 using Base::get_prepare;
William A. Kennington III0a816c52018-07-17 14:40:14 -070041};
42
43class BaseTest : public testing::Test
44{
45 protected:
46 testing::StrictMock<test::SdEventMock> mock;
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070047 sd_event_source* const expected_source =
48 reinterpret_cast<sd_event_source*>(1234);
49 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
50 UniqueEvent event = make_event(expected_event);
William A. Kennington III715c72f2018-07-17 14:40:14 -070051
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070052 UniqueEvent make_event(sd_event* event)
William A. Kennington III715c72f2018-07-17 14:40:14 -070053 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070054 auto deleter = [this, event](Event* e) {
55 EXPECT_CALL(this->mock, sd_event_unref(event))
56 .WillOnce(Return(nullptr));
57 delete e;
58 };
59 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
William A. Kennington III715c72f2018-07-17 14:40:14 -070060 }
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070061
62 // Using a unique_ptr to make sure we don't get any superfluous moves or
63 // copies.
64 std::unique_ptr<BaseImpl> make_base(const Event& event,
65 sd_event_source* source)
William A. Kennington III715c72f2018-07-17 14:40:14 -070066 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070067 EXPECT_CALL(mock, sd_event_ref(event.get()))
68 .WillOnce(Return(event.get()));
William A. Kennington III2d943ea2018-07-17 14:40:14 -070069 void* userdata;
70 EXPECT_CALL(mock, sd_event_source_set_userdata(source, testing::_))
71 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070072 auto ret = std::make_unique<BaseImpl>(event, source, std::false_type());
William A. Kennington III2d943ea2018-07-17 14:40:14 -070073 EXPECT_EQ(ret.get(), userdata);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070074 EXPECT_EQ(source, ret->get());
75 EXPECT_NE(&event, &ret->get_event());
76 EXPECT_EQ(event.get(), ret->get_event().get());
77 EXPECT_FALSE(ret->get_prepare());
78 return ret;
79 }
80
William A. Kennington IIIe0f1d192018-07-17 17:00:56 -070081 void set_prepare_placeholder(BaseImpl& base)
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070082 {
83 EXPECT_CALL(mock, sd_event_source_set_prepare(base.get(), testing::_))
84 .WillOnce(Return(0));
85 base.set_prepare([](Base&) {});
86 EXPECT_TRUE(base.get_prepare());
87 }
88
89 void empty_base(BaseImpl&& other)
90 {
William A. Kennington III2d943ea2018-07-17 14:40:14 -070091 void* userdata;
92 EXPECT_CALL(mock, sd_event_source_set_userdata(other.get(), testing::_))
93 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070094 BaseImpl mover(std::move(other));
William A. Kennington III2d943ea2018-07-17 14:40:14 -070095 EXPECT_EQ(&mover, userdata);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070096 EXPECT_EQ(nullptr, other.get());
97 EXPECT_EQ(nullptr, other.get_event().get());
98 EXPECT_FALSE(other.get_prepare());
99
100 expect_base_destruct(mover.get_event(), mover.get());
101 }
102
103 void expect_base_destruct(const Event& event, sd_event_source* source)
104 {
105 {
106 testing::InSequence seq;
107 EXPECT_CALL(mock, sd_event_source_set_enabled(source, SD_EVENT_OFF))
108 .WillOnce(Return(0));
109 EXPECT_CALL(mock, sd_event_source_unref(source))
110 .WillOnce(Return(nullptr));
111 }
112 EXPECT_CALL(mock, sd_event_unref(event.get()))
William A. Kennington III715c72f2018-07-17 14:40:14 -0700113 .WillOnce(Return(nullptr));
William A. Kennington III715c72f2018-07-17 14:40:14 -0700114 }
William A. Kennington III0a816c52018-07-17 14:40:14 -0700115};
116
117TEST_F(BaseTest, NewBaseRef)
118{
William A. Kennington III715c72f2018-07-17 14:40:14 -0700119 EXPECT_CALL(mock, sd_event_ref(expected_event))
120 .WillOnce(Return(expected_event));
William A. Kennington III0a816c52018-07-17 14:40:14 -0700121 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
122 .WillOnce(Return(expected_source));
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700123 void* userdata;
124 EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
125 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington III715c72f2018-07-17 14:40:14 -0700126 BaseImpl source(*event, expected_source);
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700127 EXPECT_EQ(&source, userdata);
William A. Kennington III4863b962018-07-17 14:40:14 -0700128 EXPECT_EQ(expected_source, source.get());
129 EXPECT_NE(event.get(), &source.get_event());
130 EXPECT_EQ(expected_event, source.get_event().get());
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700131 EXPECT_FALSE(source.get_prepare());
William A. Kennington III0a816c52018-07-17 14:40:14 -0700132
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700133 expect_base_destruct(*event, expected_source);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700134}
135
136TEST_F(BaseTest, NewBaseNoRef)
137{
William A. Kennington III715c72f2018-07-17 14:40:14 -0700138 EXPECT_CALL(mock, sd_event_ref(expected_event))
139 .WillOnce(Return(expected_event));
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700140 void* userdata;
141 EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
142 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington III715c72f2018-07-17 14:40:14 -0700143 BaseImpl source(*event, expected_source, std::false_type());
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700144 EXPECT_EQ(&source, userdata);
William A. Kennington III4863b962018-07-17 14:40:14 -0700145 EXPECT_EQ(expected_source, source.get());
146 EXPECT_NE(event.get(), &source.get_event());
147 EXPECT_EQ(expected_event, source.get_event().get());
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700148 EXPECT_FALSE(source.get_prepare());
William A. Kennington III0a816c52018-07-17 14:40:14 -0700149
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700150 expect_base_destruct(*event, expected_source);
William A. Kennington III7c24e052018-07-17 14:40:14 -0700151}
152
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700153TEST_F(BaseTest, MoveConstruct)
154{
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700155 std::unique_ptr<BaseImpl> source1 = make_base(*event, expected_source);
156 set_prepare_placeholder(*source1);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700157
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700158 void* userdata;
159 EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
160 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700161 BaseImpl source2(std::move(*source1));
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700162 EXPECT_EQ(&source2, userdata);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700163 EXPECT_EQ(nullptr, source1->get());
164 EXPECT_EQ(nullptr, source1->get_event().get());
165 EXPECT_FALSE(source1->get_prepare());
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700166 EXPECT_EQ(expected_source, source2.get());
167 EXPECT_EQ(expected_event, source2.get_event().get());
168 EXPECT_TRUE(source2.get_prepare());
169
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700170 expect_base_destruct(*event, expected_source);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700171}
172
173TEST_F(BaseTest, MoveAssignSelf)
174{
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700175 std::unique_ptr<BaseImpl> source1 = make_base(*event, expected_source);
176 set_prepare_placeholder(*source1);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700177
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700178 *source1 = std::move(*source1);
179 EXPECT_EQ(expected_source, source1->get());
180 EXPECT_EQ(expected_event, source1->get_event().get());
181 EXPECT_TRUE(source1->get_prepare());
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700182
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700183 expect_base_destruct(*event, expected_source);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700184}
185
186TEST_F(BaseTest, MoveAssignEmpty)
187{
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700188 std::unique_ptr<BaseImpl> source1 = make_base(*event, expected_source);
189 set_prepare_placeholder(*source1);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700190
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700191 std::unique_ptr<BaseImpl> source2 = make_base(*event, expected_source);
192 empty_base(std::move(*source2));
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700193
194 {
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700195 void* userdata;
196 EXPECT_CALL(mock,
197 sd_event_source_set_userdata(expected_source, testing::_))
198 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700199 *source2 = std::move(*source1);
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700200 EXPECT_EQ(source2.get(), userdata);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700201 }
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700202 EXPECT_EQ(nullptr, source1->get());
203 EXPECT_EQ(nullptr, source1->get_event().get());
204 EXPECT_FALSE(source1->get_prepare());
205 EXPECT_EQ(expected_source, source2->get());
206 EXPECT_EQ(expected_event, source2->get_event().get());
207 EXPECT_TRUE(source2->get_prepare());
208
209 // Make sure source1 is deleted to ensure it isn't holding a reference
210 source1.reset();
211 expect_base_destruct(*event, expected_source);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700212}
213
214TEST_F(BaseTest, MoveAssignExisting)
215{
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700216 sd_event_source* const expected_source2 =
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700217 reinterpret_cast<sd_event_source*>(3456);
218 sd_event* const expected_event2 = reinterpret_cast<sd_event*>(4567);
219
220 std::unique_ptr<BaseImpl> source1 = make_base(*event, expected_source);
221 set_prepare_placeholder(*source1);
222
223 UniqueEvent event2 = make_event(expected_event2);
224 std::unique_ptr<BaseImpl> source2 = make_base(*event2, expected_source2);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700225
226 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700227 expect_base_destruct(*event2, expected_source2);
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700228 void* userdata;
229 EXPECT_CALL(mock,
230 sd_event_source_set_userdata(expected_source, testing::_))
231 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700232 *source2 = std::move(*source1);
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700233 EXPECT_EQ(source2.get(), userdata);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700234 }
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700235 EXPECT_EQ(nullptr, source1->get());
236 EXPECT_EQ(nullptr, source1->get_event().get());
237 EXPECT_FALSE(source1->get_prepare());
238 EXPECT_EQ(expected_source, source2->get());
239 EXPECT_EQ(expected_event, source2->get_event().get());
240 EXPECT_TRUE(source2->get_prepare());
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700241
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700242 // Make sure source1 is deleted to ensure it isn't holding a reference
243 source1.reset();
244 expect_base_destruct(*event, expected_source);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700245}
246
William A. Kennington III65db8632018-07-17 14:40:14 -0700247class BaseMethodTest : public BaseTest
248{
249 protected:
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700250 std::unique_ptr<BaseImpl> base = make_base(*event, expected_source);
William A. Kennington III65db8632018-07-17 14:40:14 -0700251
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700252 void TearDown()
William A. Kennington III65db8632018-07-17 14:40:14 -0700253 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700254 expect_base_destruct(base->get_event(), base->get());
William A. Kennington III715c72f2018-07-17 14:40:14 -0700255 base.reset();
William A. Kennington III65db8632018-07-17 14:40:14 -0700256 }
257};
258
259TEST_F(BaseMethodTest, GetDescriptionSuccess)
260{
261 const char* expected = "test_desc";
262 EXPECT_CALL(mock,
263 sd_event_source_get_description(expected_source, testing::_))
264 .WillOnce(DoAll(SetArgPointee<1>(expected), Return(0)));
265 // Intentionally comparing pointers to make sure no copying is happening
266 EXPECT_EQ(expected, base->get_description());
267}
268
269TEST_F(BaseMethodTest, GetDescriptionError)
270{
271 EXPECT_CALL(mock,
272 sd_event_source_get_description(expected_source, testing::_))
273 .WillOnce(Return(-EINVAL));
274 EXPECT_THROW(base->get_description(), SdEventError);
275}
276
277TEST_F(BaseMethodTest, SetDescriptionSuccess)
278{
279 const char* expected = "test desc";
280 // Intentionally comparing pointers to make sure no copying is happening
281 EXPECT_CALL(mock,
282 sd_event_source_set_description(expected_source, expected))
283 .WillOnce(Return(0));
284 base->set_description(expected);
285}
286
287TEST_F(BaseMethodTest, SetDescriptionError)
288{
289 const char* expected = "test desc";
290 // Intentionally comparing pointers to make sure no copying is happening
291 EXPECT_CALL(mock,
292 sd_event_source_set_description(expected_source, expected))
293 .WillOnce(Return(-EINVAL));
294 EXPECT_THROW(base->set_description(expected), SdEventError);
295}
296
William A. Kennington III48c42752018-07-17 14:40:14 -0700297TEST_F(BaseMethodTest, SetPrepareCallback)
298{
299 bool completed = false;
300 Base::Callback callback = [&completed](Base&) { completed = true; };
301 sd_event_handler_t event_handler;
302 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
303 .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0)));
304 base->set_prepare(std::move(callback));
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700305 EXPECT_TRUE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700306 EXPECT_FALSE(callback);
307 EXPECT_FALSE(completed);
308
309 EXPECT_EQ(0, event_handler(nullptr, base.get()));
310 EXPECT_TRUE(completed);
311}
312
313TEST_F(BaseMethodTest, SetPrepareCallbackNoUserdata)
314{
315 Base::Callback callback = [](Base&) {};
316 sd_event_handler_t event_handler;
317 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
318 .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0)));
319 base->set_prepare(std::move(callback));
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700320 EXPECT_TRUE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700321 EXPECT_FALSE(callback);
322
323 EXPECT_EQ(-EINVAL, event_handler(nullptr, nullptr));
324}
325
William A. Kennington III48c42752018-07-17 14:40:14 -0700326TEST_F(BaseMethodTest, SetPrepareError)
327{
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700328 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
329 .WillOnce(Return(0));
330 base->set_prepare(std::move([](Base&) {}));
331 EXPECT_TRUE(base->get_prepare());
332
William A. Kennington III48c42752018-07-17 14:40:14 -0700333 Base::Callback callback = [](Base&) {};
334 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
335 .WillOnce(Return(-EINVAL));
336 EXPECT_THROW(base->set_prepare(std::move(callback)), SdEventError);
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700337 EXPECT_FALSE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700338 EXPECT_TRUE(callback);
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700339}
340
341TEST_F(BaseMethodTest, SetPrepareNull)
342{
343 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
344 .WillOnce(Return(0));
345 base->set_prepare(std::move([](Base&) {}));
346 EXPECT_TRUE(base->get_prepare());
347
348 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, nullptr))
349 .WillOnce(Return(0));
350 base->set_prepare(nullptr);
351 EXPECT_FALSE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700352}
353
William A. Kennington III65db8632018-07-17 14:40:14 -0700354TEST_F(BaseMethodTest, GetPendingSuccess)
355{
356 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
357 .WillOnce(Return(0));
358 EXPECT_EQ(0, base->get_pending());
359 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
360 .WillOnce(Return(4));
361 EXPECT_EQ(4, base->get_pending());
362}
363
364TEST_F(BaseMethodTest, GetPendingError)
365{
366 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
367 .WillOnce(Return(-EINVAL));
368 EXPECT_THROW(base->get_pending(), SdEventError);
369}
370
371TEST_F(BaseMethodTest, GetPrioritySuccess)
372{
373 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
374 .WillOnce(DoAll(SetArgPointee<1>(1024), Return(0)));
375 EXPECT_EQ(1024, base->get_priority());
376}
377
378TEST_F(BaseMethodTest, GetPriorityError)
379{
380 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
381 .WillOnce(Return(-EINVAL));
382 EXPECT_THROW(base->get_priority(), SdEventError);
383}
384
385TEST_F(BaseMethodTest, SetPrioritySuccess)
386{
387 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
388 .WillOnce(Return(0));
389 base->set_priority(1024);
390}
391
392TEST_F(BaseMethodTest, SetPriorityError)
393{
394 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
395 .WillOnce(Return(-EINVAL));
396 EXPECT_THROW(base->set_priority(1024), SdEventError);
397}
398
399TEST_F(BaseMethodTest, GetEnabledSuccess)
400{
401 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
402 .WillOnce(DoAll(SetArgPointee<1>(SD_EVENT_ON), Return(0)));
403 EXPECT_EQ(SD_EVENT_ON, base->get_enabled());
404}
405
406TEST_F(BaseMethodTest, GetEnabledError)
407{
408 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
409 .WillOnce(Return(-EINVAL));
410 EXPECT_THROW(base->get_enabled(), SdEventError);
411}
412
413TEST_F(BaseMethodTest, SetEnabledSuccess)
414{
415 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
416 .WillOnce(Return(0));
417 base->set_enabled(SD_EVENT_ON);
418}
419
420TEST_F(BaseMethodTest, SetEnabledError)
421{
422 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
423 .WillOnce(Return(-EINVAL));
424 EXPECT_THROW(base->set_enabled(SD_EVENT_ON), SdEventError);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700425}
426
427} // namespace
428} // namespace source
429} // namespace sdeventplus