From 13fd8722e616dd424cd585187fb0ef65f6316023 Mon Sep 17 00:00:00 2001 From: Brad Bishop Date: Mon, 15 May 2017 12:44:01 -0400 Subject: Add testcases for property watches Signed-off-by: Brad Bishop Change-Id: I9e9266901414c71a34d9686f2a45bc4764602d53 --- src/test/propertywatchtest.cpp | 225 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 src/test/propertywatchtest.cpp (limited to 'src/test/propertywatchtest.cpp') diff --git a/src/test/propertywatchtest.cpp b/src/test/propertywatchtest.cpp new file mode 100644 index 0000000..b92dbec --- /dev/null +++ b/src/test/propertywatchtest.cpp @@ -0,0 +1,225 @@ +#include +#include "propertywatchimpl.hpp" +#include "propertywatchtest.hpp" + +using namespace std::string_literals; +using namespace phosphor::dbus::monitoring; + +const std::array paths = +{ + "/xyz/openbmc_project/testing/inst1"s, + "/xyz/openbmc_project/testing/inst2"s, + "/xyz/openbmc_project/testing/inst3"s, + "/xyz/openbmc_project/testing/inst4"s, +}; + +const std::array interfaces = +{ + "xyz.openbmc_project.Iface1"s, + "xyz.openbmc_project.Iface2"s, +}; + +const std::array properties = +{ + "Value1"s, + "Value2"s, +}; + +const std::string meta; + +std::array storage = { }; + +const PropertyIndex watchIndex = +{ + { + { + PropertyIndex::key_type{paths[0], interfaces[0], properties[0]}, + PropertyIndex::mapped_type{meta, meta, storage[0]} + }, + { + PropertyIndex::key_type{paths[0], interfaces[1], properties[1]}, + PropertyIndex::mapped_type{meta, meta, storage[1]} + }, + { + PropertyIndex::key_type{paths[1], interfaces[0], properties[0]}, + PropertyIndex::mapped_type{meta, meta, storage[2]} + }, + { + PropertyIndex::key_type{paths[1], interfaces[1], properties[1]}, + PropertyIndex::mapped_type{meta, meta, storage[3]} + }, + { + PropertyIndex::key_type{paths[2], interfaces[0], properties[0]}, + PropertyIndex::mapped_type{meta, meta, storage[4]} + }, + { + PropertyIndex::key_type{paths[2], interfaces[1], properties[1]}, + PropertyIndex::mapped_type{meta, meta, storage[5]} + }, + { + PropertyIndex::key_type{paths[3], interfaces[0], properties[0]}, + PropertyIndex::mapped_type{meta, meta, storage[6]} + }, + { + PropertyIndex::key_type{paths[3], interfaces[1], properties[1]}, + PropertyIndex::mapped_type{meta, meta, storage[7]} + }, + }, +}; + +template struct ExpectedValues {}; +template <> struct ExpectedValues +{ + static auto& get(size_t i) + { + static const std::array values = + { + {0, 1, 2, 3, 4, 5, 6, 7}, + }; + return values[i]; + } +}; + +template <> struct ExpectedValues +{ + static auto& get(size_t i) + { + static const std::array values = + { + {88, 77, 66, 55, 44, 33, 22, 11}, + }; + return values[i]; + } +}; + +template <> struct ExpectedValues +{ + static auto& get(size_t i) + { + static const std::array values = + { + {0xffffffff, 1, 3, 0, 5, 7, 9, 0xffffffff}, + }; + return values[i]; + } +}; + +template <> struct ExpectedValues +{ + static auto& get(size_t i) + { + static const std::array values = + { + {0xffffffffffffffff, 3, 7, 12234, 0, 3, 9, 0xffffffff}, + }; + return values[i]; + } +}; + +template <> struct ExpectedValues +{ + static auto& get(size_t i) + { + static const std::array values = + { + {""s, "foo"s, "bar"s, "baz"s, "hello"s, "string", "\x2\x3", "\\"}, + }; + return values[i]; + } +}; + +template +void testStart() +{ + using ::testing::Return; + using ::testing::_; + + MockDBusInterface dbus; + MockDBusInterface::instance(dbus); + + const std::vector expectedMapperInterfaces; + PropertyWatchOfType watch(watchIndex); + + auto ndx = static_cast(0); + for (const auto& o : convert(watchIndex)) + { + const auto& path = o.first.get(); + const auto& interfaces = o.second; + std::vector mapperResponse; + std::transform( + interfaces.begin(), + interfaces.end(), + std::back_inserter(mapperResponse), + // *INDENT-OFF* + [](const auto & item) + { + return item.first; + }); + // *INDENT-ON* + EXPECT_CALL( + dbus, + mapperGetObject( + MAPPER_BUSNAME, + MAPPER_PATH, + MAPPER_INTERFACE, + "GetObject", + path, + expectedMapperInterfaces)) + .WillOnce(Return(GetObject({{"", mapperResponse}}))); + EXPECT_CALL( + dbus, + fwdAddMatch( + sdbusplus::bus::match::rules::member("InterfacesAdded") + + sdbusplus::bus::match::rules::path(path) + + sdbusplus::bus::match::rules::interface( + "org.freedesktop.DBus.ObjectManager"), + _)); + for (const auto& i : interfaces) + { + const auto& interface = i.first.get(); + const auto& properties = i.second; + EXPECT_CALL( + dbus, + fwdAddMatch( + sdbusplus::bus::match::rules::member("PropertiesChanged") + + sdbusplus::bus::match::rules::path(path) + + sdbusplus::bus::match::rules::argN(0, interface) + + sdbusplus::bus::match::rules::interface( + "org.freedesktop.DBus.Properties"), + _)); + + PropertiesChanged serviceResponse; + for (const auto& p : properties) + { + serviceResponse[p] = ExpectedValues::get(ndx); + ++ndx; + } + Expect::getProperties(dbus, path, interface) + .WillOnce(Return(serviceResponse)); + } + } + + watch.start(); + + ndx = 0; + for (auto s : storage) + { + ASSERT_EQ(s.empty(), false); + ASSERT_EQ(any_ns::any_cast(s), ExpectedValues::get(ndx)); + ++ndx; + } + + // Make sure start logic only runs the first time. + watch.start(); +} + +TEST(PropertyWatchTest, TestStart) +{ + testStart(); + testStart(); + testStart(); + testStart(); + testStart(); +} + +MockDBusInterface* MockDBusInterface::ptr = nullptr; -- cgit v1.2.1