1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #ifdef DBUS_ENABLE_EMBEDDED_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-credentials.h"
32 #include "dbus-internals.h"
33 #include "dbus-authorization.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 auth_set_unix_credentials(DBusAuth *auth,
218 DBusCredentials *credentials;
220 credentials = _dbus_credentials_new ();
221 if (credentials == NULL)
222 _dbus_assert_not_reached ("no memory");
224 if (uid != DBUS_UID_UNSET)
225 _dbus_credentials_add_unix_uid (credentials, uid);
226 if (pid != DBUS_PID_UNSET)
227 _dbus_credentials_add_pid (credentials, pid);
229 _dbus_auth_set_credentials (auth, credentials);
231 _dbus_credentials_unref (credentials);
235 * Runs an "auth script" which is a script for testing the
236 * authentication protocol. Scripts send and receive data, and then
237 * include assertions about the state of both ends of the connection
238 * after processing the data. A script succeeds if these assertions
241 * @param filename the file containing the script to run
242 * @returns #TRUE if the script succeeds, #FALSE otherwise
245 _dbus_auth_script_run (const DBusString *filename)
248 DBusError error = DBUS_ERROR_INIT;
253 DBusString from_auth;
261 _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
262 _dbus_string_init_const (&context, "org_freedesktop_test");
264 if (!_dbus_string_init (&file))
267 if (!_dbus_string_init (&line))
269 _dbus_string_free (&file);
273 if (!_dbus_string_init (&from_auth))
275 _dbus_string_free (&file);
276 _dbus_string_free (&line);
280 if (!_dbus_file_get_contents (&file, filename, &error)) {
281 _dbus_warn ("Getting contents of %s failed: %s\n",
282 _dbus_string_get_const_data (filename), error.message);
283 dbus_error_free (&error);
287 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
291 while (_dbus_string_pop_line (&file, &line))
295 /* _dbus_warn ("%s\n", _dbus_string_get_const_data (&line)); */
297 _dbus_string_delete_leading_blanks (&line);
301 while ((state = _dbus_auth_do_work (auth)) ==
302 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
304 const DBusString *tmp;
305 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
307 int count = _dbus_string_get_length (tmp);
309 if (_dbus_string_copy (tmp, 0, &from_auth,
310 _dbus_string_get_length (&from_auth)))
311 _dbus_auth_bytes_sent (auth, count);
316 if (_dbus_string_get_length (&line) == 0)
321 else if (_dbus_string_starts_with_c_str (&line,
324 /* Ignore this comment */
328 else if (_dbus_string_starts_with_c_str (&line,
331 /* Ignore this line */
334 else if (_dbus_string_starts_with_c_str (&line,
338 _dbus_warn ("skipping unix only auth script\n");
344 else if (_dbus_string_starts_with_c_str (&line,
347 /* Ignore this line */
350 else if (_dbus_string_starts_with_c_str (&line,
354 _dbus_warn ("skipping windows only auth script\n");
359 else if (_dbus_string_starts_with_c_str (&line,
362 DBusCredentials *creds;
366 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
370 auth = _dbus_auth_client_new ();
373 _dbus_warn ("no memory to create DBusAuth\n");
378 _dbus_auth_ref (auth);
379 _dbus_auth_unref (auth);
381 creds = _dbus_credentials_new_from_current_process ();
384 _dbus_warn ("no memory for credentials\n");
385 _dbus_auth_unref (auth);
390 if (!_dbus_auth_set_credentials (auth, creds))
392 _dbus_warn ("no memory for setting credentials\n");
393 _dbus_auth_unref (auth);
395 _dbus_credentials_unref (creds);
399 _dbus_credentials_unref (creds);
401 else if (_dbus_string_starts_with_c_str (&line,
404 DBusCredentials *creds;
405 DBusAuthorization *authorization;
409 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
413 /* empty authorization, it will use default rules */
414 authorization = _dbus_authorization_new ();
415 if (authorization == NULL)
417 _dbus_warn ("no memory to create DBusAuthorization\n");
420 auth = _dbus_auth_server_new (&guid, authorization);
421 /* DBusAuth owns it, or finalized on OOM */
422 _dbus_authorization_unref (authorization);
425 _dbus_warn ("no memory to create DBusAuth\n");
430 _dbus_auth_ref (auth);
431 _dbus_auth_unref (auth);
433 creds = _dbus_credentials_new_from_current_process ();
436 _dbus_warn ("no memory for credentials\n");
437 _dbus_auth_unref (auth);
442 if (!_dbus_auth_set_credentials (auth, creds))
444 _dbus_warn ("no memory for setting credentials\n");
445 _dbus_auth_unref (auth);
447 _dbus_credentials_unref (creds);
451 _dbus_credentials_unref (creds);
453 _dbus_auth_set_context (auth, &context);
455 else if (auth == NULL)
457 _dbus_warn ("must specify CLIENT or SERVER\n");
461 else if (_dbus_string_starts_with_c_str (&line,
464 auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
466 else if (_dbus_string_starts_with_c_str (&line,
469 auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
471 else if (_dbus_string_starts_with_c_str (&line,
472 "SILLY_CREDENTIALS"))
474 auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
476 else if (_dbus_string_starts_with_c_str (&line,
481 _dbus_string_delete_first_word (&line);
482 mechs = split_string (&line);
483 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
484 dbus_free_string_array (mechs);
486 else if (_dbus_string_starts_with_c_str (&line,
491 _dbus_string_delete_first_word (&line);
493 if (!_dbus_string_init (&to_send))
495 _dbus_warn ("no memory to allocate string\n");
499 if (!append_quoted_string (&to_send, &line))
501 _dbus_warn ("failed to append quoted string line %d\n",
503 _dbus_string_free (&to_send);
507 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
509 if (!_dbus_string_append (&to_send, "\r\n"))
511 _dbus_warn ("failed to append \r\n from line %d\n",
513 _dbus_string_free (&to_send);
517 /* Replace USERID_HEX with our username in hex */
521 if (_dbus_string_find (&to_send, 0,
522 "USERID_HEX", &where))
526 if (!_dbus_string_init (&username))
528 _dbus_warn ("no memory for userid\n");
529 _dbus_string_free (&to_send);
533 if (!_dbus_append_user_from_current_process (&username))
535 _dbus_warn ("no memory for userid\n");
536 _dbus_string_free (&username);
537 _dbus_string_free (&to_send);
541 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
543 if (!_dbus_string_hex_encode (&username, 0,
546 _dbus_warn ("no memory to subst USERID_HEX\n");
547 _dbus_string_free (&username);
548 _dbus_string_free (&to_send);
552 _dbus_string_free (&username);
554 else if (_dbus_string_find (&to_send, 0,
555 "USERNAME_HEX", &where))
559 if (!_dbus_string_init (&username))
561 _dbus_warn ("no memory for username\n");
562 _dbus_string_free (&to_send);
566 if (!_dbus_append_user_from_current_process (&username))
568 _dbus_warn ("no memory for username\n");
569 _dbus_string_free (&username);
570 _dbus_string_free (&to_send);
574 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
576 if (!_dbus_string_hex_encode (&username, 0,
579 _dbus_warn ("no memory to subst USERNAME_HEX\n");
580 _dbus_string_free (&username);
581 _dbus_string_free (&to_send);
585 _dbus_string_free (&username);
592 _dbus_auth_get_buffer (auth, &buffer);
593 if (!_dbus_string_copy (&to_send, 0,
594 buffer, _dbus_string_get_length (buffer)))
596 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
597 _dbus_string_free (&to_send);
598 _dbus_auth_return_buffer (auth, buffer, 0);
602 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
605 _dbus_string_free (&to_send);
607 else if (_dbus_string_starts_with_c_str (&line,
610 DBusAuthState expected;
612 _dbus_string_delete_first_word (&line);
614 expected = auth_state_from_string (&line);
617 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
621 if (expected != state)
623 _dbus_warn ("expected auth state %s but got %s on line %d\n",
624 auth_state_to_string (expected),
625 auth_state_to_string (state),
630 else if (_dbus_string_starts_with_c_str (&line,
635 _dbus_string_delete_first_word (&line);
637 if (!_dbus_string_init (&received))
639 _dbus_warn ("no mem to allocate string received\n");
643 if (!_dbus_string_pop_line (&from_auth, &received))
645 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
646 _dbus_string_get_const_data (&line), line_no);
647 _dbus_string_free (&received);
651 if (!same_first_word (&received, &line))
653 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
655 _dbus_string_get_const_data (&line),
656 _dbus_string_get_const_data (&received));
657 _dbus_string_free (&received);
661 _dbus_string_free (&received);
663 else if (_dbus_string_starts_with_c_str (&line,
667 const DBusString *unused;
669 _dbus_string_delete_first_word (&line);
671 if (!_dbus_string_init (&expected))
673 _dbus_warn ("no mem to allocate string expected\n");
677 if (!append_quoted_string (&expected, &line))
679 _dbus_warn ("failed to append quoted string line %d\n",
681 _dbus_string_free (&expected);
685 _dbus_auth_get_unused_bytes (auth, &unused);
687 if (_dbus_string_equal (&expected, unused))
689 _dbus_auth_delete_unused_bytes (auth);
690 _dbus_string_free (&expected);
694 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
695 _dbus_string_get_const_data (&expected),
696 _dbus_string_get_const_data (unused));
697 _dbus_string_free (&expected);
701 else if (_dbus_string_starts_with_c_str (&line,
702 "EXPECT_HAVE_NO_CREDENTIALS"))
704 DBusCredentials *authorized_identity;
706 authorized_identity = _dbus_auth_get_identity (auth);
707 if (!_dbus_credentials_are_anonymous (authorized_identity))
709 _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized\n");
713 else if (_dbus_string_starts_with_c_str (&line,
714 "EXPECT_HAVE_SOME_CREDENTIALS"))
716 DBusCredentials *authorized_identity;
718 authorized_identity = _dbus_auth_get_identity (auth);
719 if (_dbus_credentials_are_anonymous (authorized_identity))
721 _dbus_warn ("Expected to have some credentials, but we don't\n");
725 else if (_dbus_string_starts_with_c_str (&line,
730 _dbus_string_delete_first_word (&line);
732 if (!_dbus_string_init (&expected))
734 _dbus_warn ("no mem to allocate string expected\n");
738 if (!append_quoted_string (&expected, &line))
740 _dbus_warn ("failed to append quoted string line %d\n",
742 _dbus_string_free (&expected);
746 if (_dbus_string_equal_len (&expected, &from_auth,
747 _dbus_string_get_length (&expected)))
749 _dbus_string_delete (&from_auth, 0,
750 _dbus_string_get_length (&expected));
751 _dbus_string_free (&expected);
755 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
756 _dbus_string_get_const_data (&expected),
757 _dbus_string_get_const_data (&from_auth));
758 _dbus_string_free (&expected);
765 goto next_iteration; /* skip parse_failed */
769 _dbus_warn ("couldn't process line %d \"%s\"\n",
770 line_no, _dbus_string_get_const_data (&line));
777 _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER\n");
780 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
782 const DBusString *unused;
784 _dbus_auth_get_unused_bytes (auth, &unused);
786 if (_dbus_string_get_length (unused) > 0)
788 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
793 if (_dbus_string_get_length (&from_auth) > 0)
795 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
796 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
804 _dbus_auth_unref (auth);
806 _dbus_string_free (&file);
807 _dbus_string_free (&line);
808 _dbus_string_free (&from_auth);
814 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */