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