1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Common pieces to implement unit tests.
11 #define MAINLOOP_PRIVATE
13 #include "core/or/or.h"
14 #include "feature/control/control.h"
15 #include "feature/control/control_events.h"
16 #include "app/config/config.h"
17 #include "lib/crypt_ops/crypto_dh.h"
18 #include "lib/crypt_ops/crypto_ed25519.h"
19 #include "lib/crypt_ops/crypto_rand.h"
20 #include "feature/stats/predict_ports.h"
21 #include "feature/stats/bwhist.h"
22 #include "feature/stats/rephist.h"
23 #include "lib/err/backtrace.h"
24 #include "test/test.h"
25 #include "core/or/channelpadding.h"
26 #include "core/mainloop/mainloop.h"
27 #include "lib/compress/compress.h"
28 #include "lib/evloop/compat_libevent.h"
29 #include "lib/crypt_ops/crypto_init.h"
30 #include "lib/version/torversion.h"
31 #include "app/main/subsysmgr.h"
40 #ifdef HAVE_SYS_STAT_H
49 #endif /* defined(_WIN32) */
51 /** Temporary directory (set up by setup_directory) under which we store all
52 * our files during testing. */
53 static char temp_dir
[256];
57 static pid_t temp_dir_setup_in_pid
= 0;
59 /** Select and create the temporary directory we'll use to run our unit tests.
60 * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
65 static int is_setup
= 0;
67 char rnd
[256], rnd32
[256];
70 /* Due to base32 limitation needs to be a multiple of 5. */
71 #define RAND_PATH_BYTES 5
72 crypto_rand(rnd
, RAND_PATH_BYTES
);
73 base32_encode(rnd32
, sizeof(rnd32
), rnd
, RAND_PATH_BYTES
);
78 const char *tmp
= buf
;
79 const char *extra_backslash
= "";
80 /* If this fails, we're probably screwed anyway */
81 if (!GetTempPathA(sizeof(buf
),buf
))
82 tmp
= "c:\\windows\\temp\\";
83 if (strcmpend(tmp
, "\\")) {
84 /* According to MSDN, it should be impossible for GetTempPath to give us
85 * an answer that doesn't end with \. But let's make sure. */
86 extra_backslash
= "\\";
88 tor_snprintf(temp_dir
, sizeof(temp_dir
),
89 "%s%stor_test_%d_%s", tmp
, extra_backslash
,
90 (int)getpid(), rnd32
);
93 #elif defined(__ANDROID__)
94 /* tor might not like the default perms, so create a subdir */
95 tor_snprintf(temp_dir
, sizeof(temp_dir
),
96 "/data/local/tmp/tor_%d_%d_%s",
97 (int) getuid(), (int) getpid(), rnd32
);
98 r
= mkdir(temp_dir
, 0700);
100 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
104 #else /* !defined(_WIN32) */
105 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d_%s",
106 (int) getpid(), rnd32
);
107 r
= mkdir(temp_dir
, 0700);
109 /* undo sticky bit so tests don't get confused. */
110 r
= chown(temp_dir
, getuid(), getgid());
112 #endif /* defined(_WIN32) || ... */
114 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
119 temp_dir_setup_in_pid
= getpid();
122 /** Return a filename relative to our testing temporary directory, based on
123 * name and suffix. If name is NULL, return the name of the testing temporary
126 get_fname_suffix(const char *name
, const char *suffix
)
128 static char buf
[1024];
132 tor_snprintf(buf
,sizeof(buf
),"%s%s%s%s%s", temp_dir
, PATH_SEPARATOR
, name
,
133 suffix
? "_" : "", suffix
? suffix
: "");
137 /** Return a filename relative to our testing temporary directory. If name is
138 * NULL, return the name of the testing temporary directory. */
140 get_fname(const char *name
)
142 return get_fname_suffix(name
, NULL
);
145 /** Return a filename with a random suffix, relative to our testing temporary
146 * directory. If name is NULL, return the name of the testing temporary
147 * directory, without any suffix. */
149 get_fname_rnd(const char *name
)
151 char rnd
[256], rnd32
[256];
152 crypto_rand(rnd
, RAND_PATH_BYTES
);
153 base32_encode(rnd32
, sizeof(rnd32
), rnd
, RAND_PATH_BYTES
);
154 return get_fname_suffix(name
, rnd32
);
157 /* Remove a directory and all of its subdirectories */
159 rm_rf(const char *dir
)
162 smartlist_t
*elements
;
164 elements
= tor_listdir(dir
);
166 SMARTLIST_FOREACH_BEGIN(elements
, const char *, cp
) {
168 tor_asprintf(&tmp
, "%s"PATH_SEPARATOR
"%s", dir
, cp
);
169 if (0 == stat(tmp
,&st
) && (st
.st_mode
& S_IFDIR
)) {
173 fprintf(stderr
, "Error removing %s: %s\n", tmp
, strerror(errno
));
177 } SMARTLIST_FOREACH_END(cp
);
178 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
179 smartlist_free(elements
);
182 fprintf(stderr
, "Error removing directory %s: %s\n", dir
, strerror(errno
));
185 /** Remove all files stored under the temporary directory, and the directory
186 * itself. Called by atexit(). */
188 remove_directory(void)
190 if (getpid() != temp_dir_setup_in_pid
) {
191 /* Only clean out the tempdir when the main process is exiting. */
199 passthrough_test_setup(const struct testcase_t
*testcase
)
201 /* Make sure the passthrough doesn't unintentionally fail or skip tests */
202 tor_assert(testcase
->setup_data
);
203 tor_assert(testcase
->setup_data
!= (void*)TT_SKIP
);
204 return testcase
->setup_data
;
207 passthrough_test_cleanup(const struct testcase_t
*testcase
, void *ptr
)
215 ed25519_testcase_setup(const struct testcase_t
*testcase
)
217 crypto_ed25519_testing_force_impl(testcase
->setup_data
);
218 return testcase
->setup_data
;
221 ed25519_testcase_cleanup(const struct testcase_t
*testcase
, void *ptr
)
225 crypto_ed25519_testing_restore_impl();
228 const struct testcase_setup_t ed25519_test_setup
= {
229 ed25519_testcase_setup
, ed25519_testcase_cleanup
232 const struct testcase_setup_t passthrough_setup
= {
233 passthrough_test_setup
, passthrough_test_cleanup
237 an_assertion_failed(void)
239 tinytest_set_test_failed_();
242 void tinytest_prefork(void);
243 void tinytest_postfork(void);
245 tinytest_prefork(void)
248 free_pregenerated_keys();
250 subsystems_prefork();
253 tinytest_postfork(void)
255 subsystems_postfork();
257 init_pregenerated_keys();
262 log_callback_failure(int severity
, log_domain_mask_t domain
, const char *msg
)
265 if (severity
== LOG_ERR
|| (domain
& LD_BUG
)) {
266 tinytest_set_test_failed_();
270 /** Main entry point for unit test code: parse the command line, and run
271 * some unit tests. */
273 main(int c
, const char **v
)
275 or_options_t
*options
;
278 int loglevel
= LOG_ERR
;
279 int accel_crypto
= 0;
283 options
= options_new();
285 struct tor_libevent_cfg_t cfg
;
286 memset(&cfg
, 0, sizeof(cfg
));
287 tor_libevent_initialize(&cfg
);
289 control_initialize_event_queue();
291 /* Don't add default logs; the tests manage their own. */
292 quiet_level
= QUIET_SILENT
;
294 unsigned num
=1, den
=1;
296 for (i_out
= i
= 1; i
< c
; ++i
) {
297 if (!strcmp(v
[i
], "--warn")) {
299 } else if (!strcmp(v
[i
], "--notice")) {
300 loglevel
= LOG_NOTICE
;
301 } else if (!strcmp(v
[i
], "--info")) {
303 } else if (!strcmp(v
[i
], "--debug")) {
304 loglevel
= LOG_DEBUG
;
305 } else if (!strcmp(v
[i
], "--accel")) {
307 } else if (!strcmp(v
[i
], "--fraction")) {
309 printf("--fraction needs an argument.\n");
312 const char *fracstr
= v
[++i
];
314 if (sscanf(fracstr
, "%u/%u%c", &num
, &den
, &ch
) != 2) {
315 printf("--fraction expects a fraction as an input.\n");
317 if (den
== 0 || num
== 0 || num
> den
) {
318 printf("--fraction expects a valid fraction as an input.\n");
327 /* setup logs to stdout */
328 log_severity_list_t s
;
329 memset(&s
, 0, sizeof(s
));
330 set_log_severity_config(loglevel
, LOG_ERR
, &s
);
331 /* ALWAYS log bug warnings. */
332 s
.masks
[SEVERITY_MASK_IDX(LOG_WARN
)] |= LD_BUG
;
333 add_stream_log(&s
, "", fileno(stdout
));
336 /* Setup logs that cause failure. */
337 log_severity_list_t s
;
338 memset(&s
, 0, sizeof(s
));
339 set_log_severity_config(LOG_ERR
, LOG_ERR
, &s
);
340 s
.masks
[SEVERITY_MASK_IDX(LOG_WARN
)] |= LD_BUG
;
341 add_callback_log(&s
, log_callback_failure
);
343 flush_log_messages_from_startup();
344 init_protocol_warning_severity_level();
346 options
->command
= CMD_RUN_UNITTESTS
;
347 if (crypto_global_init(accel_crypto
, NULL
, NULL
)) {
348 printf("Can't initialize crypto subsystem; exiting.\n");
351 if (crypto_seed_rng() < 0) {
352 printf("Couldn't seed RNG; exiting.\n");
358 initialize_mainloop_events();
359 options_init(options
);
360 options
->DataDirectory
= tor_strdup(temp_dir
);
361 options
->DataDirectory_option
= tor_strdup(temp_dir
);
362 tor_asprintf(&options
->KeyDirectory
, "%s"PATH_SEPARATOR
"keys",
363 options
->DataDirectory
);
364 options
->CacheDirectory
= tor_strdup(temp_dir
);
365 options
->EntryStatistics
= 1;
366 if (set_options(options
, &errmsg
) < 0) {
367 printf("Failed to set initial options: %s\n", errmsg
);
372 tor_set_failed_assertion_callback(an_assertion_failed
);
374 init_pregenerated_keys();
376 channelpadding_new_consensus_params(NULL
);
378 predicted_ports_init();
380 atexit(remove_directory
);
382 /* Look for TOR_SKIP_TESTCASES: a space-separated list of tests to skip. */
383 const char *skip_tests
= getenv("TOR_SKIP_TESTCASES");
385 smartlist_t
*skip
= smartlist_new();
386 smartlist_split_string(skip
, skip_tests
, NULL
,
387 SPLIT_IGNORE_BLANK
, -1);
389 SMARTLIST_FOREACH_BEGIN(skip
, char *, cp
) {
390 n
+= tinytest_skip(testgroups
, cp
);
392 } SMARTLIST_FOREACH_END(cp
);
393 printf("Skipping %d testcases.\n", n
);
394 smartlist_free(skip
);
398 // count the tests. Linear but fast.
399 unsigned n_tests
= 0;
400 struct testgroup_t
*tg
;
401 struct testcase_t
*tc
;
402 for (tg
= testgroups
; tg
->prefix
!= NULL
; ++tg
) {
403 for (tc
= tg
->cases
; tc
->name
!= NULL
; ++tc
) {
407 // Which tests should we run? This can give iffy results if den is huge
408 // but it doesn't actually matter in practice.
409 unsigned tests_per_chunk
= CEIL_DIV(n_tests
, den
);
410 unsigned start_at
= (num
-1) * tests_per_chunk
;
412 // Skip the tests that are outside of the range.
414 for (tg
= testgroups
; tg
->prefix
!= NULL
; ++tg
) {
415 for (tc
= tg
->cases
; tc
->name
!= NULL
; ++tc
) {
416 if (idx
< start_at
|| idx
>= start_at
+ tests_per_chunk
) {
417 tc
->flags
|= TT_SKIP
;
424 int have_failed
= (tinytest_main(c
, v
, testgroups
) != 0);
426 free_pregenerated_keys();