testing_common.c 8.2 KB

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