• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 /**
30  * @addtogroup DBusCredentials
31  * @{
32  */
33 
34 /** @} */
35 
36 #ifdef DBUS_BUILD_TESTS
37 #include "dbus-test.h"
38 #include <stdio.h>
39 #include <string.h>
40 
41 static DBusCredentials*
make_credentials(dbus_uid_t unix_uid,dbus_pid_t unix_pid,const char * windows_sid)42 make_credentials(dbus_uid_t  unix_uid,
43                  dbus_pid_t  unix_pid,
44                  const char *windows_sid)
45 {
46   DBusCredentials *credentials;
47 
48   credentials = _dbus_credentials_new ();
49 
50   if (unix_uid != DBUS_UID_UNSET)
51     {
52       if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
53         {
54           _dbus_credentials_unref (credentials);
55           return NULL;
56         }
57     }
58 
59   if (unix_pid != DBUS_PID_UNSET)
60     {
61       if (!_dbus_credentials_add_unix_pid (credentials, unix_pid))
62         {
63           _dbus_credentials_unref (credentials);
64           return NULL;
65         }
66     }
67 
68   if (windows_sid != NULL)
69     {
70       if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
71         {
72           _dbus_credentials_unref (credentials);
73           return NULL;
74         }
75     }
76 
77   return credentials;
78 }
79 
80 #define SAMPLE_SID "whatever a windows sid looks like"
81 #define OTHER_SAMPLE_SID "whatever else"
82 
83 dbus_bool_t
_dbus_credentials_test(const char * test_data_dir)84 _dbus_credentials_test (const char *test_data_dir)
85 {
86   DBusCredentials *creds;
87   DBusCredentials *creds2;
88 
89   if (test_data_dir == NULL)
90     return TRUE;
91 
92   creds = make_credentials (12, 511, SAMPLE_SID);
93   if (creds == NULL)
94     _dbus_assert_not_reached ("oom");
95 
96   /* test refcounting */
97   _dbus_credentials_ref (creds);
98   _dbus_credentials_unref (creds);
99 
100   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
101   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
102   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
103 
104   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == 12);
105   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == 511);
106   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
107 
108   _dbus_assert (!_dbus_credentials_are_empty (creds));
109   _dbus_assert (!_dbus_credentials_are_anonymous (creds));
110 
111   /* Test copy */
112   creds2 = _dbus_credentials_copy (creds);
113   if (creds2 == NULL)
114     _dbus_assert_not_reached ("oom");
115 
116   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
117   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
118   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
119 
120   _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
121   _dbus_assert (_dbus_credentials_get_unix_pid (creds2) == 511);
122   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);
123 
124   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
125 
126   _dbus_credentials_unref (creds2);
127 
128   /* Same user if both unix and windows are the same */
129   creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
130   if (creds2 == NULL)
131     _dbus_assert_not_reached ("oom");
132 
133   _dbus_assert (_dbus_credentials_same_user (creds, creds2));
134 
135   _dbus_credentials_unref (creds2);
136 
137   /* Not the same user if Windows is missing */
138   creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
139   if (creds2 == NULL)
140     _dbus_assert_not_reached ("oom");
141 
142   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
143   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
144 
145   _dbus_credentials_unref (creds2);
146 
147   /* Not the same user if Windows is different */
148   creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
149   if (creds2 == NULL)
150     _dbus_assert_not_reached ("oom");
151 
152   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
153   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
154 
155   _dbus_credentials_unref (creds2);
156 
157   /* Not the same user if Unix is missing */
158   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
159   if (creds2 == NULL)
160     _dbus_assert_not_reached ("oom");
161 
162   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
163   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
164 
165   _dbus_credentials_unref (creds2);
166 
167   /* Not the same user if Unix is different */
168   creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
169   if (creds2 == NULL)
170     _dbus_assert_not_reached ("oom");
171 
172   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
173   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
174 
175   _dbus_credentials_unref (creds2);
176 
177   /* Not the same user if both are missing */
178   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
179   if (creds2 == NULL)
180     _dbus_assert_not_reached ("oom");
181 
182   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
183   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
184 
185   _dbus_credentials_unref (creds2);
186 
187   /* Clearing credentials works */
188   _dbus_credentials_clear (creds);
189 
190   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
191   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
192   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
193 
194   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
195   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == DBUS_PID_UNSET);
196   _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
197 
198   _dbus_assert (_dbus_credentials_are_empty (creds));
199   _dbus_assert (_dbus_credentials_are_anonymous (creds));
200 
201   _dbus_credentials_unref (creds);
202 
203   return TRUE;
204 }
205 
206 #endif /* DBUS_BUILD_TESTS */
207