testing_common.c 7.7 KB

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