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