Branch data Line data Source code
1 : : /* Unit tests for gdocumentportal
2 : : * GIO - GLib Input, Output and Streaming Library
3 : : *
4 : : * Copyright (C) 2025 Marco Trevisan
5 : : *
6 : : * SPDX-License-Identifier: LGPL-2.1-or-later
7 : : *
8 : : * This library is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU Lesser General Public
10 : : * License as published by the Free Software Foundation; either
11 : : * version 2.1 of the License, or (at your option) any later version.
12 : : *
13 : : * This library is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : * Lesser General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU Lesser General
19 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 : : *
21 : : * Author: Marco Trevisan <marco.trevisan@canonical.com>
22 : : */
23 : :
24 : : #include <gio/gio.h>
25 : :
26 : : #include "gdbus-sessionbus.h"
27 : : #include "fake-document-portal.h"
28 : : #include "gdocumentportal.h"
29 : :
30 : : static void
31 : 1 : test_document_portal_add_uri (void)
32 : : {
33 : 1 : GFakeDocumentPortalThread *thread = NULL;
34 : : GFile *file;
35 : 1 : GList *uris = NULL; /* (element-type utf8) */
36 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
37 : 1 : GFileIOStream *iostream = NULL;
38 : 1 : GError *error = NULL;
39 : : const char *app_id;
40 : : const char *document_portal_mount_point;
41 : : char *basename;
42 : : char *expected_uri;
43 : :
44 : : /* Run a fake-document-portal */
45 : 1 : app_id = "org.gnome.glib.gio";
46 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
47 : 1 : g_fake_document_portal_thread_run (thread);
48 : 1 : document_portal_mount_point = g_fake_document_portal_thread_get_mount_point (thread);
49 : :
50 : 1 : file = g_file_new_tmp ("test_document_portal_add_uri_XXXXXX",
51 : : &iostream, NULL);
52 : 1 : g_assert_no_error (error);
53 : 1 : g_io_stream_close ((GIOStream *) iostream, NULL, &error);
54 : 1 : g_assert_no_error (error);
55 : 1 : g_object_unref (iostream);
56 : :
57 : 1 : uris = g_list_append (uris, g_file_get_uri (file));
58 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
59 : 1 : g_assert_no_error (error);
60 : :
61 : 1 : basename = g_file_get_basename (file);
62 : 1 : expected_uri = g_strdup_printf ("file:%s/document-id-0/%s",
63 : : document_portal_mount_point, basename);
64 : :
65 : 1 : g_assert_cmpuint (g_list_length (portal_uris), ==, 1);
66 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, expected_uri);
67 : :
68 : 1 : g_fake_document_portal_thread_stop (thread);
69 : 1 : g_clear_object (&thread);
70 : 1 : g_clear_object (&file);
71 : 1 : g_clear_pointer (&expected_uri, g_free);
72 : 1 : g_clear_pointer (&basename, g_free);
73 : 1 : g_clear_list (&uris, g_free);
74 : 1 : g_clear_list (&portal_uris, g_free);
75 : 1 : }
76 : :
77 : : static void
78 : 1 : test_document_portal_add_not_existent_uri (void)
79 : : {
80 : 1 : GFakeDocumentPortalThread *thread = NULL;
81 : 1 : GList *uris = NULL; /* (element-type const char*) */
82 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
83 : 1 : GError *error = NULL;
84 : : const char *app_id;
85 : : const char *document_portal_mount_point;
86 : : const char *uri;
87 : : char *portal_uri;
88 : :
89 : : /* Run a fake-document-portal */
90 : 1 : app_id = "org.gnome.glib.gio.not-existent-uri";
91 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
92 : 1 : g_fake_document_portal_thread_run (thread);
93 : 1 : document_portal_mount_point = g_fake_document_portal_thread_get_mount_point (thread);
94 : :
95 : 1 : uri = "file:/no-existent-path-really!";
96 : 1 : uris = g_list_append (uris, (char *) uri);
97 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
98 : 1 : g_assert_no_error (error);
99 : :
100 : 1 : portal_uri = g_strdup_printf ("file:%s/document-id-0/no-existent-path-really!",
101 : : document_portal_mount_point);
102 : 1 : g_assert_false (g_file_test (portal_uri, G_FILE_TEST_EXISTS));
103 : :
104 : 1 : g_assert_cmpuint (g_list_length (portal_uris), ==, 1);
105 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, uri);
106 : :
107 : 1 : g_fake_document_portal_thread_stop (thread);
108 : 1 : g_clear_object (&thread);
109 : 1 : g_clear_pointer (&portal_uri, g_free);
110 : 1 : g_clear_list (&uris, NULL);
111 : 1 : g_clear_list (&portal_uris, g_free);
112 : 1 : }
113 : :
114 : : static void
115 : 1 : test_document_portal_add_existent_and_not_existent_uris (void)
116 : : {
117 : 1 : GFakeDocumentPortalThread *thread = NULL;
118 : : GFile *file;
119 : : GFile *expected_file0;
120 : : GFile *expected_file1;
121 : 1 : GList *uris = NULL; /* (element-type utf8) */
122 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
123 : 1 : GFileIOStream *iostream = NULL;
124 : 1 : GError *error = NULL;
125 : : const char *app_id;
126 : : const char *document_portal_mount_point;
127 : : const char *invalid_uri;
128 : : char *basename;
129 : :
130 : : /* Run a fake-document-portal */
131 : 1 : app_id = "org.gnome.glib.gio.mixed-uris";
132 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
133 : 1 : g_fake_document_portal_thread_run (thread);
134 : 1 : document_portal_mount_point = g_fake_document_portal_thread_get_mount_point (thread);
135 : :
136 : 1 : file = g_file_new_tmp ("test_document_portal_add_existent_and_not_existent_uris_XXXXXX",
137 : : &iostream, NULL);
138 : 1 : g_assert_no_error (error);
139 : 1 : g_io_stream_close ((GIOStream *) iostream, NULL, &error);
140 : 1 : g_assert_no_error (error);
141 : 1 : g_object_unref (iostream);
142 : :
143 : 1 : invalid_uri = "file:/no-existent-path-really!";
144 : :
145 : 1 : uris = g_list_append (uris, g_file_get_uri (file));
146 : 1 : uris = g_list_append (uris, g_strdup (invalid_uri));
147 : 1 : uris = g_list_append (uris, g_file_get_uri (file));
148 : 1 : uris = g_list_append (uris, g_strdup (invalid_uri));
149 : :
150 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
151 : 1 : g_assert_no_error (error);
152 : :
153 : 1 : basename = g_file_get_basename (file);
154 : 1 : expected_file0 = g_file_new_build_filename (document_portal_mount_point,
155 : : "document-id-0", basename, NULL);
156 : 1 : expected_file1 = g_file_new_build_filename (document_portal_mount_point,
157 : : "document-id-1", basename, NULL);
158 : :
159 : 1 : g_assert_cmpuint (g_list_length (portal_uris), ==, 4);
160 : 1 : g_assert_cmpstr ((char * ) g_list_nth_data (portal_uris, 0) + strlen ("file:"), ==, g_file_peek_path (expected_file0));
161 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 1), ==, invalid_uri);
162 : 1 : g_assert_cmpstr ((char * ) g_list_nth_data (portal_uris, 2) + strlen ("file:"), ==, g_file_peek_path (expected_file1));
163 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 3), ==, invalid_uri);
164 : :
165 : 1 : g_assert_true (g_file_test (g_file_peek_path (expected_file0), G_FILE_TEST_IS_REGULAR));
166 : 1 : g_assert_true (g_file_test (g_file_peek_path (expected_file1), G_FILE_TEST_IS_REGULAR));
167 : :
168 : 1 : g_fake_document_portal_thread_stop (thread);
169 : 1 : g_clear_object (&thread);
170 : 1 : g_clear_object (&file);
171 : 1 : g_clear_object (&expected_file1);
172 : 1 : g_clear_object (&expected_file0);
173 : 1 : g_clear_pointer (&basename, g_free);
174 : 1 : g_clear_list (&uris, g_free);
175 : 1 : g_clear_list (&portal_uris, g_free);
176 : 1 : }
177 : :
178 : : static void
179 : 1 : test_document_portal_add_symlink_uri (void)
180 : : {
181 : 1 : GFakeDocumentPortalThread *thread = NULL;
182 : : GFile *target;
183 : : GFile *link1;
184 : : GFile *link2;
185 : : GFile *parent_dir;
186 : 1 : GList *uris = NULL; /* (element-type utf8) */
187 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
188 : 1 : GFileIOStream *iostream = NULL;
189 : 1 : GError *error = NULL;
190 : : const char *app_id;
191 : : const char *document_portal_mount_point;
192 : : char *tmpdir_path;
193 : : char *basename;
194 : : char *expected_uri;
195 : :
196 : : /* Run a fake-document-portal */
197 : 1 : app_id = "org.gnome.glib.gio.symlinks";
198 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
199 : 1 : g_fake_document_portal_thread_run (thread);
200 : 1 : document_portal_mount_point = g_fake_document_portal_thread_get_mount_point (thread);
201 : :
202 : 1 : target = g_file_new_tmp ("test_document_portal_add_symlink_uri_XXXXXX",
203 : : &iostream, NULL);
204 : 1 : g_assert_no_error (error);
205 : 1 : g_io_stream_close ((GIOStream *) iostream, NULL, &error);
206 : 1 : g_assert_no_error (error);
207 : 1 : g_object_unref (iostream);
208 : :
209 : 1 : tmpdir_path = g_dir_make_tmp ("g_file_symlink_XXXXXX", &error);
210 : 1 : g_assert_no_error (error);
211 : :
212 : 1 : parent_dir = g_file_new_for_path (tmpdir_path);
213 : 1 : g_assert_true (g_file_query_exists (parent_dir, NULL));
214 : :
215 : 1 : link1 = g_file_get_child (parent_dir, "symlink");
216 : 1 : g_assert_false (g_file_query_exists (link1, NULL));
217 : :
218 : 1 : g_file_make_symbolic_link (link1, g_file_peek_path (target), NULL, &error);
219 : 1 : g_assert_no_error (error);
220 : 1 : g_assert_true (g_file_query_exists (link1, NULL));
221 : :
222 : 1 : link2 = g_file_get_child (parent_dir, "symlink-of-symlink");
223 : 1 : g_assert_false (g_file_query_exists (link2, NULL));
224 : :
225 : 1 : basename = g_file_get_basename (link1);
226 : 1 : g_file_make_symbolic_link (link2, basename, NULL, &error);
227 : 1 : g_clear_pointer (&basename, g_free);
228 : 1 : g_assert_true (g_file_query_exists (link2, NULL));
229 : 1 : g_assert_no_error (error);
230 : :
231 : 1 : uris = g_list_append (uris, g_file_get_uri (link1));
232 : 1 : uris = g_list_append (uris, g_file_get_uri (link2));
233 : :
234 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
235 : 1 : g_assert_no_error (error);
236 : :
237 : 1 : basename = g_file_get_basename (target);
238 : 1 : g_assert_cmpuint (g_list_length (portal_uris), ==, 2);
239 : :
240 : 1 : expected_uri = g_strdup_printf ("file:%s/document-id-0/%s",
241 : : document_portal_mount_point, basename);
242 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, expected_uri);
243 : 1 : g_clear_pointer (&expected_uri, g_free);
244 : :
245 : 1 : expected_uri = g_strdup_printf ("file:%s/document-id-1/%s",
246 : : document_portal_mount_point, basename);
247 : 1 : g_assert_cmpstr (g_list_nth_data (portal_uris, 1), ==, expected_uri);
248 : 1 : g_clear_pointer (&expected_uri, g_free);
249 : :
250 : 1 : g_fake_document_portal_thread_stop (thread);
251 : 1 : g_clear_object (&thread);
252 : 1 : g_clear_object (&target);
253 : 1 : g_clear_object (&link1);
254 : 1 : g_clear_object (&link2);
255 : 1 : g_clear_object (&parent_dir);
256 : 1 : g_clear_pointer (&expected_uri, g_free);
257 : 1 : g_clear_pointer (&basename, g_free);
258 : 1 : g_clear_pointer (&tmpdir_path, g_free);
259 : 1 : g_clear_list (&uris, g_free);
260 : 1 : g_clear_list (&portal_uris, g_free);
261 : 1 : }
262 : :
263 : : static void
264 : 1 : test_document_portal_add_uri_with_missing_doc_id_path (void)
265 : : {
266 : 1 : GFakeDocumentPortalThread *thread = NULL;
267 : : GFile *file;
268 : 1 : GList *uris = NULL; /* (element-type utf8) */
269 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
270 : 1 : GFileIOStream *iostream = NULL;
271 : 1 : GError *error = NULL;
272 : : const char *app_id;
273 : :
274 : : /* Run a fake-document-portal */
275 : 1 : app_id = G_FAKE_DOCUMENT_PORTAL_NO_CREATE_DIR_APP_ID;
276 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
277 : 1 : g_fake_document_portal_thread_run (thread);
278 : :
279 : 1 : file = g_file_new_tmp ("test_document_portal_add_uri_with_missing_doc_id_path_XXXXXX",
280 : : &iostream, NULL);
281 : 1 : g_assert_no_error (error);
282 : 1 : g_io_stream_close ((GIOStream *) iostream, NULL, &error);
283 : 1 : g_assert_no_error (error);
284 : 1 : g_object_unref (iostream);
285 : :
286 : 1 : uris = g_list_append (uris, g_file_get_uri (file));
287 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
288 : 1 : g_assert_null (portal_uris);
289 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
290 : :
291 : 1 : g_fake_document_portal_thread_stop (thread);
292 : 1 : g_clear_object (&thread);
293 : 1 : g_clear_object (&file);
294 : 1 : g_clear_list (&uris, g_free);
295 : 1 : g_clear_error (&error);
296 : 1 : g_clear_list (&portal_uris, g_free);
297 : 1 : }
298 : :
299 : : static void
300 : 1 : test_document_portal_add_uri_with_missing_doc_file (void)
301 : : {
302 : 1 : GFakeDocumentPortalThread *thread = NULL;
303 : : GFile *file;
304 : 1 : GList *uris = NULL; /* (element-type utf8) */
305 : 1 : GList *portal_uris = NULL; /* (element-type utf8) */
306 : 1 : GFileIOStream *iostream = NULL;
307 : 1 : GError *error = NULL;
308 : : const char *app_id;
309 : :
310 : : /* Run a fake-document-portal */
311 : 1 : app_id = G_FAKE_DOCUMENT_PORTAL_NO_CREATE_FILE_APP_ID;
312 : 1 : thread = g_fake_document_portal_thread_new (session_bus_get_address (), app_id);
313 : 1 : g_fake_document_portal_thread_run (thread);
314 : :
315 : 1 : file = g_file_new_tmp ("test_document_portal_add_uri_with_missing_doc_file_XXXXXX",
316 : : &iostream, NULL);
317 : 1 : g_assert_no_error (error);
318 : 1 : g_io_stream_close ((GIOStream *) iostream, NULL, &error);
319 : 1 : g_assert_no_error (error);
320 : 1 : g_object_unref (iostream);
321 : :
322 : 1 : uris = g_list_append (uris, g_file_get_uri (file));
323 : 1 : portal_uris = g_document_portal_add_documents (uris, app_id, &error);
324 : 1 : g_assert_null (portal_uris);
325 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
326 : :
327 : 1 : g_fake_document_portal_thread_stop (thread);
328 : 1 : g_clear_object (&thread);
329 : 1 : g_clear_object (&file);
330 : 1 : g_clear_list (&uris, g_free);
331 : 1 : g_clear_error (&error);
332 : 1 : g_clear_list (&portal_uris, g_free);
333 : 1 : }
334 : :
335 : : int
336 : 1 : main (int argc,
337 : : char *argv[])
338 : : {
339 : 1 : g_setenv ("LC_ALL", "C", TRUE);
340 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
341 : :
342 : 1 : g_test_add_func ("/document-portal/add-uri", test_document_portal_add_uri);
343 : 1 : g_test_add_func ("/document-portal/add-not-existent-uri", test_document_portal_add_not_existent_uri);
344 : 1 : g_test_add_func ("/document-portal/add-existent-and-not-existent-uri", test_document_portal_add_existent_and_not_existent_uris);
345 : 1 : g_test_add_func ("/document-portal/add-symlink-uri", test_document_portal_add_symlink_uri);
346 : 1 : g_test_add_func ("/document-portal/add-uri-with-missing-doc-id-path", test_document_portal_add_uri_with_missing_doc_id_path);
347 : 1 : g_test_add_func ("/document-portal/add-uri-with-missing-doc-file", test_document_portal_add_uri_with_missing_doc_file);
348 : :
349 : 1 : return session_bus_run ();
350 : : }
|