tinytest.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* tinytest.c -- Copyright 2009-2012 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. #ifdef TINYTEST_LOCAL
  26. #include "tinytest_local.h"
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #ifdef _WIN32
  33. #include <windows.h>
  34. #else
  35. #include <sys/types.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. #endif
  39. #ifndef __GNUC__
  40. #define __attribute__(x)
  41. #endif
  42. #include "tinytest.h"
  43. #include "tinytest_macros.h"
  44. #define LONGEST_TEST_NAME 16384
  45. static int in_tinytest_main = 0; /**< true if we're in tinytest_main().*/
  46. static int n_ok = 0; /**< Number of tests that have passed */
  47. static int n_bad = 0; /**< Number of tests that have failed. */
  48. static int n_skipped = 0; /**< Number of tests that have been skipped. */
  49. static int opt_forked = 0; /**< True iff we're called from inside a win32 fork*/
  50. static int opt_nofork = 0; /**< Suppress calls to fork() for debugging. */
  51. static int opt_verbosity = 1; /**< -==quiet,0==terse,1==normal,2==verbose */
  52. const char *verbosity_flag = "";
  53. enum outcome { SKIP=2, OK=1, FAIL=0 };
  54. static enum outcome cur_test_outcome = 0;
  55. const char *cur_test_prefix = NULL; /**< prefix of the current test group */
  56. /** Name of the current test, if we haven't logged is yet. Used for --quiet */
  57. const char *cur_test_name = NULL;
  58. #ifdef _WIN32
  59. /* Copy of argv[0] for win32. */
  60. static char commandname[MAX_PATH+1];
  61. #endif
  62. static void usage(struct testgroup_t *groups, int list_groups)
  63. __attribute__((noreturn));
  64. static enum outcome
  65. testcase_run_bare_(const struct testcase_t *testcase)
  66. {
  67. void *env = NULL;
  68. int outcome;
  69. if (testcase->setup) {
  70. env = testcase->setup->setup_fn(testcase);
  71. if (!env)
  72. return FAIL;
  73. else if (env == (void*)TT_SKIP)
  74. return SKIP;
  75. }
  76. cur_test_outcome = OK;
  77. testcase->fn(env);
  78. outcome = cur_test_outcome;
  79. if (testcase->setup) {
  80. if (testcase->setup->cleanup_fn(testcase, env) == 0)
  81. outcome = FAIL;
  82. }
  83. return outcome;
  84. }
  85. #define MAGIC_EXITCODE 42
  86. static enum outcome
  87. testcase_run_forked_(const struct testgroup_t *group,
  88. const struct testcase_t *testcase)
  89. {
  90. #ifdef _WIN32
  91. /* Fork? On Win32? How primitive! We'll do what the smart kids do:
  92. we'll invoke our own exe (whose name we recall from the command
  93. line) with a command line that tells it to run just the test we
  94. want, and this time without forking.
  95. (No, threads aren't an option. The whole point of forking is to
  96. share no state between tests.)
  97. */
  98. int ok;
  99. char buffer[LONGEST_TEST_NAME+256];
  100. STARTUPINFOA si;
  101. PROCESS_INFORMATION info;
  102. DWORD exitcode;
  103. if (!in_tinytest_main) {
  104. printf("\nERROR. On Windows, testcase_run_forked_ must be"
  105. " called from within tinytest_main.\n");
  106. abort();
  107. }
  108. if (opt_verbosity>0)
  109. printf("[forking] ");
  110. snprintf(buffer, sizeof(buffer), "%s --RUNNING-FORKED %s %s%s",
  111. commandname, verbosity_flag, group->prefix, testcase->name);
  112. memset(&si, 0, sizeof(si));
  113. memset(&info, 0, sizeof(info));
  114. si.cb = sizeof(si);
  115. ok = CreateProcessA(commandname, buffer, NULL, NULL, 0,
  116. 0, NULL, NULL, &si, &info);
  117. if (!ok) {
  118. printf("CreateProcess failed!\n");
  119. return 0;
  120. }
  121. WaitForSingleObject(info.hProcess, INFINITE);
  122. GetExitCodeProcess(info.hProcess, &exitcode);
  123. CloseHandle(info.hProcess);
  124. CloseHandle(info.hThread);
  125. if (exitcode == 0)
  126. return OK;
  127. else if (exitcode == MAGIC_EXITCODE)
  128. return SKIP;
  129. else
  130. return FAIL;
  131. #else
  132. int outcome_pipe[2];
  133. pid_t pid;
  134. (void)group;
  135. if (pipe(outcome_pipe))
  136. perror("opening pipe");
  137. if (opt_verbosity>0)
  138. printf("[forking] ");
  139. pid = fork();
  140. if (!pid) {
  141. /* child. */
  142. int test_r, write_r;
  143. char b[1];
  144. close(outcome_pipe[0]);
  145. test_r = testcase_run_bare_(testcase);
  146. assert(0<=(int)test_r && (int)test_r<=2);
  147. b[0] = "NYS"[test_r];
  148. write_r = (int)write(outcome_pipe[1], b, 1);
  149. if (write_r != 1) {
  150. perror("write outcome to pipe");
  151. exit(1);
  152. }
  153. exit(0);
  154. return FAIL; /* unreachable */
  155. } else {
  156. /* parent */
  157. int status, r;
  158. char b[1];
  159. /* Close this now, so that if the other side closes it,
  160. * our read fails. */
  161. close(outcome_pipe[1]);
  162. r = (int)read(outcome_pipe[0], b, 1);
  163. if (r == 0) {
  164. printf("[Lost connection!] ");
  165. return 0;
  166. } else if (r != 1) {
  167. perror("read outcome from pipe");
  168. }
  169. waitpid(pid, &status, 0);
  170. close(outcome_pipe[0]);
  171. return b[0]=='Y' ? OK : (b[0]=='S' ? SKIP : FAIL);
  172. }
  173. #endif
  174. }
  175. int
  176. testcase_run_one(const struct testgroup_t *group,
  177. const struct testcase_t *testcase)
  178. {
  179. enum outcome outcome;
  180. if (testcase->flags & TT_SKIP) {
  181. if (opt_verbosity>0)
  182. printf("%s%s: SKIPPED\n",
  183. group->prefix, testcase->name);
  184. ++n_skipped;
  185. return SKIP;
  186. }
  187. if (opt_verbosity>0 && !opt_forked) {
  188. printf("%s%s: ", group->prefix, testcase->name);
  189. } else {
  190. if (opt_verbosity==0) printf(".");
  191. cur_test_prefix = group->prefix;
  192. cur_test_name = testcase->name;
  193. }
  194. if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) {
  195. outcome = testcase_run_forked_(group, testcase);
  196. } else {
  197. outcome = testcase_run_bare_(testcase);
  198. }
  199. if (outcome == OK) {
  200. ++n_ok;
  201. if (opt_verbosity>0 && !opt_forked)
  202. puts(opt_verbosity==1?"OK":"");
  203. } else if (outcome == SKIP) {
  204. ++n_skipped;
  205. if (opt_verbosity>0 && !opt_forked)
  206. puts("SKIPPED");
  207. } else {
  208. ++n_bad;
  209. if (!opt_forked)
  210. printf("\n [%s FAILED]\n", testcase->name);
  211. }
  212. if (opt_forked) {
  213. exit(outcome==OK ? 0 : (outcome==SKIP?MAGIC_EXITCODE : 1));
  214. return 1; /* unreachable */
  215. } else {
  216. return (int)outcome;
  217. }
  218. }
  219. int
  220. tinytest_set_flag_(struct testgroup_t *groups, const char *arg, unsigned long flag)
  221. {
  222. int i, j;
  223. size_t length = LONGEST_TEST_NAME;
  224. char fullname[LONGEST_TEST_NAME];
  225. int found=0;
  226. if (strstr(arg, ".."))
  227. length = strstr(arg,"..")-arg;
  228. for (i=0; groups[i].prefix; ++i) {
  229. for (j=0; groups[i].cases[j].name; ++j) {
  230. snprintf(fullname, sizeof(fullname), "%s%s",
  231. groups[i].prefix, groups[i].cases[j].name);
  232. if (!flag) /* Hack! */
  233. printf(" %s\n", fullname);
  234. if (!strncmp(fullname, arg, length)) {
  235. groups[i].cases[j].flags |= flag;
  236. ++found;
  237. }
  238. }
  239. }
  240. return found;
  241. }
  242. static void
  243. usage(struct testgroup_t *groups, int list_groups)
  244. {
  245. puts("Options are: [--verbose|--quiet|--terse] [--no-fork]");
  246. puts(" Specify tests by name, or using a prefix ending with '..'");
  247. puts(" To skip a test, list give its name prefixed with a colon.");
  248. puts(" Use --list-tests for a list of tests.");
  249. if (list_groups) {
  250. puts("Known tests are:");
  251. tinytest_set_flag_(groups, "..", 0);
  252. }
  253. exit(0);
  254. }
  255. int
  256. tinytest_main(int c, const char **v, struct testgroup_t *groups)
  257. {
  258. int i, j, n=0;
  259. #ifdef _WIN32
  260. const char *sp = strrchr(v[0], '.');
  261. const char *extension = "";
  262. if (!sp || stricmp(sp, ".exe"))
  263. extension = ".exe"; /* Add an exe so CreateProcess will work */
  264. snprintf(commandname, sizeof(commandname), "%s%s", v[0], extension);
  265. commandname[MAX_PATH]='\0';
  266. #endif
  267. for (i=1; i<c; ++i) {
  268. if (v[i][0] == '-') {
  269. if (!strcmp(v[i], "--RUNNING-FORKED")) {
  270. opt_forked = 1;
  271. } else if (!strcmp(v[i], "--no-fork")) {
  272. opt_nofork = 1;
  273. } else if (!strcmp(v[i], "--quiet")) {
  274. opt_verbosity = -1;
  275. verbosity_flag = "--quiet";
  276. } else if (!strcmp(v[i], "--verbose")) {
  277. opt_verbosity = 2;
  278. verbosity_flag = "--verbose";
  279. } else if (!strcmp(v[i], "--terse")) {
  280. opt_verbosity = 0;
  281. verbosity_flag = "--terse";
  282. } else if (!strcmp(v[i], "--help")) {
  283. usage(groups, 0);
  284. } else if (!strcmp(v[i], "--list-tests")) {
  285. usage(groups, 1);
  286. } else {
  287. printf("Unknown option %s. Try --help\n",v[i]);
  288. return -1;
  289. }
  290. } else {
  291. const char *test = v[i];
  292. int flag = TT_ENABLED_;
  293. if (test[0] == ':') {
  294. ++test;
  295. flag = TT_SKIP;
  296. } else {
  297. ++n;
  298. }
  299. if (!tinytest_set_flag_(groups, test, flag)) {
  300. printf("No such test as %s!\n", v[i]);
  301. return -1;
  302. }
  303. }
  304. }
  305. if (!n)
  306. tinytest_set_flag_(groups, "..", TT_ENABLED_);
  307. setvbuf(stdout, NULL, _IONBF, 0);
  308. ++in_tinytest_main;
  309. for (i=0; groups[i].prefix; ++i)
  310. for (j=0; groups[i].cases[j].name; ++j)
  311. if (groups[i].cases[j].flags & TT_ENABLED_)
  312. testcase_run_one(&groups[i],
  313. &groups[i].cases[j]);
  314. --in_tinytest_main;
  315. if (opt_verbosity==0)
  316. puts("");
  317. if (n_bad)
  318. printf("%d/%d TESTS FAILED. (%d skipped)\n", n_bad,
  319. n_bad+n_ok,n_skipped);
  320. else if (opt_verbosity >= 1)
  321. printf("%d tests ok. (%d skipped)\n", n_ok, n_skipped);
  322. return (n_bad == 0) ? 0 : 1;
  323. }
  324. int
  325. tinytest_get_verbosity_(void)
  326. {
  327. return opt_verbosity;
  328. }
  329. void
  330. tinytest_set_test_failed_(void)
  331. {
  332. if (opt_verbosity <= 0 && cur_test_name) {
  333. if (opt_verbosity==0) puts("");
  334. printf("%s%s: ", cur_test_prefix, cur_test_name);
  335. cur_test_name = NULL;
  336. }
  337. cur_test_outcome = 0;
  338. }
  339. void
  340. tinytest_set_test_skipped_(void)
  341. {
  342. if (cur_test_outcome==OK)
  343. cur_test_outcome = SKIP;
  344. }