D-Bus
1.10.12
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-auth-util.c Would be in dbus-auth.c, but only used for tests/bus 00003 * 00004 * Copyright (C) 2002, 2003, 2004 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-auth.h" 00028 00036 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 00037 #include "dbus-auth-script.h" 00038 #include <stdio.h> 00039 00040 static dbus_bool_t 00041 process_test_subdir (const DBusString *test_base_dir, 00042 const char *subdir) 00043 { 00044 DBusString test_directory; 00045 DBusString filename; 00046 DBusDirIter *dir; 00047 dbus_bool_t retval; 00048 DBusError error = DBUS_ERROR_INIT; 00049 00050 retval = FALSE; 00051 dir = NULL; 00052 00053 if (!_dbus_string_init (&test_directory)) 00054 _dbus_assert_not_reached ("didn't allocate test_directory\n"); 00055 00056 _dbus_string_init_const (&filename, subdir); 00057 00058 if (!_dbus_string_copy (test_base_dir, 0, 00059 &test_directory, 0)) 00060 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory"); 00061 00062 if (!_dbus_concat_dir_and_file (&test_directory, &filename)) 00063 _dbus_assert_not_reached ("couldn't allocate full path"); 00064 00065 _dbus_string_free (&filename); 00066 if (!_dbus_string_init (&filename)) 00067 _dbus_assert_not_reached ("didn't allocate filename string\n"); 00068 00069 dir = _dbus_directory_open (&test_directory, &error); 00070 if (dir == NULL) 00071 { 00072 _dbus_warn ("Could not open %s: %s\n", 00073 _dbus_string_get_const_data (&test_directory), 00074 error.message); 00075 dbus_error_free (&error); 00076 goto failed; 00077 } 00078 00079 printf ("Testing %s:\n", subdir); 00080 00081 next: 00082 while (_dbus_directory_get_next_file (dir, &filename, &error)) 00083 { 00084 DBusString full_path; 00085 00086 if (!_dbus_string_init (&full_path)) 00087 _dbus_assert_not_reached ("couldn't init string"); 00088 00089 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0)) 00090 _dbus_assert_not_reached ("couldn't copy dir to full_path"); 00091 00092 if (!_dbus_concat_dir_and_file (&full_path, &filename)) 00093 _dbus_assert_not_reached ("couldn't concat file to dir"); 00094 00095 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script")) 00096 { 00097 _dbus_verbose ("Skipping non-.auth-script file %s\n", 00098 _dbus_string_get_const_data (&filename)); 00099 _dbus_string_free (&full_path); 00100 goto next; 00101 } 00102 00103 printf (" %s\n", _dbus_string_get_const_data (&filename)); 00104 00105 if (!_dbus_auth_script_run (&full_path)) 00106 { 00107 _dbus_string_free (&full_path); 00108 goto failed; 00109 } 00110 else 00111 _dbus_string_free (&full_path); 00112 } 00113 00114 if (dbus_error_is_set (&error)) 00115 { 00116 _dbus_warn ("Could not get next file in %s: %s\n", 00117 _dbus_string_get_const_data (&test_directory), error.message); 00118 dbus_error_free (&error); 00119 goto failed; 00120 } 00121 00122 retval = TRUE; 00123 00124 failed: 00125 00126 if (dir) 00127 _dbus_directory_close (dir); 00128 _dbus_string_free (&test_directory); 00129 _dbus_string_free (&filename); 00130 00131 return retval; 00132 } 00133 00134 static dbus_bool_t 00135 process_test_dirs (const char *test_data_dir) 00136 { 00137 DBusString test_directory; 00138 dbus_bool_t retval; 00139 00140 retval = FALSE; 00141 00142 _dbus_string_init_const (&test_directory, test_data_dir); 00143 00144 if (!process_test_subdir (&test_directory, "auth")) 00145 goto failed; 00146 00147 retval = TRUE; 00148 00149 failed: 00150 00151 _dbus_string_free (&test_directory); 00152 00153 return retval; 00154 } 00155 00156 dbus_bool_t 00157 _dbus_auth_test (const char *test_data_dir) 00158 { 00159 00160 if (test_data_dir == NULL) 00161 return TRUE; 00162 00163 if (!process_test_dirs (test_data_dir)) 00164 return FALSE; 00165 00166 return TRUE; 00167 } 00168 00169 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */