testing_common.c 9.1 KB

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