blob: b6d6ac8bad885eb794d3290da766adfe1715e539 [file] [log] [blame]
Brad Bishop9cc42ab2018-12-12 16:36:51 -05001#include "../interface_ops.hpp"
2
3#include <sdbusplus/test/sdbus_mock.hpp>
4
5#include <gtest/gtest.h>
6
7using namespace phosphor::inventory::manager;
8using namespace testing;
9using namespace std::string_literals;
10
11struct MockInterface;
12struct DummyInterfaceWithProperties;
13
14MockInterface* g_currentMock = nullptr;
15
16using FakeVariantType = int;
17using InterfaceVariant = std::map<std::string, FakeVariantType>;
18
19struct MockInterface
20{
21 MockInterface()
22 {
23 g_currentMock = this;
24 }
25 ~MockInterface()
26 {
27 g_currentMock = nullptr;
28 }
29 MockInterface(const MockInterface&) = delete;
30 MockInterface& operator=(const MockInterface&) = delete;
31 // Not supporting move semantics simply because they aren't needed.
32 MockInterface(MockInterface&&) = delete;
33 MockInterface& operator=(MockInterface&&) = delete;
34
35 // We'll be getting calls proxyed through other objects.
Brad Bishope96f2aa2020-12-06 19:09:09 -050036 MOCK_METHOD3(constructWithProperties,
37 void(const char*, const InterfaceVariant& i, bool));
Brad Bishop9cc42ab2018-12-12 16:36:51 -050038 MOCK_METHOD1(constructWithoutProperties, void(const char*));
Brad Bishope96f2aa2020-12-06 19:09:09 -050039 MOCK_METHOD3(setPropertyByName, void(std::string, FakeVariantType, bool));
Brad Bishop9cc42ab2018-12-12 16:36:51 -050040
41 MOCK_METHOD2(serializeTwoArgs,
42 void(const std::string&, const std::string&));
43 MOCK_METHOD3(serializeThreeArgs,
44 void(const std::string&, const std::string&,
45 const DummyInterfaceWithProperties&));
46
47 MOCK_METHOD0(deserializeNoop, void());
48 MOCK_METHOD3(deserializeThreeArgs,
49 void(const std::string&, const std::string&,
50 DummyInterfaceWithProperties&));
51};
52
53struct DummyInterfaceWithoutProperties
54{
Patrick Williams563306f2022-07-22 19:26:52 -050055 DummyInterfaceWithoutProperties(sdbusplus::bus_t&, const char* name)
Brad Bishop9cc42ab2018-12-12 16:36:51 -050056 {
57 g_currentMock->constructWithoutProperties(name);
58 }
59};
60
61struct DummyInterfaceWithProperties
62{
63 using PropertiesVariant = FakeVariantType;
64
Patrick Williams563306f2022-07-22 19:26:52 -050065 DummyInterfaceWithProperties(sdbusplus::bus_t&, const char* name,
Brad Bishope96f2aa2020-12-06 19:09:09 -050066 const InterfaceVariant& i, bool skipSignal)
Brad Bishop9cc42ab2018-12-12 16:36:51 -050067 {
Brad Bishope96f2aa2020-12-06 19:09:09 -050068 g_currentMock->constructWithProperties(name, i, skipSignal);
Brad Bishop9cc42ab2018-12-12 16:36:51 -050069 }
70
Brad Bishope96f2aa2020-12-06 19:09:09 -050071 void setPropertyByName(std::string name, PropertiesVariant val,
72 bool skipSignal)
Brad Bishop9cc42ab2018-12-12 16:36:51 -050073 {
Brad Bishope96f2aa2020-12-06 19:09:09 -050074 g_currentMock->setPropertyByName(name, val, skipSignal);
Brad Bishop9cc42ab2018-12-12 16:36:51 -050075 }
76};
77
78struct SerialForwarder
79{
80 static void serialize(const std::string& path, const std::string& iface)
81 {
82 g_currentMock->serializeTwoArgs(path, iface);
83 }
84
85 static void serialize(const std::string& path, const std::string& iface,
86 const DummyInterfaceWithProperties& obj)
87 {
88 g_currentMock->serializeThreeArgs(path, iface, obj);
89 }
90
George Liu23314a52022-04-13 18:26:03 +080091 static void deserialize(const std::string& /* path */,
92 const std::string& /* iface */)
Brad Bishop9cc42ab2018-12-12 16:36:51 -050093 {
94 g_currentMock->deserializeNoop();
95 }
96
97 static void deserialize(const std::string& path, const std::string& iface,
98 DummyInterfaceWithProperties& obj)
99 {
100 g_currentMock->deserializeThreeArgs(path, iface, obj);
101 }
102};
103
104TEST(InterfaceOpsTest, TestHasPropertiesNoProperties)
105{
106 EXPECT_FALSE(HasProperties<DummyInterfaceWithoutProperties>::value);
107}
108
109TEST(InterfaceOpsTest, TestHasPropertiesHasProperties)
110{
111 EXPECT_TRUE(HasProperties<DummyInterfaceWithProperties>::value);
112}
113
114TEST(InterfaceOpsTest, TestMakePropertylessInterfaceWithoutArguments)
115{
116 MockInterface mock;
117 Interface i;
118 sdbusplus::SdBusMock interface;
119
120 EXPECT_CALL(mock, constructWithoutProperties("foo")).Times(1);
Brad Bishope96f2aa2020-12-06 19:09:09 -0500121 EXPECT_CALL(mock, constructWithProperties(_, _, _)).Times(0);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500122
123 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400124 auto r =
125 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500126
127 EXPECT_NO_THROW(
128 std::any_cast<std::shared_ptr<DummyInterfaceWithoutProperties>>(r));
129}
130
131TEST(InterfaceOpsTest, TestMakePropertylessInterfaceWithOneArgument)
132{
133 MockInterface mock;
134 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
135 sdbusplus::SdBusMock interface;
136
137 EXPECT_CALL(mock, constructWithoutProperties("foo")).Times(1);
Brad Bishope96f2aa2020-12-06 19:09:09 -0500138 EXPECT_CALL(mock, constructWithProperties(_, _, _)).Times(0);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500139
140 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400141 auto r =
142 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500143
144 EXPECT_NO_THROW(
145 std::any_cast<std::shared_ptr<DummyInterfaceWithoutProperties>>(r));
146}
147
148TEST(InterfaceOpsTest, TestMakeInterfaceWithWithoutArguments)
149{
150 MockInterface mock;
151 Interface i;
152 sdbusplus::SdBusMock interface;
153
154 EXPECT_CALL(mock, constructWithoutProperties(_)).Times(0);
Brad Bishope96f2aa2020-12-06 19:09:09 -0500155 EXPECT_CALL(mock, constructWithProperties("bar", _, _)).Times(1);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500156
157 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400158 auto r =
159 MakeInterface<DummyInterfaceWithProperties>::op(b, "bar", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500160
161 EXPECT_NO_THROW(
162 std::any_cast<std::shared_ptr<DummyInterfaceWithProperties>>(r));
163}
164
165TEST(InterfaceOpsTest, TestMakeInterfaceWithOneArgument)
166{
167 MockInterface mock;
168 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
169 sdbusplus::SdBusMock interface;
170
171 EXPECT_CALL(mock, constructWithoutProperties(_)).Times(0);
Brad Bishope96f2aa2020-12-06 19:09:09 -0500172 EXPECT_CALL(mock, constructWithProperties("foo", _, _)).Times(1);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500173
174 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400175 auto r =
176 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500177
178 EXPECT_NO_THROW(
179 std::any_cast<std::shared_ptr<DummyInterfaceWithProperties>>(r));
180}
181
182TEST(InterfaceOpsTest, TestAssignPropertylessInterfaceWithoutArguments)
183{
184 MockInterface mock;
185 Interface i;
186 sdbusplus::SdBusMock interface;
187
Brad Bishope96f2aa2020-12-06 19:09:09 -0500188 EXPECT_CALL(mock, setPropertyByName(_, _, _)).Times(0);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500189
190 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400191 auto r =
192 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500193
Brad Bishope96f2aa2020-12-06 19:09:09 -0500194 AssignInterface<DummyInterfaceWithoutProperties>::op(i, r, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500195}
196
197TEST(InterfaceOpsTest, TestAssignPropertylessInterfaceWithOneArgument)
198{
199 MockInterface mock;
200 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
201 sdbusplus::SdBusMock interface;
202
Brad Bishope96f2aa2020-12-06 19:09:09 -0500203 EXPECT_CALL(mock, setPropertyByName(_, _, _)).Times(0);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500204
205 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400206 auto r =
207 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500208
Brad Bishope96f2aa2020-12-06 19:09:09 -0500209 AssignInterface<DummyInterfaceWithoutProperties>::op(i, r, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500210}
211
212TEST(InterfaceOpsTest, TestAssignInterfaceWithoutArguments)
213{
214 MockInterface mock;
215 Interface i;
216 sdbusplus::SdBusMock interface;
217
Brad Bishope96f2aa2020-12-06 19:09:09 -0500218 EXPECT_CALL(mock, setPropertyByName(_, _, _)).Times(0);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500219
220 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400221 auto r =
222 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500223
Brad Bishope96f2aa2020-12-06 19:09:09 -0500224 AssignInterface<DummyInterfaceWithProperties>::op(i, r, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500225}
226
227TEST(InterfaceOpsTest, TestAssignInterfaceWithOneArgument)
228{
229 MockInterface mock;
230 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
231 sdbusplus::SdBusMock interface;
232
Brad Bishope96f2aa2020-12-06 19:09:09 -0500233 EXPECT_CALL(mock, setPropertyByName("foo"s, 1ll, _)).Times(1);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500234
235 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400236 auto r =
237 MakeInterface<DummyInterfaceWithProperties>::op(b, "bar", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500238
Brad Bishope96f2aa2020-12-06 19:09:09 -0500239 AssignInterface<DummyInterfaceWithProperties>::op(i, r, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500240}
241
242TEST(InterfaceOpsTest, TestSerializePropertylessInterfaceWithoutArguments)
243{
244 MockInterface mock;
245 Interface i;
246 sdbusplus::SdBusMock interface;
247
248 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400249 auto r =
250 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500251
252 EXPECT_CALL(mock, serializeTwoArgs("/foo"s, "bar"s)).Times(1);
253
254 SerializeInterface<DummyInterfaceWithoutProperties, SerialForwarder>::op(
255 "/foo"s, "bar"s, r);
256}
257
258TEST(InterfaceOpsTest, TestSerializePropertylessInterfaceWithOneArgument)
259{
260 MockInterface mock;
261 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
262 sdbusplus::SdBusMock interface;
263
264 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400265 auto r =
266 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500267
268 EXPECT_CALL(mock, serializeTwoArgs("/foo"s, "bar"s)).Times(1);
269
270 SerializeInterface<DummyInterfaceWithoutProperties, SerialForwarder>::op(
271 "/foo"s, "bar"s, r);
272}
273
274TEST(InterfaceOpsTest, TestSerializeInterfaceWithNoArguments)
275{
276 MockInterface mock;
277 Interface i;
278 sdbusplus::SdBusMock interface;
279
280 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400281 auto r =
282 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500283
284 EXPECT_CALL(mock, serializeThreeArgs("/foo"s, "bar"s, _)).Times(1);
285
286 SerializeInterface<DummyInterfaceWithProperties, SerialForwarder>::op(
287 "/foo"s, "bar"s, r);
288}
289
290TEST(InterfaceOpsTest, TestSerializeInterfaceWithOneArgument)
291{
292 MockInterface mock;
293 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
294 sdbusplus::SdBusMock interface;
295
296 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400297 auto r =
298 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500299
300 EXPECT_CALL(mock, serializeThreeArgs("/foo"s, "bar"s, _)).Times(1);
301
302 SerializeInterface<DummyInterfaceWithProperties, SerialForwarder>::op(
303 "/foo"s, "bar"s, r);
304}
305
306TEST(InterfaceOpsTest, TestDeserializePropertylessInterfaceWithoutArguments)
307{
308 MockInterface mock;
309 Interface i;
310 sdbusplus::SdBusMock interface;
311
312 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400313 auto r =
314 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500315
316 EXPECT_CALL(mock, deserializeNoop()).Times(1);
317
318 DeserializeInterface<DummyInterfaceWithoutProperties, SerialForwarder>::op(
319 "/foo"s, "bar"s, r);
320}
321
322TEST(InterfaceOpsTest, TestDeserializePropertylessInterfaceWithOneArgument)
323{
324 MockInterface mock;
325 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
326 sdbusplus::SdBusMock interface;
327
328 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400329 auto r =
330 MakeInterface<DummyInterfaceWithoutProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500331
332 EXPECT_CALL(mock, deserializeNoop()).Times(1);
333
334 DeserializeInterface<DummyInterfaceWithoutProperties, SerialForwarder>::op(
335 "/foo"s, "bar"s, r);
336}
337
338TEST(InterfaceOpsTest, TestDeserializeInterfaceWithNoArguments)
339{
340 MockInterface mock;
341 Interface i;
342 sdbusplus::SdBusMock interface;
343
344 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400345 auto r =
346 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500347
348 EXPECT_CALL(mock, deserializeThreeArgs("/foo"s, "bar"s, _)).Times(1);
349
350 DeserializeInterface<DummyInterfaceWithProperties, SerialForwarder>::op(
351 "/foo"s, "bar"s, r);
352}
353
354TEST(InterfaceOpsTest, TestDeserializeInterfaceWithOneArgument)
355{
356 MockInterface mock;
357 Interface i{{"foo"s, static_cast<int64_t>(1ll)}};
358 sdbusplus::SdBusMock interface;
359
360 auto b = sdbusplus::get_mocked_new(&interface);
Patrick Williamsd8fba8b2024-08-16 15:20:15 -0400361 auto r =
362 MakeInterface<DummyInterfaceWithProperties>::op(b, "foo", i, false);
Brad Bishop9cc42ab2018-12-12 16:36:51 -0500363
364 EXPECT_CALL(mock, deserializeThreeArgs("/foo"s, "bar"s, _)).Times(1);
365
366 DeserializeInterface<DummyInterfaceWithProperties, SerialForwarder>::op(
367 "/foo"s, "bar"s, r);
368}