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