blob: d226f93bd4e86a4497b1698bdef60e888a58628e [file] [log] [blame]
Brad Bishop13fd8722017-05-15 12:44:01 -04001#pragma once
2#include <gmock/gmock.h>
3#include <gtest/gtest.h>
4#include <string>
5
6#include "data_types.hpp"
7#include "sdbusplus/bus/match.hpp"
8
9namespace phosphor
10{
11namespace dbus
12{
13namespace monitoring
14{
15
16/** @class CallMethodAndRead
17 * @brief GMock template member forwarding helper.
18 *
19 * The code under test calls callMethodAndRead, which is a templated,
20 * free function. Enable this under GMock by forwarding calls to it
21 * to functions that can be mocked.
22 *
23 * @tparam DBusInterfaceType - The mock object type.
24 * @tparam Ret - The return type of the method being called.
25 * @tparam Args - The argument types of the method being called.
26 *
27 * Specialize to implement new forwards.
28 */
Brad Bishopd1eac882018-03-29 10:34:05 -040029template <typename DBusInterfaceType, typename Ret, typename... Args>
Brad Bishop13fd8722017-05-15 12:44:01 -040030struct CallMethodAndRead
31{
Brad Bishopd1eac882018-03-29 10:34:05 -040032 static Ret op(DBusInterfaceType& dbus, const std::string& busName,
33 const std::string& path, const std::string& interface,
34 const std::string& method, Args&&... args)
Brad Bishop13fd8722017-05-15 12:44:01 -040035 {
36 static_assert(true, "Missing CallMethodAndRead definition.");
37 return Ret();
38 }
39};
40
41/** @brief CallMethodAndRead specialization for
42 * xyz.openbmc_project.ObjectMapper.GetObject. */
43template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040044struct CallMethodAndRead<DBusInterfaceType, GetObject, const MapperPath&,
45 const std::vector<std::string>&>
Brad Bishop13fd8722017-05-15 12:44:01 -040046{
Brad Bishopd1eac882018-03-29 10:34:05 -040047 static GetObject op(DBusInterfaceType& dbus, const std::string& busName,
48 const std::string& path, const std::string& interface,
49 const std::string& method, const MapperPath& objectPath,
50 const std::vector<std::string>& interfaces)
Brad Bishop13fd8722017-05-15 12:44:01 -040051 {
Brad Bishopd1eac882018-03-29 10:34:05 -040052 return dbus.mapperGetObject(busName, path, interface, method,
53 objectPath, interfaces);
Brad Bishop13fd8722017-05-15 12:44:01 -040054 }
55};
56
57/** @brief CallMethodAndRead specialization for
58 * org.freedesktop.DBus.Properties.GetAll(uint64_t). */
59template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040060struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint64_t>,
61 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040062{
Brad Bishopd1eac882018-03-29 10:34:05 -040063 static PropertiesChanged<uint64_t>
64 op(DBusInterfaceType& dbus, const std::string& busName,
65 const std::string& path, const std::string& interface,
66 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -040067 {
Brad Bishopd1eac882018-03-29 10:34:05 -040068 return dbus.getPropertiesU64(busName, path, interface, method,
69 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -040070 }
71};
72
73/** @brief CallMethodAndRead specialization for
74 * org.freedesktop.DBus.Properties.GetAll(uint32_t). */
75template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040076struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint32_t>,
77 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040078{
Brad Bishopd1eac882018-03-29 10:34:05 -040079 static PropertiesChanged<uint32_t>
80 op(DBusInterfaceType& dbus, const std::string& busName,
81 const std::string& path, const std::string& interface,
82 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -040083 {
Brad Bishopd1eac882018-03-29 10:34:05 -040084 return dbus.getPropertiesU32(busName, path, interface, method,
85 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -040086 }
87};
88
89/** @brief CallMethodAndRead specialization for
90 * org.freedesktop.DBus.Properties.GetAll(uint16_t). */
91template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040092struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint16_t>,
93 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040094{
Brad Bishopd1eac882018-03-29 10:34:05 -040095 static PropertiesChanged<uint16_t>
96 op(DBusInterfaceType& dbus, const std::string& busName,
97 const std::string& path, const std::string& interface,
98 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -040099 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400100 return dbus.getPropertiesU16(busName, path, interface, method,
101 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400102 }
103};
104
105/** @brief CallMethodAndRead specialization for
106 * org.freedesktop.DBus.Properties.GetAll(uint8_t). */
107template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400108struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint8_t>,
109 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400110{
Brad Bishopd1eac882018-03-29 10:34:05 -0400111 static PropertiesChanged<uint8_t>
112 op(DBusInterfaceType& dbus, const std::string& busName,
113 const std::string& path, const std::string& interface,
114 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400115 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400116 return dbus.getPropertiesU8(busName, path, interface, method,
117 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400118 }
119};
120
121/** @brief CallMethodAndRead specialization for
122 * org.freedesktop.DBus.Properties.GetAll(int64_t). */
123template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400124struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int64_t>,
125 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400126{
Brad Bishopd1eac882018-03-29 10:34:05 -0400127 static PropertiesChanged<int64_t>
128 op(DBusInterfaceType& dbus, const std::string& busName,
129 const std::string& path, const std::string& interface,
130 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400131 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400132 return dbus.getPropertiesU64(busName, path, interface, method,
133 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400134 }
135};
136
137/** @brief CallMethodAndRead specialization for
138 * org.freedesktop.DBus.Properties.GetAll(int32_t). */
139template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400140struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int32_t>,
141 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400142{
Brad Bishopd1eac882018-03-29 10:34:05 -0400143 static PropertiesChanged<int32_t>
144 op(DBusInterfaceType& dbus, const std::string& busName,
145 const std::string& path, const std::string& interface,
146 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400147 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400148 return dbus.getPropertiesU32(busName, path, interface, method,
149 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400150 }
151};
152
153/** @brief CallMethodAndRead specialization for
154 * org.freedesktop.DBus.Properties.GetAll(int16_t). */
155template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400156struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int16_t>,
157 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400158{
Brad Bishopd1eac882018-03-29 10:34:05 -0400159 static PropertiesChanged<int16_t>
160 op(DBusInterfaceType& dbus, const std::string& busName,
161 const std::string& path, const std::string& interface,
162 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400163 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400164 return dbus.getPropertiesU16(busName, path, interface, method,
165 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400166 }
167};
168
169/** @brief CallMethodAndRead specialization for
170 * org.freedesktop.DBus.Properties.GetAll(int8_t). */
171template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400172struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int8_t>,
173 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400174{
Brad Bishopd1eac882018-03-29 10:34:05 -0400175 static PropertiesChanged<int8_t>
176 op(DBusInterfaceType& dbus, const std::string& busName,
177 const std::string& path, const std::string& interface,
178 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400179 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400180 return dbus.getPropertiesU8(busName, path, interface, method,
181 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400182 }
183};
184
185/** @brief CallMethodAndRead specialization for
186 * org.freedesktop.DBus.Properties.GetAll(std::string). */
187template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400188struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<std::string>,
189 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400190{
Brad Bishopd1eac882018-03-29 10:34:05 -0400191 static PropertiesChanged<std::string>
192 op(DBusInterfaceType& dbus, const std::string& busName,
193 const std::string& path, const std::string& interface,
194 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400195 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400196 return dbus.getPropertiesString(busName, path, interface, method,
197 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400198 }
199};
200
201/** @class MockDBusInterface
202 * @brief DBus access delegate implementation for the property watch test
203 * suite.
204 */
205struct MockDBusInterface
206{
Brad Bishopd1eac882018-03-29 10:34:05 -0400207 MOCK_METHOD6(mapperGetObject,
208 GetObject(const std::string&, const std::string&,
209 const std::string&, const std::string&,
210 const MapperPath&, const std::vector<std::string>&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400211
Brad Bishopd1eac882018-03-29 10:34:05 -0400212 MOCK_METHOD5(getPropertiesU64,
213 PropertiesChanged<uint64_t>(
214 const std::string&, const std::string&, const std::string&,
215 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400216
Brad Bishopd1eac882018-03-29 10:34:05 -0400217 MOCK_METHOD5(getPropertiesU32,
218 PropertiesChanged<uint32_t>(
219 const std::string&, const std::string&, const std::string&,
220 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400221
Brad Bishopd1eac882018-03-29 10:34:05 -0400222 MOCK_METHOD5(getPropertiesU16,
223 PropertiesChanged<uint16_t>(
224 const std::string&, const std::string&, const std::string&,
225 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400226
Brad Bishopd1eac882018-03-29 10:34:05 -0400227 MOCK_METHOD5(getPropertiesU8,
228 PropertiesChanged<uint8_t>(
229 const std::string&, const std::string&, const std::string&,
230 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400231
Brad Bishopd1eac882018-03-29 10:34:05 -0400232 MOCK_METHOD5(getPropertiesS64,
233 PropertiesChanged<int64_t>(
234 const std::string&, const std::string&, const std::string&,
235 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400236
Brad Bishopd1eac882018-03-29 10:34:05 -0400237 MOCK_METHOD5(getPropertiesS32,
238 PropertiesChanged<int32_t>(
239 const std::string&, const std::string&, const std::string&,
240 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400241
Brad Bishopd1eac882018-03-29 10:34:05 -0400242 MOCK_METHOD5(getPropertiesS16,
243 PropertiesChanged<int16_t>(
244 const std::string&, const std::string&, const std::string&,
245 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400246
Brad Bishopd1eac882018-03-29 10:34:05 -0400247 MOCK_METHOD5(getPropertiesS8,
248 PropertiesChanged<int8_t>(
249 const std::string&, const std::string&, const std::string&,
250 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400251
Brad Bishopd1eac882018-03-29 10:34:05 -0400252 MOCK_METHOD5(getPropertiesString,
253 PropertiesChanged<std::string>(
254 const std::string&, const std::string&, const std::string&,
255 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400256
Brad Bishopd1eac882018-03-29 10:34:05 -0400257 MOCK_METHOD2(fwdAddMatch,
258 void(const std::string&,
259 const sdbusplus::bus::match::match::callback_t&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400260
261 static MockDBusInterface* ptr;
262 static MockDBusInterface& instance()
263 {
264 return *ptr;
265 }
266 static void instance(MockDBusInterface& p)
267 {
268 ptr = &p;
269 }
270
271 /** @brief GMock member template/free function forward. */
Brad Bishopd1eac882018-03-29 10:34:05 -0400272 template <typename Ret, typename... Args>
273 static auto callMethodAndRead(const std::string& busName,
274 const std::string& path,
275 const std::string& interface,
276 const std::string& method, Args&&... args)
Brad Bishop13fd8722017-05-15 12:44:01 -0400277 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400278 return CallMethodAndRead<MockDBusInterface, Ret, Args...>::op(
279 instance(), busName, path, interface, method,
280 std::forward<Args>(args)...);
Brad Bishop13fd8722017-05-15 12:44:01 -0400281 }
282
283 /** @brief GMock free function forward. */
Brad Bishopd1eac882018-03-29 10:34:05 -0400284 static auto
285 addMatch(const std::string& match,
286 const sdbusplus::bus::match::match::callback_t& callback)
Brad Bishop13fd8722017-05-15 12:44:01 -0400287 {
288 instance().fwdAddMatch(match, callback);
289 }
290};
291
292/** @class Expect
293 * @brief Enable use of EXPECT_CALL from a C++ template.
294 */
Brad Bishopd1eac882018-03-29 10:34:05 -0400295template <typename T> struct Expect
296{
297};
Brad Bishop13fd8722017-05-15 12:44:01 -0400298
Brad Bishopd1eac882018-03-29 10:34:05 -0400299template <> struct Expect<uint64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400300{
301 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400302 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
303 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400304 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400305 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
306 getPropertiesU64(::testing::_, path,
307 "org.freedesktop.DBus.Properties",
308 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400309 }
310};
311
Brad Bishopd1eac882018-03-29 10:34:05 -0400312template <> struct Expect<uint32_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400313{
314 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400315 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
316 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400317 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400318 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
319 getPropertiesU32(::testing::_, path,
320 "org.freedesktop.DBus.Properties",
321 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400322 }
323};
324
Brad Bishopd1eac882018-03-29 10:34:05 -0400325template <> struct Expect<uint16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400326{
327 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400328 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
329 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400330 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400331 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
332 getPropertiesU16(::testing::_, path,
333 "org.freedesktop.DBus.Properties",
334 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400335 }
336};
337
Brad Bishopd1eac882018-03-29 10:34:05 -0400338template <> struct Expect<uint8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400339{
340 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400341 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
342 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400343 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400344 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
345 getPropertiesU8(::testing::_, path,
346 "org.freedesktop.DBus.Properties",
347 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400348 }
349};
350
Brad Bishopd1eac882018-03-29 10:34:05 -0400351template <> struct Expect<int64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400352{
353 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400354 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
355 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400356 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400357 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
358 getPropertiesS64(::testing::_, path,
359 "org.freedesktop.DBus.Properties",
360 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400361 }
362};
363
Brad Bishopd1eac882018-03-29 10:34:05 -0400364template <> struct Expect<int32_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400365{
366 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400367 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
368 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400369 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400370 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
371 getPropertiesS32(::testing::_, path,
372 "org.freedesktop.DBus.Properties",
373 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400374 }
375};
376
Brad Bishopd1eac882018-03-29 10:34:05 -0400377template <> struct Expect<int16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400378{
379 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400380 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
381 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400382 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400383 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
384 getPropertiesS16(::testing::_, path,
385 "org.freedesktop.DBus.Properties",
386 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400387 }
388};
389
Brad Bishopd1eac882018-03-29 10:34:05 -0400390template <> struct Expect<int8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400391{
392 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400393 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
394 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400395 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400396 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
397 getPropertiesS8(::testing::_, path,
398 "org.freedesktop.DBus.Properties",
399 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400400 }
401};
402
Brad Bishopd1eac882018-03-29 10:34:05 -0400403template <> struct Expect<std::string>
Brad Bishop13fd8722017-05-15 12:44:01 -0400404{
405 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400406 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
407 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400408 {
409 return EXPECT_CALL(
Brad Bishopd1eac882018-03-29 10:34:05 -0400410 std::forward<MockObjType>(mockObj),
411 getPropertiesString(::testing::_, path,
412 "org.freedesktop.DBus.Properties", "GetAll",
413 interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400414 }
415};
416
417} // namespace monitoring
418} // namespace dbus
419} // namespace phosphor