D-Bus 1.12.4

dbus-credentials-util.c
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
3 *
4 * Copyright (C) 2007 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include <config.h>
25#include "dbus-internals.h"
26#include "dbus-test.h"
27#include "dbus-credentials.h"
28
36#ifdef DBUS_ENABLE_EMBEDDED_TESTS
37#include <stdio.h>
38#include <string.h>
39
40 static DBusCredentials*
41make_credentials(dbus_uid_t unix_uid,
42 dbus_pid_t pid,
43 const char *windows_sid)
44{
45 DBusCredentials *credentials;
46
47 credentials = _dbus_credentials_new ();
48
49 if (unix_uid != DBUS_UID_UNSET)
50 {
51 if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
52 {
53 _dbus_credentials_unref (credentials);
54 return NULL;
55 }
56 }
57
58 if (pid != DBUS_PID_UNSET)
59 {
60 if (!_dbus_credentials_add_pid (credentials, pid))
61 {
62 _dbus_credentials_unref (credentials);
63 return NULL;
64 }
65 }
66
67 if (windows_sid != NULL)
68 {
69 if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
70 {
71 _dbus_credentials_unref (credentials);
72 return NULL;
73 }
74 }
75
76 return credentials;
77}
78
79#define SAMPLE_SID "whatever a windows sid looks like"
80#define OTHER_SAMPLE_SID "whatever else"
81
82 dbus_bool_t
83_dbus_credentials_test (const char *test_data_dir)
84{
85 DBusCredentials *creds;
86 DBusCredentials *creds2;
87
88 if (test_data_dir == NULL)
89 return TRUE;
90
91 creds = make_credentials (12, 511, SAMPLE_SID);
92 if (creds == NULL)
93 _dbus_assert_not_reached ("oom");
94
95 /* test refcounting */
96 _dbus_credentials_ref (creds);
97 _dbus_credentials_unref (creds);
98
99 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
100 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
101 _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
102
103 _dbus_assert (_dbus_credentials_get_unix_uid (creds) == 12);
104 _dbus_assert (_dbus_credentials_get_pid (creds) == 511);
105 _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
106
107 _dbus_assert (!_dbus_credentials_are_empty (creds));
108 _dbus_assert (!_dbus_credentials_are_anonymous (creds));
109
110 /* Test copy */
111 creds2 = _dbus_credentials_copy (creds);
112 if (creds2 == NULL)
113 _dbus_assert_not_reached ("oom");
114
115 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
116 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
117 _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
118
119 _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
120 _dbus_assert (_dbus_credentials_get_pid (creds2) == 511);
121 _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);
122
123 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
124
125 _dbus_credentials_unref (creds2);
126
127 /* Same user if both unix and windows are the same */
128 creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
129 if (creds2 == NULL)
130 _dbus_assert_not_reached ("oom");
131
132 _dbus_assert (_dbus_credentials_same_user (creds, creds2));
133
134 _dbus_credentials_unref (creds2);
135
136 /* Not the same user if Windows is missing */
137 creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
138 if (creds2 == NULL)
139 _dbus_assert_not_reached ("oom");
140
141 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
142 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
143
144 _dbus_credentials_unref (creds2);
145
146 /* Not the same user if Windows is different */
147 creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
148 if (creds2 == NULL)
149 _dbus_assert_not_reached ("oom");
150
151 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
152 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
153
154 _dbus_credentials_unref (creds2);
155
156 /* Not the same user if Unix is missing */
157 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
158 if (creds2 == NULL)
159 _dbus_assert_not_reached ("oom");
160
161 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
162 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
163
164 _dbus_credentials_unref (creds2);
165
166 /* Not the same user if Unix is different */
167 creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
168 if (creds2 == NULL)
169 _dbus_assert_not_reached ("oom");
170
171 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
172 _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
173
174 _dbus_credentials_unref (creds2);
175
176 /* Not the same user if both are missing */
177 creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
178 if (creds2 == NULL)
179 _dbus_assert_not_reached ("oom");
180
181 _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
182 _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
183
184 _dbus_credentials_unref (creds2);
185
186 /* Clearing credentials works */
187 _dbus_credentials_clear (creds);
188
189 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
190 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
191 _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
192
193 _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
194 _dbus_assert (_dbus_credentials_get_pid (creds) == DBUS_PID_UNSET);
195 _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
196
197 _dbus_assert (_dbus_credentials_are_empty (creds));
198 _dbus_assert (_dbus_credentials_are_anonymous (creds));
199
200 _dbus_credentials_unref (creds);
201
202 return TRUE;
203}
204
205#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_credentials_ref
void _dbus_credentials_ref(DBusCredentials *credentials)
Increment refcount on credentials.
Definition: dbus-credentials.c:118
_dbus_credentials_include
dbus_bool_t _dbus_credentials_include(DBusCredentials *credentials, DBusCredentialType type)
Checks whether the given credential is present.
Definition: dbus-credentials.c:255
_dbus_credentials_are_superset
dbus_bool_t _dbus_credentials_are_superset(DBusCredentials *credentials, DBusCredentials *possible_subset)
Checks whether the first credentials object contains all the credentials found in the second credenti...
Definition: dbus-credentials.c:363
_dbus_credentials_same_user
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
Definition: dbus-credentials.c:552
_dbus_credentials_clear
void _dbus_credentials_clear(DBusCredentials *credentials)
Clear all credentials in the object.
Definition: dbus-credentials.c:503
_dbus_credentials_get_unix_uid
dbus_uid_t _dbus_credentials_get_unix_uid(DBusCredentials *credentials)
Gets the UNIX user ID in the credentials, or DBUS_UID_UNSET if the credentials object doesn't contain...
Definition: dbus-credentials.c:297
_dbus_credentials_copy
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
Definition: dbus-credentials.c:523
_dbus_credentials_new
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
Definition: dbus-credentials.c:71
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition: dbus-credentials.c:130
_dbus_credentials_are_empty
dbus_bool_t _dbus_credentials_are_empty(DBusCredentials *credentials)
Checks whether a credentials object contains anything.
Definition: dbus-credentials.c:391
_dbus_credentials_add_unix_uid
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
Definition: dbus-credentials.c:167
_dbus_credentials_add_windows_sid
dbus_bool_t _dbus_credentials_add_windows_sid(DBusCredentials *credentials, const char *windows_sid)
Add a Windows user SID to the credentials.
Definition: dbus-credentials.c:183
_dbus_credentials_add_pid
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
Definition: dbus-credentials.c:152
_dbus_credentials_get_pid
dbus_pid_t _dbus_credentials_get_pid(DBusCredentials *credentials)
Gets the UNIX process ID in the credentials, or DBUS_PID_UNSET if the credentials object doesn't cont...
Definition: dbus-credentials.c:284
_dbus_credentials_get_windows_sid
const char * _dbus_credentials_get_windows_sid(DBusCredentials *credentials)
Gets the Windows user SID in the credentials, or NULL if the credentials object doesn't contain a Win...
Definition: dbus-credentials.c:310
_dbus_credentials_are_anonymous
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
Definition: dbus-credentials.c:408
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition: dbus-internals.h:163
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
TRUE
#define TRUE
Expands to "1".
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:108
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:106
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:115
DBUS_PID_UNSET
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:113
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusCredentials
Definition: dbus-credentials.c:48