summaryrefslogtreecommitdiff
path: root/src/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test.c')
-rw-r--r--src/test.c213
1 files changed, 211 insertions, 2 deletions
diff --git a/src/test.c b/src/test.c
index f0416fb..0b14ed0 100644
--- a/src/test.c
+++ b/src/test.c
@@ -23,6 +23,7 @@
#include "config.h"
#include <string.h>
+#include <stdio.h>
#include "test-generated.h"
@@ -85,8 +86,6 @@ check_bar_introspection_data (void)
/* ---------------------------------------------------------------------------------------------------- */
-//#define FOO_BAR_HANDLE_HELLO_WORLD_CALLBACK(f, user_type) (__builtin_choose_expr (__builtin_types_compatible_p (typeof (&f), gboolean (*)(FooBar *, GDBusMethodInvocation*, const gchar *, user_type)), G_CALLBACK (f), f))
-
static gboolean
on_handle_hello_world (FooBar *object,
GDBusMethodInvocation *invocation,
@@ -271,8 +270,81 @@ on_handle_force_method (FooBat *object,
/* ---------------------------------------------------------------------------------------------------- */
+static gboolean
+my_g_authorize_method_handler (GDBusInterfaceStub *interface,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ const gchar *method_name;
+ gboolean authorized;
+
+ authorized = FALSE;
+
+ method_name = g_dbus_method_invocation_get_method_name (invocation);
+ if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
+ {
+ authorized = FALSE;
+ }
+ else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
+ {
+ authorized = TRUE;
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ if (!authorized)
+ {
+ g_dbus_method_invocation_return_error (invocation,
+ G_IO_ERROR,
+ G_IO_ERROR_PERMISSION_DENIED,
+ "not authorized...");
+ }
+ return authorized;
+}
+
+static gboolean
+on_handle_check_not_authorized (FooAuthorize *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ foo_authorize_complete_check_not_authorized (object, invocation);
+ return TRUE;
+}
+
+static gboolean
+on_handle_check_authorized (FooAuthorize *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ foo_authorize_complete_check_authorized (object, invocation);
+ return TRUE;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static gboolean
+on_handle_get_self (FooMethodThreads *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ gchar *s;
+ s = g_strdup_printf ("%p", g_thread_self ());
+ foo_method_threads_complete_get_self (object, invocation, s);
+ g_free (s);
+ return TRUE;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static GThread *method_handler_thread = NULL;
+
static FooBar *exported_bar_object = NULL;
static FooBat *exported_bat_object = NULL;
+static FooAuthorize *exported_authorize_object = NULL;
+static FooMethodThreads *exported_thread_object_1 = NULL;
+static FooMethodThreads *exported_thread_object_2 = NULL;
static void
on_bus_acquired (GDBusConnection *connection,
@@ -347,6 +419,53 @@ on_bus_acquired (GDBusConnection *connection,
"force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
"force-struct", g_variant_new ("(i)", 4300),
NULL);
+
+ exported_authorize_object = foo_authorize_stub_new ();
+ g_dbus_interface_stub_export (G_DBUS_INTERFACE_STUB (exported_authorize_object),
+ connection,
+ "/authorize",
+ &error);
+ g_assert_no_error (error);
+ g_signal_connect (exported_authorize_object,
+ "g-authorize-method",
+ G_CALLBACK (my_g_authorize_method_handler),
+ NULL);
+ g_signal_connect (exported_authorize_object,
+ "handle-check-not-authorized",
+ FOO_AUTHORIZE_HANDLE_CHECK_NOT_AUTHORIZED_CALLBACK (on_handle_check_not_authorized, gpointer),
+ NULL);
+ g_signal_connect (exported_authorize_object,
+ "handle-check-authorized",
+ FOO_AUTHORIZE_HANDLE_CHECK_AUTHORIZED_CALLBACK (on_handle_check_authorized, gpointer),
+ NULL);
+
+
+ /* only object 1 has the G_DBUS_INTERFACE_STUB_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
+ exported_thread_object_1 = foo_method_threads_stub_new ();
+ g_dbus_interface_stub_set_flags (G_DBUS_INTERFACE_STUB (exported_thread_object_1),
+ G_DBUS_INTERFACE_STUB_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
+ g_dbus_interface_stub_export (G_DBUS_INTERFACE_STUB (exported_thread_object_1),
+ connection,
+ "/method_threads_1",
+ &error);
+ g_assert_no_error (error);
+ g_signal_connect (exported_thread_object_1,
+ "handle-get-self",
+ FOO_METHOD_THREADS_HANDLE_GET_SELF_CALLBACK (on_handle_get_self, gpointer),
+ NULL);
+
+ exported_thread_object_2 = foo_method_threads_stub_new ();
+ g_dbus_interface_stub_export (G_DBUS_INTERFACE_STUB (exported_thread_object_2),
+ connection,
+ "/method_threads_2",
+ &error);
+ g_assert_no_error (error);
+ g_signal_connect (exported_thread_object_2,
+ "handle-get-self",
+ FOO_METHOD_THREADS_HANDLE_GET_SELF_CALLBACK (on_handle_get_self, gpointer),
+ NULL);
+
+ method_handler_thread = g_thread_self ();
}
static gpointer check_proxies_in_thread (gpointer user_data);
@@ -866,6 +985,63 @@ check_bat_proxy (FooBat *proxy,
/* ---------------------------------------------------------------------------------------------------- */
+static void
+check_authorize_proxy (FooAuthorize *proxy,
+ GMainLoop *thread_loop)
+{
+ GError *error;
+ gboolean ret;
+
+ /* Check that g-authorize-method works as intended */
+
+ error = NULL;
+ ret = foo_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
+ g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
+ g_error_free (error);
+ g_assert (!ret);
+
+ error = NULL;
+ ret = foo_authorize_call_check_authorized_sync (proxy, NULL, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static GThread *
+get_self_via_proxy (FooMethodThreads *proxy_1)
+{
+ GError *error;
+ gchar *self_str;
+ gboolean ret;
+ gpointer self;
+
+ error = NULL;
+ ret = foo_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+
+ g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
+
+ g_free (self_str);
+
+ return self;
+}
+
+static void
+check_thread_proxies (FooMethodThreads *proxy_1,
+ FooMethodThreads *proxy_2,
+ GMainLoop *thread_loop)
+{
+ /* proxy_1 is indeed using threads so should never get the handler thread */
+ g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
+
+ /* proxy_2 is not using threads so should get the handler thread */
+ g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
static gpointer
check_proxies_in_thread (gpointer user_data)
{
@@ -875,6 +1051,9 @@ check_proxies_in_thread (gpointer user_data)
GError *error;
FooBar *bar_proxy;
FooBat *bat_proxy;
+ FooAuthorize *authorize_proxy;
+ FooMethodThreads *thread_proxy_1;
+ FooMethodThreads *thread_proxy_2;
thread_context = g_main_context_new ();
thread_loop = g_main_loop_new (thread_context, FALSE);
@@ -903,6 +1082,36 @@ check_proxies_in_thread (gpointer user_data)
g_assert_no_error (error);
g_object_unref (bat_proxy);
+ error = NULL;
+ authorize_proxy = foo_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ "org.gtk.GDBus.BindingsTool.Test",
+ "/authorize",
+ NULL, /* GCancellable* */
+ &error);
+ check_authorize_proxy (authorize_proxy, thread_loop);
+ g_assert_no_error (error);
+ g_object_unref (authorize_proxy);
+
+ error = NULL;
+ thread_proxy_1 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ "org.gtk.GDBus.BindingsTool.Test",
+ "/method_threads_1",
+ NULL, /* GCancellable* */
+ &error);
+ g_assert_no_error (error);
+ thread_proxy_2 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ "org.gtk.GDBus.BindingsTool.Test",
+ "/method_threads_2",
+ NULL, /* GCancellable* */
+ &error);
+ g_assert_no_error (error);
+ check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
+ g_object_unref (thread_proxy_1);
+ g_object_unref (thread_proxy_2);
+
g_main_loop_unref (thread_loop);
g_main_context_unref (thread_context);