testing_common.c 8.5 KB

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