tinytest.c 12 KB

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