main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright (c) 2015-2018 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #if defined(_MSC_VER)
  22. #define _CRT_SECURE_NO_WARNINGS /* Microsoft nonsense */
  23. #endif
  24. #include "civetweb_check.h"
  25. #include "shared.h"
  26. #include "public_func.h"
  27. #include "public_server.h"
  28. #include "private.h"
  29. #include "timertest.h"
  30. #include "private_exe.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. /* This unit test file uses the excellent Check unit testing library.
  35. * The API documentation is available here:
  36. * http://check.sourceforge.net/doc/check_html/index.html
  37. *
  38. * Note: CivetWeb is tested using it's own fork of check:
  39. * https://github.com/civetweb/check
  40. * Required fixes from this fork are already available
  41. * in the main repository:
  42. * https://github.com/libcheck/check
  43. */
  44. #define FILENAME_LEN (128)
  45. int
  46. main(const int argc, char *argv[])
  47. {
  48. /* Supported command line arguments */
  49. const char *const suite_arg = "--suite=";
  50. const size_t suite_arg_size = strlen(suite_arg);
  51. const char *const test_case_arg = "--test-case=";
  52. const size_t test_case_arg_size = strlen(test_case_arg);
  53. const char *const test_dir_arg = "--test-dir=";
  54. const size_t test_dir_arg_size = strlen(test_dir_arg);
  55. const char *const test_log_arg = "--test-log=";
  56. const size_t test_log_arg_size = strlen(test_log_arg);
  57. const char *const help_arg = "--help";
  58. /* Test variables */
  59. const char *suite = NULL;
  60. const char *test_case = NULL;
  61. SRunner *srunner;
  62. int number_run = 0;
  63. int number_failed = 0;
  64. const char *test_log_prefix = NULL;
  65. char test_log_name[FILENAME_LEN];
  66. char test_xml_name[FILENAME_LEN];
  67. int i;
  68. /* Check command line parameters for tests */
  69. for (i = 1; i < argc; ++i) {
  70. if (0 == strncmp(suite_arg, argv[i], suite_arg_size)
  71. && (strlen(argv[i]) > suite_arg_size)) {
  72. suite = &argv[i][suite_arg_size];
  73. } else if (0 == strncmp(test_case_arg, argv[i], test_case_arg_size)
  74. && (strlen(argv[i]) > test_case_arg_size)) {
  75. test_case = &argv[i][test_case_arg_size];
  76. } else if (0 == strncmp(test_dir_arg, argv[i], test_dir_arg_size)
  77. && (strlen(argv[i]) > test_dir_arg_size)) {
  78. set_test_directory(&argv[i][test_dir_arg_size]);
  79. } else if (0 == strncmp(test_log_arg, argv[i], test_log_arg_size)
  80. && (strlen(argv[i]) > test_log_arg_size)) {
  81. test_log_prefix = &argv[i][test_log_arg_size];
  82. if ((strlen(test_log_prefix) + 16) > FILENAME_LEN) {
  83. fprintf(stderr, "Argument too long: %s\n", argv[i]);
  84. exit(EXIT_FAILURE);
  85. }
  86. } else if (0 == strcmp(help_arg, argv[i])) {
  87. printf(
  88. "Usage: %s [options]\n"
  89. " --suite=Suite Determines the suite to run\n"
  90. " --test-case='Test Case' Determines the test case to run\n"
  91. " --test-dir='folder/path' The location of the test directory "
  92. "with the \n"
  93. " 'fixtures' and 'expected\n",
  94. argv[0]);
  95. exit(EXIT_SUCCESS);
  96. } else {
  97. fprintf(stderr, "Invalid argument: %s\n", argv[i]);
  98. exit(EXIT_FAILURE);
  99. }
  100. }
  101. /* Register all tests to run them later */
  102. srunner = srunner_create(make_public_func_suite());
  103. srunner_add_suite(srunner, make_public_server_suite());
  104. srunner_add_suite(srunner, make_private_suite());
  105. srunner_add_suite(srunner, make_private_exe_suite());
  106. srunner_add_suite(srunner, make_timertest_suite());
  107. /* Write test logs to a file */
  108. if (test_log_prefix == NULL) {
  109. /* Find the next free log name */
  110. FILE *f;
  111. for (i = 1;; i++) {
  112. /* enumerate all log files (8.3 filename using 3 digits) */
  113. sprintf(test_log_name, "test-%03i.log", i);
  114. f = fopen(test_log_name, "r");
  115. if (f) {
  116. /* file already exists */
  117. fclose(f);
  118. /* try next name */
  119. continue;
  120. }
  121. /* file does not exist - use this name */
  122. srunner_set_log(srunner, test_log_name);
  123. /* use the same index for xml as for log */
  124. sprintf(test_xml_name, "test-%03i.xml", i);
  125. srunner_set_xml(srunner, test_xml_name);
  126. break;
  127. }
  128. } else {
  129. /* We got a test log name from the command line */
  130. sprintf(test_log_name, "%s.log", test_log_prefix);
  131. srunner_set_log(srunner, test_log_name);
  132. sprintf(test_xml_name, "%s.xml", test_log_prefix);
  133. srunner_set_xml(srunner, test_xml_name);
  134. }
  135. /* Run tests, using log level CK_VERBOSE, since CK_NORMAL
  136. * offers not enough diagnosis to analyze failed tests.
  137. * see http://check.sourceforge.net/doc/check_html/check_3.html */
  138. srunner_run(srunner, suite, test_case, CK_VERBOSE);
  139. /* Check passed / failed */
  140. number_run = srunner_ntests_run(srunner);
  141. number_failed = srunner_ntests_failed(srunner);
  142. srunner_free(srunner);
  143. return ((number_failed == 0) && (number_run != 0)) ? EXIT_SUCCESS
  144. : EXIT_FAILURE;
  145. }