Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2019 Canonical Limited
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General
17 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : * Authors: James Henstridge <james.henstridge@canonical.com>
20 : : */
21 : :
22 : : /* A stub implementation of xdg-document-portal covering enough to
23 : : * support g_document_portal_add_documents */
24 : :
25 : : #include <glib.h>
26 : : #include <glib/glib-unix.h>
27 : : #include <gio/gio.h>
28 : : #include <gio/gunixfdlist.h>
29 : :
30 : : #include "fake-document-portal.h"
31 : : #include "fake-document-portal-generated.h"
32 : :
33 : : struct _GFakeDocumentPortalThread
34 : : {
35 : : GObject parent_instance;
36 : :
37 : : char *address; /* (not nullable) */
38 : : char *app_id; /* (nullable) */
39 : : char *mount_point; /* (not nullable) */
40 : : GList *fake_documents; /* (nullable) (owned) (element-type Gio.File) */
41 : : GCancellable *cancellable; /* (not nullable) (owned) */
42 : : GThread *thread; /* (not nullable) (owned) */
43 : : GCond cond; /* (mutex mutex) */
44 : : GMutex mutex;
45 : : gboolean ready; /* (mutex mutex) */
46 : : };
47 : :
48 : 98 : G_DEFINE_FINAL_TYPE (GFakeDocumentPortalThread, g_fake_document_portal_thread, G_TYPE_OBJECT)
49 : :
50 : : static void g_fake_document_portal_thread_finalize (GObject *object);
51 : :
52 : : static void
53 : 3 : g_fake_document_portal_thread_class_init (GFakeDocumentPortalThreadClass *klass)
54 : : {
55 : 3 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
56 : :
57 : 3 : gobject_class->finalize = g_fake_document_portal_thread_finalize;
58 : 3 : }
59 : :
60 : : static void
61 : 11 : g_fake_document_portal_thread_init (GFakeDocumentPortalThread *self)
62 : : {
63 : 11 : self->cancellable = g_cancellable_new ();
64 : 11 : g_cond_init (&self->cond);
65 : 11 : g_mutex_init (&self->mutex);
66 : 11 : }
67 : :
68 : : static void
69 : 11 : g_fake_document_portal_thread_finalize (GObject *object)
70 : : {
71 : 11 : GFakeDocumentPortalThread *self = G_FAKE_DOCUMENT_PORTAL_THREAD (object);
72 : :
73 : 11 : g_assert (self->thread == NULL); /* should already have been joined */
74 : :
75 : 11 : g_mutex_clear (&self->mutex);
76 : 11 : g_cond_clear (&self->cond);
77 : 11 : g_clear_object (&self->cancellable);
78 : 11 : g_clear_pointer (&self->mount_point, g_free);
79 : 11 : g_clear_pointer (&self->address, g_free);
80 : 11 : g_clear_pointer (&self->app_id, g_free);
81 : 11 : g_clear_list (&self->fake_documents, g_object_unref);
82 : :
83 : 11 : G_OBJECT_CLASS (g_fake_document_portal_thread_parent_class)->finalize (object);
84 : 11 : }
85 : :
86 : : static gboolean
87 : 11 : on_handle_get_mount_point (FakeDocuments *object,
88 : : GDBusMethodInvocation *invocation,
89 : : gpointer user_data)
90 : : {
91 : 11 : GFakeDocumentPortalThread *self = G_FAKE_DOCUMENT_PORTAL_THREAD (user_data);
92 : :
93 : 11 : fake_documents_complete_get_mount_point (object,
94 : : invocation,
95 : 11 : self->mount_point);
96 : 11 : return TRUE;
97 : : }
98 : :
99 : : static gboolean
100 : 8 : on_handle_add_full (FakeDocuments *object,
101 : : GDBusMethodInvocation *invocation,
102 : : GUnixFDList *o_path_fds,
103 : : GVariant *o_path_fd,
104 : : guint flags,
105 : : const gchar *app_id,
106 : : const gchar * const *permissions,
107 : : gpointer user_data)
108 : : {
109 : 8 : GFakeDocumentPortalThread *self = G_FAKE_DOCUMENT_PORTAL_THREAD (user_data);
110 : 8 : gchar **doc_ids = NULL;
111 : 8 : GVariant *extra_out = NULL;
112 : : gsize length, i;
113 : :
114 : 8 : if (o_path_fds != NULL)
115 : 8 : length = g_unix_fd_list_get_length (o_path_fds);
116 : : else
117 : 0 : length = 0;
118 : :
119 : 8 : if (self->app_id)
120 : 8 : g_assert_cmpstr (self->app_id, ==, app_id);
121 : :
122 : 8 : doc_ids = g_new0 (gchar *, length + 1 /* NULL terminator */);
123 : 19 : for (i = 0; i < length; i++)
124 : : {
125 : : GFile *file;
126 : : GFile *file_dir;
127 : 11 : GError *local_error = NULL;
128 : : GFileIOStream *stream;
129 : : char *filename;
130 : : char *basename;
131 : : int fd;
132 : :
133 : 11 : doc_ids[i] = g_strdup_printf ("document-id-%" G_GSIZE_FORMAT, i);
134 : :
135 : 11 : if (g_str_equal (self->app_id, G_FAKE_DOCUMENT_PORTAL_NO_CREATE_DIR_APP_ID))
136 : 2 : continue;
137 : :
138 : 10 : g_test_message ("Creating Document ID %s folder", doc_ids[i]);
139 : :
140 : 10 : file_dir = g_file_new_build_filename (self->mount_point, doc_ids[i], NULL);
141 : 10 : g_file_make_directory (file_dir, self->cancellable, &local_error);
142 : 10 : g_assert_no_error (local_error);
143 : :
144 : 10 : if (g_str_equal (self->app_id, G_FAKE_DOCUMENT_PORTAL_NO_CREATE_FILE_APP_ID))
145 : : {
146 : 1 : g_clear_object (&file_dir);
147 : 1 : continue;
148 : : }
149 : :
150 : 9 : fd = g_unix_fd_list_get (o_path_fds, i, &local_error);
151 : 9 : g_assert_no_error (local_error);
152 : :
153 : 9 : filename = g_unix_fd_query_path (fd, &local_error);
154 : : /* fd was dup()'d by g_unix_fd_list_get(); close it now we have the path */
155 : 9 : g_close (fd, NULL);
156 : :
157 : 9 : if (local_error != NULL)
158 : : {
159 : 0 : if (g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOSYS))
160 : : {
161 : : /* g_unix_fd_query_path() is not supported on this platform (e.g. Hurd).
162 : : * We cannot determine the file path from the fd, so we skip creating
163 : : * the document stub file. Tests that require this should be skipped. */
164 : 0 : g_clear_error (&local_error);
165 : 0 : g_clear_object (&file_dir);
166 : 0 : continue;
167 : : }
168 : 0 : g_assert_no_error (local_error);
169 : : }
170 : :
171 : 9 : g_test_message ("Creating Document ID %s mapped to FD %d (%s)",
172 : 9 : doc_ids[i], fd, filename);
173 : :
174 : 9 : basename = g_path_get_basename (filename);
175 : 9 : g_clear_pointer (&filename, g_free);
176 : :
177 : 9 : file = g_file_get_child (file_dir, basename);
178 : 9 : stream = g_file_create_readwrite (file, G_FILE_CREATE_NONE,
179 : : self->cancellable, &local_error);
180 : 9 : g_assert_no_error (local_error);
181 : :
182 : 9 : g_io_stream_close (G_IO_STREAM (stream), self->cancellable, &local_error);
183 : 9 : g_assert_no_error (local_error);
184 : :
185 : 9 : self->fake_documents = g_list_prepend (self->fake_documents,
186 : : g_steal_pointer (&file));
187 : :
188 : 9 : g_clear_error (&local_error);
189 : 9 : g_clear_object (&file_dir);
190 : 9 : g_clear_object (&stream);
191 : 9 : g_clear_pointer (&basename, g_free);
192 : : }
193 : 8 : extra_out = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
194 : :
195 : 8 : fake_documents_complete_add_full (object,
196 : : invocation,
197 : : NULL,
198 : : (const char **) doc_ids,
199 : : extra_out);
200 : :
201 : 8 : g_strfreev (doc_ids);
202 : :
203 : 8 : return TRUE;
204 : : }
205 : :
206 : : static void
207 : 11 : on_name_acquired (GDBusConnection *connection,
208 : : const gchar *name,
209 : : gpointer user_data)
210 : : {
211 : 11 : GFakeDocumentPortalThread *self = G_FAKE_DOCUMENT_PORTAL_THREAD (user_data);
212 : :
213 : 11 : g_test_message ("Acquired the name %s", name);
214 : :
215 : 11 : g_mutex_lock (&self->mutex);
216 : 11 : self->ready = TRUE;
217 : 11 : g_cond_signal (&self->cond);
218 : 11 : g_mutex_unlock (&self->mutex);
219 : 11 : }
220 : :
221 : : static void
222 : 0 : on_name_lost (GDBusConnection *connection,
223 : : const gchar *name,
224 : : gpointer user_data)
225 : : {
226 : 0 : g_test_message ("Lost the name %s", name);
227 : 0 : }
228 : :
229 : : static gboolean
230 : 11 : cancelled_cb (GCancellable *cancellable,
231 : : gpointer user_data)
232 : : {
233 : 11 : g_test_message ("fake-document-portal cancelled");
234 : 11 : return G_SOURCE_CONTINUE;
235 : : }
236 : :
237 : : static gpointer
238 : 11 : fake_document_portal_thread_cb (gpointer user_data)
239 : : {
240 : 11 : GFakeDocumentPortalThread *self = G_FAKE_DOCUMENT_PORTAL_THREAD (user_data);
241 : 11 : GMainContext *context = NULL;
242 : 11 : GDBusConnection *connection = NULL;
243 : 11 : GSource *source = NULL;
244 : : guint id;
245 : 11 : FakeDocuments *interface = NULL;
246 : 11 : GError *local_error = NULL;
247 : : char *tmpdir;
248 : :
249 : 11 : tmpdir = g_dir_make_tmp ("fake-document-portal-XXXXXXX", &local_error);
250 : 11 : self->mount_point = g_build_filename (tmpdir, "documents", NULL);
251 : 11 : g_mkdir (self->mount_point, 0700);
252 : 11 : g_assert_no_error (local_error);
253 : 11 : g_test_message ("Created mount point %s", self->mount_point);
254 : :
255 : 11 : context = g_main_context_new ();
256 : 11 : g_main_context_push_thread_default (context);
257 : :
258 : 11 : connection = g_dbus_connection_new_for_address_sync (self->address,
259 : : G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
260 : : G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
261 : : NULL,
262 : : self->cancellable,
263 : : &local_error);
264 : 11 : g_assert_no_error (local_error);
265 : :
266 : : /* Listen for cancellation. The source will wake up the context iteration
267 : : * which can then re-check its exit condition below. */
268 : 11 : source = g_cancellable_source_new (self->cancellable);
269 : 11 : g_source_set_callback (source, G_SOURCE_FUNC (cancelled_cb), NULL, NULL);
270 : 11 : g_source_attach (source, context);
271 : 11 : g_source_unref (source);
272 : :
273 : : /* Set up the interface */
274 : 11 : g_test_message ("Acquired a message bus connection");
275 : :
276 : 11 : interface = fake_documents_skeleton_new ();
277 : 11 : g_signal_connect_object (interface,
278 : : "handle-get-mount-point",
279 : : G_CALLBACK (on_handle_get_mount_point),
280 : : self, G_CONNECT_DEFAULT);
281 : 11 : g_signal_connect_object (interface,
282 : : "handle-add-full",
283 : : G_CALLBACK (on_handle_add_full),
284 : : self, G_CONNECT_DEFAULT);
285 : :
286 : 11 : g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),
287 : : connection,
288 : : "/org/freedesktop/portal/documents",
289 : : &local_error);
290 : 11 : g_assert_no_error (local_error);
291 : :
292 : : /* Own the portal name */
293 : 11 : id = g_bus_own_name_on_connection (connection,
294 : : "org.freedesktop.portal.Documents",
295 : : G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
296 : : G_BUS_NAME_OWNER_FLAGS_REPLACE,
297 : : on_name_acquired,
298 : : on_name_lost,
299 : : self,
300 : : NULL);
301 : :
302 : 59 : while (!g_cancellable_is_cancelled (self->cancellable))
303 : 48 : g_main_context_iteration (context, TRUE);
304 : :
305 : : // Remove the stuff we've created.
306 : 20 : for (GList *l = self->fake_documents; l; l = l->next)
307 : : {
308 : 9 : GFile *file = G_FILE (l->data);
309 : 9 : GFile *parent_dir = g_file_get_parent (file);
310 : :
311 : 9 : g_file_delete (file, NULL, &local_error);
312 : 9 : g_assert_no_error (local_error);
313 : :
314 : 9 : g_file_delete (parent_dir, NULL, &local_error);
315 : 9 : g_assert_no_error (local_error);
316 : :
317 : 9 : g_clear_object (&parent_dir);
318 : : }
319 : 11 : g_rmdir (self->mount_point);
320 : 11 : g_assert_no_errno (errno);
321 : :
322 : 11 : g_bus_unown_name (id);
323 : 11 : g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (interface));
324 : 11 : g_clear_object (&interface);
325 : 11 : g_clear_object (&connection);
326 : 11 : g_main_context_pop_thread_default (context);
327 : 11 : g_clear_pointer (&context, g_main_context_unref);
328 : 11 : g_free (tmpdir);
329 : :
330 : 11 : return NULL;
331 : : }
332 : :
333 : : /*
334 : : * Create a new #GFakeDocumentPortalThread. The thread isn’t started until
335 : : * g_fake_document_portal_thread_run() is called on it.
336 : : *
337 : : * Returns: (transfer full): the new fake document portal wrapper
338 : : */
339 : : GFakeDocumentPortalThread *
340 : 11 : g_fake_document_portal_thread_new (const char *address,
341 : : const char *app_id)
342 : : {
343 : 11 : GFakeDocumentPortalThread *self = g_object_new (G_TYPE_FAKE_DOCUMENT_PORTAL_THREAD, NULL);
344 : 11 : self->address = g_strdup (address);
345 : 11 : self->app_id = g_strdup (app_id);
346 : 11 : return g_steal_pointer (&self);
347 : : }
348 : :
349 : : /*
350 : : * Start a worker thread which will run a fake
351 : : * `org.freedesktop.portal.Documents` portal on the bus at @address. This is
352 : : * intended to be used with #GTestDBus to mock up a portal from within a unit
353 : : * test process, rather than relying on D-Bus activation of a mock portal
354 : : * subprocess.
355 : : *
356 : : * It blocks until the thread has owned its D-Bus name and is ready to handle
357 : : * requests.
358 : : */
359 : : void
360 : 11 : g_fake_document_portal_thread_run (GFakeDocumentPortalThread *self)
361 : : {
362 : 11 : g_return_if_fail (G_IS_FAKE_DOCUMENT_PORTAL_THREAD (self));
363 : 11 : g_return_if_fail (self->thread == NULL);
364 : :
365 : 11 : self->thread = g_thread_new ("fake-document-portal", fake_document_portal_thread_cb, self);
366 : :
367 : : /* Block until the thread is ready. */
368 : 11 : g_mutex_lock (&self->mutex);
369 : 22 : while (!self->ready)
370 : 11 : g_cond_wait (&self->cond, &self->mutex);
371 : 11 : g_mutex_unlock (&self->mutex);
372 : : }
373 : :
374 : : /* Stop and join a worker thread started with fake_document_portal_thread_run().
375 : : * Blocks until the thread has stopped and joined.
376 : : *
377 : : * Once this has been called, it’s safe to drop the final reference on @self. */
378 : : void
379 : 11 : g_fake_document_portal_thread_stop (GFakeDocumentPortalThread *self)
380 : : {
381 : 11 : g_return_if_fail (G_IS_FAKE_DOCUMENT_PORTAL_THREAD (self));
382 : 11 : g_return_if_fail (self->thread != NULL);
383 : :
384 : 11 : g_cancellable_cancel (self->cancellable);
385 : 11 : g_thread_join (g_steal_pointer (&self->thread));
386 : : }
387 : :
388 : : /* Gets the thread mount point, this is valid only after the thread has been
389 : : * started. We do not care about thread-safety here since this is a value
390 : : * that is set on thread start-up.
391 : : */
392 : : const char *
393 : 7 : g_fake_document_portal_thread_get_mount_point (GFakeDocumentPortalThread *self)
394 : : {
395 : : const char *mount_point;
396 : :
397 : 7 : g_return_val_if_fail (G_IS_FAKE_DOCUMENT_PORTAL_THREAD (self), NULL);
398 : 7 : g_return_val_if_fail (self->thread != NULL, NULL);
399 : :
400 : 7 : g_mutex_lock (&self->mutex);
401 : 7 : mount_point = self->mount_point;
402 : 7 : g_mutex_unlock (&self->mutex);
403 : :
404 : 7 : return mount_point;
405 : : }
|