testc.c 5.3 KB

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