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"
31 #include "dbus-auth.h"
32 #include "dbus-string.h"
33 #include "dbus-hash.h"
34 #include "dbus-credentials.h"
35 #include "dbus-internals.h"
38 * @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
39 * @ingroup DBusInternals
40 * @brief DBusAuth unit test scripting
42 * The code in here is used for unit testing, it loads
43 * up a script that tests DBusAuth.
48 /* this is slightly different from the other append_quoted_string
49 * in dbus-message-builder.c
52 append_quoted_string (DBusString *dest,
53 const DBusString *quoted)
55 dbus_bool_t in_quotes = FALSE;
56 dbus_bool_t in_backslash = FALSE;
60 while (i < _dbus_string_get_length (quoted))
64 b = _dbus_string_get_byte (quoted, i);
78 _dbus_warn ("bad backslashed byte %c\n", b);
82 if (!_dbus_string_append_byte (dest, a))
97 if (!_dbus_string_append_byte (dest, b))
105 else if (b == ' ' || b == '\n' || b == '\t')
106 break; /* end on whitespace if not quoted */
109 if (!_dbus_string_append_byte (dest, b))
121 same_first_word (const DBusString *a,
124 int first_a_blank, first_b_blank;
126 _dbus_string_find_blank (a, 0, &first_a_blank);
127 _dbus_string_find_blank (b, 0, &first_b_blank);
129 if (first_a_blank != first_b_blank)
132 return _dbus_string_equal_len (a, b, first_a_blank);
136 auth_state_from_string (const DBusString *str)
138 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
139 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
140 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
141 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
142 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
143 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
144 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
145 return DBUS_AUTH_STATE_NEED_DISCONNECT;
146 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
147 return DBUS_AUTH_STATE_AUTHENTICATED;
153 auth_state_to_string (DBusAuthState state)
157 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
158 return "WAITING_FOR_INPUT";
159 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
160 return "WAITING_FOR_MEMORY";
161 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
162 return "HAVE_BYTES_TO_SEND";
163 case DBUS_AUTH_STATE_NEED_DISCONNECT:
164 return "NEED_DISCONNECT";
165 case DBUS_AUTH_STATE_AUTHENTICATED:
166 return "AUTHENTICATED";
173 split_string (DBusString *str)
175 int i, j, k, count, end;
178 end = _dbus_string_get_length (str);
181 _dbus_string_skip_blank (str, i, &i);
182 for (count = 0; i < end; count++)
184 _dbus_string_find_blank (str, i, &i);
185 _dbus_string_skip_blank (str, i, &i);
188 array = dbus_new0 (char *, count + 1);
193 _dbus_string_skip_blank (str, i, &i);
194 for (k = 0; k < count; k++)
196 _dbus_string_find_blank (str, i, &j);
198 array[k] = dbus_malloc (j - i + 1);
199 if (array[k] == NULL)
201 dbus_free_string_array (array);
205 _dbus_string_get_const_data_len (str, i, j - i), j - i);
206 array[k][j - i] = '\0';
208 _dbus_string_skip_blank (str, j, &i);
216 auth_set_unix_credentials(DBusAuth *auth,
220 DBusCredentials *credentials;
222 credentials = _dbus_credentials_new ();
223 if (credentials == NULL)
224 _dbus_assert_not_reached ("no memory");
226 if (uid != DBUS_UID_UNSET)
228 if (!_dbus_credentials_add_unix_uid (credentials, uid))
229 _dbus_assert_not_reached ("no memory");
231 if (pid != DBUS_PID_UNSET)
233 if (!_dbus_credentials_add_pid (credentials, pid))
234 _dbus_assert_not_reached ("no memory");
236 _dbus_auth_set_credentials (auth, credentials);
238 _dbus_credentials_unref (credentials);
242 * Runs an "auth script" which is a script for testing the
243 * authentication protocol. Scripts send and receive data, and then
244 * include assertions about the state of both ends of the connection
245 * after processing the data. A script succeeds if these assertions
248 * @param filename the file containing the script to run
249 * @returns #TRUE if the script succeeds, #FALSE otherwise
252 _dbus_auth_script_run (const DBusString *filename)
255 DBusError error = DBUS_ERROR_INIT;
260 DBusString from_auth;
268 _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
269 _dbus_string_init_const (&context, "org_freedesktop_test");
271 if (!_dbus_string_init (&file))
274 if (!_dbus_string_init (&line))
276 _dbus_string_free (&file);
280 if (!_dbus_string_init (&from_auth))
282 _dbus_string_free (&file);
283 _dbus_string_free (&line);
287 if (!_dbus_file_get_contents (&file, filename, &error)) {
288 _dbus_warn ("Getting contents of %s failed: %s\n",
289 _dbus_string_get_const_data (filename), error.message);
290 dbus_error_free (&error);
294 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
298 while (_dbus_string_pop_line (&file, &line))
302 /* _dbus_warn ("%s\n", _dbus_string_get_const_data (&line)); */
304 _dbus_string_delete_leading_blanks (&line);
308 while ((state = _dbus_auth_do_work (auth)) ==
309 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
311 const DBusString *tmp;
312 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
314 int count = _dbus_string_get_length (tmp);
316 if (_dbus_string_copy (tmp, 0, &from_auth,
317 _dbus_string_get_length (&from_auth)))
318 _dbus_auth_bytes_sent (auth, count);
323 if (_dbus_string_get_length (&line) == 0)
328 else if (_dbus_string_starts_with_c_str (&line,
331 /* Ignore this comment */
335 else if (_dbus_string_starts_with_c_str (&line,
338 /* Ignore this line */
341 else if (_dbus_string_starts_with_c_str (&line,
345 fprintf (stderr, "skipping unix only auth script\n");
351 else if (_dbus_string_starts_with_c_str (&line,
354 /* Ignore this line */
357 else if (_dbus_string_starts_with_c_str (&line,
361 fprintf (stderr, "skipping windows only auth script\n");
366 else if (_dbus_string_starts_with_c_str (&line,
369 DBusCredentials *creds;
373 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
377 auth = _dbus_auth_client_new ();
380 _dbus_warn ("no memory to create DBusAuth\n");
385 _dbus_auth_ref (auth);
386 _dbus_auth_unref (auth);
388 creds = _dbus_credentials_new_from_current_process ();
391 _dbus_warn ("no memory for credentials\n");
392 _dbus_auth_unref (auth);
397 if (!_dbus_auth_set_credentials (auth, creds))
399 _dbus_warn ("no memory for setting credentials\n");
400 _dbus_auth_unref (auth);
402 _dbus_credentials_unref (creds);
406 _dbus_credentials_unref (creds);
408 else if (_dbus_string_starts_with_c_str (&line,
411 DBusCredentials *creds;
415 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
419 auth = _dbus_auth_server_new (&guid);
422 _dbus_warn ("no memory to create DBusAuth\n");
427 _dbus_auth_ref (auth);
428 _dbus_auth_unref (auth);
430 creds = _dbus_credentials_new_from_current_process ();
433 _dbus_warn ("no memory for credentials\n");
434 _dbus_auth_unref (auth);
439 if (!_dbus_auth_set_credentials (auth, creds))
441 _dbus_warn ("no memory for setting credentials\n");
442 _dbus_auth_unref (auth);
444 _dbus_credentials_unref (creds);
448 _dbus_credentials_unref (creds);
450 _dbus_auth_set_context (auth, &context);
452 else if (auth == NULL)
454 _dbus_warn ("must specify CLIENT or SERVER\n");
458 else if (_dbus_string_starts_with_c_str (&line,
461 auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
463 else if (_dbus_string_starts_with_c_str (&line,
466 auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
468 else if (_dbus_string_starts_with_c_str (&line,
469 "SILLY_CREDENTIALS"))
471 auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
473 else if (_dbus_string_starts_with_c_str (&line,
478 _dbus_string_delete_first_word (&line);
479 mechs = split_string (&line);
480 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
481 dbus_free_string_array (mechs);
483 else if (_dbus_string_starts_with_c_str (&line,
488 _dbus_string_delete_first_word (&line);
490 if (!_dbus_string_init (&to_send))
492 _dbus_warn ("no memory to allocate string\n");
496 if (!append_quoted_string (&to_send, &line))
498 _dbus_warn ("failed to append quoted string line %d\n",
500 _dbus_string_free (&to_send);
504 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
506 if (!_dbus_string_append (&to_send, "\r\n"))
508 _dbus_warn ("failed to append \r\n from line %d\n",
510 _dbus_string_free (&to_send);
514 /* Replace USERID_HEX with our username in hex */
518 if (_dbus_string_find (&to_send, 0,
519 "USERID_HEX", &where))
523 if (!_dbus_string_init (&username))
525 _dbus_warn ("no memory for userid\n");
526 _dbus_string_free (&to_send);
530 if (!_dbus_append_user_from_current_process (&username))
532 _dbus_warn ("no memory for userid\n");
533 _dbus_string_free (&username);
534 _dbus_string_free (&to_send);
538 _dbus_string_delete (&to_send, where, (int) strlen ("USERID_HEX"));
540 if (!_dbus_string_hex_encode (&username, 0,
543 _dbus_warn ("no memory to subst USERID_HEX\n");
544 _dbus_string_free (&username);
545 _dbus_string_free (&to_send);
549 _dbus_string_free (&username);
551 else if (_dbus_string_find (&to_send, 0,
552 "USERNAME_HEX", &where))
556 if (!_dbus_string_init (&username))
558 _dbus_warn ("no memory for username\n");
559 _dbus_string_free (&to_send);
563 if (!_dbus_append_user_from_current_process (&username))
565 _dbus_warn ("no memory for username\n");
566 _dbus_string_free (&username);
567 _dbus_string_free (&to_send);
571 _dbus_string_delete (&to_send, where, (int) strlen ("USERNAME_HEX"));
573 if (!_dbus_string_hex_encode (&username, 0,
576 _dbus_warn ("no memory to subst USERNAME_HEX\n");
577 _dbus_string_free (&username);
578 _dbus_string_free (&to_send);
582 _dbus_string_free (&username);
589 _dbus_auth_get_buffer (auth, &buffer);
590 if (!_dbus_string_copy (&to_send, 0,
591 buffer, _dbus_string_get_length (buffer)))
593 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
594 _dbus_string_free (&to_send);
595 _dbus_auth_return_buffer (auth, buffer);
599 _dbus_auth_return_buffer (auth, buffer);
602 _dbus_string_free (&to_send);
604 else if (_dbus_string_starts_with_c_str (&line,
607 DBusAuthState expected;
609 _dbus_string_delete_first_word (&line);
611 expected = auth_state_from_string (&line);
614 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
618 if (expected != state)
620 _dbus_warn ("expected auth state %s but got %s on line %d\n",
621 auth_state_to_string (expected),
622 auth_state_to_string (state),
627 else if (_dbus_string_starts_with_c_str (&line,
632 _dbus_string_delete_first_word (&line);
634 if (!_dbus_string_init (&received))
636 _dbus_warn ("no mem to allocate string received\n");
640 if (!_dbus_string_pop_line (&from_auth, &received))
642 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
643 _dbus_string_get_const_data (&line), line_no);
644 _dbus_string_free (&received);
648 if (!same_first_word (&received, &line))
650 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
652 _dbus_string_get_const_data (&line),
653 _dbus_string_get_const_data (&received));
654 _dbus_string_free (&received);
658 _dbus_string_free (&received);
660 else if (_dbus_string_starts_with_c_str (&line,
664 const DBusString *unused;
666 _dbus_string_delete_first_word (&line);
668 if (!_dbus_string_init (&expected))
670 _dbus_warn ("no mem to allocate string expected\n");
674 if (!append_quoted_string (&expected, &line))
676 _dbus_warn ("failed to append quoted string line %d\n",
678 _dbus_string_free (&expected);
682 _dbus_auth_get_unused_bytes (auth, &unused);
684 if (_dbus_string_equal (&expected, unused))
686 _dbus_auth_delete_unused_bytes (auth);
687 _dbus_string_free (&expected);
691 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
692 _dbus_string_get_const_data (&expected),
693 _dbus_string_get_const_data (unused));
694 _dbus_string_free (&expected);
698 else if (_dbus_string_starts_with_c_str (&line,
699 "EXPECT_HAVE_NO_CREDENTIALS"))
701 DBusCredentials *authorized_identity;
703 authorized_identity = _dbus_auth_get_identity (auth);
704 if (!_dbus_credentials_are_anonymous (authorized_identity))
706 _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized\n");
710 else if (_dbus_string_starts_with_c_str (&line,
711 "EXPECT_HAVE_SOME_CREDENTIALS"))
713 DBusCredentials *authorized_identity;
715 authorized_identity = _dbus_auth_get_identity (auth);
716 if (_dbus_credentials_are_anonymous (authorized_identity))
718 _dbus_warn ("Expected to have some credentials, but we don't\n");
722 else if (_dbus_string_starts_with_c_str (&line,
727 _dbus_string_delete_first_word (&line);
729 if (!_dbus_string_init (&expected))
731 _dbus_warn ("no mem to allocate string expected\n");
735 if (!append_quoted_string (&expected, &line))
737 _dbus_warn ("failed to append quoted string line %d\n",
739 _dbus_string_free (&expected);
743 if (_dbus_string_equal_len (&expected, &from_auth,
744 _dbus_string_get_length (&expected)))
746 _dbus_string_delete (&from_auth, 0,
747 _dbus_string_get_length (&expected));
748 _dbus_string_free (&expected);
752 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
753 _dbus_string_get_const_data (&expected),
754 _dbus_string_get_const_data (&from_auth));
755 _dbus_string_free (&expected);
762 goto next_iteration; /* skip parse_failed */
766 _dbus_warn ("couldn't process line %d \"%s\"\n",
767 line_no, _dbus_string_get_const_data (&line));
774 _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER\n");
777 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
779 const DBusString *unused;
781 _dbus_auth_get_unused_bytes (auth, &unused);
783 if (_dbus_string_get_length (unused) > 0)
785 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
790 if (_dbus_string_get_length (&from_auth) > 0)
792 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
793 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
801 _dbus_auth_unref (auth);
803 _dbus_string_free (&file);
804 _dbus_string_free (&line);
805 _dbus_string_free (&from_auth);
811 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */