testing_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. int res;
  150. #ifdef CACHE_GENERATED_KEYS
  151. tor_assert(idx < N_PREGEN_KEYS);
  152. if (! pregen_keys[idx]) {
  153. pregen_keys[idx] = crypto_pk_new();
  154. res = crypto_pk_generate_key(pregen_keys[idx]);
  155. tor_assert(!res);
  156. }
  157. return crypto_pk_dup_key(pregen_keys[idx]);
  158. #else
  159. crypto_pk_t *result;
  160. (void) idx;
  161. result = crypto_pk_new();
  162. res = crypto_pk_generate_key(result);
  163. tor_assert(!res);
  164. return result;
  165. #endif
  166. }
  167. /** Free all storage used for the cached key optimization. */
  168. static void
  169. free_pregenerated_keys(void)
  170. {
  171. unsigned idx;
  172. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  173. if (pregen_keys[idx]) {
  174. crypto_pk_free(pregen_keys[idx]);
  175. pregen_keys[idx] = NULL;
  176. }
  177. }
  178. }
  179. static void *
  180. passthrough_test_setup(const struct testcase_t *testcase)
  181. {
  182. return testcase->setup_data;
  183. }
  184. static int
  185. passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
  186. {
  187. (void)testcase;
  188. (void)ptr;
  189. return 1;
  190. }
  191. const struct testcase_setup_t passthrough_setup = {
  192. passthrough_test_setup, passthrough_test_cleanup
  193. };
  194. extern struct testgroup_t testgroups[];
  195. /** Main entry point for unit test code: parse the command line, and run
  196. * some unit tests. */
  197. int
  198. main(int c, const char **v)
  199. {
  200. or_options_t *options;
  201. char *errmsg = NULL;
  202. int i, i_out;
  203. int loglevel = LOG_ERR;
  204. int accel_crypto = 0;
  205. #ifdef USE_DMALLOC
  206. {
  207. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  208. tor_assert(r);
  209. }
  210. #endif
  211. update_approx_time(time(NULL));
  212. options = options_new();
  213. tor_threads_init();
  214. init_logging(1);
  215. configure_backtrace_handler(get_version());
  216. for (i_out = i = 1; i < c; ++i) {
  217. if (!strcmp(v[i], "--warn")) {
  218. loglevel = LOG_WARN;
  219. } else if (!strcmp(v[i], "--notice")) {
  220. loglevel = LOG_NOTICE;
  221. } else if (!strcmp(v[i], "--info")) {
  222. loglevel = LOG_INFO;
  223. } else if (!strcmp(v[i], "--debug")) {
  224. loglevel = LOG_DEBUG;
  225. } else if (!strcmp(v[i], "--accel")) {
  226. accel_crypto = 1;
  227. } else {
  228. v[i_out++] = v[i];
  229. }
  230. }
  231. c = i_out;
  232. {
  233. log_severity_list_t s;
  234. memset(&s, 0, sizeof(s));
  235. set_log_severity_config(loglevel, LOG_ERR, &s);
  236. add_stream_log(&s, "", fileno(stdout));
  237. }
  238. options->command = CMD_RUN_UNITTESTS;
  239. if (crypto_global_init(accel_crypto, NULL, NULL)) {
  240. printf("Can't initialize crypto subsystem; exiting.\n");
  241. return 1;
  242. }
  243. crypto_set_tls_dh_prime(NULL);
  244. crypto_seed_rng();
  245. rep_hist_init();
  246. network_init();
  247. setup_directory();
  248. options_init(options);
  249. options->DataDirectory = tor_strdup(temp_dir);
  250. options->EntryStatistics = 1;
  251. if (set_options(options, &errmsg) < 0) {
  252. printf("Failed to set initial options: %s\n", errmsg);
  253. tor_free(errmsg);
  254. return 1;
  255. }
  256. atexit(remove_directory);
  257. int have_failed = (tinytest_main(c, v, testgroups) != 0);
  258. free_pregenerated_keys();
  259. #ifdef USE_DMALLOC
  260. tor_free_all(0);
  261. dmalloc_log_unfreed();
  262. #endif
  263. if (have_failed)
  264. return 1;
  265. else
  266. return 0;
  267. }