testc.c 14 KB

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