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
28static void
29test_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
58#if G_CREDENTIALS_HAS_PID
59 g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==,
60 getpid ());
61 g_assert_no_error (error);
62#else
63 g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==, -1);
64 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
65 g_clear_error (&error);
66#endif
67
68 set = g_credentials_set_unix_user (credentials: other, uid: not_me, error: &error);
69#if G_CREDENTIALS_SPOOFING_SUPPORTED
70 g_assert_no_error (error);
71 g_assert (set);
72
73 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, not_me);
74 g_assert (!g_credentials_is_same_user (creds, other, &error));
75 g_assert_no_error (error);
76#else
77 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
78 g_assert (!set);
79 g_clear_error (&error);
80
81 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, geteuid ());
82 g_assert (g_credentials_is_same_user (creds, other, &error));
83 g_assert_no_error (error);
84#endif
85
86 stringified = g_credentials_to_string (credentials: creds);
87 g_test_message (format: "%s", stringified);
88 g_free (mem: stringified);
89
90 stringified = g_credentials_to_string (credentials: other);
91 g_test_message (format: "%s", stringified);
92 g_free (mem: stringified);
93
94#if G_CREDENTIALS_USE_LINUX_UCRED
95 {
96 struct ucred *native = g_credentials_get_native (credentials: creds,
97 native_type: G_CREDENTIALS_TYPE_LINUX_UCRED);
98
99 g_assert_cmpuint (native->uid, ==, geteuid ());
100 g_assert_cmpuint (native->pid, ==, getpid ());
101 }
102#elif G_CREDENTIALS_USE_APPLE_XUCRED
103 {
104 struct xucred *native = g_credentials_get_native (creds,
105 G_CREDENTIALS_TYPE_APPLE_XUCRED);
106
107 g_assert_cmpuint (native->cr_version, ==, XUCRED_VERSION);
108 g_assert_cmpuint (native->cr_uid, ==, geteuid ());
109 }
110#elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
111 {
112 struct cmsgcred *native = g_credentials_get_native (creds,
113 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
114
115 g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
116 g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
117 }
118#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
119 {
120 struct unpcbid *native = g_credentials_get_native (creds,
121 G_CREDENTIALS_TYPE_NETBSD_UNPCBID);
122
123 g_assert_cmpuint (native->unp_euid, ==, geteuid ());
124 g_assert_cmpuint (native->unp_pid, ==, getpid ());
125 }
126#elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
127 {
128 struct sockpeercred *native = g_credentials_get_native (creds,
129 G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
130
131 g_assert_cmpuint (native->uid, ==, geteuid ());
132 g_assert_cmpuint (native->pid, ==, getpid ());
133 }
134#elif G_CREDENTIALS_USE_SOLARIS_UCRED
135 {
136 ucred_t *native = g_credentials_get_native (creds,
137 G_CREDENTIALS_TYPE_SOLARIS_UCRED);
138
139 g_assert_cmpuint (ucred_geteuid (native), ==, geteuid ());
140 g_assert_cmpuint (ucred_getpid (native), ==, getpid ());
141 }
142#else
143#error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
144#endif
145
146
147#if G_CREDENTIALS_USE_LINUX_UCRED
148 g_test_expect_message (G_LOG_DOMAIN, log_level: G_LOG_LEVEL_WARNING,
149 pattern: "*g_credentials_get_native: Trying to get*"
150 "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
151 "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
152 "supported*");
153 bad_native_creds = g_credentials_get_native (credentials: creds, native_type: G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
154 g_test_assert_expected_messages ();
155 g_assert_null (bad_native_creds);
156#else
157 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
158 "*g_credentials_get_native: Trying to get*"
159 "G_CREDENTIALS_TYPE_LINUX_UCRED "
160 "but only G_CREDENTIALS_TYPE_*supported*");
161 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
162 g_test_assert_expected_messages ();
163 g_assert_null (bad_native_creds);
164#endif
165
166#else /* ! G_CREDENTIALS_SUPPORTED */
167
168 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
169 "*g_credentials_get_native: Trying to get "
170 "credentials *but*no support*");
171 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
172 g_test_assert_expected_messages ();
173 g_assert_null (bad_native_creds);
174#endif
175
176 g_object_unref (object: creds);
177 g_object_unref (object: other);
178}
179
180int
181main (int argc,
182 char *argv[])
183{
184 g_test_init (argc: &argc, argv: &argv, NULL);
185
186 g_test_add_func (testpath: "/credentials/basic", test_func: test_basic);
187
188 return g_test_run();
189}
190

source code of gtk/subprojects/glib/gio/tests/credentials.c