Blame gio/tests/gdbus-overflow.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
/* for open(2) */
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
/* for g_unlink() */
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
Packit ae235b
#include <gio/gnetworking.h>
Packit ae235b
#include <gio/gunixsocketaddress.h>
Packit ae235b
#include <gio/gunixfdlist.h>
Packit ae235b
Packit ae235b
/* used in test_overflow */
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <gio/gunixconnection.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static gboolean is_unix = TRUE;
Packit ae235b
#else
Packit ae235b
static gboolean is_unix = FALSE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gchar *tmp_address = NULL;
Packit ae235b
static gchar *test_guid = NULL;
Packit ae235b
static GMainLoop *loop = NULL;
Packit ae235b
Packit ae235b
static const gchar *test_interface_introspection_xml =
Packit ae235b
  "<node>"
Packit ae235b
  "  <interface name='org.gtk.GDBus.PeerTestInterface'>"
Packit ae235b
  "    <method name='HelloPeer'>"
Packit ae235b
  "      <arg type='s' name='greeting' direction='in'/>"
Packit ae235b
  "      <arg type='s' name='response' direction='out'/>"
Packit ae235b
  "    </method>"
Packit ae235b
  "    <method name='EmitSignal'/>"
Packit ae235b
  "    <method name='EmitSignalWithNameSet'/>"
Packit ae235b
  "    <method name='OpenFile'>"
Packit ae235b
  "      <arg type='s' name='path' direction='in'/>"
Packit ae235b
  "    </method>"
Packit ae235b
  "    <signal name='PeerSignal'>"
Packit ae235b
  "      <arg type='s' name='a_string'/>"
Packit ae235b
  "    </signal>"
Packit ae235b
  "    <property type='s' name='PeerProperty' access='read'/>"
Packit ae235b
  "  </interface>"
Packit ae235b
  "</node>";
Packit ae235b
static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
Packit ae235b
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
Packit ae235b
/* Chosen to be big enough to overflow the socket buffer */
Packit ae235b
#define OVERFLOW_NUM_SIGNALS 5000
Packit ae235b
#define OVERFLOW_TIMEOUT_SEC 10
Packit ae235b
Packit ae235b
static GDBusMessage *
Packit ae235b
overflow_filter_func (GDBusConnection *connection,
Packit ae235b
                      GDBusMessage    *message,
Packit ae235b
                      gboolean         incoming,
Packit ae235b
                      gpointer         user_data)
Packit ae235b
{
Packit ae235b
  volatile gint *counter = user_data;
Packit ae235b
  *counter += 1;
Packit ae235b
  return message;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
overflow_on_500ms_later_func (gpointer user_data)
Packit ae235b
{
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return FALSE; /* don't keep the idle */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_overflow (void)
Packit ae235b
{
Packit ae235b
  gint sv[2];
Packit ae235b
  gint n;
Packit ae235b
  GSocket *socket;
Packit ae235b
  GSocketConnection *socket_connection;
Packit ae235b
  GDBusConnection *producer, *consumer;
Packit ae235b
  GError *error;
Packit ae235b
  GTimer *timer;
Packit ae235b
  volatile gint n_messages_received;
Packit ae235b
  volatile gint n_messages_sent;
Packit ae235b
Packit ae235b
  g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  socket = g_socket_new_from_fd (sv[0], &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  socket_connection = g_socket_connection_factory_create_connection (socket);
Packit ae235b
  g_assert (socket_connection != NULL);
Packit ae235b
  g_object_unref (socket);
Packit ae235b
  producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
Packit ae235b
					 NULL, /* guid */
Packit ae235b
					 G_DBUS_CONNECTION_FLAGS_NONE,
Packit ae235b
					 NULL, /* GDBusAuthObserver */
Packit ae235b
					 NULL, /* GCancellable */
Packit ae235b
Packit ae235b
					 &error);
Packit ae235b
  g_dbus_connection_set_exit_on_close (producer, TRUE);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_object_unref (socket_connection);
Packit ae235b
  n_messages_sent = 0;
Packit ae235b
  g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
Packit ae235b
Packit ae235b
  /* send enough data that we get an EAGAIN */
Packit ae235b
  for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
Packit ae235b
    {
Packit ae235b
      error = NULL;
Packit ae235b
      g_dbus_connection_emit_signal (producer,
Packit ae235b
                                     NULL, /* destination */
Packit ae235b
                                     "/org/foo/Object",
Packit ae235b
                                     "org.foo.Interface",
Packit ae235b
                                     "Member",
Packit ae235b
                                     g_variant_new ("(s)", "a string"),
Packit ae235b
                                     &error);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
Packit ae235b
   * kernel buffers) and verify that n_messages_sent <
Packit ae235b
   * OVERFLOW_NUM_SIGNALS
Packit ae235b
   *
Packit ae235b
   * This is to verify that not all the submitted messages have been
Packit ae235b
   * sent to the underlying transport.
Packit ae235b
   */
Packit ae235b
  g_timeout_add (500, overflow_on_500ms_later_func, NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
Packit ae235b
Packit ae235b
  /* now suck it all out as a client, and add it up */
Packit ae235b
  socket = g_socket_new_from_fd (sv[1], &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  socket_connection = g_socket_connection_factory_create_connection (socket);
Packit ae235b
  g_assert (socket_connection != NULL);
Packit ae235b
  g_object_unref (socket);
Packit ae235b
  consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
Packit ae235b
					 NULL, /* guid */
Packit ae235b
					 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
Packit ae235b
					 NULL, /* GDBusAuthObserver */
Packit ae235b
					 NULL, /* GCancellable */
Packit ae235b
					 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_object_unref (socket_connection);
Packit ae235b
  n_messages_received = 0;
Packit ae235b
  g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
Packit ae235b
  g_dbus_connection_start_message_processing (consumer);
Packit ae235b
Packit ae235b
  timer = g_timer_new ();
Packit ae235b
  g_timer_start (timer);
Packit ae235b
Packit ae235b
  while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
Packit ae235b
      g_main_context_iteration (NULL, FALSE);
Packit ae235b
Packit ae235b
  g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
Packit ae235b
  g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
Packit ae235b
Packit ae235b
  g_timer_destroy (timer);
Packit ae235b
  g_object_unref (consumer);
Packit ae235b
  g_object_unref (producer);
Packit ae235b
}
Packit ae235b
#else
Packit ae235b
static void
Packit ae235b
test_overflow (void)
Packit ae235b
{
Packit ae235b
  /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
  GDBusNodeInfo *introspection_data = NULL;
Packit ae235b
  gchar *tmpdir = NULL;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
Packit ae235b
  g_assert (introspection_data != NULL);
Packit ae235b
  test_interface_introspection_data = introspection_data->interfaces[0];
Packit ae235b
Packit ae235b
  test_guid = g_dbus_generate_guid ();
Packit ae235b
Packit ae235b
  if (is_unix)
Packit ae235b
    {
Packit ae235b
      if (g_unix_socket_address_abstract_names_supported ())
Packit ae235b
	tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
Packit ae235b
	  tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    tmp_address = g_strdup ("nonce-tcp:");
Packit ae235b
Packit ae235b
  /* all the tests rely on a shared main loop */
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/overflow", test_overflow);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
  g_free (test_guid);
Packit ae235b
  g_dbus_node_info_unref (introspection_data);
Packit ae235b
  if (is_unix)
Packit ae235b
    g_free (tmp_address);
Packit ae235b
  if (tmpdir)
Packit ae235b
    {
Packit ae235b
      g_rmdir (tmpdir);
Packit ae235b
      g_free (tmpdir);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}