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"
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_WITH_UNUSED_BYTES"))
145 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
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_WITH_UNUSED_BYTES:
166 return "AUTHENTICATED_WITH_UNUSED_BYTES";
167 case DBUS_AUTH_STATE_AUTHENTICATED:
168 return "AUTHENTICATED";
175 * Runs an "auth script" which is a script for testing the
176 * authentication protocol. Scripts send and receive data, and then
177 * include assertions about the state of both ends of the connection
178 * after processing the data. A script succeeds if these assertions
181 * @param filename the file containing the script to run
182 * @returns #TRUE if the script succeeds, #FALSE otherwise
185 _dbus_auth_script_run (const DBusString *filename)
193 DBusString from_auth;
200 _dbus_string_init_const (&context, "org_freedesktop_test");
202 if (!_dbus_string_init (&file))
205 if (!_dbus_string_init (&line))
207 _dbus_string_free (&file);
211 if (!_dbus_string_init (&from_auth))
213 _dbus_string_free (&file);
214 _dbus_string_free (&line);
218 dbus_error_init (&error);
219 if (!_dbus_file_get_contents (&file, filename, &error)) {
220 _dbus_warn ("Getting contents of %s failed: %s\n",
221 _dbus_string_get_const_data (filename), error.message);
222 dbus_error_free (&error);
226 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
229 while (_dbus_string_pop_line (&file, &line))
233 _dbus_string_delete_leading_blanks (&line);
237 while ((state = _dbus_auth_do_work (auth)) ==
238 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
240 const DBusString *tmp;
241 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
243 int count = _dbus_string_get_length (tmp);
245 if (_dbus_string_copy (tmp, 0, &from_auth,
246 _dbus_string_get_length (&from_auth)))
247 _dbus_auth_bytes_sent (auth, count);
252 if (_dbus_string_get_length (&line) == 0)
257 else if (_dbus_string_starts_with_c_str (&line,
260 /* Ignore this comment */
263 else if (_dbus_string_starts_with_c_str (&line,
266 DBusCredentials creds;
270 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
274 auth = _dbus_auth_client_new ();
277 _dbus_warn ("no memory to create DBusAuth\n");
281 _dbus_credentials_from_current_process (&creds);
282 _dbus_auth_set_credentials (auth, &creds);
284 else if (_dbus_string_starts_with_c_str (&line,
287 DBusCredentials creds;
291 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
295 auth = _dbus_auth_server_new ();
298 _dbus_warn ("no memory to create DBusAuth\n");
302 _dbus_credentials_from_current_process (&creds);
303 _dbus_auth_set_credentials (auth, &creds);
304 _dbus_auth_set_context (auth, &context);
306 else if (auth == NULL)
308 _dbus_warn ("must specify CLIENT or SERVER\n");
312 else if (_dbus_string_starts_with_c_str (&line,
315 DBusCredentials creds = { -1, -1, -1 };
316 _dbus_auth_set_credentials (auth, &creds);
318 else if (_dbus_string_starts_with_c_str (&line,
321 DBusCredentials creds = { -1, 0, 0 };
322 _dbus_auth_set_credentials (auth, &creds);
324 else if (_dbus_string_starts_with_c_str (&line,
325 "SILLY_CREDENTIALS"))
327 DBusCredentials creds = { -1, 4312, 1232 };
328 _dbus_auth_set_credentials (auth, &creds);
330 else if (_dbus_string_starts_with_c_str (&line,
335 _dbus_string_delete_first_word (&line);
337 if (!_dbus_string_init (&to_send))
339 _dbus_warn ("no memory to allocate string\n");
343 if (!append_quoted_string (&to_send, &line))
345 _dbus_warn ("failed to append quoted string line %d\n",
347 _dbus_string_free (&to_send);
351 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
353 if (!_dbus_string_append (&to_send, "\r\n"))
355 _dbus_warn ("failed to append \r\n from line %d\n",
357 _dbus_string_free (&to_send);
361 /* Replace USERID_BASE64 with our username in base64 */
365 if (_dbus_string_find (&to_send, 0,
366 "USERID_BASE64", &where))
370 if (!_dbus_string_init (&username))
372 _dbus_warn ("no memory for userid\n");
373 _dbus_string_free (&to_send);
377 if (!_dbus_string_append_uint (&username,
380 _dbus_warn ("no memory for userid\n");
381 _dbus_string_free (&username);
382 _dbus_string_free (&to_send);
386 _dbus_string_delete (&to_send, where, strlen ("USERID_BASE64"));
388 if (!_dbus_string_base64_encode (&username, 0,
391 _dbus_warn ("no memory to subst USERID_BASE64\n");
392 _dbus_string_free (&username);
393 _dbus_string_free (&to_send);
397 _dbus_string_free (&username);
399 else if (_dbus_string_find (&to_send, 0,
400 "USERNAME_BASE64", &where))
405 if (!_dbus_string_init (&username))
407 _dbus_warn ("no memory for username\n");
408 _dbus_string_free (&to_send);
412 if (!_dbus_username_from_current_process (&u) ||
413 !_dbus_string_copy (u, 0, &username,
414 _dbus_string_get_length (&username)))
416 _dbus_warn ("no memory for username\n");
417 _dbus_string_free (&username);
418 _dbus_string_free (&to_send);
422 _dbus_string_delete (&to_send, where, strlen ("USERNAME_BASE64"));
424 if (!_dbus_string_base64_encode (&username, 0,
427 _dbus_warn ("no memory to subst USERNAME_BASE64\n");
428 _dbus_string_free (&username);
429 _dbus_string_free (&to_send);
433 _dbus_string_free (&username);
440 _dbus_auth_get_buffer (auth, &buffer);
441 if (!_dbus_string_copy (&to_send, 0,
442 buffer, _dbus_string_get_length (buffer)))
444 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
445 _dbus_string_free (&to_send);
446 _dbus_auth_return_buffer (auth, buffer, 0);
450 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
453 _dbus_string_free (&to_send);
455 else if (_dbus_string_starts_with_c_str (&line,
458 DBusAuthState expected;
460 _dbus_string_delete_first_word (&line);
462 expected = auth_state_from_string (&line);
465 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
469 if (expected != state)
471 _dbus_warn ("expected auth state %s but got %s on line %d\n",
472 auth_state_to_string (expected),
473 auth_state_to_string (state),
478 else if (_dbus_string_starts_with_c_str (&line,
483 _dbus_string_delete_first_word (&line);
485 if (!_dbus_string_init (&received))
487 _dbus_warn ("no mem to allocate string received\n");
491 if (!_dbus_string_pop_line (&from_auth, &received))
493 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
494 _dbus_string_get_const_data (&line), line_no);
495 _dbus_string_free (&received);
499 if (!same_first_word (&received, &line))
501 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
503 _dbus_string_get_const_data (&line),
504 _dbus_string_get_const_data (&received));
505 _dbus_string_free (&received);
509 _dbus_string_free (&received);
511 else if (_dbus_string_starts_with_c_str (&line,
515 const DBusString *unused;
517 _dbus_string_delete_first_word (&line);
519 if (!_dbus_string_init (&expected))
521 _dbus_warn ("no mem to allocate string expected\n");
525 if (!append_quoted_string (&expected, &line))
527 _dbus_warn ("failed to append quoted string line %d\n",
529 _dbus_string_free (&expected);
533 _dbus_auth_get_unused_bytes (auth, &unused);
535 if (_dbus_string_equal (&expected, unused))
537 _dbus_auth_delete_unused_bytes (auth);
538 _dbus_string_free (&expected);
542 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
543 _dbus_string_get_const_data (&expected),
544 _dbus_string_get_const_data (unused));
545 _dbus_string_free (&expected);
549 else if (_dbus_string_starts_with_c_str (&line,
554 _dbus_string_delete_first_word (&line);
556 if (!_dbus_string_init (&expected))
558 _dbus_warn ("no mem to allocate string expected\n");
562 if (!append_quoted_string (&expected, &line))
564 _dbus_warn ("failed to append quoted string line %d\n",
566 _dbus_string_free (&expected);
570 if (_dbus_string_equal_len (&expected, &from_auth,
571 _dbus_string_get_length (&expected)))
573 _dbus_string_delete (&from_auth, 0,
574 _dbus_string_get_length (&expected));
575 _dbus_string_free (&expected);
579 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
580 _dbus_string_get_const_data (&expected),
581 _dbus_string_get_const_data (&from_auth));
582 _dbus_string_free (&expected);
589 goto next_iteration; /* skip parse_failed */
593 _dbus_warn ("couldn't process line %d \"%s\"\n",
594 line_no, _dbus_string_get_const_data (&line));
600 state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
602 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
606 if (_dbus_string_get_length (&from_auth) > 0)
608 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
609 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
617 _dbus_auth_unref (auth);
619 _dbus_string_free (&file);
620 _dbus_string_free (&line);
621 _dbus_string_free (&from_auth);
627 #endif /* DBUS_BUILD_TESTS */