testc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif /* HAVE_CONFIG_H */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <dlfcn.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <sys/uio.h>
  14. #include <errno.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #if WITH_INTERNAL_GETOPT
  18. #include "libc/getopt.h"
  19. #else
  20. #ifdef HAVE_GETOPT_H
  21. #include <getopt.h>
  22. #endif
  23. #endif
  24. #if HAVE_LOCALE_H
  25. #include <locale.h>
  26. #endif
  27. #if HAVE_LANGINFO_CODESET
  28. #include <langinfo.h>
  29. #endif
  30. #include "faux/faux.h"
  31. #include "faux/str.h"
  32. #include "faux/list.h"
  33. #include "faux/testc_helpers.h"
  34. #ifndef VERSION
  35. #define VERSION 1.0.0
  36. #endif
  37. #define QUOTE(t) #t
  38. #define version(v) printf("%s\n", v)
  39. // Version of testc API (not version of programm)
  40. #define TESTC_VERSION_MAJOR_DEFAULT 1
  41. #define TESTC_VERSION_MINOR_DEFAULT 0
  42. #define SYM_TESTC_VERSION_MAJOR "testc_version_major"
  43. #define SYM_TESTC_VERSION_MINOR "testc_version_minor"
  44. #define SYM_TESTC_MODULE "testc_module"
  45. #define CHUNK_SIZE 1024
  46. #define TEST_OUTPUT_LIMIT 1024 * CHUNK_SIZE
  47. // Command line options */
  48. struct opts_s {
  49. bool_t debug;
  50. bool_t preserve_tmp;
  51. faux_list_t *so_list;
  52. };
  53. typedef struct opts_s opts_t;
  54. static opts_t *opts_parse(int argc, char *argv[]);
  55. static void opts_free(opts_t *opts);
  56. static void help(int status, const char *argv0);
  57. static int exec_test(int (*test_sym)(void), faux_list_t **buf_list, bool_t debug);
  58. static void print_test_output(faux_list_t *buf_list);
  59. int main(int argc, char *argv[])
  60. {
  61. opts_t *opts = NULL; // Command line options
  62. faux_list_node_t *iter = NULL;
  63. char *so = NULL; // Shared object name
  64. // Return value will be negative on any error or failed test.
  65. // It doesn't mean that any error will break the processing.
  66. // The following vars are error statistics.
  67. unsigned int total_modules = 0; // Number of processed shared objects
  68. unsigned int total_broken_modules = 0; // Module processing errors
  69. unsigned int total_tests = 0; // Total number of tests
  70. unsigned int total_broken_tests = 0; // Something is wrong with test
  71. unsigned int total_failed_tests = 0; // Total number of failed tests
  72. unsigned int total_interrupted_tests = 0; // Number of interrupted tests
  73. unsigned int total_errors = 0; // Sum of all errors
  74. #if HAVE_LOCALE_H
  75. // Set current locale
  76. setlocale(LC_ALL, "");
  77. #endif
  78. // Parse command line options
  79. opts = opts_parse(argc, argv);
  80. if (!opts) {
  81. fprintf(stderr, "Error: Can't parse command line options\n");
  82. return -1;
  83. }
  84. // Main loop. Iterate through the list of shared objects
  85. iter = faux_list_head(opts->so_list);
  86. while ((so = faux_list_each(&iter))) {
  87. void *so_handle = NULL;
  88. char testc_tmpdir[] = "/tmp/testc-XXXXXX";
  89. // Module symbols
  90. unsigned char testc_version_major = TESTC_VERSION_MAJOR_DEFAULT;
  91. unsigned char testc_version_minor = TESTC_VERSION_MINOR_DEFAULT;
  92. unsigned char *testc_version = NULL;
  93. const char *(*testc_module)[2] = NULL;
  94. // Module statistics
  95. unsigned int module_tests = 0;
  96. unsigned int module_broken_tests = 0;
  97. unsigned int module_failed_tests = 0;
  98. unsigned int module_interrupted_tests = 0;
  99. unsigned int module_errors = 0; // Sum of all errors
  100. total_modules++; // Statistics
  101. printf("--------------------------------------------------------------------------------\n");
  102. // Open shared object
  103. so_handle = dlopen(so, RTLD_LAZY | RTLD_LOCAL);
  104. if (!so_handle) {
  105. fprintf(stderr, "Error: "
  106. "Can't open module \"%s\"... "
  107. "Skipped\n", so);
  108. total_broken_modules++; // Statistics
  109. continue;
  110. }
  111. // Get testc API version from module
  112. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MAJOR);
  113. if (!testc_version) {
  114. fprintf(stderr, "Warning: "
  115. "Can't get API version for module \"%s\"... "
  116. "Use defaults\n", so);
  117. } else {
  118. testc_version_major = *testc_version;
  119. testc_version = dlsym(so_handle,
  120. SYM_TESTC_VERSION_MINOR);
  121. if (!testc_version) {
  122. fprintf(stderr, "Warning: "
  123. "Can't get API minor version"
  124. " for module \"%s\"... "
  125. "Use '0'\n", so);
  126. testc_version_minor = 0;
  127. } else {
  128. testc_version_minor = *testc_version;
  129. }
  130. }
  131. if ((testc_version_major > TESTC_VERSION_MAJOR_DEFAULT) ||
  132. ((testc_version_major == TESTC_VERSION_MAJOR_DEFAULT) &&
  133. (testc_version_minor >TESTC_VERSION_MINOR_DEFAULT))) {
  134. fprintf(stderr, "Error: "
  135. "Unsupported API v%u.%u for module \"%s\"... "
  136. "Skipped\n",
  137. testc_version_major, testc_version_minor, so);
  138. total_broken_modules++; // Statistics
  139. continue;
  140. }
  141. // Get testing functions list from module
  142. testc_module = dlsym(so_handle, SYM_TESTC_MODULE);
  143. if (!testc_module) {
  144. fprintf(stderr, "Error: "
  145. "Can't get test list for module \"%s\"... "
  146. "Skipped\n", so);
  147. total_broken_modules++; // Statistics
  148. continue;
  149. }
  150. printf("Processing module \"%s\" v%u.%u ...\n", so,
  151. testc_version_major, testc_version_minor);
  152. // Create tmpdir for current shared object
  153. if (!mkdtemp(testc_tmpdir)) {
  154. fprintf(stderr, "Warning: "
  155. "Can't create tmp dir for module \"%s\"... "
  156. "Ignored\n", so);
  157. }
  158. if (opts->preserve_tmp) {
  159. fprintf(stderr, "Warning: "
  160. "Temp dir \"%s\" will be preserved\n",
  161. testc_tmpdir);
  162. }
  163. // Iterate through testing functions list
  164. while ((*testc_module)[0]) {
  165. const char *test_name = NULL;
  166. const char *test_desc = NULL;
  167. int (*test_sym)(void);
  168. int wstatus = 0; // Test's retval
  169. char *result_str = NULL;
  170. char *attention_str = NULL;
  171. faux_list_t *buf_list = NULL;
  172. char *tmpdir = NULL; // tmp dir for current test
  173. // Get name and description of testing function
  174. test_name = (*testc_module)[0];
  175. test_desc = (*testc_module)[1];
  176. if (!test_desc) // Description can be NULL
  177. test_desc = "";
  178. testc_module++; // Next test
  179. module_tests++; // Statistics
  180. // Get address of testing function by symbol name
  181. test_sym = (int (*)(void))dlsym(so_handle, test_name);
  182. if (!test_sym) {
  183. fprintf(stderr, "Error: "
  184. "Can't find symbol \"%s\"... "
  185. "Skipped\n", test_name);
  186. module_broken_tests++; // Statistics
  187. continue;
  188. }
  189. // Create tmp dir for current test and set TESTC_TMPDIR
  190. tmpdir = faux_str_sprintf("%s/test%03u",
  191. testc_tmpdir, module_tests);
  192. if (tmpdir) {
  193. if (mkdir(tmpdir, 0755) < 0) {
  194. fprintf(stderr, "Warning: "
  195. "Can't create temp dir \"%s\": %s\n",
  196. tmpdir, strerror(errno));
  197. }
  198. setenv(FAUX_TESTC_TMPDIR_ENV, tmpdir, 1);
  199. } else {
  200. fprintf(stderr, "Warning: "
  201. "Can't generate name for temp dir\n");
  202. }
  203. // Execute testing function
  204. wstatus = exec_test(test_sym, &buf_list, opts->debug);
  205. // Analyze testing function return code
  206. // Normal exit
  207. if (WIFEXITED(wstatus)) {
  208. // Success
  209. if (WEXITSTATUS(wstatus) == 0) {
  210. result_str = faux_str_dup("OK");
  211. attention_str = faux_str_dup("");
  212. // Failed
  213. } else {
  214. result_str = faux_str_sprintf(
  215. "FAIL (%d)",
  216. (int)((signed char)((unsigned char)WEXITSTATUS(wstatus))));
  217. attention_str = faux_str_dup("(!) ");
  218. module_failed_tests++; // Statistics
  219. }
  220. // Terminated by signal
  221. } else if (WIFSIGNALED(wstatus)) {
  222. result_str = faux_str_sprintf("SIGNAL (%d)",
  223. WTERMSIG(wstatus));
  224. attention_str = faux_str_dup("[!] ");
  225. module_interrupted_tests++; // Statistics
  226. // Stopped by unknown conditions
  227. } else {
  228. result_str = faux_str_dup("UNKNOWN");
  229. attention_str = faux_str_dup("[!] ");
  230. module_broken_tests++; // Statistics
  231. }
  232. // Print test execution report
  233. printf("%sTest #%03u %s() %s: %s\n",
  234. attention_str, module_tests,
  235. test_name, test_desc, result_str);
  236. faux_str_free(result_str);
  237. faux_str_free(attention_str);
  238. // Print test output if error or debug
  239. if (!WIFEXITED(wstatus) ||
  240. WEXITSTATUS(wstatus) != 0 ||
  241. opts->debug) {
  242. if (opts->preserve_tmp) {
  243. fprintf(stderr, "Info: "
  244. "Test's temp dir is \"%s\"\n",
  245. tmpdir);
  246. }
  247. if (faux_list_len(buf_list) > 0)
  248. printf(">>>\n");
  249. print_test_output(buf_list);
  250. if (faux_list_len(buf_list) > 0)
  251. printf("<<<\n");
  252. }
  253. faux_list_free(buf_list);
  254. // Remove test's tmp dir
  255. if (!opts->preserve_tmp)
  256. faux_rm(tmpdir);
  257. faux_str_free(tmpdir);
  258. }
  259. dlclose(so_handle);
  260. so_handle = NULL;
  261. // Remove module's tmp dir
  262. if (!opts->preserve_tmp)
  263. faux_rm(testc_tmpdir);
  264. // Report module statistics
  265. printf("\n");
  266. printf("Module tests: %u\n", module_tests);
  267. printf("Module broken tests: %u\n", module_broken_tests);
  268. printf("Module failed tests: %u\n", module_failed_tests);
  269. printf("Module interrupted tests: %u\n", module_interrupted_tests);
  270. module_errors =
  271. module_broken_tests +
  272. module_failed_tests +
  273. module_interrupted_tests;
  274. printf("Module errors: %u\n", module_errors);
  275. // Gather total statistics
  276. total_tests += module_tests;
  277. total_broken_tests += module_broken_tests;
  278. total_failed_tests += module_failed_tests;
  279. total_interrupted_tests += module_interrupted_tests;
  280. }
  281. opts_free(opts);
  282. // Report total statistics
  283. printf("================================================================================\n");
  284. printf("Total modules: %u\n", total_modules);
  285. printf("Total broken modules: %u\n", total_broken_modules);
  286. printf("Total tests: %u\n", total_tests);
  287. printf("Total broken_tests: %u\n", total_broken_tests);
  288. printf("Total failed tests: %u\n", total_failed_tests);
  289. printf("Total interrupted tests: %u\n", total_interrupted_tests);
  290. total_errors =
  291. total_broken_modules +
  292. total_broken_tests +
  293. total_failed_tests +
  294. total_interrupted_tests;
  295. printf("Total errors: %u\n", total_errors);
  296. if (total_errors > 0)
  297. return -1;
  298. return 0;
  299. }
  300. static void free_iov(struct iovec *iov)
  301. {
  302. faux_free(iov->iov_base);
  303. faux_free(iov);
  304. }
  305. static faux_list_t *read_test_output(int fd, size_t limit)
  306. {
  307. struct iovec *iov = NULL;
  308. size_t total_len = 0;
  309. faux_list_t *buf_list = NULL; // Buffer list
  310. buf_list = faux_list_new(
  311. FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  312. NULL, NULL, (void (*)(void *))free_iov);
  313. do {
  314. ssize_t bytes_readed = 0;
  315. iov = faux_zmalloc(sizeof(*iov));
  316. assert(iov);
  317. iov->iov_len = CHUNK_SIZE;
  318. iov->iov_base = faux_malloc(iov->iov_len);
  319. assert(iov->iov_base);
  320. do {
  321. bytes_readed = readv(fd, iov, 1);
  322. } while ((bytes_readed < 0) && (errno == EINTR));
  323. if (bytes_readed <= 0) { /* Error or EOF */
  324. free_iov(iov);
  325. break;
  326. }
  327. iov->iov_len = bytes_readed;
  328. faux_list_add(buf_list, iov);
  329. total_len += iov->iov_len;
  330. } while (total_len < limit);
  331. return buf_list;
  332. }
  333. static void print_test_output(faux_list_t *buf_list)
  334. {
  335. faux_list_node_t *iter = NULL;
  336. struct iovec *iov = NULL;
  337. iter = faux_list_head(buf_list);
  338. while ((iov = faux_list_each(&iter))) {
  339. faux_write_block(STDOUT_FILENO, iov->iov_base, iov->iov_len);
  340. }
  341. }
  342. /** Executes testing function
  343. *
  344. * Function fork() and executes testing function.
  345. *
  346. * @param [in] test_sym Testing function.
  347. * @param [in] buf_list
  348. * @return Testing function return value
  349. */
  350. static int exec_test(int (*test_sym)(void), faux_list_t **buf_list, bool_t debug)
  351. {
  352. pid_t pid = -1;
  353. int wstatus = -1;
  354. int pipefd[2];
  355. faux_list_t *blist = NULL;
  356. if (pipe(pipefd))
  357. return -1;
  358. pid = fork();
  359. assert(pid != -1);
  360. if (pid == -1)
  361. return -1;
  362. // Child
  363. if (pid == 0) {
  364. dup2(pipefd[1], 1);
  365. dup2(pipefd[1], 2);
  366. close(pipefd[0]);
  367. close(pipefd[1]);
  368. _exit(test_sym());
  369. }
  370. // Parent
  371. close(pipefd[1]);
  372. if (debug)
  373. fprintf(stderr, "Debug: Process ID is %d\n", pid);
  374. blist = read_test_output(pipefd[0], TEST_OUTPUT_LIMIT);
  375. // The pipe closing can lead to test interruption when output length
  376. // limit is exceeded. But it's ok because it saves us from iternal
  377. // loops. It doesn't saves from silent iternal loops.
  378. close(pipefd[0]);
  379. if (blist)
  380. *buf_list = blist;
  381. while (waitpid(pid, &wstatus, 0) != pid);
  382. return wstatus;
  383. }
  384. /** @brief Frees allocated opts_t structure
  385. *
  386. * @param [in] opts Allocated opts_t structure.
  387. */
  388. static void opts_free(opts_t *opts)
  389. {
  390. assert(opts);
  391. if (!opts)
  392. return;
  393. faux_list_free(opts->so_list);
  394. faux_free(opts);
  395. }
  396. /** @brief Allocates new opts_t structure
  397. *
  398. * Allocates structure that stores parse command line options.
  399. *
  400. * @return Allocated and initialized opts_t structure.
  401. * @warning The returned opts_t structure must be freed later by opts_free().
  402. */
  403. static opts_t *opts_new(void)
  404. {
  405. opts_t *opts = NULL;
  406. opts = faux_zmalloc(sizeof(*opts));
  407. assert(opts);
  408. if (!opts)
  409. return NULL;
  410. opts->debug = BOOL_FALSE;
  411. opts->preserve_tmp = BOOL_FALSE;
  412. // Members of list are static strings from argv so don't free() it
  413. opts->so_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  414. (faux_list_cmp_fn)strcmp, NULL, NULL);
  415. if (!opts->so_list) {
  416. opts_free(opts);
  417. return NULL;
  418. }
  419. return opts;
  420. }
  421. /** @brief Parse command line options
  422. *
  423. * Function allocates opts_t structure, parses command line options and
  424. * fills opts_t structure.
  425. *
  426. * @param [in] argc Standard argc argument.
  427. * @param [in] argv Standard argv argument.
  428. * @return Filled opts_t structure with parsed command line options.
  429. * @warning The returned opts_t structure must be freed later by opts_free().
  430. */
  431. static opts_t *opts_parse(int argc, char *argv[])
  432. {
  433. opts_t *opts = NULL;
  434. static const char *shortopts = "hvdt";
  435. #ifdef HAVE_GETOPT_LONG
  436. static const struct option longopts[] = {
  437. {"help", 0, NULL, 'h'},
  438. {"version", 0, NULL, 'v'},
  439. {"debug", 0, NULL, 'd'},
  440. {"preserve-tmp", 0, NULL, 't'},
  441. {NULL, 0, NULL, 0}
  442. };
  443. #endif
  444. opts = opts_new();
  445. if (!opts)
  446. return NULL;
  447. optind = 1;
  448. while (1) {
  449. int opt;
  450. #ifdef HAVE_GETOPT_LONG
  451. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  452. #else
  453. opt = getopt(argc, argv, shortopts);
  454. #endif
  455. if (-1 == opt)
  456. break;
  457. switch (opt) {
  458. case 'd':
  459. opts->debug = BOOL_TRUE;
  460. break;
  461. case 't':
  462. opts->preserve_tmp = BOOL_TRUE;
  463. break;
  464. case 'h':
  465. help(0, argv[0]);
  466. exit(0);
  467. break;
  468. case 'v':
  469. version(VERSION);
  470. exit(0);
  471. break;
  472. default:
  473. help(-1, argv[0]);
  474. exit(-1);
  475. break;
  476. }
  477. }
  478. if (optind < argc) {
  479. int i = 0;
  480. for (i = optind; i < argc; i++)
  481. faux_list_add(opts->so_list, argv[i]);
  482. } else {
  483. help(-1, argv[0]);
  484. exit(-1);
  485. }
  486. return opts;
  487. }
  488. /** @brief Prints help
  489. *
  490. * @param [in] status If status is not '0' consider help printing as a reaction
  491. * to error and print appropriate message. If status is '0' then print general
  492. * help information.
  493. * @param [in] argv0 The argv[0] argument i.e. programm name
  494. */
  495. static void help(int status, const char *argv0)
  496. {
  497. const char *name = NULL;
  498. if (!argv0)
  499. return;
  500. // Find the basename
  501. name = strrchr(argv0, '/');
  502. if (name)
  503. name++;
  504. else
  505. name = argv0;
  506. if (status != 0) {
  507. fprintf(stderr, "Try `%s -h' for more information.\n",
  508. name);
  509. } else {
  510. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  511. printf("Unit test helper for C code.\n");
  512. printf("Options:\n");
  513. printf("\t-v, --version\tPrint version.\n");
  514. printf("\t-h, --help\tPrint this help.\n");
  515. printf("\t-d, --debug\tDebug mode. Show output for all tests.\n");
  516. printf("\t-t, --preserve-tmp\tPreserve test's tmp files.\n");
  517. }
  518. }