testing_common.c 8.6 KB

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