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