| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-future.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "bus-internal.h" |
| 8 |
#include "bus-message.h" |
| 9 |
#include "log.h" |
| 10 |
#include "strv.h" |
| 11 |
#include "tests.h" |
| 12 |
|
| 13 |
struct context { |
| 14 |
int fds[2]; |
| 15 |
bool quit; |
| 16 |
char *something; |
| 17 |
char *automatic_string_property; |
| 18 |
uint32_t automatic_integer_property; |
| 19 |
}; |
| 20 |
|
| 21 |
static int something_handler(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 22 |
struct context *c = userdata; |
| 23 |
const char *s; |
| 24 |
char *n = NULL; |
| 25 |
|
| 26 |
ASSERT_OK_POSITIVE(sd_bus_message_read(m, "s", &s)); |
| 27 |
|
| 28 |
ASSERT_NOT_NULL(n = strjoin("<<<", s, ">>>")); |
| 29 |
|
| 30 |
free(c->something); |
| 31 |
c->something = n; |
| 32 |
|
| 33 |
log_info("AlterSomething() called, got %s, returning %s", s, n); |
| 34 |
|
| 35 |
/* This should fail, since the return type doesn't match */ |
| 36 |
ASSERT_ERROR(sd_bus_reply_method_return(m, "u", 4711), ENOMSG); |
| 37 |
|
| 38 |
ASSERT_OK(sd_bus_reply_method_return(m, "s", n)); |
| 39 |
|
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
static int exit_handler(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 44 |
struct context *c = userdata; |
| 45 |
|
| 46 |
c->quit = true; |
| 47 |
|
| 48 |
log_info("Exit called"); |
| 49 |
|
| 50 |
ASSERT_OK(sd_bus_reply_method_return(m, "")); |
| 51 |
|
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
static int get_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *reterr_error) { |
| 56 |
struct context *c = userdata; |
| 57 |
|
| 58 |
log_info("property get for %s called, returning \"%s\".", property, c->something); |
| 59 |
|
| 60 |
ASSERT_OK(sd_bus_message_append(reply, "s", c->something)); |
| 61 |
|
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
static int set_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *reterr_error) { |
| 66 |
struct context *c = userdata; |
| 67 |
const char *s; |
| 68 |
char *n; |
| 69 |
|
| 70 |
log_info("property set for %s called", property); |
| 71 |
|
| 72 |
ASSERT_OK(sd_bus_message_read(value, "s", &s)); |
| 73 |
|
| 74 |
ASSERT_NOT_NULL(n = strdup(s)); |
| 75 |
|
| 76 |
free(c->something); |
| 77 |
c->something = n; |
| 78 |
|
| 79 |
return 1; |
| 80 |
} |
| 81 |
|
| 82 |
static int value_handler(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *reterr_error) { |
| 83 |
_cleanup_free_ char *s = NULL; |
| 84 |
const char *x; |
| 85 |
|
| 86 |
ASSERT_OK(asprintf(&s, "object %p, path %s", userdata, path)); |
| 87 |
ASSERT_OK(sd_bus_message_append(reply, "s", s)); |
| 88 |
|
| 89 |
ASSERT_NOT_NULL(x = startswith(path, "/value/")); |
| 90 |
|
| 91 |
ASSERT_EQ(PTR_TO_UINT(userdata), 30U); |
| 92 |
|
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
static int notify_test(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 97 |
ASSERT_OK(sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), m->path, "org.freedesktop.systemd.ValueTest", "Value", NULL)); |
| 98 |
|
| 99 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 100 |
|
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
static int notify_test2(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 105 |
ASSERT_OK(sd_bus_emit_properties_changed_strv(sd_bus_message_get_bus(m), m->path, "org.freedesktop.systemd.ValueTest", NULL)); |
| 106 |
|
| 107 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 108 |
|
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
static int emit_interfaces_added(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 113 |
ASSERT_OK(sd_bus_emit_interfaces_added(sd_bus_message_get_bus(m), "/value/a/x", "org.freedesktop.systemd.ValueTest", NULL)); |
| 114 |
|
| 115 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 116 |
|
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
static int emit_interfaces_removed(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 121 |
ASSERT_OK(sd_bus_emit_interfaces_removed(sd_bus_message_get_bus(m), "/value/a/x", "org.freedesktop.systemd.ValueTest", NULL)); |
| 122 |
|
| 123 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 124 |
|
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
static int emit_object_added(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 129 |
ASSERT_OK(sd_bus_emit_object_added(sd_bus_message_get_bus(m), "/value/a/x")); |
| 130 |
|
| 131 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 132 |
|
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
static int emit_object_with_manager_added(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 137 |
ASSERT_OK(sd_bus_emit_object_added(sd_bus_message_get_bus(m), "/value/a")); |
| 138 |
|
| 139 |
return ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 140 |
} |
| 141 |
|
| 142 |
static int emit_object_removed(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) { |
| 143 |
ASSERT_OK(sd_bus_emit_object_removed(sd_bus_message_get_bus(m), "/value/a/x")); |
| 144 |
|
| 145 |
ASSERT_OK(sd_bus_reply_method_return(m, NULL)); |
| 146 |
|
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
static const sd_bus_vtable vtable[] = { |
| 151 |
SD_BUS_VTABLE_START(0), |
| 152 |
SD_BUS_METHOD("AlterSomething", "s", "s", something_handler, 0), |
| 153 |
SD_BUS_METHOD("Exit", "", "", exit_handler, 0), |
| 154 |
SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0), |
| 155 |
SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL, offsetof(struct context, automatic_string_property), 0), |
| 156 |
SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL, offsetof(struct context, automatic_integer_property), 0), |
| 157 |
SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0), |
| 158 |
SD_BUS_METHOD("EmitInterfacesAdded", NULL, NULL, emit_interfaces_added, 0), |
| 159 |
SD_BUS_METHOD("EmitInterfacesRemoved", NULL, NULL, emit_interfaces_removed, 0), |
| 160 |
SD_BUS_METHOD("EmitObjectAdded", NULL, NULL, emit_object_added, 0), |
| 161 |
SD_BUS_METHOD("EmitObjectWithManagerAdded", NULL, NULL, emit_object_with_manager_added, 0), |
| 162 |
SD_BUS_METHOD("EmitObjectRemoved", NULL, NULL, emit_object_removed, 0), |
| 163 |
SD_BUS_VTABLE_END |
| 164 |
}; |
| 165 |
|
| 166 |
static const sd_bus_vtable vtable2[] = { |
| 167 |
SD_BUS_VTABLE_START(0), |
| 168 |
SD_BUS_METHOD("NotifyTest", "", "", notify_test, 0), |
| 169 |
SD_BUS_METHOD("NotifyTest2", "", "", notify_test2, 0), |
| 170 |
SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 171 |
SD_BUS_PROPERTY("Value2", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
| 172 |
SD_BUS_PROPERTY("Value3", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_CONST), |
| 173 |
SD_BUS_PROPERTY("Value4", "s", value_handler, 10, 0), |
| 174 |
SD_BUS_PROPERTY("AnExplicitProperty", "s", NULL, offsetof(struct context, something), SD_BUS_VTABLE_PROPERTY_EXPLICIT|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
| 175 |
SD_BUS_VTABLE_END |
| 176 |
}; |
| 177 |
|
| 178 |
static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *reterr_error) { |
| 179 |
assert(nodes); |
| 180 |
|
| 181 |
if (object_path_startswith("/value", path)) |
| 182 |
ASSERT_NOT_NULL(*nodes = strv_new("/value/c", "/value/b", "/value/a")); |
| 183 |
|
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
static int enumerator2_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *reterr_error) { |
| 188 |
assert(nodes); |
| 189 |
|
| 190 |
if (object_path_startswith("/value/a", path)) |
| 191 |
ASSERT_NOT_NULL(*nodes = strv_new("/value/a/z", "/value/a/x", "/value/a/y")); |
| 192 |
|
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
static int enumerator3_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *reterr_error) { |
| 197 |
_cleanup_strv_free_ char **v = NULL; |
| 198 |
|
| 199 |
assert(nodes); |
| 200 |
|
| 201 |
if (!object_path_startswith("/value/b", path)) |
| 202 |
return 1; |
| 203 |
|
| 204 |
for (unsigned i = 10; i < 20; i++) |
| 205 |
ASSERT_OK(strv_extendf(&v, "/value/b/%u", i)); |
| 206 |
for (unsigned i = 29; i >= 20; i--) |
| 207 |
ASSERT_OK(strv_extendf(&v, "/value/b/%u", i)); |
| 208 |
|
| 209 |
*nodes = TAKE_PTR(v); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
|
| 213 |
static int server(void *userdata) { |
| 214 |
struct context *c = ASSERT_PTR(userdata); |
| 215 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 216 |
sd_id128_t id; |
| 217 |
int r; |
| 218 |
|
| 219 |
c->quit = false; |
| 220 |
|
| 221 |
ASSERT_OK(sd_id128_randomize(&id)); |
| 222 |
|
| 223 |
ASSERT_OK(sd_bus_new(&bus)); |
| 224 |
ASSERT_OK(sd_bus_set_fd(bus, c->fds[0], c->fds[0])); |
| 225 |
ASSERT_OK(sd_bus_set_server(bus, 1, id)); |
| 226 |
|
| 227 |
ASSERT_OK(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test", vtable, c)); |
| 228 |
ASSERT_OK(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.test2", vtable, c)); |
| 229 |
ASSERT_OK(sd_bus_add_fallback_vtable(bus, NULL, "/value", "org.freedesktop.systemd.ValueTest", vtable2, NULL, UINT_TO_PTR(20))); |
| 230 |
ASSERT_OK(sd_bus_add_node_enumerator(bus, NULL, "/value", enumerator_callback, NULL)); |
| 231 |
ASSERT_OK(sd_bus_add_node_enumerator(bus, NULL, "/value/a", enumerator2_callback, NULL)); |
| 232 |
ASSERT_OK(sd_bus_add_node_enumerator(bus, NULL, "/value/b", enumerator3_callback, NULL)); |
| 233 |
ASSERT_OK(sd_bus_add_object_manager(bus, NULL, "/value")); |
| 234 |
ASSERT_OK(sd_bus_add_object_manager(bus, NULL, "/value/a")); |
| 235 |
|
| 236 |
ASSERT_OK(sd_bus_start(bus)); |
| 237 |
|
| 238 |
log_error("Entering event loop on server"); |
| 239 |
|
| 240 |
while (!c->quit) { |
| 241 |
log_error("Loop!"); |
| 242 |
|
| 243 |
r = sd_bus_process(bus, NULL); |
| 244 |
if (r < 0) |
| 245 |
return log_error_errno(r, "Failed to process requests: %m"); |
| 246 |
|
| 247 |
if (r == 0) { |
| 248 |
r = sd_bus_wait(bus, UINT64_MAX); |
| 249 |
if (r < 0) |
| 250 |
return log_error_errno(r, "Failed to wait: %m"); |
| 251 |
|
| 252 |
continue; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static int client(void *p) { |
| 260 |
struct context *c = ASSERT_PTR(p); |
| 261 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 262 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 263 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 264 |
_cleanup_strv_free_ char **lines = NULL; |
| 265 |
const char *s; |
| 266 |
int r; |
| 267 |
|
| 268 |
ASSERT_OK(sd_bus_new(&bus)); |
| 269 |
ASSERT_OK(sd_bus_set_fd(bus, c->fds[1], c->fds[1])); |
| 270 |
ASSERT_OK(sd_bus_start(bus)); |
| 271 |
|
| 272 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "NoOperation", &error, NULL, NULL)); |
| 273 |
|
| 274 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "s", "hallo")); |
| 275 |
|
| 276 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 277 |
ASSERT_STREQ(s, "<<<hallo>>>"); |
| 278 |
|
| 279 |
reply = sd_bus_message_unref(reply); |
| 280 |
|
| 281 |
ASSERT_FAIL(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Doesntexist", &error, &reply, "")); |
| 282 |
ASSERT_TRUE(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); |
| 283 |
|
| 284 |
sd_bus_error_free(&error); |
| 285 |
|
| 286 |
ASSERT_FAIL(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Doesntexist", &error, &reply, NULL)); /* NULL and "" are equivalent */ |
| 287 |
ASSERT_TRUE(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); |
| 288 |
|
| 289 |
sd_bus_error_free(&error); |
| 290 |
|
| 291 |
ASSERT_FAIL(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "as", 1, "hallo")); |
| 292 |
ASSERT_TRUE(sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)); |
| 293 |
|
| 294 |
sd_bus_error_free(&error); |
| 295 |
|
| 296 |
ASSERT_OK(sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s")); |
| 297 |
|
| 298 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 299 |
ASSERT_STREQ(s, "<<<hallo>>>"); |
| 300 |
|
| 301 |
reply = sd_bus_message_unref(reply); |
| 302 |
|
| 303 |
ASSERT_OK(sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, "s", "test")); |
| 304 |
|
| 305 |
ASSERT_OK(sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Something", &error, &reply, "s")); |
| 306 |
|
| 307 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 308 |
ASSERT_STREQ(s, "test"); |
| 309 |
|
| 310 |
reply = sd_bus_message_unref(reply); |
| 311 |
|
| 312 |
ASSERT_OK(sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticIntegerProperty", &error, "u", 815)); |
| 313 |
|
| 314 |
ASSERT_EQ(c->automatic_integer_property, 815U); |
| 315 |
|
| 316 |
ASSERT_OK(sd_bus_set_property(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AutomaticStringProperty", &error, "s", "Du Dödel, Du!")); |
| 317 |
|
| 318 |
ASSERT_STREQ(c->automatic_string_property, "Du Dödel, Du!"); |
| 319 |
|
| 320 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "")); |
| 321 |
|
| 322 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 323 |
fputs(s, stdout); |
| 324 |
|
| 325 |
reply = sd_bus_message_unref(reply); |
| 326 |
|
| 327 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, NULL)); /* NULL and "" are equivalent */ |
| 328 |
|
| 329 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 330 |
fputs(s, stdout); |
| 331 |
|
| 332 |
reply = sd_bus_message_unref(reply); |
| 333 |
|
| 334 |
ASSERT_OK(sd_bus_get_property(bus, "org.freedesktop.systemd.test", "/value/xuzz", "org.freedesktop.systemd.ValueTest", "Value", &error, &reply, "s")); |
| 335 |
|
| 336 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 337 |
log_info("read %s", s); |
| 338 |
|
| 339 |
reply = sd_bus_message_unref(reply); |
| 340 |
|
| 341 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, NULL)); |
| 342 |
|
| 343 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 344 |
fputs(s, stdout); |
| 345 |
|
| 346 |
reply = sd_bus_message_unref(reply); |
| 347 |
|
| 348 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, NULL)); |
| 349 |
|
| 350 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 351 |
fputs(s, stdout); |
| 352 |
|
| 353 |
reply = sd_bus_message_unref(reply); |
| 354 |
|
| 355 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, NULL)); |
| 356 |
|
| 357 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 358 |
fputs(s, stdout); |
| 359 |
|
| 360 |
ASSERT_NOT_NULL(lines = strv_split_newlines(s)); |
| 361 |
ASSERT_TRUE(strv_contains(lines, " <node name=\"x\"/>")); |
| 362 |
ASSERT_TRUE(strv_contains(lines, " <node name=\"y\"/>")); |
| 363 |
ASSERT_TRUE(strv_contains(lines, " <node name=\"z\"/>")); |
| 364 |
lines = strv_free(lines); |
| 365 |
|
| 366 |
reply = sd_bus_message_unref(reply); |
| 367 |
|
| 368 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/b", "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, NULL)); |
| 369 |
|
| 370 |
ASSERT_OK(sd_bus_message_read(reply, "s", &s)); |
| 371 |
fputs(s, stdout); |
| 372 |
|
| 373 |
ASSERT_NOT_NULL(lines = strv_split_newlines(s)); |
| 374 |
for (unsigned i = 10; i < 30; i++) { |
| 375 |
_cleanup_free_ char *n = NULL; |
| 376 |
|
| 377 |
ASSERT_OK(asprintf(&n, " <node name=\"%u\"/>", i)); |
| 378 |
ASSERT_TRUE(strv_contains(lines, n)); |
| 379 |
} |
| 380 |
lines = strv_free(lines); |
| 381 |
|
| 382 |
reply = sd_bus_message_unref(reply); |
| 383 |
|
| 384 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", NULL)); |
| 385 |
|
| 386 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 387 |
|
| 388 |
reply = sd_bus_message_unref(reply); |
| 389 |
|
| 390 |
ASSERT_FAIL(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", "org.freedesktop.systemd.ValueTest2")); |
| 391 |
ASSERT_TRUE(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_INTERFACE)); |
| 392 |
sd_bus_error_free(&error); |
| 393 |
|
| 394 |
ASSERT_FAIL(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, NULL)); |
| 395 |
ASSERT_TRUE(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); |
| 396 |
sd_bus_error_free(&error); |
| 397 |
|
| 398 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, NULL)); |
| 399 |
|
| 400 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 401 |
|
| 402 |
/* Check that /value/b does not have ObjectManager interface but /value/a does */ |
| 403 |
ASSERT_OK_POSITIVE(sd_bus_message_rewind(reply, 1)); |
| 404 |
ASSERT_OK_POSITIVE(sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{oa{sa{sv}}}")); |
| 405 |
unsigned path_count = 0; |
| 406 |
while (ASSERT_OK(sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "oa{sa{sv}}")) > 0) { |
| 407 |
const char *path = NULL; |
| 408 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 'o', &path)); |
| 409 |
|
| 410 |
/* Check if the enumerated path is sorted. */ |
| 411 |
switch (path_count) { |
| 412 |
case 0: |
| 413 |
ASSERT_STREQ(path, "/value/a"); |
| 414 |
break; |
| 415 |
case 1: |
| 416 |
ASSERT_STREQ(path, "/value/b"); |
| 417 |
break; |
| 418 |
case 2: |
| 419 |
ASSERT_STREQ(path, "/value/c"); |
| 420 |
break; |
| 421 |
default: { |
| 422 |
unsigned u = path_count - 3 + 10; |
| 423 |
_cleanup_free_ char *path_expected = NULL; |
| 424 |
ASSERT_OK(asprintf(&path_expected, "/value/b/%u", u)); |
| 425 |
ASSERT_STREQ(path, path_expected); |
| 426 |
}} |
| 427 |
path_count++; |
| 428 |
|
| 429 |
/* Check that there is no object manager interface here */ |
| 430 |
bool found_object_manager_interface = false; |
| 431 |
ASSERT_OK_POSITIVE(sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sa{sv}}")); |
| 432 |
while (ASSERT_OK(sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sa{sv}")) > 0) { |
| 433 |
const char *interface_name = NULL; |
| 434 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 's', &interface_name)); |
| 435 |
|
| 436 |
if (streq(interface_name, "org.freedesktop.DBus.ObjectManager")) |
| 437 |
found_object_manager_interface = true; |
| 438 |
|
| 439 |
ASSERT_OK(sd_bus_message_skip(reply, "a{sv}")); |
| 440 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 441 |
} |
| 442 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 443 |
|
| 444 |
ASSERT_EQ(found_object_manager_interface, streq(path, "/value/a")); |
| 445 |
|
| 446 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 447 |
} |
| 448 |
|
| 449 |
reply = sd_bus_message_unref(reply); |
| 450 |
|
| 451 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest", &error, NULL, NULL)); |
| 452 |
|
| 453 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 454 |
|
| 455 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged")); |
| 456 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 457 |
|
| 458 |
reply = sd_bus_message_unref(reply); |
| 459 |
|
| 460 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.systemd.ValueTest", "NotifyTest2", &error, NULL, NULL)); |
| 461 |
|
| 462 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 463 |
|
| 464 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.Properties", "PropertiesChanged")); |
| 465 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 466 |
|
| 467 |
reply = sd_bus_message_unref(reply); |
| 468 |
|
| 469 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesAdded", &error, NULL, NULL)); |
| 470 |
|
| 471 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 472 |
|
| 473 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")); |
| 474 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 475 |
|
| 476 |
reply = sd_bus_message_unref(reply); |
| 477 |
|
| 478 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitInterfacesRemoved", &error, NULL, NULL)); |
| 479 |
|
| 480 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 481 |
|
| 482 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved")); |
| 483 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 484 |
|
| 485 |
reply = sd_bus_message_unref(reply); |
| 486 |
|
| 487 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectAdded", &error, NULL, NULL)); |
| 488 |
|
| 489 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 490 |
|
| 491 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")); |
| 492 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 493 |
|
| 494 |
/* Check if /value/a/x does not have org.freedesktop.DBus.ObjectManager */ |
| 495 |
ASSERT_OK(sd_bus_message_rewind(reply, 1)); |
| 496 |
const char* should_be_value_a_x = NULL; |
| 497 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 'o', &should_be_value_a_x)); |
| 498 |
ASSERT_STREQ(should_be_value_a_x, "/value/a/x"); |
| 499 |
ASSERT_OK_POSITIVE(sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sa{sv}}")); |
| 500 |
while (ASSERT_OK(sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sa{sv}")) > 0) { |
| 501 |
const char* interface_name = NULL; |
| 502 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 's', &interface_name)); |
| 503 |
|
| 504 |
ASSERT_FALSE(streq(interface_name, "org.freedesktop.DBus.ObjectManager")); |
| 505 |
|
| 506 |
ASSERT_OK(sd_bus_message_skip(reply, "a{sv}")); |
| 507 |
|
| 508 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 509 |
} |
| 510 |
|
| 511 |
reply = sd_bus_message_unref(reply); |
| 512 |
|
| 513 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectWithManagerAdded", &error, NULL, NULL)); |
| 514 |
|
| 515 |
ASSERT_OK_POSITIVE(sd_bus_process(bus, &reply)); |
| 516 |
|
| 517 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")); |
| 518 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 519 |
|
| 520 |
/* Check if /value/a has org.freedesktop.DBus.ObjectManager */ |
| 521 |
ASSERT_OK(sd_bus_message_rewind(reply, 1)); |
| 522 |
const char* should_be_value_a = NULL; |
| 523 |
bool found_object_manager = false; |
| 524 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 'o', &should_be_value_a)); |
| 525 |
ASSERT_STREQ(should_be_value_a, "/value/a"); |
| 526 |
ASSERT_OK_POSITIVE(sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sa{sv}}")); |
| 527 |
while (ASSERT_OK(sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sa{sv}")) > 0) { |
| 528 |
const char* interface_name = NULL; |
| 529 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 's', &interface_name)); |
| 530 |
|
| 531 |
if (streq(interface_name, "org.freedesktop.DBus.ObjectManager")) { |
| 532 |
found_object_manager = true; |
| 533 |
break; |
| 534 |
} |
| 535 |
|
| 536 |
ASSERT_OK(sd_bus_message_skip(reply, "a{sv}")); |
| 537 |
|
| 538 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 539 |
} |
| 540 |
ASSERT_TRUE(found_object_manager); |
| 541 |
|
| 542 |
reply = sd_bus_message_unref(reply); |
| 543 |
|
| 544 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "EmitObjectRemoved", &error, NULL, NULL)); |
| 545 |
|
| 546 |
ASSERT_OK_POSITIVE(r = sd_bus_process(bus, &reply)); |
| 547 |
|
| 548 |
ASSERT_OK_POSITIVE(sd_bus_message_is_signal(reply, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved")); |
| 549 |
sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 550 |
|
| 551 |
/* Check if /value/a/x does not have org.freedesktop.DBus.ObjectManager */ |
| 552 |
ASSERT_OK(sd_bus_message_rewind(reply, 1)); |
| 553 |
should_be_value_a_x = NULL; |
| 554 |
ASSERT_OK_POSITIVE(sd_bus_message_read_basic(reply, 'o', &should_be_value_a_x)); |
| 555 |
ASSERT_STREQ(should_be_value_a_x, "/value/a/x"); |
| 556 |
ASSERT_OK_POSITIVE(sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s")); |
| 557 |
const char* deleted_interface_name = NULL; |
| 558 |
while (ASSERT_OK(sd_bus_message_read_basic(reply, 's', &deleted_interface_name))) |
| 559 |
ASSERT_FALSE(streq(deleted_interface_name, "org.freedesktop.DBus.ObjectManager")); |
| 560 |
ASSERT_OK(sd_bus_message_exit_container(reply)); |
| 561 |
|
| 562 |
reply = sd_bus_message_unref(reply); |
| 563 |
|
| 564 |
ASSERT_OK(sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Exit", &error, NULL, NULL)); |
| 565 |
|
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
int main(int argc, char *argv[]) { |
| 570 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 571 |
_cleanup_(sd_future_unrefp) sd_future *f_server = NULL, *f_client = NULL; |
| 572 |
struct context c = {}; |
| 573 |
|
| 574 |
test_setup_logging(LOG_DEBUG); |
| 575 |
|
| 576 |
c.automatic_integer_property = 4711; |
| 577 |
ASSERT_NOT_NULL(c.automatic_string_property = strdup("dudeldu")); |
| 578 |
|
| 579 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds)); |
| 580 |
|
| 581 |
ASSERT_OK(sd_event_new(&e)); |
| 582 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 583 |
|
| 584 |
ASSERT_OK(sd_fiber_new(e, "server", server, &c, /* destroy= */ NULL, &f_server)); |
| 585 |
ASSERT_OK(sd_fiber_new(e, "client", client, &c, /* destroy= */ NULL, &f_client)); |
| 586 |
|
| 587 |
ASSERT_OK(sd_event_loop(e)); |
| 588 |
|
| 589 |
ASSERT_OK(sd_future_result(f_server)); |
| 590 |
ASSERT_OK(sd_future_result(f_client)); |
| 591 |
|
| 592 |
free(c.something); |
| 593 |
free(c.automatic_string_property); |
| 594 |
|
| 595 |
return EXIT_SUCCESS; |
| 596 |
} |