testc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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(BOOL_FALSE, BOOL_FALSE, NULL, NULL, (void (*)(void *))faux_chunk_free);
  253. // Execute testing function
  254. wstatus = exec_test(test_sym, buf_list);
  255. // Analyze testing function return code
  256. // Normal exit
  257. if (WIFEXITED(wstatus)) {
  258. // Success
  259. if (WEXITSTATUS(wstatus) == 0) {
  260. result_str = faux_str_dup("success");
  261. attention_str = faux_str_dup("");
  262. // Failed
  263. } else {
  264. result_str = faux_str_sprintf(
  265. "failed (%d)",
  266. (int)((signed char)((unsigned char)WEXITSTATUS(wstatus))));
  267. attention_str = faux_str_dup("(!) ");
  268. module_failed_tests++; // Statistics
  269. }
  270. // Terminated by signal
  271. } else if (WIFSIGNALED(wstatus)) {
  272. result_str = faux_str_sprintf("terminated (%d)",
  273. WTERMSIG(wstatus));
  274. attention_str = faux_str_dup("[!] ");
  275. module_interrupted_tests++; // Statistics
  276. // Stopped by unknown conditions
  277. } else {
  278. result_str = faux_str_dup("unknown");
  279. attention_str = faux_str_dup("[!] ");
  280. module_broken_tests++; // Statistics
  281. }
  282. // Print test execution report
  283. printf("%sTest #%03u %s() %s: %s\n",
  284. attention_str, module_tests,
  285. test_name, test_desc, result_str);
  286. faux_str_free(result_str);
  287. faux_str_free(attention_str);
  288. // Print test output if error
  289. if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
  290. iter = faux_list_head(buf_list);
  291. while ((chunk = faux_list_each(&iter))) {
  292. faux_write(STDOUT_FILENO, faux_chunk_data(chunk), faux_chunk_len(chunk));
  293. }
  294. }
  295. faux_list_free(buf_list);
  296. }
  297. dlclose(so_handle);
  298. so_handle = NULL;
  299. // Report module statistics
  300. printf("Module tests: %u\n", module_tests);
  301. printf("Module broken tests: %u\n", module_broken_tests);
  302. printf("Module failed tests: %u\n", module_failed_tests);
  303. printf("Module interrupted tests: %u\n", module_interrupted_tests);
  304. module_errors =
  305. module_broken_tests +
  306. module_failed_tests +
  307. module_interrupted_tests;
  308. printf("Module errors: %u\n", module_errors);
  309. // Gather total statistics
  310. total_tests += module_tests;
  311. total_broken_tests += module_broken_tests;
  312. total_failed_tests += module_failed_tests;
  313. total_interrupted_tests += module_interrupted_tests;
  314. }
  315. opts_free(opts);
  316. // Report total statistics
  317. printf("================================================================================\n");
  318. printf("Total modules: %u\n", total_modules);
  319. printf("Total broken modules: %u\n", total_broken_modules);
  320. printf("Total tests: %u\n", total_tests);
  321. printf("Total broken_tests: %u\n", total_broken_tests);
  322. printf("Total failed tests: %u\n", total_failed_tests);
  323. printf("Total interrupted tests: %u\n", total_interrupted_tests);
  324. total_errors =
  325. total_broken_modules +
  326. total_broken_tests +
  327. total_failed_tests +
  328. total_interrupted_tests;
  329. printf("Total errors: %u\n", total_errors);
  330. if (total_errors > 0)
  331. return -1;
  332. return 0;
  333. }
  334. #define CHUNK_SIZE 1024
  335. static faux_list_t *read_test_output(int fd, size_t limit, faux_list_t *buf_list) {
  336. faux_chunk_t *chunk = NULL;
  337. size_t total_len = 0;
  338. do {
  339. ssize_t bytes_readed = 0;
  340. chunk = faux_chunk_new(CHUNK_SIZE);
  341. bytes_readed = faux_read(fd, faux_chunk_pos(chunk), faux_chunk_left(chunk));
  342. if (bytes_readed <= 0)
  343. break;
  344. faux_chunk_inc_len(chunk, bytes_readed);
  345. faux_list_add(buf_list, chunk);
  346. total_len += faux_chunk_len(chunk);
  347. } while((!faux_chunk_left(chunk)) && (total_len < limit));
  348. return buf_list;
  349. }
  350. static int exec_test(int (*test_sym)(void), faux_list_t *buf_list) {
  351. pid_t pid = -1;
  352. int wstatus = -1;
  353. int pipefd[2];
  354. if (pipe(pipefd))
  355. return -1;
  356. pid = fork();
  357. assert(pid != -1);
  358. if (pid == -1)
  359. return -1;
  360. // Child
  361. if (pid == 0) {
  362. dup2(pipefd[1], 1);
  363. dup2(pipefd[1], 2);
  364. close(pipefd[0]);
  365. close(pipefd[1]);
  366. _exit(test_sym());
  367. }
  368. close(pipefd[1]);
  369. read_test_output(pipefd[0], 4096, buf_list);
  370. // Parent
  371. while (waitpid(pid, &wstatus, 0) != pid);
  372. return wstatus;
  373. }
  374. static void opts_free(opts_t *opts) {
  375. assert(opts);
  376. if (!opts)
  377. return;
  378. faux_list_free(opts->so_list);
  379. faux_free(opts);
  380. }
  381. static opts_t *opts_new(void) {
  382. opts_t *opts = NULL;
  383. opts = faux_zmalloc(sizeof(*opts));
  384. assert(opts);
  385. if (!opts)
  386. return NULL;
  387. opts->debug = BOOL_FALSE;
  388. // Members of list are static strings from argv so don't free() it
  389. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  390. (faux_list_cmp_fn)strcmp, NULL, NULL);
  391. if (!opts->so_list) {
  392. opts_free(opts);
  393. return NULL;
  394. }
  395. return opts;
  396. }
  397. static opts_t *opts_parse(int argc, char *argv[]) {
  398. opts_t *opts = NULL;
  399. static const char *shortopts = "hvd";
  400. #ifdef HAVE_GETOPT_LONG
  401. static const struct option longopts[] = {
  402. {"help", 0, NULL, 'h'},
  403. {"version", 0, NULL, 'v'},
  404. {"debug", 0, NULL, 'd'},
  405. {NULL, 0, NULL, 0}
  406. };
  407. #endif
  408. opts = opts_new();
  409. if (!opts)
  410. return NULL;
  411. optind = 1;
  412. while (1) {
  413. int opt;
  414. #ifdef HAVE_GETOPT_LONG
  415. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  416. #else
  417. opt = getopt(argc, argv, shortopts);
  418. #endif
  419. if (-1 == opt)
  420. break;
  421. switch (opt) {
  422. case 'd':
  423. opts->debug = BOOL_TRUE;
  424. break;
  425. case 'h':
  426. help(0, argv[0]);
  427. exit(0);
  428. break;
  429. case 'v':
  430. version(VERSION);
  431. exit(0);
  432. break;
  433. default:
  434. help(-1, argv[0]);
  435. exit(-1);
  436. break;
  437. }
  438. }
  439. if (optind < argc) {
  440. int i = 0;
  441. for (i = optind; i < argc; i++)
  442. faux_list_add(opts->so_list, argv[i]);
  443. } else {
  444. help(-1, argv[0]);
  445. exit(-1);
  446. }
  447. return opts;
  448. }
  449. static void help(int status, const char *argv0) {
  450. const char *name = NULL;
  451. if (!argv0)
  452. return;
  453. // Find the basename
  454. name = strrchr(argv0, '/');
  455. if (name)
  456. name++;
  457. else
  458. name = argv0;
  459. if (status != 0) {
  460. fprintf(stderr, "Try `%s -h' for more information.\n",
  461. name);
  462. } else {
  463. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  464. printf("Unit test helper for C code.\n");
  465. printf("Options:\n");
  466. printf("\t-v, --version\tPrint version.\n");
  467. printf("\t-h, --help\tPrint this help.\n");
  468. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  469. }
  470. }