testing_common.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  6. * since we're not linking to tor_main.c */
  7. const char tor_git_revision[] = "";
  8. /**
  9. * \file test_common.c
  10. * \brief Common pieces to implement unit tests.
  11. **/
  12. #include "orconfig.h"
  13. #include "or.h"
  14. #include "control.h"
  15. #include "config.h"
  16. #include "rephist.h"
  17. #include "backtrace.h"
  18. #include "test.h"
  19. #include <stdio.h>
  20. #ifdef HAVE_FCNTL_H
  21. #include <fcntl.h>
  22. #endif
  23. #ifdef _WIN32
  24. /* For mkdir() */
  25. #include <direct.h>
  26. #else
  27. #include <dirent.h>
  28. #endif
  29. #include "or.h"
  30. #ifdef USE_DMALLOC
  31. #include <dmalloc.h>
  32. #include <openssl/crypto.h>
  33. #include "main.h"
  34. #endif
  35. /** Temporary directory (set up by setup_directory) under which we store all
  36. * our files during testing. */
  37. static char temp_dir[256];
  38. #ifdef _WIN32
  39. #define pid_t int
  40. #endif
  41. static pid_t temp_dir_setup_in_pid = 0;
  42. /** Select and create the temporary directory we'll use to run our unit tests.
  43. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  44. * idempotent. */
  45. static void
  46. setup_directory(void)
  47. {
  48. static int is_setup = 0;
  49. int r;
  50. char rnd[256], rnd32[256];
  51. if (is_setup) return;
  52. /* Due to base32 limitation needs to be a multiple of 5. */
  53. #define RAND_PATH_BYTES 5
  54. crypto_rand(rnd, RAND_PATH_BYTES);
  55. base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
  56. #ifdef _WIN32
  57. {
  58. char buf[MAX_PATH];
  59. const char *tmp = buf;
  60. const char *extra_backslash = "";
  61. /* If this fails, we're probably screwed anyway */
  62. if (!GetTempPathA(sizeof(buf),buf))
  63. tmp = "c:\\windows\\temp\\";
  64. if (strcmpend(tmp, "\\")) {
  65. /* According to MSDN, it should be impossible for GetTempPath to give us
  66. * an answer that doesn't end with \. But let's make sure. */
  67. extra_backslash = "\\";
  68. }
  69. tor_snprintf(temp_dir, sizeof(temp_dir),
  70. "%s%stor_test_%d_%s", tmp, extra_backslash,
  71. (int)getpid(), rnd32);
  72. r = mkdir(temp_dir);
  73. }
  74. #else
  75. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
  76. (int) getpid(), rnd32);
  77. r = mkdir(temp_dir, 0700);
  78. if (!r) {
  79. /* undo sticky bit so tests don't get confused. */
  80. r = chown(temp_dir, getuid(), getgid());
  81. }
  82. #endif
  83. if (r) {
  84. fprintf(stderr, "Can't create directory %s:", temp_dir);
  85. perror("");
  86. exit(1);
  87. }
  88. is_setup = 1;
  89. temp_dir_setup_in_pid = getpid();
  90. }
  91. /** Return a filename relative to our testing temporary directory */
  92. const char *
  93. get_fname(const char *name)
  94. {
  95. static char buf[1024];
  96. setup_directory();
  97. if (!name)
  98. return temp_dir;
  99. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  100. return buf;
  101. }
  102. /* Remove a directory and all of its subdirectories */
  103. static void
  104. rm_rf(const char *dir)
  105. {
  106. struct stat st;
  107. smartlist_t *elements;
  108. elements = tor_listdir(dir);
  109. if (elements) {
  110. SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
  111. char *tmp = NULL;
  112. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  113. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  114. rm_rf(tmp);
  115. } else {
  116. if (unlink(tmp)) {
  117. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  118. }
  119. }
  120. tor_free(tmp);
  121. } SMARTLIST_FOREACH_END(cp);
  122. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  123. smartlist_free(elements);
  124. }
  125. if (rmdir(dir))
  126. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  127. }
  128. /** Remove all files stored under the temporary directory, and the directory
  129. * itself. Called by atexit(). */
  130. static void
  131. remove_directory(void)
  132. {
  133. if (getpid() != temp_dir_setup_in_pid) {
  134. /* Only clean out the tempdir when the main process is exiting. */
  135. return;
  136. }
  137. rm_rf(temp_dir);
  138. }
  139. /** Define this if unit tests spend too much time generating public keys*/
  140. #undef CACHE_GENERATED_KEYS
  141. static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
  142. #define N_PREGEN_KEYS ARRAY_LENGTH(pregen_keys)
  143. /** Generate and return a new keypair for use in unit tests. If we're using
  144. * the key cache optimization, we might reuse keys: we only guarantee that
  145. * keys made with distinct values for <b>idx</b> are different. The value of
  146. * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
  147. crypto_pk_t *
  148. pk_generate(int idx)
  149. {
  150. int res;
  151. #ifdef CACHE_GENERATED_KEYS
  152. tor_assert(idx < N_PREGEN_KEYS);
  153. if (! pregen_keys[idx]) {
  154. pregen_keys[idx] = crypto_pk_new();
  155. res = crypto_pk_generate_key(pregen_keys[idx]);
  156. tor_assert(!res);
  157. }
  158. return crypto_pk_dup_key(pregen_keys[idx]);
  159. #else
  160. crypto_pk_t *result;
  161. (void) idx;
  162. result = crypto_pk_new();
  163. res = crypto_pk_generate_key(result);
  164. tor_assert(!res);
  165. return result;
  166. #endif
  167. }
  168. /** Free all storage used for the cached key optimization. */
  169. static void
  170. free_pregenerated_keys(void)
  171. {
  172. unsigned idx;
  173. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  174. if (pregen_keys[idx]) {
  175. crypto_pk_free(pregen_keys[idx]);
  176. pregen_keys[idx] = NULL;
  177. }
  178. }
  179. }
  180. static void *
  181. passthrough_test_setup(const struct testcase_t *testcase)
  182. {
  183. return testcase->setup_data;
  184. }
  185. static int
  186. passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
  187. {
  188. (void)testcase;
  189. (void)ptr;
  190. return 1;
  191. }
  192. const struct testcase_setup_t passthrough_setup = {
  193. passthrough_test_setup, passthrough_test_cleanup
  194. };
  195. extern struct testgroup_t testgroups[];
  196. /** Main entry point for unit test code: parse the command line, and run
  197. * some unit tests. */
  198. int
  199. main(int c, const char **v)
  200. {
  201. or_options_t *options;
  202. char *errmsg = NULL;
  203. int i, i_out;
  204. int loglevel = LOG_ERR;
  205. int accel_crypto = 0;
  206. #ifdef USE_DMALLOC
  207. {
  208. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  209. tor_assert(r);
  210. }
  211. #endif
  212. update_approx_time(time(NULL));
  213. options = options_new();
  214. tor_threads_init();
  215. control_initialize_event_queue();
  216. init_logging(1);
  217. configure_backtrace_handler(get_version());
  218. for (i_out = i = 1; i < c; ++i) {
  219. if (!strcmp(v[i], "--warn")) {
  220. loglevel = LOG_WARN;
  221. } else if (!strcmp(v[i], "--notice")) {
  222. loglevel = LOG_NOTICE;
  223. } else if (!strcmp(v[i], "--info")) {
  224. loglevel = LOG_INFO;
  225. } else if (!strcmp(v[i], "--debug")) {
  226. loglevel = LOG_DEBUG;
  227. } else if (!strcmp(v[i], "--accel")) {
  228. accel_crypto = 1;
  229. } else {
  230. v[i_out++] = v[i];
  231. }
  232. }
  233. c = i_out;
  234. {
  235. log_severity_list_t s;
  236. memset(&s, 0, sizeof(s));
  237. set_log_severity_config(loglevel, LOG_ERR, &s);
  238. add_stream_log(&s, "", fileno(stdout));
  239. }
  240. options->command = CMD_RUN_UNITTESTS;
  241. if (crypto_global_init(accel_crypto, NULL, NULL)) {
  242. printf("Can't initialize crypto subsystem; exiting.\n");
  243. return 1;
  244. }
  245. crypto_set_tls_dh_prime();
  246. if (crypto_seed_rng() < 0) {
  247. printf("Couldn't seed RNG; exiting.\n");
  248. return 1;
  249. }
  250. rep_hist_init();
  251. network_init();
  252. setup_directory();
  253. options_init(options);
  254. options->DataDirectory = tor_strdup(temp_dir);
  255. options->EntryStatistics = 1;
  256. if (set_options(options, &errmsg) < 0) {
  257. printf("Failed to set initial options: %s\n", errmsg);
  258. tor_free(errmsg);
  259. return 1;
  260. }
  261. atexit(remove_directory);
  262. int have_failed = (tinytest_main(c, v, testgroups) != 0);
  263. free_pregenerated_keys();
  264. #ifdef USE_DMALLOC
  265. tor_free_all(0);
  266. dmalloc_log_unfreed();
  267. #endif
  268. crypto_global_cleanup();
  269. if (have_failed)
  270. return 1;
  271. else
  272. return 0;
  273. }