testc.c 6.9 KB

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