File: test-permission-store.c

package info (click to toggle)
xdg-desktop-portal 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,596 kB
  • sloc: ansic: 16,942; sh: 5,277; xml: 930; makefile: 167; sed: 16
file content (337 lines) | stat: -rw-r--r-- 10,316 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include <gio/gio.h>
#include <gio/gunixfdlist.h>

#include "src/xdp-utils.h"
#include "document-portal/permission-store-dbus.h"

char outdir[] = "/tmp/xdp-test-XXXXXX";

GTestDBus *dbus;
GDBusConnection *session_bus;
XdgPermissionStore *permissions;

static void
test_version (void)
{
  g_assert_cmpint (xdg_permission_store_get_version (permissions), ==, 1);
}

static int change_count;

static void
changed_cb (XdgPermissionStore *store,
            const char *table,
            const char *id,
            gboolean deleted,
            GVariant *data,
            GVariant *perms,
            gpointer user_data)
{
  char **strv;
  gboolean res;

  change_count++;

  g_assert_cmpstr (table, ==, "TEST");
  g_assert_cmpstr (id, ==, "test-resource");
  g_assert_false (deleted);
  g_assert_true (g_variant_is_of_type (perms, G_VARIANT_TYPE ("a{sas}")));
  res = g_variant_lookup (perms, "one.two.three", "^a&s", &strv);
  g_assert_true (res);
  g_assert_cmpint (g_strv_length (strv), ==, 2);
  g_assert (g_strv_contains ((const char *const *)strv, "one"));
  g_assert (g_strv_contains ((const char *const *)strv, "two"));
}

static void
changed_cb2 (XdgPermissionStore *store,
             const char *table,
             const char *id,
             gboolean deleted,
             GVariant *data,
             GVariant *perms,
             gpointer user_data)
{
  change_count++;

  g_assert_cmpstr (table, ==, "TEST");
  g_assert_cmpstr (id, ==, "test-resource");
  g_assert_true (deleted);
}

static gboolean
timeout_cb (gpointer data)
{
  gboolean *timeout_reached = data;

  *timeout_reached = TRUE;
  return G_SOURCE_CONTINUE;
}

static void
test_change (void)
{
  gulong changed_handler;
  gboolean res;
  g_autoptr(GError) error = NULL;
  const char * perms[] = { "one", "two", NULL };
  gboolean timeout_reached = FALSE;
  guint timeout_id;

  changed_handler = g_signal_connect (permissions, "changed", G_CALLBACK (changed_cb), NULL);

  change_count = 0;

  res = xdg_permission_store_call_set_permission_sync (permissions,
                                                       "TEST", TRUE,
                                                       "test-resource",
                                                       "one.two.three",
                                                       perms,
                                                       NULL,
                                                       &error);
  g_assert_no_error (error);
  g_assert_true (res);

  timeout_id = g_timeout_add (10000, timeout_cb, &timeout_reached);
  while (!timeout_reached && change_count == 0)
    g_main_context_iteration (NULL, TRUE);
  g_source_remove (timeout_id);

  g_assert_cmpint (change_count, ==, 1);
                                      
  g_signal_handler_disconnect (permissions, changed_handler);

  changed_handler = g_signal_connect (permissions, "changed", G_CALLBACK (changed_cb2), NULL);

  change_count = 0;

  res = xdg_permission_store_call_delete_sync (permissions,
                                               "TEST",
                                               "test-resource",
                                               NULL,
                                               &error);
  g_assert_no_error (error);
  g_assert_true (res);

  timeout_id = g_timeout_add (10000, timeout_cb, &timeout_reached);
  while (!timeout_reached && change_count == 0)
    g_main_context_iteration (NULL, TRUE);
  g_source_remove (timeout_id);

  g_assert_cmpint (change_count, ==, 1);
}

static void
test_lookup (void)
{
  gboolean res;
  g_autoptr(GError) error = NULL;
  const char * perms[] = { "one", "two", NULL };
  g_autoptr(GVariant) p = NULL;
  g_autoptr(GVariant) d = NULL;
  char **strv;
  GVariantBuilder pb;

  res = xdg_permission_store_call_lookup_sync (permissions,
                                               "TEST",
                                               "test-resource",
                                               &p,
                                               &d,
                                               NULL,
                                               &error);
  g_assert_error (error, XDG_DESKTOP_PORTAL_ERROR, XDG_DESKTOP_PORTAL_ERROR_NOT_FOUND);
  g_assert_false (res);
  g_clear_error (&error);

  g_variant_builder_init (&pb, G_VARIANT_TYPE ("a{sas}"));
  g_variant_builder_add (&pb, "{s@as}", "one.two.three", g_variant_new_strv (perms, -1));
  res = xdg_permission_store_call_set_sync (permissions,
                                            "TEST", TRUE,
                                            "test-resource",
                                            g_variant_builder_end (&pb),
                                            g_variant_new_variant (g_variant_new_boolean (TRUE)),
                                            NULL,
                                            &error);
  g_assert_no_error (error);
  g_assert_true (res);

  res = xdg_permission_store_call_lookup_sync (permissions,
                                               "TEST",
                                               "test-resource",
                                               &p,
                                               &d,
                                               NULL,
                                               &error);
  g_assert_no_error (error);
  g_assert_true (res);

  g_assert_true (g_variant_is_of_type (p, G_VARIANT_TYPE ("a{sas}")));
  res = g_variant_lookup (p, "one.two.three", "^a&s", &strv);
  g_assert_true (res);
  g_assert_cmpint (g_strv_length (strv), ==, 2);
  g_assert (g_strv_contains ((const char *const *)strv, "one"));
  g_assert (g_strv_contains ((const char *const *)strv, "two"));
  g_assert_true (g_variant_is_of_type (d, G_VARIANT_TYPE_VARIANT));
  g_assert_true (g_variant_is_of_type (g_variant_get_variant (d), G_VARIANT_TYPE_BOOLEAN));
  g_assert_true (g_variant_get_boolean (g_variant_get_variant (d)));

  res = xdg_permission_store_call_delete_sync (permissions,
                                               "TEST",
                                               "test-resource",
                                               NULL,
                                               &error);
  g_assert_no_error (error);
}

static void
test_create (void)
{
  gboolean res;
  g_autoptr(GError) error = NULL;
  const char * perms[] = { "one", "two", NULL };

  res = xdg_permission_store_call_set_permission_sync (permissions,
                                                       "DOESNOTEXIST", FALSE,
                                                       "test-resource",
                                                       "one.two.three",
                                                       perms,
                                                       NULL,
                                                       &error);
  g_assert_error (error, XDG_DESKTOP_PORTAL_ERROR, XDG_DESKTOP_PORTAL_ERROR_NOT_FOUND);
  g_assert_false (res);
}

static void
global_setup (void)
{
  GError *error = NULL;
  g_autofree gchar *services = NULL;
  GQuark portal_errors G_GNUC_UNUSED;

  /* make sure errors are registered */
  portal_errors = XDG_DESKTOP_PORTAL_ERROR;

  g_mkdtemp (outdir);
  g_print ("outdir: %s\n", outdir);

  g_setenv ("XDG_RUNTIME_DIR", outdir, TRUE);
  g_setenv ("XDG_DATA_HOME", outdir, TRUE);

  dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
  services = g_test_build_filename (G_TEST_BUILT, "services", NULL);
  g_test_dbus_add_service_dir (dbus, services);
  g_test_dbus_up (dbus);

  /* g_test_dbus_up unsets this, so re-set */
  g_setenv ("XDG_RUNTIME_DIR", outdir, TRUE);

  session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
  g_assert_no_error (error);

  permissions = xdg_permission_store_proxy_new_sync (session_bus, 0,
                                                     "org.freedesktop.impl.portal.PermissionStore",
                                                     "/org/freedesktop/impl/portal/PermissionStore",
                                                     NULL, &error);
  g_assert_no_error (error);
  g_assert (permissions != NULL);
}

static gboolean
rm_rf_dir (GFile         *dir,
           GError       **error)
{
  GFileEnumerator *enumerator = NULL;
  g_autoptr(GFileInfo) child_info = NULL;
  GError *temp_error = NULL;

  enumerator = g_file_enumerate_children (dir, "standard::type,standard::name",
                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                          NULL, error);
  if (!enumerator)
    return FALSE;

  while ((child_info = g_file_enumerator_next_file (enumerator, NULL, &temp_error)))
    {
      const char *name = g_file_info_get_name (child_info);
      g_autoptr(GFile) child = g_file_get_child (dir, name);

      if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY)
        {
          if (!rm_rf_dir (child, error))
            return FALSE;
        }
      else
        {
          if (!g_file_delete (child, NULL, error))
            return FALSE;
        }

      g_clear_object (&child_info);
    }

  if (temp_error != NULL)
    {
      g_propagate_error (error, temp_error);
      return FALSE;
    }

  if (!g_file_delete (dir, NULL, error))
    return FALSE;

  return TRUE;
}

static void
global_teardown (void)
{
  GError *error = NULL;
  g_autoptr(GFile) outdir_file = g_file_new_for_path (outdir);
  int res;

  g_object_unref (permissions);

  g_dbus_connection_close_sync (session_bus, NULL, &error);
  g_assert_no_error (error);

  g_object_unref (session_bus);

  g_test_dbus_down (dbus);

  g_object_unref (dbus);

  res = rm_rf_dir (outdir_file, &error);
  g_assert_no_error (error);
  g_assert_true (res);
}

int
main (int argc, char **argv)
{
  int res;

  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/permissions/version", test_version);
  g_test_add_func ("/permissions/change", test_change);
  g_test_add_func ("/permissions/lookup", test_lookup);
  g_test_add_func ("/permissions/create", test_create);

  global_setup ();

  res = g_test_run ();

  global_teardown ();

  return res;
}