Commit 104805f0 authored by Ralf Habacker's avatar Ralf Habacker

Add a test application for autostart on Windows implementation

parent 4108f508
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<type>session</type>
<listen>@TEST_LISTEN@</listen>
<policy context="default">
<allow send_destination="*" eavesdrop="true"/>
<allow eavesdrop="true"/>
<allow own="*"/>
</policy>
</busconfig>
......@@ -2,7 +2,11 @@ if(DBUS_ENABLE_EMBEDDED_TESTS)
add_definitions(${DBUS_INTERNAL_CLIENT_DEFINITIONS})
add_helper_executable(test-autolaunch test-autolaunch.c dbus-testutils)
if(WIN32)
add_test_executable(test-autolaunch-win test-autolaunch-win.c ${DBUS_INTERNAL_LIBRARIES} dbus-testutils)
else()
add_helper_executable(test-autolaunch test-autolaunch.c dbus-testutils)
endif()
add_helper_executable(test-privserver-client test-privserver-client.c dbus-testutils)
add_session_test_executable(test-ids test-ids.c ${DBUS_INTERNAL_LIBRARIES})
......
......@@ -72,7 +72,7 @@ if DBUS_ENABLE_EMBEDDED_TESTS
## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we
## build even when not doing "make check"
noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-pending-call-disconnected test-threads-init test-ids test-shutdown test-privserver-client test-autolaunch
noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-pending-call-disconnected test-threads-init test-ids test-shutdown test-privserver-client
test_pending_call_dispatch_LDADD = \
$(CODE_COVERAGE_LIBS) \
......@@ -103,9 +103,21 @@ test_privserver_client_LDADD = \
$(CODE_COVERAGE_LIBS) \
../libdbus-testutils.la \
$(NULL)
if DBUS_WIN
noinst_PROGRAMS += test-autolaunch-win
test_autolaunch_win_SOURCES = test-autolaunch-win.c
test_autolaunch_win_LDADD = \
$(CODE_COVERAGE_LIBS) \
../libdbus-testutils.la \
$(NULL)
TESTS += test-autolaunch-win
else
noinst_PROGRAMS += test-autolaunch
test_autolaunch_SOURCES = test-autolaunch.c
test_autolaunch_LDADD = \
$(CODE_COVERAGE_LIBS) \
../libdbus-testutils.la \
$(NULL)
endif
endif
/*
* Copyright © 2018-2019 Ralf Habacker <ralf.habacker@freenet.de>
*
* 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.
*/
/**
* This test checks whether a client can connect to a dbus daemon configured
* for a default, user-defined and installation path related autostart and
* whether it can connect to a server having a different autolaunch
* configuration.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <dbus/dbus.h>
#include "dbus/dbus-file.h"
#include "dbus/dbus-internals.h"
#include "dbus/dbus-sysdeps.h"
#include "dbus/dbus-test.h"
#include "dbus/dbus-test-tap.h"
#include "test/test-utils.h"
/* dbus_bus_get does not work yet */
static dbus_bool_t use_bus_get = FALSE;
static int add_wait_time = 0;
#define oom() _dbus_test_fatal ("Out of memory")
/**
* helper function
*/
#define _dbus_error_set_from_message_with_location(a, b) __dbus_error_set_from_message_with_location (__FILE__, __LINE__, __FUNCTION__, a, b)
static void __dbus_error_set_from_message_with_location (const char *file, int line, const char *function, DBusError *error, DBusMessage *message)
{
char * str = NULL;
dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID);
dbus_set_error (error, dbus_message_get_error_name (message), "[%s(%d):%s] %s", file, line, function, str ? str : "");
}
static dbus_bool_t
_send_message (DBusConnection *conn,
DBusError *error,
int timeout,
const char *interface,
const char *method_str)
{
DBusMessage *method;
DBusMessage *reply;
dbus_bool_t result = TRUE;
dbus_error_init (error);
method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
interface,
method_str);
reply = dbus_connection_send_with_reply_and_block (conn, method, timeout, error);
dbus_message_unref (method);
if (reply == NULL)
{
dbus_set_error (error, DBUS_ERROR_FAILED, "Got no reply");
result = FALSE;
goto out;
}
if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
{
if (strcmp (dbus_message_get_error_name (reply), DBUS_ERROR_DISCONNECTED) == 0)
{
_dbus_error_set_from_message_with_location (error, reply);
result = FALSE;
goto out;
}
else
{
_dbus_error_set_from_message_with_location (error, reply);
result = FALSE;
goto out;
}
}
result = TRUE;
out:
_DBUS_ASSERT_ERROR_XOR_BOOL(error, result);
dbus_message_unref (reply);
return result;
}
static dbus_bool_t
_server_check_connection (DBusConnection *conn,
DBusError *error)
{
if (use_bus_get)
return _send_message (conn, error, -1, DBUS_INTERFACE_PEER, "GetMachineId");
else
return _send_message (conn, error, -1, DBUS_INTERFACE_DBUS, "Hello");
}
static dbus_bool_t
_server_shutdown (DBusConnection *conn,
const char *scope,
int timeout,
DBusError *error)
{
return _send_message (conn, error, timeout, DBUS_INTERFACE_EMBEDDED_TESTS, "Shutdown");
}
typedef enum {
RUN_TEST_DEFAULT = 0,
RUN_TEST_EXPECT_CONNECTION_TO_FAIL = 1,
RUN_TEST_EXPECT_SERVER_API_TO_FAIL = 2,
RUN_TEST_EXPECT_RESULT_TO_FAIL = 4,
} RunTestFlags;
static
dbus_bool_t check_results (DBusConnection *conn,
DBusString *address,
const char *scope,
RunTestFlags flags,
DBusError *error)
{
if (add_wait_time)
_dbus_sleep_milliseconds (add_wait_time);
_dbus_test_diag ("%sconnection for address %s", conn ? "" : "no ", _dbus_string_get_const_data (address));
if (dbus_error_is_set (error) && conn == NULL)
{
return flags & RUN_TEST_EXPECT_CONNECTION_TO_FAIL;
}
if (!dbus_error_is_set (error) && conn == NULL)
{
_dbus_test_fatal ("Failed to autolaunch session bus and no error was set");
}
if (add_wait_time)
_dbus_sleep_milliseconds (add_wait_time);
if (!_server_check_connection (conn, error))
{
return FALSE;
}
_dbus_test_diag ("client uses %s", _dbus_string_get_const_data (address));
if (!_server_shutdown (conn, scope, -1, error))
{
return FALSE;
}
_dbus_test_diag ("server has been shut down");
return TRUE;
}
static dbus_bool_t
run_test (const char *scope, const char *test_data_dir, RunTestFlags flags)
{
DBusConnection *conn = NULL;
DBusError error;
DBusString address;
DBusString session_parameter;
dbus_bool_t result = FALSE;
dbus_error_init (&error);
if (!_dbus_string_init (&address))
oom();
_dbus_test_diag ("run test");
if (*scope != '\0')
{
if (!_dbus_string_append_printf (&address, "autolaunch:scope=%s", scope))
oom();
}
else if (!_dbus_string_append_printf (&address, "autolaunch:"))
oom();
if (!_dbus_string_init (&session_parameter))
oom();
_dbus_test_check (strchr (test_data_dir, '"') == NULL);
_dbus_test_check (strchr (_dbus_string_get_const_data (&address), '"') == NULL);
if (!_dbus_string_append_printf (&session_parameter, "\"--config-file=%s/%s\" \"--address=%s\"", test_data_dir, "valid-config-files/listen-autolaunch-win.conf", _dbus_string_get_const_data (&address)))
oom();
_dbus_win_set_autolaunch_command_line_parameter (_dbus_string_get_const_data (&session_parameter));
if (use_bus_get)
{
dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", _dbus_string_get_const_data (&address));
_dbus_test_diag ("got env %s", getenv ("DBUS_SESSION_BUS_ADDRESS"));
conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
dbus_connection_set_exit_on_disconnect (conn, FALSE);
}
else
conn = dbus_connection_open_private (_dbus_string_get_const_data( &address), &error);
result = check_results (conn, &address, scope, flags, &error);
if (use_bus_get)
dbus_shutdown ();
else if (conn)
{
dbus_connection_close (conn);
dbus_connection_unref (conn);
}
_dbus_string_free (&address);
_dbus_string_free (&session_parameter);
return result;
}
static dbus_bool_t
run_test_okay (const char *scope, const char *test_data_dir)
{
return run_test (scope, test_data_dir, RUN_TEST_DEFAULT);
}
static dbus_bool_t
run_test_should_fail (const char *scope, const char *test_data_dir)
{
return run_test (scope, test_data_dir, RUN_TEST_EXPECT_CONNECTION_TO_FAIL);
}
static dbus_bool_t
_dbus_autolaunch_default_test (const char *test_data_dir)
{
return run_test_okay ("", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_default_mismatch_test (const char *test_data_dir)
{
return run_test_should_fail ("123", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_custom_scope_test (const char *test_data_dir)
{
return run_test_okay ("123", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_custom_scope_mismatch_test (const char *test_data_dir)
{
return run_test_should_fail ("1234", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_install_path_scope_test (const char *test_data_dir)
{
return run_test_okay ("*install-path", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_install_path_scope_mismatch_test (const char *test_data_dir)
{
return run_test_should_fail ("*install-path", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_install_path_scope_mismatch2_test (const char *test_data_dir)
{
return run_test_should_fail ("1235", test_data_dir);
}
static dbus_bool_t
_dbus_autolaunch_loop_test (const char *test_data_dir)
{
int i;
int max = 10;
for (i = 0; i < max; i++) {
if (!run_test_okay ("", test_data_dir))
_dbus_test_not_ok ("%d", max);
else
_dbus_test_ok ("%d", max);
if (add_wait_time)
_dbus_sleep_milliseconds (add_wait_time);
}
return TRUE;
}
static DBusTestCase tests[] =
{
{ "default", _dbus_autolaunch_default_test },
{ "default mismatch", _dbus_autolaunch_default_mismatch_test },
{ "custom scope", _dbus_autolaunch_custom_scope_test },
{ "custom scope mismatch", _dbus_autolaunch_custom_scope_mismatch_test },
{ "install path scope", _dbus_autolaunch_install_path_scope_test },
{ "install path scope mismatch", _dbus_autolaunch_install_path_scope_mismatch_test },
{ "install path scope mismatch 2", _dbus_autolaunch_install_path_scope_mismatch2_test },
{ "loop", _dbus_autolaunch_loop_test },
{ NULL }
};
int
main (int argc,
char **argv)
{
return _dbus_test_main (argc, argv, _DBUS_N_ELEMENTS (tests), tests,
/*DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS*/0,
NULL, NULL);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment