From cccb35dfef8ac4e28594154f8a9a6337fd9d8d7f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 28 Sep 2018 16:46:41 +0100 Subject: [PATCH] test: Move sysdeps tests here Signed-off-by: Simon McVittie --- cmake/test/CMakeLists.txt | 1 + dbus/dbus-sysdeps-util.c | 127 +---------------------- dbus/dbus-test.h | 2 - test/Makefile.am | 1 + test/internals/dbus-sysdeps-util.c | 156 +++++++++++++++++++++++++++++ test/internals/misc-internals.h | 1 + 6 files changed, 164 insertions(+), 124 deletions(-) create mode 100644 test/internals/dbus-sysdeps-util.c diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt index 782a8eca0..3a7218a4f 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -121,6 +121,7 @@ if(DBUS_ENABLE_EMBEDDED_TESTS) ${TEST_DIR}/internals/dbus-marshal-recursive-util.h ${TEST_DIR}/internals/dbus-marshal-validate-util.c ${TEST_DIR}/internals/dbus-string-util.c + ${TEST_DIR}/internals/dbus-sysdeps-util.c ${TEST_DIR}/internals/misc-internals.c ${TEST_DIR}/internals/misc-internals.h) add_test_executable(test-misc-internals "${SOURCES}" dbus-testutils) diff --git a/dbus/dbus-sysdeps-util.c b/dbus/dbus-sysdeps-util.c index 4a7d08cc2..66af910e6 100644 --- a/dbus/dbus-sysdeps-util.c +++ b/dbus/dbus-sysdeps-util.c @@ -1,8 +1,11 @@ /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API * - * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc. - * Copyright (C) 2003 CodeFactory AB + * Copyright 2002-2008 Red Hat, Inc. + * Copyright 2003 CodeFactory AB + * Copyright 2006 Ralf Habacker + * Copyright 2006 Sjoerd Simons + * Copyright 2016-2018 Collabora Ltd. * * Licensed under the Academic Free License version 2.1 * @@ -26,8 +29,6 @@ #include "dbus-sysdeps.h" #include "dbus-internals.h" #include "dbus-string.h" -#include "dbus-test.h" -#include #include @@ -82,121 +83,3 @@ _dbus_get_environment (void) return environment; } - -#ifdef DBUS_ENABLE_EMBEDDED_TESTS -static void -check_dirname (const char *filename, - const char *dirname) -{ - DBusString f, d; - - _dbus_string_init_const (&f, filename); - - if (!_dbus_string_init (&d)) - _dbus_test_fatal ("no memory"); - - if (!_dbus_string_get_dirname (&f, &d)) - _dbus_test_fatal ("no memory"); - - if (!_dbus_string_equal_c_str (&d, dirname)) - { - _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"", - filename, - _dbus_string_get_const_data (&d), - dirname); - exit (1); - } - - _dbus_string_free (&d); -} - -static void -check_path_absolute (const char *path, - dbus_bool_t expected) -{ - DBusString p; - - _dbus_string_init_const (&p, path); - - if (_dbus_path_is_absolute (&p) != expected) - { - _dbus_warn ("For path \"%s\" expected absolute = %d got %d", - path, expected, _dbus_path_is_absolute (&p)); - exit (1); - } -} - -/** - * Unit test for dbus-sysdeps.c. - * - * @returns #TRUE on success. - */ -dbus_bool_t -_dbus_sysdeps_test (const char *test_data_dir _DBUS_GNUC_UNUSED) -{ -#ifdef DBUS_WIN - check_dirname ("foo\\bar", "foo"); - check_dirname ("foo\\\\bar", "foo"); - check_dirname ("foo/\\/bar", "foo"); - check_dirname ("foo\\bar/", "foo"); - check_dirname ("foo//bar\\", "foo"); - check_dirname ("foo\\bar/", "foo"); - check_dirname ("foo/bar\\\\", "foo"); - check_dirname ("\\foo", "\\"); - check_dirname ("\\\\foo", "\\"); - check_dirname ("\\", "\\"); - check_dirname ("\\\\", "\\"); - check_dirname ("\\/", "\\"); - check_dirname ("/\\/", "/"); - check_dirname ("c:\\foo\\bar", "c:\\foo"); - check_dirname ("c:\\foo", "c:\\"); - check_dirname ("c:/foo", "c:/"); - check_dirname ("c:\\", "c:\\"); - check_dirname ("c:/", "c:/"); - check_dirname ("", "."); -#else - check_dirname ("foo", "."); - check_dirname ("foo/bar", "foo"); - check_dirname ("foo//bar", "foo"); - check_dirname ("foo///bar", "foo"); - check_dirname ("foo/bar/", "foo"); - check_dirname ("foo//bar/", "foo"); - check_dirname ("foo///bar/", "foo"); - check_dirname ("foo/bar//", "foo"); - check_dirname ("foo//bar////", "foo"); - check_dirname ("foo///bar///////", "foo"); - check_dirname ("/foo", "/"); - check_dirname ("////foo", "/"); - check_dirname ("/foo/bar", "/foo"); - check_dirname ("/foo//bar", "/foo"); - check_dirname ("/foo///bar", "/foo"); - check_dirname ("/", "/"); - check_dirname ("///", "/"); - check_dirname ("", "."); -#endif - -#ifdef DBUS_WIN - check_path_absolute ("c:/", TRUE); - check_path_absolute ("c:/foo", TRUE); - check_path_absolute ("", FALSE); - check_path_absolute ("foo", FALSE); - check_path_absolute ("foo/bar", FALSE); - check_path_absolute ("", FALSE); - check_path_absolute ("foo\\bar", FALSE); - check_path_absolute ("c:\\", TRUE); - check_path_absolute ("c:\\foo", TRUE); - check_path_absolute ("c:", TRUE); - check_path_absolute ("c:\\foo\\bar", TRUE); - check_path_absolute ("\\", TRUE); - check_path_absolute ("/", TRUE); -#else - check_path_absolute ("/", TRUE); - check_path_absolute ("/foo", TRUE); - check_path_absolute ("", FALSE); - check_path_absolute ("foo", FALSE); - check_path_absolute ("foo/bar", FALSE); -#endif - - return TRUE; -} -#endif /* DBUS_ENABLE_EMBEDDED_TESTS */ diff --git a/dbus/dbus-test.h b/dbus/dbus-test.h index 5dd07e9ed..3ca2ef97d 100644 --- a/dbus/dbus-test.h +++ b/dbus/dbus-test.h @@ -67,8 +67,6 @@ dbus_bool_t _dbus_keyring_test (const char *test_data_dir); DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_data_slot_test (const char *test_data_dir); -dbus_bool_t _dbus_sysdeps_test (const char *test_data_dir); - DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_userdb_test (const char *test_data_dir); diff --git a/test/Makefile.am b/test/Makefile.am index ca96a3eea..6e81d1b66 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -231,6 +231,7 @@ test_misc_internals_SOURCES = \ internals/dbus-marshal-recursive-util.h \ internals/dbus-marshal-validate-util.c \ internals/dbus-string-util.c \ + internals/dbus-sysdeps-util.c \ internals/misc-internals.c \ internals/misc-internals.h \ $(NULL) diff --git a/test/internals/dbus-sysdeps-util.c b/test/internals/dbus-sysdeps-util.c new file mode 100644 index 000000000..7e2dc064a --- /dev/null +++ b/test/internals/dbus-sysdeps-util.c @@ -0,0 +1,156 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API + * + * Copyright 2002-2008 Red Hat, Inc. + * Copyright 2003 CodeFactory AB + * Copyright 2006 Ralf Habacker + * Copyright 2006 Sjoerd Simons + * Copyright 2016-2018 Collabora Ltd. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include + +#include "misc-internals.h" + +#include "dbus/dbus-sysdeps.h" +#include "dbus/dbus-internals.h" +#include "dbus/dbus-string.h" +#include "dbus/dbus-test.h" +#include + +#include + +#ifdef DBUS_ENABLE_EMBEDDED_TESTS +static void +check_dirname (const char *filename, + const char *dirname) +{ + DBusString f, d; + + _dbus_string_init_const (&f, filename); + + if (!_dbus_string_init (&d)) + _dbus_test_fatal ("no memory"); + + if (!_dbus_string_get_dirname (&f, &d)) + _dbus_test_fatal ("no memory"); + + if (!_dbus_string_equal_c_str (&d, dirname)) + { + _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"", + filename, + _dbus_string_get_const_data (&d), + dirname); + exit (1); + } + + _dbus_string_free (&d); +} + +static void +check_path_absolute (const char *path, + dbus_bool_t expected) +{ + DBusString p; + + _dbus_string_init_const (&p, path); + + if (_dbus_path_is_absolute (&p) != expected) + { + _dbus_warn ("For path \"%s\" expected absolute = %d got %d", + path, expected, _dbus_path_is_absolute (&p)); + exit (1); + } +} + +/** + * Unit test for dbus-sysdeps.c. + * + * @returns #TRUE on success. + */ +dbus_bool_t +_dbus_sysdeps_test (const char *test_data_dir _DBUS_GNUC_UNUSED) +{ +#ifdef DBUS_WIN + check_dirname ("foo\\bar", "foo"); + check_dirname ("foo\\\\bar", "foo"); + check_dirname ("foo/\\/bar", "foo"); + check_dirname ("foo\\bar/", "foo"); + check_dirname ("foo//bar\\", "foo"); + check_dirname ("foo\\bar/", "foo"); + check_dirname ("foo/bar\\\\", "foo"); + check_dirname ("\\foo", "\\"); + check_dirname ("\\\\foo", "\\"); + check_dirname ("\\", "\\"); + check_dirname ("\\\\", "\\"); + check_dirname ("\\/", "\\"); + check_dirname ("/\\/", "/"); + check_dirname ("c:\\foo\\bar", "c:\\foo"); + check_dirname ("c:\\foo", "c:\\"); + check_dirname ("c:/foo", "c:/"); + check_dirname ("c:\\", "c:\\"); + check_dirname ("c:/", "c:/"); + check_dirname ("", "."); +#else + check_dirname ("foo", "."); + check_dirname ("foo/bar", "foo"); + check_dirname ("foo//bar", "foo"); + check_dirname ("foo///bar", "foo"); + check_dirname ("foo/bar/", "foo"); + check_dirname ("foo//bar/", "foo"); + check_dirname ("foo///bar/", "foo"); + check_dirname ("foo/bar//", "foo"); + check_dirname ("foo//bar////", "foo"); + check_dirname ("foo///bar///////", "foo"); + check_dirname ("/foo", "/"); + check_dirname ("////foo", "/"); + check_dirname ("/foo/bar", "/foo"); + check_dirname ("/foo//bar", "/foo"); + check_dirname ("/foo///bar", "/foo"); + check_dirname ("/", "/"); + check_dirname ("///", "/"); + check_dirname ("", "."); +#endif + +#ifdef DBUS_WIN + check_path_absolute ("c:/", TRUE); + check_path_absolute ("c:/foo", TRUE); + check_path_absolute ("", FALSE); + check_path_absolute ("foo", FALSE); + check_path_absolute ("foo/bar", FALSE); + check_path_absolute ("", FALSE); + check_path_absolute ("foo\\bar", FALSE); + check_path_absolute ("c:\\", TRUE); + check_path_absolute ("c:\\foo", TRUE); + check_path_absolute ("c:", TRUE); + check_path_absolute ("c:\\foo\\bar", TRUE); + check_path_absolute ("\\", TRUE); + check_path_absolute ("/", TRUE); +#else + check_path_absolute ("/", TRUE); + check_path_absolute ("/foo", TRUE); + check_path_absolute ("", FALSE); + check_path_absolute ("foo", FALSE); + check_path_absolute ("foo/bar", FALSE); +#endif + + return TRUE; +} +#endif /* DBUS_ENABLE_EMBEDDED_TESTS */ diff --git a/test/internals/misc-internals.h b/test/internals/misc-internals.h index 7b969c382..700af46e7 100644 --- a/test/internals/misc-internals.h +++ b/test/internals/misc-internals.h @@ -31,5 +31,6 @@ dbus_bool_t _dbus_credentials_test (const char *test_data_dir); dbus_bool_t _dbus_marshal_byteswap_test (const char *test_data_dir); dbus_bool_t _dbus_marshal_validate_test (const char *test_data_dir); dbus_bool_t _dbus_string_test (const char *test_data_dir); +dbus_bool_t _dbus_sysdeps_test (const char *test_data_dir); #endif -- 2.47.3