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.
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_WITH_UNUSED_BYTES"))
144 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
145 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
146 return DBUS_AUTH_STATE_AUTHENTICATED;
152 auth_state_to_string (DBusAuthState state)
156 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
157 return "WAITING_FOR_INPUT";
158 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
159 return "WAITING_FOR_MEMORY";
160 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
161 return "HAVE_BYTES_TO_SEND";
162 case DBUS_AUTH_STATE_NEED_DISCONNECT:
163 return "NEED_DISCONNECT";
164 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
165 return "AUTHENTICATED_WITH_UNUSED_BYTES";
166 case DBUS_AUTH_STATE_AUTHENTICATED:
167 return "AUTHENTICATED";
174 * Runs an "auth script" which is a script for testing the
175 * authentication protocol. Scripts send and receive data, and then
176 * include assertions about the state of both ends of the connection
177 * after processing the data. A script succeeds if these assertions
180 * @param filename the file containing the script to run
181 * @returns #TRUE if the script succeeds, #FALSE otherwise
184 _dbus_auth_script_run (const DBusString *filename)
192 DBusString from_auth;
199 _dbus_string_init_const (&context, "org_freedesktop_test");
201 if (!_dbus_string_init (&file, _DBUS_INT_MAX))
204 if (!_dbus_string_init (&line, _DBUS_INT_MAX))
206 _dbus_string_free (&file);
210 if (!_dbus_string_init (&from_auth, _DBUS_INT_MAX))
212 _dbus_string_free (&file);
213 _dbus_string_free (&line);
217 dbus_error_init (&error);
218 if (!_dbus_file_get_contents (&file, filename, &error)) {
220 _dbus_string_get_const_data (filename, &s);
221 _dbus_warn ("Getting contents of %s failed: %s\n",
223 dbus_error_free (&error);
227 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
230 while (_dbus_string_pop_line (&file, &line))
234 _dbus_string_delete_leading_blanks (&line);
238 while ((state = _dbus_auth_do_work (auth)) ==
239 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
241 const DBusString *tmp;
242 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
244 int count = _dbus_string_get_length (tmp);
246 if (_dbus_string_copy (tmp, 0, &from_auth,
247 _dbus_string_get_length (&from_auth)))
248 _dbus_auth_bytes_sent (auth, count);
253 if (_dbus_string_get_length (&line) == 0)
258 else if (_dbus_string_starts_with_c_str (&line,
261 /* Ignore this comment */
264 else if (_dbus_string_starts_with_c_str (&line,
267 DBusCredentials creds;
271 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
275 auth = _dbus_auth_client_new ();
278 _dbus_warn ("no memory to create DBusAuth\n");
282 _dbus_credentials_from_current_process (&creds);
283 _dbus_auth_set_credentials (auth, &creds);
285 else if (_dbus_string_starts_with_c_str (&line,
288 DBusCredentials creds;
292 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
296 auth = _dbus_auth_server_new ();
299 _dbus_warn ("no memory to create DBusAuth\n");
303 _dbus_credentials_from_current_process (&creds);
304 _dbus_auth_set_credentials (auth, &creds);
305 _dbus_auth_set_context (auth, &context);
307 else if (auth == NULL)
309 _dbus_warn ("must specify CLIENT or SERVER\n");
313 else if (_dbus_string_starts_with_c_str (&line,
316 DBusCredentials creds = { -1, -1, -1 };
317 _dbus_auth_set_credentials (auth, &creds);
319 else if (_dbus_string_starts_with_c_str (&line,
322 DBusCredentials creds = { -1, 0, 0 };
323 _dbus_auth_set_credentials (auth, &creds);
325 else if (_dbus_string_starts_with_c_str (&line,
326 "SILLY_CREDENTIALS"))
328 DBusCredentials creds = { -1, 4312, 1232 };
329 _dbus_auth_set_credentials (auth, &creds);
331 else if (_dbus_string_starts_with_c_str (&line,
336 _dbus_string_delete_first_word (&line);
338 if (!_dbus_string_init (&to_send, _DBUS_INT_MAX))
340 _dbus_warn ("no memory to allocate string\n");
344 if (!append_quoted_string (&to_send, &line))
346 _dbus_warn ("failed to append quoted string line %d\n",
348 _dbus_string_free (&to_send);
354 _dbus_string_get_const_data (&to_send, &s4);
355 _dbus_verbose ("Sending '%s'\n", s4);
358 if (!_dbus_string_append (&to_send, "\r\n"))
360 _dbus_warn ("failed to append \r\n from line %d\n",
362 _dbus_string_free (&to_send);
366 /* Replace USERID_BASE64 with our username in base64 */
370 if (_dbus_string_find (&to_send, 0,
371 "USERID_BASE64", &where))
375 if (!_dbus_string_init (&username, _DBUS_INT_MAX))
377 _dbus_warn ("no memory for userid\n");
378 _dbus_string_free (&to_send);
382 if (!_dbus_string_append_our_uid (&username))
384 _dbus_warn ("no memory for userid\n");
385 _dbus_string_free (&username);
386 _dbus_string_free (&to_send);
390 _dbus_string_delete (&to_send, where, strlen ("USERID_BASE64"));
392 if (!_dbus_string_base64_encode (&username, 0,
395 _dbus_warn ("no memory to subst USERID_BASE64\n");
396 _dbus_string_free (&username);
397 _dbus_string_free (&to_send);
401 _dbus_string_free (&username);
403 else if (_dbus_string_find (&to_send, 0,
404 "USERNAME_BASE64", &where))
409 if (!_dbus_string_init (&username, _DBUS_INT_MAX))
411 _dbus_warn ("no memory for username\n");
412 _dbus_string_free (&to_send);
416 if (!_dbus_user_info_from_current_process (&u, NULL, NULL) ||
417 !_dbus_string_copy (u, 0, &username,
418 _dbus_string_get_length (&username)))
420 _dbus_warn ("no memory for username\n");
421 _dbus_string_free (&username);
422 _dbus_string_free (&to_send);
426 _dbus_string_delete (&to_send, where, strlen ("USERNAME_BASE64"));
428 if (!_dbus_string_base64_encode (&username, 0,
431 _dbus_warn ("no memory to subst USERNAME_BASE64\n");
432 _dbus_string_free (&username);
433 _dbus_string_free (&to_send);
437 _dbus_string_free (&username);
444 _dbus_auth_get_buffer (auth, &buffer);
445 if (!_dbus_string_copy (&to_send, 0,
446 buffer, _dbus_string_get_length (buffer)))
448 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
449 _dbus_string_free (&to_send);
450 _dbus_auth_return_buffer (auth, buffer, 0);
454 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
457 _dbus_string_free (&to_send);
459 else if (_dbus_string_starts_with_c_str (&line,
462 DBusAuthState expected;
464 _dbus_string_delete_first_word (&line);
466 expected = auth_state_from_string (&line);
469 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
473 if (expected != state)
475 _dbus_warn ("expected auth state %s but got %s on line %d\n",
476 auth_state_to_string (expected),
477 auth_state_to_string (state),
482 else if (_dbus_string_starts_with_c_str (&line,
487 _dbus_string_delete_first_word (&line);
489 if (!_dbus_string_init (&received, _DBUS_INT_MAX))
491 _dbus_warn ("no mem to allocate string received\n");
495 if (!_dbus_string_pop_line (&from_auth, &received))
498 _dbus_string_get_const_data (&line, &command);
499 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
501 _dbus_string_free (&received);
505 if (!same_first_word (&received, &line))
508 _dbus_string_get_const_data (&line, &s1);
509 _dbus_string_get_const_data (&received, &s2);
510 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
512 _dbus_string_free (&received);
516 _dbus_string_free (&received);
518 else if (_dbus_string_starts_with_c_str (&line,
522 const DBusString *unused;
524 _dbus_string_delete_first_word (&line);
526 if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
528 _dbus_warn ("no mem to allocate string expected\n");
532 if (!append_quoted_string (&expected, &line))
534 _dbus_warn ("failed to append quoted string line %d\n",
536 _dbus_string_free (&expected);
540 _dbus_auth_get_unused_bytes (auth, &unused);
542 if (_dbus_string_equal (&expected, unused))
544 _dbus_auth_delete_unused_bytes (auth);
545 _dbus_string_free (&expected);
550 _dbus_string_get_const_data (&expected, &e1);
551 _dbus_string_get_const_data (unused, &h1);
552 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
554 _dbus_string_free (&expected);
558 else if (_dbus_string_starts_with_c_str (&line,
563 _dbus_string_delete_first_word (&line);
565 if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
567 _dbus_warn ("no mem to allocate string expected\n");
571 if (!append_quoted_string (&expected, &line))
573 _dbus_warn ("failed to append quoted string line %d\n",
575 _dbus_string_free (&expected);
579 if (_dbus_string_equal_len (&expected, &from_auth,
580 _dbus_string_get_length (&expected)))
582 _dbus_string_delete (&from_auth, 0,
583 _dbus_string_get_length (&expected));
584 _dbus_string_free (&expected);
589 _dbus_string_get_const_data (&expected, &e1);
590 _dbus_string_get_const_data (&from_auth, &h1);
591 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
593 _dbus_string_free (&expected);
600 goto next_iteration; /* skip parse_failed */
605 _dbus_string_get_const_data (&line, &s);
606 _dbus_warn ("couldn't process line %d \"%s\"\n",
613 state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
615 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
619 if (_dbus_string_get_length (&from_auth) > 0)
622 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
623 _dbus_string_get_const_data (&from_auth, &s);
624 _dbus_warn ("Leftover data: %s\n", s);
632 _dbus_auth_unref (auth);
634 _dbus_string_free (&file);
635 _dbus_string_free (&line);
636 _dbus_string_free (&from_auth);
642 #endif /* DBUS_BUILD_TESTS */