gcredentials: add internal macros to simplify all the #ifdefs
[platform/upstream/glib.git] / gio / tests / credentials.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright © 2012 Collabora Ltd.
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 "config.h"
24
25 #include <gio/gio.h>
26 #include <gio/gcredentialsprivate.h>
27
28 static void
29 test_basic (void)
30 {
31   GCredentials *creds = g_credentials_new ();
32   GCredentials *other = g_credentials_new ();
33   gpointer bad_native_creds;
34 #if G_CREDENTIALS_SUPPORTED
35   GError *error = NULL;
36   gboolean set;
37   pid_t not_me;
38   gchar *stringified;
39 #endif
40
41   /* You can always get a credentials object, but it might not work. */
42   g_assert (creds != NULL);
43   g_assert (other != NULL);
44
45 #if G_CREDENTIALS_SUPPORTED
46   g_assert (g_credentials_is_same_user (creds, other, &error));
47   g_assert_no_error (error);
48
49   if (geteuid () == 0)
50     not_me = 65534; /* traditionally 'nobody' */
51   else
52     not_me = 0;
53
54   g_assert_cmpuint (g_credentials_get_unix_user (creds, &error), ==,
55       geteuid ());
56   g_assert_no_error (error);
57   g_assert_cmpuint (g_credentials_get_unix_pid (creds, &error), ==,
58       getpid ());
59   g_assert_no_error (error);
60
61   set = g_credentials_set_unix_user (other, not_me, &error);
62   g_assert_no_error (error);
63   g_assert (set);
64
65   g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, not_me);
66   g_assert (!g_credentials_is_same_user (creds, other, &error));
67   g_assert_no_error (error);
68
69   stringified = g_credentials_to_string (creds);
70   g_test_message ("%s", stringified);
71   g_free (stringified);
72
73   stringified = g_credentials_to_string (other);
74   g_test_message ("%s", stringified);
75   g_free (stringified);
76
77 #if G_CREDENTIALS_USE_LINUX_UCRED
78         {
79           struct ucred *native = g_credentials_get_native (creds,
80               G_CREDENTIALS_TYPE_LINUX_UCRED);
81
82           g_assert_cmpuint (native->uid, ==, geteuid ());
83           g_assert_cmpuint (native->pid, ==, getpid ());
84         }
85 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
86         {
87           struct cmsgcred *native = g_credentials_get_native (creds,
88               G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
89
90           g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
91           g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
92         }
93 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
94         {
95           struct sockpeercred *native = g_credentials_get_native (creds,
96               G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
97
98           g_assert_cmpuint (native->uid, ==, geteuid ());
99           g_assert_cmpuint (native->pid, ==, getpid ());
100         }
101 #else
102 #error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
103 #endif
104
105
106 #if G_CREDENTIALS_USE_LINUX_UCRED
107   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
108                          "*g_credentials_get_native: Trying to get*"
109                          "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
110                          "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
111                          "supported*");
112   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
113   g_test_assert_expected_messages ();
114   g_assert_null (bad_native_creds);
115 #else
116   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
117                          "*g_credentials_get_native: Trying to get*"
118                          "G_CREDENTIALS_TYPE_LINUX_UCRED "
119                          "but only G_CREDENTIALS_TYPE_*supported*");
120   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
121   g_test_assert_expected_messages ();
122   g_assert_null (bad_native_creds);
123 #endif
124
125 #else /* ! G_CREDENTIALS_SUPPORTED */
126
127   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
128                          "*g_credentials_get_native: Trying to get*"
129                          "credentials but*no support*");
130   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
131   g_test_assert_expected_messages ();
132   g_assert_null (bad_native_creds);
133 #endif
134
135   g_object_unref (creds);
136   g_object_unref (other);
137 }
138
139 int
140 main (int   argc,
141       char *argv[])
142 {
143   g_test_init (&argc, &argv, NULL);
144
145   g_test_add_func ("/credentials/basic", test_basic);
146
147   return g_test_run();
148 }