2015-01-23 17:15:53 +01:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2020-01-09 00:39:17 +01:00
|
|
|
* Copyright (c) 2007-2020, The Tor Project, Inc. */
|
2015-01-23 17:15:53 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file test_common.c
|
|
|
|
* \brief Common pieces to implement unit tests.
|
|
|
|
**/
|
|
|
|
|
2018-09-20 21:19:43 +02:00
|
|
|
#define MAINLOOP_PRIVATE
|
2015-01-23 17:15:53 +01:00
|
|
|
#include "orconfig.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
|
|
|
#include "feature/control/control.h"
|
2019-03-25 17:11:59 +01:00
|
|
|
#include "feature/control/control_events.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "app/config/config.h"
|
2018-07-01 17:32:11 +02:00
|
|
|
#include "lib/crypt_ops/crypto_dh.h"
|
2018-07-01 19:04:21 +02:00
|
|
|
#include "lib/crypt_ops/crypto_ed25519.h"
|
2018-06-21 18:47:11 +02:00
|
|
|
#include "lib/crypt_ops/crypto_rand.h"
|
2018-09-27 14:21:17 +02:00
|
|
|
#include "feature/stats/predict_ports.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/stats/rephist.h"
|
2018-06-21 15:54:04 +02:00
|
|
|
#include "lib/err/backtrace.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/channelpadding.h"
|
2018-09-20 21:19:43 +02:00
|
|
|
#include "core/mainloop/mainloop.h"
|
2018-07-01 17:32:11 +02:00
|
|
|
#include "lib/compress/compress.h"
|
2018-07-05 21:14:04 +02:00
|
|
|
#include "lib/evloop/compat_libevent.h"
|
2018-07-11 19:44:35 +02:00
|
|
|
#include "lib/crypt_ops/crypto_init.h"
|
2018-11-01 17:33:22 +01:00
|
|
|
#include "lib/version/torversion.h"
|
2018-11-01 19:13:36 +01:00
|
|
|
#include "app/main/subsysmgr.h"
|
2015-01-23 17:15:53 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2018-07-01 21:02:01 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2015-01-23 17:15:53 +01:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* For mkdir() */
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2015-01-23 17:15:53 +01:00
|
|
|
|
|
|
|
/** Temporary directory (set up by setup_directory) under which we store all
|
|
|
|
* our files during testing. */
|
|
|
|
static char temp_dir[256];
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define pid_t int
|
|
|
|
#endif
|
|
|
|
static pid_t temp_dir_setup_in_pid = 0;
|
|
|
|
|
|
|
|
/** Select and create the temporary directory we'll use to run our unit tests.
|
|
|
|
* Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
|
|
|
|
* idempotent. */
|
|
|
|
static void
|
|
|
|
setup_directory(void)
|
|
|
|
{
|
|
|
|
static int is_setup = 0;
|
|
|
|
int r;
|
|
|
|
char rnd[256], rnd32[256];
|
|
|
|
if (is_setup) return;
|
|
|
|
|
|
|
|
/* Due to base32 limitation needs to be a multiple of 5. */
|
|
|
|
#define RAND_PATH_BYTES 5
|
|
|
|
crypto_rand(rnd, RAND_PATH_BYTES);
|
|
|
|
base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
const char *tmp = buf;
|
|
|
|
const char *extra_backslash = "";
|
|
|
|
/* If this fails, we're probably screwed anyway */
|
|
|
|
if (!GetTempPathA(sizeof(buf),buf))
|
|
|
|
tmp = "c:\\windows\\temp\\";
|
|
|
|
if (strcmpend(tmp, "\\")) {
|
|
|
|
/* According to MSDN, it should be impossible for GetTempPath to give us
|
|
|
|
* an answer that doesn't end with \. But let's make sure. */
|
|
|
|
extra_backslash = "\\";
|
|
|
|
}
|
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir),
|
|
|
|
"%s%stor_test_%d_%s", tmp, extra_backslash,
|
|
|
|
(int)getpid(), rnd32);
|
|
|
|
r = mkdir(temp_dir);
|
|
|
|
}
|
2019-10-16 18:52:36 +02:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
/* tor might not like the default perms, so create a subdir */
|
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir),
|
|
|
|
"/data/local/tmp/tor_%d_%d_%s",
|
|
|
|
(int) getuid(), (int) getpid(), rnd32);
|
|
|
|
r = mkdir(temp_dir, 0700);
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "Can't create directory %s:", temp_dir);
|
|
|
|
perror("");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-09-30 14:57:37 +02:00
|
|
|
#else /* !defined(_WIN32) */
|
2015-01-23 17:15:53 +01:00
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
|
|
|
|
(int) getpid(), rnd32);
|
|
|
|
r = mkdir(temp_dir, 0700);
|
|
|
|
if (!r) {
|
|
|
|
/* undo sticky bit so tests don't get confused. */
|
|
|
|
r = chown(temp_dir, getuid(), getgid());
|
|
|
|
}
|
2019-12-20 00:45:53 +01:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2015-01-23 17:15:53 +01:00
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "Can't create directory %s:", temp_dir);
|
|
|
|
perror("");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
is_setup = 1;
|
|
|
|
temp_dir_setup_in_pid = getpid();
|
|
|
|
}
|
|
|
|
|
2016-11-02 04:13:34 +01:00
|
|
|
/** Return a filename relative to our testing temporary directory, based on
|
|
|
|
* name and suffix. If name is NULL, return the name of the testing temporary
|
|
|
|
* directory. */
|
|
|
|
static const char *
|
|
|
|
get_fname_suffix(const char *name, const char *suffix)
|
2015-01-23 17:15:53 +01:00
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
setup_directory();
|
|
|
|
if (!name)
|
|
|
|
return temp_dir;
|
2018-09-02 22:19:24 +02:00
|
|
|
tor_snprintf(buf,sizeof(buf),"%s%s%s%s%s", temp_dir, PATH_SEPARATOR, name,
|
|
|
|
suffix ? "_" : "", suffix ? suffix : "");
|
2015-01-23 17:15:53 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-11-02 04:13:34 +01:00
|
|
|
/** Return a filename relative to our testing temporary directory. If name is
|
|
|
|
* NULL, return the name of the testing temporary directory. */
|
|
|
|
const char *
|
|
|
|
get_fname(const char *name)
|
|
|
|
{
|
|
|
|
return get_fname_suffix(name, NULL);
|
|
|
|
}
|
|
|
|
|
2016-11-02 04:14:19 +01:00
|
|
|
/** Return a filename with a random suffix, relative to our testing temporary
|
|
|
|
* directory. If name is NULL, return the name of the testing temporary
|
|
|
|
* directory, without any suffix. */
|
|
|
|
const char *
|
|
|
|
get_fname_rnd(const char *name)
|
|
|
|
{
|
|
|
|
char rnd[256], rnd32[256];
|
|
|
|
crypto_rand(rnd, RAND_PATH_BYTES);
|
|
|
|
base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
|
|
|
|
return get_fname_suffix(name, rnd32);
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
/* Remove a directory and all of its subdirectories */
|
|
|
|
static void
|
|
|
|
rm_rf(const char *dir)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
smartlist_t *elements;
|
|
|
|
|
|
|
|
elements = tor_listdir(dir);
|
|
|
|
if (elements) {
|
|
|
|
SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
|
|
|
|
char *tmp = NULL;
|
|
|
|
tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
|
|
|
|
if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
|
|
|
|
rm_rf(tmp);
|
|
|
|
} else {
|
|
|
|
if (unlink(tmp)) {
|
|
|
|
fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_free(tmp);
|
|
|
|
} SMARTLIST_FOREACH_END(cp);
|
|
|
|
SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
|
|
|
}
|
|
|
|
if (rmdir(dir))
|
|
|
|
fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove all files stored under the temporary directory, and the directory
|
|
|
|
* itself. Called by atexit(). */
|
|
|
|
static void
|
|
|
|
remove_directory(void)
|
|
|
|
{
|
|
|
|
if (getpid() != temp_dir_setup_in_pid) {
|
|
|
|
/* Only clean out the tempdir when the main process is exiting. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rm_rf(temp_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
passthrough_test_setup(const struct testcase_t *testcase)
|
|
|
|
{
|
2016-11-02 04:16:14 +01:00
|
|
|
/* Make sure the passthrough doesn't unintentionally fail or skip tests */
|
|
|
|
tor_assert(testcase->setup_data);
|
|
|
|
tor_assert(testcase->setup_data != (void*)TT_SKIP);
|
2015-01-23 17:15:53 +01:00
|
|
|
return testcase->setup_data;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
|
|
|
|
{
|
|
|
|
(void)testcase;
|
|
|
|
(void)ptr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-09 14:55:11 +02:00
|
|
|
static void *
|
|
|
|
ed25519_testcase_setup(const struct testcase_t *testcase)
|
|
|
|
{
|
|
|
|
crypto_ed25519_testing_force_impl(testcase->setup_data);
|
|
|
|
return testcase->setup_data;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
ed25519_testcase_cleanup(const struct testcase_t *testcase, void *ptr)
|
|
|
|
{
|
|
|
|
(void)testcase;
|
|
|
|
(void)ptr;
|
|
|
|
crypto_ed25519_testing_restore_impl();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const struct testcase_setup_t ed25519_test_setup = {
|
|
|
|
ed25519_testcase_setup, ed25519_testcase_cleanup
|
|
|
|
};
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
const struct testcase_setup_t passthrough_setup = {
|
|
|
|
passthrough_test_setup, passthrough_test_cleanup
|
|
|
|
};
|
|
|
|
|
2016-09-08 19:27:30 +02:00
|
|
|
static void
|
|
|
|
an_assertion_failed(void)
|
|
|
|
{
|
|
|
|
tinytest_set_test_failed_();
|
|
|
|
}
|
|
|
|
|
2018-08-29 14:39:19 +02:00
|
|
|
void tinytest_prefork(void);
|
2018-07-11 21:36:54 +02:00
|
|
|
void tinytest_postfork(void);
|
|
|
|
void
|
2018-08-29 14:39:19 +02:00
|
|
|
tinytest_prefork(void)
|
2018-07-11 21:36:54 +02:00
|
|
|
{
|
2018-08-01 00:42:42 +02:00
|
|
|
free_pregenerated_keys();
|
2018-11-02 23:00:56 +01:00
|
|
|
subsystems_prefork();
|
2018-08-29 14:39:19 +02:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tinytest_postfork(void)
|
|
|
|
{
|
2018-11-02 23:00:56 +01:00
|
|
|
subsystems_postfork();
|
2018-08-01 00:42:42 +02:00
|
|
|
init_pregenerated_keys();
|
2018-07-11 21:36:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 21:47:33 +01:00
|
|
|
static void
|
2019-07-08 20:11:02 +02:00
|
|
|
log_callback_failure(int severity, log_domain_mask_t domain, const char *msg)
|
2019-01-16 21:47:33 +01:00
|
|
|
{
|
|
|
|
(void)msg;
|
|
|
|
if (severity == LOG_ERR || (domain & LD_BUG)) {
|
|
|
|
tinytest_set_test_failed_();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
/** Main entry point for unit test code: parse the command line, and run
|
|
|
|
* some unit tests. */
|
|
|
|
int
|
|
|
|
main(int c, const char **v)
|
|
|
|
{
|
|
|
|
or_options_t *options;
|
|
|
|
char *errmsg = NULL;
|
|
|
|
int i, i_out;
|
|
|
|
int loglevel = LOG_ERR;
|
|
|
|
int accel_crypto = 0;
|
|
|
|
|
2020-02-13 21:11:18 +01:00
|
|
|
subsystems_init();
|
2016-02-05 04:28:53 +01:00
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
options = options_new();
|
2016-02-04 18:30:48 +01:00
|
|
|
|
2019-11-07 14:41:22 +01:00
|
|
|
struct tor_libevent_cfg_t cfg;
|
2016-02-04 18:30:48 +01:00
|
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
|
|
tor_libevent_initialize(&cfg);
|
|
|
|
|
2015-08-12 15:35:26 +02:00
|
|
|
control_initialize_event_queue();
|
2015-01-23 17:15:53 +01:00
|
|
|
|
2019-10-17 19:23:11 +02:00
|
|
|
/* Don't add default logs; the tests manage their own. */
|
|
|
|
quiet_level = QUIET_SILENT;
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
for (i_out = i = 1; i < c; ++i) {
|
|
|
|
if (!strcmp(v[i], "--warn")) {
|
|
|
|
loglevel = LOG_WARN;
|
|
|
|
} else if (!strcmp(v[i], "--notice")) {
|
|
|
|
loglevel = LOG_NOTICE;
|
|
|
|
} else if (!strcmp(v[i], "--info")) {
|
|
|
|
loglevel = LOG_INFO;
|
|
|
|
} else if (!strcmp(v[i], "--debug")) {
|
|
|
|
loglevel = LOG_DEBUG;
|
|
|
|
} else if (!strcmp(v[i], "--accel")) {
|
|
|
|
accel_crypto = 1;
|
|
|
|
} else {
|
|
|
|
v[i_out++] = v[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = i_out;
|
|
|
|
|
|
|
|
{
|
2019-01-16 21:47:33 +01:00
|
|
|
/* setup logs to stdout */
|
2015-01-23 17:15:53 +01:00
|
|
|
log_severity_list_t s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
set_log_severity_config(loglevel, LOG_ERR, &s);
|
2016-08-31 19:22:07 +02:00
|
|
|
/* ALWAYS log bug warnings. */
|
2019-09-25 08:35:02 +02:00
|
|
|
s.masks[SEVERITY_MASK_IDX(LOG_WARN)] |= LD_BUG;
|
2015-01-23 17:15:53 +01:00
|
|
|
add_stream_log(&s, "", fileno(stdout));
|
|
|
|
}
|
2019-01-16 21:47:33 +01:00
|
|
|
{
|
|
|
|
/* Setup logs that cause failure. */
|
|
|
|
log_severity_list_t s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
set_log_severity_config(LOG_ERR, LOG_ERR, &s);
|
2019-09-25 08:35:02 +02:00
|
|
|
s.masks[SEVERITY_MASK_IDX(LOG_WARN)] |= LD_BUG;
|
2019-01-16 21:47:33 +01:00
|
|
|
add_callback_log(&s, log_callback_failure);
|
|
|
|
}
|
2018-11-01 19:13:36 +01:00
|
|
|
flush_log_messages_from_startup();
|
2018-01-24 18:02:44 +01:00
|
|
|
init_protocol_warning_severity_level();
|
2015-01-23 17:15:53 +01:00
|
|
|
|
|
|
|
options->command = CMD_RUN_UNITTESTS;
|
|
|
|
if (crypto_global_init(accel_crypto, NULL, NULL)) {
|
|
|
|
printf("Can't initialize crypto subsystem; exiting.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-11-25 16:30:58 +01:00
|
|
|
if (crypto_seed_rng() < 0) {
|
|
|
|
printf("Couldn't seed RNG; exiting.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-23 17:15:53 +01:00
|
|
|
rep_hist_init();
|
|
|
|
setup_directory();
|
2018-04-26 20:38:43 +02:00
|
|
|
initialize_mainloop_events();
|
2015-01-23 17:15:53 +01:00
|
|
|
options_init(options);
|
|
|
|
options->DataDirectory = tor_strdup(temp_dir);
|
2019-10-25 02:16:44 +02:00
|
|
|
options->DataDirectory_option = tor_strdup(temp_dir);
|
2017-11-15 00:14:08 +01:00
|
|
|
tor_asprintf(&options->KeyDirectory, "%s"PATH_SEPARATOR"keys",
|
|
|
|
options->DataDirectory);
|
|
|
|
options->CacheDirectory = tor_strdup(temp_dir);
|
2015-01-23 17:15:53 +01:00
|
|
|
options->EntryStatistics = 1;
|
|
|
|
if (set_options(options, &errmsg) < 0) {
|
|
|
|
printf("Failed to set initial options: %s\n", errmsg);
|
|
|
|
tor_free(errmsg);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-22 03:28:00 +01:00
|
|
|
|
2016-09-08 19:27:30 +02:00
|
|
|
tor_set_failed_assertion_callback(an_assertion_failed);
|
2015-01-23 17:15:53 +01:00
|
|
|
|
2016-09-11 18:36:09 +02:00
|
|
|
init_pregenerated_keys();
|
2016-09-09 14:47:41 +02:00
|
|
|
|
2016-09-22 23:52:25 +02:00
|
|
|
channelpadding_new_consensus_params(NULL);
|
|
|
|
|
2017-02-22 03:28:00 +01:00
|
|
|
predicted_ports_init();
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
atexit(remove_directory);
|
|
|
|
|
2020-03-19 20:25:11 +01:00
|
|
|
/* Look for TOR_SKIP_TESTCASES: a space-separated list of tests to skip. */
|
|
|
|
const char *skip_tests = getenv("TOR_SKIP_TESTCASES");
|
|
|
|
if (skip_tests) {
|
|
|
|
smartlist_t *skip = smartlist_new();
|
|
|
|
smartlist_split_string(skip, skip_tests, NULL,
|
|
|
|
SPLIT_IGNORE_BLANK, -1);
|
|
|
|
int n = 0;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(skip, char *, cp) {
|
|
|
|
n += tinytest_skip(testgroups, cp);
|
|
|
|
tor_free(cp);
|
|
|
|
} SMARTLIST_FOREACH_END(cp);
|
|
|
|
printf("Skipping %d testcases.\n", n);
|
|
|
|
smartlist_free(skip);
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
int have_failed = (tinytest_main(c, v, testgroups) != 0);
|
|
|
|
|
|
|
|
free_pregenerated_keys();
|
2018-06-20 16:19:56 +02:00
|
|
|
|
2015-01-23 17:15:53 +01:00
|
|
|
if (have_failed)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|