testc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. so_handle = dlopen(so, RTLD_LAZY | RTLD_LOCAL);
  176. if (!so_handle) {
  177. fprintf(stderr, "Error: Can't open module \"%s\"... Skipped\n", so);
  178. total_broken_modules++; // Statistics
  179. continue;
  180. }
  181. // Get testc API version from module
  182. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MAJOR);
  183. if (!testc_version) {
  184. fprintf(stderr, "Warning: Can't get API version for module \"%s\"... Use defaults\n", so);
  185. } else {
  186. testc_version_major = *testc_version;
  187. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MINOR);
  188. if (!testc_version) {
  189. fprintf(stderr, "Warning: Can't get API minor version for module \"%s\"... Use '0'\n", so);
  190. testc_version_minor = 0;
  191. } else {
  192. testc_version_minor = *testc_version;
  193. }
  194. }
  195. if ((testc_version_major > TESTC_VERSION_MAJOR_DEFAULT) ||
  196. ((testc_version_major == TESTC_VERSION_MAJOR_DEFAULT) &&
  197. (testc_version_minor >TESTC_VERSION_MINOR_DEFAULT))) {
  198. fprintf(stderr, "Error: Unsupported API v%u.%u for module \"%s\"... Skipped\n",
  199. testc_version_major, testc_version_minor, so);
  200. total_broken_modules++; // Statistics
  201. continue;
  202. }
  203. testc_module = dlsym(so_handle, SYM_TESTC_MODULE);
  204. if (!testc_module) {
  205. fprintf(stderr, "Error: Can't get test list for module \"%s\"... Skipped\n", so);
  206. total_broken_modules++; // Statistics
  207. continue;
  208. }
  209. printf("Processing module \"%s\" v%u.%u ...\n", so,
  210. testc_version_major, testc_version_minor);
  211. while ((*testc_module)[0]) {
  212. const char *test_name = NULL;
  213. const char *test_desc = NULL;
  214. int (*test_sym)(void);
  215. int wstatus = 0;
  216. char *result_str = NULL;
  217. char *attention_str = NULL;
  218. faux_list_t *buf_list = NULL;
  219. faux_list_node_t *iter = NULL;
  220. faux_chunk_t *chunk = NULL;
  221. test_name = (*testc_module)[0];
  222. test_desc = (*testc_module)[1];
  223. if (!test_desc)
  224. test_desc = "";
  225. testc_module++;
  226. module_tests++; // Statistics
  227. test_sym = (int (*)(void))dlsym(so_handle, test_name);
  228. if (!test_sym) {
  229. fprintf(stderr, "Error: Can't find symbol \"%s\"... Skipped\n", test_name);
  230. module_broken_tests++; // Statistics
  231. continue;
  232. }
  233. buf_list = faux_list_new(BOOL_FALSE, BOOL_FALSE, NULL, NULL, (void (*)(void *))faux_chunk_free);
  234. wstatus = exec_test(test_sym, buf_list);
  235. if (WIFEXITED(wstatus)) {
  236. if (WEXITSTATUS(wstatus) == 0) {
  237. result_str = faux_str_dup("success");
  238. attention_str = faux_str_dup("");
  239. } else {
  240. result_str = faux_str_sprintf("failed (%d)",
  241. (int)((signed char)((unsigned char)WEXITSTATUS(wstatus))));
  242. attention_str = faux_str_dup("(!) ");
  243. module_failed_tests++; // Statistics
  244. }
  245. } else if (WIFSIGNALED(wstatus)) {
  246. result_str = faux_str_sprintf("terminated (%d)",
  247. WTERMSIG(wstatus));
  248. attention_str = faux_str_dup("[!] ");
  249. module_interrupted_tests++; // Statistics
  250. } else {
  251. result_str = faux_str_dup("unknown");
  252. attention_str = faux_str_dup("[!] ");
  253. module_broken_tests++; // Statistics
  254. }
  255. printf("%sTest #%03u %s() %s: %s\n", attention_str, module_tests, test_name, test_desc, result_str);
  256. faux_str_free(result_str);
  257. faux_str_free(attention_str);
  258. // Print test output if error
  259. if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
  260. iter = faux_list_head(buf_list);
  261. while ((chunk = faux_list_each(&iter))) {
  262. faux_write(STDOUT_FILENO, faux_chunk_data(chunk), faux_chunk_len(chunk));
  263. }
  264. }
  265. faux_list_free(buf_list);
  266. }
  267. dlclose(so_handle);
  268. so_handle = NULL;
  269. // Report module statistics
  270. printf("Module tests: %u\n", module_tests);
  271. printf("Module broken tests: %u\n", module_broken_tests);
  272. printf("Module failed tests: %u\n", module_failed_tests);
  273. printf("Module interrupted tests: %u\n", module_interrupted_tests);
  274. module_errors =
  275. module_broken_tests +
  276. module_failed_tests +
  277. module_interrupted_tests;
  278. printf("Module errors: %u\n", module_errors);
  279. // Gather total statistics
  280. total_tests += module_tests;
  281. total_broken_tests += module_broken_tests;
  282. total_failed_tests += module_failed_tests;
  283. total_interrupted_tests += module_interrupted_tests;
  284. }
  285. opts_free(opts);
  286. // Report total statistics
  287. printf("================================================================================\n");
  288. printf("Total modules: %u\n", total_modules);
  289. printf("Total broken modules: %u\n", total_broken_modules);
  290. printf("Total tests: %u\n", total_tests);
  291. printf("Total broken_tests: %u\n", total_broken_tests);
  292. printf("Total failed tests: %u\n", total_failed_tests);
  293. printf("Total interrupted tests: %u\n", total_interrupted_tests);
  294. total_errors =
  295. total_broken_modules +
  296. total_broken_tests +
  297. total_failed_tests +
  298. total_interrupted_tests;
  299. printf("Total errors: %u\n", total_errors);
  300. if (total_errors > 0)
  301. return -1;
  302. return 0;
  303. }
  304. #define CHUNK_SIZE 1024
  305. static faux_list_t *read_test_output(int fd, size_t limit, faux_list_t *buf_list) {
  306. faux_chunk_t *chunk = NULL;
  307. size_t total_len = 0;
  308. do {
  309. ssize_t bytes_readed = 0;
  310. chunk = faux_chunk_new(CHUNK_SIZE);
  311. bytes_readed = faux_read(fd, faux_chunk_pos(chunk), faux_chunk_left(chunk));
  312. if (bytes_readed <= 0)
  313. break;
  314. faux_chunk_inc_len(chunk, bytes_readed);
  315. faux_list_add(buf_list, chunk);
  316. total_len += faux_chunk_len(chunk);
  317. } while((!faux_chunk_left(chunk)) && (total_len < limit));
  318. return buf_list;
  319. }
  320. static int exec_test(int (*test_sym)(void), faux_list_t *buf_list) {
  321. pid_t pid = -1;
  322. int wstatus = -1;
  323. int pipefd[2];
  324. if (pipe(pipefd))
  325. return -1;
  326. pid = fork();
  327. assert(pid != -1);
  328. if (pid == -1)
  329. return -1;
  330. // Child
  331. if (pid == 0) {
  332. dup2(pipefd[1], 1);
  333. dup2(pipefd[1], 2);
  334. close(pipefd[0]);
  335. close(pipefd[1]);
  336. _exit(test_sym());
  337. }
  338. close(pipefd[1]);
  339. read_test_output(pipefd[0], 4096, buf_list);
  340. // Parent
  341. while (waitpid(pid, &wstatus, 0) != pid);
  342. return wstatus;
  343. }
  344. static void opts_free(opts_t *opts) {
  345. assert(opts);
  346. if (!opts)
  347. return;
  348. faux_list_free(opts->so_list);
  349. faux_free(opts);
  350. }
  351. static opts_t *opts_new(void) {
  352. opts_t *opts = NULL;
  353. opts = faux_zmalloc(sizeof(*opts));
  354. assert(opts);
  355. if (!opts)
  356. return NULL;
  357. opts->debug = BOOL_FALSE;
  358. // Members of list are static strings from argv so don't free() it
  359. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  360. (faux_list_cmp_fn)strcmp, NULL, NULL);
  361. if (!opts->so_list) {
  362. opts_free(opts);
  363. return NULL;
  364. }
  365. return opts;
  366. }
  367. static opts_t *opts_parse(int argc, char *argv[]) {
  368. opts_t *opts = NULL;
  369. static const char *shortopts = "hvd";
  370. #ifdef HAVE_GETOPT_LONG
  371. static const struct option longopts[] = {
  372. {"help", 0, NULL, 'h'},
  373. {"version", 0, NULL, 'v'},
  374. {"debug", 0, NULL, 'd'},
  375. {NULL, 0, NULL, 0}
  376. };
  377. #endif
  378. opts = opts_new();
  379. if (!opts)
  380. return NULL;
  381. optind = 1;
  382. while (1) {
  383. int opt;
  384. #ifdef HAVE_GETOPT_LONG
  385. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  386. #else
  387. opt = getopt(argc, argv, shortopts);
  388. #endif
  389. if (-1 == opt)
  390. break;
  391. switch (opt) {
  392. case 'd':
  393. opts->debug = BOOL_TRUE;
  394. break;
  395. case 'h':
  396. help(0, argv[0]);
  397. exit(0);
  398. break;
  399. case 'v':
  400. version(VERSION);
  401. exit(0);
  402. break;
  403. default:
  404. help(-1, argv[0]);
  405. exit(-1);
  406. break;
  407. }
  408. }
  409. if (optind < argc) {
  410. int i = 0;
  411. for (i = optind; i < argc; i++)
  412. faux_list_add(opts->so_list, argv[i]);
  413. } else {
  414. help(-1, argv[0]);
  415. exit(-1);
  416. }
  417. return opts;
  418. }
  419. static void help(int status, const char *argv0) {
  420. const char *name = NULL;
  421. if (!argv0)
  422. return;
  423. // Find the basename
  424. name = strrchr(argv0, '/');
  425. if (name)
  426. name++;
  427. else
  428. name = argv0;
  429. if (status != 0) {
  430. fprintf(stderr, "Try `%s -h' for more information.\n",
  431. name);
  432. } else {
  433. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  434. printf("Unit test helper for C code.\n");
  435. printf("Options:\n");
  436. printf("\t-v, --version\tPrint version.\n");
  437. printf("\t-h, --help\tPrint this help.\n");
  438. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  439. }
  440. }