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