gdbus-non-socket.c 8.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/* GLib testing framework examples and tests
 *
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include <gio/gio.h>
#include <unistd.h>
#include <string.h>

#include <stdlib.h>

#ifdef G_OS_UNIX
#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
#include <gio/gunixconnection.h>
#endif

#include "gdbus-tests.h"

static GMainLoop *loop = NULL;

/* ---------------------------------------------------------------------------------------------------- */
#ifdef G_OS_UNIX

42 43
#include "test-pipe-unix.h"
#include "test-io-stream.h"
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

/* ---------------------------------------------------------------------------------------------------- */

static const GDBusArgInfo pokee_method_poke_out_arg0 = {
  -1,   /* ref_count */
  "result",
  "s",
  NULL  /* annotations */
};

static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
  &pokee_method_poke_out_arg0,
  NULL,
};

static const GDBusArgInfo pokee_method_poke_in_arg0 = {
  -1,   /* ref_count */
  "value",
  "s",
  NULL  /* annotations */
};

static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
  &pokee_method_poke_in_arg0,
  NULL,
};

static const GDBusMethodInfo pokee_method_poke = {
  -1,   /* ref_count */
  "Poke",
  (GDBusArgInfo**) pokee_method_poke_in_args,
  (GDBusArgInfo**) pokee_method_poke_out_args,
  NULL  /* annotations */
};

static const GDBusMethodInfo *pokee_methods[2] = {
  &pokee_method_poke,
  NULL
};

static const GDBusInterfaceInfo pokee_object_info = {
  -1,  /* ref_count */
  "org.gtk.GDBus.Pokee",
  (GDBusMethodInfo**) pokee_methods,
  NULL, /* signals */
  NULL, /* properties */
  NULL  /* annotations */
};

static void
pokee_method_call (GDBusConnection       *connection,
                   const gchar           *sender,
                   const gchar           *object_path,
                   const gchar           *interface_name,
                   const gchar           *method_name,
                   GVariant              *parameters,
                   GDBusMethodInvocation *invocation,
                   gpointer               user_data)
{
  const gchar *str;
  gchar *ret;

  g_assert_cmpstr (method_name, ==, "Poke");

  g_variant_get (parameters, "(&s)", &str);
  ret = g_strdup_printf ("You poked me with: `%s'", str);
  g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ret));
  g_free (ret);
}

static const GDBusInterfaceVTable pokee_vtable = {
  pokee_method_call,
  NULL, /* get_property */
  NULL  /* set_property */
};

120 121 122 123 124 125 126 127 128 129
/* Processes:
 *
 * parent
 * \- first child (via fork()) is the pokee
 * \- second child (via g_test_trap_fork()) is the poker
 *
 * The second child only exists to avoid sharing a main context between several
 * second-children if we run a test resembling this one multiple times.
 * See https://bugzilla.gnome.org/show_bug.cgi?id=658999 for why that's bad.
 */
130 131 132
static void
test_non_socket (void)
{
133
  GIOStream *streams[2];
134 135 136
  GDBusConnection *connection;
  GError *error;
  gchar *guid;
137
  pid_t first_child;
138 139
  GVariant *ret;
  const gchar *str;
140
  gboolean ok;
141

142 143 144 145 146 147 148 149 150 151 152
  error = NULL;

  ok = test_bidi_pipe (&streams[0], &streams[1], &error);
  g_assert_no_error (error);
  g_assert (ok);
  g_assert (G_IS_IO_STREAM (streams[0]));
  g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[0])));
  g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[0])));
  g_assert (G_IS_IO_STREAM (streams[1]));
  g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[1])));
  g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[1])));
153

154
  switch ((first_child = fork ()))
155 156 157 158 159 160
    {
    case -1:
      g_assert_not_reached ();
      break;

    case 0:
161 162 163 164 165 166 167
      /* first child */

      /* we shouldn't do this in the parent, because we shouldn't use a
       * GMainContext both before and after fork
       */
      loop = g_main_loop_new (NULL, FALSE);

168 169 170 171 172
      ok = g_io_stream_close (streams[1], NULL, &error);
      g_assert_no_error (error);
      g_assert (ok);
      g_object_unref (streams[1]);

173 174
      guid = g_dbus_generate_guid ();
      error = NULL;
175 176 177 178 179 180 181 182
      /* We need to delay message processing to avoid the race
       * described in
       *
       *  https://bugzilla.gnome.org/show_bug.cgi?id=627188
       *
       * This is because (early) dispatching is done on the IO thread
       * (method_call() isn't called until we're in the right thread
       * though) so in rare cases the parent sends the message before
183
       * we (the first child) register the object
184
       */
185
      connection = g_dbus_connection_new_sync (streams[0],
186
                                               guid,
187 188
                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
                                               G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
189 190 191 192 193
                                               NULL, /* GDBusAuthObserver */
                                               NULL,
                                               &error);
      g_free (guid);
      g_assert_no_error (error);
194
      g_object_unref (streams[0]);
195 196 197 198 199 200 201 202 203 204 205 206 207 208

      /* make sure we exit along with the parent */
      g_dbus_connection_set_exit_on_close (connection, TRUE);

      error = NULL;
      g_dbus_connection_register_object (connection,
                                         "/pokee",
                                         (GDBusInterfaceInfo *) &pokee_object_info,
                                         &pokee_vtable,
                                         NULL, /* user_data */
                                         NULL, /* user_data_free_func */
                                         &error);
      g_assert_no_error (error);

209 210 211
      /* and now start message processing */
      g_dbus_connection_start_message_processing (connection);

212 213 214 215 216 217 218 219 220 221
      g_main_loop_run (loop);

      g_assert_not_reached ();
      break;

    default:
      /* parent continues below */
      break;
    }

222 223 224
  if (!g_test_trap_fork (0, 0))
    {
      /* parent */
225 226 227
      g_object_unref (streams[0]);
      g_object_unref (streams[1]);

228 229 230 231 232 233 234 235 236 237 238 239
      g_test_trap_assert_passed ();
      g_assert_cmpint (kill (first_child, SIGTERM), ==, 0);
      return;
    }

  /* second child */

  /* we shouldn't do this in the parent, because we shouldn't use a
   * GMainContext both before and after fork
   */
  loop = g_main_loop_new (NULL, FALSE);

240 241 242 243 244 245
  ok = g_io_stream_close (streams[0], NULL, &error);
  g_assert_no_error (error);
  g_assert (ok);
  g_object_unref (streams[0]);

  connection = g_dbus_connection_new_sync (streams[1],
246 247 248 249 250 251
                                           NULL, /* guid */
                                           G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                           NULL, /* GDBusAuthObserver */
                                           NULL,
                                           &error);
  g_assert_no_error (error);
252
  g_object_unref (streams[1]);
253

254
  /* poke the first child */
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  error = NULL;
  ret = g_dbus_connection_call_sync (connection,
                                     NULL, /* name */
                                     "/pokee",
                                     "org.gtk.GDBus.Pokee",
                                     "Poke",
                                     g_variant_new ("(s)", "I am the POKER!"),
                                     G_VARIANT_TYPE ("(s)"), /* return type */
                                     G_DBUS_CALL_FLAGS_NONE,
                                     -1,
                                     NULL, /* cancellable */
                                     &error);
  g_assert_no_error (error);
  g_variant_get (ret, "(&s)", &str);
  g_assert_cmpstr (str, ==, "You poked me with: `I am the POKER!'");
  g_variant_unref (ret);

  g_object_unref (connection);
273 274
  g_main_loop_unref (loop);
  exit (0);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

#else /* G_OS_UNIX */

static void
test_non_socket (void)
{
  /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
}
#endif

/* ---------------------------------------------------------------------------------------------------- */

int
main (int   argc,
      char *argv[])
{
  gint ret;

  g_type_init ();
  g_test_init (&argc, &argv, NULL);

297
  g_test_add_func ("/gdbus/non-socket", test_non_socket);
298 299 300 301 302

  ret = g_test_run();

  return ret;
}