1 /* Unit tests for GPermission
2 * Copyright (C) 2012 Red Hat, Inc
3 * Author: Matthias Clasen
4 *
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 *
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
10 *
11 * This work 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.
14 *
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
23 */
24
25 #include <gio/gio.h>
26
27 static void
28 acquired (GObject *source,
29 GAsyncResult *res,
30 gpointer user_data)
31 {
32 GPermission *p = G_PERMISSION (source);
33 GMainLoop *loop = user_data;
34 GError *error = NULL;
35 gboolean ret;
36
37 ret = g_permission_acquire_finish (p, res, &error);
38 g_assert (!ret);
39 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
40 g_clear_error (&error);
41
42 g_main_loop_quit (loop);
43 }
44
45 static void
46 released (GObject *source,
47 GAsyncResult *res,
48 gpointer user_data)
49 {
50 GPermission *p = G_PERMISSION (source);
51 GMainLoop *loop = user_data;
52 GError *error = NULL;
53 gboolean ret;
54
55 ret = g_permission_release_finish (p, res, &error);
56 g_assert (!ret);
57 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
58 g_clear_error (&error);
59
60 g_main_loop_quit (loop);
61 }
62
63 static void
64 test_simple (void)
65 {
66 GPermission *p;
67 gboolean allowed;
68 gboolean can_acquire;
69 gboolean can_release;
70 gboolean ret;
71 GError *error = NULL;
72 GMainLoop *loop;
73
74 p = g_simple_permission_new (TRUE);
75
76 g_assert (g_permission_get_allowed (p));
77 g_assert (!g_permission_get_can_acquire (p));
78 g_assert (!g_permission_get_can_release (p));
79
80 g_object_get (p,
81 "allowed", &allowed,
82 "can-acquire", &can_acquire,
83 "can-release", &can_release,
84 NULL);
85
86 g_assert (allowed);
87 g_assert (!can_acquire);
88 g_assert (!can_release);
89
90 ret = g_permission_acquire (p, NULL, &error);
91 g_assert (!ret);
92 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
93 g_clear_error (&error);
94
95 ret = g_permission_release (p, NULL, &error);
96 g_assert (!ret);
97 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
98 g_clear_error (&error);
99
100 loop = g_main_loop_new (NULL, FALSE);
101 g_permission_acquire_async (p, NULL, acquired, loop);
102 g_main_loop_run (loop);
103 g_permission_release_async (p, NULL, released, loop);
104 g_main_loop_run (loop);
105
106 g_main_loop_unref (loop);
107
108 g_object_unref (p);
109 }
110
111 int
112 main (int argc, char *argv[])
113 {
114 g_test_init (&argc, &argv, NULL);
115
116 g_test_add_func ("/permission/simple", test_simple);
117
118 return g_test_run ();
119 }