diff options
author | Neal H. Walfield <neal@g10code.com> | 2015-09-02 15:07:06 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@g10code.com> | 2015-09-02 15:08:57 +0200 |
commit | ee7ec1256b24dc340656c331ef92fc59cad817b6 (patch) | |
tree | c9642ff125bbbe5b095be55aa148110dae823fb0 /g10/test.c | |
parent | g10: Make the keyblock cache per-handle rather than global. (diff) | |
download | gnupg2-ee7ec1256b24dc340656c331ef92fc59cad817b6.tar.xz gnupg2-ee7ec1256b24dc340656c331ef92fc59cad817b6.zip |
g10: Add test for keydb as well as new testing infrastructure.
* g10/Makefile.am (EXTRA_DIST): Add test.c.
(AM_CPPFLAGS): Add -DSOURCE_DIR="\"$(srcdir)\"".
(module_tests): Add t-keydb.
(t_keydb_SOURCES): New variable.
(t_keydb_LDADD): Likewise.
* g10/t-keydb.c: New file.
* g10/t-keydb-keyring.kbx: New file.
* g10/test-stubs.c: New file.
* g10/test.c: New file.
--
Signed-off-by: Neal H. Walfield <neal@g10code.com>.
Diffstat (limited to 'g10/test.c')
-rw-r--r-- | g10/test.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/g10/test.c b/g10/test.c new file mode 100644 index 000000000..6910f95ad --- /dev/null +++ b/g10/test.c @@ -0,0 +1,141 @@ +/* test.c - Infrastructure for unit tests. + * Copyright (C) 2015 g10 Code GmbH + * + * This file is part of GnuPG. + * + * GnuPG 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 3 of the License, or + * (at your option) any later version. + * + * GnuPG 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, see <http://www.gnu.org/licenses/>. + */ + +#include <config.h> +#include "gpg.h" + +/* A unit test consists of one or more tests. Tests can be broken + into groups and each group can consist of one or more tests. */ + +/* The number of test groups. */ +static int test_groups; +/* The current test group. */ +static char *test_group; + +/* Whether there was already a failure in the current test group. */ +static int current_test_group_failed; +/* The number of test groups with a failure. */ +static int test_groups_failed; + +/* The total number of tests. */ +static int tests; +/* The total number of tests that failed. */ +static int tests_failed; + +#define TEST_GROUP(description) \ + do { \ + test_group = (description); \ + test_groups ++; \ + current_test_group_failed = 0; \ + } while (0) + +#define STRINGIFY2(x) #x +#define STRINGIFY(x) STRINGIFY2(x) + +/* Execute a test. */ +#define TEST(description, test, expected) \ + do { \ + int test_result; \ + int expected_result; \ + \ + tests ++; \ + \ + printf ("%d. Checking %s...", \ + tests, (description) ?: ""); \ + fflush (stdout); \ + \ + test_result = (test); \ + expected_result = (expected); \ + \ + if (test_result == expected_result) \ + { \ + printf (" ok.\n"); \ + } \ + else \ + { \ + printf (" failed.\n"); \ + printf (" %s == %s failed.\n", \ + STRINGIFY(test), \ + STRINGIFY(expected)); \ + tests_failed ++; \ + if (! current_test_group_failed) \ + { \ + current_test_group_failed = 1; \ + test_groups_failed ++; \ + } \ + } \ + } while (0) + +/* Test that a condition evaluates to true. */ +#define TEST_P(description, test) \ + TEST(description, !!(test), 1) + +/* Like CHECK, but if the test fails, abort the program. */ +#define ASSERT(description, test, expected) \ + do { \ + int tests_failed_pre = tests_failed; \ + CHECK(description, test, expected); \ + if (tests_failed_pre != tests_failed) \ + exit (1); \ + } while (0) + +/* Call this if something went wrong. */ +#define ABORT(message) \ + do { \ + printf ("aborting..."); \ + if (message) \ + printf (" %s\n", (message)); \ + \ + exit(1); \ + } while (0) + +/* You need to fill this function in. */ +static void do_test (int argc, char *argv[]); + +static void +print_results (void) +{ + if (tests_failed == 0) + { + printf ("All %d tests passed.\n", tests); + exit (0); + } + else + { + printf ("%d of %d tests failed", + tests_failed, tests); + if (test_groups > 1) + printf (" (%d of %d groups)", + test_groups_failed, test_groups); + printf ("\n"); + + exit (1); + } +} + +int +main (int argc, char *argv[]) +{ + (void) test_group; + + do_test (argc, argv); + atexit (print_results); + + return tests_failed == 0; +} |