#include <config.h>
#include "dispatch.h"
+#include "check.h"
#include "connection.h"
#include "driver.h"
#include "services.h"
#include "signals.h"
#include "test.h"
#include <dbus/dbus-internals.h>
+#include <dbus/dbus-connection-internal.h>
#include <dbus/dbus-misc.h>
#include <string.h>
return TRUE;
}
+typedef struct {
+ DBusTimeout *timeout;
+ DBusConnection *connection;
+ dbus_bool_t timedout;
+ int check_counter;
+} BusTestCheckData;
+
+static BusTestCheckData *cdata;
+
+static dbus_bool_t
+bus_dispatch_test_check_timeout (void *data)
+{
+ _dbus_verbose ("timeout triggered - pretend that privilege check result is available");
+
+ /* should only happen once during the test */
+ _dbus_assert (!cdata->timedout);
+ cdata->timedout = TRUE;
+ _dbus_connection_enable_dispatch (cdata->connection);
+
+ /* don't call this again */
+ _dbus_loop_remove_timeout (bus_connection_get_loop (cdata->connection),
+ cdata->timeout);
+ dbus_connection_unref (cdata->connection);
+ cdata->connection = NULL;
+ return TRUE;
+}
+
+static dbus_bool_t
+bus_dispatch_test_check_override (DBusConnection *connection,
+ const char *privilege)
+{
+ _dbus_verbose ("overriding privilege check %s #%d", privilege, cdata->check_counter);
+ cdata->check_counter++;
+ if (!cdata->timedout)
+ {
+ dbus_bool_t added;
+
+ /* Should be the first privilege check for the "Echo" method. */
+ _dbus_assert (cdata->check_counter == 1);
+ cdata->timeout = _dbus_timeout_new (1, bus_dispatch_test_check_timeout,
+ NULL, NULL);
+ _dbus_assert (cdata->timeout);
+ added = _dbus_loop_add_timeout (bus_connection_get_loop (connection),
+ cdata->timeout);
+ _dbus_assert (added);
+ cdata->connection = connection;
+ dbus_connection_ref (connection);
+ _dbus_connection_disable_dispatch (connection);
+ return BUS_RESULT_LATER;
+ }
+ else
+ {
+ /* Should only be checked one more time, and this time succeeds. */
+ _dbus_assert (cdata->check_counter == 2);
+ return BUS_RESULT_TRUE;
+ }
+}
+
+static dbus_bool_t
+bus_dispatch_test_check (const DBusString *test_data_dir)
+{
+ const char *filename = "valid-config-files/debug-check-some.conf";
+ BusContext *context;
+ DBusConnection *foo;
+ DBusError error;
+ dbus_bool_t result = TRUE;
+ BusTestCheckData data;
+
+ /* save the config name for the activation helper */
+ if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
+ _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
+
+ dbus_error_init (&error);
+
+ context = bus_context_new_test (test_data_dir, filename);
+ if (context == NULL)
+ return FALSE;
+
+ foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
+ if (foo == NULL)
+ _dbus_assert_not_reached ("could not alloc connection");
+
+ if (!bus_setup_debug_client (foo))
+ _dbus_assert_not_reached ("could not set up connection");
+
+ spin_connection_until_authenticated (context, foo);
+
+ if (!check_hello_message (context, foo))
+ _dbus_assert_not_reached ("hello message failed");
+
+ if (!check_double_hello_message (context, foo))
+ _dbus_assert_not_reached ("double hello message failed");
+
+ if (!check_add_match_all (context, foo))
+ _dbus_assert_not_reached ("AddMatch message failed");
+
+ /*
+ * Cause bus_check_privilege() to return BUS_RESULT_LATER in the
+ * first call, then BUS_RESULT_TRUE.
+ */
+ cdata = &data;
+ memset (cdata, 0, sizeof(*cdata));
+ bus_check_test_override = bus_dispatch_test_check_override;
+
+ result = check_existent_service_auto_start (context, foo);
+
+ _dbus_assert (cdata->check_counter == 2);
+ _dbus_assert (cdata->timedout);
+ _dbus_assert (cdata->timeout);
+ _dbus_assert (!cdata->connection);
+ _dbus_timeout_unref (cdata->timeout);
+
+ kill_client_connection_unchecked (foo);
+
+ bus_context_unref (context);
+
+ return result;
+}
+
dbus_bool_t
bus_dispatch_test (const DBusString *test_data_dir)
{
+ _dbus_verbose ("<check> tests\n");
+ if (!bus_dispatch_test_check (test_data_dir))
+ return FALSE;
+
/* run normal activation tests */
_dbus_verbose ("Normal activation tests\n");
if (!bus_dispatch_test_conf (test_data_dir,