All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ramon Ribeiro <rhpr@cesar.org.br>
To: ell@lists.01.org
Subject: [PATCH v1 2/2] unit: dbus: Add unit test to dbus tcp address
Date: Mon, 15 Apr 2019 09:23:27 -0300	[thread overview]
Message-ID: <1555331007-26977-3-git-send-email-rhpr@cesar.org.br> (raw)
In-Reply-To: <1555331007-26977-1-git-send-email-rhpr@cesar.org.br>

[-- Attachment #1: Type: text/plain, Size: 7902 bytes --]

---
 .gitignore           |   1 +
 Makefile.am          |   3 +
 unit/dbus.conf       |   2 +
 unit/test-dbus-tcp.c | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 267 insertions(+)
 create mode 100644 unit/test-dbus-tcp.c

diff --git a/.gitignore b/.gitignore
index 520c509..7d938f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,6 +34,7 @@ unit/test-netlink
 unit/test-genl
 unit/test-genl-msg
 unit/test-dbus
+unit/test-dbus-tcp
 unit/test-dbus-message
 unit/test-dbus-message-fds
 unit/test-dbus-util
diff --git a/Makefile.am b/Makefile.am
index 5bc13bd..28b8452 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -175,6 +175,7 @@ unit_tests = unit/test-unit \
 
 dbus_tests = unit/test-hwdb \
 			unit/test-dbus \
+			unit/test-dbus-tcp \
 			unit/test-dbus-util \
 			unit/test-dbus-message \
 			unit/test-dbus-message-fds \
@@ -243,6 +244,8 @@ unit_test_genl_msg_LDADD = ell/libell-private.la
 
 unit_test_dbus_LDADD = ell/libell-private.la
 
+unit_test_dbus_tcp_LDADD = ell/libell-private.la
+
 unit_test_dbus_message_LDADD = ell/libell-private.la
 
 unit_test_dbus_message_fds_LDADD = ell/libell-private.la
diff --git a/unit/dbus.conf b/unit/dbus.conf
index 97e1bb5..6b1fccd 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=1234,family=ipv4</listen>
+  <apparmor mode="disabled" />
 
   <policy context="default">
     <!-- Allow everything to be sent -->
diff --git a/unit/test-dbus-tcp.c b/unit/test-dbus-tcp.c
new file mode 100644
index 0000000..4e11ca9
--- /dev/null
+++ b/unit/test-dbus-tcp.c
@@ -0,0 +1,261 @@
+/*
+ *
+ *  Embedded Linux library
+ *
+ *  Copyright (C) 2011-2014  Intel Corporation. All rights reserved.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+
+#include <ell/ell.h>
+
+#ifndef WAIT_ANY
+#define WAIT_ANY (-1) /* Any process */
+#endif
+
+#define TEST_BUS_ADDRESS "tcp:host=localhost,port=1234"
+
+static pid_t dbus_daemon_pid = -1;
+
+static void start_dbus_daemon(void)
+{
+	char *prg_argv[6];
+	char *prg_envp[1];
+	pid_t pid;
+
+	prg_argv[0] = "/usr/bin/dbus-daemon";
+	prg_argv[1] = "--nopidfile";
+	prg_argv[2] = "--nofork";
+	prg_argv[3] = "--config-file=" UNITDIR "dbus.conf";
+	prg_argv[4] = "--print-address";
+	prg_argv[5] = NULL;
+
+	prg_envp[0] = NULL;
+
+	l_info("launching dbus-daemon");
+
+	pid = fork();
+	if (pid < 0) {
+		l_error("failed to fork new process");
+		return;
+	}
+
+	if (pid == 0) {
+		execve(prg_argv[0], prg_argv, prg_envp);
+		exit(EXIT_SUCCESS);
+	}
+
+	l_info("dbus-daemon process %d created", pid);
+
+	dbus_daemon_pid = pid;
+}
+
+static void signal_handler(uint32_t signo, void *user_data)
+{
+	switch (signo) {
+	case SIGINT:
+	case SIGTERM:
+		l_info("Terminate");
+		l_main_quit();
+		break;
+	}
+}
+
+static void sigchld_handler(void *user_data)
+{
+	while (1) {
+		pid_t pid;
+		int status;
+
+		pid = waitpid(WAIT_ANY, &status, WNOHANG);
+		if (pid < 0 || pid == 0)
+			break;
+
+		l_info("process %d terminated with status=%d\n", pid, status);
+
+		if (pid == dbus_daemon_pid) {
+			dbus_daemon_pid = -1;
+			l_main_quit();
+		}
+	}
+}
+
+static void do_debug(const char *str, void *user_data)
+{
+	const char *prefix = user_data;
+
+	l_info("%s%s", prefix, str);
+}
+
+static void signal_message(struct l_dbus_message *message, void *user_data)
+{
+	const char *path, *interface, *member, *destination, *sender;
+
+	path = l_dbus_message_get_path(message);
+	destination = l_dbus_message_get_destination(message);
+
+	l_info("path=%s destination=%s", path, destination);
+
+	interface = l_dbus_message_get_interface(message);
+	member = l_dbus_message_get_member(message);
+
+	l_info("interface=%s member=%s", interface, member);
+
+	sender = l_dbus_message_get_sender(message);
+
+	l_info("sender=%s", sender);
+
+	if (!strcmp(member, "NameOwnerChanged")) {
+		const char *name, *old_owner, *new_owner;
+
+		if (!l_dbus_message_get_arguments(message, "sss",
+					&name, &old_owner, &new_owner))
+			return;
+
+		l_info("name=%s old=%s new=%s", name, old_owner, new_owner);
+	}
+}
+
+static void request_name_setup(struct l_dbus_message *message, void *user_data)
+{
+	const char *name = "org.test";
+
+	l_dbus_message_set_arguments(message, "su", name, 0);
+}
+
+static void request_name_callback(struct l_dbus_message *message,
+							void *user_data)
+{
+	const char *error, *text;
+	uint32_t result;
+
+	if (l_dbus_message_get_error(message, &error, &text)) {
+		l_error("error=%s", error);
+		l_error("message=%s", text);
+		goto done;
+	}
+
+	if (!l_dbus_message_get_arguments(message, "u", &result))
+		goto done;
+
+	l_info("request name result=%d", result);
+
+done:
+	l_main_quit();
+}
+
+static const char *match_rule = "type=signal,sender=org.freedesktop.DBus";
+
+static void add_match_setup(struct l_dbus_message *message, void *user_data)
+{
+	l_dbus_message_set_arguments(message, "s", match_rule);
+}
+
+static void add_match_callback(struct l_dbus_message *message, void *user_data)
+{
+	const char *error, *text;
+
+	if (l_dbus_message_get_error(message, &error, &text)) {
+		l_error("error=%s", error);
+		l_error("message=%s", text);
+		return;
+	}
+
+	if (!l_dbus_message_get_arguments(message, ""))
+		return;
+
+	l_info("add match");
+}
+
+static void ready_callback(void *user_data)
+{
+	l_info("ready");
+}
+
+static void disconnect_callback(void *user_data)
+{
+	l_main_quit();
+}
+
+int main(int argc, char *argv[])
+{
+	struct l_dbus *dbus;
+	struct l_signal *sigchld;
+	int i;
+
+	if (!l_main_init())
+		return -1;
+
+	l_log_set_stderr();
+
+	start_dbus_daemon();
+
+	for (i = 0; i < 10; i++) {
+		usleep(200 * 1000);
+
+		dbus = l_dbus_new(TEST_BUS_ADDRESS);
+		if (dbus)
+			break;
+
+	}
+
+	sigchld = l_signal_create(SIGCHLD, sigchld_handler, NULL, NULL);
+
+	if (!dbus)
+		goto done;
+
+	l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL);
+
+	l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
+	l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);
+
+	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);
+
+	l_dbus_destroy(dbus);
+
+done:
+	if (dbus_daemon_pid > 0)
+		kill(dbus_daemon_pid, SIGKILL);
+
+	l_signal_remove(sigchld);
+
+	l_main_exit();
+
+	return 0;
+}
-- 
2.7.4


  parent reply	other threads:[~2019-04-15 12:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-31 14:00 [PATCH 0/2] Allow connection to a tcp dbus address Ramon Henrique
2019-03-31 14:00 ` [PATCH 1/2] dbus: Add support to tcp address Ramon Henrique
2019-04-09 16:44   ` Denis Kenzior
2019-03-31 14:00 ` [PATCH 2/2] unit: dbus: Add unit test to dbus " Ramon Henrique
2019-04-15 12:23 ` [PATCH v1 0/2] Allow connection to a tcp dbus address Ramon Ribeiro
2019-04-15 12:23   ` [PATCH v1 1/2] dbus: Add support to tcp address Ramon Ribeiro
2019-04-15 16:59     ` Denis Kenzior
2019-04-15 12:23   ` Ramon Ribeiro [this message]
2019-04-16 13:05   ` [PATCH v2 0/2] Allow connection to a tcp dbus address Ramon Ribeiro
2019-04-16 13:05     ` [PATCH v2 1/2] dbus: Add support to tcp address Ramon Ribeiro
2019-04-17 20:13       ` Denis Kenzior
2019-04-16 13:05     ` [PATCH v2 2/2] unit: dbus: Add unit test to dbus " Ramon Ribeiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1555331007-26977-3-git-send-email-rhpr@cesar.org.br \
    --to=rhpr@cesar.org.br \
    --cc=ell@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.