Commit 295e5a19 authored by Simon McVittie's avatar Simon McVittie

tests: Collect common code from embedded-test main executables

This all seems to have been written by Red Hat or by Collabora, so we
have permission to relicense it under the MIT/X11 license, and we
might as well do so.
Signed-off-by: Simon McVittie's avatarSimon McVittie <smcv@collabora.com>
parent 9c4f9206
......@@ -25,11 +25,9 @@
#include "test.h"
#include "activation-helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-misc.h>
#include <dbus/dbus-test-tap.h>
#include <dbus/dbus-test-wrappers.h>
#if !defined(DBUS_ENABLE_EMBEDDED_TESTS) || !defined(DBUS_UNIX)
#error This file is only relevant for the embedded tests on Unix
......@@ -72,24 +70,13 @@ bus_activation_helper_oom_test (void *data,
#endif
int
main (int argc, char **argv)
static dbus_bool_t
bus_activation_helper_test (const char *test_data_dir)
{
const char *dir;
DBusString config_file;
if (argc > 1 && strcmp (argv[1], "--tap") != 0)
dir = argv[1];
else
dir = _dbus_getenv ("DBUS_TEST_DATA");
if (dir == NULL)
_dbus_test_fatal ("Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable");
_dbus_test_diag ("%s: Running launch helper OOM checks", argv[0]);
if (!_dbus_string_init (&config_file) ||
!_dbus_string_append (&config_file, dir) ||
!_dbus_string_append (&config_file, test_data_dir) ||
!_dbus_string_append (&config_file, "/valid-config-files-system/debug-allow-all-pass.conf"))
_dbus_test_fatal ("OOM during initialization");
......@@ -105,9 +92,20 @@ main (int argc, char **argv)
_dbus_test_fatal ("OOM test failed");
/* ... otherwise it must have passed */
_dbus_test_ok ("%s", argv[0]);
return TRUE;
}
_dbus_test_check_memleaks (argv[0]);
static DBusTestCase test =
{
"activation-helper",
bus_activation_helper_test,
};
return _dbus_test_done_testing ();
int
main (int argc, char **argv)
{
return _dbus_test_main (argc, argv, 1, &test,
(DBUS_TEST_FLAGS_REQUIRE_DATA |
DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS),
NULL, NULL);
}
......@@ -22,105 +22,48 @@
*/
#include <config.h>
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-string.h>
#include <dbus/dbus-sysdeps.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-message-internal.h>
#include <dbus/dbus-test-tap.h>
#include <dbus/dbus-test-wrappers.h>
#include "selinux.h"
#ifndef DBUS_ENABLE_EMBEDDED_TESTS
#error This file is only relevant for the embedded tests
#endif
#ifdef DBUS_UNIX
# include <dbus/dbus-sysdeps-unix.h>
#endif
static const char *only;
static DBusInitialFDs *initial_fds = NULL;
static void
test_pre_hook (void)
{
initial_fds = _dbus_check_fdleaks_enter ();
}
static void
test_post_hook (const char *name)
test_post_hook (void)
{
if (_dbus_getenv ("DBUS_TEST_SELINUX"))
bus_selinux_shutdown ();
_dbus_test_check_memleaks (name);
_dbus_check_fdleaks_leave (initial_fds);
initial_fds = NULL;
}
static void
test_one (const char *name,
dbus_bool_t (*func) (const char *),
const char *test_data_dir)
static DBusTestCase tests[] =
{
long before, after;
if (only != NULL && strcmp (only, name) != 0)
{
_dbus_test_skip ("%s - Only intending to run %s", name, only);
return;
}
_dbus_test_diag ("Running test: %s", name);
_dbus_get_monotonic_time (&before, NULL);
test_pre_hook ();
if (func (test_data_dir))
_dbus_test_ok ("%s", name);
else
_dbus_test_not_ok ("%s", name);
_dbus_get_monotonic_time (&after, NULL);
_dbus_test_diag ("%s test took %ld seconds", name, after - before);
test_post_hook (name);
}
{ "expire-list", bus_expire_list_test },
{ "config-parser", bus_config_parser_test },
{ "signals", bus_signals_test },
{ "dispatch-sha1", bus_dispatch_sha1_test },
{ "dispatch", bus_dispatch_test },
{ "activation-service-reload", bus_activation_service_reload_test },
{ "unix-fds-passing", bus_unix_fds_passing_test },
{ NULL }
};
int
main (int argc, char **argv)
{
const char *dir;
if (argc > 1 && strcmp (argv[1], "--tap") != 0)
dir = argv[1];
else
dir = _dbus_getenv ("DBUS_TEST_DATA");
if (argc > 2)
only = argv[2];
else
only = _dbus_getenv ("DBUS_TEST_ONLY");
if (dir == NULL)
_dbus_test_fatal ("Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable");
#ifdef DBUS_UNIX
/* close any inherited fds so dbus-spawn's check for close-on-exec works */
_dbus_close_all ();
#endif
test_one ("expire-list", bus_expire_list_test, dir);
test_one ("config-parser", bus_config_parser_test, dir);
test_one ("signals", bus_signals_test, dir);
test_one ("dispatch-sha1", bus_dispatch_sha1_test, dir);
test_one ("dispatch", bus_dispatch_test, dir);
test_one ("activation-service-reload",
bus_activation_service_reload_test, dir);
test_one ("unix-fds-passing", bus_unix_fds_passing_test, dir);
return _dbus_test_done_testing ();
return _dbus_test_main (argc, argv, _DBUS_N_ELEMENTS (tests), tests,
(DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS |
DBUS_TEST_FLAGS_CHECK_FD_LEAKS |
DBUS_TEST_FLAGS_REQUIRE_DATA),
test_pre_hook, test_post_hook);
}
......@@ -22,53 +22,27 @@
*/
#include <config.h>
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-string.h>
#include <dbus/dbus-sysdeps.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-test-tap.h>
#include <dbus/dbus-test-wrappers.h>
#if !defined(DBUS_ENABLE_EMBEDDED_TESTS) || !defined(DBUS_UNIX)
#error This file is only relevant for the embedded tests on Unix
#endif
static void
test_pre_hook (void)
{
}
static const char *progname = "";
static void
test_post_hook (void)
static DBusTestCase test =
{
_dbus_test_check_memleaks (progname);
}
"config-parser-trivial",
bus_config_parser_trivial_test
};
int
main (int argc, char **argv)
{
const char *dir;
progname = argv[0];
if (argc > 1 && strcmp (argv[1], "--tap") != 0)
dir = argv[1];
else
dir = _dbus_getenv ("DBUS_TEST_DATA");
if (dir == NULL)
_dbus_test_fatal ("Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable");
test_pre_hook ();
_dbus_test_diag ("%s: Running config file parser (trivial) test", argv[0]);
if (!bus_config_parser_trivial_test (dir))
_dbus_test_fatal ("OOM creating parser");
/* All failure modes for this test are currently fatal */
_dbus_test_ok ("%s", argv[0]);
test_post_hook ();
return _dbus_test_done_testing ();
return _dbus_test_main (argc, argv, 1, &test,
(DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS |
DBUS_TEST_FLAGS_REQUIRE_DATA),
NULL, NULL);
}
......@@ -171,8 +171,8 @@ set (DBUS_UTIL_SOURCES
if (DBUS_ENABLE_EMBEDDED_TESTS)
set (DBUS_UTIL_SOURCES
${DBUS_UTIL_SOURCES}
${DBUS_DIR}/dbus-test.c
${DBUS_DIR}/dbus-spawn-test.c
${DBUS_DIR}/dbus-test-wrappers.c
)
endif (DBUS_ENABLE_EMBEDDED_TESTS)
......@@ -185,6 +185,7 @@ set (DBUS_UTIL_HEADERS
${DBUS_DIR}/dbus-socket-set.h
${DBUS_DIR}/dbus-spawn.h
${DBUS_DIR}/dbus-test.h
${DBUS_DIR}/dbus-test-wrappers.h
)
### platform specific settings
......
......@@ -266,11 +266,14 @@ DBUS_UTIL_SOURCES= \
dbus-spawn.h \
dbus-string-util.c \
dbus-sysdeps-util.c \
dbus-test.c \
dbus-test.h
if DBUS_ENABLE_EMBEDDED_TESTS
DBUS_UTIL_SOURCES += dbus-spawn-test.c
DBUS_UTIL_SOURCES += \
dbus-spawn-test.c \
dbus-test-wrappers.c \
dbus-test-wrappers.h \
$(NULL)
endif
libdbus_1_la_SOURCES= \
......
......@@ -21,48 +21,53 @@
*
*/
#include <config.h>
#include "dbus-types.h"
#include "dbus-test.h"
#include "dbus-test-tap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef DBUS_UNIX
# include <dbus/dbus-sysdeps-unix.h>
#endif
#include "dbus-internals.h"
#include "dbus-test.h"
#include "dbus-test-wrappers.h"
int
main (int argc,
char **argv)
static DBusTestCase tests[] =
{
const char *test_data_dir;
const char *specific_test;
{ "string", _dbus_string_test },
{ "sysdeps", _dbus_sysdeps_test },
{ "data-slot", _dbus_data_slot_test },
{ "misc", _dbus_misc_test },
{ "address", _dbus_address_test },
{ "server", _dbus_server_test },
{ "object-tree", _dbus_object_tree_test },
{ "signature", _dbus_signature_test },
{ "marshalling", _dbus_marshal_test },
{ "marshal-recursive" , _dbus_marshal_recursive_test },
{ "byteswap", _dbus_marshal_byteswap_test },
{ "memory", _dbus_memory_test },
{ "mem-pool", _dbus_mem_pool_test },
{ "list", _dbus_list_test },
{ "marshal-validate", _dbus_marshal_validate_test },
{ "message", _dbus_message_test },
{ "hash", _dbus_hash_test },
{ "credentials", _dbus_credentials_test },
{ "keyring", _dbus_keyring_test },
{ "sha", _dbus_sha_test },
{ "auth", _dbus_auth_test },
#ifdef DBUS_UNIX
/* close any inherited fds so dbus-spawn's check for close-on-exec works */
_dbus_close_all ();
#if defined(DBUS_UNIX)
{ "userdb", _dbus_userdb_test },
{ "transport-unix", _dbus_transport_unix_test },
#endif
#if HAVE_SETLOCALE
setlocale(LC_ALL, "");
#if !defined(DBUS_WINCE)
{ "spawn", _dbus_spawn_test },
#endif
if (argc > 1 && strcmp (argv[1], "--tap") != 0)
test_data_dir = argv[1];
else
test_data_dir = NULL;
if (argc > 2)
specific_test = argv[2];
else
specific_test = _dbus_getenv ("DBUS_TEST_ONLY");
{ NULL }
};
_dbus_run_tests (test_data_dir, specific_test);
return _dbus_test_done_testing ();
int
main (int argc,
char **argv)
{
return _dbus_test_main (argc, argv, _DBUS_N_ELEMENTS (tests), tests,
DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS,
NULL, NULL);
}
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* Copyright 2002-2009 Red Hat Inc.
* Copyright 2011-2018 Collabora Ltd.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <config.h>
#include "dbus/dbus-test-wrappers.h"
#ifndef DBUS_ENABLE_EMBEDDED_TESTS
#error This file is only relevant for the embedded tests
#endif
#include "dbus/dbus-message-internal.h"
#include "dbus/dbus-test-tap.h"
#if HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef DBUS_UNIX
# include <dbus/dbus-sysdeps-unix.h>
#endif
/*
* _dbus_test_main:
* @argc: number of command-line arguments
* @argv: array of @argc arguments
* @n_tests: length of @tests
* @tests: array of @n_tests tests
* @flags: flags affecting all tests
* @test_pre_hook: if not %NULL, called before each test
* @test_post_hook: if not %NULL, called after each test
*
* Wrapper for dbus tests that do not use GLib. Processing of @tests
* can be terminated early by an entry with @name = NULL, which is a
* convenient way to put a trailing comma on every "real" test entry
* without breaking compilation on pedantic C compilers.
*/
int
_dbus_test_main (int argc,
char **argv,
size_t n_tests,
const DBusTestCase *tests,
DBusTestFlags flags,
void (*test_pre_hook) (void),
void (*test_post_hook) (void))
{
const char *test_data_dir;
const char *specific_test;
size_t i;
#ifdef DBUS_UNIX
/* close any inherited fds so dbus-spawn's check for close-on-exec works */
_dbus_close_all ();
#endif
#if HAVE_SETLOCALE
setlocale(LC_ALL, "");
#endif
if (argc > 1 && strcmp (argv[1], "--tap") != 0)
test_data_dir = argv[1];
else
test_data_dir = _dbus_getenv ("DBUS_TEST_DATA");
if (test_data_dir != NULL)
_dbus_test_diag ("Test data in %s", test_data_dir);
else if (flags & DBUS_TEST_FLAGS_REQUIRE_DATA)
_dbus_test_fatal ("Must specify test data directory as argv[1] or "
"in DBUS_TEST_DATA environment variable");
else
_dbus_test_diag ("No test data!");
if (argc > 2)
specific_test = argv[2];
else
specific_test = _dbus_getenv ("DBUS_TEST_ONLY");
for (i = 0; i < n_tests; i++)
{
long before, after;
DBusInitialFDs *initial_fds = NULL;
if (tests[i].name == NULL)
break;
if (n_tests > 1 &&
specific_test != NULL &&
strcmp (specific_test, tests[i].name) != 0)
{
_dbus_test_skip ("%s - Only intending to run %s",
tests[i].name, specific_test);
continue;
}
_dbus_test_diag ("Running test: %s", tests[i].name);
_dbus_get_monotonic_time (&before, NULL);
if (test_pre_hook)
test_pre_hook ();
if (flags & DBUS_TEST_FLAGS_CHECK_FD_LEAKS)
initial_fds = _dbus_check_fdleaks_enter ();
if (tests[i].func (test_data_dir))
_dbus_test_ok ("%s", tests[i].name);
else
_dbus_test_not_ok ("%s", tests[i].name);
_dbus_get_monotonic_time (&after, NULL);
_dbus_test_diag ("%s test took %ld seconds",
tests[i].name, after - before);
if (test_post_hook)
test_post_hook ();
if (flags & DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS)
_dbus_test_check_memleaks (tests[i].name);
if (flags & DBUS_TEST_FLAGS_CHECK_FD_LEAKS)
_dbus_check_fdleaks_leave (initial_fds);
}
return _dbus_test_done_testing ();
}
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* Copyright © 2017 Collabora Ltd.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef DBUS_TEST_WRAPPERS_H
#define DBUS_TEST_WRAPPERS_H
#include <dbus/dbus-types.h>
typedef struct
{
const char *name;
dbus_bool_t (*func) (const char *test_data_dir);
} DBusTestCase;
typedef enum
{
DBUS_TEST_FLAGS_REQUIRE_DATA = (1 << 0),
DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS = (1 << 1),
DBUS_TEST_FLAGS_CHECK_FD_LEAKS = (1 << 2),
DBUS_TEST_FLAGS_NONE = 0
} DBusTestFlags;
int _dbus_test_main (int argc,
char **argv,
size_t n_tests,
const DBusTestCase *tests,
DBusTestFlags flags,
void (*test_pre_hook) (void),
void (*test_post_hook) (void));
#endif
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-test.c Program to run all tests
*
* Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
*
* 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 <config.h>
#include "dbus-test.h"
#include "dbus-sysdeps.h"
#include "dbus-internals.h"
#include <dbus/dbus-test-tap.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
typedef dbus_bool_t (*TestDataFunc)(const char *data);
static void
run_data_test (const char *test_name,
const char *specific_test,
TestDataFunc test,
const char *test_data_dir)
{
long before, after;
if (specific_test != NULL && strcmp (specific_test, test_name) != 0)
{
_dbus_test_skip ("%s - Only intending to run %s", test_name, specific_test);
return;
}
_dbus_test_diag ("%s: running %s tests", "test-dbus", test_name);
_dbus_get_monotonic_time (&before, NULL);
if (test (test_data_dir))
_dbus_test_ok ("%s", test_name);
else
_dbus_test_not_ok ("%s", test_name);
_dbus_get_monotonic_time (&after, NULL);
_dbus_test_diag ("%s: %s test took %ld seconds",
"test-dbus", test_name, after - before);
_dbus_test_check_memleaks (test_name);
}
/**
* An exported symbol to be run in order to execute
* unit tests. Should not be used by
* any app other than our test app, this symbol
* won't exist in some builds of the library.
* (with --enable-tests=no)
*
* @param test_data_dir the directory with test data (test/data normally)
* @param specific_test run specific test or #NULL to run all tests
*/
void
_dbus_run_tests (const char *test_data_dir,
const char *specific_test)
{
if (test_data_dir == NULL)
test_data_dir = _dbus_getenv ("DBUS_TEST_DATA");
if (test_data_dir != NULL)
_dbus_test_diag ("Test data in %s", test_data_dir);
else
_dbus_test_diag ("No test data!");
run_data_test ("string", specific_test, _dbus_string_test,
test_data_dir);
run_data_test ("sysdeps", specific_test, _dbus_sysdeps_test,
test_data_dir);
run_data_test ("data-slot", specific_test, _dbus_data_slot_test,
test_data_dir);
run_data_test ("misc", specific_test, _dbus_misc_test, test_data_dir);
run_data_test ("address", specific_test, _dbus_address_test,
test_data_dir);
run_data_test ("server", specific_test, _dbus_server_test,
test_data_dir);
run_data_test ("object-tree", specific_test, _dbus_object_tree_test,
test_data_dir);
run_data_test ("signature", specific_test, _dbus_signature_test,
test_data_dir);
run_data_test ("marshalling", specific_test, _dbus_marshal_test,
test_data_dir);
run_data_test ("marshal-recursive", specific_test,
_dbus_marshal_recursive_test, test_data_dir);
run_data_test ("byteswap", specific_test, _dbus_marshal_byteswap_test,
test_data_dir);
run_data_test ("memory", specific_test, _dbus_memory_test,
test_data_dir);
run_data_test ("mem-pool", specific_test, _dbus_mem_pool_test,
test_data_dir);
run_data_test ("list", specific_test, _dbus_list_test, test_data_dir);
run_data_test ("marshal-validate", specific_test,
_dbus_marshal_validate_test, test_data_dir);
run_data_test ("message", specific_test, _dbus_message_test, test_data_dir);
run_data_test ("hash", specific_test, _dbus_hash_test, test_data_dir);
#if !defined(DBUS_WINCE)
run_data_test ("spawn", specific_test, _dbus_spawn_test, test_data_dir);
#endif
run_data_test ("credentials", specific_test, _dbus_credentials_test, test_data_dir);
#ifdef DBUS_UNIX
run_data_test ("userdb", specific_test, _dbus_userdb_test, test_data_dir);
run_data_test ("transport-unix", specific_test,
_dbus_transport_unix_test, test_data_dir);
#endif
run_data_test ("keyring", specific_test, _dbus_keyring_test,
test_data_dir);
run_data_test ("sha", specific_test, _dbus_sha_test, test_data_dir);
run_data_test ("auth", specific_test, _dbus_auth_test, test_data_dir);
}
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
......@@ -96,9 +96,6 @@ dbus_bool_t _dbus_object_tree_test (const char *test_data_dir);
dbus_bool_t _dbus_credentials_test (const char *test_data_dir);
void _dbus_run_tests (const char *test_data_dir,
const char *specific_test);
dbus_bool_t _dbus_test_generate_bodies (int sequence,
int byte_order,
DBusString *signature,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment