testc.c 5.5 KB

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