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