| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* We make an exception here to our usual "include system headers first" rule because we need one of these |
| 4 |
* macros to disable a warning triggered by the glib headers. */ |
| 5 |
#include "macro.h" |
| 6 |
|
| 7 |
#if HAVE_GLIB |
| 8 |
DISABLE_WARNING_FORMAT_NONLITERAL |
| 9 |
#include <gio/gio.h> /* NOLINT */ |
| 10 |
REENABLE_WARNING |
| 11 |
#endif |
| 12 |
|
| 13 |
#if HAVE_DBUS |
| 14 |
#include <dbus/dbus.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "sd-bus.h" |
| 18 |
|
| 19 |
#include "alloc-util.h" |
| 20 |
#include "bus-internal.h" |
| 21 |
#include "bus-label.h" |
| 22 |
#include "bus-message.h" |
| 23 |
#include "bus-util.h" |
| 24 |
#include "dlfcn-util.h" |
| 25 |
#include "escape.h" |
| 26 |
#include "fd-util.h" |
| 27 |
#include "log.h" |
| 28 |
#include "memfd-util.h" |
| 29 |
#include "memstream-util.h" |
| 30 |
#include "stat-util.h" |
| 31 |
#include "tests.h" |
| 32 |
|
| 33 |
#if HAVE_GLIB |
| 34 |
static DLSYM_PROTOTYPE(g_dbus_message_new_from_blob) = NULL; |
| 35 |
static DLSYM_PROTOTYPE(g_dbus_message_print) = NULL; |
| 36 |
static DLSYM_PROTOTYPE(g_free) = NULL; |
| 37 |
static DLSYM_PROTOTYPE(g_object_unref) = NULL; |
| 38 |
|
| 39 |
static int dlopen_glib(void) { |
| 40 |
static void *glib_dl = NULL; |
| 41 |
|
| 42 |
return dlopen_many_sym_or_warn( |
| 43 |
&glib_dl, "libgio-2.0.so.0", LOG_DEBUG, |
| 44 |
DLSYM_ARG(g_dbus_message_new_from_blob), |
| 45 |
DLSYM_ARG(g_dbus_message_print), |
| 46 |
DLSYM_ARG(g_free), |
| 47 |
DLSYM_ARG(g_object_unref)); |
| 48 |
} |
| 49 |
#endif |
| 50 |
|
| 51 |
#if HAVE_DBUS |
| 52 |
static DLSYM_PROTOTYPE(dbus_error_init) = NULL; |
| 53 |
static DLSYM_PROTOTYPE(dbus_error_free) = NULL; |
| 54 |
static DLSYM_PROTOTYPE(dbus_message_demarshal) = NULL; |
| 55 |
static DLSYM_PROTOTYPE(dbus_message_unref) = NULL; |
| 56 |
|
| 57 |
static int dlopen_libdbus(void) { |
| 58 |
static void *libdbus_dl = NULL; |
| 59 |
|
| 60 |
return dlopen_many_sym_or_warn( |
| 61 |
&libdbus_dl, "libdbus-1.so.3", LOG_DEBUG, |
| 62 |
DLSYM_ARG(dbus_error_init), |
| 63 |
DLSYM_ARG(dbus_error_free), |
| 64 |
DLSYM_ARG(dbus_message_demarshal), |
| 65 |
DLSYM_ARG(dbus_message_unref)); |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
static void test_bus_path_encode_unique(void) { |
| 70 |
_cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; |
| 71 |
|
| 72 |
assert_se(bus_path_encode_unique(NULL, "/foo/bar", "some.sender", "a.suffix", &a) >= 0 && streq_ptr(a, "/foo/bar/some_2esender/a_2esuffix")); |
| 73 |
assert_se(bus_path_decode_unique(a, "/foo/bar", &b, &c) > 0 && streq_ptr(b, "some.sender") && streq_ptr(c, "a.suffix")); |
| 74 |
assert_se(bus_path_decode_unique(a, "/bar/foo", &d, &d) == 0 && !d); |
| 75 |
assert_se(bus_path_decode_unique("/foo/bar/onlyOneSuffix", "/foo/bar", &d, &d) == 0 && !d); |
| 76 |
assert_se(bus_path_decode_unique("/foo/bar/_/_", "/foo/bar", &d, &e) > 0 && streq_ptr(d, "") && streq_ptr(e, "")); |
| 77 |
} |
| 78 |
|
| 79 |
static void test_bus_path_encode(void) { |
| 80 |
_cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL; |
| 81 |
|
| 82 |
assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo")); |
| 83 |
assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL); |
| 84 |
assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo")); |
| 85 |
|
| 86 |
ASSERT_RETURN_EXPECTED_SE(sd_bus_path_encode("xxxx", "waldo", &c) < 0); |
| 87 |
ASSERT_RETURN_EXPECTED_SE(sd_bus_path_encode("/foo/", "waldo", &c) < 0); |
| 88 |
|
| 89 |
assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_")); |
| 90 |
assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, "")); |
| 91 |
|
| 92 |
assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar")); |
| 93 |
assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar")); |
| 94 |
|
| 95 |
assert_se(sd_bus_path_decode("/waldo", "/waldo", &g) > 0 && streq(g, "")); |
| 96 |
} |
| 97 |
|
| 98 |
static void test_bus_path_encode_many(void) { |
| 99 |
_cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL; |
| 100 |
|
| 101 |
assert_se(sd_bus_path_decode_many("/foo/bar", "/prefix/%", NULL) == 0); |
| 102 |
assert_se(sd_bus_path_decode_many("/prefix/bar", "/prefix/%bar", NULL) == 1); |
| 103 |
assert_se(sd_bus_path_decode_many("/foo/bar", "/prefix/%/suffix", NULL) == 0); |
| 104 |
assert_se(sd_bus_path_decode_many("/prefix/foobar/suffix", "/prefix/%/suffix", &a) == 1 && streq_ptr(a, "foobar")); |
| 105 |
assert_se(sd_bus_path_decode_many("/prefix/one_foo_two/mid/three_bar_four/suffix", "/prefix/one_%_two/mid/three_%_four/suffix", &b, &c) == 1 && streq_ptr(b, "foo") && streq_ptr(c, "bar")); |
| 106 |
assert_se(sd_bus_path_decode_many("/prefix/one_foo_two/mid/three_bar_four/suffix", "/prefix/one_%_two/mid/three_%_four/suffix", NULL, &d) == 1 && streq_ptr(d, "bar")); |
| 107 |
|
| 108 |
assert_se(sd_bus_path_decode_many("/foo/bar", "/foo/bar/%", NULL) == 0); |
| 109 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/bar%", NULL) == 0); |
| 110 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/bar", NULL) == 0); |
| 111 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%bar", NULL) == 0); |
| 112 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/bar/suffix") == 1); |
| 113 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%%/suffix", NULL, NULL) == 0); /* multiple '%' are treated verbatim */ |
| 114 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/suffi", NULL) == 0); |
| 115 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/suffix", &e) == 1 && streq_ptr(e, "bar")); |
| 116 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/foo/%/%", NULL, NULL) == 1); |
| 117 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%/%", NULL, NULL, NULL) == 1); |
| 118 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "%/%/%", NULL, NULL, NULL) == 0); |
| 119 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%", NULL, NULL) == 0); |
| 120 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/%/", NULL, NULL) == 0); |
| 121 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%/", NULL) == 0); |
| 122 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "/%", NULL) == 0); |
| 123 |
assert_se(sd_bus_path_decode_many("/foo/bar/suffix", "%", NULL) == 0); |
| 124 |
|
| 125 |
assert_se(sd_bus_path_encode_many(&f, "/prefix/one_%_two/mid/three_%_four/suffix", "foo", "bar") >= 0 && streq_ptr(f, "/prefix/one_foo_two/mid/three_bar_four/suffix")); |
| 126 |
} |
| 127 |
|
| 128 |
static void test_bus_label_escape_one(const char *a, const char *b) { |
| 129 |
_cleanup_free_ char *t = NULL, *x = NULL, *y = NULL; |
| 130 |
|
| 131 |
assert_se(t = bus_label_escape(a)); |
| 132 |
assert_se(streq(t, b)); |
| 133 |
|
| 134 |
assert_se(x = bus_label_unescape(t)); |
| 135 |
assert_se(streq(a, x)); |
| 136 |
|
| 137 |
assert_se(y = bus_label_unescape(b)); |
| 138 |
assert_se(streq(a, y)); |
| 139 |
} |
| 140 |
|
| 141 |
static ino_t get_inode(int fd) { |
| 142 |
struct stat st; |
| 143 |
assert_se(fstat(fd, &st) >= 0); |
| 144 |
return st.st_ino; |
| 145 |
} |
| 146 |
|
| 147 |
static void test_bus_fds_truncated(void) { |
| 148 |
_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
| 149 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 150 |
_cleanup_close_ int memfd0 = -EBADF, memfd1 = -EBADF; |
| 151 |
_cleanup_free_ void *blob = NULL; |
| 152 |
_cleanup_free_ int *fds = NULL; |
| 153 |
ino_t ino0, ino1; |
| 154 |
size_t blob_size; |
| 155 |
int fd; |
| 156 |
|
| 157 |
/* Create two memfds and record their inodes for later verification */ |
| 158 |
memfd0 = ASSERT_OK(memfd_create_wrapper("test-fd-0", 0)); |
| 159 |
ino0 = get_inode(memfd0); |
| 160 |
|
| 161 |
memfd1 = ASSERT_OK(memfd_create_wrapper("test-fd-1", 0)); |
| 162 |
ino1 = get_inode(memfd1); |
| 163 |
|
| 164 |
/* Create a bus for message operations (no actual connection needed) */ |
| 165 |
ASSERT_OK(sd_bus_new(&bus)); |
| 166 |
bus->state = BUS_RUNNING; /* Fake state to allow message creation */ |
| 167 |
bus->can_fds = true; /* Allow fd passing */ |
| 168 |
|
| 169 |
/* Build a message containing two fds */ |
| 170 |
ASSERT_OK(sd_bus_message_new_method_call(bus, &m, "foo.bar", "/", "foo.bar", "Ping")); |
| 171 |
ASSERT_OK(sd_bus_message_append(m, "hh", memfd0, memfd1)); |
| 172 |
ASSERT_OK(sd_bus_message_seal(m, 1, 0)); |
| 173 |
|
| 174 |
/* Serialize the message to a blob */ |
| 175 |
ASSERT_OK(bus_message_get_blob(m, &blob, &blob_size)); |
| 176 |
m = sd_bus_message_unref(m); |
| 177 |
|
| 178 |
/* Duplicate the fds since bus_message_from_malloc() takes ownership */ |
| 179 |
fds = ASSERT_NOT_NULL(new(int, 2)); |
| 180 |
fds[0] = ASSERT_OK_ERRNO(fcntl(memfd0, F_DUPFD_CLOEXEC, 3)); |
| 181 |
fds[1] = ASSERT_OK_ERRNO(fcntl(memfd1, F_DUPFD_CLOEXEC, 3)); |
| 182 |
|
| 183 |
/* Test 1: Parse with correct fd count, no truncation - should succeed */ |
| 184 |
log_info("Test 1: Exact fd count match, got_ctrunc=false"); |
| 185 |
void *blob_copy = ASSERT_NOT_NULL(memdup(blob, blob_size)); |
| 186 |
ASSERT_OK(bus_message_from_malloc(bus, blob_copy, blob_size, fds, 2, /* got_ctrunc= */ false, NULL, &m)); |
| 187 |
|
| 188 |
/* Verify we can read both fds and they have the expected inodes */ |
| 189 |
ASSERT_OK(sd_bus_message_read_basic(m, 'h', &fd)); |
| 190 |
ASSERT_EQ(get_inode(fd), ino0); |
| 191 |
ASSERT_OK(sd_bus_message_read_basic(m, 'h', &fd)); |
| 192 |
ASSERT_EQ(get_inode(fd), ino1); |
| 193 |
|
| 194 |
m = sd_bus_message_unref(m); |
| 195 |
fds = NULL; /* ownership transferred */ |
| 196 |
|
| 197 |
/* Test 2: Parse with fewer fds than declared, no truncation flag - should fail */ |
| 198 |
log_info("Test 2: Fewer fds than declared, got_ctrunc=false"); |
| 199 |
fds = ASSERT_NOT_NULL(new(int, 1)); |
| 200 |
fds[0] = ASSERT_OK_ERRNO(fcntl(memfd0, F_DUPFD_CLOEXEC, 3)); |
| 201 |
|
| 202 |
blob_copy = ASSERT_NOT_NULL(memdup(blob, blob_size)); |
| 203 |
ASSERT_ERROR(bus_message_from_malloc(bus, blob_copy, blob_size, fds, 1, /* got_ctrunc= */ false, NULL, &m), EBADMSG); |
| 204 |
free(blob_copy); |
| 205 |
close(fds[0]); |
| 206 |
fds = mfree(fds); |
| 207 |
|
| 208 |
/* Test 3: Parse with fewer fds than declared, with truncation flag - parsing should succeed */ |
| 209 |
log_info("Test 3: Fewer fds than declared, got_ctrunc=true"); |
| 210 |
fds = ASSERT_NOT_NULL(new(int, 1)); |
| 211 |
fds[0] = ASSERT_OK_ERRNO(fcntl(memfd0, F_DUPFD_CLOEXEC, 3)); |
| 212 |
|
| 213 |
blob_copy = ASSERT_NOT_NULL(memdup(blob, blob_size)); |
| 214 |
ASSERT_OK(bus_message_from_malloc(bus, blob_copy, blob_size, fds, 1, /* got_ctrunc= */ true, NULL, &m)); |
| 215 |
|
| 216 |
/* First fd should be readable and have correct inode */ |
| 217 |
ASSERT_OK(sd_bus_message_read_basic(m, 'h', &fd)); |
| 218 |
ASSERT_EQ(get_inode(fd), ino0); |
| 219 |
|
| 220 |
/* Second fd was truncated - reading it should fail */ |
| 221 |
ASSERT_ERROR(sd_bus_message_read_basic(m, 'h', &fd), EBADMSG); |
| 222 |
|
| 223 |
m = sd_bus_message_unref(m); |
| 224 |
fds = NULL; /* ownership transferred */ |
| 225 |
|
| 226 |
/* Test 4: Parse with more fds than declared, with truncation flag - should fail */ |
| 227 |
log_info("Test 4: More fds than declared, got_ctrunc=true"); |
| 228 |
fds = ASSERT_NOT_NULL(new(int, 3)); |
| 229 |
fds[0] = ASSERT_OK_ERRNO(fcntl(memfd0, F_DUPFD_CLOEXEC, 3)); |
| 230 |
fds[1] = ASSERT_OK_ERRNO(fcntl(memfd1, F_DUPFD_CLOEXEC, 3)); |
| 231 |
fds[2] = ASSERT_OK_ERRNO(fcntl(memfd0, F_DUPFD_CLOEXEC, 3)); |
| 232 |
|
| 233 |
blob_copy = ASSERT_NOT_NULL(memdup(blob, blob_size)); |
| 234 |
ASSERT_ERROR(bus_message_from_malloc(bus, blob_copy, blob_size, fds, 3, /* got_ctrunc= */ true, NULL, &m), EBADMSG); |
| 235 |
free(blob_copy); |
| 236 |
close(fds[0]); |
| 237 |
close(fds[1]); |
| 238 |
close(fds[2]); |
| 239 |
fds = mfree(fds); |
| 240 |
|
| 241 |
/* Test 5: Parse with zero fds when two were declared, with truncation flag - should succeed */ |
| 242 |
log_info("Test 5: Zero fds when some declared, got_ctrunc=true"); |
| 243 |
blob_copy = ASSERT_NOT_NULL(memdup(blob, blob_size)); |
| 244 |
ASSERT_OK(bus_message_from_malloc(bus, blob_copy, blob_size, NULL, 0, /* got_ctrunc= */ true, NULL, &m)); |
| 245 |
|
| 246 |
/* Both fd reads should fail since all were truncated */ |
| 247 |
ASSERT_ERROR(sd_bus_message_read_basic(m, 'h', &fd), EBADMSG); |
| 248 |
|
| 249 |
m = sd_bus_message_unref(m); |
| 250 |
|
| 251 |
log_info("All fd truncation tests passed"); |
| 252 |
} |
| 253 |
|
| 254 |
static void test_bus_nested_variant_depth_limit(void) { |
| 255 |
/* Craft a raw D-Bus message with an unknown header field whose value is a variant |
| 256 |
* containing a variant containing a variant... nested beyond BUS_CONTAINER_DEPTH. |
| 257 |
* Without the depth limit in message_skip_fields(), this causes unbounded recursion |
| 258 |
* and stack overflow. With the fix, it should be rejected with -EBADMSG. */ |
| 259 |
|
| 260 |
_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
| 261 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 262 |
const unsigned depth = BUS_CONTAINER_DEPTH + 1; /* one past the limit */ |
| 263 |
|
| 264 |
/* Each nesting level in the fields area is: 1 byte sig_len + 1 byte 'v' + 1 byte NUL = 3 bytes. |
| 265 |
* The innermost level has sig_len=1, 'u', NUL, then 4 bytes for the uint32 value. |
| 266 |
* The field header is: 1 byte field_code + 1 byte sig_len + 1 byte 'v' + 1 byte NUL = 4 bytes. */ |
| 267 |
size_t fields_size = 4 + (depth * 3) + 4; /* field header + nested variant sigs + uint32 */ |
| 268 |
size_t padded_fields = ALIGN8(fields_size); |
| 269 |
size_t total = sizeof(BusMessageHeader) + padded_fields; |
| 270 |
|
| 271 |
_cleanup_free_ void *buf = ASSERT_PTR(malloc0(total)); |
| 272 |
|
| 273 |
BusMessageHeader *h = buf; |
| 274 |
*h = (BusMessageHeader) { |
| 275 |
.endian = BUS_NATIVE_ENDIAN, |
| 276 |
.type = SD_BUS_MESSAGE_METHOD_CALL, |
| 277 |
.version = 1, |
| 278 |
.serial = 1, |
| 279 |
.fields_size = (uint32_t) fields_size, |
| 280 |
}; |
| 281 |
|
| 282 |
uint8_t *p = (uint8_t *) buf + sizeof(BusMessageHeader); |
| 283 |
|
| 284 |
/* Unknown field code (triggers default: in message_parse_fields) */ |
| 285 |
*p++ = 0xFF; |
| 286 |
/* Field signature: variant */ |
| 287 |
*p++ = 1; /* sig length */ |
| 288 |
*p++ = 'v'; |
| 289 |
*p++ = '\0'; |
| 290 |
|
| 291 |
/* Nested variant signatures: each level declares its content is another variant */ |
| 292 |
for (unsigned i = 0; i < depth; i++) { |
| 293 |
*p++ = 1; /* sig length */ |
| 294 |
*p++ = 'v'; |
| 295 |
*p++ = '\0'; |
| 296 |
} |
| 297 |
|
| 298 |
/* Innermost value: a uint32 */ |
| 299 |
memset(p, 0, 4); |
| 300 |
|
| 301 |
ASSERT_OK(sd_bus_new(&bus)); |
| 302 |
|
| 303 |
ASSERT_ERROR(bus_message_from_malloc(bus, buf, total, NULL, 0, false, NULL, &m), EBADMSG); |
| 304 |
} |
| 305 |
|
| 306 |
static void test_bus_label_escape(void) { |
| 307 |
test_bus_label_escape_one("foo123bar", "foo123bar"); |
| 308 |
test_bus_label_escape_one("foo.bar", "foo_2ebar"); |
| 309 |
test_bus_label_escape_one("foo_2ebar", "foo_5f2ebar"); |
| 310 |
test_bus_label_escape_one("", "_"); |
| 311 |
test_bus_label_escape_one("_", "_5f"); |
| 312 |
test_bus_label_escape_one("1", "_31"); |
| 313 |
test_bus_label_escape_one(":1", "_3a1"); |
| 314 |
} |
| 315 |
|
| 316 |
int main(int argc, char *argv[]) { |
| 317 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *copy = NULL; |
| 318 |
_cleanup_free_ char *h = NULL, *first = NULL, *second = NULL, *third = NULL; |
| 319 |
const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array; |
| 320 |
const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature; |
| 321 |
size_t sz, first_size, second_size = 0, third_size = 0; |
| 322 |
_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
| 323 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 324 |
void *buffer = NULL; |
| 325 |
int r, boolean; |
| 326 |
uint64_t u64; |
| 327 |
uint8_t u, v; |
| 328 |
double dbl; |
| 329 |
FILE *mf; |
| 330 |
char *s; |
| 331 |
|
| 332 |
test_setup_logging(LOG_INFO); |
| 333 |
|
| 334 |
r = sd_bus_default_user(&bus); |
| 335 |
if (r < 0) |
| 336 |
r = sd_bus_default_system(&bus); |
| 337 |
if (r < 0) |
| 338 |
return log_tests_skipped("Failed to connect to bus"); |
| 339 |
|
| 340 |
r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep"); |
| 341 |
assert_se(r >= 0); |
| 342 |
|
| 343 |
r = sd_bus_message_append(m, ""); |
| 344 |
assert_se(r >= 0); |
| 345 |
|
| 346 |
r = sd_bus_message_append(m, "s", "a string"); |
| 347 |
assert_se(r >= 0); |
| 348 |
|
| 349 |
r = sd_bus_message_append(m, "s", NULL); |
| 350 |
assert_se(r >= 0); |
| 351 |
|
| 352 |
r = sd_bus_message_append(m, "asg", 2, "string #1", "string #2", "sba(tt)ss"); |
| 353 |
assert_se(r >= 0); |
| 354 |
|
| 355 |
r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after"); |
| 356 |
assert_se(r >= 0); |
| 357 |
|
| 358 |
r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo"); |
| 359 |
assert_se(r >= 0); |
| 360 |
|
| 361 |
r = sd_bus_message_append(m, "y(ty)y(yt)y", 8, 777ULL, 7, 9, 77, 7777ULL, 10); |
| 362 |
assert_se(r >= 0); |
| 363 |
|
| 364 |
r = sd_bus_message_append(m, "()"); |
| 365 |
assert_se(r == -EINVAL); |
| 366 |
|
| 367 |
r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3"); |
| 368 |
assert_se(r >= 0); |
| 369 |
|
| 370 |
r = sd_bus_message_open_container(m, 'a', "s"); |
| 371 |
assert_se(r >= 0); |
| 372 |
|
| 373 |
r = sd_bus_message_append_basic(m, 's', "foobar"); |
| 374 |
assert_se(r >= 0); |
| 375 |
|
| 376 |
r = sd_bus_message_append_basic(m, 's', "waldo"); |
| 377 |
assert_se(r >= 0); |
| 378 |
|
| 379 |
r = sd_bus_message_close_container(m); |
| 380 |
assert_se(r >= 0); |
| 381 |
|
| 382 |
r = sd_bus_message_append_string_space(m, 5, &s); |
| 383 |
assert_se(r >= 0); |
| 384 |
strcpy(s, "hallo"); |
| 385 |
|
| 386 |
r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array)); |
| 387 |
assert_se(r >= 0); |
| 388 |
|
| 389 |
r = sd_bus_message_append_array(m, 'u', NULL, 0); |
| 390 |
assert_se(r >= 0); |
| 391 |
|
| 392 |
r = sd_bus_message_append(m, "a(stdo)", 1, "foo", 815ULL, 47.0, "/"); |
| 393 |
assert_se(r >= 0); |
| 394 |
|
| 395 |
r = sd_bus_message_seal(m, 4711, 0); |
| 396 |
assert_se(r >= 0); |
| 397 |
|
| 398 |
sd_bus_message_dump(m, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 399 |
|
| 400 |
assert_se(mf = memstream_init(&ms)); |
| 401 |
sd_bus_message_dump(m, mf, 0); |
| 402 |
assert_se(memstream_finalize(&ms, &first, &first_size) >= 0); |
| 403 |
|
| 404 |
r = bus_message_get_blob(m, &buffer, &sz); |
| 405 |
assert_se(r >= 0); |
| 406 |
|
| 407 |
h = cescape_length(buffer, sz); |
| 408 |
assert_se(h); |
| 409 |
log_info("message size = %zu, contents =\n%s", sz, h); |
| 410 |
|
| 411 |
#if HAVE_GLIB |
| 412 |
if (dlopen_glib() >= 0) { |
| 413 |
GDBusMessage *g; |
| 414 |
char *p; |
| 415 |
|
| 416 |
g = sym_g_dbus_message_new_from_blob(buffer, sz, 0, NULL); |
| 417 |
p = sym_g_dbus_message_print(g, 0); |
| 418 |
log_info("%s", p); |
| 419 |
sym_g_free(p); |
| 420 |
sym_g_object_unref(g); |
| 421 |
} |
| 422 |
#endif |
| 423 |
|
| 424 |
#if HAVE_DBUS |
| 425 |
if (dlopen_libdbus() >= 0) { |
| 426 |
DBusMessage *w; |
| 427 |
DBusError error; |
| 428 |
|
| 429 |
sym_dbus_error_init(&error); |
| 430 |
|
| 431 |
w = sym_dbus_message_demarshal(buffer, sz, &error); |
| 432 |
if (!w) |
| 433 |
log_error("%s", error.message); |
| 434 |
else |
| 435 |
sym_dbus_message_unref(w); |
| 436 |
|
| 437 |
sym_dbus_error_free(&error); |
| 438 |
} |
| 439 |
#endif |
| 440 |
|
| 441 |
m = sd_bus_message_unref(m); |
| 442 |
|
| 443 |
r = bus_message_from_malloc(bus, buffer, sz, NULL, 0, /* got_ctrunc= */ false, NULL, &m); |
| 444 |
assert_se(r >= 0); |
| 445 |
|
| 446 |
sd_bus_message_dump(m, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); |
| 447 |
|
| 448 |
assert_se(mf = memstream_init(&ms)); |
| 449 |
sd_bus_message_dump(m, mf, 0); |
| 450 |
assert_se(memstream_finalize(&ms, &second, &second_size) >= 0); |
| 451 |
assert_se(first_size == second_size); |
| 452 |
assert_se(memcmp(first, second, first_size) == 0); |
| 453 |
|
| 454 |
assert_se(sd_bus_message_rewind(m, true) >= 0); |
| 455 |
|
| 456 |
r = sd_bus_message_read(m, "ssasg", &x, &x2, 2, &y, &z, &a_signature); |
| 457 |
assert_se(r > 0); |
| 458 |
assert_se(streq(x, "a string")); |
| 459 |
assert_se(streq(x2, "")); |
| 460 |
assert_se(streq(y, "string #1")); |
| 461 |
assert_se(streq(z, "string #2")); |
| 462 |
assert_se(streq(a_signature, "sba(tt)ss")); |
| 463 |
|
| 464 |
r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d); |
| 465 |
assert_se(r > 0); |
| 466 |
assert_se(streq(x, "foobar")); |
| 467 |
assert_se(streq(y, "foo")); |
| 468 |
assert_se(streq(z, "bar")); |
| 469 |
assert_se(streq(a, "waldo")); |
| 470 |
assert_se(streq(b, "piep")); |
| 471 |
assert_se(streq(c, "pap")); |
| 472 |
assert_se(streq(d, "after")); |
| 473 |
|
| 474 |
r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y); |
| 475 |
assert_se(r > 0); |
| 476 |
assert_se(u == 3); |
| 477 |
assert_se(streq(x, "foo")); |
| 478 |
assert_se(v == 5); |
| 479 |
assert_se(streq(y, "waldo")); |
| 480 |
|
| 481 |
r = sd_bus_message_read(m, "y(ty)", &v, &u64, &u); |
| 482 |
assert_se(r > 0); |
| 483 |
assert_se(v == 8); |
| 484 |
assert_se(u64 == 777); |
| 485 |
assert_se(u == 7); |
| 486 |
|
| 487 |
r = sd_bus_message_read(m, "y(yt)", &v, &u, &u64); |
| 488 |
assert_se(r > 0); |
| 489 |
assert_se(v == 9); |
| 490 |
assert_se(u == 77); |
| 491 |
assert_se(u64 == 7777); |
| 492 |
|
| 493 |
r = sd_bus_message_read(m, "y", &v); |
| 494 |
assert_se(r > 0); |
| 495 |
assert_se(v == 10); |
| 496 |
|
| 497 |
r = sd_bus_message_read(m, "()"); |
| 498 |
assert_se(r < 0); |
| 499 |
|
| 500 |
r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d); |
| 501 |
assert_se(r > 0); |
| 502 |
assert_se(boolean); |
| 503 |
assert_se(streq(x, "aaa")); |
| 504 |
assert_se(streq(y, "1")); |
| 505 |
assert_se(streq(a, "bbb")); |
| 506 |
assert_se(streq(b, "2")); |
| 507 |
assert_se(streq(c, "ccc")); |
| 508 |
assert_se(streq(d, "3")); |
| 509 |
|
| 510 |
assert_se(sd_bus_message_verify_type(m, 'a', "s") > 0); |
| 511 |
|
| 512 |
r = sd_bus_message_read(m, "as", 2, &x, &y); |
| 513 |
assert_se(r > 0); |
| 514 |
assert_se(streq(x, "foobar")); |
| 515 |
assert_se(streq(y, "waldo")); |
| 516 |
|
| 517 |
r = sd_bus_message_read_basic(m, 's', &s); |
| 518 |
assert_se(r > 0); |
| 519 |
assert_se(streq(s, "hallo")); |
| 520 |
|
| 521 |
r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz); |
| 522 |
assert_se(r > 0); |
| 523 |
assert_se(sz == sizeof(integer_array)); |
| 524 |
assert_se(memcmp(integer_array, return_array, sz) == 0); |
| 525 |
|
| 526 |
r = sd_bus_message_read_array(m, 'u', (const void**) &return_array, &sz); |
| 527 |
assert_se(r > 0); |
| 528 |
assert_se(sz == 0); |
| 529 |
|
| 530 |
r = sd_bus_message_read(m, "a(stdo)", 1, &x, &u64, &dbl, &y); |
| 531 |
assert_se(r > 0); |
| 532 |
assert_se(streq(x, "foo")); |
| 533 |
assert_se(u64 == 815ULL); |
| 534 |
assert_se(ABS(dbl - 47.0) < 0.1); |
| 535 |
assert_se(streq(y, "/")); |
| 536 |
|
| 537 |
r = sd_bus_message_peek_type(m, NULL, NULL); |
| 538 |
assert_se(r == 0); |
| 539 |
|
| 540 |
r = sd_bus_message_new_method_call(bus, ©, "foobar.waldo", "/", "foobar.waldo", "Piep"); |
| 541 |
assert_se(r >= 0); |
| 542 |
|
| 543 |
r = sd_bus_message_rewind(m, true); |
| 544 |
assert_se(r >= 0); |
| 545 |
|
| 546 |
r = sd_bus_message_copy(copy, m, true); |
| 547 |
assert_se(r >= 0); |
| 548 |
|
| 549 |
r = sd_bus_message_seal(copy, 4712, 0); |
| 550 |
assert_se(r >= 0); |
| 551 |
|
| 552 |
assert_se(mf = memstream_init(&ms)); |
| 553 |
sd_bus_message_dump(copy, mf, 0); |
| 554 |
assert_se(memstream_finalize(&ms, &third, &third_size) >= 0); |
| 555 |
|
| 556 |
printf("<%.*s>\n", (int) first_size, first); |
| 557 |
printf("<%.*s>\n", (int) third_size, third); |
| 558 |
|
| 559 |
assert_se(first_size == third_size); |
| 560 |
assert_se(memcmp(first, third, third_size) == 0); |
| 561 |
|
| 562 |
r = sd_bus_message_rewind(m, true); |
| 563 |
assert_se(r >= 0); |
| 564 |
|
| 565 |
assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0); |
| 566 |
|
| 567 |
r = sd_bus_message_skip(m, "ssasg"); |
| 568 |
assert_se(r > 0); |
| 569 |
|
| 570 |
assert_se(sd_bus_message_verify_type(m, 's', NULL) > 0); |
| 571 |
|
| 572 |
r = sd_bus_message_skip(m, "sass"); |
| 573 |
assert_se(r >= 0); |
| 574 |
|
| 575 |
assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0); |
| 576 |
|
| 577 |
r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y"); |
| 578 |
assert_se(r >= 0); |
| 579 |
|
| 580 |
assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0); |
| 581 |
|
| 582 |
r = sd_bus_message_read(m, "b", &boolean); |
| 583 |
assert_se(r > 0); |
| 584 |
assert_se(boolean); |
| 585 |
|
| 586 |
r = sd_bus_message_enter_container(m, 0, NULL); |
| 587 |
assert_se(r > 0); |
| 588 |
|
| 589 |
r = sd_bus_message_read(m, "(ss)", &x, &y); |
| 590 |
assert_se(r > 0); |
| 591 |
|
| 592 |
r = sd_bus_message_read(m, "(ss)", &a, &b); |
| 593 |
assert_se(r > 0); |
| 594 |
|
| 595 |
r = sd_bus_message_read(m, "(ss)", &c, &d); |
| 596 |
assert_se(r > 0); |
| 597 |
|
| 598 |
r = sd_bus_message_read(m, "(ss)", &x, &y); |
| 599 |
assert_se(r == 0); |
| 600 |
|
| 601 |
r = sd_bus_message_exit_container(m); |
| 602 |
assert_se(r >= 0); |
| 603 |
|
| 604 |
assert_se(streq(x, "aaa")); |
| 605 |
assert_se(streq(y, "1")); |
| 606 |
assert_se(streq(a, "bbb")); |
| 607 |
assert_se(streq(b, "2")); |
| 608 |
assert_se(streq(c, "ccc")); |
| 609 |
assert_se(streq(d, "3")); |
| 610 |
|
| 611 |
test_bus_label_escape(); |
| 612 |
test_bus_path_encode(); |
| 613 |
test_bus_path_encode_unique(); |
| 614 |
test_bus_path_encode_many(); |
| 615 |
test_bus_fds_truncated(); |
| 616 |
test_bus_nested_variant_depth_limit(); |
| 617 |
|
| 618 |
return 0; |
| 619 |
} |