tinytest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #define TINYTEST_POSTFORK
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #ifndef NO_FORKING
  34. #ifdef _WIN32
  35. #include <windows.h>
  36. #else
  37. #include <sys/types.h>
  38. #include <sys/wait.h>
  39. #include <unistd.h>
  40. #endif
  41. #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
  42. #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
  43. __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
  44. /* Workaround for a stupid bug in OSX 10.6 */
  45. #define FORK_BREAKS_GCOV
  46. #include <vproc.h>
  47. #endif
  48. #endif
  49. #endif /* !NO_FORKING */
  50. #ifndef __GNUC__
  51. #define __attribute__(x)
  52. #endif
  53. #include "tinytest.h"
  54. #include "tinytest_macros.h"
  55. #define LONGEST_TEST_NAME 16384
  56. static int in_tinytest_main = 0; /**< true if we're in tinytest_main().*/
  57. static int n_ok = 0; /**< Number of tests that have passed */
  58. static int n_bad = 0; /**< Number of tests that have failed. */
  59. static int n_skipped = 0; /**< Number of tests that have been skipped. */
  60. static int opt_forked = 0; /**< True iff we're called from inside a win32 fork*/
  61. static int opt_nofork = 0; /**< Suppress calls to fork() for debugging. */
  62. static int opt_verbosity = 1; /**< -==quiet,0==terse,1==normal,2==verbose */
  63. static const char *verbosity_flag = "";
  64. static const struct testlist_alias_t *cfg_aliases=NULL;
  65. enum outcome { SKIP=2, OK=1, FAIL=0 };
  66. static enum outcome cur_test_outcome = 0;
  67. /** prefix of the current test group */
  68. static const char *cur_test_prefix = NULL;
  69. /** Name of the current test, if we haven't logged is yet. Used for --quiet */
  70. static const char *cur_test_name = NULL;
  71. #ifdef _WIN32
  72. /* Copy of argv[0] for win32. */
  73. static char commandname[MAX_PATH+1];
  74. #endif
  75. static void usage(struct testgroup_t *groups, int list_groups)
  76. __attribute__((noreturn));
  77. static int process_test_option(struct testgroup_t *groups, const char *test);
  78. static enum outcome
  79. testcase_run_bare_(const struct testcase_t *testcase)
  80. {
  81. void *env = NULL;
  82. int outcome;
  83. if (testcase->setup) {
  84. env = testcase->setup->setup_fn(testcase);
  85. if (!env)
  86. return FAIL;
  87. else if (env == (void*)TT_SKIP)
  88. return SKIP;
  89. }
  90. cur_test_outcome = OK;
  91. testcase->fn(env);
  92. outcome = cur_test_outcome;
  93. if (testcase->setup) {
  94. if (testcase->setup->cleanup_fn(testcase, env) == 0)
  95. outcome = FAIL;
  96. }
  97. return outcome;
  98. }
  99. #define MAGIC_EXITCODE 42
  100. #ifndef NO_FORKING
  101. #ifdef TINYTEST_POSTFORK
  102. void tinytest_prefork(void);
  103. void tinytest_postfork(void);
  104. #else
  105. static void tinytest_prefork(void) { }
  106. static void tinytest_postfork(void) { }
  107. #endif
  108. static enum outcome
  109. testcase_run_forked_(const struct testgroup_t *group,
  110. const struct testcase_t *testcase)
  111. {
  112. #ifdef _WIN32
  113. /* Fork? On Win32? How primitive! We'll do what the smart kids do:
  114. we'll invoke our own exe (whose name we recall from the command
  115. line) with a command line that tells it to run just the test we
  116. want, and this time without forking.
  117. (No, threads aren't an option. The whole point of forking is to
  118. share no state between tests.)
  119. */
  120. int ok;
  121. char buffer[LONGEST_TEST_NAME+256];
  122. STARTUPINFOA si;
  123. PROCESS_INFORMATION info;
  124. DWORD exitcode;
  125. if (!in_tinytest_main) {
  126. printf("\nERROR. On Windows, testcase_run_forked_ must be"
  127. " called from within tinytest_main.\n");
  128. abort();
  129. }
  130. if (opt_verbosity>0)
  131. printf("[forking] ");
  132. snprintf(buffer, sizeof(buffer), "\"%s\" --RUNNING-FORKED %s %s%s",
  133. commandname, verbosity_flag, group->prefix, testcase->name);
  134. memset(&si, 0, sizeof(si));
  135. memset(&info, 0, sizeof(info));
  136. si.cb = sizeof(si);
  137. ok = CreateProcessA(commandname, buffer, NULL, NULL, 0,
  138. 0, NULL, NULL, &si, &info);
  139. if (!ok) {
  140. printf("CreateProcess failed!\n");
  141. return 0;
  142. }
  143. WaitForSingleObject(info.hProcess, INFINITE);
  144. GetExitCodeProcess(info.hProcess, &exitcode);
  145. CloseHandle(info.hProcess);
  146. CloseHandle(info.hThread);
  147. if (exitcode == 0)
  148. return OK;
  149. else if (exitcode == MAGIC_EXITCODE)
  150. return SKIP;
  151. else
  152. return FAIL;
  153. #else
  154. int outcome_pipe[2];
  155. pid_t pid;
  156. (void)group;
  157. if (pipe(outcome_pipe))
  158. perror("opening pipe");
  159. if (opt_verbosity>0)
  160. printf("[forking] ");
  161. tinytest_prefork();
  162. pid = fork();
  163. #ifdef FORK_BREAKS_GCOV
  164. vproc_transaction_begin(0);
  165. #endif
  166. tinytest_postfork();
  167. if (!pid) {
  168. /* child. */
  169. int test_r, write_r;
  170. char b[1];
  171. close(outcome_pipe[0]);
  172. test_r = testcase_run_bare_(testcase);
  173. assert(0<=(int)test_r && (int)test_r<=2);
  174. b[0] = "NYS"[test_r];
  175. write_r = (int)write(outcome_pipe[1], b, 1);
  176. if (write_r != 1) {
  177. perror("write outcome to pipe");
  178. exit(1);
  179. }
  180. exit(0);
  181. return FAIL; /* unreachable */
  182. } else {
  183. /* parent */
  184. int status, r;
  185. char b[1];
  186. /* Close this now, so that if the other side closes it,
  187. * our read fails. */
  188. close(outcome_pipe[1]);
  189. r = (int)read(outcome_pipe[0], b, 1);
  190. if (r == 0) {
  191. printf("[Lost connection!] ");
  192. return FAIL;
  193. } else if (r != 1) {
  194. perror("read outcome from pipe");
  195. }
  196. r = waitpid(pid, &status, 0);
  197. close(outcome_pipe[0]);
  198. if (r == -1) {
  199. perror("waitpid");
  200. return FAIL;
  201. }
  202. if (! WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  203. printf("[did not exit cleanly.]");
  204. return FAIL;
  205. }
  206. return b[0]=='Y' ? OK : (b[0]=='S' ? SKIP : FAIL);
  207. }
  208. #endif
  209. }
  210. #endif /* !NO_FORKING */
  211. int
  212. testcase_run_one(const struct testgroup_t *group,
  213. const struct testcase_t *testcase)
  214. {
  215. enum outcome outcome;
  216. if (testcase->flags & (TT_SKIP|TT_OFF_BY_DEFAULT)) {
  217. if (opt_verbosity>0)
  218. printf("%s%s: %s\n",
  219. group->prefix, testcase->name,
  220. (testcase->flags & TT_SKIP) ? "SKIPPED" : "DISABLED");
  221. ++n_skipped;
  222. return SKIP;
  223. }
  224. if (opt_verbosity>0 && !opt_forked) {
  225. printf("%s%s: ", group->prefix, testcase->name);
  226. } else {
  227. if (opt_verbosity==0) printf(".");
  228. cur_test_prefix = group->prefix;
  229. cur_test_name = testcase->name;
  230. }
  231. #ifndef NO_FORKING
  232. if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) {
  233. outcome = testcase_run_forked_(group, testcase);
  234. } else {
  235. #else
  236. {
  237. #endif
  238. outcome = testcase_run_bare_(testcase);
  239. }
  240. if (outcome == OK) {
  241. ++n_ok;
  242. if (opt_verbosity>0 && !opt_forked)
  243. puts(opt_verbosity==1?"OK":"");
  244. } else if (outcome == SKIP) {
  245. ++n_skipped;
  246. if (opt_verbosity>0 && !opt_forked)
  247. puts("SKIPPED");
  248. } else {
  249. ++n_bad;
  250. if (!opt_forked)
  251. printf("\n [%s FAILED]\n", testcase->name);
  252. }
  253. if (opt_forked) {
  254. exit(outcome==OK ? 0 : (outcome==SKIP?MAGIC_EXITCODE : 1));
  255. return 1; /* unreachable */
  256. } else {
  257. return (int)outcome;
  258. }
  259. }
  260. int
  261. tinytest_set_flag_(struct testgroup_t *groups, const char *arg, int set, unsigned long flag)
  262. {
  263. int i, j;
  264. size_t length = LONGEST_TEST_NAME;
  265. char fullname[LONGEST_TEST_NAME];
  266. int found=0;
  267. if (strstr(arg, ".."))
  268. length = strstr(arg,"..")-arg;
  269. for (i=0; groups[i].prefix; ++i) {
  270. for (j=0; groups[i].cases[j].name; ++j) {
  271. struct testcase_t *testcase = &groups[i].cases[j];
  272. snprintf(fullname, sizeof(fullname), "%s%s",
  273. groups[i].prefix, testcase->name);
  274. if (!flag) { /* Hack! */
  275. printf(" %s", fullname);
  276. if (testcase->flags & TT_OFF_BY_DEFAULT)
  277. puts(" (Off by default)");
  278. else if (testcase->flags & TT_SKIP)
  279. puts(" (DISABLED)");
  280. else
  281. puts("");
  282. }
  283. if (!strncmp(fullname, arg, length)) {
  284. if (set)
  285. testcase->flags |= flag;
  286. else
  287. testcase->flags &= ~flag;
  288. ++found;
  289. }
  290. }
  291. }
  292. return found;
  293. }
  294. static void
  295. usage(struct testgroup_t *groups, int list_groups)
  296. {
  297. puts("Options are: [--verbose|--quiet|--terse] [--no-fork]");
  298. puts(" Specify tests by name, or using a prefix ending with '..'");
  299. puts(" To skip a test, prefix its name with a colon.");
  300. puts(" To enable a disabled test, prefix its name with a plus.");
  301. puts(" Use --list-tests for a list of tests.");
  302. if (list_groups) {
  303. puts("Known tests are:");
  304. tinytest_set_flag_(groups, "..", 1, 0);
  305. }
  306. exit(0);
  307. }
  308. static int
  309. process_test_alias(struct testgroup_t *groups, const char *test)
  310. {
  311. int i, j, n, r;
  312. for (i=0; cfg_aliases && cfg_aliases[i].name; ++i) {
  313. if (!strcmp(cfg_aliases[i].name, test)) {
  314. n = 0;
  315. for (j = 0; cfg_aliases[i].tests[j]; ++j) {
  316. r = process_test_option(groups, cfg_aliases[i].tests[j]);
  317. if (r<0)
  318. return -1;
  319. n += r;
  320. }
  321. return n;
  322. }
  323. }
  324. printf("No such test alias as @%s!",test);
  325. return -1;
  326. }
  327. static int
  328. process_test_option(struct testgroup_t *groups, const char *test)
  329. {
  330. int flag = TT_ENABLED_;
  331. int n = 0;
  332. if (test[0] == '@') {
  333. return process_test_alias(groups, test + 1);
  334. } else if (test[0] == ':') {
  335. ++test;
  336. flag = TT_SKIP;
  337. } else if (test[0] == '+') {
  338. ++test;
  339. ++n;
  340. if (!tinytest_set_flag_(groups, test, 0, TT_OFF_BY_DEFAULT)) {
  341. printf("No such test as %s!\n", test);
  342. return -1;
  343. }
  344. } else {
  345. ++n;
  346. }
  347. if (!tinytest_set_flag_(groups, test, 1, flag)) {
  348. printf("No such test as %s!\n", test);
  349. return -1;
  350. }
  351. return n;
  352. }
  353. void
  354. tinytest_set_aliases(const struct testlist_alias_t *aliases)
  355. {
  356. cfg_aliases = aliases;
  357. }
  358. int
  359. tinytest_main(int c, const char **v, struct testgroup_t *groups)
  360. {
  361. int i, j, n=0;
  362. #ifdef _WIN32
  363. const char *sp = strrchr(v[0], '.');
  364. const char *extension = "";
  365. if (!sp || stricmp(sp, ".exe"))
  366. extension = ".exe"; /* Add an exe so CreateProcess will work */
  367. snprintf(commandname, sizeof(commandname), "%s%s", v[0], extension);
  368. commandname[MAX_PATH]='\0';
  369. #endif
  370. for (i=1; i<c; ++i) {
  371. if (v[i][0] == '-') {
  372. if (!strcmp(v[i], "--RUNNING-FORKED")) {
  373. opt_forked = 1;
  374. } else if (!strcmp(v[i], "--no-fork")) {
  375. opt_nofork = 1;
  376. } else if (!strcmp(v[i], "--quiet")) {
  377. opt_verbosity = -1;
  378. verbosity_flag = "--quiet";
  379. } else if (!strcmp(v[i], "--verbose")) {
  380. opt_verbosity = 2;
  381. verbosity_flag = "--verbose";
  382. } else if (!strcmp(v[i], "--terse")) {
  383. opt_verbosity = 0;
  384. verbosity_flag = "--terse";
  385. } else if (!strcmp(v[i], "--help")) {
  386. usage(groups, 0);
  387. } else if (!strcmp(v[i], "--list-tests")) {
  388. usage(groups, 1);
  389. } else {
  390. printf("Unknown option %s. Try --help\n",v[i]);
  391. return -1;
  392. }
  393. } else {
  394. int r = process_test_option(groups, v[i]);
  395. if (r<0)
  396. return -1;
  397. n += r;
  398. }
  399. }
  400. if (!n)
  401. tinytest_set_flag_(groups, "..", 1, TT_ENABLED_);
  402. #ifdef _IONBF
  403. setvbuf(stdout, NULL, _IONBF, 0);
  404. #endif
  405. ++in_tinytest_main;
  406. for (i=0; groups[i].prefix; ++i)
  407. for (j=0; groups[i].cases[j].name; ++j)
  408. if (groups[i].cases[j].flags & TT_ENABLED_)
  409. testcase_run_one(&groups[i],
  410. &groups[i].cases[j]);
  411. --in_tinytest_main;
  412. if (opt_verbosity==0)
  413. puts("");
  414. if (n_bad)
  415. printf("%d/%d TESTS FAILED. (%d skipped)\n", n_bad,
  416. n_bad+n_ok,n_skipped);
  417. else if (opt_verbosity >= 1)
  418. printf("%d tests ok. (%d skipped)\n", n_ok, n_skipped);
  419. return (n_bad == 0) ? 0 : 1;
  420. }
  421. int
  422. tinytest_get_verbosity_(void)
  423. {
  424. return opt_verbosity;
  425. }
  426. void
  427. tinytest_set_test_failed_(void)
  428. {
  429. if (opt_verbosity <= 0 && cur_test_name) {
  430. if (opt_verbosity==0) puts("");
  431. printf("%s%s: ", cur_test_prefix, cur_test_name);
  432. cur_test_name = NULL;
  433. }
  434. cur_test_outcome = 0;
  435. }
  436. void
  437. tinytest_set_test_skipped_(void)
  438. {
  439. if (cur_test_outcome==OK)
  440. cur_test_outcome = SKIP;
  441. }
  442. char *
  443. tinytest_format_hex_(const void *val_, unsigned long len)
  444. {
  445. const unsigned char *val = val_;
  446. char *result, *cp;
  447. size_t i;
  448. int ellipses = 0;
  449. if (!val)
  450. return strdup("null");
  451. if (len > 1024) {
  452. ellipses = 3;
  453. len = 1024;
  454. }
  455. if (!(result = malloc(len*2+4)))
  456. return strdup("<allocation failure>");
  457. cp = result;
  458. for (i=0;i<len;++i) {
  459. *cp++ = "0123456789ABCDEF"[(val[i] >> 4)&0x0f];
  460. *cp++ = "0123456789ABCDEF"[val[i] & 0x0f];
  461. }
  462. while (ellipses--)
  463. *cp++ = '.';
  464. *cp = 0;
  465. return result;
  466. }