dbus-daemon.c 22.9 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 37
#include <errno.h>
#include <string.h>

#include <dbus/dbus.h>

#include <glib.h>
#include <glib/gstdio.h>

38
#include "test-utils-glib.h"
39

40 41
#include <string.h>

42
#ifdef DBUS_UNIX
43
# include <pwd.h>
44
# include <unistd.h>
45
# include <sys/types.h>
46 47 48 49 50 51 52 53

# ifdef HAVE_SYS_RESOURCE_H
#   include <sys/resource.h>
# endif

# ifdef HAVE_SYS_TIME_H
#   include <sys/time.h>
# endif
54 55
#endif

56 57 58 59 60 61 62 63 64
/* 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__) || \
65
  defined(__NetBSD__) || \
66 67 68 69 70 71 72 73 74 75 76
  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.
 */
77
#if 0 /* defined(__your_platform_here__) */
78 79 80
# define UNIX_USER_SHOULD_WORK
#endif

81
typedef struct {
82 83
    gboolean skip;

84 85
    TestMainContext *ctx;

86 87 88
    DBusError e;
    GError *ge;

89
    GPid daemon_pid;
90 91 92 93 94

    DBusConnection *left_conn;

    DBusConnection *right_conn;
    gboolean right_conn_echo;
95
    gboolean wait_forever_called;
96 97 98

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

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

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

112 113 114 115 116 117 118
  /* 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;
    }

119 120 121 122 123 124 125 126 127 128 129 130 131
  reply = dbus_message_new_method_return (message);

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

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

  dbus_message_unref (reply);

  return DBUS_HANDLER_RESULT_HANDLED;
}

132 133 134 135
typedef struct {
    const char *bug_ref;
    guint min_messages;
    const char *config_file;
136
    TestUser user;
137
    enum { SPECIFY_ADDRESS = 0, RELY_ON_DEFAULT } connect_mode;
138 139
} Config;

140 141
static void
setup (Fixture *f,
142
    gconstpointer context)
143
{
144
  const Config *config = context;
145 146
  gchar *address;

147
  f->ctx = test_main_context_get ();
148 149 150
  f->ge = NULL;
  dbus_error_init (&f->e);

151 152 153 154 155 156 157 158 159
  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);
160
      g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
161 162
    }

163
  address = test_get_dbus_daemon (config ? config->config_file : NULL,
164
                                  config ? config->user : TEST_USER_ME,
165
                                  &f->daemon_pid);
166

167
  if (address == NULL)
168
    {
169 170
      f->skip = TRUE;
      return;
171
    }
172

173
  f->left_conn = test_connect_to_bus (f->ctx, address);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  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
    {
      f->right_conn = test_connect_to_bus (f->ctx, address);
    }

190 191 192 193 194 195
  g_free (address);
}

static void
add_echo_filter (Fixture *f)
{
196
  if (!dbus_connection_add_filter (f->right_conn, echo_filter, f, NULL))
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    g_error ("OOM");

  f->right_conn_echo = TRUE;
}

static void
pc_count (DBusPendingCall *pc,
    void *data)
{
  guint *received_p = data;

  (*received_p)++;
}

static void
test_echo (Fixture *f,
213
    gconstpointer context)
214
{
215
  const Config *config = context;
216 217 218 219 220
  guint count = 2000;
  guint sent;
  guint received = 0;
  double elapsed;

221 222 223 224 225 226
  if (f->skip)
    return;

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

227 228 229
  if (g_test_perf ())
    count = 100000;

230 231 232
  if (config != NULL)
    count = MAX (config->min_messages, count);

233 234 235 236 237 238 239 240 241 242 243 244 245 246
  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");

247 248 249
      if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                            DBUS_TIMEOUT_INFINITE) ||
          pc == NULL)
250 251 252 253 254 255 256 257 258 259 260 261 262
        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");

      dbus_pending_call_unref (pc);
      dbus_message_unref (m);
    }

  while (received < count)
263
    test_main_context_iterate (f->ctx, TRUE);
264 265 266 267 268 269 270

  elapsed = g_test_timer_elapsed ();

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

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
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))
307 308 309
    test_pending_call_store_reply (pc, &reply);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
        &reply, NULL))
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 342 343 344
    g_error ("OOM");

  dbus_pending_call_unref (pc);
  dbus_message_unref (m);

  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);
      dbus_connection_unref (f->right_conn);
      f->right_conn = NULL;
    }

  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)");
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
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,
360 361
      SEEN_WINDOWS_SID = 4,
      SEEN_LINUX_SECURITY_LABEL = 8
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  } 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");

  dbus_message_unref (m);
  m = NULL;

  if (dbus_pending_call_get_completed (pc))
381 382
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
383 384 385 386
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
387
    test_main_context_iterate (f->ctx, TRUE);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  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);
420
          g_test_message ("%s of this process is %u", name, u32);
421 422 423 424
          g_assert_cmpuint (u32, ==, geteuid ());
          seen |= SEEN_UNIX_USER;
#else
          g_assert_not_reached ();
425 426 427 428 429 430 431 432 433 434 435 436
#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);
437
          g_test_message ("%s of this process is %s", name, sid);
438 439 440 441 442 443 444 445
          if (_dbus_getsid (&self_sid, 0))
            {
              g_assert_cmpstr (self_sid, ==, sid);
              LocalFree(self_sid);
            }
          seen |= SEEN_WINDOWS_SID;
#else
          g_assert_not_reached ();
446 447 448 449 450 451 452 453 454 455
#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);
456
          g_test_message ("%s of this process is %u", name, u32);
457 458 459 460 461 462 463 464 465
#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;
        }
466 467 468 469 470 471 472 473 474 475 476 477 478 479
      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);
480
          g_test_message ("%s of this process is %s", name, label);
481 482 483 484 485 486
          g_assert_cmpuint (strlen (label) + 1, ==, len);
          seen |= SEEN_LINUX_SECURITY_LABEL;
#else
          g_assert_not_reached ();
#endif
        }
487 488 489 490

      dbus_message_iter_next (&arr_iter);
    }

491
#ifdef UNIX_USER_SHOULD_WORK
492
  g_assert (seen & SEEN_UNIX_USER);
493 494 495
#endif

#ifdef PID_SHOULD_WORK
496 497 498 499 500 501 502 503
  g_assert (seen & SEEN_PID);
#endif

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

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
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");

  dbus_message_unref (m);
  m = NULL;

  if (dbus_pending_call_get_completed (pc))
532 533
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
534 535 536 537 538 539
                                          &m, NULL))
    g_error ("OOM");

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

540 541 542 543 544 545 546 547 548 549 550 551
  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,
552 553 554 555
        DBUS_TYPE_UINT32, &pid,
        DBUS_TYPE_INVALID))
    {
      g_assert_cmpstr (dbus_message_get_signature (m), ==, "u");
556
      test_assert_no_error (&error);
557

558
      g_test_message ("GetConnectionUnixProcessID returned %u", pid);
559 560

#ifdef G_OS_UNIX
561
      g_assert_cmpuint (pid, ==, getpid ());
562
#elif defined(G_OS_WIN32)
563
      g_assert_cmpuint (pid, ==, GetCurrentProcessId ());
564
#else
565
      g_assert_not_reached ();
566
#endif
567 568 569
    }
  else
    {
570
      g_error ("Unexpected error: %s: %s", error.name, error.message);
571
    }
572 573
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
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");

  dbus_message_unref (m);
  m = NULL;

  if (dbus_pending_call_get_completed (pc))
604 605
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
606 607 608 609 610 611 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 640 641
                                          &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);

  dbus_message_unref (m);

  /* 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");

  dbus_message_unref (m);
  m = NULL;

  if (dbus_pending_call_get_completed (pc))
642 643
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
                                          &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");

  dbus_message_unref (m);
}

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 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
#define DESIRED_RLIMIT 65536

#ifdef DBUS_UNIX
static void
test_fd_limit (Fixture *f,
               gconstpointer context)
{
#ifdef HAVE_PRLIMIT
  struct rlimit lim;
  const struct passwd *pwd = NULL;
#endif

  if (f->skip)
    return;

#ifdef HAVE_PRLIMIT

  if (getuid () != 0)
    {
      g_test_skip ("Cannot test, only uid 0 is expected to raise fd limit");
      return;
    }

  pwd = getpwnam (DBUS_USER);

  if (pwd == NULL)
    {
      gchar *message = g_strdup_printf ("user '%s' does not exist",
          DBUS_USER);

      g_test_skip (message);
      g_free (message);
      return;
    }

  if (prlimit (getpid (), RLIMIT_NOFILE, NULL, &lim) < 0)
    g_error ("prlimit(): %s", g_strerror (errno));

  g_test_message ("our RLIMIT_NOFILE: rlim_cur: %ld, rlim_max: %ld",
                  (long) lim.rlim_cur, (long) lim.rlim_max);

  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= DESIRED_RLIMIT)
    {
      /* The dbus-daemon will have inherited our large rlimit */
      g_test_skip ("Cannot test, our own fd limit was already large");
      return;
    }

  if (prlimit (f->daemon_pid, RLIMIT_NOFILE, NULL, &lim) < 0)
    g_error ("prlimit(): %s", g_strerror (errno));

  g_test_message ("dbus-daemon's RLIMIT_NOFILE: rlim_cur: %ld, rlim_max: %ld",
                  (long) lim.rlim_cur, (long) lim.rlim_max);

  if (lim.rlim_cur != RLIM_INFINITY)
    g_assert_cmpint (lim.rlim_cur, >=, DESIRED_RLIMIT);

#else /* !HAVE_PRLIMIT */

  g_test_skip ("prlimit() not supported on this platform");

#endif /* !HAVE_PRLIMIT */
}
#endif

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
static void
teardown (Fixture *f,
    gconstpointer context G_GNUC_UNUSED)
{
  dbus_error_free (&f->e);
  g_clear_error (&f->ge);

  if (f->left_conn != NULL)
    {
      dbus_connection_close (f->left_conn);
      dbus_connection_unref (f->left_conn);
      f->left_conn = NULL;
    }

  if (f->right_conn != NULL)
    {
      if (f->right_conn_echo)
        {
742
          dbus_connection_remove_filter (f->right_conn, echo_filter, f);
743 744 745 746 747 748 749 750
          f->right_conn_echo = FALSE;
        }

      dbus_connection_close (f->right_conn);
      dbus_connection_unref (f->right_conn);
      f->right_conn = NULL;
    }

751 752
  if (f->daemon_pid != 0)
    {
753
      test_kill_pid (f->daemon_pid);
754 755 756
      g_spawn_close_pid (f->daemon_pid);
      f->daemon_pid = 0;
    }
757

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  if (f->tmp_runtime_dir != NULL)
    {
      gchar *path;

      /* the socket may exist */
      path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir);
      g_assert (g_remove (path) == 0 || errno == ENOENT);
      g_free (path);
      /* there shouldn't be anything else in there */
      g_assert_cmpint (g_rmdir (f->tmp_runtime_dir), ==, 0);

      /* we're relying on being single-threaded for this to be safe */
      if (f->saved_runtime_dir != NULL)
        g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE);
      else
        g_unsetenv ("XDG_RUNTIME_DIR");
      g_free (f->saved_runtime_dir);
      g_free (f->tmp_runtime_dir);
    }

778
  test_main_context_unref (f->ctx);
779 780
}

781
static Config limited_config = {
782
    "34393", 10000, "valid-config-files/incoming-limit.conf",
783
    TEST_USER_ME, SPECIFY_ADDRESS
784 785
};

786
static Config finite_timeout_config = {
787
    NULL, 1, "valid-config-files/finite-timeout.conf",
788
    TEST_USER_ME, SPECIFY_ADDRESS
789 790
};

791 792 793
#ifdef DBUS_UNIX
static Config listen_unix_runtime_config = {
    "61303", 1, "valid-config-files/listen-unix-runtime.conf",
794 795 796 797 798 799 800 801
    TEST_USER_ME, RELY_ON_DEFAULT
};

static Config as_another_user_config = {
    NULL, 1, "valid-config-files/as-another-user.conf",
    /* We start the dbus-daemon as root and drop privileges, like the
     * real system bus does */
    TEST_USER_ROOT, SPECIFY_ADDRESS
802 803 804
};
#endif

805 806 807 808
int
main (int argc,
    char **argv)
{
809
  test_init (&argc, &argv);
810 811

  g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
812 813
  g_test_add ("/echo/limited", Fixture, &limited_config,
      setup, test_echo, teardown);
814 815 816 817
  g_test_add ("/no-reply/disconnect", Fixture, NULL,
      setup, test_no_reply, teardown);
  g_test_add ("/no-reply/timeout", Fixture, &finite_timeout_config,
      setup, test_no_reply, teardown);
818
  g_test_add ("/creds", Fixture, NULL, setup, test_creds, teardown);
819
  g_test_add ("/processid", Fixture, NULL, setup, test_processid, teardown);
820 821
  g_test_add ("/canonical-path/uae", Fixture, NULL,
      setup, test_canonical_path_uae, teardown);
822 823 824 825 826 827
#ifdef DBUS_UNIX
  /* We can't test this in loopback.c with the rest of unix:runtime=yes,
   * because dbus_bus_get[_private] is the only way to use the default,
   * and that blocks on a round-trip to the dbus-daemon */
  g_test_add ("/unix-runtime-is-default", Fixture, &listen_unix_runtime_config,
      setup, test_echo, teardown);
828 829 830 831 832

  g_test_add ("/fd-limit/session", Fixture, NULL,
              setup, test_fd_limit, teardown);
  g_test_add ("/fd-limit/system", Fixture, &as_another_user_config,
              setup, test_fd_limit, teardown);
833
#endif
834 835 836

  return g_test_run ();
}