From c9ea8fac502c6109713aa372c4c8cfafd0b86858 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 13 Feb 2003 02:33:32 +0000 Subject: 2003-02-12 Havoc Pennington * dbus/dbus-string.c (_dbus_string_pop_line): fix to also strip \r off of popped lines * dbus/dbus-auth.c (_dbus_auth_test): write code to run auth scripts * dbus/dbus-auth-script.c (_dbus_auth_script_run): when doing a SEND, append \r\n --- dbus/dbus-auth-script.c | 22 ++++++-- dbus/dbus-auth.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-string.c | 27 +++++++--- 3 files changed, 169 insertions(+), 11 deletions(-) (limited to 'dbus') diff --git a/dbus/dbus-auth-script.c b/dbus/dbus-auth-script.c index a9df6d90..6c5009a5 100644 --- a/dbus/dbus-auth-script.c +++ b/dbus/dbus-auth-script.c @@ -82,8 +82,6 @@ append_quoted_string (DBusString *dest, ++i; } - if (!_dbus_string_append_byte (dest, '\0')) - return FALSE; return TRUE; } @@ -280,6 +278,20 @@ _dbus_auth_script_run (const DBusString *filename) goto out; } + { + const char *s4; + _dbus_string_get_const_data (&to_send, &s4); + _dbus_verbose ("Sending '%s'\n", s4); + } + + if (!_dbus_string_append (&to_send, "\r\n")) + { + _dbus_warn ("failed to append \r\n from line %d\n", + line_no); + _dbus_string_free (&to_send); + goto out; + } + if (!_dbus_auth_bytes_received (auth, &to_send)) { _dbus_warn ("not enough memory to call bytes_received\n"); @@ -340,8 +352,8 @@ _dbus_auth_script_run (const DBusString *filename) const char *s1, *s2; _dbus_string_get_const_data (&line, &s1); _dbus_string_get_const_data (&received, &s2); - _dbus_warn ("expected command '%s' and got '%s' line %d\n", - s1, s2, line_no); + _dbus_warn ("line %d expected command '%s' and got '%s'\n", + line_no, s1, s2); _dbus_string_free (&received); goto out; } @@ -386,7 +398,7 @@ _dbus_auth_script_run (const DBusString *filename) _dbus_auth_unref (auth); _dbus_string_free (&file); - _dbus_string_free (&file); + _dbus_string_free (&line); _dbus_string_free (&from_auth); return retval; diff --git a/dbus/dbus-auth.c b/dbus/dbus-auth.c index 697723b1..071e3416 100644 --- a/dbus/dbus-auth.c +++ b/dbus/dbus-auth.c @@ -1061,6 +1061,8 @@ process_command (DBusAuth *auth) int i, j; dbus_bool_t retval; + _dbus_verbose (" trying process_command()\n"); + retval = FALSE; eol = 0; @@ -1596,12 +1598,141 @@ _dbus_auth_get_identity (DBusAuth *auth, #ifdef DBUS_BUILD_TESTS #include "dbus-test.h" +#include "dbus-auth-script.h" #include +static dbus_bool_t +process_test_subdir (const DBusString *test_base_dir, + const char *subdir) +{ + DBusString test_directory; + DBusString filename; + DBusDirIter *dir; + dbus_bool_t retval; + DBusResultCode result; + + retval = FALSE; + dir = NULL; + + if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX)) + _dbus_assert_not_reached ("didn't allocate test_directory\n"); + + _dbus_string_init_const (&filename, subdir); + + if (!_dbus_string_copy (test_base_dir, 0, + &test_directory, 0)) + _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory"); + + if (!_dbus_concat_dir_and_file (&test_directory, &filename)) + _dbus_assert_not_reached ("couldn't allocate full path"); + + _dbus_string_free (&filename); + if (!_dbus_string_init (&filename, _DBUS_INT_MAX)) + _dbus_assert_not_reached ("didn't allocate filename string\n"); + + dir = _dbus_directory_open (&test_directory, &result); + if (dir == NULL) + { + const char *s; + _dbus_string_get_const_data (&test_directory, &s); + _dbus_warn ("Could not open %s: %s\n", s, + dbus_result_to_string (result)); + goto failed; + } + + printf ("Testing:\n"); + + result = DBUS_RESULT_SUCCESS; + next: + while (_dbus_directory_get_next_file (dir, &filename, &result)) + { + DBusString full_path; + + if (!_dbus_string_init (&full_path, _DBUS_INT_MAX)) + _dbus_assert_not_reached ("couldn't init string"); + + if (!_dbus_string_copy (&test_directory, 0, &full_path, 0)) + _dbus_assert_not_reached ("couldn't copy dir to full_path"); + + if (!_dbus_concat_dir_and_file (&full_path, &filename)) + _dbus_assert_not_reached ("couldn't concat file to dir"); + + if (!_dbus_string_ends_with_c_str (&filename, ".auth-script")) + { + const char *filename_c; + _dbus_string_get_const_data (&filename, &filename_c); + _dbus_verbose ("Skipping non-.auth-script file %s\n", + filename_c); + goto next; + } + + { + const char *s; + _dbus_string_get_const_data (&filename, &s); + printf (" %s\n", s); + } + + if (!_dbus_auth_script_run (&full_path)) + { + _dbus_string_free (&full_path); + goto failed; + } + else + _dbus_string_free (&full_path); + } + + if (result != DBUS_RESULT_SUCCESS) + { + const char *s; + _dbus_string_get_const_data (&test_directory, &s); + _dbus_warn ("Could not get next file in %s: %s\n", + s, dbus_result_to_string (result)); + goto failed; + } + + retval = TRUE; + + failed: + + if (dir) + _dbus_directory_close (dir); + _dbus_string_free (&test_directory); + _dbus_string_free (&filename); + + return retval; +} + +static dbus_bool_t +process_test_dirs (const char *test_data_dir) +{ + DBusString test_directory; + dbus_bool_t retval; + + retval = FALSE; + + _dbus_string_init_const (&test_directory, test_data_dir); + + if (!process_test_subdir (&test_directory, "auth")) + goto failed; + + retval = TRUE; + + failed: + + _dbus_string_free (&test_directory); + + return retval; +} + dbus_bool_t _dbus_auth_test (const char *test_data_dir) { + if (test_data_dir == NULL) + return TRUE; + + if (!process_test_dirs (test_data_dir)) + return FALSE; return TRUE; } diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index ded726fa..daa4297d 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -1416,10 +1416,10 @@ _dbus_string_skip_blank (const DBusString *str, } /** - * Assigns a newline-terminated line from the front of the string - * to the given dest string. The dest string's previous contents are - * deleted. If the source string contains no newline, moves the - * entire source string to the dest string. + * Assigns a newline-terminated or \r\n-terminated line from the front + * of the string to the given dest string. The dest string's previous + * contents are deleted. If the source string contains no newline, + * moves the entire source string to the dest string. * * @param source the source string * @param dest the destination string (contents are replaced) @@ -1455,12 +1455,23 @@ _dbus_string_pop_line (DBusString *source, return FALSE; } - /* dump the newline */ + /* dump the newline and the \r if we have one */ if (have_newline) { + dbus_bool_t have_cr; + _dbus_assert (_dbus_string_get_length (dest) > 0); + + if (_dbus_string_get_length (dest) > 1 && + _dbus_string_get_byte (dest, + _dbus_string_get_length (dest) - 2) == '\r') + have_cr = TRUE; + else + have_cr = FALSE; + _dbus_string_set_length (dest, - _dbus_string_get_length (dest) - 1); + _dbus_string_get_length (dest) - + (have_cr ? 2 : 1)); } return TRUE; @@ -2542,6 +2553,10 @@ _dbus_string_test (void) _dbus_assert_not_reached ("didn't find 'He'"); _dbus_assert (i == 0); + if (!_dbus_string_find (&str, 0, "Hello", &i)) + _dbus_assert_not_reached ("didn't find 'Hello'"); + _dbus_assert (i == 0); + if (!_dbus_string_find (&str, 0, "ello", &i)) _dbus_assert_not_reached ("didn't find 'ello'"); _dbus_assert (i == 1); -- cgit