test-driver.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Main function for test programs.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file should be included from test cases. It will define a
  16. main function which provides the test wrapper.
  17. It assumes that the test case defines a function
  18. int do_test (void);
  19. and arranges for that function being called under the test wrapper.
  20. The do_test function should return 0 to indicate a passing test, 1
  21. to indicate a failing test, or 77 to indicate an unsupported test.
  22. Other result values could be used to indicate a failing test, but
  23. the result of the expression is passed to exit and exit only
  24. returns the lower 8 bits of its input. A non-zero return with some
  25. values could cause a test to incorrectly be considered passing when
  26. it really failed. For this reason, the function should always
  27. return 0 (EXIT_SUCCESS), 1 (EXIT_FAILURE), or 77
  28. (EXIT_UNSUPPORTED).
  29. The test function may print out diagnostic or warning messages as well
  30. as messages about failures. These messages should be printed to stdout
  31. and not stderr so that the output is properly ordered with respect to
  32. the rest of the glibc testsuite run output.
  33. Several preprocessors macros can be defined before including this
  34. file.
  35. The name of the do_test function can be changed with the
  36. TEST_FUNCTION macro. It must expand to the desired function name.
  37. If the test case needs access to command line parameters, it must
  38. define the TEST_FUNCTION_ARGV macro with the name of the test
  39. function. It must have the following type:
  40. int TEST_FUNCTION_ARGV (int argc, char **argv);
  41. This overrides the do_test default function and is incompatible
  42. with the TEST_FUNCTION macro.
  43. If PREPARE is defined, it must expand to the name of a function of
  44. the type
  45. void PREPARE (int argc, char **);
  46. This function will be called early, after parsing the command line,
  47. but before running the test, in the parent process which acts as
  48. the test supervisor.
  49. If CLEANUP_HANDLER is defined, it must expand to the name of a
  50. function of the type
  51. void CLEANUP_HANDLER (void);
  52. This function will be called from the timeout (SIGALRM) signal
  53. handler.
  54. If EXPECTED_SIGNAL is defined, it must expanded to a constant which
  55. denotes the expected signal number.
  56. If EXPECTED_STATUS is defined, it must expand to the expected exit
  57. status.
  58. If TIMEOUT is defined, it must be positive constant. It overrides
  59. the default test timeout and is measured in seconds.
  60. If TEST_NO_MALLOPT is defined, the test wrapper will not call
  61. mallopt.
  62. Custom command line handling can be implemented by defining the
  63. CMDLINE_OPTION macro (after including the <getopt.h> header; this
  64. requires _GNU_SOURCE to be defined). This macro must expand to a
  65. to a comma-separated list of braced initializers for struct option
  66. from <getopt.h>, with a trailing comma. CMDLINE_PROCESS can be
  67. defined as the name of a function which is called to process these
  68. options. The function is passed the option character/number and
  69. has this type:
  70. void CMDLINE_PROCESS (int);
  71. If the program also to process custom default short command line
  72. argument (similar to getopt) it must define CMDLINE_OPTSTRING
  73. with the expected options (for instance "vb").
  74. */
  75. #include <support/test-driver.h>
  76. #include <string.h>
  77. int
  78. main (int argc, char **argv)
  79. {
  80. struct test_config test_config;
  81. memset (&test_config, 0, sizeof (test_config));
  82. #ifdef PREPARE
  83. test_config.prepare_function = (PREPARE);
  84. #endif
  85. #if defined (TEST_FUNCTION) && defined (TEST_FUNCTON_ARGV)
  86. # error TEST_FUNCTION and TEST_FUNCTION_ARGV cannot be defined at the same time
  87. #endif
  88. #if defined (TEST_FUNCTION)
  89. test_config.test_function = TEST_FUNCTION;
  90. #elif defined (TEST_FUNCTION_ARGV)
  91. test_config.test_function_argv = TEST_FUNCTION_ARGV;
  92. #else
  93. test_config.test_function = do_test;
  94. #endif
  95. #ifdef CLEANUP_HANDLER
  96. test_config.cleanup_function = CLEANUP_HANDLER;
  97. #endif
  98. #ifdef EXPECTED_SIGNAL
  99. test_config.expected_signal = (EXPECTED_SIGNAL);
  100. #endif
  101. #ifdef EXPECTED_STATUS
  102. test_config.expected_status = (EXPECTED_STATUS);
  103. #endif
  104. #ifdef TEST_NO_MALLOPT
  105. test_config.no_mallopt = 1;
  106. #endif
  107. #ifdef TEST_NO_SETVBUF
  108. test_config.no_setvbuf = 1;
  109. #endif
  110. #ifdef TIMEOUT
  111. test_config.timeout = TIMEOUT;
  112. #endif
  113. #ifdef CMDLINE_OPTIONS
  114. struct option options[] =
  115. {
  116. CMDLINE_OPTIONS
  117. TEST_DEFAULT_OPTIONS
  118. };
  119. test_config.options = &options;
  120. #endif
  121. #ifdef CMDLINE_PROCESS
  122. test_config.cmdline_function = CMDLINE_PROCESS;
  123. #endif
  124. #ifdef CMDLINE_OPTSTRING
  125. test_config.optstring = "+" CMDLINE_OPTSTRING;
  126. #else
  127. test_config.optstring = "+";
  128. #endif
  129. return support_test_main (argc, argv, &test_config);
  130. }