• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List

dbus-credentials-util.c

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-credentials-util.c Would be in dbus-credentials.c, but only used for tests/bus
00003  *
00004  * Copyright (C) 2007 Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  *
00022  */
00023 
00024 #include <config.h>
00025 #include "dbus-internals.h"
00026 #include "dbus-test.h"
00027 #include "dbus-credentials.h"
00028 
00036 #ifdef DBUS_BUILD_TESTS
00037 #include "dbus-test.h"
00038 #include <stdio.h>
00039 #include <string.h>
00040 
00041 static DBusCredentials*
00042 make_credentials(dbus_uid_t  unix_uid,
00043                  dbus_pid_t  unix_pid,
00044                  const char *windows_sid)
00045 {
00046   DBusCredentials *credentials;
00047 
00048   credentials = _dbus_credentials_new ();
00049 
00050   if (unix_uid != DBUS_UID_UNSET)
00051     {
00052       if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
00053         {
00054           _dbus_credentials_unref (credentials);
00055           return NULL;
00056         }
00057     }
00058 
00059   if (unix_pid != DBUS_PID_UNSET)
00060     {
00061       if (!_dbus_credentials_add_unix_pid (credentials, unix_pid))
00062         {
00063           _dbus_credentials_unref (credentials);
00064           return NULL;
00065         }
00066     }
00067 
00068   if (windows_sid != NULL)
00069     {
00070       if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
00071         {
00072           _dbus_credentials_unref (credentials);
00073           return NULL;
00074         }
00075     }
00076 
00077   return credentials;
00078 }
00079 
00080 #define SAMPLE_SID "whatever a windows sid looks like"
00081 #define OTHER_SAMPLE_SID "whatever else"
00082 
00083 dbus_bool_t
00084 _dbus_credentials_test (const char *test_data_dir)
00085 {
00086   DBusCredentials *creds;
00087   DBusCredentials *creds2;
00088   
00089   if (test_data_dir == NULL)
00090     return TRUE;
00091 
00092   creds = make_credentials (12, 511, SAMPLE_SID);
00093   if (creds == NULL)
00094     _dbus_assert_not_reached ("oom");
00095 
00096   /* test refcounting */
00097   _dbus_credentials_ref (creds);
00098   _dbus_credentials_unref (creds);
00099   
00100   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
00101   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00102   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
00103 
00104   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == 12);
00105   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == 511);
00106   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
00107 
00108   _dbus_assert (!_dbus_credentials_are_empty (creds));
00109   _dbus_assert (!_dbus_credentials_are_anonymous (creds));
00110 
00111   /* Test copy */
00112   creds2 = _dbus_credentials_copy (creds);
00113   if (creds2 == NULL)
00114     _dbus_assert_not_reached ("oom");
00115 
00116   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
00117   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00118   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
00119 
00120   _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
00121   _dbus_assert (_dbus_credentials_get_unix_pid (creds2) == 511);
00122   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);  
00123 
00124   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00125   
00126   _dbus_credentials_unref (creds2);
00127   
00128   /* Same user if both unix and windows are the same */
00129   creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
00130   if (creds2 == NULL)
00131     _dbus_assert_not_reached ("oom");
00132 
00133   _dbus_assert (_dbus_credentials_same_user (creds, creds2));
00134 
00135   _dbus_credentials_unref (creds2);
00136 
00137   /* Not the same user if Windows is missing */
00138   creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
00139   if (creds2 == NULL)
00140     _dbus_assert_not_reached ("oom");
00141 
00142   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00143   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00144   
00145   _dbus_credentials_unref (creds2);
00146 
00147   /* Not the same user if Windows is different */
00148   creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
00149   if (creds2 == NULL)
00150     _dbus_assert_not_reached ("oom");
00151 
00152   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00153   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
00154   
00155   _dbus_credentials_unref (creds2);
00156 
00157   /* Not the same user if Unix is missing */
00158   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
00159   if (creds2 == NULL)
00160     _dbus_assert_not_reached ("oom");
00161 
00162   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00163   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00164   
00165   _dbus_credentials_unref (creds2);
00166 
00167   /* Not the same user if Unix is different */
00168   creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
00169   if (creds2 == NULL)
00170     _dbus_assert_not_reached ("oom");
00171 
00172   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00173   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
00174   
00175   _dbus_credentials_unref (creds2);
00176 
00177   /* Not the same user if both are missing */
00178   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
00179   if (creds2 == NULL)
00180     _dbus_assert_not_reached ("oom");
00181 
00182   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00183   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00184   
00185   _dbus_credentials_unref (creds2);
00186 
00187   /* Clearing credentials works */
00188   _dbus_credentials_clear (creds);
00189 
00190   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
00191   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00192   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
00193 
00194   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
00195   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == DBUS_PID_UNSET);
00196   _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
00197 
00198   _dbus_assert (_dbus_credentials_are_empty (creds));
00199   _dbus_assert (_dbus_credentials_are_anonymous (creds));
00200 
00201   _dbus_credentials_unref (creds);
00202   
00203   return TRUE;
00204 }
00205 
00206 #endif /* DBUS_BUILD_TESTS */

Generated on Sun Aug 28 2011 for D-Bus by  doxygen 1.7.1