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 2.1
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-userdb.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.
45 /* this is slightly different from the other append_quoted_string
46 * in dbus-message-builder.c
49 append_quoted_string (DBusString *dest,
50 const DBusString *quoted)
52 dbus_bool_t in_quotes = FALSE;
53 dbus_bool_t in_backslash = FALSE;
57 while (i < _dbus_string_get_length (quoted))
61 b = _dbus_string_get_byte (quoted, i);
75 _dbus_warn ("bad backslashed byte %c\n", b);
79 if (!_dbus_string_append_byte (dest, a))
94 if (!_dbus_string_append_byte (dest, b))
102 else if (b == ' ' || b == '\n' || b == '\t')
103 break; /* end on whitespace if not quoted */
106 if (!_dbus_string_append_byte (dest, b))
118 same_first_word (const DBusString *a,
121 int first_a_blank, first_b_blank;
123 _dbus_string_find_blank (a, 0, &first_a_blank);
124 _dbus_string_find_blank (b, 0, &first_b_blank);
126 if (first_a_blank != first_b_blank)
129 return _dbus_string_equal_len (a, b, first_a_blank);
133 auth_state_from_string (const DBusString *str)
135 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
136 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
137 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
138 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
139 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
140 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
141 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
142 return DBUS_AUTH_STATE_NEED_DISCONNECT;
143 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
144 return DBUS_AUTH_STATE_AUTHENTICATED;
150 auth_state_to_string (DBusAuthState state)
154 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
155 return "WAITING_FOR_INPUT";
156 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
157 return "WAITING_FOR_MEMORY";
158 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
159 return "HAVE_BYTES_TO_SEND";
160 case DBUS_AUTH_STATE_NEED_DISCONNECT:
161 return "NEED_DISCONNECT";
162 case DBUS_AUTH_STATE_AUTHENTICATED:
163 return "AUTHENTICATED";
170 split_string (DBusString *str)
172 int i, j, k, count, end;
175 end = _dbus_string_get_length (str);
178 _dbus_string_skip_blank (str, i, &i);
179 for (count = 0; i < end; count++)
181 _dbus_string_find_blank (str, i, &i);
182 _dbus_string_skip_blank (str, i, &i);
185 array = dbus_new0 (char *, count + 1);
190 _dbus_string_skip_blank (str, i, &i);
191 for (k = 0; k < count; k++)
193 _dbus_string_find_blank (str, i, &j);
195 array[k] = dbus_malloc (j - i + 1);
196 if (array[k] == NULL)
198 dbus_free_string_array (array);
202 _dbus_string_get_const_data_len (str, i, j - i), j - i);
203 array[k][j - i] = '\0';
205 _dbus_string_skip_blank (str, j, &i);
213 * Runs an "auth script" which is a script for testing the
214 * authentication protocol. Scripts send and receive data, and then
215 * include assertions about the state of both ends of the connection
216 * after processing the data. A script succeeds if these assertions
219 * @param filename the file containing the script to run
220 * @returns #TRUE if the script succeeds, #FALSE otherwise
223 _dbus_auth_script_run (const DBusString *filename)
231 DBusString from_auth;
239 _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
240 _dbus_string_init_const (&context, "org_freedesktop_test");
242 if (!_dbus_string_init (&file))
245 if (!_dbus_string_init (&line))
247 _dbus_string_free (&file);
251 if (!_dbus_string_init (&from_auth))
253 _dbus_string_free (&file);
254 _dbus_string_free (&line);
258 dbus_error_init (&error);
259 if (!_dbus_file_get_contents (&file, filename, &error)) {
260 _dbus_warn ("Getting contents of %s failed: %s\n",
261 _dbus_string_get_const_data (filename), error.message);
262 dbus_error_free (&error);
266 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
269 while (_dbus_string_pop_line (&file, &line))
273 _dbus_string_delete_leading_blanks (&line);
277 while ((state = _dbus_auth_do_work (auth)) ==
278 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
280 const DBusString *tmp;
281 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
283 int count = _dbus_string_get_length (tmp);
285 if (_dbus_string_copy (tmp, 0, &from_auth,
286 _dbus_string_get_length (&from_auth)))
287 _dbus_auth_bytes_sent (auth, count);
292 if (_dbus_string_get_length (&line) == 0)
297 else if (_dbus_string_starts_with_c_str (&line,
300 /* Ignore this comment */
303 else if (_dbus_string_starts_with_c_str (&line,
306 DBusCredentials creds;
310 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
314 auth = _dbus_auth_client_new ();
317 _dbus_warn ("no memory to create DBusAuth\n");
322 _dbus_auth_ref (auth);
323 _dbus_auth_unref (auth);
325 _dbus_credentials_from_current_process (&creds);
326 _dbus_auth_set_credentials (auth, &creds);
328 else if (_dbus_string_starts_with_c_str (&line,
331 DBusCredentials creds;
335 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
339 auth = _dbus_auth_server_new (&guid);
342 _dbus_warn ("no memory to create DBusAuth\n");
347 _dbus_auth_ref (auth);
348 _dbus_auth_unref (auth);
350 _dbus_credentials_from_current_process (&creds);
351 _dbus_auth_set_credentials (auth, &creds);
352 _dbus_auth_set_context (auth, &context);
354 else if (auth == NULL)
356 _dbus_warn ("must specify CLIENT or SERVER\n");
360 else if (_dbus_string_starts_with_c_str (&line,
363 DBusCredentials creds = { -1, -1, -1 };
364 _dbus_auth_set_credentials (auth, &creds);
366 else if (_dbus_string_starts_with_c_str (&line,
369 DBusCredentials creds = { -1, 0, 0 };
370 _dbus_auth_set_credentials (auth, &creds);
372 else if (_dbus_string_starts_with_c_str (&line,
373 "SILLY_CREDENTIALS"))
375 DBusCredentials creds = { -1, 4312, 1232 };
376 _dbus_auth_set_credentials (auth, &creds);
378 else if (_dbus_string_starts_with_c_str (&line,
383 _dbus_string_delete_first_word (&line);
384 mechs = split_string (&line);
385 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
386 dbus_free_string_array (mechs);
388 else if (_dbus_string_starts_with_c_str (&line,
393 _dbus_string_delete_first_word (&line);
395 if (!_dbus_string_init (&to_send))
397 _dbus_warn ("no memory to allocate string\n");
401 if (!append_quoted_string (&to_send, &line))
403 _dbus_warn ("failed to append quoted string line %d\n",
405 _dbus_string_free (&to_send);
409 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
411 if (!_dbus_string_append (&to_send, "\r\n"))
413 _dbus_warn ("failed to append \r\n from line %d\n",
415 _dbus_string_free (&to_send);
419 /* Replace USERID_HEX with our username in hex */
423 if (_dbus_string_find (&to_send, 0,
424 "USERID_HEX", &where))
428 if (!_dbus_string_init (&username))
430 _dbus_warn ("no memory for userid\n");
431 _dbus_string_free (&to_send);
435 if (!_dbus_string_append_uint (&username,
438 _dbus_warn ("no memory for userid\n");
439 _dbus_string_free (&username);
440 _dbus_string_free (&to_send);
444 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
446 if (!_dbus_string_hex_encode (&username, 0,
449 _dbus_warn ("no memory to subst USERID_HEX\n");
450 _dbus_string_free (&username);
451 _dbus_string_free (&to_send);
455 _dbus_string_free (&username);
457 else if (_dbus_string_find (&to_send, 0,
458 "USERNAME_HEX", &where))
463 if (!_dbus_string_init (&username))
465 _dbus_warn ("no memory for username\n");
466 _dbus_string_free (&to_send);
470 if (!_dbus_username_from_current_process (&u) ||
471 !_dbus_string_copy (u, 0, &username,
472 _dbus_string_get_length (&username)))
474 _dbus_warn ("no memory for username\n");
475 _dbus_string_free (&username);
476 _dbus_string_free (&to_send);
480 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
482 if (!_dbus_string_hex_encode (&username, 0,
485 _dbus_warn ("no memory to subst USERNAME_HEX\n");
486 _dbus_string_free (&username);
487 _dbus_string_free (&to_send);
491 _dbus_string_free (&username);
498 _dbus_auth_get_buffer (auth, &buffer);
499 if (!_dbus_string_copy (&to_send, 0,
500 buffer, _dbus_string_get_length (buffer)))
502 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
503 _dbus_string_free (&to_send);
504 _dbus_auth_return_buffer (auth, buffer, 0);
508 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
511 _dbus_string_free (&to_send);
513 else if (_dbus_string_starts_with_c_str (&line,
516 DBusAuthState expected;
518 _dbus_string_delete_first_word (&line);
520 expected = auth_state_from_string (&line);
523 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
527 if (expected != state)
529 _dbus_warn ("expected auth state %s but got %s on line %d\n",
530 auth_state_to_string (expected),
531 auth_state_to_string (state),
536 else if (_dbus_string_starts_with_c_str (&line,
541 _dbus_string_delete_first_word (&line);
543 if (!_dbus_string_init (&received))
545 _dbus_warn ("no mem to allocate string received\n");
549 if (!_dbus_string_pop_line (&from_auth, &received))
551 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
552 _dbus_string_get_const_data (&line), line_no);
553 _dbus_string_free (&received);
557 if (!same_first_word (&received, &line))
559 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
561 _dbus_string_get_const_data (&line),
562 _dbus_string_get_const_data (&received));
563 _dbus_string_free (&received);
567 _dbus_string_free (&received);
569 else if (_dbus_string_starts_with_c_str (&line,
573 const DBusString *unused;
575 _dbus_string_delete_first_word (&line);
577 if (!_dbus_string_init (&expected))
579 _dbus_warn ("no mem to allocate string expected\n");
583 if (!append_quoted_string (&expected, &line))
585 _dbus_warn ("failed to append quoted string line %d\n",
587 _dbus_string_free (&expected);
591 _dbus_auth_get_unused_bytes (auth, &unused);
593 if (_dbus_string_equal (&expected, unused))
595 _dbus_auth_delete_unused_bytes (auth);
596 _dbus_string_free (&expected);
600 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
601 _dbus_string_get_const_data (&expected),
602 _dbus_string_get_const_data (unused));
603 _dbus_string_free (&expected);
607 else if (_dbus_string_starts_with_c_str (&line,
612 _dbus_string_delete_first_word (&line);
614 if (!_dbus_string_init (&expected))
616 _dbus_warn ("no mem to allocate string expected\n");
620 if (!append_quoted_string (&expected, &line))
622 _dbus_warn ("failed to append quoted string line %d\n",
624 _dbus_string_free (&expected);
628 if (_dbus_string_equal_len (&expected, &from_auth,
629 _dbus_string_get_length (&expected)))
631 _dbus_string_delete (&from_auth, 0,
632 _dbus_string_get_length (&expected));
633 _dbus_string_free (&expected);
637 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
638 _dbus_string_get_const_data (&expected),
639 _dbus_string_get_const_data (&from_auth));
640 _dbus_string_free (&expected);
647 goto next_iteration; /* skip parse_failed */
651 _dbus_warn ("couldn't process line %d \"%s\"\n",
652 line_no, _dbus_string_get_const_data (&line));
658 state == DBUS_AUTH_STATE_AUTHENTICATED)
660 const DBusString *unused;
662 _dbus_auth_get_unused_bytes (auth, &unused);
664 if (_dbus_string_get_length (unused) > 0)
666 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
671 if (_dbus_string_get_length (&from_auth) > 0)
673 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
674 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
682 _dbus_auth_unref (auth);
684 _dbus_string_free (&file);
685 _dbus_string_free (&line);
686 _dbus_string_free (&from_auth);
692 #endif /* DBUS_BUILD_TESTS */