testc.c 11 KB

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