testing_common.c 7.3 KB

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