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 | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 21 | |
| 22 | constexpr auto SERVICE = "phosphor.inventory.test"; |
| 23 | constexpr auto INTERFACE = IFACE; |
| 24 | constexpr auto ROOT = "/testing/inventory"; |
| 25 | |
| 26 | auto server_thread(void *data) |
| 27 | { |
| 28 | auto mgr = static_cast<phosphor::inventory::manager::Manager*>(data); |
| 29 | |
| 30 | mgr->run(); |
| 31 | |
| 32 | return static_cast<void *>(nullptr); |
| 33 | } |
| 34 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 35 | /** @class SignalQueue |
| 36 | * @brief Store DBus signals in a queue. |
| 37 | */ |
| 38 | class SignalQueue |
| 39 | { |
| 40 | public: |
| 41 | ~SignalQueue() = default; |
| 42 | SignalQueue() = delete; |
| 43 | SignalQueue(const SignalQueue &) = delete; |
| 44 | SignalQueue(SignalQueue &&) = default; |
| 45 | SignalQueue& operator=(const SignalQueue &) = delete; |
| 46 | SignalQueue& operator=(SignalQueue &&) = default; |
| 47 | explicit SignalQueue(const std::string &match) : |
| 48 | _bus(sdbusplus::bus::new_default()), |
| 49 | _match(_bus, match.c_str(), &callback, this), |
| 50 | _next(nullptr) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | auto &&pop(unsigned timeout=1000000) |
| 55 | { |
| 56 | while(timeout > 0 && !_next) |
| 57 | { |
| 58 | _bus.process_discard(); |
| 59 | _bus.wait(50000); |
| 60 | timeout -= 50000; |
| 61 | } |
| 62 | return std::move(_next); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | static int callback(sd_bus_message *m, void *context, sd_bus_error *) |
| 67 | { |
| 68 | auto *me = static_cast<SignalQueue *>(context); |
| 69 | sd_bus_message_ref(m); |
| 70 | sdbusplus::message::message msg{m}; |
| 71 | me->_next = std::move(msg); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | sdbusplus::bus::bus _bus; |
| 76 | sdbusplus::server::match::match _match; |
| 77 | sdbusplus::message::message _next; |
| 78 | }; |
| 79 | |
| 80 | template <typename ...T> |
| 81 | using Object = std::map< |
| 82 | std::string, |
| 83 | std::map< |
| 84 | std::string, |
| 85 | sdbusplus::message::variant<T...>>>; |
| 86 | |
| 87 | using ObjectPath = std::string; |
| 88 | |
| 89 | /**@brief Find a subset of interfaces and properties in an object. */ |
| 90 | template <typename ...T> |
| 91 | auto hasProperties(const Object<T...> &l, const Object<T...> &r) |
| 92 | { |
| 93 | Object<T...> result; |
| 94 | std::set_difference( |
| 95 | r.cbegin(), |
| 96 | r.cend(), |
| 97 | l.cbegin(), |
| 98 | l.cend(), |
| 99 | std::inserter(result, result.end())); |
| 100 | return result.empty(); |
| 101 | } |
| 102 | |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 103 | /**@brief Check an object for one or more interfaces. */ |
| 104 | template <typename ...T> |
| 105 | auto hasInterfaces(const std::vector<std::string> &l, const Object<T...> &r) |
| 106 | { |
| 107 | std::vector<std::string> stripped, interfaces; |
| 108 | std::transform( |
| 109 | r.cbegin(), |
| 110 | r.cend(), |
| 111 | std::back_inserter(stripped), |
| 112 | [](auto &p){ return p.first; }); |
| 113 | std::set_difference( |
| 114 | stripped.cbegin(), |
| 115 | stripped.cend(), |
| 116 | l.cbegin(), |
| 117 | l.cend(), |
| 118 | std::back_inserter(interfaces)); |
| 119 | return interfaces.empty(); |
| 120 | } |
| 121 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 122 | void runTests(phosphor::inventory::manager::Manager &mgr) |
| 123 | { |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 124 | const std::string root{ROOT}; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 125 | auto b = sdbusplus::bus::new_default(); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 126 | auto notify = [&]() |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 127 | { |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 128 | return b.new_method_call( |
| 129 | SERVICE, |
| 130 | ROOT, |
| 131 | INTERFACE, |
| 132 | "Notify"); |
| 133 | }; |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 134 | auto set = [&](const std::string &path) |
| 135 | { |
| 136 | return b.new_method_call( |
| 137 | SERVICE, |
| 138 | path.c_str(), |
| 139 | "org.freedesktop.DBus.Properties", |
| 140 | "Set"); |
| 141 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 142 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 143 | Object<std::string> obj{ |
| 144 | { |
| 145 | "xyz.openbmc_project.Example.Iface1", |
| 146 | {{"ExampleProperty1", "test1"}} |
| 147 | }, |
| 148 | { |
| 149 | "xyz.openbmc_project.Example.Iface2", |
| 150 | {{"ExampleProperty2", "test2"}} |
| 151 | }, |
| 152 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 153 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 154 | // Make sure the notify method works. |
| 155 | { |
| 156 | ObjectPath relPath{"/foo"}; |
| 157 | ObjectPath path{root + relPath}; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 158 | |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 159 | SignalQueue queue( |
| 160 | "path='" + root + "',member='InterfacesAdded'"); |
| 161 | |
| 162 | auto m = notify(); |
| 163 | m.append(relPath); |
| 164 | m.append(obj); |
| 165 | b.call(m); |
| 166 | |
| 167 | auto sig{queue.pop()}; |
| 168 | assert(sig); |
| 169 | ObjectPath signalPath; |
| 170 | Object<std::string> signalObject; |
| 171 | sig.read(signalPath); |
| 172 | assert(path == signalPath); |
| 173 | sig.read(signalObject); |
| 174 | assert(hasProperties(signalObject, obj)); |
| 175 | auto moreSignals{queue.pop()}; |
| 176 | assert(!moreSignals); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Brad Bishop | 432e352 | 2016-12-01 00:24:14 -0500 | [diff] [blame] | 179 | // Make sure DBus signals are handled. |
| 180 | { |
| 181 | ObjectPath relDeleteMeOne{"/deleteme1"}; |
| 182 | ObjectPath relDeleteMeTwo{"/deleteme2"}; |
| 183 | ObjectPath relTriggerOne{"/trigger1"}; |
| 184 | ObjectPath deleteMeOne{root + relDeleteMeOne}; |
| 185 | ObjectPath deleteMeTwo{root + relDeleteMeTwo}; |
| 186 | ObjectPath triggerOne{root + relTriggerOne}; |
| 187 | |
| 188 | // Create some objects to be deleted by an action. |
| 189 | { |
| 190 | auto m = notify(); |
| 191 | m.append(relDeleteMeOne); |
| 192 | m.append(obj); |
| 193 | b.call(m); |
| 194 | } |
| 195 | { |
| 196 | auto m = notify(); |
| 197 | m.append(relDeleteMeTwo); |
| 198 | m.append(obj); |
| 199 | b.call(m); |
| 200 | } |
| 201 | |
| 202 | // Create the triggering object. |
| 203 | { |
| 204 | auto m = notify(); |
| 205 | m.append(relTriggerOne); |
| 206 | m.append(obj); |
| 207 | b.call(m); |
| 208 | } |
| 209 | |
| 210 | // Set a property that should not trigger due to a filter. |
| 211 | { |
| 212 | SignalQueue queue( |
| 213 | "path='" + root + "',member='InterfacesRemoved'"); |
| 214 | auto m = set(triggerOne); |
| 215 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 216 | m.append("ExampleProperty2"); |
| 217 | m.append(sdbusplus::message::variant<std::string>("abc123")); |
| 218 | b.call(m); |
| 219 | auto sig{queue.pop()}; |
| 220 | assert(!sig); |
| 221 | } |
| 222 | |
| 223 | // Set a property that should trigger. |
| 224 | { |
| 225 | SignalQueue queue( |
| 226 | "path='" + root + "',member='InterfacesRemoved'"); |
| 227 | |
| 228 | auto m = set(triggerOne); |
| 229 | m.append("xyz.openbmc_project.Example.Iface2"); |
| 230 | m.append("ExampleProperty2"); |
| 231 | m.append(sdbusplus::message::variant<std::string>("xxxyyy")); |
| 232 | b.call(m); |
| 233 | |
| 234 | ObjectPath sigpath; |
| 235 | std::vector<std::string> interfaces; |
| 236 | { |
| 237 | std::vector<std::string> interfaces; |
| 238 | auto sig{queue.pop()}; |
| 239 | assert(sig); |
| 240 | sig.read(sigpath); |
| 241 | assert(sigpath == deleteMeOne); |
| 242 | sig.read(interfaces); |
| 243 | std::sort(interfaces.begin(), interfaces.end()); |
| 244 | assert(hasInterfaces(interfaces, obj)); |
| 245 | } |
| 246 | { |
| 247 | std::vector<std::string> interfaces; |
| 248 | auto sig{queue.pop()}; |
| 249 | assert(sig); |
| 250 | sig.read(sigpath); |
| 251 | assert(sigpath == deleteMeTwo); |
| 252 | sig.read(interfaces); |
| 253 | std::sort(interfaces.begin(), interfaces.end()); |
| 254 | assert(hasInterfaces(interfaces, obj)); |
| 255 | } |
| 256 | { |
| 257 | // Make sure there were only two signals. |
| 258 | auto sig{queue.pop()}; |
| 259 | assert(!sig); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 264 | mgr.shutdown(); |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 265 | std::cout << "Success!" << std::endl; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int main() |
| 269 | { |
| 270 | auto mgr = phosphor::inventory::manager::Manager( |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 271 | sdbusplus::bus::new_default(), |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 272 | SERVICE, ROOT, INTERFACE); |
| 273 | |
| 274 | pthread_t t; |
| 275 | { |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 276 | pthread_create(&t, nullptr, server_thread, &mgr); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | runTests(mgr); |
| 280 | |
| 281 | // Wait for server thread to exit. |
Brad Bishop | abb2a1a | 2016-11-30 22:02:28 -0500 | [diff] [blame] | 282 | pthread_join(t, nullptr); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |