testing_common.c 8.3 KB

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