Add some GPermission tests
[platform/upstream/glib.git] / gio / tests / permission.c
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   GError *error = NULL;
32   gboolean ret;
33
34   ret = g_permission_acquire_finish (p, res, &error);
35   g_assert (!ret);
36   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
37   g_clear_error (&error);
38 }
39
40 static void
41 released (GObject      *source,
42           GAsyncResult *res,
43           gpointer      user_data)
44 {
45   GPermission *p = G_PERMISSION (source);
46   GError *error = NULL;
47   gboolean ret;
48
49   ret = g_permission_release_finish (p, res, &error);
50   g_assert (!ret);
51   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
52   g_clear_error (&error);
53 }
54
55 static void
56 test_simple (void)
57 {
58   GPermission *p;
59   gboolean allowed;
60   gboolean can_acquire;
61   gboolean can_release;
62   gboolean ret;
63   GError *error = NULL;
64
65   p = g_simple_permission_new (TRUE);
66
67   g_assert (g_permission_get_allowed (p));
68   g_assert (!g_permission_get_can_acquire (p));
69   g_assert (!g_permission_get_can_release (p));
70
71   g_object_get (p,
72                 "allowed", &allowed,
73                 "can-acquire", &can_acquire,
74                 "can-release", &can_release,
75                 NULL);
76
77   g_assert (allowed);
78   g_assert (!can_acquire);
79   g_assert (!can_release);
80
81   ret = g_permission_acquire (p, NULL, &error);
82   g_assert (!ret);
83   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
84   g_clear_error (&error);
85
86   ret = g_permission_release (p, NULL, &error);
87   g_assert (!ret);
88   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
89   g_clear_error (&error);
90
91   g_permission_acquire_async (p, NULL, acquired, NULL);
92   g_permission_release_async (p, NULL, released, NULL);
93
94   g_object_unref (p);
95 }
96
97 int
98 main (int argc, char *argv[])
99 {
100   g_type_init ();
101   g_test_init (&argc, &argv, NULL);
102
103   g_test_add_func ("/permission/simple", test_simple);
104
105   return g_test_run ();
106 }