corrupt test: close connection before releasing GSocket
[platform/upstream/dbus.git] / test / loopback.c
index 0567edb..39cf03a 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 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
@@ -31,6 +31,8 @@
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
+#include <string.h>
+
 typedef struct {
     DBusError e;
 
@@ -115,6 +117,66 @@ test_connect (Fixture *f,
 }
 
 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);
+  dbus_connection_setup_with_g_main (f->client_conn, NULL);
+
+  while (f->server_conn == NULL)
+    {
+      g_print (".");
+      g_main_context_iteration (NULL, TRUE);
+    }
+
+  /* We get disconnected */
+
+  while (g_queue_is_empty (&f->server_messages))
+    {
+      g_print (".");
+      g_main_context_iteration (NULL, 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
 test_message (Fixture *f,
     gconstpointer addr)
 {
@@ -152,6 +214,10 @@ test_message (Fixture *f,
   g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
   g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
   g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
+
+  dbus_message_unref (incoming);
+
+  dbus_message_unref (outgoing);
 }
 
 static void
@@ -185,12 +251,18 @@ 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);
   g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
       test_message, teardown);
 
+  g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
+      test_connect, teardown);
+  g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
+      test_message, teardown);
+
 #ifdef DBUS_UNIX
   g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
       test_connect, teardown);
@@ -198,5 +270,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 ();
 }