testc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = NULL;
  65. const char *test_name = NULL;
  66. const char *test_desc = 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. test_list = (const char **)dlsym(so_handle, TESTC_LIST_SYM);
  83. while (*test_list) {
  84. int (*test_sym)(void);
  85. int retval = 0;
  86. char *result = NULL;
  87. test_name = *test_list;
  88. test_list++;
  89. if (!*test_list) // Broken test list structure
  90. break;
  91. test_desc = *test_list;
  92. test_list++;
  93. module_tests++;
  94. test_sym = (int (*)(void))dlsym(so_handle, test_name);
  95. if (!test_sym) {
  96. fprintf(stderr, "Error: Can't find symbol \"%s\"... Skipped\n", test_name);
  97. module_errors++;
  98. continue;
  99. }
  100. retval = test_sym();
  101. if (0 == retval) {
  102. result = "success";
  103. } else {
  104. result = "fail";
  105. module_errors++;
  106. }
  107. printf("Test #%03u %s() %s: %s\n", module_tests, test_name, test_desc, result);
  108. }
  109. dlclose(so_handle);
  110. so_handle = NULL;
  111. printf("Module tests: %u\n", module_tests);
  112. printf("Module errors: %u\n", module_errors);
  113. total_tests += module_tests;
  114. total_errors += module_errors;
  115. }
  116. opts_free(opts);
  117. // Total statistics
  118. printf("Total modules: %u\n", total_modules);
  119. printf("Total tests: %u\n", total_tests);
  120. printf("Total errors: %u\n", total_errors);
  121. if (total_errors > 0)
  122. return -1;
  123. return 0;
  124. }
  125. static void opts_free(opts_t *opts) {
  126. assert(opts);
  127. if (!opts)
  128. return;
  129. faux_list_free(opts->so_list);
  130. faux_free(opts);
  131. }
  132. static opts_t *opts_new(void) {
  133. opts_t *opts = NULL;
  134. opts = faux_zmalloc(sizeof(*opts));
  135. assert(opts);
  136. if (!opts)
  137. return NULL;
  138. opts->debug = BOOL_FALSE;
  139. // Members of list are static strings from argv so don't free() it
  140. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  141. (faux_list_cmp_fn)strcmp, NULL, NULL);
  142. if (!opts->so_list) {
  143. opts_free(opts);
  144. return NULL;
  145. }
  146. return opts;
  147. }
  148. static opts_t *opts_parse(int argc, char *argv[]) {
  149. opts_t *opts = NULL;
  150. static const char *shortopts = "hvd";
  151. #ifdef HAVE_GETOPT_LONG
  152. static const struct option longopts[] = {
  153. {"help", 0, NULL, 'h'},
  154. {"version", 0, NULL, 'v'},
  155. {"debug", 0, NULL, 'd'},
  156. {NULL, 0, NULL, 0}
  157. };
  158. #endif
  159. opts = opts_new();
  160. if (!opts)
  161. return NULL;
  162. optind = 1;
  163. while (1) {
  164. int opt;
  165. #ifdef HAVE_GETOPT_LONG
  166. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  167. #else
  168. opt = getopt(argc, argv, shortopts);
  169. #endif
  170. if (-1 == opt)
  171. break;
  172. switch (opt) {
  173. case 'd':
  174. opts->debug = BOOL_TRUE;
  175. break;
  176. case 'h':
  177. help(0, argv[0]);
  178. exit(0);
  179. break;
  180. case 'v':
  181. version(VERSION);
  182. exit(0);
  183. break;
  184. default:
  185. help(-1, argv[0]);
  186. exit(-1);
  187. break;
  188. }
  189. }
  190. if (optind < argc) {
  191. int i = 0;
  192. for (i = optind; i < argc; i++)
  193. faux_list_add(opts->so_list, argv[i]);
  194. } else {
  195. help(-1, argv[0]);
  196. exit(-1);
  197. }
  198. return opts;
  199. }
  200. static void help(int status, const char *argv0) {
  201. const char *name = NULL;
  202. if (!argv0)
  203. return;
  204. /* Find the basename */
  205. name = strrchr(argv0, '/');
  206. if (name)
  207. name++;
  208. else
  209. name = argv0;
  210. if (status != 0) {
  211. fprintf(stderr, "Try `%s -h' for more information.\n",
  212. name);
  213. } else {
  214. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  215. printf("Unit test helper for C code.\n");
  216. printf("Options:\n");
  217. printf("\t-v, --version\tPrint version.\n");
  218. printf("\t-h, --help\tPrint this help.\n");
  219. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  220. }
  221. }