Tests: Fix up the expected messages
[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 #if G_CREDENTIALS_SPOOFING_SUPPORTED
63   g_assert_no_error (error);
64   g_assert (set);
65
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);
73
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
78
79   stringified = g_credentials_to_string (creds);
80   g_test_message ("%s", stringified);
81   g_free (stringified);
82
83   stringified = g_credentials_to_string (other);
84   g_test_message ("%s", stringified);
85   g_free (stringified);
86
87 #if G_CREDENTIALS_USE_LINUX_UCRED
88         {
89           struct ucred *native = g_credentials_get_native (creds,
90               G_CREDENTIALS_TYPE_LINUX_UCRED);
91
92           g_assert_cmpuint (native->uid, ==, geteuid ());
93           g_assert_cmpuint (native->pid, ==, getpid ());
94         }
95 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
96         {
97           struct cmsgcred *native = g_credentials_get_native (creds,
98               G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
99
100           g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
101           g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
102         }
103 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
104         {
105           struct sockpeercred *native = g_credentials_get_native (creds,
106               G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
107
108           g_assert_cmpuint (native->uid, ==, geteuid ());
109           g_assert_cmpuint (native->pid, ==, getpid ());
110         }
111 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
112         {
113           ucred_t *native = g_credentials_get_native (creds,
114               G_CREDENTIALS_TYPE_SOLARIS_UCRED);
115
116           g_assert_cmpuint (ucred_geteuid (native), ==, geteuid ());
117           g_assert_cmpuint (ucred_getpid (native), ==, getpid ());
118         }
119 #else
120 #error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
121 #endif
122
123
124 #if G_CREDENTIALS_USE_LINUX_UCRED
125   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
126                          "*g_credentials_get_native: Trying to get*"
127                          "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
128                          "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
129                          "supported*");
130   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
131   g_test_assert_expected_messages ();
132   g_assert_null (bad_native_creds);
133 #else
134   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
135                          "*g_credentials_get_native: Trying to get*"
136                          "G_CREDENTIALS_TYPE_LINUX_UCRED "
137                          "but only G_CREDENTIALS_TYPE_*supported*");
138   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
139   g_test_assert_expected_messages ();
140   g_assert_null (bad_native_creds);
141 #endif
142
143 #else /* ! G_CREDENTIALS_SUPPORTED */
144
145   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
146                          "*g_credentials_get_native: Trying to get "
147                          "credentials *but*no support*");
148   bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
149   g_test_assert_expected_messages ();
150   g_assert_null (bad_native_creds);
151 #endif
152
153   g_object_unref (creds);
154   g_object_unref (other);
155 }
156
157 int
158 main (int   argc,
159       char *argv[])
160 {
161   g_test_init (&argc, &argv, NULL);
162
163   g_test_add_func ("/credentials/basic", test_basic);
164
165   return g_test_run();
166 }