Update German translation
[glib.git] / gio / tests / credentials.c
blob6d7609964bb15e2f685e6537f377b187171a8f13
1 /* GLib testing framework examples and tests
3 * Copyright © 2012 Collabora Ltd.
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.
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.
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.
23 #include "config.h"
25 #include <gio/gio.h>
26 #include <gio/gcredentialsprivate.h>
28 static void
29 test_basic (void)
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
41 /* You can always get a credentials object, but it might not work. */
42 g_assert (creds != NULL);
43 g_assert (other != NULL);
45 #if G_CREDENTIALS_SUPPORTED
46 g_assert (g_credentials_is_same_user (creds, other, &error));
47 g_assert_no_error (error);
49 if (geteuid () == 0)
50 not_me = 65534; /* traditionally 'nobody' */
51 else
52 not_me = 0;
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);
61 set = g_credentials_set_unix_user (other, not_me, &error);
62 #if G_CREDENTIALS_SPOOFING_SUPPORTED
63 g_assert_no_error (error);
64 g_assert (set);
66 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, not_me);
67 g_assert (!g_credentials_is_same_user (creds, other, &error));
68 g_assert_no_error (error);
69 #else
70 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
71 g_assert (!set);
72 g_clear_error (&error);
74 g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, geteuid ());
75 g_assert (g_credentials_is_same_user (creds, other, &error));
76 g_assert_no_error (error);
77 #endif
79 stringified = g_credentials_to_string (creds);
80 g_test_message ("%s", stringified);
81 g_free (stringified);
83 stringified = g_credentials_to_string (other);
84 g_test_message ("%s", stringified);
85 g_free (stringified);
87 #if G_CREDENTIALS_USE_LINUX_UCRED
89 struct ucred *native = g_credentials_get_native (creds,
90 G_CREDENTIALS_TYPE_LINUX_UCRED);
92 g_assert_cmpuint (native->uid, ==, geteuid ());
93 g_assert_cmpuint (native->pid, ==, getpid ());
95 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
97 struct cmsgcred *native = g_credentials_get_native (creds,
98 G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
100 g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
101 g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
103 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
105 struct unpcbid *native = g_credentials_get_native (creds,
106 G_CREDENTIALS_TYPE_NETBSD_UNPCBID);
108 g_assert_cmpuint (native->unp_euid, ==, geteuid ());
109 g_assert_cmpuint (native->unp_pid, ==, getpid ());
111 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
113 struct sockpeercred *native = g_credentials_get_native (creds,
114 G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
116 g_assert_cmpuint (native->uid, ==, geteuid ());
117 g_assert_cmpuint (native->pid, ==, getpid ());
119 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
121 ucred_t *native = g_credentials_get_native (creds,
122 G_CREDENTIALS_TYPE_SOLARIS_UCRED);
124 g_assert_cmpuint (ucred_geteuid (native), ==, geteuid ());
125 g_assert_cmpuint (ucred_getpid (native), ==, getpid ());
127 #else
128 #error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
129 #endif
132 #if G_CREDENTIALS_USE_LINUX_UCRED
133 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
134 "*g_credentials_get_native: Trying to get*"
135 "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
136 "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
137 "supported*");
138 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
139 g_test_assert_expected_messages ();
140 g_assert_null (bad_native_creds);
141 #else
142 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
143 "*g_credentials_get_native: Trying to get*"
144 "G_CREDENTIALS_TYPE_LINUX_UCRED "
145 "but only G_CREDENTIALS_TYPE_*supported*");
146 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
147 g_test_assert_expected_messages ();
148 g_assert_null (bad_native_creds);
149 #endif
151 #else /* ! G_CREDENTIALS_SUPPORTED */
153 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
154 "*g_credentials_get_native: Trying to get "
155 "credentials *but*no support*");
156 bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
157 g_test_assert_expected_messages ();
158 g_assert_null (bad_native_creds);
159 #endif
161 g_object_unref (creds);
162 g_object_unref (other);
166 main (int argc,
167 char *argv[])
169 g_test_init (&argc, &argv, NULL);
171 g_test_add_func ("/credentials/basic", test_basic);
173 return g_test_run();