blob: f1925461d6c8b241d3192981cf57e67e968547ea [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <systemd/sd-event.h>
2
William A. Kennington III715c72f2018-07-17 14:40:14 -07003#include <sdeventplus/event.hpp>
William A. Kennington III65db8632018-07-17 14:40:14 -07004#include <sdeventplus/exception.hpp>
William A. Kennington III0a816c52018-07-17 14:40:14 -07005#include <sdeventplus/internal/sdevent.hpp>
6#include <sdeventplus/source/base.hpp>
7#include <sdeventplus/test/sdevent.hpp>
William A. Kennington III5320b1f2019-03-29 20:00:37 -07008#include <sdeventplus/types.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05009
10#include <cerrno>
11#include <functional>
12#include <memory>
13#include <optional>
William A. Kennington III65db8632018-07-17 14:40:14 -070014#include <string>
William A. Kennington III5320b1f2019-03-29 20:00:37 -070015#include <tuple>
William A. Kennington III0a816c52018-07-17 14:40:14 -070016#include <type_traits>
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -070017#include <utility>
William A. Kennington III0a816c52018-07-17 14:40:14 -070018
Patrick Williamsa8c11e32023-05-10 07:50:56 -050019#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
William A. Kennington III0a816c52018-07-17 14:40:14 -070022namespace sdeventplus
23{
24namespace source
25{
26namespace
27{
28
William A. Kennington III65db8632018-07-17 14:40:14 -070029using testing::DoAll;
William A. Kennington III0a816c52018-07-17 14:40:14 -070030using testing::Return;
William A. Kennington III48c42752018-07-17 14:40:14 -070031using testing::SaveArg;
William A. Kennington III65db8632018-07-17 14:40:14 -070032using testing::SetArgPointee;
William A. Kennington III0a816c52018-07-17 14:40:14 -070033
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070034using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
35
William A. Kennington III5320b1f2019-03-29 20:00:37 -070036class BaseImplData;
37
William A. Kennington III0a816c52018-07-17 14:40:14 -070038class BaseImpl : public Base
39{
40 public:
William A. Kennington III5320b1f2019-03-29 20:00:37 -070041 BaseImpl(const Event& event, sd_event_source* source, std::false_type);
42
43 BaseImpl(const BaseImpl& other, sdeventplus::internal::NoOwn) :
44 Base(other, sdeventplus::internal::NoOwn())
Patrick Williamsa8c11e32023-05-10 07:50:56 -050045 {}
William A. Kennington IIIe0f1d192018-07-17 17:00:56 -070046
47 using Base::get_prepare;
William A. Kennington III0a816c52018-07-17 14:40:14 -070048};
49
William A. Kennington III5320b1f2019-03-29 20:00:37 -070050class BaseImplData : public BaseImpl, public detail::BaseData
51{
52 public:
53 BaseImplData(const BaseImpl& base) :
54 BaseImpl(base, sdeventplus::internal::NoOwn()), BaseData(base)
Patrick Williamsa8c11e32023-05-10 07:50:56 -050055 {}
William A. Kennington III5320b1f2019-03-29 20:00:37 -070056};
57
58BaseImpl::BaseImpl(const Event& event, sd_event_source* source,
59 std::false_type) :
60 Base(event, source, std::false_type())
61{
62 set_userdata(std::make_unique<BaseImplData>(*this));
63}
64
William A. Kennington III0a816c52018-07-17 14:40:14 -070065class BaseTest : public testing::Test
66{
67 protected:
68 testing::StrictMock<test::SdEventMock> mock;
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070069 sd_event_source* const expected_source =
70 reinterpret_cast<sd_event_source*>(1234);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070071 sd_event_source* const expected_source2 =
72 reinterpret_cast<sd_event_source*>(3456);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070073 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070074 sd_event* const expected_event2 = reinterpret_cast<sd_event*>(4567);
75
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070076 UniqueEvent event = make_event(expected_event);
William A. Kennington III715c72f2018-07-17 14:40:14 -070077
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070078 UniqueEvent make_event(sd_event* event)
William A. Kennington III715c72f2018-07-17 14:40:14 -070079 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070080 auto deleter = [this, event](Event* e) {
81 EXPECT_CALL(this->mock, sd_event_unref(event))
82 .WillOnce(Return(nullptr));
83 delete e;
84 };
85 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
William A. Kennington III715c72f2018-07-17 14:40:14 -070086 }
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070087
88 // Using a unique_ptr to make sure we don't get any superfluous moves or
89 // copies.
William A. Kennington III5320b1f2019-03-29 20:00:37 -070090 std::tuple<std::unique_ptr<BaseImpl>, std::function<void()>>
91 make_base(const Event& event, sd_event_source* source)
William A. Kennington III715c72f2018-07-17 14:40:14 -070092 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -070093 EXPECT_CALL(mock, sd_event_ref(event.get()))
94 .WillOnce(Return(event.get()));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070095 sd_event_destroy_t destroy;
William A. Kennington III2d943ea2018-07-17 14:40:14 -070096 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070097 {
98 testing::InSequence seq;
99 EXPECT_CALL(
100 mock, sd_event_source_set_destroy_callback(source, testing::_))
101 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
102 EXPECT_CALL(mock, sd_event_source_set_userdata(source, testing::_))
103 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
104 }
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700105 auto ret = std::make_unique<BaseImpl>(event, source, std::false_type());
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700106 EXPECT_CALL(mock, sd_event_source_get_userdata(source))
107 .WillRepeatedly(Return(userdata));
108 EXPECT_NE(ret.get(), userdata);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700109 EXPECT_EQ(source, ret->get());
110 EXPECT_NE(&event, &ret->get_event());
111 EXPECT_EQ(event.get(), ret->get_event().get());
112 EXPECT_FALSE(ret->get_prepare());
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700113 return {std::move(ret), std::bind(destroy, userdata)};
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700114 }
115
William A. Kennington IIIe0f1d192018-07-17 17:00:56 -0700116 void set_prepare_placeholder(BaseImpl& base)
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700117 {
118 EXPECT_CALL(mock, sd_event_source_set_prepare(base.get(), testing::_))
119 .WillOnce(Return(0));
120 base.set_prepare([](Base&) {});
121 EXPECT_TRUE(base.get_prepare());
122 }
123
124 void empty_base(BaseImpl&& other)
125 {
126 BaseImpl mover(std::move(other));
William A. Kennington IIIe04cb032018-11-13 14:28:36 -0800127 EXPECT_THROW(other.get(), std::bad_optional_access);
128 EXPECT_THROW(other.get_event().get(), std::bad_optional_access);
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700129 EXPECT_THROW(other.get_prepare(), std::bad_optional_access);
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700130
131 expect_base_destruct(mover.get_event(), mover.get());
132 }
133
134 void expect_base_destruct(const Event& event, sd_event_source* source)
135 {
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700136 EXPECT_CALL(mock, sd_event_source_unref(source))
137 .WillOnce(Return(nullptr));
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700138 EXPECT_CALL(mock, sd_event_unref(event.get()))
William A. Kennington III715c72f2018-07-17 14:40:14 -0700139 .WillOnce(Return(nullptr));
William A. Kennington III715c72f2018-07-17 14:40:14 -0700140 }
William A. Kennington III0a816c52018-07-17 14:40:14 -0700141};
142
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700143TEST_F(BaseTest, NewBaseFail)
144{
145 EXPECT_CALL(mock, sd_event_ref(expected_event))
146 .WillOnce(Return(expected_event));
147 EXPECT_CALL(
148 mock, sd_event_source_set_destroy_callback(expected_source, testing::_))
149 .WillOnce(Return(-EINVAL));
150 expect_base_destruct(*event, expected_source);
151 EXPECT_THROW(BaseImpl(*event, expected_source, std::false_type()),
152 SdEventError);
153}
154
William A. Kennington III0a816c52018-07-17 14:40:14 -0700155TEST_F(BaseTest, NewBaseNoRef)
156{
William A. Kennington III715c72f2018-07-17 14:40:14 -0700157 EXPECT_CALL(mock, sd_event_ref(expected_event))
158 .WillOnce(Return(expected_event));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700159 sd_event_destroy_t destroy;
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700160 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700161 {
162 testing::InSequence seq;
163 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
164 testing::_))
165 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
166 EXPECT_CALL(mock,
167 sd_event_source_set_userdata(expected_source, testing::_))
168 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
169 }
William A. Kennington III715c72f2018-07-17 14:40:14 -0700170 BaseImpl source(*event, expected_source, std::false_type());
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700171 EXPECT_NE(&source, userdata);
William A. Kennington III4863b962018-07-17 14:40:14 -0700172 EXPECT_EQ(expected_source, source.get());
173 EXPECT_NE(event.get(), &source.get_event());
174 EXPECT_EQ(expected_event, source.get_event().get());
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700175
176 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
177 .WillOnce(Return(userdata));
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700178 EXPECT_FALSE(source.get_prepare());
William A. Kennington III0a816c52018-07-17 14:40:14 -0700179
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700180 expect_base_destruct(*event, expected_source);
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700181 destroy(userdata);
William A. Kennington III7c24e052018-07-17 14:40:14 -0700182}
183
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700184TEST_F(BaseTest, UserdataOutlives)
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700185{
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700186 EXPECT_CALL(mock, sd_event_ref(expected_event))
187 .WillOnce(Return(expected_event));
188 sd_event_destroy_t destroy;
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700189 void* userdata;
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700190 {
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700191 testing::InSequence seq;
192 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
193 testing::_))
194 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
William A. Kennington III2d943ea2018-07-17 14:40:14 -0700195 EXPECT_CALL(mock,
196 sd_event_source_set_userdata(expected_source, testing::_))
197 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700198 }
Patrick Williamsa8c11e32023-05-10 07:50:56 -0500199 auto source = std::make_unique<BaseImpl>(*event, expected_source,
200 std::false_type());
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700201 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
202 .WillRepeatedly(Return(userdata));
203 EXPECT_FALSE(source->get_prepare());
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700204
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700205 expect_base_destruct(*event, expected_source);
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700206 source.reset();
207 EXPECT_FALSE(reinterpret_cast<BaseImpl*>(userdata)->get_prepare());
208 destroy(userdata);
William A. Kennington IIIcc6b12b2018-07-17 14:40:14 -0700209}
210
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700211TEST_F(BaseTest, CopyCorrectness)
212{
213 std::unique_ptr<BaseImpl> base1, base2;
214 std::function<void()> destroy;
215 std::tie(base1, destroy) = make_base(*event, expected_source);
216 set_prepare_placeholder(*base1);
217 EXPECT_TRUE(base1->get_prepare());
218
219 EXPECT_CALL(mock, sd_event_ref(expected_event))
220 .WillOnce(Return(expected_event));
221 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
222 .WillOnce(Return(expected_source));
223 base2 = std::make_unique<BaseImpl>(*base1);
224 EXPECT_EQ(&base1->get_prepare(), &base2->get_prepare());
225
226 empty_base(std::move(*base1));
227 EXPECT_THROW(base1->get_prepare(), std::bad_optional_access);
228 EXPECT_CALL(mock, sd_event_ref(expected_event))
229 .WillOnce(Return(expected_event));
230 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
231 .WillOnce(Return(expected_source));
232 *base1 = *base2;
233 EXPECT_EQ(&base1->get_prepare(), &base2->get_prepare());
234
235 expect_base_destruct(*event, expected_source);
236 base2.reset();
237 expect_base_destruct(*event, expected_source);
238 base1.reset();
239 destroy();
240}
241
William A. Kennington III65db8632018-07-17 14:40:14 -0700242class BaseMethodTest : public BaseTest
243{
244 protected:
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700245 std::unique_ptr<BaseImpl> base;
246 std::function<void()> destroy;
247
248 void SetUp()
249 {
250 std::tie(base, destroy) = make_base(*event, expected_source);
251 }
William A. Kennington III65db8632018-07-17 14:40:14 -0700252
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700253 void TearDown()
William A. Kennington III65db8632018-07-17 14:40:14 -0700254 {
William A. Kennington IIIe3e1df02018-07-17 14:40:14 -0700255 expect_base_destruct(base->get_event(), base->get());
William A. Kennington III715c72f2018-07-17 14:40:14 -0700256 base.reset();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700257 destroy();
William A. Kennington III65db8632018-07-17 14:40:14 -0700258 }
259};
260
261TEST_F(BaseMethodTest, GetDescriptionSuccess)
262{
263 const char* expected = "test_desc";
264 EXPECT_CALL(mock,
265 sd_event_source_get_description(expected_source, testing::_))
266 .WillOnce(DoAll(SetArgPointee<1>(expected), Return(0)));
267 // Intentionally comparing pointers to make sure no copying is happening
268 EXPECT_EQ(expected, base->get_description());
269}
270
271TEST_F(BaseMethodTest, GetDescriptionError)
272{
273 EXPECT_CALL(mock,
274 sd_event_source_get_description(expected_source, testing::_))
275 .WillOnce(Return(-EINVAL));
276 EXPECT_THROW(base->get_description(), SdEventError);
277}
278
279TEST_F(BaseMethodTest, SetDescriptionSuccess)
280{
281 const char* expected = "test desc";
282 // Intentionally comparing pointers to make sure no copying is happening
283 EXPECT_CALL(mock,
284 sd_event_source_set_description(expected_source, expected))
285 .WillOnce(Return(0));
286 base->set_description(expected);
287}
288
289TEST_F(BaseMethodTest, SetDescriptionError)
290{
291 const char* expected = "test desc";
292 // Intentionally comparing pointers to make sure no copying is happening
293 EXPECT_CALL(mock,
294 sd_event_source_set_description(expected_source, expected))
295 .WillOnce(Return(-EINVAL));
296 EXPECT_THROW(base->set_description(expected), SdEventError);
297}
298
William A. Kennington III48c42752018-07-17 14:40:14 -0700299TEST_F(BaseMethodTest, SetPrepareCallback)
300{
301 bool completed = false;
302 Base::Callback callback = [&completed](Base&) { completed = true; };
303 sd_event_handler_t event_handler;
304 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
305 .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0)));
306 base->set_prepare(std::move(callback));
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700307 EXPECT_TRUE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700308 EXPECT_FALSE(callback);
309 EXPECT_FALSE(completed);
310
311 EXPECT_EQ(0, event_handler(nullptr, base.get()));
312 EXPECT_TRUE(completed);
313}
314
315TEST_F(BaseMethodTest, SetPrepareCallbackNoUserdata)
316{
317 Base::Callback callback = [](Base&) {};
318 sd_event_handler_t event_handler;
319 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
320 .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0)));
321 base->set_prepare(std::move(callback));
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700322 EXPECT_TRUE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700323 EXPECT_FALSE(callback);
324
325 EXPECT_EQ(-EINVAL, event_handler(nullptr, nullptr));
326}
327
William A. Kennington III48c42752018-07-17 14:40:14 -0700328TEST_F(BaseMethodTest, SetPrepareError)
329{
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700330 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
331 .WillOnce(Return(0));
William A. Kennington III651e87a2018-12-11 16:45:37 -0800332 base->set_prepare([](Base&) {});
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700333 EXPECT_TRUE(base->get_prepare());
334
William A. Kennington III48c42752018-07-17 14:40:14 -0700335 Base::Callback callback = [](Base&) {};
336 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
337 .WillOnce(Return(-EINVAL));
338 EXPECT_THROW(base->set_prepare(std::move(callback)), SdEventError);
William A. Kennington IIIf5285c72018-07-17 14:40:14 -0700339 EXPECT_FALSE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700340 EXPECT_TRUE(callback);
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700341}
342
343TEST_F(BaseMethodTest, SetPrepareNull)
344{
345 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_))
346 .WillOnce(Return(0));
William A. Kennington III651e87a2018-12-11 16:45:37 -0800347 base->set_prepare([](Base&) {});
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -0700348 EXPECT_TRUE(base->get_prepare());
349
350 EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, nullptr))
351 .WillOnce(Return(0));
352 base->set_prepare(nullptr);
353 EXPECT_FALSE(base->get_prepare());
William A. Kennington III48c42752018-07-17 14:40:14 -0700354}
355
William A. Kennington III65db8632018-07-17 14:40:14 -0700356TEST_F(BaseMethodTest, GetPendingSuccess)
357{
358 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
359 .WillOnce(Return(0));
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700360 EXPECT_FALSE(base->get_pending());
William A. Kennington III65db8632018-07-17 14:40:14 -0700361 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
362 .WillOnce(Return(4));
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700363 EXPECT_TRUE(base->get_pending());
William A. Kennington III65db8632018-07-17 14:40:14 -0700364}
365
366TEST_F(BaseMethodTest, GetPendingError)
367{
368 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
369 .WillOnce(Return(-EINVAL));
370 EXPECT_THROW(base->get_pending(), SdEventError);
371}
372
373TEST_F(BaseMethodTest, GetPrioritySuccess)
374{
375 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
376 .WillOnce(DoAll(SetArgPointee<1>(1024), Return(0)));
377 EXPECT_EQ(1024, base->get_priority());
378}
379
380TEST_F(BaseMethodTest, GetPriorityError)
381{
382 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
383 .WillOnce(Return(-EINVAL));
384 EXPECT_THROW(base->get_priority(), SdEventError);
385}
386
387TEST_F(BaseMethodTest, SetPrioritySuccess)
388{
389 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
390 .WillOnce(Return(0));
391 base->set_priority(1024);
392}
393
394TEST_F(BaseMethodTest, SetPriorityError)
395{
396 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
397 .WillOnce(Return(-EINVAL));
398 EXPECT_THROW(base->set_priority(1024), SdEventError);
399}
400
401TEST_F(BaseMethodTest, GetEnabledSuccess)
402{
403 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
404 .WillOnce(DoAll(SetArgPointee<1>(SD_EVENT_ON), Return(0)));
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700405 EXPECT_EQ(Enabled::On, base->get_enabled());
William A. Kennington III65db8632018-07-17 14:40:14 -0700406}
407
408TEST_F(BaseMethodTest, GetEnabledError)
409{
410 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
411 .WillOnce(Return(-EINVAL));
412 EXPECT_THROW(base->get_enabled(), SdEventError);
413}
414
415TEST_F(BaseMethodTest, SetEnabledSuccess)
416{
417 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
418 .WillOnce(Return(0));
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700419 base->set_enabled(Enabled::On);
William A. Kennington III65db8632018-07-17 14:40:14 -0700420}
421
422TEST_F(BaseMethodTest, SetEnabledError)
423{
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700424 EXPECT_CALL(mock,
425 sd_event_source_set_enabled(expected_source, SD_EVENT_ONESHOT))
William A. Kennington III65db8632018-07-17 14:40:14 -0700426 .WillOnce(Return(-EINVAL));
William A. Kennington III8fd0cd42018-07-23 18:33:04 -0700427 EXPECT_THROW(base->set_enabled(Enabled::OneShot), SdEventError);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700428}
429
William A. Kennington III43493b52019-03-29 21:29:04 -0700430TEST_F(BaseMethodTest, GetFloatingSuccess)
431{
432 EXPECT_CALL(mock, sd_event_source_get_floating(expected_source))
433 .WillOnce(Return(2));
434 EXPECT_TRUE(base->get_floating());
435 EXPECT_CALL(mock, sd_event_source_get_floating(expected_source))
436 .WillOnce(Return(0));
437 EXPECT_FALSE(base->get_floating());
438}
439
440TEST_F(BaseMethodTest, GetFloatingError)
441{
442 EXPECT_CALL(mock, sd_event_source_get_floating(expected_source))
443 .WillOnce(Return(-EINVAL));
444 EXPECT_THROW(base->get_floating(), SdEventError);
445}
446
447TEST_F(BaseMethodTest, SetFloatingSuccess)
448{
449 EXPECT_CALL(mock, sd_event_source_set_floating(expected_source, 1))
450 .WillOnce(Return(0));
451 base->set_floating(true);
452}
453
454TEST_F(BaseMethodTest, SetFloatingError)
455{
456 EXPECT_CALL(mock, sd_event_source_set_floating(expected_source, 1))
457 .WillOnce(Return(-EINVAL));
458 EXPECT_THROW(base->set_floating(true), SdEventError);
459}
460
William A. Kennington III0a816c52018-07-17 14:40:14 -0700461} // namespace
462} // namespace source
463} // namespace sdeventplus