diff mbox series

[2/2] unit: add testing for dbus-over-tcp

Message ID 20240513175826.2363352-2-ram.subramanian@getcruise.com (mailing list archive)
State New
Headers show
Series [1/2] dbus: add dbus-over-tcp support | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success

Commit Message

Ram Subramanian May 13, 2024, 5:58 p.m. UTC
Co-authored-by: Ramon Ribeiro <rhpr@cesar.org.br>
---
 unit/dbus.conf   |   2 +
 unit/test-dbus.c | 118 +++++++++++++++++++++++++++++++++--------------
 2 files changed, 85 insertions(+), 35 deletions(-)

Comments

Denis Kenzior May 16, 2024, 7:36 p.m. UTC | #1
On 5/13/24 12:58 PM, Ram Subramanian wrote:
> Co-authored-by: Ramon Ribeiro <rhpr@cesar.org.br>

Since the unit test was all yours, I removed this line.

> ---
>   unit/dbus.conf   |   2 +
>   unit/test-dbus.c | 118 +++++++++++++++++++++++++++++++++--------------
>   2 files changed, 85 insertions(+), 35 deletions(-)
> 

<snip>

> @@ -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 ++;

I moved this to test_dbus() and fixed up the style (no spaces before ++).

Applied, thanks.

Regards,
-Denis
diff mbox series

Patch

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;
 }