#pragma once #include #include #include #include "data_types.hpp" #include "sdbusplus/bus/match.hpp" namespace phosphor { namespace dbus { namespace monitoring { /** @class CallMethodAndRead * @brief GMock template member forwarding helper. * * The code under test calls callMethodAndRead, which is a templated, * free function. Enable this under GMock by forwarding calls to it * to functions that can be mocked. * * @tparam DBusInterfaceType - The mock object type. * @tparam Ret - The return type of the method being called. * @tparam Args - The argument types of the method being called. * * Specialize to implement new forwards. */ template struct CallMethodAndRead { static Ret op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, Args&&... args) { static_assert(true, "Missing CallMethodAndRead definition."); return Ret(); } }; /** @brief CallMethodAndRead specialization for * xyz.openbmc_project.ObjectMapper.GetObject. */ template struct CallMethodAndRead&> { static GetObject op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const MapperPath& objectPath, const std::vector& interfaces) { return dbus.mapperGetObject(busName, path, interface, method, objectPath, interfaces); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(uint64_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU64(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(uint32_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU32(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(uint16_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU16(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(uint8_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU8(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(int64_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU64(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(int32_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU32(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(int16_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU16(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(int8_t). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesU8(busName, path, interface, method, propertiesInterface); } }; /** @brief CallMethodAndRead specialization for * org.freedesktop.DBus.Properties.GetAll(std::string). */ template struct CallMethodAndRead, const std::string&> { static PropertiesChanged op(DBusInterfaceType& dbus, const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, const std::string& propertiesInterface) { return dbus.getPropertiesString(busName, path, interface, method, propertiesInterface); } }; /** @class MockDBusInterface * @brief DBus access delegate implementation for the property watch test * suite. */ struct MockDBusInterface { MOCK_METHOD6(mapperGetObject, GetObject(const std::string&, const std::string&, const std::string&, const std::string&, const MapperPath&, const std::vector&)); MOCK_METHOD5(getPropertiesU64, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesU32, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesU16, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesU8, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesS64, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesS32, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesS16, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesS8, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD5(getPropertiesString, PropertiesChanged( const std::string&, const std::string&, const std::string&, const std::string&, const std::string&)); MOCK_METHOD2(fwdAddMatch, void(const std::string&, const sdbusplus::bus::match::match::callback_t&)); static MockDBusInterface* ptr; static MockDBusInterface& instance() { return *ptr; } static void instance(MockDBusInterface& p) { ptr = &p; } /** @brief GMock member template/free function forward. */ template static auto callMethodAndRead(const std::string& busName, const std::string& path, const std::string& interface, const std::string& method, Args&&... args) { return CallMethodAndRead::op( instance(), busName, path, interface, method, std::forward(args)...); } /** @brief GMock free function forward. */ static auto addMatch(const std::string& match, const sdbusplus::bus::match::match::callback_t& callback) { instance().fwdAddMatch(match, callback); } }; /** @class Expect * @brief Enable use of EXPECT_CALL from a C++ template. */ template struct Expect { }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesU64(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesU32(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesU16(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesU8(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesS64(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesS32(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesS16(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL(std::forward(mockObj), getPropertiesS8(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; template <> struct Expect { template static auto& getProperties(MockObjType&& mockObj, const std::string& path, const std::string& interface) { return EXPECT_CALL( std::forward(mockObj), getPropertiesString(::testing::_, path, "org.freedesktop.DBus.Properties", "GetAll", interface)); } }; } // namespace monitoring } // namespace dbus } // namespace phosphor