summaryrefslogtreecommitdiffstats
path: root/g10/test.c
blob: 734458af3cff3252d418ae1c953db38c04c2f512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* 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 <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 ++;					\
    if (verbose)                                \
      {                                         \
         printf ("%d. Checking %s...",		\
	        tests, (description) ?: "");	\
         fflush (stdout);			\
      }                                         \
    test_result = (test);			\
    expected_result = (expected);		\
						\
    if (test_result == expected_result)		\
      {						\
        if (verbose) printf (" ok.\n");         \
      }						\
    else					\
      {						\
        if (!verbose)                           \
          printf ("%d. Checking %s...",         \
                  tests, (description) ?: "");  \
	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_tests (1);					\
  } while (0)

/* Call this if something went wrong.  */
#define ABORT(message)				\
  do {						\
    printf ("aborting...");			\
    if (message)				\
      printf (" %s\n", (message));		\
						\
    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
exit_tests (int force)
{
  if (tests_failed == 0)
    {
      if (verbose)
        printf ("All %d tests passed.\n", tests);
      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);
    }
}


/* Prepend FNAME with the srcdir environment variable's value and
   return a malloced filename.  Caller must release the returned
   string using test_free.  */
char *
prepend_srcdir (const char *fname)
{
  static const char *srcdir;
  char *result;

  if (!srcdir && !(srcdir = getenv ("srcdir")))
    srcdir = ".";

  result = malloc (strlen (srcdir) + 1 + strlen (fname) + 1);
  strcpy (result, srcdir);
  strcat (result, "/");
  strcat (result, fname);
  return result;
}


void
test_free (void *a)
{
  if (a)
    free (a);
}


int
main (int argc, char *argv[])
{
  const char *s;

  (void) test_group;

  s = getenv ("verbose");
  if (s && *s)
    verbose = 1;

  do_test (argc, argv);
  exit_tests (0);

  return !!tests_failed;
}