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