diff options
Diffstat (limited to 'tests/lib/util.c')
-rw-r--r-- | tests/lib/util.c | 340 |
1 files changed, 273 insertions, 67 deletions
diff --git a/tests/lib/util.c b/tests/lib/util.c index b58227e..1e8c402 100644 --- a/tests/lib/util.c +++ b/tests/lib/util.c @@ -12,11 +12,21 @@ #include "util.h" +#include <telepathy-glib/connection.h> + +#include <glib/gstdio.h> +#include <string.h> #include <stdlib.h> + #ifdef G_OS_UNIX # include <unistd.h> /* for alarm() */ #endif +#ifdef HAVE_GIO_UNIX +#include <gio/gunixsocketaddress.h> +#include <gio/gunixconnection.h> +#endif + void tp_tests_proxy_run_until_prepared (gpointer proxy, const GQuark *features) @@ -105,50 +115,6 @@ tp_tests_proxy_run_until_dbus_queue_processed (gpointer proxy) g_main_loop_unref (loop); } -typedef struct { - GMainLoop *loop; - TpHandle handle; -} HandleRequestResult; - -static void -handles_requested_cb (TpConnection *connection G_GNUC_UNUSED, - TpHandleType handle_type G_GNUC_UNUSED, - guint n_handles, - const TpHandle *handles, - const gchar * const *ids G_GNUC_UNUSED, - const GError *error, - gpointer user_data, - GObject *weak_object G_GNUC_UNUSED) -{ - HandleRequestResult *result = user_data; - - g_assert_no_error ((GError *) error); - g_assert_cmpuint (n_handles, ==, 1); - result->handle = handles[0]; -} - -static void -handle_request_result_finish (gpointer r) -{ - HandleRequestResult *result = r; - - g_main_loop_quit (result->loop); -} - -TpHandle -tp_tests_connection_run_request_contact_handle (TpConnection *connection, - const gchar *id) -{ - HandleRequestResult result = { g_main_loop_new (NULL, FALSE), 0 }; - const gchar * const ids[] = { id, NULL }; - - tp_connection_request_handles (connection, -1, TP_HANDLE_TYPE_CONTACT, ids, - handles_requested_cb, &result, handle_request_result_finish, NULL); - g_main_loop_run (result.loop); - g_main_loop_unref (result.loop); - return result.handle; -} - void _test_assert_empty_strv (const char *file, int line, @@ -211,41 +177,43 @@ _tp_tests_assert_strv_equals (const char *file, } void -tp_tests_copy_dir (const gchar *from_dir, const gchar *to_dir) +_tp_tests_assert_bytes_equal (const gchar *file, int line, + GBytes *actual, gconstpointer expected_data, + gsize expected_length) { - gchar *command; - - // If destination directory exist erase it - command = g_strdup_printf ("rm -rf %s", to_dir); - g_assert (system (command) == 0); - g_free (command); - - command = g_strdup_printf ("cp -r %s %s", from_dir, to_dir); - g_assert (system (command) == 0); - g_free (command); - - // In distcheck mode the files and directory are read-only, fix that - command = g_strdup_printf ("chmod -R +w %s", to_dir); - g_assert (system (command) == 0); - g_free (command); + if (expected_length != g_bytes_get_size (actual)) + { + g_error ("%s:%d: assertion failed: expected %"G_GSIZE_FORMAT + " bytes, got %"G_GSIZE_FORMAT, + file, line, expected_length, g_bytes_get_size (actual)); + } + else if (memcmp (g_bytes_get_data (actual, NULL), + expected_data, expected_length) != 0) + { + g_error ( + "%s:%d: assertion failed: expected data didn't match the actual data", + file, line); + } } void -tp_tests_create_and_connect_conn (GType conn_type, +tp_tests_create_conn (GType conn_type, const gchar *account, + gboolean connect, TpBaseConnection **service_conn, TpConnection **client_conn) { TpDBusDaemon *dbus; + TpSimpleClientFactory *factory; gchar *name; gchar *conn_path; GError *error = NULL; - GQuark conn_features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 }; g_assert (service_conn != NULL); g_assert (client_conn != NULL); dbus = tp_tests_dbus_daemon_dup_or_die (); + factory = (TpSimpleClientFactory *) tp_automatic_client_factory_new (dbus); *service_conn = tp_tests_object_new_static_class ( conn_type, @@ -258,18 +226,33 @@ tp_tests_create_and_connect_conn (GType conn_type, &name, &conn_path, &error)); g_assert_no_error (error); - *client_conn = tp_connection_new (dbus, name, conn_path, - &error); + *client_conn = tp_simple_client_factory_ensure_connection (factory, + conn_path, NULL, &error); g_assert (*client_conn != NULL); g_assert_no_error (error); - tp_cli_connection_call_connect (*client_conn, -1, NULL, NULL, NULL, NULL); - tp_tests_proxy_run_until_prepared (*client_conn, conn_features); + if (connect) + { + GQuark conn_features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 }; + + tp_cli_connection_call_connect (*client_conn, -1, NULL, NULL, NULL, NULL); + tp_tests_proxy_run_until_prepared (*client_conn, conn_features); + } g_free (name); g_free (conn_path); g_object_unref (dbus); + g_object_unref (factory); +} + +void +tp_tests_create_and_connect_conn (GType conn_type, + const gchar *account, + TpBaseConnection **service_conn, + TpConnection **client_conn) +{ + tp_tests_create_conn (conn_type, account, TRUE, service_conn, client_conn); } /* This object exists solely so that tests/tests.supp can ignore "leaked" @@ -300,7 +283,27 @@ time_out (gpointer nil G_GNUC_UNUSED) void tp_tests_abort_after (guint sec) { - if (g_getenv ("TP_TESTS_NO_TIMEOUT") != NULL) + gboolean debugger = FALSE; + gchar *contents; + + if (g_file_get_contents ("/proc/self/status", &contents, NULL, NULL)) + { +/* http://www.youtube.com/watch?v=SXmv8quf_xM */ +#define TRACER_T "\nTracerPid:\t" + gchar *line = strstr (contents, TRACER_T); + + if (line != NULL) + { + gchar *value = line + strlen (TRACER_T); + + if (value[0] != '0' || value[1] != '\n') + debugger = TRUE; + } + + g_free (contents); + } + + if (g_getenv ("TP_TESTS_NO_TIMEOUT") != NULL || debugger) return; g_timeout_add_seconds (sec, time_out, NULL); @@ -312,3 +315,206 @@ tp_tests_abort_after (guint sec) alarm (sec + 2); #endif } + +void +tp_tests_init (int *argc, + char ***argv) +{ + g_type_init (); + tp_tests_abort_after (10); + tp_debug_set_flags ("all"); + + g_test_init (argc, argv, NULL); +} + +void +_tp_destroy_socket_control_list (gpointer data) +{ + GArray *tab = data; + g_array_unref (tab); +} + +GValue * +_tp_create_local_socket (TpSocketAddressType address_type, + TpSocketAccessControl access_control, + GSocketService **service, + gchar **unix_address, + gchar **unix_tmpdir, + GError **error) +{ + gboolean success; + GSocketAddress *address, *effective_address; + GValue *address_gvalue; + + g_assert (service != NULL); + g_assert (unix_address != NULL); + + switch (access_control) + { + case TP_SOCKET_ACCESS_CONTROL_LOCALHOST: + case TP_SOCKET_ACCESS_CONTROL_CREDENTIALS: + case TP_SOCKET_ACCESS_CONTROL_PORT: + break; + + default: + g_assert_not_reached (); + } + + switch (address_type) + { +#ifdef HAVE_GIO_UNIX + case TP_SOCKET_ADDRESS_TYPE_UNIX: + { + GError *e = NULL; + gchar *dir = g_dir_make_tmp ("tp-glib-tests.XXXXXX", &e); + gchar *name; + + g_assert_no_error (e); + + name = g_build_filename (dir, "s", NULL); + address = g_unix_socket_address_new (name); + g_free (name); + + if (unix_tmpdir != NULL) + *unix_tmpdir = dir; + else + g_free (dir); + break; + } +#endif + + case TP_SOCKET_ADDRESS_TYPE_IPV4: + case TP_SOCKET_ADDRESS_TYPE_IPV6: + { + GInetAddress *localhost; + + localhost = g_inet_address_new_loopback ( + address_type == TP_SOCKET_ADDRESS_TYPE_IPV4 ? + G_SOCKET_FAMILY_IPV4 : G_SOCKET_FAMILY_IPV6); + address = g_inet_socket_address_new (localhost, 0); + + g_object_unref (localhost); + break; + } + + default: + g_assert_not_reached (); + } + + *service = g_socket_service_new (); + + success = g_socket_listener_add_address ( + G_SOCKET_LISTENER (*service), + address, G_SOCKET_TYPE_STREAM, + G_SOCKET_PROTOCOL_DEFAULT, + NULL, &effective_address, NULL); + g_assert (success); + + switch (address_type) + { +#ifdef HAVE_GIO_UNIX + case TP_SOCKET_ADDRESS_TYPE_UNIX: + *unix_address = g_strdup (g_unix_socket_address_get_path ( + G_UNIX_SOCKET_ADDRESS (effective_address))); + address_gvalue = tp_g_value_slice_new_bytes ( + g_unix_socket_address_get_path_len ( + G_UNIX_SOCKET_ADDRESS (effective_address)), + g_unix_socket_address_get_path ( + G_UNIX_SOCKET_ADDRESS (effective_address))); + break; +#endif + + case TP_SOCKET_ADDRESS_TYPE_IPV4: + case TP_SOCKET_ADDRESS_TYPE_IPV6: + *unix_address = NULL; + + address_gvalue = tp_g_value_slice_new_take_boxed ( + TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4, + dbus_g_type_specialized_construct ( + TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)); + + dbus_g_type_struct_set (address_gvalue, + 0, address_type == TP_SOCKET_ADDRESS_TYPE_IPV4 ? + "127.0.0.1" : "::1", + 1, g_inet_socket_address_get_port ( + G_INET_SOCKET_ADDRESS (effective_address)), + G_MAXUINT); + break; + + default: + g_assert_not_reached (); + } + + g_object_unref (address); + g_object_unref (effective_address); + return address_gvalue; +} + +void +tp_tests_connection_assert_disconnect_succeeds (TpConnection *connection) +{ + GAsyncResult *result = NULL; + GError *error = NULL; + gboolean ok; + + tp_connection_disconnect_async (connection, tp_tests_result_ready_cb, + &result); + tp_tests_run_until_result (&result); + ok = tp_connection_disconnect_finish (connection, result, &error); + g_assert_no_error (error); + g_assert (ok); + g_object_unref (result); +} + +static void +one_contact_cb (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + TpConnection *connection = (TpConnection *) object; + TpContact **contact_loc = user_data; + GError *error = NULL; + + *contact_loc = tp_connection_dup_contact_by_id_finish (connection, result, + &error); + + g_assert_no_error (error); + g_assert (TP_IS_CONTACT (*contact_loc)); +} + +TpContact * +tp_tests_connection_run_until_contact_by_id (TpConnection *connection, + const gchar *id, + guint n_features, + const TpContactFeature *features) +{ + TpContact *contact = NULL; + + tp_connection_dup_contact_by_id_async (connection, id, n_features, features, + one_contact_cb, &contact); + + while (contact == NULL) + g_main_context_iteration (NULL, TRUE); + + return contact; +} + +void +tp_tests_copy_dir (const gchar *from_dir, const gchar *to_dir) +{ + gchar *command; + + // If destination directory exist erase it + command = g_strdup_printf ("rm -rf %s", to_dir); + g_assert (system (command) == 0); + g_free (command); + + command = g_strdup_printf ("cp -r %s %s", from_dir, to_dir); + g_assert (system (command) == 0); + g_free (command); + + // In distcheck mode the files and directory are read-only, fix that + command = g_strdup_printf ("chmod -R +w %s", to_dir); + g_assert (system (command) == 0); + g_free (command); +} |