tinytest.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* tinytest.c -- Copyright 2009 Nick Mathewson
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * 1. Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #ifdef WIN32
  30. #include <windows.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <unistd.h>
  35. #endif
  36. #ifndef __GNUC__
  37. #define __attribute__(x)
  38. #endif
  39. #include "tinytest.h"
  40. #include "tinytest_macros.h"
  41. #define LONGEST_TEST_NAME 16384
  42. static int in_tinytest_main = 0; /**< true if we're in tinytest_main().*/
  43. static int n_ok = 0; /**< Number of tests that have passed */
  44. static int n_bad = 0; /**< Number of tests that have failed. */
  45. static int n_skipped = 0; /**< Number of tests that have been skipped. */
  46. static int opt_forked = 0; /**< True iff we're called from inside a win32 fork*/
  47. static int opt_nofork = 0; /**< Suppress calls to fork() for debugging. */
  48. static int opt_verbosity = 1; /**< -==quiet,0==terse,1==normal,2==verbose */
  49. const char *verbosity_flag = "";
  50. enum outcome { SKIP=2, OK=1, FAIL=0 };
  51. static enum outcome cur_test_outcome = 0;
  52. const char *cur_test_prefix = NULL; /**< prefix of the current test group */
  53. /** Name of the current test, if we haven't logged is yet. Used for --quiet */
  54. const char *cur_test_name = NULL;
  55. #ifdef WIN32
  56. /** Pointer to argv[0] for win32. */
  57. static const char *commandname = NULL;
  58. #endif
  59. static void usage(struct testgroup_t *groups, int list_groups)
  60. __attribute__((noreturn));
  61. static enum outcome
  62. _testcase_run_bare(const struct testcase_t *testcase)
  63. {
  64. void *env = NULL;
  65. int outcome;
  66. if (testcase->setup) {
  67. env = testcase->setup->setup_fn(testcase);
  68. if (!env)
  69. return FAIL;
  70. else if (env == (void*)TT_SKIP)
  71. return SKIP;
  72. }
  73. cur_test_outcome = OK;
  74. testcase->fn(env);
  75. outcome = cur_test_outcome;
  76. if (testcase->setup) {
  77. if (testcase->setup->cleanup_fn(testcase, env) == 0)
  78. outcome = FAIL;
  79. }
  80. return outcome;
  81. }
  82. #define MAGIC_EXITCODE 42
  83. static enum outcome
  84. _testcase_run_forked(const struct testgroup_t *group,
  85. const struct testcase_t *testcase)
  86. {
  87. #ifdef WIN32
  88. /* Fork? On Win32? How primitive! We'll do what the smart kids do:
  89. we'll invoke our own exe (whose name we recall from the command
  90. line) with a command line that tells it to run just the test we
  91. want, and this time without forking.
  92. (No, threads aren't an option. The whole point of forking is to
  93. share no state between tests.)
  94. */
  95. int ok;
  96. char buffer[LONGEST_TEST_NAME+256];
  97. STARTUPINFO si;
  98. PROCESS_INFORMATION info;
  99. DWORD exitcode;
  100. if (!in_tinytest_main) {
  101. printf("\nERROR. On Windows, _testcase_run_forked must be"
  102. " called from within tinytest_main.\n");
  103. abort();
  104. }
  105. if (opt_verbosity>0)
  106. printf("[forking] ");
  107. snprintf(buffer, sizeof(buffer), "%s --RUNNING-FORKED %s %s%s",
  108. commandname, verbosity_flag, group->prefix, testcase->name);
  109. memset(&si, 0, sizeof(si));
  110. memset(&info, 0, sizeof(info));
  111. si.cb = sizeof(si);
  112. ok = CreateProcess(commandname, buffer, NULL, NULL, 0,
  113. 0, NULL, NULL, &si, &info);
  114. if (!ok) {
  115. printf("CreateProcess failed!\n");
  116. return 0;
  117. }
  118. WaitForSingleObject(info.hProcess, INFINITE);
  119. GetExitCodeProcess(info.hProcess, &exitcode);
  120. CloseHandle(info.hProcess);
  121. CloseHandle(info.hThread);
  122. if (exitcode == 0)
  123. return OK;
  124. else if (exitcode == MAGIC_EXITCODE)
  125. return SKIP;
  126. else
  127. return FAIL;
  128. #else
  129. int outcome_pipe[2];
  130. pid_t pid;
  131. (void)group;
  132. if (pipe(outcome_pipe))
  133. perror("opening pipe");
  134. if (opt_verbosity>0)
  135. printf("[forking] ");
  136. pid = fork();
  137. if (!pid) {
  138. /* child. */
  139. int test_r, write_r;
  140. char b[1];
  141. close(outcome_pipe[0]);
  142. test_r = _testcase_run_bare(testcase);
  143. assert(0<=(int)test_r && (int)test_r<=2);
  144. b[0] = "NYS"[test_r];
  145. write_r = (int)write(outcome_pipe[1], b, 1);
  146. if (write_r != 1) {
  147. perror("write outcome to pipe");
  148. exit(1);
  149. }
  150. exit(0);
  151. } else {
  152. /* parent */
  153. int status, r;
  154. char b[1];
  155. /* Close this now, so that if the other side closes it,
  156. * our read fails. */
  157. close(outcome_pipe[1]);
  158. r = (int)read(outcome_pipe[0], b, 1);
  159. if (r == 0) {
  160. printf("[Lost connection!] ");
  161. return 0;
  162. } else if (r != 1) {
  163. perror("read outcome from pipe");
  164. }
  165. waitpid(pid, &status, 0);
  166. close(outcome_pipe[0]);
  167. return b[0]=='Y' ? OK : (b[0]=='S' ? SKIP : FAIL);
  168. }
  169. #endif
  170. }
  171. int
  172. testcase_run_one(const struct testgroup_t *group,
  173. const struct testcase_t *testcase)
  174. {
  175. enum outcome outcome;
  176. if (testcase->flags & TT_SKIP) {
  177. if (opt_verbosity>0)
  178. printf("%s%s: SKIPPED\n",
  179. group->prefix, testcase->name);
  180. ++n_skipped;
  181. return SKIP;
  182. }
  183. if (opt_verbosity>0 && !opt_forked) {
  184. printf("%s%s: ", group->prefix, testcase->name);
  185. } else {
  186. if (opt_verbosity==0) printf(".");
  187. cur_test_prefix = group->prefix;
  188. cur_test_name = testcase->name;
  189. }
  190. if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) {
  191. outcome = _testcase_run_forked(group, testcase);
  192. } else {
  193. outcome = _testcase_run_bare(testcase);
  194. }
  195. if (outcome == OK) {
  196. ++n_ok;
  197. if (opt_verbosity>0 && !opt_forked)
  198. puts(opt_verbosity==1?"OK":"");
  199. } else if (outcome == SKIP) {
  200. ++n_skipped;
  201. if (opt_verbosity>0 && !opt_forked)
  202. puts("SKIPPED");
  203. } else {
  204. ++n_bad;
  205. if (!opt_forked)
  206. printf("\n [%s FAILED]\n", testcase->name);
  207. }
  208. if (opt_forked) {
  209. exit(outcome==OK ? 0 : (outcome==SKIP?MAGIC_EXITCODE : 1));
  210. } else {
  211. return (int)outcome;
  212. }
  213. }
  214. int
  215. _tinytest_set_flag(struct testgroup_t *groups, const char *arg, unsigned long flag)
  216. {
  217. int i, j;
  218. size_t length = LONGEST_TEST_NAME;
  219. char fullname[LONGEST_TEST_NAME];
  220. int found=0;
  221. if (strstr(arg, ".."))
  222. length = strstr(arg,"..")-arg;
  223. for (i=0; groups[i].prefix; ++i) {
  224. for (j=0; groups[i].cases[j].name; ++j) {
  225. snprintf(fullname, sizeof(fullname), "%s%s",
  226. groups[i].prefix, groups[i].cases[j].name);
  227. if (!flag) /* Hack! */
  228. printf(" %s\n", fullname);
  229. if (!strncmp(fullname, arg, length)) {
  230. groups[i].cases[j].flags |= flag;
  231. ++found;
  232. }
  233. }
  234. }
  235. return found;
  236. }
  237. static void
  238. usage(struct testgroup_t *groups, int list_groups)
  239. {
  240. puts("Options are: [--verbose|--quiet|--terse] [--no-fork]");
  241. puts(" Specify tests by name, or using a prefix ending with '..'");
  242. puts(" Use --list-tests for a list of tests.");
  243. if (list_groups) {
  244. puts("Known tests are:");
  245. _tinytest_set_flag(groups, "..", 0);
  246. }
  247. exit(0);
  248. }
  249. int
  250. tinytest_main(int c, const char **v, struct testgroup_t *groups)
  251. {
  252. int i, j, n=0;
  253. #ifdef WIN32
  254. commandname = v[0];
  255. #endif
  256. for (i=1; i<c; ++i) {
  257. if (v[i][0] == '-') {
  258. if (!strcmp(v[i], "--RUNNING-FORKED")) {
  259. opt_forked = 1;
  260. } else if (!strcmp(v[i], "--no-fork")) {
  261. opt_nofork = 1;
  262. } else if (!strcmp(v[i], "--quiet")) {
  263. opt_verbosity = -1;
  264. verbosity_flag = "--quiet";
  265. } else if (!strcmp(v[i], "--verbose")) {
  266. opt_verbosity = 2;
  267. verbosity_flag = "--verbose";
  268. } else if (!strcmp(v[i], "--terse")) {
  269. opt_verbosity = 0;
  270. verbosity_flag = "--terse";
  271. } else if (!strcmp(v[i], "--help")) {
  272. usage(groups, 0);
  273. } else if (!strcmp(v[i], "--list-tests")) {
  274. usage(groups, 1);
  275. } else {
  276. printf("Unknown option %s. Try --help\n",v[i]);
  277. return -1;
  278. }
  279. } else {
  280. ++n;
  281. if (!_tinytest_set_flag(groups, v[i], _TT_ENABLED)) {
  282. printf("No such test as %s!\n", v[i]);
  283. return -1;
  284. }
  285. }
  286. }
  287. if (!n)
  288. _tinytest_set_flag(groups, "..", _TT_ENABLED);
  289. setvbuf(stdout, NULL, _IONBF, 0);
  290. ++in_tinytest_main;
  291. for (i=0; groups[i].prefix; ++i)
  292. for (j=0; groups[i].cases[j].name; ++j)
  293. if (groups[i].cases[j].flags & _TT_ENABLED)
  294. testcase_run_one(&groups[i],
  295. &groups[i].cases[j]);
  296. --in_tinytest_main;
  297. if (opt_verbosity==0)
  298. puts("");
  299. if (n_bad)
  300. printf("%d/%d TESTS FAILED. (%d skipped)\n", n_bad,
  301. n_bad+n_ok,n_skipped);
  302. else if (opt_verbosity >= 1)
  303. printf("%d tests ok. (%d skipped)\n", n_ok, n_skipped);
  304. return (n_bad == 0) ? 0 : 1;
  305. }
  306. int
  307. _tinytest_get_verbosity(void)
  308. {
  309. return opt_verbosity;
  310. }
  311. void
  312. _tinytest_set_test_failed(void)
  313. {
  314. if (opt_verbosity <= 0 && cur_test_name) {
  315. if (opt_verbosity==0) puts("");
  316. printf("%s%s: ", cur_test_prefix, cur_test_name);
  317. cur_test_name = NULL;
  318. }
  319. cur_test_outcome = 0;
  320. }
  321. void
  322. _tinytest_set_test_skipped(void)
  323. {
  324. if (cur_test_outcome==OK)
  325. cur_test_outcome = SKIP;
  326. }