testing_common.c 8.2 KB

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