Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 17 | |
| 18 | #include "manager.hpp" |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 19 | #include "xyz/openbmc_project/Example/Iface1/server.hpp" |
| 20 | #include "xyz/openbmc_project/Example/Iface2/server.hpp" |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 21 | |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <chrono> |
| 25 | #include <iostream> |
| 26 | #include <thread> |
| 27 | |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 28 | using namespace std::literals::chrono_literals; |
| 29 | using namespace std::literals::string_literals; |
| 30 | |
Brad Bishop | 03f4cd9 | 2017-02-03 15:17:21 -0500 | [diff] [blame] | 31 | using Object = phosphor::inventory::manager::Object; |
| 32 | using ObjectMap = std::map<sdbusplus::message::object_path, Object>; |
| 33 | |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 34 | constexpr auto MGR_SERVICE = "phosphor.inventory.test.mgr"; |
| 35 | constexpr auto MGR_INTERFACE = IFACE; |
| 36 | constexpr auto MGR_ROOT = "/testing/inventory"; |
| 37 | constexpr auto EXAMPLE_SERVICE = "phosphor.inventory.test.example"; |
| 38 | constexpr auto EXAMPLE_ROOT = "/testing"; |
| 39 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 40 | const auto trigger1 = |
| 41 | sdbusplus::message::object_path(EXAMPLE_ROOT + "/trigger1"s); |
| 42 | const auto trigger2 = |
| 43 | sdbusplus::message::object_path(EXAMPLE_ROOT + "/trigger2"s); |
| 44 | const auto trigger3 = |
| 45 | sdbusplus::message::object_path(EXAMPLE_ROOT + "/trigger3"s); |
| 46 | const auto trigger4 = |
| 47 | sdbusplus::message::object_path(EXAMPLE_ROOT + "/trigger4"s); |
| 48 | const auto trigger5 = |
| 49 | sdbusplus::message::object_path(EXAMPLE_ROOT + "/trigger5"s); |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 50 | |
| 51 | const sdbusplus::message::object_path relDeleteMeOne{"/deleteme1"}; |
| 52 | const sdbusplus::message::object_path relDeleteMeTwo{"/deleteme2"}; |
| 53 | const sdbusplus::message::object_path relDeleteMeThree{"/deleteme3"}; |
| 54 | |
| 55 | const std::string root{MGR_ROOT}; |
| 56 | const std::string deleteMeOne{root + relDeleteMeOne.str}; |
| 57 | const std::string deleteMeTwo{root + relDeleteMeTwo.str}; |
| 58 | const std::string deleteMeThree{root + relDeleteMeThree.str}; |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 59 | |
| 60 | using ExampleIface1 = sdbusplus::xyz::openbmc_project::Example::server::Iface1; |
| 61 | using ExampleIface2 = sdbusplus::xyz::openbmc_project::Example::server::Iface2; |
| 62 | |
| 63 | /** @class ExampleService |
| 64 | * @brief Host an object for triggering events. |
| 65 | */ |
| 66 | struct ExampleService |
| 67 | { |
| 68 | ~ExampleService() = default; |
| 69 | ExampleService() : |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 70 | shutdown(false), bus(sdbusplus::bus::new_default()), |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 71 | objmgr(sdbusplus::server::manager::manager(bus, MGR_ROOT)) |
| 72 | { |
| 73 | bus.request_name(EXAMPLE_SERVICE); |
| 74 | } |
| 75 | |
| 76 | void run() |
| 77 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 78 | sdbusplus::server::object::object<ExampleIface1, ExampleIface2> t1( |
| 79 | bus, trigger1.str.c_str()); |
| 80 | sdbusplus::server::object::object<ExampleIface1, ExampleIface2> t2( |
| 81 | bus, trigger2.str.c_str()); |
| 82 | sdbusplus::server::object::object<ExampleIface1, ExampleIface2> t3( |
| 83 | bus, trigger3.str.c_str()); |
| 84 | sdbusplus::server::object::object<ExampleIface1, ExampleIface2> t4( |
| 85 | bus, trigger4.str.c_str()); |
| 86 | sdbusplus::server::object::object<ExampleIface1, ExampleIface2> t5( |
| 87 | bus, trigger5.str.c_str()); |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 88 | |
| 89 | while (!shutdown) |
| 90 | { |
| 91 | bus.process_discard(); |
| 92 | bus.wait((5000000us).count()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | volatile bool shutdown; |
| 97 | sdbusplus::bus::bus bus; |
| 98 | sdbusplus::server::manager::manager objmgr; |
| 99 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 100 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 101 | /** @class SignalQueue |
| 102 | * @brief Store DBus signals in a queue. |
| 103 | */ |
| 104 | class SignalQueue |
| 105 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 106 | public: |
| 107 | ~SignalQueue() = default; |
| 108 | SignalQueue() = delete; |
| 109 | SignalQueue(const SignalQueue&) = delete; |
| 110 | SignalQueue(SignalQueue&&) = default; |
| 111 | SignalQueue& operator=(const SignalQueue&) = delete; |
| 112 | SignalQueue& operator=(SignalQueue&&) = default; |
| 113 | explicit SignalQueue(const std::string& match) : |
| 114 | _bus(sdbusplus::bus::new_default()), |
| 115 | _match(_bus, match.c_str(), &callback, this), _next(nullptr) |
| 116 | { |
| 117 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 118 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 119 | auto&& pop(unsigned timeout = 1000000) |
| 120 | { |
| 121 | while (timeout > 0 && !_next) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 122 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 123 | _bus.process_discard(); |
| 124 | _bus.wait(50000); |
| 125 | timeout -= 50000; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 126 | } |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 127 | return std::move(_next); |
| 128 | } |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 129 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 130 | private: |
| 131 | static int callback(sd_bus_message* m, void* context, sd_bus_error*) |
| 132 | { |
| 133 | auto* me = static_cast<SignalQueue*>(context); |
| 134 | sd_bus_message_ref(m); |
| 135 | sdbusplus::message::message msg{m}; |
| 136 | me->_next = std::move(msg); |
| 137 | return 0; |
| 138 | } |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 139 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 140 | sdbusplus::bus::bus _bus; |
| 141 | sdbusplus::bus::match_t _match; |
| 142 | sdbusplus::message::message _next; |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 145 | /**@brief Find a subset of interfaces and properties in an object. */ |
Brad Bishop | 1157af1 | 2017-01-22 01:03:02 -0500 | [diff] [blame] | 146 | auto hasProperties(const Object& l, const Object& r) |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 147 | { |
Brad Bishop | 1157af1 | 2017-01-22 01:03:02 -0500 | [diff] [blame] | 148 | Object result; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 149 | std::set_difference(r.cbegin(), r.cend(), l.cbegin(), l.cend(), |
| 150 | std::inserter(result, result.end())); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 151 | return result.empty(); |
| 152 | } |
| 153 | |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 154 | /**@brief Check an object for one or more interfaces. */ |
Brad Bishop | 1157af1 | 2017-01-22 01:03:02 -0500 | [diff] [blame] | 155 | auto hasInterfaces(const std::vector<std::string>& l, const Object& r) |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 156 | { |
| 157 | std::vector<std::string> stripped, interfaces; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 158 | std::transform(r.cbegin(), r.cend(), std::back_inserter(stripped), |
| 159 | [](auto& p) { return p.first; }); |
| 160 | std::set_difference(stripped.cbegin(), stripped.cend(), l.cbegin(), |
| 161 | l.cend(), std::back_inserter(interfaces)); |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 162 | return interfaces.empty(); |
| 163 | } |
| 164 | |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 165 | void runTests() |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 166 | { |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 167 | const std::string exampleRoot{EXAMPLE_ROOT}; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 168 | auto b = sdbusplus::bus::new_default(); |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 169 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 170 | auto notify = [&]() { |
| 171 | return b.new_method_call(MGR_SERVICE, MGR_ROOT, MGR_INTERFACE, |
| 172 | "Notify"); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 173 | }; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 174 | auto set = [&](const std::string& path) { |
| 175 | return b.new_method_call(EXAMPLE_SERVICE, path.c_str(), |
| 176 | "org.freedesktop.DBus.Properties", "Set"); |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 177 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 178 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 179 | Object obj{ |
| 180 | {"xyz.openbmc_project.Example.Iface1", |
| 181 | {{"ExampleProperty1", "test1"s}}}, |
| 182 | {"xyz.openbmc_project.Example.Iface2", |
| 183 | {{"ExampleProperty2", "test2"s}, |
| 184 | {"ExampleProperty3", static_cast<int64_t>(0ll)}}}, |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 185 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 186 | |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 187 | // Validate startup events occurred. |
| 188 | { |
| 189 | sdbusplus::message::object_path relCreateMe3{"/createme3"}; |
| 190 | std::string createMe3{root + relCreateMe3.str}; |
| 191 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 192 | auto get = |
| 193 | b.new_method_call(MGR_SERVICE, createMe3.c_str(), |
| 194 | "org.freedesktop.DBus.Properties", "GetAll"); |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 195 | get.append("xyz.openbmc_project.Example.Iface1"); |
| 196 | auto resp = b.call(get); |
| 197 | |
| 198 | Object::mapped_type properties; |
| 199 | assert(!resp.is_method_error()); |
| 200 | resp.read(properties); |
| 201 | } |
| 202 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 203 | // Make sure the notify method works. |
| 204 | { |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 205 | sdbusplus::message::object_path relPath{"/foo"}; |
| 206 | std::string path(root + relPath.str); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 207 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 208 | SignalQueue queue("path='" + root + "',member='InterfacesAdded'"); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 209 | |
| 210 | auto m = notify(); |
Brad Bishop | 03f4cd9 | 2017-02-03 15:17:21 -0500 | [diff] [blame] | 211 | m.append(ObjectMap({{relPath, obj}})); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 212 | b.call(m); |
| 213 | |
| 214 | auto sig{queue.pop()}; |
Brad Bishop | 6211a43 | 2018-02-21 13:24:41 -0500 | [diff] [blame] | 215 | assert(static_cast<bool>(sig)); |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 216 | sdbusplus::message::object_path signalPath; |
Brad Bishop | 1157af1 | 2017-01-22 01:03:02 -0500 | [diff] [blame] | 217 | Object signalObjectType; |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 218 | sig.read(signalPath); |
Brad Bishop | e07a164 | 2017-01-24 14:37:33 -0500 | [diff] [blame] | 219 | assert(path == signalPath.str); |
Brad Bishop | 1157af1 | 2017-01-22 01:03:02 -0500 | [diff] [blame] | 220 | sig.read(signalObjectType); |
| 221 | assert(hasProperties(signalObjectType, obj)); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 222 | auto moreSignals{queue.pop()}; |
| 223 | assert(!moreSignals); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 226 | // Validate the propertyIs filter. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 227 | {// Create an object to be deleted. |
| 228 | {auto m = notify(); |
| 229 | m.append(ObjectMap({{relDeleteMeThree, obj}})); |
| 230 | b.call(m); |
| 231 | } |
| 232 | |
| 233 | // Validate that the action does not run if the property doesn't match. |
| 234 | { |
| 235 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 236 | auto m = set(trigger4.str); |
| 237 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 238 | m.append("ExampleProperty2"); |
| 239 | m.append(sdbusplus::message::variant<std::string>("123")); |
| 240 | b.call(m); |
| 241 | auto sig{queue.pop()}; |
| 242 | assert(!sig); |
| 243 | } |
| 244 | |
| 245 | // Validate that the action does run if the property matches. |
| 246 | { |
| 247 | // Set ExampleProperty2 to something else to the 123 filter |
| 248 | // matches. |
| 249 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 250 | auto m = set(trigger4.str); |
| 251 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 252 | m.append("ExampleProperty2"); |
| 253 | m.append(sdbusplus::message::variant<std::string>("xyz")); |
| 254 | b.call(m); |
| 255 | auto sig{queue.pop()}; |
| 256 | assert(!sig); |
| 257 | } |
| 258 | { |
| 259 | // Set ExampleProperty3 to 99. |
| 260 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 261 | auto m = set(trigger4.str); |
| 262 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 263 | m.append("ExampleProperty3"); |
| 264 | m.append(sdbusplus::message::variant<int64_t>(99)); |
| 265 | b.call(m); |
| 266 | auto sig{queue.pop()}; |
| 267 | assert(!sig); |
| 268 | } |
| 269 | { |
| 270 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 271 | auto m = set(trigger4.str); |
| 272 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 273 | m.append("ExampleProperty2"); |
| 274 | m.append(sdbusplus::message::variant<std::string>("123")); |
| 275 | b.call(m); |
| 276 | |
| 277 | sdbusplus::message::object_path sigpath; |
| 278 | std::vector<std::string> interfaces; |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 279 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 280 | std::vector<std::string> interfaces; |
| 281 | auto sig{queue.pop()}; |
| 282 | assert(static_cast<bool>(sig)); |
| 283 | sig.read(sigpath); |
| 284 | assert(sigpath == deleteMeThree); |
| 285 | sig.read(interfaces); |
| 286 | std::sort(interfaces.begin(), interfaces.end()); |
| 287 | assert(hasInterfaces(interfaces, obj)); |
| 288 | } |
| 289 | } |
| 290 | } |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 291 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 292 | // Make sure DBus signals are handled. |
| 293 | {// Create some objects to be deleted by an action. |
| 294 | {auto m = notify(); |
| 295 | m.append(ObjectMap({{relDeleteMeOne, obj}})); |
| 296 | b.call(m); |
| 297 | } |
| 298 | { |
| 299 | auto m = notify(); |
| 300 | m.append(ObjectMap({{relDeleteMeTwo, obj}})); |
| 301 | b.call(m); |
| 302 | } |
| 303 | { |
| 304 | auto m = notify(); |
| 305 | m.append(ObjectMap({{relDeleteMeThree, obj}})); |
| 306 | b.call(m); |
| 307 | } |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 308 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 309 | // Set some properties that should not trigger due to a filter. |
| 310 | { |
| 311 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 312 | auto m = set(trigger1.str); |
| 313 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 314 | m.append("ExampleProperty2"); |
| 315 | m.append(sdbusplus::message::variant<std::string>("abc123")); |
| 316 | b.call(m); |
| 317 | auto sig{queue.pop()}; |
| 318 | assert(!sig); |
| 319 | } |
| 320 | { |
| 321 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 322 | auto m = set(trigger3.str); |
| 323 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 324 | m.append("ExampleProperty3"); |
| 325 | m.append(sdbusplus::message::variant<int64_t>(11)); |
| 326 | b.call(m); |
| 327 | auto sig{queue.pop()}; |
| 328 | assert(!sig); |
| 329 | } |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 330 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 331 | // Set some properties that should trigger. |
| 332 | { |
| 333 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 334 | |
| 335 | auto m = set(trigger1.str); |
| 336 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 337 | m.append("ExampleProperty2"); |
| 338 | m.append(sdbusplus::message::variant<std::string>("xxxyyy")); |
| 339 | b.call(m); |
| 340 | |
| 341 | sdbusplus::message::object_path sigpath; |
| 342 | std::vector<std::string> interfaces; |
| 343 | { |
| 344 | std::vector<std::string> interfaces; |
| 345 | auto sig{queue.pop()}; |
| 346 | assert(static_cast<bool>(sig)); |
| 347 | sig.read(sigpath); |
| 348 | assert(sigpath == deleteMeOne); |
| 349 | sig.read(interfaces); |
| 350 | std::sort(interfaces.begin(), interfaces.end()); |
| 351 | assert(hasInterfaces(interfaces, obj)); |
| 352 | } |
| 353 | { |
| 354 | std::vector<std::string> interfaces; |
| 355 | auto sig{queue.pop()}; |
| 356 | assert(static_cast<bool>(sig)); |
| 357 | sig.read(sigpath); |
| 358 | assert(sigpath == deleteMeTwo); |
| 359 | sig.read(interfaces); |
| 360 | std::sort(interfaces.begin(), interfaces.end()); |
| 361 | assert(hasInterfaces(interfaces, obj)); |
| 362 | } |
| 363 | { |
| 364 | // Make sure there were only two signals. |
| 365 | auto sig{queue.pop()}; |
| 366 | assert(!sig); |
| 367 | } |
| 368 | } |
| 369 | { |
| 370 | SignalQueue queue("path='" + root + "',member='InterfacesRemoved'"); |
| 371 | |
| 372 | auto m = set(trigger3.str); |
| 373 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 374 | m.append("ExampleProperty3"); |
| 375 | m.append(sdbusplus::message::variant<int64_t>(10)); |
| 376 | b.call(m); |
| 377 | |
| 378 | sdbusplus::message::object_path sigpath; |
| 379 | std::vector<std::string> interfaces; |
| 380 | { |
| 381 | std::vector<std::string> interfaces; |
| 382 | auto sig{queue.pop()}; |
| 383 | assert(static_cast<bool>(sig)); |
| 384 | sig.read(sigpath); |
| 385 | assert(sigpath == deleteMeThree); |
| 386 | sig.read(interfaces); |
| 387 | std::sort(interfaces.begin(), interfaces.end()); |
| 388 | assert(hasInterfaces(interfaces, obj)); |
| 389 | } |
| 390 | { |
| 391 | // Make sure there was only one signal. |
| 392 | auto sig{queue.pop()}; |
| 393 | assert(!sig); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // Validate the set property action. |
| 399 | { |
| 400 | sdbusplus::message::object_path relChangeMe{"/changeme"}; |
| 401 | std::string changeMe{root + relChangeMe.str}; |
| 402 | |
| 403 | // Create an object to be updated by the set property action. |
| 404 | { |
| 405 | auto m = notify(); |
| 406 | m.append(ObjectMap({{relChangeMe, obj}})); |
| 407 | b.call(m); |
| 408 | } |
| 409 | |
| 410 | // Trigger and validate the change. |
| 411 | { |
| 412 | SignalQueue queue("path='" + changeMe + "',member='PropertiesChanged'"); |
| 413 | auto m = set(trigger2.str); |
| 414 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 415 | m.append("ExampleProperty2"); |
| 416 | m.append(sdbusplus::message::variant<std::string>("yyyxxx")); |
| 417 | b.call(m); |
| 418 | |
| 419 | std::string sigInterface; |
| 420 | std::map<std::string, sdbusplus::message::variant<std::string>> |
| 421 | sigProperties; |
| 422 | { |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 423 | std::vector<std::string> interfaces; |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 424 | auto sig{queue.pop()}; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 425 | sig.read(sigInterface); |
| 426 | assert(sigInterface == "xyz.openbmc_project.Example.Iface1"); |
| 427 | sig.read(sigProperties); |
William A. Kennington III | 6e94b65 | 2018-11-06 17:11:28 -0800 | [diff] [blame] | 428 | assert(sdbusplus::message::variant_ns::get<std::string>( |
| 429 | sigProperties["ExampleProperty1"]) == "changed"); |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 430 | } |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | // Validate the create object action. |
| 435 | { |
| 436 | sdbusplus::message::object_path relCreateMe1{"/createme1"}; |
| 437 | sdbusplus::message::object_path relCreateMe2{"/createme2"}; |
| 438 | std::string createMe1{root + relCreateMe1.str}; |
| 439 | std::string createMe2{root + relCreateMe2.str}; |
| 440 | |
| 441 | // Trigger the action. |
| 442 | { |
| 443 | sdbusplus::message::object_path signalPath; |
| 444 | Object signalObject; |
| 445 | |
| 446 | SignalQueue queue("path='" + root + "',member='InterfacesAdded'"); |
| 447 | |
| 448 | auto m = set(trigger5.str); |
| 449 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 450 | m.append("ExampleProperty2"); |
| 451 | m.append(sdbusplus::message::variant<std::string>("abc123")); |
| 452 | b.call(m); |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 453 | { |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 454 | auto sig{queue.pop()}; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 455 | assert(static_cast<bool>(sig)); |
| 456 | sig.read(signalPath); |
| 457 | assert(createMe1 == signalPath.str); |
| 458 | sig.read(signalObject); |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 459 | } |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 460 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 461 | auto sig{queue.pop()}; |
| 462 | assert(static_cast<bool>(sig)); |
| 463 | sig.read(signalPath); |
| 464 | assert(createMe2 == signalPath.str); |
| 465 | sig.read(signalObject); |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 466 | } |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 467 | |
| 468 | auto moreSignals{queue.pop()}; |
| 469 | assert(!moreSignals); |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 470 | } |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 471 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | int main() |
| 475 | { |
Brad Bishop | 6524758 | 2017-01-15 19:48:41 -0500 | [diff] [blame] | 476 | phosphor::inventory::manager::Manager mgr( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 477 | sdbusplus::bus::new_default(), MGR_SERVICE, MGR_ROOT, MGR_INTERFACE); |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 478 | ExampleService d; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 479 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 480 | auto f1 = [](auto mgr) { mgr->run(); }; |
| 481 | auto f2 = [](auto d) { d->run(); }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 482 | |
Brad Bishop | 8f86850 | 2017-01-23 13:13:58 -0500 | [diff] [blame] | 483 | auto t1 = std::thread(f1, &mgr); |
| 484 | auto t2 = std::thread(f2, &d); |
| 485 | |
| 486 | runTests(); |
| 487 | |
| 488 | mgr.shutdown(); |
| 489 | d.shutdown = true; |
| 490 | |
| 491 | // Wait for server threads to exit. |
| 492 | t1.join(); |
| 493 | t2.join(); |
| 494 | std::cout << "Success! Waiting for threads to exit..." << std::endl; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |