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))
89 same_first_word (const DBusString *a,
92 int first_a_blank, first_b_blank;
94 _dbus_string_find_blank (a, 0, &first_a_blank);
95 _dbus_string_find_blank (b, 0, &first_b_blank);
97 if (first_a_blank != first_b_blank)
100 return _dbus_string_equal_len (a, b, first_a_blank);
104 auth_state_from_string (const DBusString *str)
106 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
107 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
108 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
109 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
110 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
111 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
112 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
113 return DBUS_AUTH_STATE_NEED_DISCONNECT;
114 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED_WITH_UNUSED_BYTES"))
115 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
116 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
117 return DBUS_AUTH_STATE_AUTHENTICATED;
123 auth_state_to_string (DBusAuthState state)
127 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
128 return "WAITING_FOR_INPUT";
129 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
130 return "WAITING_FOR_MEMORY";
131 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
132 return "HAVE_BYTES_TO_SEND";
133 case DBUS_AUTH_STATE_NEED_DISCONNECT:
134 return "NEED_DISCONNECT";
135 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
136 return "AUTHENTICATED_WITH_UNUSED_BYTES";
137 case DBUS_AUTH_STATE_AUTHENTICATED:
138 return "AUTHENTICATED";
145 _dbus_auth_script_run (const DBusString *filename)
148 DBusResultCode result;
153 DBusString from_auth;
159 if (!_dbus_string_init (&file, _DBUS_INT_MAX))
162 if (!_dbus_string_init (&line, _DBUS_INT_MAX))
164 _dbus_string_free (&file);
168 if (!_dbus_string_init (&from_auth, _DBUS_INT_MAX))
170 _dbus_string_free (&file);
171 _dbus_string_free (&line);
175 if ((result = _dbus_file_get_contents (&file, filename)) != DBUS_RESULT_SUCCESS)
178 _dbus_string_get_const_data (filename, &s);
179 _dbus_warn ("Getting contents of %s failed: %s\n",
180 s, dbus_result_to_string (result));
185 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
188 while (_dbus_string_pop_line (&file, &line))
192 _dbus_string_delete_leading_blanks (&line);
196 while ((state = _dbus_auth_do_work (auth)) ==
197 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
199 const DBusString *tmp;
200 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
202 int count = _dbus_string_get_length (tmp);
204 if (_dbus_string_copy (tmp, 0, &from_auth,
205 _dbus_string_get_length (&from_auth)))
206 _dbus_auth_bytes_sent (auth, count);
211 if (_dbus_string_get_length (&line) == 0)
216 else if (_dbus_string_starts_with_c_str (&line,
219 /* Ignore this comment */
222 else if (_dbus_string_starts_with_c_str (&line,
227 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
231 auth = _dbus_auth_client_new ();
234 _dbus_warn ("no memory to create DBusAuth\n");
238 else if (_dbus_string_starts_with_c_str (&line,
243 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
247 auth = _dbus_auth_server_new ();
250 _dbus_warn ("no memory to create DBusAuth\n");
254 else if (auth == NULL)
256 _dbus_warn ("must specify CLIENT or SERVER\n");
260 else if (_dbus_string_starts_with_c_str (&line,
265 _dbus_string_delete_first_word (&line);
267 if (!_dbus_string_init (&to_send, _DBUS_INT_MAX))
269 _dbus_warn ("no memory to allocate string\n");
273 if (!append_quoted_string (&to_send, &line))
275 _dbus_warn ("failed to append quoted string line %d\n",
277 _dbus_string_free (&to_send);
283 _dbus_string_get_const_data (&to_send, &s4);
284 _dbus_verbose ("Sending '%s'\n", s4);
287 if (!_dbus_string_append (&to_send, "\r\n"))
289 _dbus_warn ("failed to append \r\n from line %d\n",
291 _dbus_string_free (&to_send);
295 if (!_dbus_auth_bytes_received (auth, &to_send))
297 _dbus_warn ("not enough memory to call bytes_received\n");
298 _dbus_string_free (&to_send);
302 _dbus_string_free (&to_send);
304 else if (_dbus_string_starts_with_c_str (&line,
307 DBusAuthState expected;
309 _dbus_string_delete_first_word (&line);
311 expected = auth_state_from_string (&line);
314 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
318 if (expected != state)
320 _dbus_warn ("expected auth state %s but got %s on line %d\n",
321 auth_state_to_string (expected),
322 auth_state_to_string (state),
327 else if (_dbus_string_starts_with_c_str (&line,
332 _dbus_string_delete_first_word (&line);
334 if (!_dbus_string_init (&received, _DBUS_INT_MAX))
336 _dbus_warn ("no mem to allocate string received\n");
340 if (!_dbus_string_pop_line (&from_auth, &received))
343 _dbus_string_get_const_data (&line, &command);
344 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
346 _dbus_string_free (&received);
350 if (!same_first_word (&received, &line))
353 _dbus_string_get_const_data (&line, &s1);
354 _dbus_string_get_const_data (&received, &s2);
355 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
357 _dbus_string_free (&received);
361 _dbus_string_free (&received);
366 goto next_iteration; /* skip parse_failed */
371 _dbus_string_get_const_data (&line, &s);
372 _dbus_warn ("couldn't process line %d \"%s\"\n",
379 state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
381 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
385 if (_dbus_string_get_length (&from_auth) > 0)
388 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
389 _dbus_string_get_const_data (&from_auth, &s);
390 _dbus_warn ("Leftover data: %s\n", s);
398 _dbus_auth_unref (auth);
400 _dbus_string_free (&file);
401 _dbus_string_free (&line);
402 _dbus_string_free (&from_auth);
408 #endif /* DBUS_BUILD_TESTS */