1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-credentials-util.c Would be in dbus-credentials.c, but only used for tests/bus
4 * Copyright (C) 2007 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-internals.h"
24 #include "dbus-test.h"
25 #include "dbus-credentials.h"
28 * @addtogroup DBusCredentials
34 #ifdef DBUS_BUILD_TESTS
35 #include "dbus-test.h"
39 static DBusCredentials*
40 make_credentials(dbus_uid_t unix_uid,
42 const char *windows_sid)
44 DBusCredentials *credentials;
46 credentials = _dbus_credentials_new ();
48 if (unix_uid != DBUS_UID_UNSET)
50 if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
52 _dbus_credentials_unref (credentials);
57 if (unix_pid != DBUS_PID_UNSET)
59 if (!_dbus_credentials_add_unix_pid (credentials, unix_pid))
61 _dbus_credentials_unref (credentials);
66 if (windows_sid != NULL)
68 if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
70 _dbus_credentials_unref (credentials);
78 #define SAMPLE_SID "whatever a windows sid looks like"
79 #define OTHER_SAMPLE_SID "whatever else"
82 _dbus_credentials_test (const char *test_data_dir)
84 DBusCredentials *creds;
85 DBusCredentials *creds2;
87 if (test_data_dir == NULL)
90 creds = make_credentials (12, 511, SAMPLE_SID);
92 _dbus_assert_not_reached ("oom");
94 /* test refcounting */
95 _dbus_credentials_ref (creds);
96 _dbus_credentials_unref (creds);
98 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
99 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
100 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
102 _dbus_assert (_dbus_credentials_get_unix_uid (creds) == 12);
103 _dbus_assert (_dbus_credentials_get_unix_pid (creds) == 511);
104 _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
106 _dbus_assert (!_dbus_credentials_are_empty (creds));
109 creds2 = _dbus_credentials_copy (creds);
111 _dbus_assert_not_reached ("oom");
113 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
114 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
115 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
117 _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
118 _dbus_assert (_dbus_credentials_get_unix_pid (creds2) == 511);
119 _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);
121 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
123 _dbus_credentials_unref (creds2);
125 /* Same user if both unix and windows are the same */
126 creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
128 _dbus_assert_not_reached ("oom");
130 _dbus_assert (_dbus_credentials_same_user (creds, creds2));
132 _dbus_credentials_unref (creds2);
134 /* Not the same user if Windows is missing */
135 creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
137 _dbus_assert_not_reached ("oom");
139 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
140 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
142 _dbus_credentials_unref (creds2);
144 /* Not the same user if Windows is different */
145 creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
147 _dbus_assert_not_reached ("oom");
149 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
150 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
152 _dbus_credentials_unref (creds2);
154 /* Not the same user if Unix is missing */
155 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
157 _dbus_assert_not_reached ("oom");
159 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
160 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
162 _dbus_credentials_unref (creds2);
164 /* Not the same user if Unix is different */
165 creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
167 _dbus_assert_not_reached ("oom");
169 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
170 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
172 _dbus_credentials_unref (creds2);
174 /* Not the same user if both are missing */
175 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
177 _dbus_assert_not_reached ("oom");
179 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
180 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
182 _dbus_credentials_unref (creds2);
184 /* Clearing credentials works */
185 _dbus_credentials_clear (creds);
187 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
188 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
189 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
191 _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
192 _dbus_assert (_dbus_credentials_get_unix_pid (creds) == DBUS_PID_UNSET);
193 _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
195 _dbus_assert (_dbus_credentials_are_empty (creds));
197 _dbus_credentials_unref (creds);
202 #endif /* DBUS_BUILD_TESTS */