testc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <unistd.h>
  9. #include <dlfcn.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #if WITH_INTERNAL_GETOPT
  13. #include "libc/getopt.h"
  14. #else
  15. #ifdef HAVE_GETOPT_H
  16. #include <getopt.h>
  17. #endif
  18. #endif
  19. #if HAVE_LOCALE_H
  20. #include <locale.h>
  21. #endif
  22. #if HAVE_LANGINFO_CODESET
  23. #include <langinfo.h>
  24. #endif
  25. #include "faux/faux.h"
  26. #include "faux/str.h"
  27. #include "faux/list.h"
  28. #ifndef VERSION
  29. #define VERSION 1.0.0
  30. #endif
  31. #define QUOTE(t) #t
  32. #define version(v) printf("%s\n", v)
  33. // Version of testc API (not version of programm)
  34. #define TESTC_VERSION_MAJOR_DEFAULT 1
  35. #define TESTC_VERSION_MINOR_DEFAULT 0
  36. #define SYM_TESTC_VERSION_MAJOR "testc_version_major"
  37. #define SYM_TESTC_VERSION_MINOR "testc_version_minor"
  38. #define SYM_TESTC_MODULE "testc_module"
  39. // Command line options */
  40. struct opts_s {
  41. int debug;
  42. faux_list_t *so_list;
  43. };
  44. typedef struct opts_s opts_t;
  45. static opts_t *opts_parse(int argc, char *argv[]);
  46. static void opts_free(opts_t *opts);
  47. static void help(int status, const char *argv0);
  48. static int exec_test(int (*test_sym)(void));
  49. int main(int argc, char *argv[]) {
  50. opts_t *opts = NULL;
  51. faux_list_node_t *iter = NULL;
  52. char *so = NULL;
  53. // Return value will be negative on any error or failed test.
  54. // It doesn't mean that any error will break the processing.
  55. // The following var is error counter.
  56. unsigned int total_errors = 0;
  57. unsigned int total_modules = 0;
  58. unsigned int total_tests = 0;
  59. #if HAVE_LOCALE_H
  60. // Set current locale
  61. setlocale(LC_ALL, "");
  62. #endif
  63. // Parse command line options
  64. opts = opts_parse(argc, argv);
  65. if (!opts) {
  66. fprintf(stderr, "Error: Can't parse command line options\n");
  67. return -1;
  68. }
  69. iter = faux_list_head(opts->so_list);
  70. while ((so = faux_list_each(&iter))) {
  71. void *so_handle = NULL;
  72. // Module symbols
  73. unsigned char testc_version_major = TESTC_VERSION_MAJOR_DEFAULT;
  74. unsigned char testc_version_minor = TESTC_VERSION_MINOR_DEFAULT;
  75. unsigned char *testc_version = NULL;
  76. const char *(*testc_module)[2] = NULL;
  77. // Module counters
  78. unsigned int module_tests = 0;
  79. unsigned int module_errors = 0;
  80. printf("--------------------------------------------------------------------------------\n");
  81. if (access(so, R_OK) < 0) {
  82. fprintf(stderr, "Error: Can't read module \"%s\"... Skipped\n", so);
  83. total_errors++;
  84. continue;
  85. }
  86. so_handle = dlopen(so, RTLD_LAZY | RTLD_LOCAL);
  87. if (!so_handle) {
  88. fprintf(stderr, "Error: Can't open module \"%s\"... Skipped\n", so);
  89. total_errors++;
  90. continue;
  91. }
  92. // Get testc API version from module
  93. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MAJOR);
  94. if (!testc_version) {
  95. fprintf(stderr, "Warning: Can't get API version for module \"%s\"... Use defaults\n", so);
  96. } else {
  97. testc_version_major = *testc_version;
  98. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MINOR);
  99. if (!testc_version) {
  100. fprintf(stderr, "Warning: Can't get API minor version for module \"%s\"... Use '0'\n", so);
  101. testc_version_minor = 0;
  102. } else {
  103. testc_version_minor = *testc_version;
  104. }
  105. }
  106. if ((testc_version_major > TESTC_VERSION_MAJOR_DEFAULT) ||
  107. ((testc_version_major == TESTC_VERSION_MAJOR_DEFAULT) &&
  108. (testc_version_minor >TESTC_VERSION_MINOR_DEFAULT))) {
  109. fprintf(stderr, "Error: Unsupported API v%u.%u for module \"%s\"... Skipped\n",
  110. testc_version_major, testc_version_minor, so);
  111. continue;
  112. }
  113. testc_module = dlsym(so_handle, SYM_TESTC_MODULE);
  114. if (!testc_module) {
  115. fprintf(stderr, "Error: Can't get test list for module \"%s\"... Skipped\n", so);
  116. total_errors++;
  117. continue;
  118. }
  119. total_modules++;
  120. printf("Processing module \"%s\" v%u.%u ...\n", so,
  121. testc_version_major, testc_version_minor);
  122. while ((*testc_module)[0]) {
  123. const char *test_name = NULL;
  124. const char *test_desc = NULL;
  125. int (*test_sym)(void);
  126. int wstatus = 0;
  127. char *result_str = NULL;
  128. char *attention_str = NULL;
  129. test_name = (*testc_module)[0];
  130. test_desc = (*testc_module)[1];
  131. if (!test_desc)
  132. test_desc = "";
  133. testc_module++;
  134. module_tests++;
  135. test_sym = (int (*)(void))dlsym(so_handle, test_name);
  136. if (!test_sym) {
  137. fprintf(stderr, "Error: Can't find symbol \"%s\"... Skipped\n", test_name);
  138. module_errors++;
  139. continue;
  140. }
  141. wstatus = exec_test(test_sym);
  142. if (WIFEXITED(wstatus)) {
  143. if (WEXITSTATUS(wstatus) == 0) {
  144. result_str = faux_str_dup("success");
  145. attention_str = faux_str_dup("");
  146. } else {
  147. result_str = faux_str_sprintf("failed (%d)",
  148. (int)((signed char)((unsigned char)WEXITSTATUS(wstatus))));
  149. attention_str = faux_str_dup("(!) ");
  150. module_errors++;
  151. }
  152. } else if (WIFSIGNALED(wstatus)) {
  153. result_str = faux_str_sprintf("terminated (%d)",
  154. WTERMSIG(wstatus));
  155. attention_str = faux_str_dup("[!] ");
  156. module_errors++;
  157. } else {
  158. result_str = faux_str_dup("unknown");
  159. attention_str = faux_str_dup("[!] ");
  160. module_errors++;
  161. }
  162. printf("%sTest #%03u %s() %s: %s\n", attention_str, module_tests, test_name, test_desc, result_str);
  163. faux_str_free(result_str);
  164. faux_str_free(attention_str);
  165. }
  166. dlclose(so_handle);
  167. so_handle = NULL;
  168. printf("Module tests: %u\n", module_tests);
  169. printf("Module errors: %u\n", module_errors);
  170. total_tests += module_tests;
  171. total_errors += module_errors;
  172. }
  173. opts_free(opts);
  174. // Total statistics
  175. printf("================================================================================\n");
  176. printf("Total modules: %u\n", total_modules);
  177. printf("Total tests: %u\n", total_tests);
  178. printf("Total errors: %u\n", total_errors);
  179. if (total_errors > 0)
  180. return -1;
  181. return 0;
  182. }
  183. static int exec_test(int (*test_sym)(void)) {
  184. pid_t pid = -1;
  185. int wstatus = -1;
  186. pid = fork();
  187. assert(pid != -1);
  188. if (pid == -1)
  189. return -1;
  190. // Child
  191. if (pid == 0)
  192. _exit(test_sym());
  193. // Parent
  194. while (waitpid(pid, &wstatus, 0) != pid);
  195. return wstatus;
  196. }
  197. static void opts_free(opts_t *opts) {
  198. assert(opts);
  199. if (!opts)
  200. return;
  201. faux_list_free(opts->so_list);
  202. faux_free(opts);
  203. }
  204. static opts_t *opts_new(void) {
  205. opts_t *opts = NULL;
  206. opts = faux_zmalloc(sizeof(*opts));
  207. assert(opts);
  208. if (!opts)
  209. return NULL;
  210. opts->debug = BOOL_FALSE;
  211. // Members of list are static strings from argv so don't free() it
  212. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  213. (faux_list_cmp_fn)strcmp, NULL, NULL);
  214. if (!opts->so_list) {
  215. opts_free(opts);
  216. return NULL;
  217. }
  218. return opts;
  219. }
  220. static opts_t *opts_parse(int argc, char *argv[]) {
  221. opts_t *opts = NULL;
  222. static const char *shortopts = "hvd";
  223. #ifdef HAVE_GETOPT_LONG
  224. static const struct option longopts[] = {
  225. {"help", 0, NULL, 'h'},
  226. {"version", 0, NULL, 'v'},
  227. {"debug", 0, NULL, 'd'},
  228. {NULL, 0, NULL, 0}
  229. };
  230. #endif
  231. opts = opts_new();
  232. if (!opts)
  233. return NULL;
  234. optind = 1;
  235. while (1) {
  236. int opt;
  237. #ifdef HAVE_GETOPT_LONG
  238. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  239. #else
  240. opt = getopt(argc, argv, shortopts);
  241. #endif
  242. if (-1 == opt)
  243. break;
  244. switch (opt) {
  245. case 'd':
  246. opts->debug = BOOL_TRUE;
  247. break;
  248. case 'h':
  249. help(0, argv[0]);
  250. exit(0);
  251. break;
  252. case 'v':
  253. version(VERSION);
  254. exit(0);
  255. break;
  256. default:
  257. help(-1, argv[0]);
  258. exit(-1);
  259. break;
  260. }
  261. }
  262. if (optind < argc) {
  263. int i = 0;
  264. for (i = optind; i < argc; i++)
  265. faux_list_add(opts->so_list, argv[i]);
  266. } else {
  267. help(-1, argv[0]);
  268. exit(-1);
  269. }
  270. return opts;
  271. }
  272. static void help(int status, const char *argv0) {
  273. const char *name = NULL;
  274. if (!argv0)
  275. return;
  276. // Find the basename
  277. name = strrchr(argv0, '/');
  278. if (name)
  279. name++;
  280. else
  281. name = argv0;
  282. if (status != 0) {
  283. fprintf(stderr, "Try `%s -h' for more information.\n",
  284. name);
  285. } else {
  286. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  287. printf("Unit test helper for C code.\n");
  288. printf("Options:\n");
  289. printf("\t-v, --version\tPrint version.\n");
  290. printf("\t-h, --help\tPrint this help.\n");
  291. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  292. }
  293. }