2007-06-09 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-credentials-util.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-internals.h"
24 #include "dbus-test.h"
25 #include "dbus-credentials.h"
26
27 /**
28  * @addtogroup DBusCredentials
29  * @{
30  */
31
32 /** @} */
33
34 #ifdef DBUS_BUILD_TESTS
35 #include "dbus-test.h"
36 #include <stdio.h>
37 #include <string.h>
38
39 static DBusCredentials*
40 make_credentials(dbus_uid_t  unix_uid,
41                  dbus_pid_t  unix_pid,
42                  const char *windows_sid)
43 {
44   DBusCredentials *credentials;
45
46   credentials = _dbus_credentials_new ();
47
48   if (unix_uid != DBUS_UID_UNSET)
49     {
50       if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
51         {
52           _dbus_credentials_unref (credentials);
53           return NULL;
54         }
55     }
56
57   if (unix_pid != DBUS_PID_UNSET)
58     {
59       if (!_dbus_credentials_add_unix_pid (credentials, unix_pid))
60         {
61           _dbus_credentials_unref (credentials);
62           return NULL;
63         }
64     }
65
66   if (windows_sid != NULL)
67     {
68       if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
69         {
70           _dbus_credentials_unref (credentials);
71           return NULL;
72         }
73     }
74
75   return credentials;
76 }
77
78 #define SAMPLE_SID "whatever a windows sid looks like"
79 #define OTHER_SAMPLE_SID "whatever else"
80
81 dbus_bool_t
82 _dbus_credentials_test (const char *test_data_dir)
83 {
84   DBusCredentials *creds;
85   DBusCredentials *creds2;
86   
87   if (test_data_dir == NULL)
88     return TRUE;
89
90   creds = make_credentials (12, 511, SAMPLE_SID);
91   if (creds == NULL)
92     _dbus_assert_not_reached ("oom");
93
94   /* test refcounting */
95   _dbus_credentials_ref (creds);
96   _dbus_credentials_unref (creds);
97   
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));
101
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);
105
106   _dbus_assert (!_dbus_credentials_are_empty (creds));
107
108   /* Test copy */
109   creds2 = _dbus_credentials_copy (creds);
110   if (creds2 == NULL)
111     _dbus_assert_not_reached ("oom");
112
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));
116
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);  
120
121   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
122   
123   _dbus_credentials_unref (creds2);
124   
125   /* Same user if both unix and windows are the same */
126   creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
127   if (creds2 == NULL)
128     _dbus_assert_not_reached ("oom");
129
130   _dbus_assert (_dbus_credentials_same_user (creds, creds2));
131
132   _dbus_credentials_unref (creds2);
133
134   /* Not the same user if Windows is missing */
135   creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
136   if (creds2 == NULL)
137     _dbus_assert_not_reached ("oom");
138
139   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
140   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
141   
142   _dbus_credentials_unref (creds2);
143
144   /* Not the same user if Windows is different */
145   creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
146   if (creds2 == NULL)
147     _dbus_assert_not_reached ("oom");
148
149   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
150   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
151   
152   _dbus_credentials_unref (creds2);
153
154   /* Not the same user if Unix is missing */
155   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
156   if (creds2 == NULL)
157     _dbus_assert_not_reached ("oom");
158
159   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
160   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
161   
162   _dbus_credentials_unref (creds2);
163
164   /* Not the same user if Unix is different */
165   creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
166   if (creds2 == NULL)
167     _dbus_assert_not_reached ("oom");
168
169   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
170   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
171   
172   _dbus_credentials_unref (creds2);
173
174   /* Not the same user if both are missing */
175   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
176   if (creds2 == NULL)
177     _dbus_assert_not_reached ("oom");
178
179   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
180   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
181   
182   _dbus_credentials_unref (creds2);
183
184   /* Clearing credentials works */
185   _dbus_credentials_clear (creds);
186
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));
190
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);
194
195   _dbus_assert (_dbus_credentials_are_empty (creds));
196
197   _dbus_credentials_unref (creds);
198   
199   return TRUE;
200 }
201
202 #endif /* DBUS_BUILD_TESTS */