D-Bus  1.13.7
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-test.h"
27 #include "dbus-auth.h"
28 #include <dbus/dbus-test-tap.h>
29 
37 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
38 #include "dbus-auth-script.h"
39 #include <stdio.h>
40 
41 static dbus_bool_t
42 process_test_subdir (const DBusString *test_base_dir,
43  const char *subdir)
44 {
45  DBusString test_directory;
46  DBusString filename;
47  DBusDirIter *dir;
48  dbus_bool_t retval;
49  DBusError error = DBUS_ERROR_INIT;
50 
51  retval = FALSE;
52  dir = NULL;
53 
54  if (!_dbus_string_init (&test_directory))
55  _dbus_test_fatal ("didn't allocate test_directory");
56 
57  _dbus_string_init_const (&filename, subdir);
58 
59  if (!_dbus_string_copy (test_base_dir, 0,
60  &test_directory, 0))
61  _dbus_test_fatal ("couldn't copy test_base_dir to test_directory");
62 
63  if (!_dbus_concat_dir_and_file (&test_directory, &filename))
64  _dbus_test_fatal ("couldn't allocate full path");
65 
66  _dbus_string_free (&filename);
67  if (!_dbus_string_init (&filename))
68  _dbus_test_fatal ("didn't allocate filename string");
69 
70  dir = _dbus_directory_open (&test_directory, &error);
71  if (dir == NULL)
72  {
73  _dbus_warn ("Could not open %s: %s",
74  _dbus_string_get_const_data (&test_directory),
75  error.message);
76  dbus_error_free (&error);
77  goto failed;
78  }
79 
80  _dbus_test_diag ("Testing %s:", subdir);
81 
82  next:
83  while (_dbus_directory_get_next_file (dir, &filename, &error))
84  {
85  DBusString full_path;
86 
87  if (!_dbus_string_init (&full_path))
88  _dbus_test_fatal ("couldn't init string");
89 
90  if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
91  _dbus_test_fatal ("couldn't copy dir to full_path");
92 
93  if (!_dbus_concat_dir_and_file (&full_path, &filename))
94  _dbus_test_fatal ("couldn't concat file to dir");
95 
96  if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
97  {
98  _dbus_verbose ("Skipping non-.auth-script file %s\n",
99  _dbus_string_get_const_data (&filename));
100  _dbus_string_free (&full_path);
101  goto next;
102  }
103 
104  _dbus_test_diag (" %s", _dbus_string_get_const_data (&filename));
105 
106  if (!_dbus_auth_script_run (&full_path))
107  {
108  _dbus_string_free (&full_path);
109  goto failed;
110  }
111  else
112  _dbus_string_free (&full_path);
113  }
114 
115  if (dbus_error_is_set (&error))
116  {
117  _dbus_warn ("Could not get next file in %s: %s",
118  _dbus_string_get_const_data (&test_directory), error.message);
119  dbus_error_free (&error);
120  goto failed;
121  }
122 
123  retval = TRUE;
124 
125  failed:
126 
127  if (dir)
128  _dbus_directory_close (dir);
129  _dbus_string_free (&test_directory);
130  _dbus_string_free (&filename);
131 
132  return retval;
133 }
134 
135 static dbus_bool_t
136 process_test_dirs (const char *test_data_dir)
137 {
138  DBusString test_directory;
139  dbus_bool_t retval;
140 
141  retval = FALSE;
142 
143  _dbus_string_init_const (&test_directory, test_data_dir);
144 
145  if (!process_test_subdir (&test_directory, "auth"))
146  goto failed;
147 
148  retval = TRUE;
149 
150  failed:
151 
152  _dbus_string_free (&test_directory);
153 
154  return retval;
155 }
156 
158 _dbus_auth_test (const char *test_data_dir)
159 {
160 
161  if (test_data_dir == NULL)
162  return TRUE;
163 
164  if (!process_test_dirs (test_data_dir))
165  return FALSE;
166 
167  return TRUE;
168 }
169 
170 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
void _dbus_directory_close(DBusDirIter *iter)
Closes a directory iteration.
dbus_bool_t _dbus_directory_get_next_file(DBusDirIter *iter, DBusString *filename, DBusError *error)
Get next file in the directory.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1300
dbus_bool_t _dbus_string_ends_with_c_str(const DBusString *a, const char *c_str)
Returns whether a string ends with the given suffix.
Internals of directory iterator.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Object representing an exception.
Definition: dbus-errors.h:48
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329