1 /* GLib testing framework examples and tests
3 * Copyright © 2012 Collabora Ltd.
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
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.
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.
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.
28 #include <gio/gcredentialsprivate.h>
35 GCredentials *creds = g_credentials_new ();
39 stringified = g_credentials_to_string (creds);
40 g_test_message ("%s", stringified);
43 pid = g_credentials_get_native (creds,
44 G_CREDENTIALS_TYPE_WIN32_PID);
45 g_assert_cmpuint (*pid, ==, GetCurrentProcessId ());
47 g_object_unref (creds);
55 GCredentials *creds = g_credentials_new ();
56 GCredentials *other = g_credentials_new ();
57 gpointer bad_native_creds;
58 #if G_CREDENTIALS_SUPPORTED
65 /* You can always get a credentials object, but it might not work. */
66 g_assert (creds != NULL);
67 g_assert (other != NULL);
69 #if G_CREDENTIALS_SUPPORTED
70 g_assert (g_credentials_is_same_user (creds, other, &error));
71 g_assert_no_error (error);
74 not_me = 65534; /* traditionally 'nobody' */
78 g_assert_cmpuint (g_credentials_get_unix_user (creds, &error), ==,
80 g_assert_no_error (error);
82 #if G_CREDENTIALS_HAS_PID
83 g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==,
85 g_assert_no_error (error);
87 g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==, -1);
88 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
89 g_clear_error (&error);
92 set = g_credentials_set_unix_user (other, not_me, &error);
93 #if G_CREDENTIALS_SPOOFING_SUPPORTED
94 g_assert_no_error (error);
97 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, not_me);
98 g_assert (!g_credentials_is_same_user (creds, other, &error));
99 g_assert_no_error (error);
101 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
103 g_clear_error (&error);
105 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, geteuid ());
106 g_assert (g_credentials_is_same_user (creds, other, &error));
107 g_assert_no_error (error);
110 stringified = g_credentials_to_string (creds);
111 g_test_message ("%s", stringified);
112 g_free (stringified);
114 stringified = g_credentials_to_string (other);
115 g_test_message ("%s", stringified);
116 g_free (stringified);
118 #if G_CREDENTIALS_USE_LINUX_UCRED
120 struct ucred *native = g_credentials_get_native (creds,
121 G_CREDENTIALS_TYPE_LINUX_UCRED);
123 g_assert_cmpuint (native->uid, ==, geteuid ());
124 g_assert_cmpuint (native->pid, ==, getpid ());
126 #elif G_CREDENTIALS_USE_APPLE_XUCRED
128 struct xucred *native = g_credentials_get_native (creds,
129 G_CREDENTIALS_TYPE_APPLE_XUCRED);
131 g_assert_cmpuint (native->cr_version, ==, XUCRED_VERSION);
132 g_assert_cmpuint (native->cr_uid, ==, geteuid ());
134 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
136 struct cmsgcred *native = g_credentials_get_native (creds,
137 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
139 g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
140 g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
142 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
144 struct unpcbid *native = g_credentials_get_native (creds,
145 G_CREDENTIALS_TYPE_NETBSD_UNPCBID);
147 g_assert_cmpuint (native->unp_euid, ==, geteuid ());
148 g_assert_cmpuint (native->unp_pid, ==, getpid ());
150 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
152 struct sockpeercred *native = g_credentials_get_native (creds,
153 G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
155 g_assert_cmpuint (native->uid, ==, geteuid ());
156 g_assert_cmpuint (native->pid, ==, getpid ());
158 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
160 ucred_t *native = g_credentials_get_native (creds,
161 G_CREDENTIALS_TYPE_SOLARIS_UCRED);
163 g_assert_cmpuint (ucred_geteuid (native), ==, geteuid ());
164 g_assert_cmpuint (ucred_getpid (native), ==, getpid ());
167 #error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
171 #if G_CREDENTIALS_USE_LINUX_UCRED
172 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
173 "*g_credentials_get_native: Trying to get*"
174 "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
175 "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
177 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
178 g_test_assert_expected_messages ();
179 g_assert_null (bad_native_creds);
181 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
182 "*g_credentials_get_native: Trying to get*"
183 "G_CREDENTIALS_TYPE_LINUX_UCRED "
184 "but only G_CREDENTIALS_TYPE_*supported*");
185 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
186 g_test_assert_expected_messages ();
187 g_assert_null (bad_native_creds);
190 #else /* ! G_CREDENTIALS_SUPPORTED */
192 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
193 "*g_credentials_get_native: Trying to get "
194 "credentials *but*no support*");
195 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
196 g_test_assert_expected_messages ();
197 g_assert_null (bad_native_creds);
200 g_object_unref (creds);
201 g_object_unref (other);
204 #endif /* !G_OS_WIN32 */
210 g_test_init (&argc, &argv, NULL);
212 g_test_add_func ("/credentials/basic", test_basic);