[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / test / loopback.c
index d0d69c8..7526d8d 100644 (file)
@@ -1,7 +1,7 @@
 /* Simple sanity-check for loopback through TCP and Unix sockets.
  *
  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
- * Copyright © 2010-2011 Nokia Corporation
+ * Copyright © 2010-2012 Nokia Corporation
  *
  * Permission is hereby granted, free of charge, to any person
  * obtaining a copy of this software and associated documentation files
 #include <glib.h>
 
 #include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
+
+#include <string.h>
+
+#include "test-utils.h"
 
 typedef struct {
+    TestMainContext *ctx;
     DBusError e;
 
     DBusServer *server;
@@ -72,7 +76,7 @@ new_conn_cb (DBusServer *server,
 
   g_assert (f->server_conn == NULL);
   f->server_conn = dbus_connection_ref (server_conn);
-  dbus_connection_setup_with_g_main (server_conn, NULL);
+  test_connection_setup (f->ctx, server_conn);
 
   have_mem = dbus_connection_add_filter (server_conn,
       server_message_cb, f, NULL);
@@ -83,6 +87,7 @@ static void
 setup (Fixture *f,
     gconstpointer addr)
 {
+  f->ctx = test_main_context_get ();
   dbus_error_init (&f->e);
   g_queue_init (&f->server_messages);
 
@@ -92,7 +97,7 @@ setup (Fixture *f,
 
   dbus_server_set_new_connection_function (f->server,
       new_conn_cb, f, NULL);
-  dbus_server_setup_with_g_main (f->server, NULL);
+  test_server_setup (f->ctx, f->server);
 }
 
 static void
@@ -105,13 +110,73 @@ test_connect (Fixture *f,
       dbus_server_get_address (f->server), &f->e);
   assert_no_error (&f->e);
   g_assert (f->client_conn != NULL);
-  dbus_connection_setup_with_g_main (f->client_conn, NULL);
+  test_connection_setup (f->ctx, f->client_conn);
 
   while (f->server_conn == NULL)
     {
       g_print (".");
-      g_main_context_iteration (NULL, TRUE);
+      test_main_context_iterate (f->ctx, TRUE);
+    }
+}
+
+static void
+test_bad_guid (Fixture *f,
+    gconstpointer addr G_GNUC_UNUSED)
+{
+  DBusMessage *incoming;
+  gchar *address = g_strdup (dbus_server_get_address (f->server));
+  gchar *guid;
+
+  g_test_bug ("39720");
+
+  g_assert (f->server_conn == NULL);
+
+  g_assert (strstr (address, "guid=") != NULL);
+  guid = strstr (address, "guid=");
+  g_assert_cmpuint (strlen (guid), >=, 5 + 32);
+
+  /* Change the first char of the guid to something different */
+  if (guid[5] == '0')
+    guid[5] = 'f';
+  else
+    guid[5] = '0';
+
+  f->client_conn = dbus_connection_open_private (address, &f->e);
+  assert_no_error (&f->e);
+  g_assert (f->client_conn != NULL);
+  test_connection_setup (f->ctx, f->client_conn);
+
+  while (f->server_conn == NULL)
+    {
+      g_print (".");
+      test_main_context_iterate (f->ctx, TRUE);
+    }
+
+  /* We get disconnected */
+
+  while (g_queue_is_empty (&f->server_messages))
+    {
+      g_print (".");
+      test_main_context_iterate (f->ctx, TRUE);
     }
+
+  g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
+
+  incoming = g_queue_pop_head (&f->server_messages);
+
+  g_assert (!dbus_message_contains_unix_fds (incoming));
+  g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
+  g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
+  g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
+      DBUS_INTERFACE_LOCAL);
+  g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
+  g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
+  g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
+  g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
+
+  dbus_message_unref (incoming);
+
+  g_free (address);
 }
 
 static void
@@ -135,7 +200,7 @@ test_message (Fixture *f,
   while (g_queue_is_empty (&f->server_messages))
     {
       g_print (".");
-      g_main_context_iteration (NULL, TRUE);
+      test_main_context_iterate (f->ctx, TRUE);
     }
 
   g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
@@ -182,6 +247,8 @@ teardown (Fixture *f,
       dbus_server_unref (f->server);
       f->server = NULL;
     }
+
+  test_main_context_unref (f->ctx);
 }
 
 int
@@ -189,6 +256,7 @@ main (int argc,
     char **argv)
 {
   g_test_init (&argc, &argv, NULL);
+  g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
 
   g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
       test_connect, teardown);
@@ -207,5 +275,8 @@ main (int argc,
       test_message, teardown);
 #endif
 
+  g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup,
+      test_bad_guid, teardown);
+
   return g_test_run ();
 }