]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
test: Use DBUS_TEST_SOCKET_DIR to create the test socket
authorAlex Richardson <arichardson@FreeBSD.org>
Fri, 12 Aug 2022 10:15:14 +0000 (10:15 +0000)
committerAlex Richardson <arichardson@FreeBSD.org>
Thu, 15 Sep 2022 18:58:20 +0000 (18:58 +0000)
I am trying to run cross-compiled tests in QEMU with the build directory
mounted via smbfs, and therefore creating the sockets in the CWD does not
work. Using DBUS_TEST_SOCKET_DIR (/tmp by default) allows me to run the
tests successfully.

test/internals/misc-internals.c

index a1777bb2aee9aaf4d4e443c81549108af0352b12..e8abbb576bb9267be09d79c91e1fd4d7d714e91c 100644 (file)
@@ -35,6 +35,7 @@
 
 #ifdef DBUS_UNIX
 #include "dbus/dbus-userdb.h"
+#include <unistd.h>
 #endif
 
 #include "misc-internals.h"
@@ -671,55 +672,96 @@ _dbus_misc_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
   return TRUE;
 }
 
-static dbus_bool_t
-_dbus_server_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
+_DBUS_GNUC_PRINTF (1, 2) static void
+start_test_dbus_server (const char *fmt, ...)
 {
-  const char *valid_addresses[] = {
-    "tcp:port=1234",
-    "tcp:host=localhost,port=1234",
-    "tcp:host=localhost,port=1234;tcp:port=5678",
-#ifdef DBUS_UNIX
-    "unix:path=./boogie",
-    "tcp:port=1234;unix:path=./boogie",
-#endif
-  };
-
   DBusServer *server;
-  int i;
 
-  for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
+  DBusError error = DBUS_ERROR_INIT;
+  DBusString listen_address = _DBUS_STRING_INIT_INVALID;
+  char *address;
+  char *id;
+  va_list ap;
+
+  if (!_dbus_string_init (&listen_address))
+    _dbus_test_fatal ("Out of memory");
+
+  va_start (ap, fmt);
+  if (!_dbus_string_append_printf_valist (&listen_address, fmt, ap))
+    _dbus_test_fatal ("Out of memory");
+  va_end (ap);
+
+  _dbus_test_diag ("checking server address '%s'",
+                   _dbus_string_get_const_data (&listen_address));
+  server = dbus_server_listen (_dbus_string_get_const_data (&listen_address),
+                               &error);
+  if (server == NULL)
     {
-      DBusError error = DBUS_ERROR_INIT;
-      char *address;
-      char *id;
+      _dbus_warn ("server listen error: %s: %s", error.name, error.message);
+      dbus_error_free (&error);
+      _dbus_test_fatal ("Failed to listen for valid address '%s'.",
+                        _dbus_string_get_const_data (&listen_address));
+    }
 
-      server = dbus_server_listen (valid_addresses[i], &error);
-      if (server == NULL)
-        {
-          _dbus_warn ("server listen error: %s: %s", error.name, error.message);
-          dbus_error_free (&error);
-          _dbus_test_fatal ("Failed to listen for valid address.");
-        }
+  id = dbus_server_get_id (server);
+  _dbus_test_check (id != NULL);
+  address = dbus_server_get_address (server);
+  _dbus_test_check (address != NULL);
 
-      id = dbus_server_get_id (server);
-      _dbus_test_check (id != NULL);
-      address = dbus_server_get_address (server);
-      _dbus_test_check (address != NULL);
+  if (strstr (address, id) == NULL)
+    {
+      _dbus_warn ("server id '%s' is not in the server address '%s'", id,
+                  address);
+      _dbus_test_fatal ("bad server id or address");
+    }
 
-      if (strstr (address, id) == NULL)
-        {
-          _dbus_warn ("server id '%s' is not in the server address '%s'",
-                      id, address);
-          _dbus_test_fatal ("bad server id or address");
-        }
+  dbus_free (id);
+  dbus_free (address);
 
-      dbus_free (id);
-      dbus_free (address);
+  dbus_server_disconnect (server);
+  dbus_server_unref (server);
+  _dbus_string_free (&listen_address);
+}
 
-      dbus_server_disconnect (server);
-      dbus_server_unref (server);
-    }
+#ifdef DBUS_UNIX
+static char test_socket_dir[] = DBUS_TEST_SOCKET_DIR "/dbus-test-XXXXXX";
+static void
+cleanup_test_socket_tempdir (void)
+{
+  if (rmdir (test_socket_dir) != 0)
+    _dbus_test_not_ok ("failed to remove test socket directory %s",
+                       test_socket_dir);
+  else
+    _dbus_test_diag ("removed test socket directory %s", test_socket_dir);
+}
+#endif
 
+static dbus_bool_t
+_dbus_server_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
+{
+  start_test_dbus_server ("tcp:port=1234");
+  start_test_dbus_server ("tcp:host=localhost,port=1234");
+  start_test_dbus_server ("tcp:host=localhost,port=1234;tcp:port=5678");
+  start_test_dbus_server ("tcp:port=1234");
+#ifdef DBUS_UNIX
+  /* Create a unique temporary directory for socket paths. */
+  if (mkdtemp (test_socket_dir) == NULL)
+    _dbus_test_fatal ("Failed to create temporary dir from template '%s'.",
+                      test_socket_dir);
+  /* Clean up the test socket directory using atexit() since the test function
+   * can call _dbus_test_fatal() which terminates the test program. */
+  atexit (cleanup_test_socket_tempdir);
+
+  /* Check that both absolute and relative paths work. */
+  start_test_dbus_server ("unix:path=%s/boogie", test_socket_dir);
+  start_test_dbus_server ("tcp:port=1234;unix:path=%s/boogie",
+                          test_socket_dir);
+
+  if (chdir (test_socket_dir) != 0)
+    _dbus_test_fatal ("Failed to chdir() to %s.", test_socket_dir);
+  start_test_dbus_server ("unix:path=./boogie");
+  start_test_dbus_server ("tcp:port=1234;unix:path=./boogie");
+#endif
   return TRUE;
 }