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., 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 = DBUS_ERROR_INIT;
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   dir = _dbus_directory_open (&test_directory, &error);
00069   if (dir == NULL)
00070     {
00071       _dbus_warn ("Could not open %s: %s\n",
00072                   _dbus_string_get_const_data (&test_directory),
00073                   error.message);
00074       dbus_error_free (&error);
00075       goto failed;
00076     }
00077 
00078   printf ("Testing %s:\n", subdir);
00079   
00080  next:
00081   while (_dbus_directory_get_next_file (dir, &filename, &error))
00082     {
00083       DBusString full_path;
00084       
00085       if (!_dbus_string_init (&full_path))
00086         _dbus_assert_not_reached ("couldn't init string");
00087 
00088       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
00089         _dbus_assert_not_reached ("couldn't copy dir to full_path");
00090 
00091       if (!_dbus_concat_dir_and_file (&full_path, &filename))
00092         _dbus_assert_not_reached ("couldn't concat file to dir");
00093 
00094       if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
00095         {
00096           _dbus_verbose ("Skipping non-.auth-script file %s\n",
00097                          _dbus_string_get_const_data (&filename));
00098           _dbus_string_free (&full_path);
00099           goto next;
00100         }
00101 
00102       printf ("    %s\n", _dbus_string_get_const_data (&filename));
00103       
00104       if (!_dbus_auth_script_run (&full_path))
00105         {
00106           _dbus_string_free (&full_path);
00107           goto failed;
00108         }
00109       else
00110         _dbus_string_free (&full_path);
00111     }
00112 
00113   if (dbus_error_is_set (&error))
00114     {
00115       _dbus_warn ("Could not get next file in %s: %s\n",
00116                   _dbus_string_get_const_data (&test_directory), error.message);
00117       dbus_error_free (&error);
00118       goto failed;
00119     }
00120     
00121   retval = TRUE;
00122   
00123  failed:
00124 
00125   if (dir)
00126     _dbus_directory_close (dir);
00127   _dbus_string_free (&test_directory);
00128   _dbus_string_free (&filename);
00129 
00130   return retval;
00131 }
00132 
00133 static dbus_bool_t
00134 process_test_dirs (const char *test_data_dir)
00135 {
00136   DBusString test_directory;
00137   dbus_bool_t retval;
00138 
00139   retval = FALSE;
00140   
00141   _dbus_string_init_const (&test_directory, test_data_dir);
00142 
00143   if (!process_test_subdir (&test_directory, "auth"))
00144     goto failed;
00145 
00146   retval = TRUE;
00147   
00148  failed:
00149 
00150   _dbus_string_free (&test_directory);
00151   
00152   return retval;
00153 }
00154 
00155 dbus_bool_t
00156 _dbus_auth_test (const char *test_data_dir)
00157 {
00158   
00159   if (test_data_dir == NULL)
00160     return TRUE;
00161   
00162   if (!process_test_dirs (test_data_dir))
00163     return FALSE;
00164 
00165   return TRUE;
00166 }
00167 
00168 #endif /* DBUS_BUILD_TESTS */

Generated on Tue Feb 24 16:40:39 2009 for D-Bus by  doxygen 1.5.1