[PATCH 2/2] unit: add testing for dbus-over-tcp
Ram Subramanian <[email protected]> Mon, 13 May 2024 10:58:26 -0700
| Newsgroups | dev.linux.lists.ell |
|---|---|
| Message-ID | <[email protected]> |
Co-authored-by: Ramon Ribeiro <[email protected]> --- unit/dbus.conf | 2 + unit/test-dbus.c | 118 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 35 deletions(-) diff --git a/unit/dbus.conf b/unit/dbus.conf index 97e1bb5..5fe41a9 100644 --- a/unit/dbus.conf +++ b/unit/dbus.conf @@ -7,6 +7,8 @@ <allow_anonymous /> <listen>unix:path=/tmp/ell-test-bus</listen> + <listen>tcp:host=localhost,bind=*,port=14046,family=ipv4</listen> + <apparmor mode="disabled" /> <policy context="default"> <!-- Allow everything to be sent --> diff --git a/unit/test-dbus.c b/unit/test-dbus.c index 5b98385..9006d0e 100644 --- a/unit/test-dbus.c +++ b/unit/test-dbus.c @@ -20,11 +20,17 @@ #define WAIT_ANY (-1) /* Any process */ #endif -#define TEST_BUS_ADDRESS "unix:path=/tmp/ell-test-bus" +#define TEST_BUS_ADDRESS_UNIX "unix:path=/tmp/ell-test-bus" +#define TEST_BUS_ADDRESS_TCP "tcp:host=127.0.0.1,port=14046" static pid_t dbus_daemon_pid = -1; -static void start_dbus_daemon(void) +static int tests_completed = 0; +static bool bus_became_ready = false; +static bool match_cb_called = false; +static bool req_name_cb_called = false; + +static bool start_dbus_daemon(void) { char *prg_argv[5]; char *prg_envp[1]; @@ -43,7 +49,7 @@ static void start_dbus_daemon(void) pid = fork(); if (pid < 0) { l_error("failed to fork new process"); - return; + return false; } if (pid == 0) { @@ -54,6 +60,8 @@ static void start_dbus_daemon(void) l_info("dbus-daemon process %d created", pid); dbus_daemon_pid = pid; + + return true; } static void signal_handler(uint32_t signo, void *user_data) @@ -93,6 +101,17 @@ static void do_debug(const char *str, void *user_data) l_info("%s%s", prefix, str); } +#define test_assert(cond) \ + do { \ + if (!(cond)) { \ + l_info("TEST FAILED in %s at %s:%i: %s", \ + __func__, __FILE__, __LINE__, \ + L_STRINGIFY(cond)); \ + l_main_quit(); \ + return; \ + } \ + } while (0) + static void signal_message(struct l_dbus_message *message, void *user_data) { const char *path, *interface, *member, *destination, *sender; @@ -135,18 +154,19 @@ static void request_name_callback(struct l_dbus_message *message, const char *error, *text; uint32_t result; + req_name_cb_called = true; + if (l_dbus_message_get_error(message, &error, &text)) { l_error("error=%s", error); l_error("message=%s", text); - goto done; + test_assert(false); } - if (!l_dbus_message_get_arguments(message, "u", &result)) - goto done; + test_assert(l_dbus_message_get_arguments(message, "u", &result)); l_info("request name result=%d", result); -done: + tests_completed ++; l_main_quit(); } @@ -161,21 +181,41 @@ static void add_match_callback(struct l_dbus_message *message, void *user_data) { const char *error, *text; + match_cb_called = true; + if (l_dbus_message_get_error(message, &error, &text)) { l_error("error=%s", error); l_error("message=%s", text); + test_assert(false); return; } - if (!l_dbus_message_get_arguments(message, "")) - return; + test_assert(l_dbus_message_get_arguments(message, "")); l_info("add match"); } static void ready_callback(void *user_data) { + struct l_dbus *dbus = user_data; + int rc; + l_info("ready"); + bus_became_ready = true; + + rc = l_dbus_method_call(dbus, "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", "AddMatch", + add_match_setup, + add_match_callback, NULL, NULL); + test_assert(rc > 0); + + rc = l_dbus_method_call(dbus, "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", "RequestName", + request_name_setup, + request_name_callback, NULL, NULL); + test_assert(rc > 0); } static void disconnect_callback(void *user_data) @@ -183,31 +223,29 @@ static void disconnect_callback(void *user_data) l_main_quit(); } -int main(int argc, char *argv[]) +static void test_dbus(const void *data) { + const char *address = data; struct l_dbus *dbus; - struct l_signal *sigchld; int i; - if (!l_main_init()) - return -1; + bus_became_ready = false; + match_cb_called = false; + req_name_cb_called = false; + + test_assert(l_main_init()); l_log_set_stderr(); - start_dbus_daemon(); - for (i = 0; i < 10; i++) { usleep(200 * 1000); - dbus = l_dbus_new(TEST_BUS_ADDRESS); + dbus = l_dbus_new(address); if (dbus) break; } - sigchld = l_signal_create(SIGCHLD, sigchld_handler, NULL, NULL); - - if (!dbus) - goto done; + test_assert(dbus); l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL); @@ -216,29 +254,39 @@ int main(int argc, char *argv[]) l_dbus_register(dbus, signal_message, NULL, NULL); - l_dbus_method_call(dbus, "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", "AddMatch", - add_match_setup, - add_match_callback, NULL, NULL); - - l_dbus_method_call(dbus, "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", "RequestName", - request_name_setup, - request_name_callback, NULL, NULL); - l_main_run_with_signal(signal_handler, NULL); + test_assert(bus_became_ready); + test_assert(match_cb_called); + test_assert(req_name_cb_called); + l_dbus_destroy(dbus); + l_main_exit(); +} + +int main(int argc, char *argv[]) +{ + struct l_signal *sigchld; + + l_test_init(&argc, &argv); + + l_test_add("Using a unix socket", test_dbus, TEST_BUS_ADDRESS_UNIX); + l_test_add("Using a tcp socket", test_dbus, TEST_BUS_ADDRESS_TCP); + + sigchld = l_signal_create(SIGCHLD, sigchld_handler, NULL, NULL); + + if (!start_dbus_daemon()) + return -1; + + l_test_run(); -done: if (dbus_daemon_pid > 0) kill(dbus_daemon_pid, SIGKILL); l_signal_remove(sigchld); - l_main_exit(); + if (tests_completed == 2) + return 0; - return 0; + return -1; } -- 2.43.2 -- *Confidentiality Note:* We care about protecting our proprietary information, confidential material, and trade secrets. This message may contain some or all of those things. Cruise will suffer material harm if anyone other than the intended recipient disseminates or takes any action based on this message. If you have received this message (including any attachments) in error, please delete it immediately and notify the sender promptly.