g10: Improve portability of the new test driver.

* g10/test.c: Include stdio.h and stdlib.h.
(verbose): New.
(print_results): Rename to exit_tests.
(main): Remove atexit and call exit_tests.  Set verbose.
(ASSERT, ABORT): Call exit_tests instead of exit.
--

Calling exit from an exit handler is undefined behaviour.  It works on
Linux but other systems will hit an endless loop.  That is indeed
unfortunate but we can't do anything about it.  Calling _exit() would
be possible but that may lead to other problems.  Thus we change to
call a custom exit function :-(.

Using "make check verbose=1" is supported by tests/openpgp and thus
we add the same mechanism here.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-09-10 17:43:13 +02:00
parent cafcd4336a
commit fbf24cd09a
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 28 additions and 10 deletions

View File

@ -80,8 +80,11 @@ do_test (int argc, char *argv[])
ABORT ("1E42B367 has no user id packet");
uid2 = kb2->pkt->pkt.user_id->name;
printf ("user id for DBFC6AD9: %s\n", uid1);
printf ("user id for 1E42B367: %s\n", uid2);
if (verbose)
{
printf ("user id for DBFC6AD9: %s\n", uid1);
printf ("user id for 1E42B367: %s\n", uid2);
}
TEST_P ("cache consistency", strcmp (uid1, uid2) != 0);
}

View File

@ -18,6 +18,9 @@
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "gpg.h"
/* A unit test consists of one or more tests. Tests can be broken
@ -38,6 +41,10 @@ static int tests;
/* The total number of tests that failed. */
static int tests_failed;
/* Flag to request verbose diagnostics. This is set if the envvar
"verbose" exists and is not the empty string. */
static int verbose;
#define TEST_GROUP(description) \
do { \
test_group = (description); \
@ -92,7 +99,7 @@ static int tests_failed;
int tests_failed_pre = tests_failed; \
CHECK(description, test, expected); \
if (tests_failed_pre != tests_failed) \
exit (1); \
exit_tests (1); \
} while (0)
/* Call this if something went wrong. */
@ -102,19 +109,22 @@ static int tests_failed;
if (message) \
printf (" %s\n", (message)); \
\
exit(1); \
exit_tests (1); \
} while (0)
/* You need to fill this function in. */
static void do_test (int argc, char *argv[]);
/* Print stats and call the real exit. If FORCE is set use
EXIT_FAILURE even if no test has failed. */
static void
print_results (void)
exit_tests (int force)
{
if (tests_failed == 0)
{
printf ("All %d tests passed.\n", tests);
exit (0);
exit (!!force);
}
else
{
@ -124,7 +134,6 @@ print_results (void)
printf (" (%d of %d groups)",
test_groups_failed, test_groups);
printf ("\n");
exit (1);
}
}
@ -132,10 +141,16 @@ print_results (void)
int
main (int argc, char *argv[])
{
const char *s;
(void) test_group;
do_test (argc, argv);
atexit (print_results);
s = getenv ("verbose");
if (s && *s)
verbose = 1;
return tests_failed == 0;
do_test (argc, argv);
exit_tests (0);
return !!tests_failed;
}