Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <cassert> |
| 3 | #include <sdbusplus/message.hpp> |
| 4 | #include <sdbusplus/bus.hpp> |
| 5 | |
| 6 | // Global to share the dbus type string between client and server. |
| 7 | static std::string verifyTypeString; |
| 8 | |
| 9 | using verifyCallback_t = void(*)(sdbusplus::message::message&); |
| 10 | verifyCallback_t verifyCallback = nullptr; |
| 11 | |
Patrick Williams | 24fb296 | 2016-11-04 17:08:59 -0500 | [diff] [blame] | 12 | static constexpr auto SERVICE = "sdbusplus.test.message.read"; |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 13 | static constexpr auto INTERFACE = SERVICE; |
| 14 | static constexpr auto TEST_METHOD = "test"; |
| 15 | static constexpr auto QUIT_METHOD = "quit"; |
| 16 | |
| 17 | // Open up the sdbus and claim SERVICE name. |
| 18 | auto serverInit() |
| 19 | { |
| 20 | auto b = sdbusplus::bus::new_default(); |
| 21 | b.request_name(SERVICE); |
| 22 | |
| 23 | return std::move(b); |
| 24 | } |
| 25 | |
| 26 | // Thread to run the dbus server. |
| 27 | void* server(void* b) |
| 28 | { |
| 29 | auto bus = sdbusplus::bus::bus(reinterpret_cast<sdbusplus::bus::busp_t>(b)); |
| 30 | |
| 31 | while(1) |
| 32 | { |
| 33 | // Wait for messages. |
| 34 | auto m = bus.process(); |
| 35 | |
| 36 | if(!m) |
| 37 | { |
| 38 | bus.wait(); |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | if (m.is_method_call(INTERFACE, TEST_METHOD)) |
| 43 | { |
| 44 | // Verify the message type matches what the test expects. |
| 45 | assert(verifyTypeString == m.get_signature()); |
| 46 | |
| 47 | if (verifyCallback) |
| 48 | { |
| 49 | |
| 50 | verifyCallback(m); |
| 51 | verifyCallback = nullptr; |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | std::cout << "Warning: No verification for " |
| 56 | << verifyTypeString << std::endl; |
| 57 | } |
| 58 | // Reply to client. |
| 59 | sd_bus_reply_method_return(m.release(), nullptr); |
| 60 | } |
| 61 | else if (m.is_method_call(INTERFACE, QUIT_METHOD)) |
| 62 | { |
| 63 | // Reply and exit. |
| 64 | sd_bus_reply_method_return(m.release(), nullptr); |
| 65 | break; |
| 66 | } |
| 67 | } |
Patrick Williams | 696e315 | 2016-11-04 17:03:39 -0500 | [diff] [blame] | 68 | |
| 69 | return nullptr; |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | auto newMethodCall__test(sdbusplus::bus::bus& b) |
| 73 | { |
| 74 | // Allocate a method-call message for INTERFACE,TEST_METHOD. |
| 75 | return b.new_method_call(SERVICE, "/", INTERFACE, TEST_METHOD); |
| 76 | } |
| 77 | |
| 78 | void runTests() |
| 79 | { |
| 80 | using namespace std::literals; |
| 81 | |
| 82 | auto b = sdbusplus::bus::new_default(); |
| 83 | |
| 84 | // Test r-value int. |
| 85 | { |
| 86 | auto m = newMethodCall__test(b); |
| 87 | m.append(1); |
| 88 | verifyTypeString = "i"; |
| 89 | |
| 90 | struct verify |
| 91 | { |
| 92 | static void op(sdbusplus::message::message& m) |
| 93 | { |
| 94 | int32_t i = 0; |
| 95 | m.read(i); |
| 96 | assert(i == 1); |
| 97 | } |
| 98 | }; |
| 99 | verifyCallback = &verify::op; |
| 100 | |
| 101 | b.call_noreply(m); |
| 102 | } |
| 103 | // Test l-value int. |
| 104 | { |
| 105 | auto m = newMethodCall__test(b); |
| 106 | int a = 1; |
| 107 | m.append(a, a); |
| 108 | verifyTypeString = "ii"; |
| 109 | |
| 110 | struct verify |
| 111 | { |
| 112 | static void op(sdbusplus::message::message& m) |
| 113 | { |
| 114 | int32_t a = 0, b = 0; |
| 115 | m.read(a, b); |
| 116 | assert(a == 1); |
| 117 | assert(b == 1); |
| 118 | } |
| 119 | }; |
| 120 | verifyCallback = &verify::op; |
| 121 | |
| 122 | b.call_noreply(m); |
| 123 | } |
| 124 | |
| 125 | // Test multiple ints. |
| 126 | { |
| 127 | auto m = newMethodCall__test(b); |
| 128 | m.append(1, 2, 3, 4, 5); |
| 129 | verifyTypeString = "iiiii"; |
| 130 | |
| 131 | struct verify |
| 132 | { |
| 133 | static void op(sdbusplus::message::message& m) |
| 134 | { |
| 135 | int32_t a = 0, b = 0, c = 0, d = 0, e = 0; |
| 136 | m.read(a,b,c,d,e); |
| 137 | assert(a == 1); |
| 138 | assert(b == 2); |
| 139 | assert(c == 3); |
| 140 | assert(d == 4); |
| 141 | assert(e == 5); |
| 142 | } |
| 143 | }; |
| 144 | verifyCallback = &verify::op; |
| 145 | |
| 146 | b.call_noreply(m); |
| 147 | } |
| 148 | |
Patrick Williams | 9e3b51e | 2016-10-18 07:47:29 -0500 | [diff] [blame] | 149 | // Test double and bool. |
| 150 | { |
| 151 | auto m = newMethodCall__test(b); |
| 152 | bool t = true; |
Patrick Williams | b760a99 | 2016-11-28 11:26:55 -0600 | [diff] [blame] | 153 | bool f = false; |
| 154 | bool f2 = false; |
| 155 | m.append(t, true, f, std::move(f2), false, 1.1); |
| 156 | verifyTypeString = "bbbbbd"; |
Patrick Williams | 9e3b51e | 2016-10-18 07:47:29 -0500 | [diff] [blame] | 157 | |
| 158 | struct verify |
| 159 | { |
| 160 | static void op(sdbusplus::message::message& m) |
| 161 | { |
Patrick Williams | b760a99 | 2016-11-28 11:26:55 -0600 | [diff] [blame] | 162 | bool t1, t2, f1, f2, f3; |
Patrick Williams | 9e3b51e | 2016-10-18 07:47:29 -0500 | [diff] [blame] | 163 | double d; |
Patrick Williams | b760a99 | 2016-11-28 11:26:55 -0600 | [diff] [blame] | 164 | m.read(t1, t2, f1, f2, f3, d); |
Patrick Williams | 9e3b51e | 2016-10-18 07:47:29 -0500 | [diff] [blame] | 165 | assert(t1); |
| 166 | assert(t2); |
| 167 | assert(!f1); |
Patrick Williams | b760a99 | 2016-11-28 11:26:55 -0600 | [diff] [blame] | 168 | assert(!f2); |
| 169 | assert(!f3); |
Patrick Williams | 9e3b51e | 2016-10-18 07:47:29 -0500 | [diff] [blame] | 170 | assert(d == 1.1); |
| 171 | } |
| 172 | }; |
| 173 | verifyCallback = &verify::op; |
| 174 | |
| 175 | b.call_noreply(m); |
| 176 | } |
| 177 | |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 178 | // Test r-value string. |
| 179 | { |
| 180 | auto m = newMethodCall__test(b); |
| 181 | m.append("asdf"s); |
| 182 | verifyTypeString = "s"; |
| 183 | |
| 184 | struct verify |
| 185 | { |
| 186 | static void op(sdbusplus::message::message& m) |
| 187 | { |
| 188 | const char* s = nullptr; |
| 189 | m.read(s); |
| 190 | assert(0 == strcmp("asdf", s)); |
| 191 | } |
| 192 | }; |
| 193 | verifyCallback = &verify::op; |
| 194 | |
| 195 | b.call_noreply(m); |
| 196 | } |
| 197 | |
| 198 | // Test multiple strings, various forms. |
| 199 | { |
| 200 | auto m = newMethodCall__test(b); |
| 201 | auto str = "jkl;"s; |
| 202 | auto str2 = "JKL:"s; |
| 203 | m.append(1, "asdf", "ASDF"s, str, |
| 204 | std::move(str2), 5); |
| 205 | verifyTypeString = "issssi"; |
| 206 | |
| 207 | struct verify |
| 208 | { |
| 209 | static void op(sdbusplus::message::message& m) |
| 210 | { |
| 211 | int32_t a = 0, b = 0; |
| 212 | std::string s0, s1, s2, s3; |
| 213 | m.read(a, s0, s1, s2, s3, b); |
| 214 | assert(a == 1); |
| 215 | assert(b == 5); |
| 216 | assert(s0 == "asdf"s); |
| 217 | assert(s1 == "ASDF"s); |
| 218 | assert(s2 == "jkl;"s); |
| 219 | assert(s3 == "JKL:"); |
| 220 | } |
| 221 | }; |
| 222 | verifyCallback = &verify::op; |
| 223 | |
| 224 | b.call_noreply(m); |
| 225 | } |
| 226 | |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 227 | // Test vector. |
| 228 | { |
| 229 | auto m = newMethodCall__test(b); |
| 230 | std::vector<std::string> s{ "1", "2", "3"}; |
| 231 | m.append(1, s, 2); |
| 232 | verifyTypeString = "iasi"; |
| 233 | |
| 234 | struct verify |
| 235 | { |
| 236 | static void op(sdbusplus::message::message& m) |
| 237 | { |
| 238 | int32_t a = 0; |
| 239 | std::vector<std::string> s; |
| 240 | m.read(a, s); |
| 241 | assert(a == 1); |
| 242 | assert(s[0] == "1"); |
| 243 | assert(s[1] == "2"); |
| 244 | assert(s[2] == "3"); |
| 245 | decltype(s) s2 = { "1" , "2" , "3" }; |
| 246 | assert(s == s2); |
| 247 | } |
| 248 | }; |
| 249 | verifyCallback = &verify::op; |
| 250 | |
| 251 | b.call_noreply(m); |
| 252 | } |
| 253 | |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 254 | // Test map. |
| 255 | { |
| 256 | auto m = newMethodCall__test(b); |
| 257 | std::map<std::string, int> s = { { "asdf", 3 }, { "jkl;", 4 } }; |
| 258 | m.append(1, s, 2); |
| 259 | verifyTypeString = "ia{si}i"; |
| 260 | |
| 261 | struct verify |
| 262 | { |
| 263 | static void op(sdbusplus::message::message& m) |
| 264 | { |
| 265 | int32_t a = 0, b = 0; |
| 266 | std::map<std::string, int> s{}; |
| 267 | |
| 268 | m.read(a, s, b); |
| 269 | assert(a == 1); |
| 270 | assert(s.size() == 2); |
| 271 | assert(s["asdf"] == 3); |
| 272 | assert(s["jkl;"] == 4); |
| 273 | assert(b == 2); |
| 274 | } |
| 275 | }; |
| 276 | verifyCallback = &verify::op; |
| 277 | |
| 278 | b.call_noreply(m); |
| 279 | } |
| 280 | |
Patrick Williams | aa2f48a | 2016-09-10 14:23:34 -0500 | [diff] [blame] | 281 | // Test tuple. |
| 282 | { |
| 283 | auto m = newMethodCall__test(b); |
| 284 | std::tuple<int, double, std::string> a{ 3, 4.1, "asdf" }; |
| 285 | m.append(1, a, 2); |
| 286 | verifyTypeString = "i(ids)i"; |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 287 | |
Patrick Williams | aa2f48a | 2016-09-10 14:23:34 -0500 | [diff] [blame] | 288 | struct verify |
| 289 | { |
| 290 | static void op(sdbusplus::message::message& m) |
| 291 | { |
| 292 | int32_t a = 0, b = 0; |
| 293 | std::tuple<int, double, std::string> c{}; |
| 294 | |
| 295 | m.read(a, c, b); |
| 296 | assert(a == 1); |
| 297 | assert(b == 2); |
| 298 | assert(c == std::make_tuple(3, 4.1, "asdf"s)); |
| 299 | } |
| 300 | }; |
| 301 | verifyCallback = &verify::op; |
| 302 | |
| 303 | b.call_noreply(m); |
| 304 | } |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 305 | |
Patrick Williams | 7559678 | 2016-09-11 21:14:10 -0500 | [diff] [blame] | 306 | // Test variant. |
| 307 | { |
| 308 | auto m = newMethodCall__test(b); |
| 309 | sdbusplus::message::variant<int, double> a1{3.1}, a2{4}; |
| 310 | m.append(1, a1, a2, 2); |
| 311 | verifyTypeString = "ivvi"; |
| 312 | |
| 313 | struct verify |
| 314 | { |
| 315 | static void op(sdbusplus::message::message& m) |
| 316 | { |
| 317 | int32_t a, b; |
| 318 | sdbusplus::message::variant<int, double> a1{}, a2{}; |
| 319 | |
| 320 | m.read(a, a1, a2, b); |
| 321 | assert(a == 1); |
| 322 | assert(a1 == 3.1); |
| 323 | assert(a2 == 4); |
| 324 | assert(b == 2); |
| 325 | } |
| 326 | }; |
| 327 | verifyCallback = &verify::op; |
| 328 | |
| 329 | b.call_noreply(m); |
| 330 | } |
| 331 | |
Patrick Williams | f101ee2 | 2016-11-04 17:44:04 -0500 | [diff] [blame] | 332 | // Test variant with missing/wrong type. |
| 333 | { |
| 334 | auto m = newMethodCall__test(b); |
| 335 | sdbusplus::message::variant<uint64_t, double> a1{3.1}, a2{uint64_t(4)}; |
| 336 | m.append(1, a1, a2, 2); |
| 337 | verifyTypeString = "ivvi"; |
| 338 | |
| 339 | struct verify |
| 340 | { |
| 341 | static void op(sdbusplus::message::message& m) |
| 342 | { |
| 343 | int32_t a, b; |
| 344 | sdbusplus::message::variant<uint8_t, double> a1{}, a2{}; |
| 345 | |
| 346 | m.read(a, a1, a2, b); |
| 347 | assert(a == 1); |
| 348 | assert(a1 == 3.1); |
| 349 | assert(a2 == uint8_t()); |
| 350 | assert(b == 2); |
| 351 | } |
| 352 | }; |
| 353 | verifyCallback = &verify::op; |
| 354 | |
| 355 | b.call_noreply(m); |
| 356 | } |
| 357 | |
Patrick Williams | 7559678 | 2016-09-11 21:14:10 -0500 | [diff] [blame] | 358 | // Test map-variant. |
| 359 | { |
| 360 | auto m = newMethodCall__test(b); |
| 361 | std::map<std::string, sdbusplus::message::variant<int, double>> a1 = |
| 362 | { { "asdf", 3 }, { "jkl;", 4.1 } }; |
| 363 | m.append(1, a1, 2); |
| 364 | verifyTypeString = "ia{sv}i"; |
| 365 | |
| 366 | struct verify |
| 367 | { |
| 368 | static void op(sdbusplus::message::message& m) |
| 369 | { |
| 370 | int32_t a = 0, b = 0; |
| 371 | std::map<std::string, |
| 372 | sdbusplus::message::variant<int, double>> a1{}; |
| 373 | |
| 374 | m.read(a, a1, b); |
| 375 | assert(a == 1); |
| 376 | assert(a1["asdf"] == 3); |
| 377 | assert(a1["jkl;"] == 4.1); |
| 378 | assert(b == 2); |
| 379 | } |
| 380 | }; |
| 381 | verifyCallback = &verify::op; |
| 382 | |
| 383 | b.call_noreply(m); |
| 384 | } |
| 385 | |
| 386 | |
Patrick Williams | 4fe85a3 | 2016-09-08 15:03:56 -0500 | [diff] [blame] | 387 | // Shutdown server. |
| 388 | { |
| 389 | auto m = b.new_method_call(SERVICE, "/", INTERFACE, QUIT_METHOD); |
| 390 | b.call_noreply(m); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | int main() |
| 395 | { |
| 396 | |
| 397 | // Initialize and start server thread. |
| 398 | pthread_t t; |
| 399 | { |
| 400 | auto b = serverInit(); |
| 401 | pthread_create(&t, NULL, server, b.release()); |
| 402 | } |
| 403 | |
| 404 | runTests(); |
| 405 | |
| 406 | // Wait for server thread to exit. |
| 407 | pthread_join(t, NULL); |
| 408 | |
| 409 | return 0; |
| 410 | } |