testc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. so_handle = dlopen(so, RTLD_LAZY | RTLD_LOCAL);
  82. if (!so_handle) {
  83. fprintf(stderr, "Error: Can't open module \"%s\"... Skipped\n", so);
  84. total_errors++;
  85. continue;
  86. }
  87. // Get testc API version from module
  88. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MAJOR);
  89. if (!testc_version) {
  90. fprintf(stderr, "Warning: Can't get API version for module \"%s\"... Use defaults\n", so);
  91. } else {
  92. testc_version_major = *testc_version;
  93. testc_version = dlsym(so_handle, SYM_TESTC_VERSION_MINOR);
  94. if (!testc_version) {
  95. fprintf(stderr, "Warning: Can't get API minor version for module \"%s\"... Use '0'\n", so);
  96. testc_version_minor = 0;
  97. } else {
  98. testc_version_minor = *testc_version;
  99. }
  100. }
  101. if ((testc_version_major > TESTC_VERSION_MAJOR_DEFAULT) ||
  102. ((testc_version_major == TESTC_VERSION_MAJOR_DEFAULT) &&
  103. (testc_version_minor >TESTC_VERSION_MINOR_DEFAULT))) {
  104. fprintf(stderr, "Error: Unsupported API v%u.%u for module \"%s\"... Skipped\n",
  105. testc_version_major, testc_version_minor, so);
  106. continue;
  107. }
  108. testc_module = dlsym(so_handle, SYM_TESTC_MODULE);
  109. if (!testc_module) {
  110. fprintf(stderr, "Error: Can't get test list for module \"%s\"... Skipped\n", so);
  111. total_errors++;
  112. continue;
  113. }
  114. total_modules++;
  115. printf("Processing module \"%s\" v%u.%u ...\n", so,
  116. testc_version_major, testc_version_minor);
  117. while ((*testc_module)[0]) {
  118. const char *test_name = NULL;
  119. const char *test_desc = NULL;
  120. int (*test_sym)(void);
  121. int wstatus = 0;
  122. char *result_str = NULL;
  123. char *attention_str = NULL;
  124. test_name = (*testc_module)[0];
  125. test_desc = (*testc_module)[1];
  126. if (!test_desc)
  127. test_desc = "";
  128. testc_module++;
  129. module_tests++;
  130. test_sym = (int (*)(void))dlsym(so_handle, test_name);
  131. if (!test_sym) {
  132. fprintf(stderr, "Error: Can't find symbol \"%s\"... Skipped\n", test_name);
  133. module_errors++;
  134. continue;
  135. }
  136. wstatus = exec_test(test_sym);
  137. if (WIFEXITED(wstatus)) {
  138. if (WEXITSTATUS(wstatus) == 0) {
  139. result_str = faux_str_dup("success");
  140. attention_str = faux_str_dup("");
  141. } else {
  142. result_str = faux_str_sprintf("failed (%d)",
  143. (int)((signed char)((unsigned char)WEXITSTATUS(wstatus))));
  144. attention_str = faux_str_dup("(!) ");
  145. module_errors++;
  146. }
  147. } else if (WIFSIGNALED(wstatus)) {
  148. result_str = faux_str_sprintf("terminated (%d)",
  149. WTERMSIG(wstatus));
  150. attention_str = faux_str_dup("[!] ");
  151. module_errors++;
  152. } else {
  153. result_str = faux_str_dup("unknown");
  154. attention_str = faux_str_dup("[!] ");
  155. module_errors++;
  156. }
  157. printf("%sTest #%03u %s() %s: %s\n", attention_str, module_tests, test_name, test_desc, result_str);
  158. faux_str_free(result_str);
  159. faux_str_free(attention_str);
  160. }
  161. dlclose(so_handle);
  162. so_handle = NULL;
  163. printf("Module tests: %u\n", module_tests);
  164. printf("Module errors: %u\n", module_errors);
  165. total_tests += module_tests;
  166. total_errors += module_errors;
  167. }
  168. opts_free(opts);
  169. // Total statistics
  170. printf("================================================================================\n");
  171. printf("Total modules: %u\n", total_modules);
  172. printf("Total tests: %u\n", total_tests);
  173. printf("Total errors: %u\n", total_errors);
  174. if (total_errors > 0)
  175. return -1;
  176. return 0;
  177. }
  178. static int exec_test(int (*test_sym)(void)) {
  179. pid_t pid = -1;
  180. int wstatus = -1;
  181. pid = fork();
  182. assert(pid != -1);
  183. if (pid == -1)
  184. return -1;
  185. // Child
  186. if (pid == 0)
  187. _exit(test_sym());
  188. // Parent
  189. while (waitpid(pid, &wstatus, 0) != pid);
  190. return wstatus;
  191. }
  192. static void opts_free(opts_t *opts) {
  193. assert(opts);
  194. if (!opts)
  195. return;
  196. faux_list_free(opts->so_list);
  197. faux_free(opts);
  198. }
  199. static opts_t *opts_new(void) {
  200. opts_t *opts = NULL;
  201. opts = faux_zmalloc(sizeof(*opts));
  202. assert(opts);
  203. if (!opts)
  204. return NULL;
  205. opts->debug = BOOL_FALSE;
  206. // Members of list are static strings from argv so don't free() it
  207. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  208. (faux_list_cmp_fn)strcmp, NULL, NULL);
  209. if (!opts->so_list) {
  210. opts_free(opts);
  211. return NULL;
  212. }
  213. return opts;
  214. }
  215. static opts_t *opts_parse(int argc, char *argv[]) {
  216. opts_t *opts = NULL;
  217. static const char *shortopts = "hvd";
  218. #ifdef HAVE_GETOPT_LONG
  219. static const struct option longopts[] = {
  220. {"help", 0, NULL, 'h'},
  221. {"version", 0, NULL, 'v'},
  222. {"debug", 0, NULL, 'd'},
  223. {NULL, 0, NULL, 0}
  224. };
  225. #endif
  226. opts = opts_new();
  227. if (!opts)
  228. return NULL;
  229. optind = 1;
  230. while (1) {
  231. int opt;
  232. #ifdef HAVE_GETOPT_LONG
  233. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  234. #else
  235. opt = getopt(argc, argv, shortopts);
  236. #endif
  237. if (-1 == opt)
  238. break;
  239. switch (opt) {
  240. case 'd':
  241. opts->debug = BOOL_TRUE;
  242. break;
  243. case 'h':
  244. help(0, argv[0]);
  245. exit(0);
  246. break;
  247. case 'v':
  248. version(VERSION);
  249. exit(0);
  250. break;
  251. default:
  252. help(-1, argv[0]);
  253. exit(-1);
  254. break;
  255. }
  256. }
  257. if (optind < argc) {
  258. int i = 0;
  259. for (i = optind; i < argc; i++)
  260. faux_list_add(opts->so_list, argv[i]);
  261. } else {
  262. help(-1, argv[0]);
  263. exit(-1);
  264. }
  265. return opts;
  266. }
  267. static void help(int status, const char *argv0) {
  268. const char *name = NULL;
  269. if (!argv0)
  270. return;
  271. // Find the basename
  272. name = strrchr(argv0, '/');
  273. if (name)
  274. name++;
  275. else
  276. name = argv0;
  277. if (status != 0) {
  278. fprintf(stderr, "Try `%s -h' for more information.\n",
  279. name);
  280. } else {
  281. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  282. printf("Unit test helper for C code.\n");
  283. printf("Options:\n");
  284. printf("\t-v, --version\tPrint version.\n");
  285. printf("\t-h, --help\tPrint this help.\n");
  286. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  287. }
  288. }