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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #include "dbus-internals.h"
00024 #include "dbus-test.h"
00025 #include "dbus-credentials.h"
00026 
00034 #ifdef DBUS_BUILD_TESTS
00035 #include "dbus-test.h"
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 static DBusCredentials*
00040 make_credentials(dbus_uid_t  unix_uid,
00041                  dbus_pid_t  unix_pid,
00042                  const char *windows_sid)
00043 {
00044   DBusCredentials *credentials;
00045 
00046   credentials = _dbus_credentials_new ();
00047 
00048   if (unix_uid != DBUS_UID_UNSET)
00049     {
00050       if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
00051         {
00052           _dbus_credentials_unref (credentials);
00053           return NULL;
00054         }
00055     }
00056 
00057   if (unix_pid != DBUS_PID_UNSET)
00058     {
00059       if (!_dbus_credentials_add_unix_pid (credentials, unix_pid))
00060         {
00061           _dbus_credentials_unref (credentials);
00062           return NULL;
00063         }
00064     }
00065 
00066   if (windows_sid != NULL)
00067     {
00068       if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
00069         {
00070           _dbus_credentials_unref (credentials);
00071           return NULL;
00072         }
00073     }
00074 
00075   return credentials;
00076 }
00077 
00078 #define SAMPLE_SID "whatever a windows sid looks like"
00079 #define OTHER_SAMPLE_SID "whatever else"
00080 
00081 dbus_bool_t
00082 _dbus_credentials_test (const char *test_data_dir)
00083 {
00084   DBusCredentials *creds;
00085   DBusCredentials *creds2;
00086   
00087   if (test_data_dir == NULL)
00088     return TRUE;
00089 
00090   creds = make_credentials (12, 511, SAMPLE_SID);
00091   if (creds == NULL)
00092     _dbus_assert_not_reached ("oom");
00093 
00094   /* test refcounting */
00095   _dbus_credentials_ref (creds);
00096   _dbus_credentials_unref (creds);
00097   
00098   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
00099   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00100   _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
00101 
00102   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == 12);
00103   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == 511);
00104   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
00105 
00106   _dbus_assert (!_dbus_credentials_are_empty (creds));
00107   _dbus_assert (!_dbus_credentials_are_anonymous (creds));
00108 
00109   /* Test copy */
00110   creds2 = _dbus_credentials_copy (creds);
00111   if (creds2 == NULL)
00112     _dbus_assert_not_reached ("oom");
00113 
00114   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
00115   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00116   _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
00117 
00118   _dbus_assert (_dbus_credentials_get_unix_uid (creds2) == 12);
00119   _dbus_assert (_dbus_credentials_get_unix_pid (creds2) == 511);
00120   _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);  
00121 
00122   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00123   
00124   _dbus_credentials_unref (creds2);
00125   
00126   /* Same user if both unix and windows are the same */
00127   creds2 = make_credentials (12, DBUS_PID_UNSET, SAMPLE_SID);
00128   if (creds2 == NULL)
00129     _dbus_assert_not_reached ("oom");
00130 
00131   _dbus_assert (_dbus_credentials_same_user (creds, creds2));
00132 
00133   _dbus_credentials_unref (creds2);
00134 
00135   /* Not the same user if Windows is missing */
00136   creds2 = make_credentials (12, DBUS_PID_UNSET, NULL);
00137   if (creds2 == NULL)
00138     _dbus_assert_not_reached ("oom");
00139 
00140   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00141   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00142   
00143   _dbus_credentials_unref (creds2);
00144 
00145   /* Not the same user if Windows is different */
00146   creds2 = make_credentials (12, DBUS_PID_UNSET, OTHER_SAMPLE_SID);
00147   if (creds2 == NULL)
00148     _dbus_assert_not_reached ("oom");
00149 
00150   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00151   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
00152   
00153   _dbus_credentials_unref (creds2);
00154 
00155   /* Not the same user if Unix is missing */
00156   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, SAMPLE_SID);
00157   if (creds2 == NULL)
00158     _dbus_assert_not_reached ("oom");
00159 
00160   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00161   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00162   
00163   _dbus_credentials_unref (creds2);
00164 
00165   /* Not the same user if Unix is different */
00166   creds2 = make_credentials (15, DBUS_PID_UNSET, SAMPLE_SID);
00167   if (creds2 == NULL)
00168     _dbus_assert_not_reached ("oom");
00169 
00170   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00171   _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
00172   
00173   _dbus_credentials_unref (creds2);
00174 
00175   /* Not the same user if both are missing */
00176   creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, NULL);
00177   if (creds2 == NULL)
00178     _dbus_assert_not_reached ("oom");
00179 
00180   _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
00181   _dbus_assert (_dbus_credentials_are_superset (creds, creds2));
00182   
00183   _dbus_credentials_unref (creds2);
00184 
00185   /* Clearing credentials works */
00186   _dbus_credentials_clear (creds);
00187 
00188   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
00189   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
00190   _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
00191 
00192   _dbus_assert (_dbus_credentials_get_unix_uid (creds) == DBUS_UID_UNSET);
00193   _dbus_assert (_dbus_credentials_get_unix_pid (creds) == DBUS_PID_UNSET);
00194   _dbus_assert (_dbus_credentials_get_windows_sid (creds) == NULL);
00195 
00196   _dbus_assert (_dbus_credentials_are_empty (creds));
00197   _dbus_assert (_dbus_credentials_are_anonymous (creds));
00198 
00199   _dbus_credentials_unref (creds);
00200   
00201   return TRUE;
00202 }
00203 
00204 #endif /* DBUS_BUILD_TESTS */

Generated on Sat Dec 6 19:43:43 2008 for D-Bus by  doxygen 1.5.1