testc.c 13 KB

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