testing_common.c 8.7 KB

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