Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F36623858
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
6 KB
Subscribers
None
View Options
diff --git a/g10/t-keydb.c b/g10/t-keydb.c
index 0f176431c..634cb05a7 100644
--- a/g10/t-keydb.c
+++ b/g10/t-keydb.c
@@ -1,87 +1,90 @@
/* t-keydb.c - Tests for keydb.c.
* 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 "test.c"
#include "keydb.h"
static void
do_test (int argc, char *argv[])
{
int rc;
KEYDB_HANDLE hd1, hd2;
KEYDB_SEARCH_DESC desc1, desc2;
KBNODE kb1, kb2;
char *uid1;
char *uid2;
(void) argc;
(void) argv;
rc = keydb_add_resource (SOURCE_DIR "/t-keydb-keyring.kbx", 0);
if (rc)
ABORT ("Failed to open keyring.");
hd1 = keydb_new ();
hd2 = keydb_new ();
rc = classify_user_id ("2689 5E25 E844 6D44 A26D 8FAF 2F79 98F3 DBFC 6AD9",
&desc1, 0);
if (rc)
ABORT ("Failed to convert fingerprint for DBFC6AD9");
rc = keydb_search (hd1, &desc1, 1, NULL);
if (rc)
ABORT ("Failed to lookup key associated with DBFC6AD9");
classify_user_id ("8061 5870 F5BA D690 3336 86D0 F2AD 85AC 1E42 B367",
&desc2, 0);
if (rc)
ABORT ("Failed to convert fingerprint for 1E42B367");
rc = keydb_search (hd2, &desc2, 1, NULL);
if (rc)
ABORT ("Failed to lookup key associated with 1E42B367");
rc = keydb_get_keyblock (hd2, &kb2);
if (rc)
ABORT ("Failed to get keyblock for 1E42B367");
rc = keydb_get_keyblock (hd1, &kb1);
if (rc)
ABORT ("Failed to get keyblock for DBFC6AD9");
while (kb1 && kb1->pkt->pkttype != PKT_USER_ID)
kb1 = kb1->next;
if (! kb1)
ABORT ("DBFC6AD9 has no user id packet");
uid1 = kb1->pkt->pkt.user_id->name;
while (kb2 && kb2->pkt->pkttype != PKT_USER_ID)
kb2 = kb2->next;
if (! kb2)
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);
}
diff --git a/g10/test.c b/g10/test.c
index 6910f95ad..e9e6b2342 100644
--- a/g10/test.c
+++ b/g10/test.c
@@ -1,141 +1,156 @@
/* 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 <stdio.h>
+#include <stdlib.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;
+/* 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); \
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); \
+ exit_tests (1); \
} while (0)
/* Call this if something went wrong. */
#define ABORT(message) \
do { \
printf ("aborting..."); \
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
{
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[])
{
+ const char *s;
+
(void) test_group;
+ s = getenv ("verbose");
+ if (s && *s)
+ verbose = 1;
+
do_test (argc, argv);
- atexit (print_results);
+ exit_tests (0);
- return tests_failed == 0;
+ return !!tests_failed;
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Feb 26, 7:01 PM (19 h, 10 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
1d/63/3ca8397caa92c34fa449037f7c0f
Attached To
rG GnuPG
Event Timeline
Log In to Comment