about summary refs log tree commit diff
path: root/support/test-driver.c
blob: 9798f16227b9d46708c9800ab837925b36515846 (plain) (blame)
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
/* Main function for test programs.
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

/* This file should be included from test cases.  It will define a
   main function which provides the test wrapper.

   It assumes that the test case defines a function

     int do_test (void);

   and arranges for that function being called under the test wrapper.
   The do_test function should return 0 to indicate a passing test, 1
   to indicate a failing test, or 77 to indicate an unsupported test.
   Other result values could be used to indicate a failing test, but
   the result of the expression is passed to exit and exit only
   returns the lower 8 bits of its input.  A non-zero return with some
   values could cause a test to incorrectly be considered passing when
   it really failed.  For this reason, the function should always
   return 0 (EXIT_SUCCESS), 1 (EXIT_FAILURE), or 77
   (EXIT_UNSUPPORTED).

   The test function may print out diagnostic or warning messages as well
   as messages about failures.  These messages should be printed to stdout
   and not stderr so that the output is properly ordered with respect to
   the rest of the glibc testsuite run output.

   Several preprocessors macros can be defined before including this
   file.

   The name of the do_test function can be changed with the
   TEST_FUNCTION macro.  It must expand to the desired function name.

   If the test case needs access to command line parameters, it must
   define the TEST_FUNCTION_ARGV macro with the name of the test
   function.  It must have the following type:

     int TEST_FUNCTION_ARGV (int argc, char **argv);

   This overrides the do_test default function and is incompatible
   with the TEST_FUNCTION macro.

   If PREPARE is defined, it must expand to the name of a function of
   the type

     void PREPARE (int argc, char **);

   This function will be called early, after parsing the command line,
   but before running the test, in the parent process which acts as
   the test supervisor.

   If CLEANUP_HANDLER is defined, it must expand to the name of a
   function of the type

     void CLEANUP_HANDLER (void);

   This function will be called from the timeout (SIGALRM) signal
   handler.

   If EXPECTED_SIGNAL is defined, it must expanded to a constant which
   denotes the expected signal number.

   If EXPECTED_STATUS is defined, it must expand to the expected exit
   status.

   If TIMEOUT is defined, it must be positive constant.  It overrides
   the default test timeout and is measured in seconds.

   If TEST_NO_MALLOPT is defined, the test wrapper will not call
   mallopt.

   Custom command line handling can be implemented by defining the
   CMDLINE_OPTION macro (after including the <getopt.h> header; this
   requires _GNU_SOURCE to be defined).  This macro must expand to a
   to a comma-separated list of braced initializers for struct option
   from <getopt.h>, with a trailing comma.  CMDLINE_PROCESS can be
   defined as the name of a function which is called to process these
   options.  The function is passed the option character/number and
   has this type:

     void CMDLINE_PROCESS (int);

   If the program also to process custom default short command line
   argument (similar to getopt) it must define CMDLINE_OPTSTRING
   with the expected options (for instance "vb").
*/

#include <support/test-driver.h>

#include <string.h>

int
main (int argc, char **argv)
{
  struct test_config test_config;
  memset (&test_config, 0, sizeof (test_config));

#ifdef PREPARE
  test_config.prepare_function = (PREPARE);
#endif

#if defined (TEST_FUNCTION) && defined (TEST_FUNCTON_ARGV)
# error TEST_FUNCTION and TEST_FUNCTION_ARGV cannot be defined at the same time
#endif
#if defined (TEST_FUNCTION)
  test_config.test_function = TEST_FUNCTION;
#elif defined (TEST_FUNCTION_ARGV)
  test_config.test_function_argv = TEST_FUNCTION_ARGV;
#else
  test_config.test_function = do_test;
#endif

#ifdef CLEANUP_HANDLER
  test_config.cleanup_function = CLEANUP_HANDLER;
#endif

#ifdef EXPECTED_SIGNAL
  test_config.expected_signal = (EXPECTED_SIGNAL);
#endif

#ifdef EXPECTED_STATUS
  test_config.expected_status = (EXPECTED_STATUS);
#endif

#ifdef TEST_NO_MALLOPT
  test_config.no_mallopt = 1;
#endif

#ifdef TEST_NO_SETVBUF
  test_config.no_setvbuf = 1;
#endif

#ifdef TIMEOUT
  test_config.timeout = TIMEOUT;
#endif

#ifdef CMDLINE_OPTIONS
  struct option options[] =
    {
      CMDLINE_OPTIONS
      TEST_DEFAULT_OPTIONS
    };
  test_config.options = &options;
#endif
#ifdef CMDLINE_PROCESS
  test_config.cmdline_function = CMDLINE_PROCESS;
#endif
#ifdef CMDLINE_OPTSTRING
  test_config.optstring = "+" CMDLINE_OPTSTRING;
#else
  test_config.optstring = "+";
#endif

  return support_test_main (argc, argv, &test_config);
}