From 269d74f214f87bed0f6fd58f1c848240d31e0b9f Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Wed, 26 Mar 2003 07:16:03 +0000 Subject: 2003-03-26 Havoc Pennington * bus/test-main.c, dbus/dbus-test.c (main): check memleaks after every test so it's quick and easy to see which leaked, and so we test multiple dbus_shutdown() calls * configure.in: change configure.in XML stuff to also support expat * config-loader-libxml.c: some hacking * config-loader-expat.c: some hacking * config-parser.c: some hacking, plus tests --- bus/config-parser.c | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) (limited to 'bus/config-parser.c') diff --git a/bus/config-parser.c b/bus/config-parser.c index 0d33d2b9..8fb3b29e 100644 --- a/bus/config-parser.c +++ b/bus/config-parser.c @@ -183,6 +183,7 @@ bus_config_parser_start_element (BusConfigParser *parser, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); + return TRUE; } dbus_bool_t @@ -192,6 +193,7 @@ bus_config_parser_end_element (BusConfigParser *parser, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); + return TRUE; } dbus_bool_t @@ -201,6 +203,7 @@ bus_config_parser_content (BusConfigParser *parser, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); + return TRUE; } dbus_bool_t @@ -209,6 +212,7 @@ bus_config_parser_finished (BusConfigParser *parser, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); + return TRUE; } const char* @@ -216,13 +220,233 @@ bus_config_parser_get_user (BusConfigParser *parser) { + return NULL; } #ifdef DBUS_BUILD_TESTS +#include + +typedef enum +{ + VALID, + INVALID, + UNKNOWN +} Validity; + +static dbus_bool_t +do_load (const DBusString *full_path, + Validity validity, + dbus_bool_t oom_possible) +{ + BusConfigParser *parser; + DBusError error; + + dbus_error_init (&error); + + parser = bus_config_load (full_path, &error); + if (parser == NULL) + { + _DBUS_ASSERT_ERROR_IS_SET (&error); + + if (oom_possible && + dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + { + _dbus_verbose ("Failed to load valid file due to OOM\n"); + dbus_error_free (&error); + return TRUE; + } + else if (validity == VALID) + { + _dbus_warn ("Failed to load valid file but still had memory: %s\n", + error.message); + + dbus_error_free (&error); + return FALSE; + } + else + { + dbus_error_free (&error); + return TRUE; + } + } + else + { + _DBUS_ASSERT_ERROR_IS_CLEAR (&error); + + bus_config_parser_unref (parser); + + if (validity == INVALID) + { + _dbus_warn ("Accepted invalid file\n"); + return FALSE; + } + + return TRUE; + } +} + +static dbus_bool_t +check_oom_loading (const DBusString *full_path, + Validity validity) +{ + int approx_mallocs; + + /* Run once to see about how many mallocs are involved */ + + _dbus_set_fail_alloc_counter (_DBUS_INT_MAX); + + if (!do_load (full_path, validity, FALSE)) + return FALSE; + + approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter (); + + _dbus_verbose ("=================\nabout %d mallocs total\n=================\n", + approx_mallocs); + + approx_mallocs += 10; /* fudge factor */ + + /* Now run failing each malloc */ + + while (approx_mallocs >= 0) + { + + _dbus_set_fail_alloc_counter (approx_mallocs); + + _dbus_verbose ("\n===\n(will fail malloc %d)\n===\n", + approx_mallocs); + + if (!do_load (full_path, validity, TRUE)) + return FALSE; + + approx_mallocs -= 1; + } + + _dbus_set_fail_alloc_counter (_DBUS_INT_MAX); + + _dbus_verbose ("=================\n all iterations passed\n=================\n"); + + return TRUE; +} + +static dbus_bool_t +process_test_subdir (const DBusString *test_base_dir, + const char *subdir, + Validity validity) +{ + DBusString test_directory; + DBusString filename; + DBusDirIter *dir; + dbus_bool_t retval; + DBusError error; + + 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"); + + dbus_error_init (&error); + dir = _dbus_directory_open (&test_directory, &error); + if (dir == NULL) + { + const char *s; + _dbus_string_get_const_data (&test_directory, &s); + _dbus_warn ("Could not open %s: %s\n", s, + error.message); + dbus_error_free (&error); + goto failed; + } + + printf ("Testing:\n"); + + next: + while (_dbus_directory_get_next_file (dir, &filename, &error)) + { + 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 (&full_path, ".conf")) + { + const char *filename_c; + _dbus_string_get_const_data (&filename, &filename_c); + _dbus_verbose ("Skipping non-.conf file %s\n", + filename_c); + _dbus_string_free (&full_path); + goto next; + } + + { + const char *s; + _dbus_string_get_const_data (&filename, &s); + printf (" %s\n", s); + } + + _dbus_verbose (" expecting %s\n", + validity == VALID ? "valid" : + (validity == INVALID ? "invalid" : + (validity == UNKNOWN ? "unknown" : "???"))); + + if (!check_oom_loading (&full_path, validity)) + _dbus_assert_not_reached ("test failed"); + + _dbus_string_free (&full_path); + } + + if (dbus_error_is_set (&error)) + { + const char *s; + _dbus_string_get_const_data (&test_directory, &s); + _dbus_warn ("Could not get next file in %s: %s\n", + s, error.message); + dbus_error_free (&error); + goto failed; + } + + retval = TRUE; + + failed: + + if (dir) + _dbus_directory_close (dir); + _dbus_string_free (&test_directory); + _dbus_string_free (&filename); + + return retval; +} dbus_bool_t bus_config_parser_test (const DBusString *test_data_dir) { + if (test_data_dir == NULL || + _dbus_string_get_length (test_data_dir) == 0) + { + printf ("No test data\n"); + return TRUE; + } + + if (!process_test_subdir (test_data_dir, "valid-config-files", VALID)) + return FALSE; return TRUE; } -- cgit