testing_common.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. 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 "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, based on
  92. * name and suffix. If name is NULL, return the name of the testing temporary
  93. * directory. */
  94. static const char *
  95. get_fname_suffix(const char *name, const char *suffix)
  96. {
  97. static char buf[1024];
  98. setup_directory();
  99. if (!name)
  100. return temp_dir;
  101. tor_snprintf(buf,sizeof(buf),"%s/%s%s%s",temp_dir,name,suffix ? "_" : "",
  102. suffix ? suffix : "");
  103. return buf;
  104. }
  105. /** Return a filename relative to our testing temporary directory. If name is
  106. * NULL, return the name of the testing temporary directory. */
  107. const char *
  108. get_fname(const char *name)
  109. {
  110. return get_fname_suffix(name, NULL);
  111. }
  112. /** Return a filename with a random suffix, relative to our testing temporary
  113. * directory. If name is NULL, return the name of the testing temporary
  114. * directory, without any suffix. */
  115. const char *
  116. get_fname_rnd(const char *name)
  117. {
  118. char rnd[256], rnd32[256];
  119. crypto_rand(rnd, RAND_PATH_BYTES);
  120. base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
  121. return get_fname_suffix(name, rnd32);
  122. }
  123. /* Remove a directory and all of its subdirectories */
  124. static void
  125. rm_rf(const char *dir)
  126. {
  127. struct stat st;
  128. smartlist_t *elements;
  129. elements = tor_listdir(dir);
  130. if (elements) {
  131. SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
  132. char *tmp = NULL;
  133. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  134. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  135. rm_rf(tmp);
  136. } else {
  137. if (unlink(tmp)) {
  138. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  139. }
  140. }
  141. tor_free(tmp);
  142. } SMARTLIST_FOREACH_END(cp);
  143. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  144. smartlist_free(elements);
  145. }
  146. if (rmdir(dir))
  147. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  148. }
  149. /** Remove all files stored under the temporary directory, and the directory
  150. * itself. Called by atexit(). */
  151. static void
  152. remove_directory(void)
  153. {
  154. if (getpid() != temp_dir_setup_in_pid) {
  155. /* Only clean out the tempdir when the main process is exiting. */
  156. return;
  157. }
  158. rm_rf(temp_dir);
  159. }
  160. static void *
  161. passthrough_test_setup(const struct testcase_t *testcase)
  162. {
  163. /* Make sure the passthrough doesn't unintentionally fail or skip tests */
  164. tor_assert(testcase->setup_data);
  165. tor_assert(testcase->setup_data != (void*)TT_SKIP);
  166. return testcase->setup_data;
  167. }
  168. static int
  169. passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
  170. {
  171. (void)testcase;
  172. (void)ptr;
  173. return 1;
  174. }
  175. static void *
  176. ed25519_testcase_setup(const struct testcase_t *testcase)
  177. {
  178. crypto_ed25519_testing_force_impl(testcase->setup_data);
  179. return testcase->setup_data;
  180. }
  181. static int
  182. ed25519_testcase_cleanup(const struct testcase_t *testcase, void *ptr)
  183. {
  184. (void)testcase;
  185. (void)ptr;
  186. crypto_ed25519_testing_restore_impl();
  187. return 1;
  188. }
  189. const struct testcase_setup_t ed25519_test_setup = {
  190. ed25519_testcase_setup, ed25519_testcase_cleanup
  191. };
  192. const struct testcase_setup_t passthrough_setup = {
  193. passthrough_test_setup, passthrough_test_cleanup
  194. };
  195. static void
  196. an_assertion_failed(void)
  197. {
  198. tinytest_set_test_failed_();
  199. }
  200. /** Main entry point for unit test code: parse the command line, and run
  201. * some unit tests. */
  202. int
  203. main(int c, const char **v)
  204. {
  205. or_options_t *options;
  206. char *errmsg = NULL;
  207. int i, i_out;
  208. int loglevel = LOG_ERR;
  209. int accel_crypto = 0;
  210. /* We must initialise logs before we call tor_assert() */
  211. init_logging(1);
  212. #ifdef USE_DMALLOC
  213. {
  214. int r = crypto_use_tor_alloc_functions();
  215. tor_assert(r == 0);
  216. }
  217. #endif
  218. update_approx_time(time(NULL));
  219. options = options_new();
  220. tor_threads_init();
  221. tor_compress_init();
  222. network_init();
  223. monotime_init();
  224. struct tor_libevent_cfg cfg;
  225. memset(&cfg, 0, sizeof(cfg));
  226. tor_libevent_initialize(&cfg);
  227. control_initialize_event_queue();
  228. configure_backtrace_handler(get_version());
  229. for (i_out = i = 1; i < c; ++i) {
  230. if (!strcmp(v[i], "--warn")) {
  231. loglevel = LOG_WARN;
  232. } else if (!strcmp(v[i], "--notice")) {
  233. loglevel = LOG_NOTICE;
  234. } else if (!strcmp(v[i], "--info")) {
  235. loglevel = LOG_INFO;
  236. } else if (!strcmp(v[i], "--debug")) {
  237. loglevel = LOG_DEBUG;
  238. } else if (!strcmp(v[i], "--accel")) {
  239. accel_crypto = 1;
  240. } else {
  241. v[i_out++] = v[i];
  242. }
  243. }
  244. c = i_out;
  245. {
  246. log_severity_list_t s;
  247. memset(&s, 0, sizeof(s));
  248. set_log_severity_config(loglevel, LOG_ERR, &s);
  249. /* ALWAYS log bug warnings. */
  250. s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
  251. add_stream_log(&s, "", fileno(stdout));
  252. }
  253. options->command = CMD_RUN_UNITTESTS;
  254. if (crypto_global_init(accel_crypto, NULL, NULL)) {
  255. printf("Can't initialize crypto subsystem; exiting.\n");
  256. return 1;
  257. }
  258. crypto_set_tls_dh_prime();
  259. if (crypto_seed_rng() < 0) {
  260. printf("Couldn't seed RNG; exiting.\n");
  261. return 1;
  262. }
  263. rep_hist_init();
  264. setup_directory();
  265. options_init(options);
  266. options->DataDirectory = tor_strdup(temp_dir);
  267. options->EntryStatistics = 1;
  268. if (set_options(options, &errmsg) < 0) {
  269. printf("Failed to set initial options: %s\n", errmsg);
  270. tor_free(errmsg);
  271. return 1;
  272. }
  273. tor_set_failed_assertion_callback(an_assertion_failed);
  274. init_pregenerated_keys();
  275. atexit(remove_directory);
  276. int have_failed = (tinytest_main(c, v, testgroups) != 0);
  277. free_pregenerated_keys();
  278. #ifdef USE_DMALLOC
  279. tor_free_all(0);
  280. dmalloc_log_unfreed();
  281. #endif
  282. crypto_global_cleanup();
  283. if (have_failed)
  284. return 1;
  285. else
  286. return 0;
  287. }