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.0
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");
282 _dbus_auth_ref (auth);
283 _dbus_auth_unref (auth);
285 _dbus_credentials_from_current_process (&creds);
286 _dbus_auth_set_credentials (auth, &creds);
288 else if (_dbus_string_starts_with_c_str (&line,
291 DBusCredentials creds;
295 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
299 auth = _dbus_auth_server_new ();
302 _dbus_warn ("no memory to create DBusAuth\n");
307 _dbus_auth_ref (auth);
308 _dbus_auth_unref (auth);
310 _dbus_credentials_from_current_process (&creds);
311 _dbus_auth_set_credentials (auth, &creds);
312 _dbus_auth_set_context (auth, &context);
314 else if (auth == NULL)
316 _dbus_warn ("must specify CLIENT or SERVER\n");
320 else if (_dbus_string_starts_with_c_str (&line,
323 DBusCredentials creds = { -1, -1, -1 };
324 _dbus_auth_set_credentials (auth, &creds);
326 else if (_dbus_string_starts_with_c_str (&line,
329 DBusCredentials creds = { -1, 0, 0 };
330 _dbus_auth_set_credentials (auth, &creds);
332 else if (_dbus_string_starts_with_c_str (&line,
333 "SILLY_CREDENTIALS"))
335 DBusCredentials creds = { -1, 4312, 1232 };
336 _dbus_auth_set_credentials (auth, &creds);
338 else if (_dbus_string_starts_with_c_str (&line,
343 _dbus_string_delete_first_word (&line);
345 if (!_dbus_string_init (&to_send))
347 _dbus_warn ("no memory to allocate string\n");
351 if (!append_quoted_string (&to_send, &line))
353 _dbus_warn ("failed to append quoted string line %d\n",
355 _dbus_string_free (&to_send);
359 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
361 if (!_dbus_string_append (&to_send, "\r\n"))
363 _dbus_warn ("failed to append \r\n from line %d\n",
365 _dbus_string_free (&to_send);
369 /* Replace USERID_HEX with our username in hex */
373 if (_dbus_string_find (&to_send, 0,
374 "USERID_HEX", &where))
378 if (!_dbus_string_init (&username))
380 _dbus_warn ("no memory for userid\n");
381 _dbus_string_free (&to_send);
385 if (!_dbus_string_append_uint (&username,
388 _dbus_warn ("no memory for userid\n");
389 _dbus_string_free (&username);
390 _dbus_string_free (&to_send);
394 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
396 if (!_dbus_string_hex_encode (&username, 0,
399 _dbus_warn ("no memory to subst USERID_HEX\n");
400 _dbus_string_free (&username);
401 _dbus_string_free (&to_send);
405 _dbus_string_free (&username);
407 else if (_dbus_string_find (&to_send, 0,
408 "USERNAME_HEX", &where))
413 if (!_dbus_string_init (&username))
415 _dbus_warn ("no memory for username\n");
416 _dbus_string_free (&to_send);
420 if (!_dbus_username_from_current_process (&u) ||
421 !_dbus_string_copy (u, 0, &username,
422 _dbus_string_get_length (&username)))
424 _dbus_warn ("no memory for username\n");
425 _dbus_string_free (&username);
426 _dbus_string_free (&to_send);
430 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
432 if (!_dbus_string_hex_encode (&username, 0,
435 _dbus_warn ("no memory to subst USERNAME_HEX\n");
436 _dbus_string_free (&username);
437 _dbus_string_free (&to_send);
441 _dbus_string_free (&username);
448 _dbus_auth_get_buffer (auth, &buffer);
449 if (!_dbus_string_copy (&to_send, 0,
450 buffer, _dbus_string_get_length (buffer)))
452 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
453 _dbus_string_free (&to_send);
454 _dbus_auth_return_buffer (auth, buffer, 0);
458 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
461 _dbus_string_free (&to_send);
463 else if (_dbus_string_starts_with_c_str (&line,
466 DBusAuthState expected;
468 _dbus_string_delete_first_word (&line);
470 expected = auth_state_from_string (&line);
473 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
477 if (expected != state)
479 _dbus_warn ("expected auth state %s but got %s on line %d\n",
480 auth_state_to_string (expected),
481 auth_state_to_string (state),
486 else if (_dbus_string_starts_with_c_str (&line,
491 _dbus_string_delete_first_word (&line);
493 if (!_dbus_string_init (&received))
495 _dbus_warn ("no mem to allocate string received\n");
499 if (!_dbus_string_pop_line (&from_auth, &received))
501 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
502 _dbus_string_get_const_data (&line), line_no);
503 _dbus_string_free (&received);
507 if (!same_first_word (&received, &line))
509 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
511 _dbus_string_get_const_data (&line),
512 _dbus_string_get_const_data (&received));
513 _dbus_string_free (&received);
517 _dbus_string_free (&received);
519 else if (_dbus_string_starts_with_c_str (&line,
523 const DBusString *unused;
525 _dbus_string_delete_first_word (&line);
527 if (!_dbus_string_init (&expected))
529 _dbus_warn ("no mem to allocate string expected\n");
533 if (!append_quoted_string (&expected, &line))
535 _dbus_warn ("failed to append quoted string line %d\n",
537 _dbus_string_free (&expected);
541 _dbus_auth_get_unused_bytes (auth, &unused);
543 if (_dbus_string_equal (&expected, unused))
545 _dbus_auth_delete_unused_bytes (auth);
546 _dbus_string_free (&expected);
550 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
551 _dbus_string_get_const_data (&expected),
552 _dbus_string_get_const_data (unused));
553 _dbus_string_free (&expected);
557 else if (_dbus_string_starts_with_c_str (&line,
562 _dbus_string_delete_first_word (&line);
564 if (!_dbus_string_init (&expected))
566 _dbus_warn ("no mem to allocate string expected\n");
570 if (!append_quoted_string (&expected, &line))
572 _dbus_warn ("failed to append quoted string line %d\n",
574 _dbus_string_free (&expected);
578 if (_dbus_string_equal_len (&expected, &from_auth,
579 _dbus_string_get_length (&expected)))
581 _dbus_string_delete (&from_auth, 0,
582 _dbus_string_get_length (&expected));
583 _dbus_string_free (&expected);
587 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
588 _dbus_string_get_const_data (&expected),
589 _dbus_string_get_const_data (&from_auth));
590 _dbus_string_free (&expected);
597 goto next_iteration; /* skip parse_failed */
601 _dbus_warn ("couldn't process line %d \"%s\"\n",
602 line_no, _dbus_string_get_const_data (&line));
608 state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
610 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
614 if (_dbus_string_get_length (&from_auth) > 0)
616 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
617 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
625 _dbus_auth_unref (auth);
627 _dbus_string_free (&file);
628 _dbus_string_free (&line);
629 _dbus_string_free (&from_auth);
635 #endif /* DBUS_BUILD_TESTS */