dbus-daemon.c 63.1 KB
Newer Older
1 2 3
/* Integration tests for the dbus-daemon
 *
 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
Simon McVittie's avatar
Simon McVittie committed
4
 * Copyright © 2010-2011 Nokia Corporation
5
 * Copyright © 2015 Collabora Ltd.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <config.h>

30 31 32 33 34 35 36
#include <errno.h>
#include <string.h>

#include <dbus/dbus.h>

#include <glib.h>
#include <glib/gstdio.h>
37
#include <gio/gio.h>
38

39
#include "bus/stats.h"
40
#include "test-utils-glib.h"
41

42 43
#include <string.h>

44
#ifdef DBUS_UNIX
45
# include <unistd.h>
Simon McVittie's avatar
Simon McVittie committed
46
# include <sys/types.h>
47 48 49 50 51

# ifdef HAVE_GIO_UNIX
    /* The CMake build system doesn't know how to check for this yet */
#   include <gio/gunixfdmessage.h>
# endif
52 53
#endif

54 55 56 57 58 59 60 61 62
/* Platforms where we know that credentials-passing passes both the
 * uid and the pid. Please keep these in alphabetical order.
 *
 * These platforms should #error in _dbus_read_credentials_socket()
 * if we didn't detect their flavour of credentials-passing, since that
 * would be a regression.
 */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
  defined(__linux__) || \
63
  defined(__NetBSD__) || \
64 65 66 67 68 69 70 71 72 73 74
  defined(__OpenBSD__)
# define UNIX_USER_SHOULD_WORK
# define PID_SHOULD_WORK
#endif

/* Platforms where we know that credentials-passing passes the
 * uid, but not necessarily the pid. Again, alphabetical order please.
 *
 * These platforms should also #error in _dbus_read_credentials_socket()
 * if we didn't detect their flavour of credentials-passing.
 */
75
#if 0 /* defined(__your_platform_here__) */
76 77 78
# define UNIX_USER_SHOULD_WORK
#endif

79
typedef struct {
80 81
    gboolean skip;

82 83
    TestMainContext *ctx;

84 85 86
    DBusError e;
    GError *ge;

87
    GPid daemon_pid;
88
    gchar *address;
89 90 91 92

    DBusConnection *left_conn;

    DBusConnection *right_conn;
93
    GQueue held_messages;
94
    gboolean right_conn_echo;
95
    gboolean right_conn_hold;
96
    gboolean wait_forever_called;
97 98 99

    gchar *tmp_runtime_dir;
    gchar *saved_runtime_dir;
100 101 102 103 104 105 106
} Fixture;

static DBusHandlerResult
echo_filter (DBusConnection *connection,
    DBusMessage *message,
    void *user_data)
{
107
  Fixture *f = user_data;
108 109 110 111 112
  DBusMessage *reply;

  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

113 114 115 116 117 118 119
  /* WaitForever() never replies, emulating a service that has got stuck */
  if (dbus_message_is_method_call (message, "com.example", "WaitForever"))
    {
      f->wait_forever_called = TRUE;
      return DBUS_HANDLER_RESULT_HANDLED;
    }

120 121 122 123 124 125 126 127
  reply = dbus_message_new_method_return (message);

  if (reply == NULL)
    g_error ("OOM");

  if (!dbus_connection_send (connection, reply, NULL))
    g_error ("OOM");

128
  dbus_clear_message (&reply);
129 130 131 132

  return DBUS_HANDLER_RESULT_HANDLED;
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static DBusHandlerResult
hold_filter (DBusConnection *connection,
    DBusMessage *message,
    void *user_data)
{
  Fixture *f = user_data;

  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

  g_queue_push_tail (&f->held_messages, dbus_message_ref (message));

  return DBUS_HANDLER_RESULT_HANDLED;
}

148 149 150 151
typedef struct {
    const char *bug_ref;
    guint min_messages;
    const char *config_file;
152
    enum { SPECIFY_ADDRESS = 0, RELY_ON_DEFAULT } connect_mode;
153 154
} Config;

155 156
static void
setup (Fixture *f,
157
    gconstpointer context)
158
{
159
  const Config *config = context;
160

161 162 163
  /* Some tests are fairly slow, so make the test timeout per-test */
  test_timeout_reset ();

164
  f->ctx = test_main_context_get ();
165 166 167
  f->ge = NULL;
  dbus_error_init (&f->e);

168 169 170 171 172 173 174 175 176
  if (config != NULL && config->connect_mode == RELY_ON_DEFAULT)
    {
      /* this is chosen to be something needing escaping */
      f->tmp_runtime_dir = g_dir_make_tmp ("dbus=daemon=test.XXXXXX", &f->ge);
      g_assert_no_error (f->ge);

      /* we're relying on being single-threaded for this to be safe */
      f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
      g_setenv ("XDG_RUNTIME_DIR", f->tmp_runtime_dir, TRUE);
177
      g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
178 179
    }

180
  f->address = test_get_dbus_daemon (config ? config->config_file : NULL,
181
                                     TEST_USER_ME, NULL,
182
                                     &f->daemon_pid);
183

184
  if (f->address == NULL)
185
    {
186 187
      f->skip = TRUE;
      return;
188
    }
189

190
  f->left_conn = test_connect_to_bus (f->ctx, f->address);
191 192 193 194 195 196 197 198 199 200 201 202 203

  if (config != NULL && config->connect_mode == RELY_ON_DEFAULT)
    {
      /* use the default bus for the echo service ("right"), to check that
       * it ends up on the same bus as the client ("left") */
      f->right_conn = dbus_bus_get_private (DBUS_BUS_SESSION, &f->e);
      test_assert_no_error (&f->e);

      if (!test_connection_setup (f->ctx, f->right_conn))
        g_error ("OOM");
    }
  else
    {
204
      f->right_conn = test_connect_to_bus (f->ctx, f->address);
205
    }
206 207 208 209 210
}

static void
add_echo_filter (Fixture *f)
{
211
  if (!dbus_connection_add_filter (f->right_conn, echo_filter, f, NULL))
212 213 214 215 216
    g_error ("OOM");

  f->right_conn_echo = TRUE;
}

217 218 219 220 221 222 223 224 225
static void
add_hold_filter (Fixture *f)
{
  if (!dbus_connection_add_filter (f->right_conn, hold_filter, f, NULL))
    g_error ("OOM");

  f->right_conn_hold = TRUE;
}

226 227 228 229 230 231 232 233 234
static void
pc_count (DBusPendingCall *pc,
    void *data)
{
  guint *received_p = data;

  (*received_p)++;
}

235 236 237 238 239 240 241 242 243 244 245
static void
pc_enqueue (DBusPendingCall *pc,
            void *data)
{
  GQueue *q = data;
  DBusMessage *m = dbus_pending_call_steal_reply (pc);

  g_test_message ("message of type %d", dbus_message_get_type (m));
  g_queue_push_tail (q, m);
}

246 247
static void
test_echo (Fixture *f,
248
    gconstpointer context)
249
{
250
  const Config *config = context;
251 252 253 254 255
  guint count = 2000;
  guint sent;
  guint received = 0;
  double elapsed;

256 257 258 259 260 261
  if (f->skip)
    return;

  if (config != NULL && config->bug_ref != NULL)
    g_test_bug (config->bug_ref);

262 263 264
  if (g_test_perf ())
    count = 100000;

265 266 267
  if (config != NULL)
    count = MAX (config->min_messages, count);

268 269 270 271 272 273 274 275 276 277 278 279 280 281
  add_echo_filter (f);

  g_test_timer_start ();

  for (sent = 0; sent < count; sent++)
    {
      DBusMessage *m = dbus_message_new_method_call (
          dbus_bus_get_unique_name (f->right_conn), "/",
          "com.example", "Spam");
      DBusPendingCall *pc;

      if (m == NULL)
        g_error ("OOM");

282 283 284
      if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                            DBUS_TIMEOUT_INFINITE) ||
          pc == NULL)
285 286 287 288 289 290 291 292
        g_error ("OOM");

      if (dbus_pending_call_get_completed (pc))
        pc_count (pc, &received);
      else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
            NULL))
        g_error ("OOM");

293 294
      dbus_clear_pending_call (&pc);
      dbus_clear_message (&m);
295 296 297
    }

  while (received < count)
298
    test_main_context_iterate (f->ctx, TRUE);
299 300 301 302 303 304 305

  elapsed = g_test_timer_elapsed ();

  g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
      count, elapsed);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static void
test_no_reply (Fixture *f,
    gconstpointer context)
{
  const Config *config = context;
  DBusMessage *m;
  DBusPendingCall *pc;
  DBusMessage *reply = NULL;
  enum { TIMEOUT, DISCONNECT } mode;
  gboolean ok;

  if (f->skip)
    return;

  g_test_bug ("76112");

  if (config != NULL && config->config_file != NULL)
    mode = TIMEOUT;
  else
    mode = DISCONNECT;

  m = dbus_message_new_method_call (
      dbus_bus_get_unique_name (f->right_conn), "/",
      "com.example", "WaitForever");

  add_echo_filter (f);

  if (m == NULL)
    g_error ("OOM");

  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))
342 343 344
    test_pending_call_store_reply (pc, &reply);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
        &reply, NULL))
345 346
    g_error ("OOM");

347 348
  dbus_clear_pending_call (&pc);
  dbus_clear_message (&m);
349 350 351 352 353 354 355 356

  if (mode == DISCONNECT)
    {
      while (!f->wait_forever_called)
        test_main_context_iterate (f->ctx, TRUE);

      dbus_connection_remove_filter (f->right_conn, echo_filter, f);
      dbus_connection_close (f->right_conn);
357
      dbus_clear_connection (&f->right_conn);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    }

  while (reply == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  /* using inefficient string comparison for better assertion message */
  g_assert_cmpstr (
      dbus_message_type_to_string (dbus_message_get_type (reply)), ==,
      dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR));
  ok = dbus_set_error_from_message (&f->e, reply);
  g_assert (ok);
  g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);

  if (mode == DISCONNECT)
    g_assert_cmpstr (f->e.message, ==,
        "Message recipient disconnected from message bus without replying");
  else
    g_assert_cmpstr (f->e.message, ==,
        "Message did not receive a reply (timeout by message bus)");
377

378
  dbus_clear_message (&reply);
379 380
}

Simon McVittie's avatar
Simon McVittie committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static void
test_creds (Fixture *f,
    gconstpointer context)
{
  const char *unique = dbus_bus_get_unique_name (f->left_conn);
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionCredentials");
  DBusPendingCall *pc;
  DBusMessageIter args_iter;
  DBusMessageIter arr_iter;
  DBusMessageIter pair_iter;
  DBusMessageIter var_iter;
  enum {
      SEEN_UNIX_USER = 1,
      SEEN_PID = 2,
396 397
      SEEN_WINDOWS_SID = 4,
      SEEN_LINUX_SECURITY_LABEL = 8
Simon McVittie's avatar
Simon McVittie committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  } seen = 0;

  if (m == NULL)
    g_error ("OOM");

  if (!dbus_message_append_args (m,
        DBUS_TYPE_STRING, &unique,
        DBUS_TYPE_INVALID))
    g_error ("OOM");

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

413
  dbus_clear_message (&m);
Simon McVittie's avatar
Simon McVittie committed
414 415

  if (dbus_pending_call_get_completed (pc))
416 417
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
Simon McVittie's avatar
Simon McVittie committed
418 419 420 421
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
422
    test_main_context_iterate (f->ctx, TRUE);
Simon McVittie's avatar
Simon McVittie committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

  g_assert_cmpstr (dbus_message_get_signature (m), ==, "a{sv}");

  dbus_message_iter_init (m, &args_iter);
  g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
      DBUS_TYPE_ARRAY);
  g_assert_cmpuint (dbus_message_iter_get_element_type (&args_iter), ==,
      DBUS_TYPE_DICT_ENTRY);
  dbus_message_iter_recurse (&args_iter, &arr_iter);

  while (dbus_message_iter_get_arg_type (&arr_iter) != DBUS_TYPE_INVALID)
    {
      const char *name;

      dbus_message_iter_recurse (&arr_iter, &pair_iter);
      g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==,
          DBUS_TYPE_STRING);
      dbus_message_iter_get_basic (&pair_iter, &name);
      dbus_message_iter_next (&pair_iter);
      g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==,
          DBUS_TYPE_VARIANT);
      dbus_message_iter_recurse (&pair_iter, &var_iter);

      if (g_strcmp0 (name, "UnixUserID") == 0)
        {
#ifdef G_OS_UNIX
          guint32 u32;

          g_assert (!(seen & SEEN_UNIX_USER));
          g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
              DBUS_TYPE_UINT32);
          dbus_message_iter_get_basic (&var_iter, &u32);
455
          g_test_message ("%s of this process is %u", name, u32);
Simon McVittie's avatar
Simon McVittie committed
456 457 458 459
          g_assert_cmpuint (u32, ==, geteuid ());
          seen |= SEEN_UNIX_USER;
#else
          g_assert_not_reached ();
Ralf Habacker's avatar
Ralf Habacker committed
460 461 462 463 464 465 466 467 468 469 470 471
#endif
        }
      else if (g_strcmp0 (name, "WindowsSID") == 0)
        {
#ifdef G_OS_WIN32
          gchar *sid;
          char *self_sid;

          g_assert (!(seen & SEEN_WINDOWS_SID));
          g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
              DBUS_TYPE_STRING);
          dbus_message_iter_get_basic (&var_iter, &sid);
472
          g_test_message ("%s of this process is %s", name, sid);
Ralf Habacker's avatar
Ralf Habacker committed
473 474 475 476 477 478 479 480
          if (_dbus_getsid (&self_sid, 0))
            {
              g_assert_cmpstr (self_sid, ==, sid);
              LocalFree(self_sid);
            }
          seen |= SEEN_WINDOWS_SID;
#else
          g_assert_not_reached ();
Simon McVittie's avatar
Simon McVittie committed
481 482 483 484 485 486 487 488 489 490
#endif
        }
      else if (g_strcmp0 (name, "ProcessID") == 0)
        {
          guint32 u32;

          g_assert (!(seen & SEEN_PID));
          g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
              DBUS_TYPE_UINT32);
          dbus_message_iter_get_basic (&var_iter, &u32);
491
          g_test_message ("%s of this process is %u", name, u32);
Simon McVittie's avatar
Simon McVittie committed
492 493 494 495 496 497 498 499 500
#ifdef G_OS_UNIX
          g_assert_cmpuint (u32, ==, getpid ());
#elif defined(G_OS_WIN32)
          g_assert_cmpuint (u32, ==, GetCurrentProcessId ());
#else
          g_assert_not_reached ();
#endif
          seen |= SEEN_PID;
        }
501 502 503 504 505 506 507 508 509 510 511 512 513 514
      else if (g_strcmp0 (name, "LinuxSecurityLabel") == 0)
        {
#ifdef __linux__
          gchar *label;
          int len;
          DBusMessageIter ay_iter;

          g_assert (!(seen & SEEN_LINUX_SECURITY_LABEL));
          g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
              DBUS_TYPE_ARRAY);
          dbus_message_iter_recurse (&var_iter, &ay_iter);
          g_assert_cmpuint (dbus_message_iter_get_arg_type (&ay_iter), ==,
              DBUS_TYPE_BYTE);
          dbus_message_iter_get_fixed_array (&ay_iter, &label, &len);
515
          g_test_message ("%s of this process is %s", name, label);
516 517 518 519 520 521
          g_assert_cmpuint (strlen (label) + 1, ==, len);
          seen |= SEEN_LINUX_SECURITY_LABEL;
#else
          g_assert_not_reached ();
#endif
        }
Simon McVittie's avatar
Simon McVittie committed
522 523 524 525

      dbus_message_iter_next (&arr_iter);
    }

526
#ifdef UNIX_USER_SHOULD_WORK
Simon McVittie's avatar
Simon McVittie committed
527
  g_assert (seen & SEEN_UNIX_USER);
528 529 530
#endif

#ifdef PID_SHOULD_WORK
Simon McVittie's avatar
Simon McVittie committed
531 532 533 534 535 536
  g_assert (seen & SEEN_PID);
#endif

#ifdef G_OS_WIN32
  g_assert (seen & SEEN_WINDOWS_SID);
#endif
537

538 539
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
Simon McVittie's avatar
Simon McVittie committed
540 541
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
static void
test_processid (Fixture *f,
    gconstpointer context)
{
  const char *unique = dbus_bus_get_unique_name (f->left_conn);
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionUnixProcessID");
  DBusPendingCall *pc;
  DBusError error = DBUS_ERROR_INIT;
  guint32 pid;

  if (m == NULL)
    g_error ("OOM");

  if (!dbus_message_append_args (m,
        DBUS_TYPE_STRING, &unique,
        DBUS_TYPE_INVALID))
    g_error ("OOM");

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

566
  dbus_clear_message (&m);
567 568

  if (dbus_pending_call_get_completed (pc))
569 570
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
571 572 573 574 575 576
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

577 578 579 580 581 582 583 584 585 586 587 588
  if (dbus_set_error_from_message (&error, m))
    {
      g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN);

#ifdef PID_SHOULD_WORK
      g_error ("Expected pid to be passed, but got %s: %s",
          error.name, error.message);
#endif

      dbus_error_free (&error);
    }
  else if (dbus_message_get_args (m, &error,
589 590 591 592
        DBUS_TYPE_UINT32, &pid,
        DBUS_TYPE_INVALID))
    {
      g_assert_cmpstr (dbus_message_get_signature (m), ==, "u");
593
      test_assert_no_error (&error);
594

595
      g_test_message ("GetConnectionUnixProcessID returned %u", pid);
596 597

#ifdef G_OS_UNIX
598
      g_assert_cmpuint (pid, ==, getpid ());
599
#elif defined(G_OS_WIN32)
600
      g_assert_cmpuint (pid, ==, GetCurrentProcessId ());
601
#else
602
      g_assert_not_reached ();
603
#endif
604 605 606
    }
  else
    {
607
      g_error ("Unexpected error: %s: %s", error.name, error.message);
608
    }
609

610 611
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
612 613
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static void
test_canonical_path_uae (Fixture *f,
    gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "UpdateActivationEnvironment");
  DBusPendingCall *pc;
  DBusMessageIter args_iter;
  DBusMessageIter arr_iter;

  if (m == NULL)
    g_error ("OOM");

  dbus_message_iter_init_append (m, &args_iter);

  /* Append an empty a{ss} (string => string dictionary). */
  if (!dbus_message_iter_open_container (&args_iter, DBUS_TYPE_ARRAY,
        "{ss}", &arr_iter) ||
      !dbus_message_iter_close_container (&args_iter, &arr_iter))
    g_error ("OOM");

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

640
  dbus_clear_message (&m);
641 642

  if (dbus_pending_call_get_completed (pc))
643 644
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
645 646 647 648 649 650 651 652 653 654
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  /* it succeeds */
  g_assert_cmpint (dbus_message_get_type (m), ==,
      DBUS_MESSAGE_TYPE_METHOD_RETURN);

655 656
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

  /* Now try with the wrong object path */
  m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      "/com/example/Wrong", DBUS_INTERFACE_DBUS, "UpdateActivationEnvironment");

  if (m == NULL)
    g_error ("OOM");

  dbus_message_iter_init_append (m, &args_iter);

  /* Append an empty a{ss} (string => string dictionary). */
  if (!dbus_message_iter_open_container (&args_iter, DBUS_TYPE_ARRAY,
        "{ss}", &arr_iter) ||
      !dbus_message_iter_close_container (&args_iter, &arr_iter))
    g_error ("OOM");

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

678
  dbus_clear_message (&m);
679 680

  if (dbus_pending_call_get_completed (pc))
681 682
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
683 684 685 686 687 688 689 690 691 692 693 694
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  /* it fails, yielding an error message with one string argument */
  g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
  g_assert_cmpstr (dbus_message_get_error_name (m), ==,
      DBUS_ERROR_ACCESS_DENIED);
  g_assert_cmpstr (dbus_message_get_signature (m), ==, "s");

695 696
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
697 698
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
static void
test_max_connections (Fixture *f,
    gconstpointer context)
{
  DBusError error = DBUS_ERROR_INIT;
  DBusConnection *third_conn;
  DBusConnection *failing_conn;

  if (f->skip)
    return;

  /* We have two connections already */
  g_assert (f->left_conn != NULL);
  g_assert (f->right_conn != NULL);

  /* Our configuration file sets the limit to 3 connections, either globally
   * or per uid, so this one is the last that will work */
  third_conn = test_connect_to_bus (f->ctx, f->address);

  /* This one is going to fail. We don't guarantee whether it will fail
   * now, or while registering (implementation detail: it's the latter). */
  failing_conn = dbus_connection_open_private (f->address, &error);

  if (failing_conn != NULL)
    {
      gboolean ok = dbus_bus_register (failing_conn, &error);

      g_assert (!ok);
    }

  g_assert (dbus_error_is_set (&error));
  g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED);

  if (failing_conn != NULL)
733
    dbus_connection_close (failing_conn);
734

735
  dbus_clear_connection (&failing_conn);
736
  dbus_connection_close (third_conn);
737
  dbus_clear_connection (&third_conn);
738 739
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
static void
test_max_replies_per_connection (Fixture *f,
    gconstpointer context)
{
  GQueue received = G_QUEUE_INIT;
  GQueue errors = G_QUEUE_INIT;
  DBusMessage *m;
  DBusPendingCall *pc;
  guint i;
  DBusError error = DBUS_ERROR_INIT;

  if (f->skip)
    return;

  add_hold_filter (f);

  /* The configured limit is 3 replies per connection. */
  for (i = 0; i < 3; i++)
    {
      m = dbus_message_new_method_call (
          dbus_bus_get_unique_name (f->right_conn), "/",
          "com.example", "Spam");

      if (m == NULL)
        g_error ("OOM");

      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))
        pc_enqueue (pc, &received);
      else if (!dbus_pending_call_set_notify (pc, pc_enqueue, &received,
            NULL))
        g_error ("OOM");

      dbus_pending_call_unref (pc);
      dbus_message_unref (m);
    }

  while (g_queue_get_length (&f->held_messages) < 3)
    test_main_context_iterate (f->ctx, TRUE);

  g_assert_cmpuint (g_queue_get_length (&received), ==, 0);
  g_assert_cmpuint (g_queue_get_length (&errors), ==, 0);

  /* Go a couple of messages over the limit. */
  for (i = 0; i < 2; i++)
    {
      m = dbus_message_new_method_call (
          dbus_bus_get_unique_name (f->right_conn), "/",
          "com.example", "Spam");

      if (m == NULL)
        g_error ("OOM");

      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))
        pc_enqueue (pc, &errors);
      else if (!dbus_pending_call_set_notify (pc, pc_enqueue, &errors,
            NULL))
        g_error ("OOM");

      dbus_pending_call_unref (pc);
      dbus_message_unref (m);
    }

  /* Reply to the held messages. */
  for (m = g_queue_pop_head (&f->held_messages);
       m != NULL;
       m = g_queue_pop_head (&f->held_messages))
    {
      DBusMessage *reply = dbus_message_new_method_return (m);

      if (reply == NULL)
        g_error ("OOM");

      if (!dbus_connection_send (f->right_conn, reply, NULL))
        g_error ("OOM");

825 826
      dbus_clear_message (&m);
      dbus_clear_message (&reply);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    }

  /* Wait for all 5 replies to come in. */
  while (g_queue_get_length (&received) + g_queue_get_length (&errors) < 5)
    test_main_context_iterate (f->ctx, TRUE);

  /* The first three succeeded. */
  for (i = 0; i < 3; i++)
    {
      m = g_queue_pop_head (&received);
      g_assert (m != NULL);

      if (dbus_set_error_from_message (&error, m))
        g_error ("Unexpected error: %s: %s", error.name, error.message);

842
      dbus_clear_message (&m);
843 844 845 846 847 848 849 850 851 852 853 854 855
    }

  /* The last two failed. */
  for (i = 0; i < 2; i++)
    {
      m = g_queue_pop_head (&errors);
      g_assert (m != NULL);

      if (!dbus_set_error_from_message (&error, m))
        g_error ("Unexpected success");

      g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED);
      dbus_error_free (&error);
856
      dbus_clear_message (&m);
857 858 859 860 861 862 863 864
    }

  g_assert_cmpuint (g_queue_get_length (&received), ==, 0);
  g_assert_cmpuint (g_queue_get_length (&errors), ==, 0);
  g_queue_clear (&received);
  g_queue_clear (&errors);
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
static void
test_max_match_rules_per_connection (Fixture *f,
    gconstpointer context)
{
  DBusError error = DBUS_ERROR_INIT;

  if (f->skip)
    return;

  dbus_bus_add_match (f->left_conn, "sender='com.example.C1'", &error);
  test_assert_no_error (&error);
  dbus_bus_add_match (f->left_conn, "sender='com.example.C2'", &error);
  test_assert_no_error (&error);
  dbus_bus_add_match (f->left_conn, "sender='com.example.C3'", &error);
  test_assert_no_error (&error);

  dbus_bus_add_match (f->left_conn, "sender='com.example.C4'", &error);
  g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED);
  dbus_error_free (&error);

  dbus_bus_remove_match (f->left_conn, "sender='com.example.C3'", &error);
  test_assert_no_error (&error);

  dbus_bus_add_match (f->left_conn, "sender='com.example.C4'", &error);
  test_assert_no_error (&error);
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
static void
test_max_names_per_connection (Fixture *f,
    gconstpointer context)
{
  DBusError error = DBUS_ERROR_INIT;
  int ret;

  if (f->skip)
    return;

  /* The limit in the configuration file is set to 4, but we only own 3
   * names here - remember that the unique name is a name too. */

  ret = dbus_bus_request_name (f->left_conn, "com.example.C1", 0, &error);
  test_assert_no_error (&error);
  g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);

  ret = dbus_bus_request_name (f->left_conn, "com.example.C2", 0, &error);
  test_assert_no_error (&error);
  g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);

  ret = dbus_bus_request_name (f->left_conn, "com.example.C3", 0, &error);
  test_assert_no_error (&error);
  g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);

  ret = dbus_bus_request_name (f->left_conn, "com.example.C4", 0, &error);
  g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED);
  dbus_error_free (&error);
  g_assert_cmpint (ret, ==, -1);

  ret = dbus_bus_release_name (f->left_conn, "com.example.C3", &error);
  test_assert_no_error (&error);
  g_assert_cmpint (ret, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);

  ret = dbus_bus_request_name (f->left_conn, "com.example.C4", 0, &error);
  test_assert_no_error (&error);
  g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
#if defined(DBUS_UNIX) && defined(HAVE_UNIX_FD_PASSING) && defined(HAVE_GIO_UNIX)

static DBusHandlerResult
wait_for_disconnected_cb (DBusConnection *client_conn,
    DBusMessage *message,
    void *data)
{
  gboolean *disconnected = data;

  if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected"))
    {
      *disconnected = TRUE;
      return DBUS_HANDLER_RESULT_HANDLED;
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static const guchar partial_message[] =
{
  DBUS_LITTLE_ENDIAN,
  DBUS_MESSAGE_TYPE_METHOD_CALL,
  0, /* flags */
  1, /* version */
  0xff, 0xff, 0, 0, /* length of body = 65535 bytes */
  1, 2, 3, 4, /* cookie */
  0xff, 0xff, 0, 0, /* length of header fields array = 65535 bytes */
  42 /* pretending to be the beginning of the header fields array */
};

static void
send_all_with_fd (GSocket *socket,
963
                  const guchar *local_partial_message,
964 965 966 967 968 969
                  gsize len,
                  int fd)
{
  GSocketControlMessage *fdm = g_unix_fd_message_new ();
  GError *error = NULL;
  gssize sent;
970
  GOutputVector vector = { local_partial_message, len };
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

  g_unix_fd_message_append_fd (G_UNIX_FD_MESSAGE (fdm), fd, &error);
  g_assert_no_error (error);

  sent = g_socket_send_message (socket, NULL, &vector, 1, &fdm, 1,
      G_SOCKET_MSG_NONE, NULL, &error);
  g_assert_no_error (error);
  g_assert_cmpint (sent, >=, 1);
  g_assert_cmpint (sent, <=, vector.size);

  while (((gsize) sent) < vector.size)
    {
      vector.size -= sent;
      vector.buffer = ((const guchar *) vector.buffer) + sent;
      sent = g_socket_send_message (socket, NULL, &vector, 1, NULL, 0,
          G_SOCKET_MSG_NONE, NULL, &error);
      g_assert_no_error (error);
      g_assert_cmpint (sent, >=, 1);
      g_assert_cmpint (sent, <=, vector.size);
    }

  g_object_unref (fdm);
}

static void
test_pending_fd_timeout (Fixture *f,
    gconstpointer context)
{
  GError *error = NULL;
  gint64 start;
  int fd;
  GSocket *socket;
  gboolean have_mem;
  gboolean disconnected = FALSE;

  if (f->skip)
    return;

1009 1010 1011 1012 1013 1014
  if (getuid () == 0)
    {
      g_test_skip ("Cannot test, uid 0 is immune to this limit");
      return;
    }

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  have_mem = dbus_connection_add_filter (f->left_conn, wait_for_disconnected_cb,
      &disconnected, NULL);
  g_assert (have_mem);

  /* This is not API. Never do this. */

  if (!dbus_connection_get_socket (f->left_conn, &fd))
    g_error ("failed to steal fd from left connection");

  socket = g_socket_new_from_fd (fd, &error);
  g_assert_no_error (error);
  g_assert (socket != NULL);

  /* We send part of a message that contains a fd, then stop. */
  start = g_get_monotonic_time ();
  send_all_with_fd (socket, partial_message, G_N_ELEMENTS (partial_message),
                    fd);

  while (!disconnected)
    {
      test_progress ('.');
      test_main_context_iterate (f->ctx, TRUE);

      /* It should take no longer than 500ms to get disconnected. We'll
       * be generous and allow 1000ms. */
      g_assert_cmpint (g_get_monotonic_time (), <=, start + G_USEC_PER_SEC);
    }

  g_object_unref (socket);
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
typedef struct
{
  const gchar *path;
  guint n_fds;
  gboolean should_work;
} CountFdsVector;

static const CountFdsVector count_fds_vectors[] =
{
  /* Deny sending if number of fds <= 2 */
  { "/test/DenySendMax2", 1, FALSE },
  { "/test/DenySendMax2", 2, FALSE },
  { "/test/DenySendMax2", 3, TRUE },
  { "/test/DenySendMax2", 4, TRUE },

  /* Deny receiving if number of fds <= 3 */
  { "/test/DenyReceiveMax3", 2, FALSE },
  { "/test/DenyReceiveMax3", 3, FALSE },
  { "/test/DenyReceiveMax3", 4, TRUE },
  { "/test/DenyReceiveMax3", 5, TRUE },

  /* Deny sending if number of fds >= 4 */
  { "/test/DenySendMin4", 2, TRUE },
  { "/test/DenySendMin4", 3, TRUE },
  { "/test/DenySendMin4", 4, FALSE },
  { "/test/DenySendMin4", 5, FALSE },

  /* Deny receiving if number of fds >= 5 */
  { "/test/DenyReceiveMin5", 3, TRUE },
  { "/test/DenyReceiveMin5", 4, TRUE },
  { "/test/DenyReceiveMin5", 5, FALSE },
  { "/test/DenyReceiveMin5", 6, FALSE },
};

static void
test_count_fds (Fixture *f,
    gconstpointer context)
{
  GQueue received = G_QUEUE_INIT;
  DBusMessage *m;
  DBusPendingCall *pc;
  guint i;
  DBusError error = DBUS_ERROR_INIT;
  const int stdin_fd = 0;

  if (f->skip)
    return;

  add_hold_filter (f);

  for (i = 0; i < G_N_ELEMENTS (count_fds_vectors); i++)
    {
      const CountFdsVector *vector = &count_fds_vectors[i];
      guint j;

      m = dbus_message_new_method_call (
          dbus_bus_get_unique_name (f->right_conn), vector->path,
          "com.example", "Spam");

      if (m == NULL)
        g_error ("OOM");

      for (j = 0; j < vector->n_fds; j++)
        {
          if (!dbus_message_append_args (m,
                                         DBUS_TYPE_UNIX_FD, &stdin_fd,
                                         DBUS_TYPE_INVALID))
            g_error ("OOM");
        }

      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))
        pc_enqueue (pc, &received);
      else if (!dbus_pending_call_set_notify (pc, pc_enqueue, &received,
            NULL))
        g_error ("OOM");

      dbus_pending_call_unref (pc);
      dbus_message_unref (m);

      if (vector->should_work)
        {
          DBusMessage *reply;

          while (g_queue_get_length (&f->held_messages) < 1)
            test_main_context_iterate (f->ctx, TRUE);

          g_assert_cmpint (g_queue_get_length (&f->held_messages), ==, 1);

          m = g_queue_pop_head (&f->held_messages);

          g_assert_cmpint (g_queue_get_length (&f->held_messages), ==, 0);

          reply = dbus_message_new_method_return (m);

          if (reply == NULL)
            g_error ("OOM");

          if (!dbus_connection_send (f->right_conn, reply, NULL))
            g_error ("OOM");

          dbus_message_unref (reply);
          dbus_message_unref (m);
        }

      while (g_queue_get_length (&received) < 1)
        test_main_context_iterate (f->ctx, TRUE);

      g_assert_cmpint (g_queue_get_length (&received), ==, 1);
      m = g_queue_pop_head (&received);
      g_assert (m != NULL);
      g_assert_cmpint (g_queue_get_length (&received), ==, 0);

      if (vector->should_work)
        {
          if (dbus_set_error_from_message (&error, m))
            g_error ("Unexpected error: %s: %s", error.name, error.message);

          g_test_message ("Sending %u fds to %s was not denied, as expected",
                          vector->n_fds, vector->path);
        }
      else if (!dbus_set_error_from_message (&error, m))
        {
          g_error ("Unexpected success");
        }
      else
        {
          g_assert_cmpstr (error.name, ==, DBUS_ERROR_ACCESS_DENIED);
          dbus_error_free (&error);
          g_test_message ("Sending %u fds to %s was denied, as expected",
                          vector->n_fds, vector->path);
        }

      dbus_message_unref (m);
    }
}

1187 1188
#endif

1189 1190 1191 1192
static void
test_peer_get_machine_id (Fixture *f,
                          gconstpointer context)
{
1193
  char *what_i_think;
1194 1195 1196 1197 1198 1199 1200 1201
  const char *what_daemon_thinks;
  DBusMessage *m = NULL;
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;

  if (f->skip)
    return;

1202 1203 1204
  what_i_think = dbus_try_get_local_machine_id (&error);

  if (what_i_think == NULL)
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    {
      if (g_getenv ("DBUS_TEST_UNINSTALLED") != NULL)
        {
          /* When running unit tests during make check or make installcheck,
           * tolerate this */
          g_test_skip ("Machine UUID not available");
          return;
        }
      else
        {
          /* When running integration tests, don't tolerate it */
1216
          g_error ("%s", error.message);
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
        }
    }

  /* Check that the dbus-daemon agrees with us. */
  m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                    DBUS_PATH_DBUS,
                                    DBUS_INTERFACE_PEER,
                                    "GetMachineId");

  if (m == NULL ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1232
  dbus_clear_message (&m);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_message_get_args (m, &error,
        DBUS_TYPE_STRING, &what_daemon_thinks,
        DBUS_TYPE_INVALID))
    g_error ("%s: %s", error.name, error.message);

1248
  g_assert_cmpstr (what_i_think, ==, what_daemon_thinks);
1249 1250 1251
  g_assert_nonnull (what_daemon_thinks);
  g_assert_cmpuint (strlen (what_daemon_thinks), ==, 32);

1252 1253
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1254
  dbus_free (what_i_think);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
}

static void
test_peer_ping (Fixture *f,
                gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PEER, "Ping");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;

  if (f->skip)
    return;

  m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PEER, "Ping");

  if (m == NULL ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1278
  dbus_clear_message (&m);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_message_get_args (m, &error, DBUS_TYPE_INVALID))
    g_error ("%s: %s", error.name, error.message);

1292 1293
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1294 1295
}

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
static void
test_get_invalid_path (Fixture *f,
                       gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      "/", DBUS_INTERFACE_PROPERTIES, "Get");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = DBUS_INTERFACE_DBUS;
  const char *property = "Interfaces";

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1320
  dbus_clear_message (&m);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  /* That object path does not have that interface */
  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE);
  dbus_error_free (&error);

1338 1339
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
}

static void
test_get_invalid_iface (Fixture *f,
                        gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PROPERTIES, "Get");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = "com.example.Nope";
  const char *property = "Whatever";

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1366
  dbus_clear_message (&m);
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE);
  dbus_error_free (&error);

1383 1384
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
}

static void
test_get_invalid (Fixture *f,
                  gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PROPERTIES, "Get");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = DBUS_INTERFACE_DBUS;
  const char *property = "Whatever";

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1411
  dbus_clear_message (&m);
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_PROPERTY);
  dbus_error_free (&error);

1428 1429
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
}

static void
test_get_all_invalid_iface (Fixture *f,
                            gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PROPERTIES, "GetAll");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = "com.example.Nope";

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_INVALID) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1454
  dbus_clear_message (&m);
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE);
  dbus_error_free (&error);

1471 1472
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
}

static void
test_get_all_invalid_path (Fixture *f,
                           gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      "/", DBUS_INTERFACE_PROPERTIES, "GetAll");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = DBUS_INTERFACE_DBUS;

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_INVALID) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1497
  dbus_clear_message (&m);
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  /* That object path does not have that interface */
  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE);
  dbus_error_free (&error);

1515 1516
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
}

static void
test_set_invalid_iface (Fixture *f,
                        gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_PROPERTIES, "Set");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = "com.example.Nope";
  const char *property = "Whatever";
  DBusMessageIter args_iter;
  DBusMessageIter var_iter;
  dbus_bool_t b = FALSE;

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID))
    g_error ("OOM");

  dbus_message_iter_init_append (m, &args_iter);

  if (!dbus_message_iter_open_container (&args_iter,
        DBUS_TYPE_VARIANT, "b", &var_iter) ||
      !dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_BOOLEAN, &b) ||
      !dbus_message_iter_close_container (&args_iter, &var_iter) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1554
  dbus_clear_message (&m);
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

  if (!dbus_set_error_from_message (&error, m))
    g_error ("Unexpected success");

  g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE);
  dbus_error_free (&error);

1571 1572
  dbus_clear_message (&m);
  dbus_clear_pending_call (&pc);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
}

static void
test_set_invalid_path (Fixture *f,
                       gconstpointer context)
{
  DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
      "/", DBUS_INTERFACE_PROPERTIES, "Set");
  DBusPendingCall *pc = NULL;
  DBusError error = DBUS_ERROR_INIT;
  const char *iface = DBUS_INTERFACE_DBUS;
  const char *property = "Interfaces";
  DBusMessageIter args_iter;
  DBusMessageIter var_iter;
  dbus_bool_t b = FALSE;

  if (f->skip)
    return;

  if (m == NULL ||
      !dbus_message_append_args (m,
        DBUS_TYPE_STRING, &iface,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID))
    g_error ("OOM");

  dbus_message_iter_init_append (m, &args_iter);

  if (!dbus_message_iter_open_container (&args_iter,
        DBUS_TYPE_VARIANT, "b", &var_iter) ||
      !dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_BOOLEAN, &b) ||
      !dbus_message_iter_close_container (&args_iter, &var_iter) ||
      !dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

1610
  dbus_clear_message (&m);
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620