Fix linking of libdbus-testutils.la with Automake targeting Windows
[platform/upstream/dbus.git] / test / dbus-daemon.c
index ed10d09..cc87153 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 
 #ifdef DBUS_WIN
+# include <io.h>
 # include <windows.h>
 #else
 # include <signal.h>
 #endif
 
 typedef struct {
+    gboolean skip;
+
     DBusError e;
     GError *ge;
 
-    gint daemon_pid;
+    GPid daemon_pid;
 
     DBusConnection *left_conn;
 
@@ -66,7 +69,7 @@ _assert_no_error (const DBusError *e,
 static gchar *
 spawn_dbus_daemon (gchar *binary,
     gchar *configuration,
-    gint *daemon_pid)
+    GPid *daemon_pid)
 {
   GError *error = NULL;
   GString *address;
@@ -148,8 +151,6 @@ echo_filter (DBusConnection *connection,
     void *user_data)
 {
   DBusMessage *reply;
-  DBusError error = DBUS_ERROR_INIT;
-  int *sleep_ms = user_data;
 
   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
@@ -167,42 +168,63 @@ echo_filter (DBusConnection *connection,
   return DBUS_HANDLER_RESULT_HANDLED;
 }
 
+typedef struct {
+    const char *bug_ref;
+    guint min_messages;
+    const char *config_file;
+} Config;
+
 static void
 setup (Fixture *f,
-    gconstpointer context G_GNUC_UNUSED)
+    gconstpointer context)
 {
+  const Config *config = context;
   gchar *dbus_daemon;
-  gchar *config;
+  gchar *arg;
   gchar *address;
 
   f->ge = NULL;
   dbus_error_init (&f->e);
 
-  dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
-
-  if (dbus_daemon == NULL)
-    dbus_daemon = g_strdup ("dbus-daemon");
+  if (config != NULL && config->config_file != NULL)
+    {
+      if (g_getenv ("DBUS_TEST_DATA") == NULL)
+        {
+          g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
+              config->config_file);
+          f->skip = TRUE;
+          return;
+        }
 
-  if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
+      arg = g_strdup_printf (
+          "--config-file=%s/%s",
+          g_getenv ("DBUS_TEST_DATA"), config->config_file);
+    }
+  else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
     {
-      config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
+      arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
           g_getenv ("DBUS_TEST_SYSCONFDIR"));
     }
   else if (g_getenv ("DBUS_TEST_DATA") != NULL)
     {
-      config = g_strdup_printf (
+      arg = g_strdup_printf (
           "--config-file=%s/valid-config-files/session.conf",
           g_getenv ("DBUS_TEST_DATA"));
     }
   else
     {
-      config = g_strdup ("--session");
+      arg = g_strdup ("--session");
     }
 
-  address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
+  dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
+
+  if (dbus_daemon == NULL)
+    dbus_daemon = g_strdup ("dbus-daemon");
+
+  address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid);
 
   g_free (dbus_daemon);
-  g_free (config);
+  g_free (arg);
 
   f->left_conn = connect_to_bus (address);
   f->right_conn = connect_to_bus (address);
@@ -229,16 +251,26 @@ pc_count (DBusPendingCall *pc,
 
 static void
 test_echo (Fixture *f,
-    gconstpointer context G_GNUC_UNUSED)
+    gconstpointer context)
 {
+  const Config *config = context;
   guint count = 2000;
   guint sent;
   guint received = 0;
   double elapsed;
 
+  if (f->skip)
+    return;
+
+  if (config != NULL && config->bug_ref != NULL)
+    g_test_bug (config->bug_ref);
+
   if (g_test_perf ())
     count = 100000;
 
+  if (config != NULL)
+    count = MAX (config->min_messages, count);
+
   add_echo_filter (f);
 
   g_test_timer_start ();
@@ -304,15 +336,23 @@ teardown (Fixture *f,
       f->right_conn = NULL;
     }
 
+  if (f->daemon_pid != 0)
+    {
 #ifdef DBUS_WIN
-  TerminateProcess (f->daemon_pid, 1);
+      TerminateProcess (f->daemon_pid, 1);
 #else
-  kill (f->daemon_pid, SIGTERM);
+      kill (f->daemon_pid, SIGTERM);
 #endif
 
-  g_spawn_close_pid (f->daemon_pid);
+      g_spawn_close_pid (f->daemon_pid);
+      f->daemon_pid = 0;
+    }
 }
 
+static Config limited_config = {
+    "34393", 10000, "valid-config-files/incoming-limit.conf"
+};
+
 int
 main (int argc,
     char **argv)
@@ -321,6 +361,8 @@ main (int argc,
   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
 
   g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
+  g_test_add ("/echo/limited", Fixture, &limited_config,
+      setup, test_echo, teardown);
 
   return g_test_run ();
 }