dbus-auth-util.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 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-auth.h"
00026 
00034 #ifdef DBUS_BUILD_TESTS
00035 #include "dbus-test.h"
00036 #include "dbus-auth-script.h"
00037 #include <stdio.h>
00038 
00039 static dbus_bool_t
00040 process_test_subdir (const DBusString          *test_base_dir,
00041                      const char                *subdir)
00042 {
00043   DBusString test_directory;
00044   DBusString filename;
00045   DBusDirIter *dir;
00046   dbus_bool_t retval;
00047   DBusError error;
00048 
00049   retval = FALSE;
00050   dir = NULL;
00051   
00052   if (!_dbus_string_init (&test_directory))
00053     _dbus_assert_not_reached ("didn't allocate test_directory\n");
00054 
00055   _dbus_string_init_const (&filename, subdir);
00056   
00057   if (!_dbus_string_copy (test_base_dir, 0,
00058                           &test_directory, 0))
00059     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
00060   
00061   if (!_dbus_concat_dir_and_file (&test_directory, &filename))    
00062     _dbus_assert_not_reached ("couldn't allocate full path");
00063 
00064   _dbus_string_free (&filename);
00065   if (!_dbus_string_init (&filename))
00066     _dbus_assert_not_reached ("didn't allocate filename string\n");
00067 
00068   dbus_error_init (&error);
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_BUILD_TESTS */

Generated on Fri Sep 21 18:12:11 2007 for D-Bus by  doxygen 1.5.1