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