Skip to content

Commit d603324

Browse files
committed
test-bus-{vtable,introspect}: share data and test introspect_path()
test-bus-introspect is also applied to the tables from test-bus-vtable.c. test-bus-vtable.c is also used as C++ sources to produce test-bus-vtable-cc, and our hashmap headers are not C++ compatible. So let's do the introspection part only in the C version.
1 parent dff9e25 commit d603324

File tree

6 files changed

+178
-142
lines changed

6 files changed

+178
-142
lines changed

src/libsystemd/sd-bus/bus-objects.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ int introspect_path(
888888
const char *path,
889889
struct node *n,
890890
bool require_fallback,
891+
bool ignore_nodes_modified,
891892
bool *found_object,
892893
char **ret,
893894
sd_bus_error *error) {
@@ -899,10 +900,16 @@ int introspect_path(
899900
bool empty;
900901
int r;
901902

903+
if (!n) {
904+
n = hashmap_get(bus->nodes, path);
905+
if (!n)
906+
return -ENOENT;
907+
}
908+
902909
r = get_child_nodes(bus, path, n, 0, &s, error);
903910
if (r < 0)
904911
return r;
905-
if (bus->nodes_modified)
912+
if (bus->nodes_modified && !ignore_nodes_modified)
906913
return 0;
907914

908915
r = introspect_begin(&intro, bus->trusted);
@@ -922,7 +929,7 @@ int introspect_path(
922929
r = node_vtable_get_userdata(bus, path, c, NULL, error);
923930
if (r < 0)
924931
return r;
925-
if (bus->nodes_modified)
932+
if (bus->nodes_modified && !ignore_nodes_modified)
926933
return 0;
927934
if (r == 0)
928935
continue;
@@ -955,11 +962,12 @@ int introspect_path(
955962
r = bus_node_exists(bus, n, path, require_fallback);
956963
if (r <= 0)
957964
return r;
958-
if (bus->nodes_modified)
965+
if (bus->nodes_modified && !ignore_nodes_modified)
959966
return 0;
960967
}
961968

962-
*found_object = true;
969+
if (found_object)
970+
*found_object = true;
963971

964972
r = introspect_write_child_nodes(&intro, s, path);
965973
if (r < 0)
@@ -989,7 +997,7 @@ static int process_introspect(
989997
assert(n);
990998
assert(found_object);
991999

992-
r = introspect_path(bus, m->path, n, require_fallback, found_object, &s, &error);
1000+
r = introspect_path(bus, m->path, n, require_fallback, false, found_object, &s, &error);
9931001
if (r < 0)
9941002
return bus_maybe_reply_error(m, r, &error);
9951003
if (r == 0)

src/libsystemd/sd-bus/bus-objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int introspect_path(
1414
const char *path,
1515
struct node *n,
1616
bool require_fallback,
17+
bool ignore_nodes_modified,
1718
bool *found_object,
1819
char **ret,
1920
sd_bus_error *error);

src/libsystemd/sd-bus/test-bus-introspect.c

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@
44
#include "log.h"
55
#include "tests.h"
66

7-
static int prop_get(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
8-
return -EINVAL;
9-
}
10-
11-
static int prop_set(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
12-
return -EINVAL;
13-
}
7+
#include "test-vtable-data.h"
148

15-
static const sd_bus_vtable vtable[] = {
16-
SD_BUS_VTABLE_START(0),
17-
SD_BUS_METHOD("Hello", "ssas", "a(uu)", NULL, 0),
18-
SD_BUS_METHOD("DeprecatedHello", "", "", NULL, SD_BUS_VTABLE_DEPRECATED),
19-
SD_BUS_METHOD("DeprecatedHelloNoReply", "", "", NULL, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_METHOD_NO_REPLY),
20-
SD_BUS_SIGNAL("Wowza", "sss", 0),
21-
SD_BUS_SIGNAL("DeprecatedWowza", "ut", SD_BUS_VTABLE_DEPRECATED),
22-
SD_BUS_WRITABLE_PROPERTY("AProperty", "s", prop_get, prop_set, 0, 0),
23-
SD_BUS_PROPERTY("AReadOnlyDeprecatedProperty", "(ut)", prop_get, 0, SD_BUS_VTABLE_DEPRECATED),
24-
SD_BUS_PROPERTY("ChangingProperty", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
25-
SD_BUS_PROPERTY("Invalidating", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
26-
SD_BUS_PROPERTY("Constant", "t", prop_get, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EXPLICIT),
27-
SD_BUS_VTABLE_END
28-
};
29-
30-
static void test_manual_introspection(void) {
9+
static void test_manual_introspection(const sd_bus_vtable vtable[]) {
3110
struct introspect intro = {};
3211
_cleanup_free_ char *s = NULL;
3312

13+
log_info("/* %s */", __func__);
14+
3415
assert_se(introspect_begin(&intro, false) >= 0);
3516

3617
fprintf(intro.f, " <interface name=\"org.foo\">\n");
@@ -39,12 +20,16 @@ static void test_manual_introspection(void) {
3920

4021
assert_se(introspect_finish(&intro, &s) == 0);
4122
fputs(s, stdout);
23+
fputs("\n", stdout);
4224
}
4325

4426
int main(int argc, char *argv[]) {
4527
test_setup_logging(LOG_DEBUG);
4628

47-
test_manual_introspection();
29+
test_manual_introspection(test_vtable_1);
30+
test_manual_introspection(test_vtable_2);
31+
test_manual_introspection(test_vtable_deprecated);
32+
test_manual_introspection((const sd_bus_vtable *) vtable_format_221);
4833

4934
return 0;
5035
}

src/libsystemd/sd-bus/test-bus-vtable.c

Lines changed: 19 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,23 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
13
#include <stdbool.h>
24
#include <stddef.h>
35

46
/* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */
57
#undef NDEBUG
68
#include <assert.h>
79
#include <errno.h>
10+
#include <stdio.h>
811

912
#include "sd-bus-vtable.h"
1013

11-
#define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
12-
13-
struct context {
14-
bool quit;
15-
char *something;
16-
char *automatic_string_property;
17-
uint32_t automatic_integer_property;
18-
};
19-
20-
static int handler(sd_bus_message *m, void *userdata, sd_bus_error *error) {
21-
return 1;
22-
}
23-
24-
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 *error) {
25-
return 1;
26-
}
14+
#ifndef __cplusplus
15+
# include "bus-objects.h"
16+
#endif
2717

28-
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 *error) {
29-
return 1;
30-
}
18+
#include "test-vtable-data.h"
3119

32-
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 *error) {
33-
return 1;
34-
}
35-
36-
static const sd_bus_vtable vtable[] = {
37-
SD_BUS_VTABLE_START(0),
38-
SD_BUS_METHOD("AlterSomething", "s", "s", handler, 0),
39-
SD_BUS_METHOD("Exit", "", "", handler, 0),
40-
SD_BUS_METHOD_WITH_OFFSET("AlterSomething2", "s", "s", handler, 200, 0),
41-
SD_BUS_METHOD_WITH_OFFSET("Exit2", "", "", handler, 200, 0),
42-
SD_BUS_METHOD_WITH_NAMES_OFFSET("AlterSomething3", "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path),
43-
"s", SD_BUS_PARAM(returnstring), handler, 200, 0),
44-
SD_BUS_METHOD_WITH_NAMES("Exit3", "bx", SD_BUS_PARAM(with_confirmation) SD_BUS_PARAM(after_msec),
45-
"bb", SD_BUS_PARAM(accepted) SD_BUS_PARAM(scheduled), handler, 0),
46-
SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
47-
SD_BUS_PROPERTY("Value2", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
48-
SD_BUS_PROPERTY("Value3", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_CONST),
49-
SD_BUS_PROPERTY("Value4", "s", value_handler, 10, 0),
50-
SD_BUS_PROPERTY("AnExplicitProperty", "s", NULL, offsetof(struct context, something),
51-
SD_BUS_VTABLE_PROPERTY_EXPLICIT|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
52-
SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0),
53-
SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL,
54-
offsetof(struct context, automatic_string_property), 0),
55-
SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL,
56-
offsetof(struct context, automatic_integer_property), 0),
57-
SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0),
58-
SD_BUS_SIGNAL("DummySignal", "b", 0),
59-
SD_BUS_SIGNAL("DummySignal2", "so", 0),
60-
SD_BUS_SIGNAL_WITH_NAMES("DummySignal3", "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path), 0),
61-
SD_BUS_VTABLE_END
62-
};
63-
64-
struct sd_bus_vtable_221 {
65-
uint8_t type:8;
66-
uint64_t flags:56;
67-
union {
68-
struct {
69-
size_t element_size;
70-
} start;
71-
struct {
72-
const char *member;
73-
const char *signature;
74-
const char *result;
75-
sd_bus_message_handler_t handler;
76-
size_t offset;
77-
} method;
78-
struct {
79-
const char *member;
80-
const char *signature;
81-
} signal;
82-
struct {
83-
const char *member;
84-
const char *signature;
85-
sd_bus_property_get_t get;
86-
sd_bus_property_set_t set;
87-
size_t offset;
88-
} property;
89-
} x;
90-
};
91-
92-
static const struct sd_bus_vtable_221 vtable_format_221[] = {
93-
{
94-
.type = _SD_BUS_VTABLE_START,
95-
.flags = 0,
96-
.x = {
97-
.start = {
98-
.element_size = sizeof(struct sd_bus_vtable_221)
99-
},
100-
},
101-
},
102-
{
103-
.type = _SD_BUS_VTABLE_METHOD,
104-
.flags = 0,
105-
.x = {
106-
.method = {
107-
.member = "Exit",
108-
.signature = "",
109-
.result = "",
110-
.handler = handler,
111-
.offset = 0,
112-
},
113-
},
114-
},
115-
{
116-
.type = _SD_BUS_VTABLE_END,
117-
.flags = 0,
118-
.x = { { 0 } },
119-
}
120-
};
20+
#define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
12121

12222
static void test_vtable(void) {
12323
sd_bus *bus = NULL;
@@ -126,16 +26,24 @@ static void test_vtable(void) {
12626

12727
assert(sd_bus_new(&bus) >= 0);
12828

129-
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", vtable, &c) >= 0);
130-
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", vtable, &c) >= 0);
29+
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", test_vtable_2, &c) >= 0);
30+
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", test_vtable_2, &c) >= 0);
13131
/* the cast on the line below is needed to test with the old version of the table */
132-
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable221", (const sd_bus_vtable *)vtable_format_221, &c) >= 0);
32+
assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable221",
33+
(const sd_bus_vtable *)vtable_format_221, &c) >= 0);
13334

13435
assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0);
13536
r = sd_bus_start(bus);
13637
assert(r == 0 || /* success */
13738
r == -ENOENT /* dbus is inactive */ );
13839

40+
#ifndef __cplusplus
41+
_cleanup_free_ char *s = NULL;
42+
43+
assert_se(introspect_path(bus, "/foo", NULL, false, true, NULL, &s, NULL) == 1);
44+
fputs(s, stdout);
45+
#endif
46+
13947
sd_bus_unref(bus);
14048
}
14149

0 commit comments

Comments
 (0)