1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth-script.c Test DBusAuth using a special script file (internal to D-BUS implementation)
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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
25 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-auth-script.h"
28 #include "dbus-auth.h"
29 #include "dbus-string.h"
30 #include "dbus-hash.h"
31 #include "dbus-internals.h"
32 #include "dbus-marshal.h"
35 * @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
36 * @ingroup DBusInternals
37 * @brief DBusAuth unit test scripting
39 * The code in here is used for unit testing, it loads
40 * up a script that tests DBusAuth.
46 append_quoted_string (DBusString *dest,
47 const DBusString *quoted)
49 dbus_bool_t in_quotes = FALSE;
53 while (i < _dbus_string_get_length (quoted))
57 b = _dbus_string_get_byte (quoted, i);
65 if (!_dbus_string_append_byte (dest, b))
73 else if (b == ' ' || b == '\n' || b == '\t')
74 break; /* end on whitespace if not quoted */
77 if (!_dbus_string_append_byte (dest, b))
85 if (!_dbus_string_append_byte (dest, '\0'))
91 same_first_word (const DBusString *a,
94 int first_a_blank, first_b_blank;
96 _dbus_string_find_blank (a, 0, &first_a_blank);
97 _dbus_string_find_blank (b, 0, &first_b_blank);
99 if (first_a_blank != first_b_blank)
102 return _dbus_string_equal_len (a, b, first_a_blank);
106 auth_state_from_string (const DBusString *str)
108 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
109 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
110 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
111 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
112 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
113 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
114 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
115 return DBUS_AUTH_STATE_NEED_DISCONNECT;
116 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED_WITH_UNUSED_BYTES"))
117 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
118 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
119 return DBUS_AUTH_STATE_AUTHENTICATED;
125 auth_state_to_string (DBusAuthState state)
129 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
130 return "WAITING_FOR_INPUT";
131 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
132 return "WAITING_FOR_MEMORY";
133 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
134 return "HAVE_BYTES_TO_SEND";
135 case DBUS_AUTH_STATE_NEED_DISCONNECT:
136 return "NEED_DISCONNECT";
137 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
138 return "AUTHENTICATED_WITH_UNUSED_BYTES";
139 case DBUS_AUTH_STATE_AUTHENTICATED:
140 return "AUTHENTICATED";
147 _dbus_auth_script_run (const DBusString *filename)
150 DBusResultCode result;
155 DBusString from_auth;
161 if (!_dbus_string_init (&file, _DBUS_INT_MAX))
164 if (!_dbus_string_init (&line, _DBUS_INT_MAX))
166 _dbus_string_free (&file);
170 if (!_dbus_string_init (&from_auth, _DBUS_INT_MAX))
172 _dbus_string_free (&file);
173 _dbus_string_free (&line);
177 if ((result = _dbus_file_get_contents (&file, filename)) != DBUS_RESULT_SUCCESS)
180 _dbus_string_get_const_data (filename, &s);
181 _dbus_warn ("Getting contents of %s failed: %s\n",
182 s, dbus_result_to_string (result));
187 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
190 while (_dbus_string_pop_line (&file, &line))
194 _dbus_string_delete_leading_blanks (&line);
198 while ((state = _dbus_auth_do_work (auth)) ==
199 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
201 const DBusString *tmp;
202 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
204 int count = _dbus_string_get_length (tmp);
206 if (_dbus_string_copy (tmp, 0, &from_auth,
207 _dbus_string_get_length (&from_auth)))
208 _dbus_auth_bytes_sent (auth, count);
213 if (_dbus_string_get_length (&line) == 0)
218 else if (_dbus_string_starts_with_c_str (&line,
221 /* Ignore this comment */
224 else if (_dbus_string_starts_with_c_str (&line,
229 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
233 auth = _dbus_auth_client_new ();
236 _dbus_warn ("no memory to create DBusAuth\n");
240 else if (_dbus_string_starts_with_c_str (&line,
245 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
249 auth = _dbus_auth_server_new ();
252 _dbus_warn ("no memory to create DBusAuth\n");
256 else if (auth == NULL)
258 _dbus_warn ("must specify CLIENT or SERVER\n");
262 else if (_dbus_string_starts_with_c_str (&line,
267 _dbus_string_delete_first_word (&line);
269 if (!_dbus_string_init (&to_send, _DBUS_INT_MAX))
271 _dbus_warn ("no memory to allocate string\n");
275 if (!append_quoted_string (&to_send, &line))
277 _dbus_warn ("failed to append quoted string line %d\n",
279 _dbus_string_free (&to_send);
283 if (!_dbus_auth_bytes_received (auth, &to_send))
285 _dbus_warn ("not enough memory to call bytes_received\n");
286 _dbus_string_free (&to_send);
290 _dbus_string_free (&to_send);
292 else if (_dbus_string_starts_with_c_str (&line,
295 DBusAuthState expected;
297 _dbus_string_delete_first_word (&line);
299 expected = auth_state_from_string (&line);
302 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
306 if (expected != state)
308 _dbus_warn ("expected auth state %s but got %s on line %d\n",
309 auth_state_to_string (expected),
310 auth_state_to_string (state),
315 else if (_dbus_string_starts_with_c_str (&line,
320 _dbus_string_delete_first_word (&line);
322 if (!_dbus_string_init (&received, _DBUS_INT_MAX))
324 _dbus_warn ("no mem to allocate string received\n");
328 if (!_dbus_string_pop_line (&from_auth, &received))
331 _dbus_string_get_const_data (&line, &command);
332 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
334 _dbus_string_free (&received);
338 if (!same_first_word (&received, &line))
341 _dbus_string_get_const_data (&line, &s1);
342 _dbus_string_get_const_data (&received, &s2);
343 _dbus_warn ("expected command '%s' and got '%s' line %d\n",
345 _dbus_string_free (&received);
349 _dbus_string_free (&received);
354 goto next_iteration; /* skip parse_failed */
359 _dbus_string_get_const_data (&line, &s);
360 _dbus_warn ("couldn't process line %d \"%s\"\n",
367 state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
369 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
373 if (_dbus_string_get_length (&from_auth) > 0)
376 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
377 _dbus_string_get_const_data (&from_auth, &s);
378 _dbus_warn ("Leftover data: %s\n", s);
386 _dbus_auth_unref (auth);
388 _dbus_string_free (&file);
389 _dbus_string_free (&file);
390 _dbus_string_free (&from_auth);
396 #endif /* DBUS_BUILD_TESTS */