testc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 or debug
  291. if (!WIFEXITED(wstatus) ||
  292. WEXITSTATUS(wstatus) != 0 ||
  293. opts->debug) {
  294. iter = faux_list_head(buf_list);
  295. while ((chunk = faux_list_each(&iter))) {
  296. faux_write(STDOUT_FILENO,
  297. faux_chunk_data(chunk),
  298. faux_chunk_len(chunk));
  299. }
  300. }
  301. faux_list_free(buf_list);
  302. }
  303. dlclose(so_handle);
  304. so_handle = NULL;
  305. // Report module statistics
  306. printf("Module tests: %u\n", module_tests);
  307. printf("Module broken tests: %u\n", module_broken_tests);
  308. printf("Module failed tests: %u\n", module_failed_tests);
  309. printf("Module interrupted tests: %u\n", module_interrupted_tests);
  310. module_errors =
  311. module_broken_tests +
  312. module_failed_tests +
  313. module_interrupted_tests;
  314. printf("Module errors: %u\n", module_errors);
  315. // Gather total statistics
  316. total_tests += module_tests;
  317. total_broken_tests += module_broken_tests;
  318. total_failed_tests += module_failed_tests;
  319. total_interrupted_tests += module_interrupted_tests;
  320. }
  321. opts_free(opts);
  322. // Report total statistics
  323. printf("================================================================================\n");
  324. printf("Total modules: %u\n", total_modules);
  325. printf("Total broken modules: %u\n", total_broken_modules);
  326. printf("Total tests: %u\n", total_tests);
  327. printf("Total broken_tests: %u\n", total_broken_tests);
  328. printf("Total failed tests: %u\n", total_failed_tests);
  329. printf("Total interrupted tests: %u\n", total_interrupted_tests);
  330. total_errors =
  331. total_broken_modules +
  332. total_broken_tests +
  333. total_failed_tests +
  334. total_interrupted_tests;
  335. printf("Total errors: %u\n", total_errors);
  336. if (total_errors > 0)
  337. return -1;
  338. return 0;
  339. }
  340. #define CHUNK_SIZE 1024
  341. static faux_list_t *read_test_output(int fd, size_t limit, faux_list_t *buf_list) {
  342. faux_chunk_t *chunk = NULL;
  343. size_t total_len = 0;
  344. do {
  345. ssize_t bytes_readed = 0;
  346. chunk = faux_chunk_new(CHUNK_SIZE);
  347. bytes_readed = faux_read(fd, faux_chunk_pos(chunk), faux_chunk_left(chunk));
  348. if (bytes_readed <= 0) {
  349. faux_chunk_free(chunk);
  350. break;
  351. }
  352. faux_chunk_inc_len(chunk, bytes_readed);
  353. faux_list_add(buf_list, chunk);
  354. total_len += faux_chunk_len(chunk);
  355. } while((!faux_chunk_left(chunk)) && (total_len < limit));
  356. return buf_list;
  357. }
  358. /** Executes testing function
  359. *
  360. * Function fork() and executes testing function.
  361. *
  362. * @param [in] test_sym Testing function.
  363. * @param [in] buf_list
  364. * @return Testing function return value
  365. */
  366. static int exec_test(int (*test_sym)(void), faux_list_t *buf_list) {
  367. pid_t pid = -1;
  368. int wstatus = -1;
  369. int pipefd[2];
  370. if (pipe(pipefd))
  371. return -1;
  372. pid = fork();
  373. assert(pid != -1);
  374. if (pid == -1)
  375. return -1;
  376. // Child
  377. if (pid == 0) {
  378. dup2(pipefd[1], 1);
  379. dup2(pipefd[1], 2);
  380. close(pipefd[0]);
  381. close(pipefd[1]);
  382. _exit(test_sym());
  383. }
  384. close(pipefd[1]);
  385. read_test_output(pipefd[0], 4096, buf_list);
  386. // Parent
  387. while (waitpid(pid, &wstatus, 0) != pid);
  388. return wstatus;
  389. }
  390. /** @brief Frees allocated opts_t structure
  391. *
  392. * @param [in] opts Allocated opts_t structure.
  393. */
  394. static void opts_free(opts_t *opts) {
  395. assert(opts);
  396. if (!opts)
  397. return;
  398. faux_list_free(opts->so_list);
  399. faux_free(opts);
  400. }
  401. /** @brief Allocates new opts_t structure
  402. *
  403. * Allocates structure that stores parse command line options.
  404. *
  405. * @return Allocated and initialized opts_t structure.
  406. * @warning The returned opts_t structure must be freed later by opts_free().
  407. */
  408. static opts_t *opts_new(void) {
  409. opts_t *opts = NULL;
  410. opts = faux_zmalloc(sizeof(*opts));
  411. assert(opts);
  412. if (!opts)
  413. return NULL;
  414. opts->debug = BOOL_FALSE;
  415. // Members of list are static strings from argv so don't free() it
  416. opts->so_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  417. (faux_list_cmp_fn)strcmp, NULL, NULL);
  418. if (!opts->so_list) {
  419. opts_free(opts);
  420. return NULL;
  421. }
  422. return opts;
  423. }
  424. /** @brief Parse command line options
  425. *
  426. * Function allocates opts_t structure, parses command line options and
  427. * fills opts_t structure.
  428. *
  429. * @param [in] argc Standard argc argument.
  430. * @param [in] argv Standard argv argument.
  431. * @return Filled opts_t structure with parsed command line options.
  432. * @warning The returned opts_t structure must be freed later by opts_free().
  433. */
  434. static opts_t *opts_parse(int argc, char *argv[]) {
  435. opts_t *opts = NULL;
  436. static const char *shortopts = "hvd";
  437. #ifdef HAVE_GETOPT_LONG
  438. static const struct option longopts[] = {
  439. {"help", 0, NULL, 'h'},
  440. {"version", 0, NULL, 'v'},
  441. {"debug", 0, NULL, 'd'},
  442. {NULL, 0, NULL, 0}
  443. };
  444. #endif
  445. opts = opts_new();
  446. if (!opts)
  447. return NULL;
  448. optind = 1;
  449. while (1) {
  450. int opt;
  451. #ifdef HAVE_GETOPT_LONG
  452. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  453. #else
  454. opt = getopt(argc, argv, shortopts);
  455. #endif
  456. if (-1 == opt)
  457. break;
  458. switch (opt) {
  459. case 'd':
  460. opts->debug = BOOL_TRUE;
  461. break;
  462. case 'h':
  463. help(0, argv[0]);
  464. exit(0);
  465. break;
  466. case 'v':
  467. version(VERSION);
  468. exit(0);
  469. break;
  470. default:
  471. help(-1, argv[0]);
  472. exit(-1);
  473. break;
  474. }
  475. }
  476. if (optind < argc) {
  477. int i = 0;
  478. for (i = optind; i < argc; i++)
  479. faux_list_add(opts->so_list, argv[i]);
  480. } else {
  481. help(-1, argv[0]);
  482. exit(-1);
  483. }
  484. return opts;
  485. }
  486. /** @brief Prints help
  487. *
  488. * @param [in] status If status is not '0' consider help printing as a reaction
  489. * to error and print appropriate message. If status is '0' then print general
  490. * help information.
  491. * @param [in] argv0 The argv[0] argument i.e. programm name
  492. */
  493. static void help(int status, const char *argv0) {
  494. const char *name = NULL;
  495. if (!argv0)
  496. return;
  497. // Find the basename
  498. name = strrchr(argv0, '/');
  499. if (name)
  500. name++;
  501. else
  502. name = argv0;
  503. if (status != 0) {
  504. fprintf(stderr, "Try `%s -h' for more information.\n",
  505. name);
  506. } else {
  507. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  508. printf("Unit test helper for C code.\n");
  509. printf("Options:\n");
  510. printf("\t-v, --version\tPrint version.\n");
  511. printf("\t-h, --help\tPrint this help.\n");
  512. printf("\t-d, --debug\tDebug mode. Show output for all tests.\n");
  513. }
  514. }