1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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));
107 _dbus_assert (!_dbus_credentials_are_anonymous (creds));
110 creds2 = _dbus_credentials_copy (creds);
112 _dbus_assert_not_reached ("oom");
114 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
115 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
116 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
118 _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
119 _dbus_assert (_dbus_credentials_get_unix_pid (creds2) == 511);
120 _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);
122 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
124 _dbus_credentials_unref (creds2);
126 /* Same user if both unix and windows are the same */
127 creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
129 _dbus_assert_not_reached ("oom");
131 _dbus_assert (_dbus_credentials_same_user (creds, creds2));
133 _dbus_credentials_unref (creds2);
135 /* Not the same user if Windows is missing */
136 creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
138 _dbus_assert_not_reached ("oom");
140 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
141 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
143 _dbus_credentials_unref (creds2);
145 /* Not the same user if Windows is different */
146 creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
148 _dbus_assert_not_reached ("oom");
150 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
151 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
153 _dbus_credentials_unref (creds2);
155 /* Not the same user if Unix is missing */
156 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
158 _dbus_assert_not_reached ("oom");
160 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
161 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
163 _dbus_credentials_unref (creds2);
165 /* Not the same user if Unix is different */
166 creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
168 _dbus_assert_not_reached ("oom");
170 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
171 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
173 _dbus_credentials_unref (creds2);
175 /* Not the same user if both are missing */
176 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
178 _dbus_assert_not_reached ("oom");
180 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
181 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
183 _dbus_credentials_unref (creds2);
185 /* Clearing credentials works */
186 _dbus_credentials_clear (creds);
188 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
189 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
190 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
192 _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
193 _dbus_assert (_dbus_credentials_get_unix_pid (creds) == DBUS_PID_UNSET);
194 _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
196 _dbus_assert (_dbus_credentials_are_empty (creds));
197 _dbus_assert (_dbus_credentials_are_anonymous (creds));
199 _dbus_credentials_unref (creds);
204 #endif /* DBUS_BUILD_TESTS */