added dbus support
[monky] / src / dbus / dbus-auth-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-auth-util.c Would be in dbus-auth.c, but only used for tests/bus
3  *
4  * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-internals.h"
24 #include "dbus-test.h"
25 #include "dbus-auth.h"
26
27 /**
28  * @addtogroup DBusAuth
29  * @{
30  */
31
32 /** @} */
33
34 #ifdef DBUS_BUILD_TESTS
35 #include "dbus-test.h"
36 #include "dbus-auth-script.h"
37 #include <stdio.h>
38
39 static dbus_bool_t
40 process_test_subdir (const DBusString          *test_base_dir,
41                      const char                *subdir)
42 {
43   DBusString test_directory;
44   DBusString filename;
45   DBusDirIter *dir;
46   dbus_bool_t retval;
47   DBusError error = DBUS_ERROR_INIT;
48
49   retval = FALSE;
50   dir = NULL;
51   
52   if (!_dbus_string_init (&test_directory))
53     _dbus_assert_not_reached ("didn't allocate test_directory\n");
54
55   _dbus_string_init_const (&filename, subdir);
56   
57   if (!_dbus_string_copy (test_base_dir, 0,
58                           &test_directory, 0))
59     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
60   
61   if (!_dbus_concat_dir_and_file (&test_directory, &filename))    
62     _dbus_assert_not_reached ("couldn't allocate full path");
63
64   _dbus_string_free (&filename);
65   if (!_dbus_string_init (&filename))
66     _dbus_assert_not_reached ("didn't allocate filename string\n");
67
68   dir = _dbus_directory_open (&test_directory, &error);
69   if (dir == NULL)
70     {
71       _dbus_warn ("Could not open %s: %s\n",
72                   _dbus_string_get_const_data (&test_directory),
73                   error.message);
74       dbus_error_free (&error);
75       goto failed;
76     }
77
78   printf ("Testing %s:\n", subdir);
79   
80  next:
81   while (_dbus_directory_get_next_file (dir, &filename, &error))
82     {
83       DBusString full_path;
84       
85       if (!_dbus_string_init (&full_path))
86         _dbus_assert_not_reached ("couldn't init string");
87
88       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
89         _dbus_assert_not_reached ("couldn't copy dir to full_path");
90
91       if (!_dbus_concat_dir_and_file (&full_path, &filename))
92         _dbus_assert_not_reached ("couldn't concat file to dir");
93
94       if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
95         {
96           _dbus_verbose ("Skipping non-.auth-script file %s\n",
97                          _dbus_string_get_const_data (&filename));
98           _dbus_string_free (&full_path);
99           goto next;
100         }
101
102       printf ("    %s\n", _dbus_string_get_const_data (&filename));
103       
104       if (!_dbus_auth_script_run (&full_path))
105         {
106           _dbus_string_free (&full_path);
107           goto failed;
108         }
109       else
110         _dbus_string_free (&full_path);
111     }
112
113   if (dbus_error_is_set (&error))
114     {
115       _dbus_warn ("Could not get next file in %s: %s\n",
116                   _dbus_string_get_const_data (&test_directory), error.message);
117       dbus_error_free (&error);
118       goto failed;
119     }
120     
121   retval = TRUE;
122   
123  failed:
124
125   if (dir)
126     _dbus_directory_close (dir);
127   _dbus_string_free (&test_directory);
128   _dbus_string_free (&filename);
129
130   return retval;
131 }
132
133 static dbus_bool_t
134 process_test_dirs (const char *test_data_dir)
135 {
136   DBusString test_directory;
137   dbus_bool_t retval;
138
139   retval = FALSE;
140   
141   _dbus_string_init_const (&test_directory, test_data_dir);
142
143   if (!process_test_subdir (&test_directory, "auth"))
144     goto failed;
145
146   retval = TRUE;
147   
148  failed:
149
150   _dbus_string_free (&test_directory);
151   
152   return retval;
153 }
154
155 dbus_bool_t
156 _dbus_auth_test (const char *test_data_dir)
157 {
158   
159   if (test_data_dir == NULL)
160     return TRUE;
161   
162   if (!process_test_dirs (test_data_dir))
163     return FALSE;
164
165   return TRUE;
166 }
167
168 #endif /* DBUS_BUILD_TESTS */