testing_common.c 8.7 KB

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