blob: e912b43d24acf25766bd42589ad1f57cbead6188 [file] [log] [blame]
Brad Bishop13fd8722017-05-15 12:44:01 -04001#pragma once
Brad Bishop13fd8722017-05-15 12:44:01 -04002#include "data_types.hpp"
3#include "sdbusplus/bus/match.hpp"
4
Patrick Venture3d6d3182018-08-31 09:33:09 -07005#include <string>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
Brad Bishop13fd8722017-05-15 12:44:01 -040010namespace phosphor
11{
12namespace dbus
13{
14namespace monitoring
15{
16
17/** @class CallMethodAndRead
18 * @brief GMock template member forwarding helper.
19 *
20 * The code under test calls callMethodAndRead, which is a templated,
21 * free function. Enable this under GMock by forwarding calls to it
22 * to functions that can be mocked.
23 *
24 * @tparam DBusInterfaceType - The mock object type.
25 * @tparam Ret - The return type of the method being called.
26 * @tparam Args - The argument types of the method being called.
27 *
28 * Specialize to implement new forwards.
29 */
Brad Bishopd1eac882018-03-29 10:34:05 -040030template <typename DBusInterfaceType, typename Ret, typename... Args>
Brad Bishop13fd8722017-05-15 12:44:01 -040031struct CallMethodAndRead
32{
Brad Bishopd1eac882018-03-29 10:34:05 -040033 static Ret op(DBusInterfaceType& dbus, const std::string& busName,
34 const std::string& path, const std::string& interface,
35 const std::string& method, Args&&... args)
Brad Bishop13fd8722017-05-15 12:44:01 -040036 {
37 static_assert(true, "Missing CallMethodAndRead definition.");
38 return Ret();
39 }
40};
41
42/** @brief CallMethodAndRead specialization for
43 * xyz.openbmc_project.ObjectMapper.GetObject. */
44template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040045struct CallMethodAndRead<DBusInterfaceType, GetObject, const MapperPath&,
46 const std::vector<std::string>&>
Brad Bishop13fd8722017-05-15 12:44:01 -040047{
Brad Bishopd1eac882018-03-29 10:34:05 -040048 static GetObject op(DBusInterfaceType& dbus, const std::string& busName,
49 const std::string& path, const std::string& interface,
50 const std::string& method, const MapperPath& objectPath,
51 const std::vector<std::string>& interfaces)
Brad Bishop13fd8722017-05-15 12:44:01 -040052 {
Brad Bishopd1eac882018-03-29 10:34:05 -040053 return dbus.mapperGetObject(busName, path, interface, method,
54 objectPath, interfaces);
Brad Bishop13fd8722017-05-15 12:44:01 -040055 }
56};
57
58/** @brief CallMethodAndRead specialization for
59 * org.freedesktop.DBus.Properties.GetAll(uint64_t). */
60template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040061struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint64_t>,
62 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040063{
Brad Bishopd1eac882018-03-29 10:34:05 -040064 static PropertiesChanged<uint64_t>
65 op(DBusInterfaceType& dbus, const std::string& busName,
66 const std::string& path, const std::string& interface,
67 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -040068 {
Brad Bishopd1eac882018-03-29 10:34:05 -040069 return dbus.getPropertiesU64(busName, path, interface, method,
70 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -040071 }
72};
73
74/** @brief CallMethodAndRead specialization for
75 * org.freedesktop.DBus.Properties.GetAll(uint32_t). */
76template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040077struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint32_t>,
78 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040079{
Brad Bishopd1eac882018-03-29 10:34:05 -040080 static PropertiesChanged<uint32_t>
81 op(DBusInterfaceType& dbus, const std::string& busName,
82 const std::string& path, const std::string& interface,
83 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -040084 {
Brad Bishopd1eac882018-03-29 10:34:05 -040085 return dbus.getPropertiesU32(busName, path, interface, method,
86 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -040087 }
88};
89
90/** @brief CallMethodAndRead specialization for
91 * org.freedesktop.DBus.Properties.GetAll(uint16_t). */
92template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -040093struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint16_t>,
94 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -040095{
Brad Bishopd1eac882018-03-29 10:34:05 -040096 static PropertiesChanged<uint16_t>
97 op(DBusInterfaceType& dbus, const std::string& busName,
98 const std::string& path, const std::string& interface,
99 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400100 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400101 return dbus.getPropertiesU16(busName, path, interface, method,
102 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400103 }
104};
105
106/** @brief CallMethodAndRead specialization for
107 * org.freedesktop.DBus.Properties.GetAll(uint8_t). */
108template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400109struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<uint8_t>,
110 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400111{
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 static PropertiesChanged<uint8_t>
113 op(DBusInterfaceType& dbus, const std::string& busName,
114 const std::string& path, const std::string& interface,
115 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400116 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400117 return dbus.getPropertiesU8(busName, path, interface, method,
118 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400119 }
120};
121
122/** @brief CallMethodAndRead specialization for
123 * org.freedesktop.DBus.Properties.GetAll(int64_t). */
124template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400125struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int64_t>,
126 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400127{
Brad Bishopd1eac882018-03-29 10:34:05 -0400128 static PropertiesChanged<int64_t>
129 op(DBusInterfaceType& dbus, const std::string& busName,
130 const std::string& path, const std::string& interface,
131 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400132 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400133 return dbus.getPropertiesU64(busName, path, interface, method,
134 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400135 }
136};
137
138/** @brief CallMethodAndRead specialization for
139 * org.freedesktop.DBus.Properties.GetAll(int32_t). */
140template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400141struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int32_t>,
142 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400143{
Brad Bishopd1eac882018-03-29 10:34:05 -0400144 static PropertiesChanged<int32_t>
145 op(DBusInterfaceType& dbus, const std::string& busName,
146 const std::string& path, const std::string& interface,
147 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400148 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400149 return dbus.getPropertiesU32(busName, path, interface, method,
150 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400151 }
152};
153
154/** @brief CallMethodAndRead specialization for
155 * org.freedesktop.DBus.Properties.GetAll(int16_t). */
156template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400157struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int16_t>,
158 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400159{
Brad Bishopd1eac882018-03-29 10:34:05 -0400160 static PropertiesChanged<int16_t>
161 op(DBusInterfaceType& dbus, const std::string& busName,
162 const std::string& path, const std::string& interface,
163 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400164 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400165 return dbus.getPropertiesU16(busName, path, interface, method,
166 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400167 }
168};
169
170/** @brief CallMethodAndRead specialization for
171 * org.freedesktop.DBus.Properties.GetAll(int8_t). */
172template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400173struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<int8_t>,
174 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400175{
Brad Bishopd1eac882018-03-29 10:34:05 -0400176 static PropertiesChanged<int8_t>
177 op(DBusInterfaceType& dbus, const std::string& busName,
178 const std::string& path, const std::string& interface,
179 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400180 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400181 return dbus.getPropertiesU8(busName, path, interface, method,
182 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400183 }
184};
185
186/** @brief CallMethodAndRead specialization for
187 * org.freedesktop.DBus.Properties.GetAll(std::string). */
188template <typename DBusInterfaceType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400189struct CallMethodAndRead<DBusInterfaceType, PropertiesChanged<std::string>,
190 const std::string&>
Brad Bishop13fd8722017-05-15 12:44:01 -0400191{
Brad Bishopd1eac882018-03-29 10:34:05 -0400192 static PropertiesChanged<std::string>
193 op(DBusInterfaceType& dbus, const std::string& busName,
194 const std::string& path, const std::string& interface,
195 const std::string& method, const std::string& propertiesInterface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400196 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400197 return dbus.getPropertiesString(busName, path, interface, method,
198 propertiesInterface);
Brad Bishop13fd8722017-05-15 12:44:01 -0400199 }
200};
201
202/** @class MockDBusInterface
203 * @brief DBus access delegate implementation for the property watch test
204 * suite.
205 */
206struct MockDBusInterface
207{
Brad Bishopd1eac882018-03-29 10:34:05 -0400208 MOCK_METHOD6(mapperGetObject,
209 GetObject(const std::string&, const std::string&,
210 const std::string&, const std::string&,
211 const MapperPath&, const std::vector<std::string>&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400212
Brad Bishopd1eac882018-03-29 10:34:05 -0400213 MOCK_METHOD5(getPropertiesU64,
214 PropertiesChanged<uint64_t>(
215 const std::string&, const std::string&, const std::string&,
216 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400217
Brad Bishopd1eac882018-03-29 10:34:05 -0400218 MOCK_METHOD5(getPropertiesU32,
219 PropertiesChanged<uint32_t>(
220 const std::string&, const std::string&, const std::string&,
221 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400222
Brad Bishopd1eac882018-03-29 10:34:05 -0400223 MOCK_METHOD5(getPropertiesU16,
224 PropertiesChanged<uint16_t>(
225 const std::string&, const std::string&, const std::string&,
226 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400227
Brad Bishopd1eac882018-03-29 10:34:05 -0400228 MOCK_METHOD5(getPropertiesU8,
229 PropertiesChanged<uint8_t>(
230 const std::string&, const std::string&, const std::string&,
231 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400232
Brad Bishopd1eac882018-03-29 10:34:05 -0400233 MOCK_METHOD5(getPropertiesS64,
234 PropertiesChanged<int64_t>(
235 const std::string&, const std::string&, const std::string&,
236 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400237
Brad Bishopd1eac882018-03-29 10:34:05 -0400238 MOCK_METHOD5(getPropertiesS32,
239 PropertiesChanged<int32_t>(
240 const std::string&, const std::string&, const std::string&,
241 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400242
Brad Bishopd1eac882018-03-29 10:34:05 -0400243 MOCK_METHOD5(getPropertiesS16,
244 PropertiesChanged<int16_t>(
245 const std::string&, const std::string&, const std::string&,
246 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400247
Brad Bishopd1eac882018-03-29 10:34:05 -0400248 MOCK_METHOD5(getPropertiesS8,
249 PropertiesChanged<int8_t>(
250 const std::string&, const std::string&, const std::string&,
251 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400252
Brad Bishopd1eac882018-03-29 10:34:05 -0400253 MOCK_METHOD5(getPropertiesString,
254 PropertiesChanged<std::string>(
255 const std::string&, const std::string&, const std::string&,
256 const std::string&, const std::string&));
Brad Bishop13fd8722017-05-15 12:44:01 -0400257
Patrick Williams413a4852022-07-22 19:26:52 -0500258 MOCK_METHOD2(fwdAddMatch, void(const std::string&,
259 const sdbusplus::bus::match_t::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>
Patrick Williamseab4f8c2024-08-16 15:20:10 -0400273 static auto callMethodAndRead(
274 const std::string& busName, const std::string& path,
275 const std::string& interface, const std::string& method, Args&&... args)
Brad Bishop13fd8722017-05-15 12:44:01 -0400276 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400277 return CallMethodAndRead<MockDBusInterface, Ret, Args...>::op(
278 instance(), busName, path, interface, method,
279 std::forward<Args>(args)...);
Brad Bishop13fd8722017-05-15 12:44:01 -0400280 }
281
282 /** @brief GMock free function forward. */
Patrick Williams413a4852022-07-22 19:26:52 -0500283 static auto addMatch(const std::string& match,
284 const sdbusplus::bus::match_t::callback_t& callback)
Brad Bishop13fd8722017-05-15 12:44:01 -0400285 {
286 instance().fwdAddMatch(match, callback);
287 }
288};
289
290/** @class Expect
291 * @brief Enable use of EXPECT_CALL from a C++ template.
292 */
Patrick Venture3d6d3182018-08-31 09:33:09 -0700293template <typename T>
294struct Expect
George Liu3fe976c2022-06-21 09:37:04 +0800295{};
Brad Bishop13fd8722017-05-15 12:44:01 -0400296
Patrick Venture3d6d3182018-08-31 09:33:09 -0700297template <>
298struct Expect<uint64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400299{
300 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400301 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
302 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400303 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400304 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
305 getPropertiesU64(::testing::_, path,
306 "org.freedesktop.DBus.Properties",
307 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400308 }
309};
310
Patrick Venture3d6d3182018-08-31 09:33:09 -0700311template <>
312struct 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
Patrick Venture3d6d3182018-08-31 09:33:09 -0700325template <>
326struct Expect<uint16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400327{
328 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400329 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
330 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400331 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400332 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
333 getPropertiesU16(::testing::_, path,
334 "org.freedesktop.DBus.Properties",
335 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400336 }
337};
338
Patrick Venture3d6d3182018-08-31 09:33:09 -0700339template <>
340struct Expect<uint8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400341{
342 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400343 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
344 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400345 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400346 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
347 getPropertiesU8(::testing::_, path,
348 "org.freedesktop.DBus.Properties",
349 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400350 }
351};
352
Patrick Venture3d6d3182018-08-31 09:33:09 -0700353template <>
354struct Expect<int64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400355{
356 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400357 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
358 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400359 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400360 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
361 getPropertiesS64(::testing::_, path,
362 "org.freedesktop.DBus.Properties",
363 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400364 }
365};
366
Patrick Venture3d6d3182018-08-31 09:33:09 -0700367template <>
368struct Expect<int32_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400369{
370 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400371 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
372 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400373 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400374 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
375 getPropertiesS32(::testing::_, path,
376 "org.freedesktop.DBus.Properties",
377 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400378 }
379};
380
Patrick Venture3d6d3182018-08-31 09:33:09 -0700381template <>
382struct Expect<int16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400383{
384 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400385 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
386 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400387 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400388 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
389 getPropertiesS16(::testing::_, path,
390 "org.freedesktop.DBus.Properties",
391 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400392 }
393};
394
Patrick Venture3d6d3182018-08-31 09:33:09 -0700395template <>
396struct Expect<int8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -0400397{
398 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400399 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
400 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400401 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400402 return EXPECT_CALL(std::forward<MockObjType>(mockObj),
403 getPropertiesS8(::testing::_, path,
404 "org.freedesktop.DBus.Properties",
405 "GetAll", interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400406 }
407};
408
Patrick Venture3d6d3182018-08-31 09:33:09 -0700409template <>
410struct Expect<std::string>
Brad Bishop13fd8722017-05-15 12:44:01 -0400411{
412 template <typename MockObjType>
Brad Bishopd1eac882018-03-29 10:34:05 -0400413 static auto& getProperties(MockObjType&& mockObj, const std::string& path,
414 const std::string& interface)
Brad Bishop13fd8722017-05-15 12:44:01 -0400415 {
416 return EXPECT_CALL(
Brad Bishopd1eac882018-03-29 10:34:05 -0400417 std::forward<MockObjType>(mockObj),
418 getPropertiesString(::testing::_, path,
419 "org.freedesktop.DBus.Properties", "GetAll",
420 interface));
Brad Bishop13fd8722017-05-15 12:44:01 -0400421 }
422};
423
424} // namespace monitoring
425} // namespace dbus
426} // namespace phosphor