Parallelize src/test/test into chunks.
[tor.git] / src / test / testing_common.c
blobdaa7aa524a077ac9282a767aecda7e4812ca7c11
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 */
6 /**
7 * \file test_common.c
8 * \brief Common pieces to implement unit tests.
9 **/
11 #define MAINLOOP_PRIVATE
12 #include "orconfig.h"
13 #include "core/or/or.h"
14 #include "feature/control/control.h"
15 #include "app/config/config.h"
16 #include "lib/crypt_ops/crypto_dh.h"
17 #include "lib/crypt_ops/crypto_ed25519.h"
18 #include "lib/crypt_ops/crypto_rand.h"
19 #include "feature/stats/predict_ports.h"
20 #include "feature/stats/rephist.h"
21 #include "lib/err/backtrace.h"
22 #include "test/test.h"
23 #include "core/or/channelpadding.h"
24 #include "core/mainloop/mainloop.h"
25 #include "lib/compress/compress.h"
26 #include "lib/evloop/compat_libevent.h"
27 #include "lib/crypt_ops/crypto_init.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
40 #ifdef _WIN32
41 /* For mkdir() */
42 #include <direct.h>
43 #else
44 #include <dirent.h>
45 #endif /* defined(_WIN32) */
47 /** Temporary directory (set up by setup_directory) under which we store all
48 * our files during testing. */
49 static char temp_dir[256];
50 #ifdef _WIN32
51 #define pid_t int
52 #endif
53 static pid_t temp_dir_setup_in_pid = 0;
55 /** Select and create the temporary directory we'll use to run our unit tests.
56 * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
57 * idempotent. */
58 static void
59 setup_directory(void)
61 static int is_setup = 0;
62 int r;
63 char rnd[256], rnd32[256];
64 if (is_setup) return;
66 /* Due to base32 limitation needs to be a multiple of 5. */
67 #define RAND_PATH_BYTES 5
68 crypto_rand(rnd, RAND_PATH_BYTES);
69 base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
71 #ifdef _WIN32
73 char buf[MAX_PATH];
74 const char *tmp = buf;
75 const char *extra_backslash = "";
76 /* If this fails, we're probably screwed anyway */
77 if (!GetTempPathA(sizeof(buf),buf))
78 tmp = "c:\\windows\\temp\\";
79 if (strcmpend(tmp, "\\")) {
80 /* According to MSDN, it should be impossible for GetTempPath to give us
81 * an answer that doesn't end with \. But let's make sure. */
82 extra_backslash = "\\";
84 tor_snprintf(temp_dir, sizeof(temp_dir),
85 "%s%stor_test_%d_%s", tmp, extra_backslash,
86 (int)getpid(), rnd32);
87 r = mkdir(temp_dir);
89 #else /* !(defined(_WIN32)) */
90 tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
91 (int) getpid(), rnd32);
92 r = mkdir(temp_dir, 0700);
93 if (!r) {
94 /* undo sticky bit so tests don't get confused. */
95 r = chown(temp_dir, getuid(), getgid());
97 #endif /* defined(_WIN32) */
98 if (r) {
99 fprintf(stderr, "Can't create directory %s:", temp_dir);
100 perror("");
101 exit(1);
103 is_setup = 1;
104 temp_dir_setup_in_pid = getpid();
107 /** Return a filename relative to our testing temporary directory, based on
108 * name and suffix. If name is NULL, return the name of the testing temporary
109 * directory. */
110 static const char *
111 get_fname_suffix(const char *name, const char *suffix)
113 static char buf[1024];
114 setup_directory();
115 if (!name)
116 return temp_dir;
117 tor_snprintf(buf,sizeof(buf),"%s%s%s%s%s", temp_dir, PATH_SEPARATOR, name,
118 suffix ? "_" : "", suffix ? suffix : "");
119 return buf;
122 /** Return a filename relative to our testing temporary directory. If name is
123 * NULL, return the name of the testing temporary directory. */
124 const char *
125 get_fname(const char *name)
127 return get_fname_suffix(name, NULL);
130 /** Return a filename with a random suffix, relative to our testing temporary
131 * directory. If name is NULL, return the name of the testing temporary
132 * directory, without any suffix. */
133 const char *
134 get_fname_rnd(const char *name)
136 char rnd[256], rnd32[256];
137 crypto_rand(rnd, RAND_PATH_BYTES);
138 base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
139 return get_fname_suffix(name, rnd32);
142 /* Remove a directory and all of its subdirectories */
143 static void
144 rm_rf(const char *dir)
146 struct stat st;
147 smartlist_t *elements;
149 elements = tor_listdir(dir);
150 if (elements) {
151 SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
152 char *tmp = NULL;
153 tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
154 if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
155 rm_rf(tmp);
156 } else {
157 if (unlink(tmp)) {
158 fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
161 tor_free(tmp);
162 } SMARTLIST_FOREACH_END(cp);
163 SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
164 smartlist_free(elements);
166 if (rmdir(dir))
167 fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
170 /** Remove all files stored under the temporary directory, and the directory
171 * itself. Called by atexit(). */
172 static void
173 remove_directory(void)
175 if (getpid() != temp_dir_setup_in_pid) {
176 /* Only clean out the tempdir when the main process is exiting. */
177 return;
180 rm_rf(temp_dir);
183 static void *
184 passthrough_test_setup(const struct testcase_t *testcase)
186 /* Make sure the passthrough doesn't unintentionally fail or skip tests */
187 tor_assert(testcase->setup_data);
188 tor_assert(testcase->setup_data != (void*)TT_SKIP);
189 return testcase->setup_data;
191 static int
192 passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
194 (void)testcase;
195 (void)ptr;
196 return 1;
199 static void *
200 ed25519_testcase_setup(const struct testcase_t *testcase)
202 crypto_ed25519_testing_force_impl(testcase->setup_data);
203 return testcase->setup_data;
205 static int
206 ed25519_testcase_cleanup(const struct testcase_t *testcase, void *ptr)
208 (void)testcase;
209 (void)ptr;
210 crypto_ed25519_testing_restore_impl();
211 return 1;
213 const struct testcase_setup_t ed25519_test_setup = {
214 ed25519_testcase_setup, ed25519_testcase_cleanup
217 const struct testcase_setup_t passthrough_setup = {
218 passthrough_test_setup, passthrough_test_cleanup
221 static void
222 an_assertion_failed(void)
224 tinytest_set_test_failed_();
227 void tinytest_prefork(void);
228 void tinytest_postfork(void);
229 void
230 tinytest_prefork(void)
232 free_pregenerated_keys();
233 crypto_prefork();
235 void
236 tinytest_postfork(void)
238 crypto_postfork();
239 init_pregenerated_keys();
242 static void
243 log_callback_failure(int severity, uint32_t domain, const char *msg)
245 (void)msg;
246 if (severity == LOG_ERR || (domain & LD_BUG)) {
247 tinytest_set_test_failed_();
251 /** Main entry point for unit test code: parse the command line, and run
252 * some unit tests. */
254 main(int c, const char **v)
256 or_options_t *options;
257 char *errmsg = NULL;
258 int i, i_out;
259 int loglevel = LOG_ERR;
260 int accel_crypto = 0;
262 /* We must initialise logs before we call tor_assert() */
263 init_logging(1);
265 update_approx_time(time(NULL));
266 options = options_new();
267 tor_threads_init();
268 tor_compress_init();
270 network_init();
272 monotime_init();
274 struct tor_libevent_cfg cfg;
275 memset(&cfg, 0, sizeof(cfg));
276 tor_libevent_initialize(&cfg);
278 control_initialize_event_queue();
279 configure_backtrace_handler(get_version());
281 unsigned num=1, den=1;
283 for (i_out = i = 1; i < c; ++i) {
284 if (!strcmp(v[i], "--warn")) {
285 loglevel = LOG_WARN;
286 } else if (!strcmp(v[i], "--notice")) {
287 loglevel = LOG_NOTICE;
288 } else if (!strcmp(v[i], "--info")) {
289 loglevel = LOG_INFO;
290 } else if (!strcmp(v[i], "--debug")) {
291 loglevel = LOG_DEBUG;
292 } else if (!strcmp(v[i], "--accel")) {
293 accel_crypto = 1;
294 } else if (!strcmp(v[i], "--fraction")) {
295 if (i+1 == c) {
296 printf("--fraction needs an argument.\n");
297 return 1;
299 const char *fracstr = v[++i];
300 char ch;
301 if (sscanf(fracstr, "%u/%u%c", &num, &den, &ch) != 2) {
302 printf("--fraction expects a fraction as an input.\n");
304 if (den == 0 || num == 0 || num > den) {
305 printf("--fraction expects a valid fraction as an input.\n");
307 } else {
308 v[i_out++] = v[i];
311 c = i_out;
314 /* setup logs to stdout */
315 log_severity_list_t s;
316 memset(&s, 0, sizeof(s));
317 set_log_severity_config(loglevel, LOG_ERR, &s);
318 /* ALWAYS log bug warnings. */
319 s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
320 add_stream_log(&s, "", fileno(stdout));
323 /* Setup logs that cause failure. */
324 log_severity_list_t s;
325 memset(&s, 0, sizeof(s));
326 set_log_severity_config(LOG_ERR, LOG_ERR, &s);
327 s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
328 add_callback_log(&s, log_callback_failure);
330 init_protocol_warning_severity_level();
332 options->command = CMD_RUN_UNITTESTS;
333 if (crypto_global_init(accel_crypto, NULL, NULL)) {
334 printf("Can't initialize crypto subsystem; exiting.\n");
335 return 1;
337 if (crypto_seed_rng() < 0) {
338 printf("Couldn't seed RNG; exiting.\n");
339 return 1;
341 rep_hist_init();
342 setup_directory();
343 initialize_mainloop_events();
344 options_init(options);
345 options->DataDirectory = tor_strdup(temp_dir);
346 tor_asprintf(&options->KeyDirectory, "%s"PATH_SEPARATOR"keys",
347 options->DataDirectory);
348 options->CacheDirectory = tor_strdup(temp_dir);
349 options->EntryStatistics = 1;
350 if (set_options(options, &errmsg) < 0) {
351 printf("Failed to set initial options: %s\n", errmsg);
352 tor_free(errmsg);
353 return 1;
356 tor_set_failed_assertion_callback(an_assertion_failed);
358 init_pregenerated_keys();
360 channelpadding_new_consensus_params(NULL);
362 predicted_ports_init();
364 atexit(remove_directory);
366 /* Look for TOR_SKIP_TESTCASES: a space-separated list of tests to skip. */
367 const char *skip_tests = getenv("TOR_SKIP_TESTCASES");
368 if (skip_tests) {
369 smartlist_t *skip = smartlist_new();
370 smartlist_split_string(skip, skip_tests, NULL,
371 SPLIT_IGNORE_BLANK, -1);
372 int n = 0;
373 SMARTLIST_FOREACH_BEGIN(skip, char *, cp) {
374 n += tinytest_skip(testgroups, cp);
375 tor_free(cp);
376 } SMARTLIST_FOREACH_END(cp);
377 printf("Skipping %d testcases.\n", n);
378 smartlist_free(skip);
381 if (den != 1) {
382 // count the tests. Linear but fast.
383 unsigned n_tests = 0;
384 struct testgroup_t *tg;
385 struct testcase_t *tc;
386 for (tg = testgroups; tg->prefix != NULL; ++tg) {
387 for (tc = tg->cases; tc->name != NULL; ++tc) {
388 ++n_tests;
391 // Which tests should we run? This can give iffy results if den is huge
392 // but it doesn't actually matter in practice.
393 unsigned tests_per_chunk = CEIL_DIV(n_tests, den);
394 unsigned start_at = (num-1) * tests_per_chunk;
396 // Skip the tests that are outside of the range.
397 unsigned idx = 0;
398 for (tg = testgroups; tg->prefix != NULL; ++tg) {
399 for (tc = tg->cases; tc->name != NULL; ++tc) {
400 if (idx < start_at || idx >= start_at + tests_per_chunk) {
401 tc->flags |= TT_SKIP;
403 ++idx;
408 int have_failed = (tinytest_main(c, v, testgroups) != 0);
410 free_pregenerated_keys();
412 crypto_global_cleanup();
414 if (have_failed)
415 return 1;
416 else
417 return 0;