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-marshal.h"
33 #include "dbus-userdb.h"
36 * @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
37 * @ingroup DBusInternals
38 * @brief DBusAuth unit test scripting
40 * The code in here is used for unit testing, it loads
41 * up a script that tests DBusAuth.
46 /* this is slightly different from the other append_quoted_string
47 * in dbus-message-builder.c
50 append_quoted_string (DBusString *dest,
51 const DBusString *quoted)
53 dbus_bool_t in_quotes = FALSE;
54 dbus_bool_t in_backslash = FALSE;
58 while (i < _dbus_string_get_length (quoted))
62 b = _dbus_string_get_byte (quoted, i);
76 _dbus_warn ("bad backslashed byte %c\n", b);
80 if (!_dbus_string_append_byte (dest, a))
95 if (!_dbus_string_append_byte (dest, b))
103 else if (b == ' ' || b == '\n' || b == '\t')
104 break; /* end on whitespace if not quoted */
107 if (!_dbus_string_append_byte (dest, b))
119 same_first_word (const DBusString *a,
122 int first_a_blank, first_b_blank;
124 _dbus_string_find_blank (a, 0, &first_a_blank);
125 _dbus_string_find_blank (b, 0, &first_b_blank);
127 if (first_a_blank != first_b_blank)
130 return _dbus_string_equal_len (a, b, first_a_blank);
134 auth_state_from_string (const DBusString *str)
136 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
137 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
138 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
139 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
140 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
141 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
142 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
143 return DBUS_AUTH_STATE_NEED_DISCONNECT;
144 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
145 return DBUS_AUTH_STATE_AUTHENTICATED;
151 auth_state_to_string (DBusAuthState state)
155 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
156 return "WAITING_FOR_INPUT";
157 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
158 return "WAITING_FOR_MEMORY";
159 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
160 return "HAVE_BYTES_TO_SEND";
161 case DBUS_AUTH_STATE_NEED_DISCONNECT:
162 return "NEED_DISCONNECT";
163 case DBUS_AUTH_STATE_AUTHENTICATED:
164 return "AUTHENTICATED";
171 split_string (DBusString *str)
173 int i, j, k, count, end;
176 end = _dbus_string_get_length (str);
179 _dbus_string_skip_blank (str, i, &i);
180 for (count = 0; i < end; count++)
182 _dbus_string_find_blank (str, i, &i);
183 _dbus_string_skip_blank (str, i, &i);
186 array = dbus_new0 (char *, count + 1);
191 _dbus_string_skip_blank (str, i, &i);
192 for (k = 0; k < count; k++)
194 _dbus_string_find_blank (str, i, &j);
196 array[k] = dbus_malloc (j - i + 1);
197 if (array[k] == NULL)
199 dbus_free_string_array (array);
203 _dbus_string_get_const_data_len (str, i, j - i), j - i);
204 array[k][j - i] = '\0';
206 _dbus_string_skip_blank (str, j, &i);
214 * Runs an "auth script" which is a script for testing the
215 * authentication protocol. Scripts send and receive data, and then
216 * include assertions about the state of both ends of the connection
217 * after processing the data. A script succeeds if these assertions
220 * @param filename the file containing the script to run
221 * @returns #TRUE if the script succeeds, #FALSE otherwise
224 _dbus_auth_script_run (const DBusString *filename)
232 DBusString from_auth;
239 _dbus_string_init_const (&context, "org_freedesktop_test");
241 if (!_dbus_string_init (&file))
244 if (!_dbus_string_init (&line))
246 _dbus_string_free (&file);
250 if (!_dbus_string_init (&from_auth))
252 _dbus_string_free (&file);
253 _dbus_string_free (&line);
257 dbus_error_init (&error);
258 if (!_dbus_file_get_contents (&file, filename, &error)) {
259 _dbus_warn ("Getting contents of %s failed: %s\n",
260 _dbus_string_get_const_data (filename), error.message);
261 dbus_error_free (&error);
265 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
268 while (_dbus_string_pop_line (&file, &line))
272 _dbus_string_delete_leading_blanks (&line);
276 while ((state = _dbus_auth_do_work (auth)) ==
277 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
279 const DBusString *tmp;
280 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
282 int count = _dbus_string_get_length (tmp);
284 if (_dbus_string_copy (tmp, 0, &from_auth,
285 _dbus_string_get_length (&from_auth)))
286 _dbus_auth_bytes_sent (auth, count);
291 if (_dbus_string_get_length (&line) == 0)
296 else if (_dbus_string_starts_with_c_str (&line,
299 /* Ignore this comment */
302 else if (_dbus_string_starts_with_c_str (&line,
305 DBusCredentials creds;
309 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
313 auth = _dbus_auth_client_new ();
316 _dbus_warn ("no memory to create DBusAuth\n");
321 _dbus_auth_ref (auth);
322 _dbus_auth_unref (auth);
324 _dbus_credentials_from_current_process (&creds);
325 _dbus_auth_set_credentials (auth, &creds);
327 else if (_dbus_string_starts_with_c_str (&line,
330 DBusCredentials creds;
334 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
338 auth = _dbus_auth_server_new ();
341 _dbus_warn ("no memory to create DBusAuth\n");
346 _dbus_auth_ref (auth);
347 _dbus_auth_unref (auth);
349 _dbus_credentials_from_current_process (&creds);
350 _dbus_auth_set_credentials (auth, &creds);
351 _dbus_auth_set_context (auth, &context);
353 else if (auth == NULL)
355 _dbus_warn ("must specify CLIENT or SERVER\n");
359 else if (_dbus_string_starts_with_c_str (&line,
362 DBusCredentials creds = { -1, -1, -1 };
363 _dbus_auth_set_credentials (auth, &creds);
365 else if (_dbus_string_starts_with_c_str (&line,
368 DBusCredentials creds = { -1, 0, 0 };
369 _dbus_auth_set_credentials (auth, &creds);
371 else if (_dbus_string_starts_with_c_str (&line,
372 "SILLY_CREDENTIALS"))
374 DBusCredentials creds = { -1, 4312, 1232 };
375 _dbus_auth_set_credentials (auth, &creds);
377 else if (_dbus_string_starts_with_c_str (&line,
382 _dbus_string_delete_first_word (&line);
383 mechs = split_string (&line);
384 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
385 dbus_free_string_array (mechs);
387 else if (_dbus_string_starts_with_c_str (&line,
392 _dbus_string_delete_first_word (&line);
394 if (!_dbus_string_init (&to_send))
396 _dbus_warn ("no memory to allocate string\n");
400 if (!append_quoted_string (&to_send, &line))
402 _dbus_warn ("failed to append quoted string line %d\n",
404 _dbus_string_free (&to_send);
408 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
410 if (!_dbus_string_append (&to_send, "\r\n"))
412 _dbus_warn ("failed to append \r\n from line %d\n",
414 _dbus_string_free (&to_send);
418 /* Replace USERID_HEX with our username in hex */
422 if (_dbus_string_find (&to_send, 0,
423 "USERID_HEX", &where))
427 if (!_dbus_string_init (&username))
429 _dbus_warn ("no memory for userid\n");
430 _dbus_string_free (&to_send);
434 if (!_dbus_string_append_uint (&username,
437 _dbus_warn ("no memory for userid\n");
438 _dbus_string_free (&username);
439 _dbus_string_free (&to_send);
443 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
445 if (!_dbus_string_hex_encode (&username, 0,
448 _dbus_warn ("no memory to subst USERID_HEX\n");
449 _dbus_string_free (&username);
450 _dbus_string_free (&to_send);
454 _dbus_string_free (&username);
456 else if (_dbus_string_find (&to_send, 0,
457 "USERNAME_HEX", &where))
462 if (!_dbus_string_init (&username))
464 _dbus_warn ("no memory for username\n");
465 _dbus_string_free (&to_send);
469 if (!_dbus_username_from_current_process (&u) ||
470 !_dbus_string_copy (u, 0, &username,
471 _dbus_string_get_length (&username)))
473 _dbus_warn ("no memory for username\n");
474 _dbus_string_free (&username);
475 _dbus_string_free (&to_send);
479 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
481 if (!_dbus_string_hex_encode (&username, 0,
484 _dbus_warn ("no memory to subst USERNAME_HEX\n");
485 _dbus_string_free (&username);
486 _dbus_string_free (&to_send);
490 _dbus_string_free (&username);
497 _dbus_auth_get_buffer (auth, &buffer);
498 if (!_dbus_string_copy (&to_send, 0,
499 buffer, _dbus_string_get_length (buffer)))
501 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
502 _dbus_string_free (&to_send);
503 _dbus_auth_return_buffer (auth, buffer, 0);
507 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
510 _dbus_string_free (&to_send);
512 else if (_dbus_string_starts_with_c_str (&line,
515 DBusAuthState expected;
517 _dbus_string_delete_first_word (&line);
519 expected = auth_state_from_string (&line);
522 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
526 if (expected != state)
528 _dbus_warn ("expected auth state %s but got %s on line %d\n",
529 auth_state_to_string (expected),
530 auth_state_to_string (state),
535 else if (_dbus_string_starts_with_c_str (&line,
540 _dbus_string_delete_first_word (&line);
542 if (!_dbus_string_init (&received))
544 _dbus_warn ("no mem to allocate string received\n");
548 if (!_dbus_string_pop_line (&from_auth, &received))
550 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
551 _dbus_string_get_const_data (&line), line_no);
552 _dbus_string_free (&received);
556 if (!same_first_word (&received, &line))
558 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
560 _dbus_string_get_const_data (&line),
561 _dbus_string_get_const_data (&received));
562 _dbus_string_free (&received);
566 _dbus_string_free (&received);
568 else if (_dbus_string_starts_with_c_str (&line,
572 const DBusString *unused;
574 _dbus_string_delete_first_word (&line);
576 if (!_dbus_string_init (&expected))
578 _dbus_warn ("no mem to allocate string expected\n");
582 if (!append_quoted_string (&expected, &line))
584 _dbus_warn ("failed to append quoted string line %d\n",
586 _dbus_string_free (&expected);
590 _dbus_auth_get_unused_bytes (auth, &unused);
592 if (_dbus_string_equal (&expected, unused))
594 _dbus_auth_delete_unused_bytes (auth);
595 _dbus_string_free (&expected);
599 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
600 _dbus_string_get_const_data (&expected),
601 _dbus_string_get_const_data (unused));
602 _dbus_string_free (&expected);
606 else if (_dbus_string_starts_with_c_str (&line,
611 _dbus_string_delete_first_word (&line);
613 if (!_dbus_string_init (&expected))
615 _dbus_warn ("no mem to allocate string expected\n");
619 if (!append_quoted_string (&expected, &line))
621 _dbus_warn ("failed to append quoted string line %d\n",
623 _dbus_string_free (&expected);
627 if (_dbus_string_equal_len (&expected, &from_auth,
628 _dbus_string_get_length (&expected)))
630 _dbus_string_delete (&from_auth, 0,
631 _dbus_string_get_length (&expected));
632 _dbus_string_free (&expected);
636 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
637 _dbus_string_get_const_data (&expected),
638 _dbus_string_get_const_data (&from_auth));
639 _dbus_string_free (&expected);
646 goto next_iteration; /* skip parse_failed */
650 _dbus_warn ("couldn't process line %d \"%s\"\n",
651 line_no, _dbus_string_get_const_data (&line));
657 state == DBUS_AUTH_STATE_AUTHENTICATED)
659 const DBusString *unused;
661 _dbus_auth_get_unused_bytes (auth, &unused);
663 if (_dbus_string_get_length (unused) > 0)
665 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
670 if (_dbus_string_get_length (&from_auth) > 0)
672 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
673 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
681 _dbus_auth_unref (auth);
683 _dbus_string_free (&file);
684 _dbus_string_free (&line);
685 _dbus_string_free (&from_auth);
691 #endif /* DBUS_BUILD_TESTS */