From 5ffb709b42783b0d13a49b8c9a84c75f556c88a2 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 14 Nov 2017 14:01:56 +0000 Subject: Add utility functions to emit TAP diagnostics and fatal errors Reviewed-by: Philip Withnall [smcv: Add an explanatory comment as suggested] Signed-off-by: Simon McVittie Bug: https://bugs.freedesktop.org/show_bug.cgi?id=103601 --- cmake/dbus/CMakeLists.txt | 2 ++ dbus/Makefile.am | 2 ++ dbus/dbus-test-tap.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-test-tap.h | 44 +++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 dbus/dbus-test-tap.c create mode 100644 dbus/dbus-test-tap.h diff --git a/cmake/dbus/CMakeLists.txt b/cmake/dbus/CMakeLists.txt index 8a01d918b..2fdd11282 100644 --- a/cmake/dbus/CMakeLists.txt +++ b/cmake/dbus/CMakeLists.txt @@ -127,6 +127,7 @@ set (DBUS_SHARED_SOURCES ${DBUS_DIR}/dbus-string.c ${DBUS_DIR}/dbus-sysdeps.c ${DBUS_DIR}/dbus-pipe.c + ${DBUS_DIR}/dbus-test-tap.c ) set (DBUS_SHARED_HEADERS @@ -141,6 +142,7 @@ set (DBUS_SHARED_HEADERS ${DBUS_DIR}/dbus-string-private.h ${DBUS_DIR}/dbus-pipe.h ${DBUS_DIR}/dbus-sysdeps.h + ${DBUS_DIR}/dbus-test-tap.h ) ### source code that is generic utility functionality used diff --git a/dbus/Makefile.am b/dbus/Makefile.am index b2913ef0e..d4fe09f89 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -231,6 +231,8 @@ DBUS_SHARED_SOURCES= \ $(DBUS_SHARED_arch_sources) \ dbus-sysdeps.c \ dbus-sysdeps.h \ + dbus-test-tap.c \ + dbus-test-tap.h \ dbus-valgrind-internal.h ### source code that is generic utility functionality used diff --git a/dbus/dbus-test-tap.c b/dbus/dbus-test-tap.c new file mode 100644 index 000000000..a6f99b547 --- /dev/null +++ b/dbus/dbus-test-tap.c @@ -0,0 +1,77 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-test-tap — TAP helpers for "embedded tests" + * + * Copyright © 2017 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 +#include "dbus/dbus-test-tap.h" + +/* + * TAP, the Test Anything Protocol, is a text-based syntax for test-cases + * to report results to test harnesses. + * + * See for details of the syntax, which + * will not be explained here. + */ + +#ifdef DBUS_ENABLE_EMBEDDED_TESTS + +#include +#include + +/* + * Output TAP indicating a fatal error, and exit unsuccessfully. + */ +void +_dbus_test_fatal (const char *format, + ...) +{ + va_list ap; + + printf ("Bail out! "); + va_start (ap, format); + vprintf (format, ap); + va_end (ap); + printf ("\n"); + fflush (stdout); + exit (1); +} + +/* + * Output TAP indicating a diagnostic (informational message). + */ +void +_dbus_test_diag (const char *format, + ...) +{ + va_list ap; + + printf ("# "); + va_start (ap, format); + vprintf (format, ap); + va_end (ap); + printf ("\n"); +} + +#endif diff --git a/dbus/dbus-test-tap.h b/dbus/dbus-test-tap.h new file mode 100644 index 000000000..706475bdd --- /dev/null +++ b/dbus/dbus-test-tap.h @@ -0,0 +1,44 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-test-tap — TAP helpers for "embedded tests" + * + * Copyright © 2017 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. + */ + +#ifndef DBUS_TEST_TAP_H +#define DBUS_TEST_TAP_H + +#include + +#ifdef DBUS_ENABLE_EMBEDDED_TESTS + +DBUS_PRIVATE_EXPORT +void _dbus_test_fatal (const char *format, + ...) _DBUS_GNUC_NORETURN _DBUS_GNUC_PRINTF (1, 2); + +DBUS_PRIVATE_EXPORT +void _dbus_test_diag (const char *format, + ...) _DBUS_GNUC_PRINTF (1, 2); + +#endif + +#endif -- cgit v1.2.3