From c460d2f3b149d40dafe4cc7f5775b8119d6ea188 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Fri, 5 Sep 2014 14:35:15 +0200 Subject: [PATCH 2/4] Add directory test application. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=57272 --- cmake/test/CMakeLists.txt | 5 +++ dbus/dbus-errors.c | 9 ++++++ dbus/dbus-errors.h | 3 ++ test/test-dir.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 test/test-dir.c diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt index 28bb84c..f7a1d32 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -37,6 +37,10 @@ set (test-spawn_SOURCES ${CMAKE_SOURCE_DIR}/../test/spawn-test.c ) +set (test-dir_SOURCES + ${CMAKE_SOURCE_DIR}/../test/test-dir.c +) + set (test-exit_SOURCES ${CMAKE_SOURCE_DIR}/../test/test-exit.c ) @@ -55,6 +59,7 @@ add_test_executable(test-shell ${test-shell_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) add_test_executable(test-printf ${CMAKE_SOURCE_DIR}/../test/internals/printf.c dbus-testutils) add_helper_executable(test-shell-service ${test-shell-service_SOURCES} dbus-testutils) add_helper_executable(test-spawn ${test-spawn_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) +add_helper_executable(test-dir ${test-dir_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) add_helper_executable(test-exit ${test-exit_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) add_helper_executable(test-segfault ${test-segfault_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) add_helper_executable(test-sleep-forever ${test-sleep-forever_SOURCES} ${DBUS_INTERNAL_LIBRARIES}) diff --git a/dbus/dbus-errors.c b/dbus/dbus-errors.c index a0571a5..cb42160 100644 --- a/dbus/dbus-errors.c +++ b/dbus/dbus-errors.c @@ -334,6 +334,15 @@ dbus_error_is_set (const DBusError *error) return error->name != NULL; } +const char * +dbus_error_get_message (const DBusError *error) +{ + _dbus_return_val_if_fail (error != NULL, FALSE); + _dbus_assert ((error->name != NULL && error->message != NULL) || + (error->name == NULL && error->message == NULL)); + return error->message; +} + /** * Assigns an error name and message to a DBusError. * Does nothing if error is #NULL. diff --git a/dbus/dbus-errors.h b/dbus/dbus-errors.h index e63139a..14c5d48 100644 --- a/dbus/dbus-errors.h +++ b/dbus/dbus-errors.h @@ -83,6 +83,9 @@ dbus_bool_t dbus_error_has_name (const DBusError *error, DBUS_EXPORT dbus_bool_t dbus_error_is_set (const DBusError *error); +DBUS_EXPORT +const char * dbus_error_get_message (const DBusError *error); + /** @} */ DBUS_END_DECLS diff --git a/test/test-dir.c b/test/test-dir.c new file mode 100644 index 0000000..be6ce18 --- /dev/null +++ b/test/test-dir.c @@ -0,0 +1,77 @@ +#include +#include "test-utils.h" + +#include "dbus/dbus-sysdeps.h" + +static void +die (const char *message) +{ + fprintf (stderr, "*** test-dir: %s\n", message); + exit (1); +} + +static void +debug(const char *message) +{ + fprintf (stdout, "+++ test-dir: %s\n", message); +} + +int +main (int argc, + char **argv) +{ + DBusString filename; + DBusString dirname; + DBusError tmp_error; + DBusDirIter *dir; + + if (argc != 2) + die ("run test-dir "); + + dbus_error_init (&tmp_error); + + if (!_dbus_string_init (&filename)) + die ("memory allocation error"); + + if (!_dbus_string_init (&dirname)) + die ("memory allocation error"); + + _dbus_string_append (&dirname, argv[1]); + dir = _dbus_directory_open (&dirname, &tmp_error); + + if (dir == NULL) + die ("could not open directory"); + + while (_dbus_directory_get_next_file (dir, &filename, &tmp_error)) + { + DBusString full_path; + if (!_dbus_string_init (&full_path)) + { + die("memory allocation error"); + } + + if (!_dbus_string_copy (&dirname, 0, &full_path, 0)) + { + die("memory allocation error"); + } + + if (!_dbus_concat_dir_and_file (&full_path, &filename)) + { + die("memory allocation error"); + } + debug (_dbus_string_get_const_data (&filename)); + _dbus_string_free (&full_path); + } + + if (dbus_error_is_set (&tmp_error)) + die (dbus_error_get_message (&tmp_error)); + + _dbus_string_free (&filename); + + if (dir) + _dbus_directory_close (dir); + + _dbus_verbose ("*** Test dir name exiting\n"); + + return 0; +} -- 1.8.4.5