Fix linking of libdbus-testutils.la with Automake targeting Windows
[platform/upstream/dbus.git] / test / dbus-daemon.c
index 04955e0..cc87153 100644 (file)
@@ -1,7 +1,7 @@
 /* Integration tests for the dbus-daemon
  *
  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
- * Copyright © 2010 Nokia Corporation
+ * Copyright © 2010-2011 Nokia Corporation
  *
  * Permission is hereby granted, free of charge, to any person
  * obtaining a copy of this software and associated documentation files
@@ -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;
@@ -82,7 +85,7 @@ spawn_dbus_daemon (gchar *binary,
   g_spawn_async_with_pipes (NULL, /* working directory */
       argv,
       NULL, /* envp */
-      G_SPAWN_DO_NOT_REAP_CHILD,
+      G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
       NULL, /* child_setup */
       NULL, /* user data */
       daemon_pid,
@@ -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,37 +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);
 
-  g_assert (g_getenv ("DBUS_TEST_DAEMON") != NULL);
-
-  dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_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_USE_INSTALLED") != 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 ("--session");
+      arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
+          g_getenv ("DBUS_TEST_SYSCONFDIR"));
     }
-  else
+  else if (g_getenv ("DBUS_TEST_DATA") != NULL)
     {
-      g_assert (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
+    {
+      arg = g_strdup ("--session");
+    }
+
+  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, config, &f->daemon_pid);
+  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);
@@ -224,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 ();
@@ -248,8 +285,9 @@ test_echo (Fixture *f,
       if (m == NULL)
         g_error ("OOM");
 
-      if (!dbus_connection_send_with_reply (f->left_conn, m, &pc, 0x7FFFFFFF)
-          || pc == NULL)
+      if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
+                                            DBUS_TIMEOUT_INFINITE) ||
+          pc == NULL)
         g_error ("OOM");
 
       if (dbus_pending_call_get_completed (pc))
@@ -298,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)
@@ -315,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 ();
 }