aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/Makefile.nmake3
-rw-r--r--src/test/bench.c77
-rwxr-xr-xsrc/test/bt_test.py17
-rw-r--r--src/test/example_extrainfo.inc34
-rw-r--r--src/test/fakechans.h2
-rw-r--r--src/test/include.am126
-rw-r--r--src/test/log_test_helpers.c241
-rw-r--r--src/test/log_test_helpers.h105
-rwxr-xr-xsrc/test/ntor_ref.py2
-rw-r--r--src/test/rend_test_helpers.c73
-rw-r--r--src/test/rend_test_helpers.h15
-rw-r--r--src/test/sr_commit_calc_ref.py51
-rw-r--r--src/test/sr_srv_calc_ref.py71
-rw-r--r--src/test/test-child.c4
-rw-r--r--src/test/test-memwipe.c26
-rwxr-xr-xsrc/test/test-network.sh152
-rw-r--r--src/test/test-timers.c144
-rw-r--r--src/test/test.c134
-rw-r--r--src/test/test.h84
-rw-r--r--src/test/test_accounting.c26
-rw-r--r--src/test/test_addr.c19
-rw-r--r--src/test/test_address.c255
-rwxr-xr-xsrc/test/test_bt.sh9
-rw-r--r--src/test/test_bt_cl.c9
-rw-r--r--src/test/test_buffers.c143
-rw-r--r--src/test/test_cell_formats.c6
-rw-r--r--src/test/test_cell_queue.c2
-rw-r--r--src/test/test_channel.c142
-rw-r--r--src/test/test_channeltls.c25
-rw-r--r--src/test/test_checkdir.c2
-rw-r--r--src/test/test_circuitlist.c24
-rw-r--r--src/test/test_circuitmux.c6
-rw-r--r--src/test/test_compat_libevent.c131
-rw-r--r--src/test/test_config.c1754
-rw-r--r--src/test/test_connection.c882
-rw-r--r--src/test/test_containers.c26
-rw-r--r--src/test/test_controller.c1182
-rw-r--r--src/test/test_controller_events.c2
-rw-r--r--src/test/test_crypto.c1323
-rw-r--r--src/test/test_crypto_slow.c101
-rw-r--r--src/test/test_data.c4
-rw-r--r--src/test/test_dir.c2682
-rw-r--r--src/test/test_dir_common.c419
-rw-r--r--src/test/test_dir_common.h52
-rw-r--r--src/test/test_dir_handle_get.c2510
-rw-r--r--src/test/test_dns.c514
-rw-r--r--src/test/test_entryconn.c2
-rw-r--r--src/test/test_entrynodes.c228
-rw-r--r--src/test/test_extorport.c7
-rw-r--r--src/test/test_guardfraction.c11
-rw-r--r--src/test/test_handles.c95
-rw-r--r--src/test/test_helpers.c10
-rw-r--r--src/test/test_helpers.h2
-rw-r--r--src/test/test_hs.c355
-rw-r--r--src/test/test_introduce.c4
-rw-r--r--src/test/test_keypin.c2
-rw-r--r--src/test/test_link_handshake.c134
-rw-r--r--src/test/test_logging.c40
-rw-r--r--src/test/test_microdesc.c19
-rw-r--r--src/test/test_nodelist.c43
-rw-r--r--src/test/test_ntor_cl.c10
-rw-r--r--src/test/test_oom.c100
-rw-r--r--src/test/test_oos.c456
-rw-r--r--src/test/test_options.c4397
-rw-r--r--src/test/test_policy.c1890
-rw-r--r--src/test/test_procmon.c58
-rw-r--r--src/test/test_protover.c195
-rw-r--r--src/test/test_pt.c14
-rw-r--r--src/test/test_pubsub.c85
-rw-r--r--src/test/test_relay.c14
-rw-r--r--src/test/test_relaycell.c4
-rw-r--r--src/test/test_rendcache.c1273
-rw-r--r--src/test/test_replay.c36
-rw-r--r--src/test/test_routerkeys.c2
-rw-r--r--src/test/test_routerlist.c424
-rw-r--r--src/test/test_routerset.c124
-rw-r--r--src/test/test_scheduler.c32
-rw-r--r--src/test/test_shared_random.c1290
-rw-r--r--src/test/test_slow.c5
-rw-r--r--src/test/test_socks.c4
-rw-r--r--src/test/test_status.c19
-rw-r--r--src/test/test_switch_id.c192
-rwxr-xr-xsrc/test/test_switch_id.sh32
-rw-r--r--src/test/test_threads.c24
-rw-r--r--src/test/test_tortls.c2825
-rw-r--r--src/test/test_util.c1676
-rw-r--r--src/test/test_util_format.c395
-rw-r--r--src/test/test_util_process.c83
-rw-r--r--src/test/test_util_slow.c2
-rw-r--r--src/test/test_workqueue.c19
-rwxr-xr-xsrc/test/test_workqueue_cancel.sh4
-rwxr-xr-xsrc/test/test_workqueue_efd.sh4
-rwxr-xr-xsrc/test/test_workqueue_efd2.sh4
-rwxr-xr-xsrc/test/test_workqueue_pipe.sh4
-rwxr-xr-xsrc/test/test_workqueue_pipe2.sh4
-rwxr-xr-xsrc/test/test_workqueue_socketpair.sh4
-rw-r--r--src/test/testing_common.c141
-rw-r--r--src/test/vote_descriptors.inc94
98 files changed, 29168 insertions, 1334 deletions
diff --git a/src/test/Makefile.nmake b/src/test/Makefile.nmake
index 0435617683..0ba56d7036 100644
--- a/src/test/Makefile.nmake
+++ b/src/test/Makefile.nmake
@@ -14,7 +14,8 @@ LIBS = ..\..\..\build-alpha\lib\libevent.lib \
TEST_OBJECTS = test.obj test_addr.obj test_channel.obj test_channeltls.obj \
test_containers.obj \
test_controller_events.obj test_crypto.obj test_data.obj test_dir.obj \
- test_checkdir.obj test_microdesc.obj test_pt.obj test_util.obj test_config.obj \
+ test_checkdir.obj test_microdesc.obj test_pt.obj test_util.obj \
+ test_config.obj test_connection.obj \
test_cell_formats.obj test_relay.obj test_replay.obj \
test_scheduler.obj test_introduce.obj test_hs.obj tinytest.obj
diff --git a/src/test/bench.c b/src/test/bench.c
index 2a27377c80..30984fda70 100644
--- a/src/test/bench.c
+++ b/src/test/bench.c
@@ -1,8 +1,9 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
+extern const char tor_git_revision[];
/* Ordinarily defined in tor_main.c; this bit is just here to provide one
* since we're not linking to tor_main.c */
const char tor_git_revision[] = "";
@@ -89,7 +90,9 @@ bench_aes(void)
uint64_t start, end;
const int bytes_per_iter = (1<<24);
reset_perftime();
- c = crypto_cipher_new(NULL);
+ char key[CIPHER_KEY_LEN];
+ crypto_rand(key, sizeof(key));
+ c = crypto_cipher_new(key);
for (len = 1; len <= 8192; len *= 2) {
int iters = bytes_per_iter / len;
@@ -327,8 +330,9 @@ bench_cell_aes(void)
char *b = tor_malloc(len+max_misalign);
crypto_cipher_t *c;
int i, misalign;
-
- c = crypto_cipher_new(NULL);
+ char key[CIPHER_KEY_LEN];
+ crypto_rand(key, sizeof(key));
+ c = crypto_cipher_new(key);
reset_perftime();
for (misalign = 0; misalign <= max_misalign; ++misalign) {
@@ -443,6 +447,45 @@ bench_siphash(void)
}
static void
+bench_digest(void)
+{
+ char buf[8192];
+ char out[DIGEST512_LEN];
+ const int lens[] = { 1, 16, 32, 64, 128, 512, 1024, 2048, -1 };
+ const int N = 300000;
+ uint64_t start, end;
+ crypto_rand(buf, sizeof(buf));
+
+ for (int alg = 0; alg < N_DIGEST_ALGORITHMS; alg++) {
+ for (int i = 0; lens[i] > 0; ++i) {
+ reset_perftime();
+ start = perftime();
+ for (int j = 0; j < N; ++j) {
+ switch (alg) {
+ case DIGEST_SHA1:
+ crypto_digest(out, buf, lens[i]);
+ break;
+ case DIGEST_SHA256:
+ case DIGEST_SHA3_256:
+ crypto_digest256(out, buf, lens[i], alg);
+ break;
+ case DIGEST_SHA512:
+ case DIGEST_SHA3_512:
+ crypto_digest512(out, buf, lens[i], alg);
+ break;
+ default:
+ tor_assert(0);
+ }
+ }
+ end = perftime();
+ printf("%s(%d): %.2f ns per call\n",
+ crypto_digest_algorithm_get_name(alg),
+ lens[i], NANOCOUNT(start,end,N));
+ }
+ }
+}
+
+static void
bench_cell_ops(void)
{
const int iters = 1<<16;
@@ -461,8 +504,11 @@ bench_cell_ops(void)
or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
/* Initialize crypto */
- or_circ->p_crypto = crypto_cipher_new(NULL);
- or_circ->n_crypto = crypto_cipher_new(NULL);
+ char key1[CIPHER_KEY_LEN], key2[CIPHER_KEY_LEN];
+ crypto_rand(key1, sizeof(key1));
+ crypto_rand(key2, sizeof(key2));
+ or_circ->p_crypto = crypto_cipher_new(key1);
+ or_circ->n_crypto = crypto_cipher_new(key2);
or_circ->p_digest = crypto_digest_new();
or_circ->n_digest = crypto_digest_new();
@@ -517,7 +563,7 @@ bench_dh(void)
dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
secret_b, sizeof(secret_b));
tor_assert(slen_a == slen_b);
- tor_assert(!memcmp(secret_a, secret_b, slen_a));
+ tor_assert(fast_memeq(secret_a, secret_b, slen_a));
crypto_dh_free(dh_a);
crypto_dh_free(dh_b);
}
@@ -555,7 +601,7 @@ bench_ecdh_impl(int nid, const char *name)
NULL);
tor_assert(slen_a == slen_b);
- tor_assert(!memcmp(secret_a, secret_b, slen_a));
+ tor_assert(fast_memeq(secret_a, secret_b, slen_a));
EC_KEY_free(dh_a);
EC_KEY_free(dh_b);
}
@@ -589,6 +635,7 @@ typedef struct benchmark_t {
static struct benchmark_t benchmarks[] = {
ENT(dmap),
ENT(siphash),
+ ENT(digest),
ENT(aes),
ENT(onion_TAP),
ENT(onion_ntor),
@@ -621,7 +668,6 @@ main(int argc, const char **argv)
{
int i;
int list=0, n_enabled=0;
- benchmark_t *b;
char *errmsg;
or_options_t *options;
@@ -631,10 +677,10 @@ main(int argc, const char **argv)
if (!strcmp(argv[i], "--list")) {
list = 1;
} else {
- benchmark_t *b = find_benchmark(argv[i]);
+ benchmark_t *benchmark = find_benchmark(argv[i]);
++n_enabled;
- if (b) {
- b->enabled = 1;
+ if (benchmark) {
+ benchmark->enabled = 1;
} else {
printf("No such benchmark as %s\n", argv[i]);
}
@@ -643,7 +689,10 @@ main(int argc, const char **argv)
reset_perftime();
- crypto_seed_rng();
+ if (crypto_seed_rng() < 0) {
+ printf("Couldn't seed RNG; exiting.\n");
+ return 1;
+ }
crypto_init_siphash_key();
options = options_new();
init_logging(1);
@@ -656,7 +705,7 @@ main(int argc, const char **argv)
return 1;
}
- for (b = benchmarks; b->name; ++b) {
+ for (benchmark_t *b = benchmarks; b->name; ++b) {
if (b->enabled || n_enabled == 0) {
printf("===== %s =====\n", b->name);
if (!list)
diff --git a/src/test/bt_test.py b/src/test/bt_test.py
index e694361703..30591453b9 100755
--- a/src/test/bt_test.py
+++ b/src/test/bt_test.py
@@ -15,6 +15,7 @@ OK
"""
+from __future__ import print_function
import sys
@@ -37,6 +38,16 @@ for I in range(len(LINES)):
if matches(LINES[I:], FUNCNAMES):
print("OK")
sys.exit(0)
-else:
- print("BAD")
- sys.exit(1)
+
+print("BAD")
+
+for l in LINES:
+ print("{}".format(l), end="")
+
+if sys.platform.startswith('freebsd'):
+ # See bug #17808 if you know how to fix this.
+ print("Test failed; but FreeBSD is known to have backtrace problems.\n"
+ "Treating as 'SKIP'.")
+ sys.exit(77)
+
+sys.exit(1)
diff --git a/src/test/example_extrainfo.inc b/src/test/example_extrainfo.inc
index e096afd6c4..0bf2341ef5 100644
--- a/src/test/example_extrainfo.inc
+++ b/src/test/example_extrainfo.inc
@@ -133,7 +133,7 @@ static const char EX_EI_BAD_NICKNAME_KEY[] =
"/UBWNSyXCFDMqnddb/LZ8+VgttmxfYkpeRzSSmDijN3RbOvYJhhBAgMBAAE=\n"
"-----END RSA PUBLIC KEY-----\n";
-const char EX_EI_BAD_TOKENS[] =
+static const char EX_EI_BAD_TOKENS[] =
"extra-info bob 6F314FB01A31162BD5E473D4977AC570DC5B86BB\n"
"published 2014-10-05 20:07:00\n"
"published 2014-10-05 20:07:00\n"
@@ -145,8 +145,9 @@ const char EX_EI_BAD_TOKENS[] =
"-----END SIGNATURE-----\n"
;
-const char EX_EI_BAD_TOKENS_FP[] = "6F314FB01A31162BD5E473D4977AC570DC5B86BB";
-const char EX_EI_BAD_TOKENS_KEY[] =
+static const char EX_EI_BAD_TOKENS_FP[] =
+ "6F314FB01A31162BD5E473D4977AC570DC5B86BB";
+static const char EX_EI_BAD_TOKENS_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAL7Z8tz45Tb4tnEFS2sAyjubBV/giSfZdmXRkDV8Jo4xqWqhWFJn7+zN\n"
"AXBWBThGeVH2WXrpz5seNJXgZJPxMTMsrnSCGcRXZw0Npti2MkLuQ6+prZa+OPwE\n"
@@ -210,7 +211,8 @@ static const char EX_EI_GOOD_ED_EI[] =
"\n"
"\n"
;
-const char EX_EI_GOOD_ED_EI_FP[] = "A692FE045C32B5E3A54B52882EF678A9DAC46A73";
+static const char EX_EI_GOOD_ED_EI_FP[] =
+ "A692FE045C32B5E3A54B52882EF678A9DAC46A73";
static const char EX_EI_GOOD_ED_EI_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAM3jdYwjwGxDWYj/vyFkQT7RgeCNIn89Ei6D2+L/fdtFnqrMXOreFFHL\n"
@@ -237,7 +239,8 @@ static const char EX_EI_ED_MISSING_SIG[] =
"\n"
"\n"
;
-const char EX_EI_ED_MISSING_SIG_FP[] = "2A7521497B91A8437021515308A47491164EDBA1";
+static const char EX_EI_ED_MISSING_SIG_FP[] =
+ "2A7521497B91A8437021515308A47491164EDBA1";
static const char EX_EI_ED_MISSING_SIG_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAOOB8ccxbtk2dB5FuKFhGndDcO6STNjB6KiG0b9X2QwKrOZMfmXSigto\n"
@@ -260,7 +263,8 @@ static const char EX_EI_ED_MISSING_CERT[] =
"\n"
"\n"
;
-const char EX_EI_ED_MISSING_CERT_FP[] = "E88E43E86015345A323D93D825C33E4AD1028F65";
+static const char EX_EI_ED_MISSING_CERT_FP[] =
+ "E88E43E86015345A323D93D825C33E4AD1028F65";
static const char EX_EI_ED_MISSING_CERT_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBALjA/geb0TR9rp/UPvLhABQpB0XUDYuZAnLkrv+i7AAV7FemTDveEGnc\n"
@@ -284,7 +288,8 @@ static const char EX_EI_ED_BAD_CERT1[] =
"-----END SIGNATURE-----\n"
"\n"
;
-const char EX_EI_ED_BAD_CERT1_FP[] = "F78D8A655607D32281D02144817A4F1D26AE520F";
+static const char EX_EI_ED_BAD_CERT1_FP[] =
+ "F78D8A655607D32281D02144817A4F1D26AE520F";
static const char EX_EI_ED_BAD_CERT1_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAMlR46JhxsCmWYtmIB/JjTV2TUYIhJLmHy+X7FfkK3ZVQvvl9/3GSXFL\n"
@@ -309,7 +314,8 @@ static const char EX_EI_ED_BAD_CERT2[] =
"cVrtU6RVmzldSbyir8V/Z4S/Cm67gYAgjM5gfoFUqDs=\n"
"-----END SIGNATURE-----\n"
;
-const char EX_EI_ED_BAD_CERT2_FP[] = "7C2B42E783C4E0EB0CC3BDB37385D16737BACFBD";
+static const char EX_EI_ED_BAD_CERT2_FP[] =
+ "7C2B42E783C4E0EB0CC3BDB37385D16737BACFBD";
static const char EX_EI_ED_BAD_CERT2_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBALAM1F/0XJEsbxIQqb3+ObX/yGVnq9of8Q9sLsmxffD6hwVpCqnV3lTg\n"
@@ -335,7 +341,8 @@ static const char EX_EI_ED_BAD_SIG1[] =
"-----END SIGNATURE-----\n"
"\n"
;
-const char EX_EI_ED_BAD_SIG1_FP[] = "5AC3A538FEEFC6F9FCC5FA0CE64704396C30D62A";
+static const char EX_EI_ED_BAD_SIG1_FP[] =
+ "5AC3A538FEEFC6F9FCC5FA0CE64704396C30D62A";
static const char EX_EI_ED_BAD_SIG1_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAMvb6SuoIkPfBkJgQuo5aQDepAs1kEETZ9VXotMlhB0JJikrqBrAAz+7\n"
@@ -361,7 +368,8 @@ static const char EX_EI_ED_BAD_SIG2[] =
"-----END SIGNATURE-----\n"
"\n"
;
-const char EX_EI_ED_BAD_SIG2_FP[] = "7F1D4DD477E340C6D6B389FAC26EDC746113082F";
+static const char EX_EI_ED_BAD_SIG2_FP[] =
+ "7F1D4DD477E340C6D6B389FAC26EDC746113082F";
static const char EX_EI_ED_BAD_SIG2_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBALzOyfCEUZnvCyhlyMctPkdXg/XRE3Cr6QgyzdKf5kQbUiu2n0FgSHOX\n"
@@ -388,7 +396,8 @@ static const char EX_EI_ED_MISPLACED_CERT[] =
"-----END SIGNATURE-----\n"
"\n"
;
-const char EX_EI_ED_MISPLACED_CERT_FP[] = "3B788BD0CE348BC5CED48313307C78175EB6D0F3";
+static const char EX_EI_ED_MISPLACED_CERT_FP[] =
+ "3B788BD0CE348BC5CED48313307C78175EB6D0F3";
static const char EX_EI_ED_MISPLACED_CERT_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBALTwNqhTprg1oC6bEbDqwIYBoER6prqUXQFbwbFDn+ekXhZj8vltgGwp\n"
@@ -414,7 +423,8 @@ static const char EX_EI_ED_MISPLACED_SIG[] =
"-----END SIGNATURE-----\n"
"\n"
;
-const char EX_EI_ED_MISPLACED_SIG_FP[] = "384E40A5DEED4AB1D8A74F1FCBDB18B7C24A8284";
+static const char EX_EI_ED_MISPLACED_SIG_FP[] =
+ "384E40A5DEED4AB1D8A74F1FCBDB18B7C24A8284";
static const char EX_EI_ED_MISPLACED_SIG_KEY[] =
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAK0HgOCG/6433VCrwz/vhk3cKmyOfenCp0GZ4DIUwPWt4DeyP4nTbN6T\n"
diff --git a/src/test/fakechans.h b/src/test/fakechans.h
index 8fb8f420a8..fa0e37dbe6 100644
--- a/src/test/fakechans.h
+++ b/src/test/fakechans.h
@@ -1,4 +1,4 @@
- /* Copyright (c) 2014-2015, The Tor Project, Inc. */
+ /* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#ifndef TOR_FAKECHANS_H
diff --git a/src/test/include.am b/src/test/include.am
index a37fe23db8..8ecfaf10c6 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -1,22 +1,37 @@
+# When the day comes that Tor requires Automake >= 1.12 change
+# TESTS_ENVIRONMENT to AM_TESTS_ENVIRONMENT because the former is reserved for
+# users while the later is reserved for developers.
+TESTS_ENVIRONMENT = \
+ export PYTHON="$(PYTHON)"; \
+ export SHELL="$(SHELL)"; \
+ export abs_top_srcdir="$(abs_top_srcdir)"; \
+ export builddir="$(builddir)"; \
+ export TESTING_TOR_BINARY="$(TESTING_TOR_BINARY)";
-export PYTHON=@PYTHON@
-export SHELL=@SHELL@
-export abs_top_srcdir=@abs_top_srcdir@
-export builddir=@builddir@
-
-TESTSCRIPTS = src/test/test_zero_length_keys.sh
+TESTSCRIPTS = src/test/test_zero_length_keys.sh \
+ src/test/test_workqueue_cancel.sh \
+ src/test/test_workqueue_efd.sh \
+ src/test/test_workqueue_efd2.sh \
+ src/test/test_workqueue_pipe.sh \
+ src/test/test_workqueue_pipe2.sh \
+ src/test/test_workqueue_socketpair.sh \
+ src/test/test_switch_id.sh
if USEPYTHON
TESTSCRIPTS += src/test/test_ntor.sh src/test/test_bt.sh
endif
TESTS += src/test/test src/test/test-slow src/test/test-memwipe \
- src/test/test_workqueue src/test/test_keygen.sh $(TESTSCRIPTS)
+ src/test/test_workqueue \
+ src/test/test_keygen.sh \
+ src/test/test-timers \
+ $(TESTSCRIPTS)
# These flavors are run using automake's test-driver and test-network.sh
-TEST_CHUTNEY_FLAVORS = basic-min bridges-min hs-min bridges+hs
+TEST_CHUTNEY_FLAVORS = basic-min bridges-min hs-min single-onion
# only run if we can ping6 ::1 (localhost)
-TEST_CHUTNEY_FLAVORS_IPV6 = bridges+ipv6-min ipv6-exit-min
+TEST_CHUTNEY_FLAVORS_IPV6 = bridges+ipv6-min ipv6-exit-min hs-ipv6 \
+ single-onion-ipv6
# only run if we can find a stable (or simply another) version of tor
TEST_CHUTNEY_FLAVORS_MIXED = mixed
@@ -33,7 +48,9 @@ noinst_PROGRAMS+= \
src/test/test-slow \
src/test/test-memwipe \
src/test/test-child \
- src/test/test_workqueue
+ src/test/test_workqueue \
+ src/test/test-switch-id \
+ src/test/test-timers
endif
src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
@@ -49,6 +66,8 @@ src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
# matters a lot there, and is quite hard to debug if you forget to do it.
src_test_test_SOURCES = \
+ src/test/log_test_helpers.c \
+ src/test/rend_test_helpers.c \
src/test/test.c \
src/test/test_accounting.c \
src/test/test_addr.c \
@@ -61,18 +80,23 @@ src_test_test_SOURCES = \
src/test/test_checkdir.c \
src/test/test_circuitlist.c \
src/test/test_circuitmux.c \
+ src/test/test_compat_libevent.c \
src/test/test_config.c \
+ src/test/test_connection.c \
src/test/test_containers.c \
src/test/test_controller.c \
src/test/test_controller_events.c \
src/test/test_crypto.c \
src/test/test_data.c \
src/test/test_dir.c \
+ src/test/test_dir_common.c \
+ src/test/test_dir_handle_get.c \
src/test/test_entryconn.c \
src/test/test_entrynodes.c \
src/test/test_guardfraction.c \
src/test/test_extorport.c \
src/test/test_hs.c \
+ src/test/test_handles.c \
src/test/test_introduce.c \
src/test/test_keypin.c \
src/test/test_link_handshake.c \
@@ -80,22 +104,31 @@ src_test_test_SOURCES = \
src/test/test_microdesc.c \
src/test/test_nodelist.c \
src/test/test_oom.c \
+ src/test/test_oos.c \
src/test/test_options.c \
src/test/test_policy.c \
+ src/test/test_procmon.c \
+ src/test/test_protover.c \
src/test/test_pt.c \
+ src/test/test_pubsub.c \
src/test/test_relay.c \
src/test/test_relaycell.c \
+ src/test/test_rendcache.c \
src/test/test_replay.c \
src/test/test_routerkeys.c \
src/test/test_routerlist.c \
src/test/test_routerset.c \
src/test/test_scheduler.c \
+ src/test/test_shared_random.c \
src/test/test_socks.c \
src/test/test_status.c \
src/test/test_threads.c \
+ src/test/test_tortls.c \
src/test/test_util.c \
+ src/test/test_util_format.c \
+ src/test/test_util_process.c \
src/test/test_helpers.c \
- src/test/test_dns.c \
+ src/test/test_dns.c \
src/test/testing_common.c \
src/ext/tinytest.c
@@ -109,6 +142,8 @@ src_test_test_slow_SOURCES = \
src_test_test_memwipe_SOURCES = \
src/test/test-memwipe.c
+src_test_test_timers_SOURCES = \
+ src/test/test-timers.c
src_test_test_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
@@ -122,11 +157,26 @@ src_test_test_workqueue_SOURCES = \
src_test_test_workqueue_CPPFLAGS= $(src_test_AM_CPPFLAGS)
src_test_test_workqueue_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
+src_test_test_switch_id_SOURCES = \
+ src/test/test_switch_id.c
+src_test_test_switch_id_CPPFLAGS= $(src_test_AM_CPPFLAGS)
+src_test_test_switch_id_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
+src_test_test_switch_id_LDFLAGS = @TOR_LDFLAGS_zlib@
+src_test_test_switch_id_LDADD = \
+ src/common/libor-testing.a \
+ src/common/libor-ctime-testing.a \
+ @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@
+
src_test_test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
-src_test_test_LDADD = src/or/libtor-testing.a src/common/libor-testing.a \
- src/common/libor-crypto-testing.a $(LIBDONNA) src/common/libor.a \
- src/common/libor-event-testing.a src/trunnel/libor-trunnel-testing.a \
+src_test_test_LDADD = src/or/libtor-testing.a \
+ src/common/libor-crypto-testing.a \
+ $(LIBKECCAK_TINY) \
+ $(LIBDONNA) \
+ src/common/libor-testing.a \
+ src/common/libor-ctime-testing.a \
+ src/common/libor-event-testing.a \
+ src/trunnel/libor-trunnel-testing.a \
@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@ \
@TOR_SYSTEMD_LIBS@
@@ -137,14 +187,18 @@ src_test_test_slow_LDADD = $(src_test_test_LDADD)
src_test_test_slow_LDFLAGS = $(src_test_test_LDFLAGS)
src_test_test_memwipe_CPPFLAGS = $(src_test_test_CPPFLAGS)
-src_test_test_memwipe_CFLAGS = $(src_test_test_CFLAGS)
+# Don't use bugtrap cflags here: memwipe tests require memory violations.
+src_test_test_memwipe_CFLAGS = $(TEST_CFLAGS)
src_test_test_memwipe_LDADD = $(src_test_test_LDADD)
-src_test_test_memwipe_LDFLAGS = $(src_test_test_LDFLAGS)
+# The LDFLAGS need to include the bugtrap cflags, or else we won't link
+# successfully with the libraries built with them.
+src_test_test_memwipe_LDFLAGS = $(src_test_test_LDFLAGS) @CFLAGS_BUGTRAP@
src_test_bench_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
src_test_bench_LDADD = src/or/libtor.a src/common/libor.a \
- src/common/libor-crypto.a $(LIBDONNA) \
+ src/common/libor-ctime.a \
+ src/common/libor-crypto.a $(LIBKECCAK_TINY) $(LIBDONNA) \
src/common/libor-event.a src/trunnel/libor-trunnel.a \
@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@ \
@@ -154,26 +208,43 @@ src_test_test_workqueue_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
src_test_test_workqueue_LDADD = src/or/libtor-testing.a \
src/common/libor-testing.a \
- src/common/libor-crypto-testing.a $(LIBDONNA) \
+ src/common/libor-ctime-testing.a \
+ src/common/libor-crypto-testing.a $(LIBKECCAK_TINY) $(LIBDONNA) \
src/common/libor-event-testing.a \
@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@
+src_test_test_timers_CPPFLAGS = $(src_test_test_CPPFLAGS)
+src_test_test_timers_CFLAGS = $(src_test_test_CFLAGS)
+src_test_test_timers_LDADD = \
+ src/common/libor-testing.a \
+ src/common/libor-ctime-testing.a \
+ src/common/libor-event-testing.a \
+ src/common/libor-crypto-testing.a $(LIBKECCAK_TINY) $(LIBDONNA) \
+ @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
+ @TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@
+src_test_test_timers_LDFLAGS = $(src_test_test_LDFLAGS)
+
noinst_HEADERS+= \
src/test/fakechans.h \
+ src/test/log_test_helpers.h \
+ src/test/rend_test_helpers.h \
src/test/test.h \
src/test/test_helpers.h \
+ src/test/test_dir_common.h \
src/test/test_descriptors.inc \
src/test/example_extrainfo.inc \
src/test/failing_routerdescs.inc \
src/test/ed25519_vectors.inc \
- src/test/test_descriptors.inc
+ src/test/test_descriptors.inc \
+ src/test/vote_descriptors.inc
noinst_PROGRAMS+= src/test/test-ntor-cl
src_test_test_ntor_cl_SOURCES = src/test/test_ntor_cl.c
src_test_test_ntor_cl_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@
src_test_test_ntor_cl_LDADD = src/or/libtor.a src/common/libor.a \
- src/common/libor-crypto.a $(LIBDONNA) \
+ src/common/libor-ctime.a \
+ src/common/libor-crypto.a $(LIBKECCAK_TINY) $(LIBDONNA) \
@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@
src_test_test_ntor_cl_AM_CPPFLAGS = \
@@ -182,6 +253,7 @@ src_test_test_ntor_cl_AM_CPPFLAGS = \
noinst_PROGRAMS += src/test/test-bt-cl
src_test_test_bt_cl_SOURCES = src/test/test_bt_cl.c
src_test_test_bt_cl_LDADD = src/common/libor-testing.a \
+ src/common/libor-ctime-testing.a \
@TOR_LIB_MATH@ \
@TOR_LIB_WS32@ @TOR_LIB_GDI@
src_test_test_bt_cl_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
@@ -193,6 +265,14 @@ EXTRA_DIST += \
src/test/slownacl_curve25519.py \
src/test/zero_length_keys.sh \
src/test/test_keygen.sh \
- src/test/test_zero_length_keys.sh \
- src/test/test_ntor.sh src/test/test_bt.sh \
- src/test/test-network.sh
+ src/test/test_zero_length_keys.sh \
+ src/test/test_ntor.sh src/test/test_bt.sh \
+ src/test/test-network.sh \
+ src/test/test_switch_id.sh \
+ src/test/test_workqueue_cancel.sh \
+ src/test/test_workqueue_efd.sh \
+ src/test/test_workqueue_efd2.sh \
+ src/test/test_workqueue_pipe.sh \
+ src/test/test_workqueue_pipe2.sh \
+ src/test/test_workqueue_socketpair.sh
+
diff --git a/src/test/log_test_helpers.c b/src/test/log_test_helpers.c
new file mode 100644
index 0000000000..c788a33c17
--- /dev/null
+++ b/src/test/log_test_helpers.c
@@ -0,0 +1,241 @@
+/* Copyright (c) 2015-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+#define LOG_PRIVATE
+#include "torlog.h"
+#include "log_test_helpers.h"
+
+/**
+ * \file log_test_helpers.c
+ * \brief Code to check for expected log messages during testing.
+ */
+
+static void mock_saving_logv(int severity, log_domain_mask_t domain,
+ const char *funcname, const char *suffix,
+ const char *format, va_list ap)
+ CHECK_PRINTF(5, 0);
+
+/**
+ * Smartlist of all the logs we've received since we last set up
+ * log capture.
+ */
+static smartlist_t *saved_logs = NULL;
+
+/** Boolean: should we also send messages to the test-runner? */
+static int echo_to_real_logs = 1;
+
+/** Record logs at this level or more severe */
+static int record_logs_at_level = LOG_ERR;
+
+static int saved_log_level = 0;
+
+/**
+ * As setup_capture_of_logs, but do not relay log messages into the main
+ * logging system.
+ *
+ * Avoid using this function; use setup_capture_of_logs() instead if you
+ * can. If you must use this function, then make sure you detect any
+ * unexpected log messages, and treat them as test failures. */
+void
+setup_full_capture_of_logs(int new_level)
+{
+ setup_capture_of_logs(new_level);
+ echo_to_real_logs = 0;
+}
+
+/**
+ * Temporarily capture all the messages logged at severity <b>new_level</b> or
+ * higher.
+ *
+ * This function does not prevent messages from being sent to the main
+ * logging system.
+ */
+void
+setup_capture_of_logs(int new_level)
+{
+ if (saved_log_level == 0) {
+ saved_log_level = log_global_min_severity_;
+ } else {
+ tor_assert(0);
+ }
+
+ /* Only change the log_global_min_severity_ if we're making things _more_
+ * verbose. Otherwise we could prevent real log messages that the test-
+ * runner wanted.
+ */
+ if (log_global_min_severity_ < new_level)
+ log_global_min_severity_ = new_level;
+
+ record_logs_at_level = new_level;
+ mock_clean_saved_logs();
+ saved_logs = smartlist_new();
+ MOCK(logv, mock_saving_logv);
+ echo_to_real_logs = 1;
+}
+
+/**
+ * Undo setup_capture_of_logs().
+ *
+ * This function is safe to call more than once.
+ */
+void
+teardown_capture_of_logs(void)
+{
+ UNMOCK(logv);
+ if (saved_log_level)
+ log_global_min_severity_ = saved_log_level;
+ saved_log_level = 0;
+ mock_clean_saved_logs();
+}
+
+/**
+ * Clear all messages in mock_saved_logs()
+ */
+void
+mock_clean_saved_logs(void)
+{
+ if (!saved_logs)
+ return;
+ SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
+ { tor_free(m->generated_msg); tor_free(m); });
+ smartlist_free(saved_logs);
+ saved_logs = NULL;
+}
+
+/**
+ * Return a list of all the messages captured since the last
+ * setup_[full_]capture_of_logs() call. Each log call is recorded as a
+ * mock_saved_log_entry_t.
+ */
+const smartlist_t *
+mock_saved_logs(void)
+{
+ return saved_logs;
+}
+
+int
+mock_saved_log_n_entries(void)
+{
+ return saved_logs ? smartlist_len(saved_logs) : 0;
+}
+
+/**
+ * Return true iff there is a message recorded by log capture
+ * that is exactly equal to <b>msg</b>
+ */
+int
+mock_saved_log_has_message(const char *msg)
+{
+ if (saved_logs) {
+ SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
+ {
+ if (msg && m->generated_msg &&
+ !strcmp(msg, m->generated_msg)) {
+ return 1;
+ }
+ });
+ }
+
+ return 0;
+}
+
+/**
+ * Return true iff there is a message recorded by log capture
+ * that contains <b>msg</b> as a substring.
+ */
+int
+mock_saved_log_has_message_containing(const char *msg)
+{
+ if (saved_logs) {
+ SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
+ {
+ if (msg && m->generated_msg &&
+ strstr(m->generated_msg, msg)) {
+ return 1;
+ }
+ });
+ }
+
+ return 0;
+}
+
+/** Return true iff the saved logs have any messages with <b>severity</b> */
+int
+mock_saved_log_has_severity(int severity)
+{
+ int has_sev = 0;
+ if (saved_logs) {
+ SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
+ {
+ if (m->severity == severity) {
+ has_sev = 1;
+ }
+ });
+ }
+
+ return has_sev;
+}
+
+/** Return true iff the the saved logs have at lease one message */
+int
+mock_saved_log_has_entry(void)
+{
+ if (saved_logs) {
+ return smartlist_len(saved_logs) > 0;
+ }
+ return 0;
+}
+
+/* Replacement for logv: record the log message, and (maybe) send it
+ * into the logging system again.
+ */
+static void
+mock_saving_logv(int severity, log_domain_mask_t domain,
+ const char *funcname, const char *suffix,
+ const char *format, va_list ap)
+{
+ char *buf = tor_malloc_zero(10240);
+ int n;
+ n = tor_vsnprintf(buf,10240,format,ap);
+ tor_assert(n < 10240-1);
+ buf[n]='\n';
+ buf[n+1]='\0';
+
+ if (echo_to_real_logs) {
+ tor_log(severity, domain|LD_NO_MOCK, "%s", buf);
+ }
+
+ if (severity > record_logs_at_level) {
+ tor_free(buf);
+ return;
+ }
+
+ if (!saved_logs)
+ saved_logs = smartlist_new();
+
+ mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
+ e->severity = severity;
+ e->funcname = funcname;
+ e->suffix = suffix;
+ e->format = format;
+ e->generated_msg = tor_strdup(buf);
+ tor_free(buf);
+
+ smartlist_add(saved_logs, e);
+}
+
+void
+mock_dump_saved_logs(void)
+{
+ if (saved_logs == NULL) {
+ puts(" Captured logs: NULL");
+ return;
+ }
+
+ puts(" Captured logs:");
+ SMARTLIST_FOREACH_BEGIN(saved_logs, const mock_saved_log_entry_t *, m) {
+ printf("% 5d. %s: %s\n", m_sl_idx + 1,
+ log_level_to_string(m->severity),
+ escaped(m->generated_msg));
+ } SMARTLIST_FOREACH_END(m);
+}
+
diff --git a/src/test/log_test_helpers.h b/src/test/log_test_helpers.h
new file mode 100644
index 0000000000..922c68b42f
--- /dev/null
+++ b/src/test/log_test_helpers.h
@@ -0,0 +1,105 @@
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+
+#ifndef TOR_LOG_TEST_HELPERS_H
+#define TOR_LOG_TEST_HELPERS_H
+
+/** An element of mock_saved_logs(); records the log element that we
+ * received. */
+typedef struct mock_saved_log_entry_t {
+ int severity;
+ const char *funcname;
+ const char *suffix;
+ const char *format;
+ char *generated_msg;
+} mock_saved_log_entry_t;
+
+void mock_clean_saved_logs(void);
+const smartlist_t *mock_saved_logs(void);
+void setup_capture_of_logs(int new_level);
+void setup_full_capture_of_logs(int new_level);
+void teardown_capture_of_logs(void);
+
+int mock_saved_log_has_message(const char *msg);
+int mock_saved_log_has_message_containing(const char *msg);
+int mock_saved_log_has_severity(int severity);
+int mock_saved_log_has_entry(void);
+int mock_saved_log_n_entries(void);
+void mock_dump_saved_logs(void);
+
+#define assert_log_predicate(predicate, failure_msg) \
+ do { \
+ if (!(predicate)) { \
+ tt_fail_msg((failure_msg)); \
+ mock_dump_saved_logs(); \
+ TT_EXIT_TEST_FUNCTION; \
+ } \
+ } while (0)
+
+#define expect_log_msg(str) \
+ assert_log_predicate(mock_saved_log_has_message(str), \
+ "expected log to contain " # str);
+
+#define expect_log_msg_containing(str) \
+ assert_log_predicate(mock_saved_log_has_message_containing(str), \
+ "expected log to contain " # str);
+
+#define expect_log_msg_containing_either(str1, str2) \
+ assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
+ mock_saved_log_has_message_containing(str2), \
+ "expected log to contain " # str1 " or " # str2);
+
+#define expect_log_msg_containing_either3(str1, str2, str3) \
+ assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
+ mock_saved_log_has_message_containing(str2) || \
+ mock_saved_log_has_message_containing(str3), \
+ "expected log to contain " # str1 " or " # str2 \
+ " or " # str3);
+
+#define expect_log_msg_containing_either4(str1, str2, str3, str4) \
+ assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
+ mock_saved_log_has_message_containing(str2) || \
+ mock_saved_log_has_message_containing(str3) || \
+ mock_saved_log_has_message_containing(str4), \
+ "expected log to contain " # str1 " or " # str2 \
+ " or " # str3 " or " # str4);
+
+#define expect_single_log_msg(str) \
+ do { \
+ \
+ assert_log_predicate(mock_saved_log_has_message_containing(str) && \
+ mock_saved_log_n_entries() == 1, \
+ "expected log to contain exactly 1 message: " # str); \
+ } while (0);
+
+#define expect_single_log_msg_containing(str) \
+ do { \
+ assert_log_predicate(mock_saved_log_has_message_containing(str)&& \
+ mock_saved_log_n_entries() == 1 , \
+ "expected log to contain 1 message, containing" # str); \
+ } while (0);
+
+#define expect_no_log_msg(str) \
+ assert_log_predicate(!mock_saved_log_has_message(str), \
+ "expected log to not contain " # str);
+
+#define expect_log_severity(severity) \
+ assert_log_predicate(mock_saved_log_has_severity(severity), \
+ "expected log to contain severity " # severity);
+
+#define expect_no_log_severity(severity) \
+ assert_log_predicate(!mock_saved_log_has_severity(severity), \
+ "expected log to not contain severity " # severity);
+
+#define expect_log_entry() \
+ assert_log_predicate(mock_saved_log_has_entry(), \
+ "expected log to contain entries");
+
+#define expect_no_log_entry() \
+ assert_log_predicate(!mock_saved_log_has_entry(), \
+ "expected log to not contain entries");
+
+#endif
+
diff --git a/src/test/ntor_ref.py b/src/test/ntor_ref.py
index 767da57a9c..df065853f3 100755
--- a/src/test/ntor_ref.py
+++ b/src/test/ntor_ref.py
@@ -322,7 +322,7 @@ def kdf_vectors():
"""
import binascii
def kdf_vec(inp):
- k = kdf(inp, T_KEY, M_EXPAND, 100)
+ k = kdf_rfc5869(inp, T_KEY, M_EXPAND, 100)
print(repr(inp), "\n\""+ binascii.b2a_hex(k)+ "\"")
kdf_vec("")
kdf_vec("Tor")
diff --git a/src/test/rend_test_helpers.c b/src/test/rend_test_helpers.c
new file mode 100644
index 0000000000..377337bcb9
--- /dev/null
+++ b/src/test/rend_test_helpers.c
@@ -0,0 +1,73 @@
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+#include "test.h"
+#include "rendcommon.h"
+#include "rend_test_helpers.h"
+
+void
+generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
+ char **service_id, int intro_points)
+{
+ rend_service_descriptor_t *generated = NULL;
+ smartlist_t *descs = smartlist_new();
+ time_t now;
+
+ now = time(NULL) + time_diff;
+ create_descriptor(&generated, service_id, intro_points);
+ generated->timestamp = now;
+
+ rend_encode_v2_descriptors(descs, generated, now, 0, REND_NO_AUTH, NULL,
+ NULL);
+ tor_assert(smartlist_len(descs) > 1);
+ *desc = smartlist_get(descs, 0);
+ smartlist_set(descs, 0, NULL);
+
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ rend_service_descriptor_free(generated);
+}
+
+void
+create_descriptor(rend_service_descriptor_t **generated, char **service_id,
+ int intro_points)
+{
+ crypto_pk_t *pk1 = NULL;
+ crypto_pk_t *pk2 = NULL;
+ int i;
+
+ *service_id = tor_malloc(REND_SERVICE_ID_LEN_BASE32+1);
+ pk1 = pk_generate(0);
+ pk2 = pk_generate(1);
+
+ *generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ (*generated)->pk = crypto_pk_dup_key(pk1);
+ rend_get_service_id((*generated)->pk, *service_id);
+
+ (*generated)->version = 2;
+ (*generated)->protocols = 42;
+ (*generated)->intro_nodes = smartlist_new();
+
+ for (i = 0; i < intro_points; i++) {
+ rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
+ crypto_pk_t *okey = pk_generate(2 + i);
+ intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
+ intro->extend_info->onion_key = okey;
+ crypto_pk_get_digest(intro->extend_info->onion_key,
+ intro->extend_info->identity_digest);
+ intro->extend_info->nickname[0] = '$';
+ base16_encode(intro->extend_info->nickname + 1,
+ sizeof(intro->extend_info->nickname) - 1,
+ intro->extend_info->identity_digest, DIGEST_LEN);
+ tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
+ intro->extend_info->port = 1 + crypto_rand_int(65535);
+ intro->intro_key = crypto_pk_dup_key(pk2);
+ smartlist_add((*generated)->intro_nodes, intro);
+ }
+
+ crypto_pk_free(pk1);
+ crypto_pk_free(pk2);
+}
+
diff --git a/src/test/rend_test_helpers.h b/src/test/rend_test_helpers.h
new file mode 100644
index 0000000000..180a4e8fde
--- /dev/null
+++ b/src/test/rend_test_helpers.h
@@ -0,0 +1,15 @@
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+
+#ifndef TOR_REND_TEST_HELPERS_H
+#define TOR_REND_TEST_HELPERS_H
+
+void generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
+ char **service_id, int intro_points);
+void create_descriptor(rend_service_descriptor_t **generated,
+ char **service_id, int intro_points);
+
+#endif
+
diff --git a/src/test/sr_commit_calc_ref.py b/src/test/sr_commit_calc_ref.py
new file mode 100644
index 0000000000..45e629cfb0
--- /dev/null
+++ b/src/test/sr_commit_calc_ref.py
@@ -0,0 +1,51 @@
+# This is a reference implementation of the COMMIT/REVEAL calculation for
+# prop250. We use it to generate a test vector for the test_encoding()
+# unittest.
+#
+# Here is the computation formula:
+#
+# H = SHA3-256
+# TIMESTAMP = 8 bytes network-endian value
+# RAND = H(32 bytes of random)
+#
+# REVEAL = base64-encode( TIMESTAMP || RAND )
+# COMMIT = base64-encode( TIMESTAMP || H(REVEAL) )
+#
+
+import sys
+import hashlib
+import struct
+import base64
+
+# Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
+# the pysha3 package (pip install pysha3).
+if sys.version_info < (3, 6):
+ import sha3
+
+# Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
+# used the old Keccak implementation. During the finalization of SHA3, NIST
+# changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
+# stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
+TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
+if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
+ print("pysha3 version is < 1.0. Please install from:")
+ print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
+ sys.exit(1)
+
+# TIMESTAMP
+ts = 1454333590
+# RAND
+data = 'A' * 32 # Yes very very random, NIST grade :).
+rand = hashlib.sha3_256(data)
+
+reveal = struct.pack('!Q', ts) + rand.digest()
+b64_reveal = base64.b64encode(reveal)
+print("REVEAL: %s" % (b64_reveal))
+
+# Yes we do hash the _encoded_ reveal here that is H(REVEAL)
+hashed_reveal = hashlib.sha3_256(b64_reveal)
+commit = struct.pack('!Q', ts) + hashed_reveal.digest()
+print("COMMIT: %s" % (base64.b64encode(commit)))
+
+# REVEAL: AAAAAFavXpZJxbwTupvaJCTeIUCQmOPxAMblc7ChL5H2nZKuGchdaA==
+# COMMIT: AAAAAFavXpbkBMzMQG7aNoaGLFNpm2Wkk1ozXhuWWqL//GynltxVAg==
diff --git a/src/test/sr_srv_calc_ref.py b/src/test/sr_srv_calc_ref.py
new file mode 100644
index 0000000000..492ca62b15
--- /dev/null
+++ b/src/test/sr_srv_calc_ref.py
@@ -0,0 +1,71 @@
+# This is a reference implementation of the SRV calculation for prop250. We
+# use it to generate a test vector for the test_sr_compute_srv() unittest.
+# (./test shared-random/sr_compute_srv)
+#
+# Here is the SRV computation formula:
+#
+# HASHED_REVEALS = H(ID_a | R_a | ID_b | R_b | ..)
+#
+# SRV = SHA3-256("shared-random" | INT_8(reveal_num) | INT_4(version) |
+# HASHED_REVEALS | previous_SRV)
+#
+
+import sys
+import hashlib
+import struct
+
+# Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
+# the pysha3 package (pip install pysha3).
+if sys.version_info < (3, 6):
+ import sha3
+
+# Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
+# used the old Keccak implementation. During the finalization of SHA3, NIST
+# changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
+# stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
+TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
+if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
+ print("pysha3 version is < 1.0. Please install from:")
+ print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
+ sys.exit(1)
+
+# In this example, we use three reveal values.
+reveal_num = 3
+version = 1
+
+# We set directly the ascii value because memset(buf, 'A', 20) makes it to 20
+# times "41" in the final string.
+
+# Identity and reveal value of dirauth a
+ID_a = 20 * "41" # RSA identity of 40 base16 bytes.
+R_a = 56 * 'A' # 56 base64 characters
+
+# Identity and reveal value of dirauth b
+ID_b = 20 * "42" # RSA identity of 40 base16 bytes.
+R_b = 56 * 'B' # 56 base64 characters
+
+# Identity and reveal value of dirauth c
+ID_c = 20 * "43" # RSA identity of 40 base16 bytes.
+R_c = 56 * 'C' # 56 base64 characters
+
+# Concatenate them all together and hash them to form HASHED_REVEALS.
+REVEALS = (ID_a + R_a + ID_b + R_b + ID_c + R_c).encode()
+hashed_reveals_object = hashlib.sha3_256(REVEALS)
+hashed_reveals = hashed_reveals_object.digest()
+
+previous_SRV = (32 * 'Z').encode()
+
+# Now form the message.
+#srv_msg = struct.pack('13sQL256ss', "shared-random", reveal_num, version,
+# hashed_reveals, previous_SRV)
+invariant_token = b"shared-random"
+srv_msg = invariant_token + \
+ struct.pack('!QL', reveal_num, version) + \
+ hashed_reveals + \
+ previous_SRV
+
+# Now calculate the HMAC
+srv = hashlib.sha3_256(srv_msg)
+print("%s" % srv.hexdigest().upper())
+
+# 2A9B1D6237DAB312A40F575DA85C147663E7ED3F80E9555395F15B515C74253D
diff --git a/src/test/test-child.c b/src/test/test-child.c
index 2ce01ea9bb..fdf3ccec0a 100644
--- a/src/test/test-child.c
+++ b/src/test/test-child.c
@@ -1,8 +1,8 @@
-/* Copyright (c) 2011-2015, The Tor Project, Inc. */
+/* Copyright (c) 2011-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#include <stdio.h>
#include "orconfig.h"
+#include <stdio.h>
#ifdef _WIN32
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
diff --git a/src/test/test-memwipe.c b/src/test/test-memwipe.c
index a39bad1540..21882448c3 100644
--- a/src/test/test-memwipe.c
+++ b/src/test/test-memwipe.c
@@ -1,3 +1,4 @@
+#include "orconfig.h"
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
@@ -5,9 +6,7 @@
#include "crypto.h"
#include "compat.h"
-
-#undef MIN
-#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
+#include "util.h"
static unsigned fill_a_buffer_memset(void) __attribute__((noinline));
static unsigned fill_a_buffer_memwipe(void) __attribute__((noinline));
@@ -17,6 +16,7 @@ static unsigned fill_heap_buffer_memwipe(void) __attribute__((noinline));
static unsigned fill_heap_buffer_nothing(void) __attribute__((noinline));
static unsigned check_a_buffer(void) __attribute__((noinline));
+extern const char *s; /* Make the linkage global */
const char *s = NULL;
#define BUF_LEN 2048
@@ -36,6 +36,12 @@ const char *s = NULL;
sum += (unsigned char)buf[i]; \
}
+#ifdef __OpenBSD__
+/* Disable some of OpenBSD's malloc protections for this test. This helps
+ * us do bad things, such as access freed buffers, without crashing. */
+const char *malloc_options="sufjj";
+#endif
+
static unsigned
fill_a_buffer_memset(void)
{
@@ -62,7 +68,7 @@ fill_a_buffer_nothing(void)
return sum;
}
-static INLINE int
+static inline int
vmemeq(volatile char *a, const char *b, size_t n)
{
while (n--) {
@@ -100,29 +106,29 @@ static char *heap_buf = NULL;
static unsigned
fill_heap_buffer_memset(void)
{
- char *buf = heap_buf = malloc(BUF_LEN);
+ char *buf = heap_buf = raw_malloc(BUF_LEN);
FILL_BUFFER_IMPL()
memset(buf, 0, BUF_LEN);
- free(buf);
+ raw_free(buf);
return sum;
}
static unsigned
fill_heap_buffer_memwipe(void)
{
- char *buf = heap_buf = malloc(BUF_LEN);
+ char *buf = heap_buf = raw_malloc(BUF_LEN);
FILL_BUFFER_IMPL()
memwipe(buf, 0, BUF_LEN);
- free(buf);
+ raw_free(buf);
return sum;
}
static unsigned
fill_heap_buffer_nothing(void)
{
- char *buf = heap_buf = malloc(BUF_LEN);
+ char *buf = heap_buf = raw_malloc(BUF_LEN);
FILL_BUFFER_IMPL()
- free(buf);
+ raw_free(buf);
return sum;
}
diff --git a/src/test/test-network.sh b/src/test/test-network.sh
index 05080e0c52..4d9776822b 100755
--- a/src/test/test-network.sh
+++ b/src/test/test-network.sh
@@ -1,7 +1,43 @@
#! /bin/sh
+# Please do not modify this script, it has been moved to chutney/tools
+
ECHO_N="/bin/echo -n"
-use_coverage_binary=false
+
+# Output is prefixed with the name of the script
+myname=$(basename $0)
+
+# We need to find CHUTNEY_PATH, so that we can call the version of this script
+# in chutney/tools. And we want to pass any arguments to that script as well.
+# So we source this script, which processes its arguments to find CHUTNEY_PATH.
+
+# Avoid recursively sourcing this script, and don't call the chutney version
+# while recursing, either
+if [ "$TEST_NETWORK_RECURSING" != true ]; then
+ # Process the arguments into environmental variables with this script
+ # to make sure $CHUTNEY_PATH is set
+ # When we switch to using test-network.sh in chutney/tools, --dry-run
+ # can be removed, because this script will find chutney, then pass all
+ # arguments to chutney's test-network.sh
+ echo "$myname: Parsing command-line arguments to find \$CHUTNEY_PATH"
+ export TEST_NETWORK_RECURSING=true
+ . "$0" --dry-run "$@"
+
+ # Call the chutney version of this script, if it exists, and we can find it
+ if [ -d "$CHUTNEY_PATH" -a -x "$CHUTNEY_PATH/tools/test-network.sh" ]; then
+ unset NETWORK_DRY_RUN
+ echo "$myname: Calling newer chutney script \
+$CHUTNEY_PATH/tools/test-network.sh"
+ "$CHUTNEY_PATH/tools/test-network.sh" "$@"
+ exit $?
+ else
+ echo "$myname: This script has moved to chutney/tools."
+ echo "$myname: Please update your chutney using 'git pull'."
+ # When we switch to using test-network.sh in chutney/tools, we should
+ # exit with a very loud failure here
+ echo "$myname: Falling back to the old tor version of the script."
+ fi
+fi
until [ -z "$1" ]
do
@@ -14,6 +50,9 @@ do
export TOR_DIR="$2"
shift
;;
+ # When we switch to using test-network.sh in chutney/tools, only the
+ # --chutney-path and --tor-path arguments need to be processed by this
+ # script, everything else can be handled by chutney's test-network.sh
--flavor|--flavour|--network-flavor|--network-flavour)
export NETWORK_FLAVOUR="$2"
shift
@@ -30,8 +69,8 @@ do
;;
# Make this many connections per client (1)
# Note: If you create 7 or more connections to a hidden service from
- # a single client, you'll likely get a verification failure due to
- # https://trac.torproject.org/projects/tor/ticket/15937
+ # a single Tor 0.2.7 client, you'll likely get a verification failure due
+ # to #15937. This is fixed in 0.2.8.
--connections|--connection|--connection-count|--count)
export CHUTNEY_CONNECTIONS="$2"
shift
@@ -44,44 +83,101 @@ do
shift
;;
--coverage)
- use_coverage_binary=true
+ export USE_COVERAGE_BINARY=true
+ ;;
+ --dry-run)
+ # process arguments, but don't call any other scripts
+ export NETWORK_DRY_RUN=true
;;
*)
- echo "Sorry, I don't know what to do with '$1'."
- exit 2
+ echo "$myname: Sorry, I don't know what to do with '$1'."
+ echo "$myname: Maybe chutney's test-network.sh understands '$1'."
+ echo "$myname: Please update your chutney using 'git pull', and set \
+\$CHUTNEY_PATH"
+ # continue processing arguments during a dry run
+ if [ "$NETWORK_DRY_RUN" != true ]; then
+ exit 2
+ fi
;;
esac
shift
done
-TOR_DIR="${TOR_DIR:-$PWD}"
-NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
-CHUTNEY_NETWORK=networks/$NETWORK_FLAVOUR
-myname=$(basename $0)
-
-[ -n "$CHUTNEY_PATH" ] || {
- echo "$myname: \$CHUTNEY_PATH not set, trying $TOR_DIR/../chutney"
- CHUTNEY_PATH="$TOR_DIR/../chutney"
-}
+# optional: $TOR_DIR is the tor build directory
+# it's used to find the location of tor binaries
+# if it's not set:
+# - set it ro $BUILDDIR, or
+# - if $PWD looks like a tor build directory, set it to $PWD, or
+# - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
+if [ ! -d "$TOR_DIR" ]; then
+ if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
+ # Choose the build directory
+ # But only if it looks like one
+ echo "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
+ export TOR_DIR="$BUILDDIR"
+ elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
+ # Guess the tor directory is the current directory
+ # But only if it looks like one
+ echo "$myname: \$TOR_DIR not set, trying \$PWD"
+ export TOR_DIR="$PWD"
+ else
+ echo "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
+ unset TOR_DIR
+ fi
+fi
-[ -d "$CHUTNEY_PATH" ] && [ -x "$CHUTNEY_PATH/chutney" ] || {
- echo "$myname: missing 'chutney' in CHUTNEY_PATH ($CHUTNEY_PATH)"
- echo "$myname: Get chutney: git clone https://git.torproject.org/\
+# mandatory: $CHUTNEY_PATH is the path to the chutney launch script
+# if it's not set:
+# - if $PWD looks like a chutney directory, set it to $PWD, or
+# - set it based on $TOR_DIR, expecting chutney to be next to tor, or
+# - fail and tell the user how to clone the chutney repository
+if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" ]; then
+ if [ -x "$PWD/chutney" ]; then
+ echo "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
+ export CHUTNEY_PATH="$PWD"
+ elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
+ -x "$TOR_DIR/../chutney/chutney" ]; then
+ echo "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
+ export CHUTNEY_PATH="$TOR_DIR/../chutney"
+ else
+ # TODO: work out how to package and install chutney,
+ # so users can find it in $PATH
+ echo "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
+ echo "$myname: Get chutney: git clone https://git.torproject.org/\
chutney.git"
- echo "$myname: Set \$CHUTNEY_PATH to a non-standard location: export CHUTNEY_PATH=\`pwd\`/chutney"
- exit 1
-}
+ echo "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
+CHUTNEY_PATH=\`pwd\`/chutney"
+ unset CHUTNEY_PATH
+ exit 1
+ fi
+fi
+
+# When we switch to using test-network.sh in chutney/tools, this comment and
+# everything below it can be removed
-cd "$CHUTNEY_PATH"
# For picking up the right tor binaries.
-tor_name=tor
-tor_gencert_name=tor-gencert
-if test "$use_coverage_binary" = true; then
- tor_name=tor-cov
+# If these varibles aren't set, chutney looks for tor binaries in $PATH
+if [ -d "$TOR_DIR" ]; then
+ tor_name=tor
+ tor_gencert_name=tor-gencert
+ if [ "$USE_COVERAGE_BINARY" = true ]; then
+ tor_name=tor-cov
+ fi
+ export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
+ export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
fi
-export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
-export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
+# Set the variables for the chutney network flavour
+export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
+export CHUTNEY_NETWORK=networks/$NETWORK_FLAVOUR
+
+# And finish up if we're doing a dry run
+if [ "$NETWORK_DRY_RUN" = true ]; then
+ # we can't exit here, it breaks argument processing
+ return
+fi
+
+cd "$CHUTNEY_PATH"
./tools/bootstrap-network.sh $NETWORK_FLAVOUR || exit 2
# Sleep some, waiting for the network to bootstrap.
diff --git a/src/test/test-timers.c b/src/test/test-timers.c
new file mode 100644
index 0000000000..b5fcade7f8
--- /dev/null
+++ b/src/test/test-timers.c
@@ -0,0 +1,144 @@
+/* Copyright 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <event2/event.h>
+
+#include "compat.h"
+#include "compat_libevent.h"
+#include "crypto.h"
+#include "timers.h"
+#include "util.h"
+
+#define N_TIMERS 1000
+#define MAX_DURATION 30
+#define N_DISABLE 5
+
+static struct timeval fire_at[N_TIMERS] = { {0,0} };
+static int is_disabled[N_TIMERS] = {0};
+static int fired[N_TIMERS] = {0};
+static struct timeval difference[N_TIMERS] = { {0,0} };
+static tor_timer_t *timers[N_TIMERS] = {NULL};
+
+static int n_active_timers = 0;
+static int n_fired = 0;
+
+static monotime_t started_at;
+static int64_t delay_usec[N_TIMERS];
+static int64_t diffs_mono_usec[N_TIMERS];
+
+static void
+timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
+{
+ struct timeval now;
+
+ tor_gettimeofday(&now);
+ tor_timer_t **t_ptr = arg;
+ tor_assert(*t_ptr == t);
+ int idx = (int) (t_ptr - timers);
+ ++fired[idx];
+ timersub(&now, &fire_at[idx], &difference[idx]);
+ diffs_mono_usec[idx] =
+ monotime_diff_usec(&started_at, now_mono) -
+ delay_usec[idx];
+ ++n_fired;
+
+ // printf("%d / %d\n",n_fired, N_TIMERS);
+ if (n_fired == n_active_timers) {
+ event_base_loopbreak(tor_libevent_get_base());
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ (void)argc;
+ (void)argv;
+ tor_libevent_cfg cfg;
+ memset(&cfg, 0, sizeof(cfg));
+ tor_libevent_initialize(&cfg);
+ timers_initialize();
+
+ int i;
+ int ret;
+ struct timeval now;
+ tor_gettimeofday(&now);
+ monotime_get(&started_at);
+ for (i = 0; i < N_TIMERS; ++i) {
+ struct timeval delay;
+ delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
+ delay.tv_usec = crypto_rand_int_range(0,1000000);
+ delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
+ timeradd(&now, &delay, &fire_at[i]);
+ timers[i] = timer_new(timer_cb, &timers[i]);
+ timer_schedule(timers[i], &delay);
+ ++n_active_timers;
+ }
+
+ /* Disable some; we'll make sure they don't trigger. */
+ for (i = 0; i < N_DISABLE; ++i) {
+ int idx = crypto_rand_int_range(0, N_TIMERS);
+ if (is_disabled[idx])
+ continue;
+ is_disabled[idx] = 1;
+ timer_disable(timers[idx]);
+ --n_active_timers;
+ }
+
+ event_base_loop(tor_libevent_get_base(), 0);
+
+ int64_t total_difference = 0;
+ uint64_t total_square_difference = 0;
+ tor_assert(n_fired == n_active_timers);
+ for (i = 0; i < N_TIMERS; ++i) {
+ if (is_disabled[i]) {
+ tor_assert(fired[i] == 0);
+ continue;
+ }
+ tor_assert(fired[i] == 1);
+ //int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
+ int64_t diff = diffs_mono_usec[i];
+ total_difference += diff;
+ total_square_difference += diff*diff;
+ }
+ const int64_t mean_diff = total_difference / n_active_timers;
+ printf("mean difference: "I64_FORMAT" usec\n",
+ I64_PRINTF_ARG(mean_diff));
+
+ const double mean_sq = ((double)total_square_difference)/ n_active_timers;
+ const double sq_mean = mean_diff * mean_diff;
+ const double stddev = sqrt(mean_sq - sq_mean);
+ printf("standard deviation: %lf usec\n", stddev);
+
+#define MAX_DIFF_USEC (500*1000)
+#define MAX_STDDEV_USEC (500*1000)
+#define ODD_DIFF_USEC (2000)
+#define ODD_STDDEV_USEC (2000)
+
+ if (mean_diff < 0 || mean_diff > MAX_DIFF_USEC || stddev > MAX_STDDEV_USEC) {
+ printf("Either your system is under ridiculous load, or the "
+ "timer backend is broken.\n");
+ ret = 1;
+ } else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
+ printf("Either your system is a bit slow or the "
+ "timer backend is odd.\n");
+ ret = 0;
+ } else {
+ printf("Looks good enough.\n");
+ ret = 0;
+ }
+
+ timer_free(NULL);
+
+ for (i = 0; i < N_TIMERS; ++i) {
+ timer_free(timers[i]);
+ }
+ timers_shutdown();
+ return ret;
+}
+
diff --git a/src/test/test.c b/src/test/test.c
index e10e260266..9a41b976b8 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
@@ -28,6 +28,7 @@
#define ROUTER_PRIVATE
#define CIRCUITSTATS_PRIVATE
#define CIRCUITLIST_PRIVATE
+#define MAIN_PRIVATE
#define STATEFILE_PRIVATE
/*
@@ -47,11 +48,14 @@ double fabs(double x);
#include "connection_edge.h"
#include "geoip.h"
#include "rendcommon.h"
+#include "rendcache.h"
#include "test.h"
#include "torgzip.h"
+#include "main.h"
#include "memarea.h"
#include "onion.h"
#include "onion_ntor.h"
+#include "onion_fast.h"
#include "onion_tap.h"
#include "policies.h"
#include "rephist.h"
@@ -175,20 +179,26 @@ test_bad_onion_handshake(void *arg)
s_buf, s_keys, 40));
/* Client: Case 1: The server sent back junk. */
+ const char *msg = NULL;
s_buf[64] ^= 33;
tt_int_op(-1, OP_EQ,
- onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
+ onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
s_buf[64] ^= 33;
+ tt_str_op(msg, OP_EQ, "Digest DOES NOT MATCH on onion handshake. "
+ "Bug or attack.");
/* Let the client finish; make sure it can. */
+ msg = NULL;
tt_int_op(0, OP_EQ,
- onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
+ onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
tt_mem_op(s_keys,OP_EQ, c_keys, 40);
+ tt_ptr_op(msg, OP_EQ, NULL);
/* Client: Case 2: The server sent back a degenerate DH. */
memset(s_buf, 0, sizeof(s_buf));
tt_int_op(-1, OP_EQ,
- onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
+ onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
+ tt_str_op(msg, OP_EQ, "DH computation failed.");
done:
crypto_dh_free(c_dh);
@@ -243,11 +253,56 @@ test_ntor_handshake(void *arg)
memset(s_buf, 0, 40);
tt_mem_op(c_keys,OP_NE, s_buf, 40);
+ /* Now try with a bogus server response. Zero input should trigger
+ * All The Problems. */
+ memset(c_keys, 0, 400);
+ memset(s_buf, 0, NTOR_REPLY_LEN);
+ const char *msg = NULL;
+ tt_int_op(-1, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
+ c_keys, 400, &msg));
+ tt_str_op(msg, OP_EQ, "Zero output from curve25519 handshake");
+
done:
ntor_handshake_state_free(c_state);
dimap_free(s_keymap, NULL);
}
+static void
+test_fast_handshake(void *arg)
+{
+ /* tests for the obsolete "CREATE_FAST" handshake. */
+ (void) arg;
+ fast_handshake_state_t *state = NULL;
+ uint8_t client_handshake[CREATE_FAST_LEN];
+ uint8_t server_handshake[CREATED_FAST_LEN];
+ uint8_t s_keys[100], c_keys[100];
+
+ /* First, test an entire handshake. */
+ memset(client_handshake, 0, sizeof(client_handshake));
+ tt_int_op(0, OP_EQ, fast_onionskin_create(&state, client_handshake));
+ tt_assert(! tor_mem_is_zero((char*)client_handshake,
+ sizeof(client_handshake)));
+
+ tt_int_op(0, OP_EQ,
+ fast_server_handshake(client_handshake, server_handshake,
+ s_keys, 100));
+ const char *msg = NULL;
+ tt_int_op(0, OP_EQ,
+ fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
+ tt_ptr_op(msg, OP_EQ, NULL);
+ tt_mem_op(s_keys, OP_EQ, c_keys, 100);
+
+ /* Now test a failing handshake. */
+ server_handshake[0] ^= 3;
+ tt_int_op(-1, OP_EQ,
+ fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
+ tt_str_op(msg, OP_EQ, "Digest DOES NOT MATCH on fast handshake. "
+ "Bug or attack.");
+
+ done:
+ fast_handshake_state_free(state);
+}
+
/** Run unit tests for the onion queues. */
static void
test_onion_queues(void *arg)
@@ -316,6 +371,9 @@ test_circuit_timeout(void *arg)
int i, runs;
double close_ms;
(void)arg;
+
+ initialize_periodic_events();
+
circuit_build_times_init(&initial);
circuit_build_times_init(&estimate);
circuit_build_times_init(&final);
@@ -455,6 +513,7 @@ test_circuit_timeout(void *arg)
circuit_build_times_free_timeouts(&estimate);
circuit_build_times_free_timeouts(&final);
or_state_free(state);
+ teardown_periodic_events();
}
/** Test encoding and parsing of rendezvous service descriptors. */
@@ -494,6 +553,9 @@ test_rend_fns(void *arg)
tt_str_op(address6,OP_EQ, "abcdefghijklmnop");
tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
+ /* Initialize the service cache. */
+ rend_cache_init();
+
pk1 = pk_generate(0);
pk2 = pk_generate(1);
generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
@@ -1105,8 +1167,9 @@ static struct testcase_t test_array[] = {
{ "bad_onion_handshake", test_bad_onion_handshake, 0, NULL, NULL },
ENT(onion_queues),
{ "ntor_handshake", test_ntor_handshake, 0, NULL, NULL },
- ENT(circuit_timeout),
- ENT(rend_fns),
+ { "fast_handshake", test_fast_handshake, 0, NULL, NULL },
+ FORK(circuit_timeout),
+ FORK(rend_fns),
ENT(geoip),
FORK(geoip_with_pt),
FORK(stats),
@@ -1114,52 +1177,6 @@ static struct testcase_t test_array[] = {
END_OF_TESTCASES
};
-extern struct testcase_t accounting_tests[];
-extern struct testcase_t addr_tests[];
-extern struct testcase_t address_tests[];
-extern struct testcase_t buffer_tests[];
-extern struct testcase_t cell_format_tests[];
-extern struct testcase_t cell_queue_tests[];
-extern struct testcase_t channel_tests[];
-extern struct testcase_t channeltls_tests[];
-extern struct testcase_t checkdir_tests[];
-extern struct testcase_t circuitlist_tests[];
-extern struct testcase_t circuitmux_tests[];
-extern struct testcase_t config_tests[];
-extern struct testcase_t container_tests[];
-extern struct testcase_t controller_tests[];
-extern struct testcase_t controller_event_tests[];
-extern struct testcase_t crypto_tests[];
-extern struct testcase_t dir_tests[];
-extern struct testcase_t entryconn_tests[];
-extern struct testcase_t entrynodes_tests[];
-extern struct testcase_t guardfraction_tests[];
-extern struct testcase_t extorport_tests[];
-extern struct testcase_t hs_tests[];
-extern struct testcase_t introduce_tests[];
-extern struct testcase_t keypin_tests[];
-extern struct testcase_t link_handshake_tests[];
-extern struct testcase_t logging_tests[];
-extern struct testcase_t microdesc_tests[];
-extern struct testcase_t nodelist_tests[];
-extern struct testcase_t oom_tests[];
-extern struct testcase_t options_tests[];
-extern struct testcase_t policy_tests[];
-extern struct testcase_t pt_tests[];
-extern struct testcase_t relay_tests[];
-extern struct testcase_t relaycell_tests[];
-extern struct testcase_t replaycache_tests[];
-extern struct testcase_t router_tests[];
-extern struct testcase_t routerkeys_tests[];
-extern struct testcase_t routerlist_tests[];
-extern struct testcase_t routerset_tests[];
-extern struct testcase_t scheduler_tests[];
-extern struct testcase_t socks_tests[];
-extern struct testcase_t status_tests[];
-extern struct testcase_t thread_tests[];
-extern struct testcase_t util_tests[];
-extern struct testcase_t dns_tests[];
-
struct testgroup_t testgroups[] = {
{ "", test_array },
{ "accounting/", accounting_tests },
@@ -1173,12 +1190,15 @@ struct testgroup_t testgroups[] = {
{ "checkdir/", checkdir_tests },
{ "circuitlist/", circuitlist_tests },
{ "circuitmux/", circuitmux_tests },
+ { "compat/libevent/", compat_libevent_tests },
{ "config/", config_tests },
+ { "connection/", connection_tests },
{ "container/", container_tests },
{ "control/", controller_tests },
{ "control/event/", controller_event_tests },
{ "crypto/", crypto_tests },
{ "dir/", dir_tests },
+ { "dir_handle_get/", dir_handle_get_tests },
{ "dir/md/", microdesc_tests },
{ "entryconn/", entryconn_tests },
{ "entrynodes/", entrynodes_tests },
@@ -1190,21 +1210,31 @@ struct testgroup_t testgroups[] = {
{ "link-handshake/", link_handshake_tests },
{ "nodelist/", nodelist_tests },
{ "oom/", oom_tests },
+ { "oos/", oos_tests },
{ "options/", options_tests },
{ "policy/" , policy_tests },
+ { "procmon/", procmon_tests },
+ { "protover/", protover_tests },
{ "pt/", pt_tests },
{ "relay/" , relay_tests },
{ "relaycell/", relaycell_tests },
+ { "rend_cache/", rend_cache_tests },
{ "replaycache/", replaycache_tests },
{ "routerkeys/", routerkeys_tests },
{ "routerlist/", routerlist_tests },
{ "routerset/" , routerset_tests },
{ "scheduler/", scheduler_tests },
{ "socks/", socks_tests },
+ { "shared-random/", sr_tests },
{ "status/" , status_tests },
+ { "tortls/", tortls_tests },
{ "util/", util_tests },
+ { "util/format/", util_format_tests },
{ "util/logging/", logging_tests },
+ { "util/process/", util_process_tests },
+ { "util/pubsub/", pubsub_tests },
{ "util/thread/", thread_tests },
+ { "util/handle/", handle_tests },
{ "dns/", dns_tests },
END_OF_GROUPS
};
diff --git a/src/test/test.h b/src/test/test.h
index 86699c3d07..25336ac83e 100644
--- a/src/test/test.h
+++ b/src/test/test.h
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2003, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#ifndef TOR_TEST_H
@@ -73,7 +73,8 @@
{print_ = (I64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
const char *get_fname(const char *name);
-crypto_pk_t *pk_generate(int idx);
+const char *get_fname_rnd(const char *name);
+struct crypto_pk_t *pk_generate(int idx);
#define US2_CONCAT_2__(a, b) a ## __ ## b
#define US_CONCAT_2__(a, b) a ## _ ## b
@@ -163,11 +164,90 @@ crypto_pk_t *pk_generate(int idx);
#define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
#define NS_DECL(retval, mock_fn, args) \
+ extern int CALLED(mock_fn); \
static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
#define NS_MOCK(name) MOCK(name, NS(name))
#define NS_UNMOCK(name) UNMOCK(name)
extern const struct testcase_setup_t passthrough_setup;
+extern const struct testcase_setup_t ed25519_test_setup;
+
+extern struct testcase_t accounting_tests[];
+extern struct testcase_t addr_tests[];
+extern struct testcase_t address_tests[];
+extern struct testcase_t buffer_tests[];
+extern struct testcase_t cell_format_tests[];
+extern struct testcase_t cell_queue_tests[];
+extern struct testcase_t channel_tests[];
+extern struct testcase_t channeltls_tests[];
+extern struct testcase_t checkdir_tests[];
+extern struct testcase_t circuitlist_tests[];
+extern struct testcase_t circuitmux_tests[];
+extern struct testcase_t compat_libevent_tests[];
+extern struct testcase_t config_tests[];
+extern struct testcase_t connection_tests[];
+extern struct testcase_t container_tests[];
+extern struct testcase_t controller_tests[];
+extern struct testcase_t controller_event_tests[];
+extern struct testcase_t crypto_tests[];
+extern struct testcase_t dir_tests[];
+extern struct testcase_t dir_handle_get_tests[];
+extern struct testcase_t entryconn_tests[];
+extern struct testcase_t entrynodes_tests[];
+extern struct testcase_t guardfraction_tests[];
+extern struct testcase_t extorport_tests[];
+extern struct testcase_t hs_tests[];
+extern struct testcase_t introduce_tests[];
+extern struct testcase_t keypin_tests[];
+extern struct testcase_t link_handshake_tests[];
+extern struct testcase_t logging_tests[];
+extern struct testcase_t microdesc_tests[];
+extern struct testcase_t nodelist_tests[];
+extern struct testcase_t oom_tests[];
+extern struct testcase_t oos_tests[];
+extern struct testcase_t options_tests[];
+extern struct testcase_t policy_tests[];
+extern struct testcase_t procmon_tests[];
+extern struct testcase_t protover_tests[];
+extern struct testcase_t pubsub_tests[];
+extern struct testcase_t pt_tests[];
+extern struct testcase_t relay_tests[];
+extern struct testcase_t relaycell_tests[];
+extern struct testcase_t rend_cache_tests[];
+extern struct testcase_t replaycache_tests[];
+extern struct testcase_t router_tests[];
+extern struct testcase_t routerkeys_tests[];
+extern struct testcase_t routerlist_tests[];
+extern struct testcase_t routerset_tests[];
+extern struct testcase_t scheduler_tests[];
+extern struct testcase_t socks_tests[];
+extern struct testcase_t status_tests[];
+extern struct testcase_t thread_tests[];
+extern struct testcase_t tortls_tests[];
+extern struct testcase_t util_tests[];
+extern struct testcase_t util_format_tests[];
+extern struct testcase_t util_process_tests[];
+extern struct testcase_t dns_tests[];
+extern struct testcase_t handle_tests[];
+extern struct testcase_t sr_tests[];
+
+extern struct testcase_t slow_crypto_tests[];
+extern struct testcase_t slow_util_tests[];
+
+extern struct testgroup_t testgroups[];
+
+extern const char AUTHORITY_CERT_1[];
+extern const char AUTHORITY_SIGNKEY_1[];
+extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
+extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
+extern const char AUTHORITY_CERT_2[];
+extern const char AUTHORITY_SIGNKEY_2[];
+extern const char AUTHORITY_SIGNKEY_B_DIGEST[];
+extern const char AUTHORITY_SIGNKEY_B_DIGEST256[];
+extern const char AUTHORITY_CERT_3[];
+extern const char AUTHORITY_SIGNKEY_3[];
+extern const char AUTHORITY_SIGNKEY_C_DIGEST[];
+extern const char AUTHORITY_SIGNKEY_C_DIGEST256[];
#endif
diff --git a/src/test/test_accounting.c b/src/test/test_accounting.c
index 25908e942c..7edba988a6 100644
--- a/src/test/test_accounting.c
+++ b/src/test/test_accounting.c
@@ -61,6 +61,32 @@ test_accounting_limits(void *arg)
fake_time += 1;
consider_hibernation(fake_time);
tor_assert(we_are_hibernating() == 1);
+
+ options->AccountingRule = ACCT_OUT;
+
+ accounting_add_bytes(100, 10, 1);
+ fake_time += 1;
+ consider_hibernation(fake_time);
+ tor_assert(we_are_hibernating() == 0);
+
+ accounting_add_bytes(0, 90, 1);
+ fake_time += 1;
+ consider_hibernation(fake_time);
+ tor_assert(we_are_hibernating() == 1);
+
+ options->AccountingMax = 300;
+ options->AccountingRule = ACCT_IN;
+
+ accounting_add_bytes(10, 100, 1);
+ fake_time += 1;
+ consider_hibernation(fake_time);
+ tor_assert(we_are_hibernating() == 0);
+
+ accounting_add_bytes(90, 0, 1);
+ fake_time += 1;
+ consider_hibernation(fake_time);
+ tor_assert(we_are_hibernating() == 1);
+
goto done;
done:
NS_UNMOCK(get_or_state);
diff --git a/src/test/test_addr.c b/src/test/test_addr.c
index 0f77e73630..be440a0925 100644
--- a/src/test/test_addr.c
+++ b/src/test/test_addr.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define ADDRESSMAP_PRIVATE
@@ -81,14 +81,14 @@ test_addr_basic(void *arg)
#define test_op_ip6_(a,op,b,e1,e2) \
STMT_BEGIN \
tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
- (memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
+ (fast_memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
char *, "%s", \
- { int i; char *cp; \
+ { char *cp; \
cp = print_ = tor_malloc(64); \
- for (i=0;i<16;++i) { \
- tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[i]);\
+ for (int ii_=0;ii_<16;++ii_) { \
+ tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[ii_]); \
cp += 2; \
- if (i != 15) *cp++ = ':'; \
+ if (ii_ != 15) *cp++ = ':'; \
} \
}, \
{ tor_free(print_); }, \
@@ -302,6 +302,7 @@ test_addr_ip6_helpers(void *arg)
//test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
+ test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
test_ntop6_reduces("008:0::0", "8::");
@@ -1045,17 +1046,17 @@ test_addr_make_null(void *data)
(void) data;
/* Ensure that before tor_addr_make_null, addr != 0's */
memset(addr, 1, sizeof(*addr));
- tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
+ tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
/* Test with AF == AF_INET */
zeros->family = AF_INET;
tor_addr_make_null(addr, AF_INET);
- tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
+ tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "0.0.0.0");
/* Test with AF == AF_INET6 */
memset(addr, 1, sizeof(*addr));
zeros->family = AF_INET6;
tor_addr_make_null(addr, AF_INET6);
- tt_int_op(memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
+ tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "::");
done:
tor_free(addr);
diff --git a/src/test/test_address.c b/src/test/test_address.c
index 3e73c3e27b..0d142ad483 100644
--- a/src/test/test_address.c
+++ b/src/test/test_address.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define ADDRESS_PRIVATE
@@ -26,6 +26,7 @@
#include "or.h"
#include "address.h"
#include "test.h"
+#include "log_test_helpers.h"
/** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
* the same IP address and port combination. Otherwise, return 0.
@@ -119,6 +120,21 @@ smartlist_contains_internal_tor_addr(smartlist_t *smartlist)
}
/** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
+ * that is NULL or the null tor_addr_t. Otherwise, return 0.
+ */
+static int
+smartlist_contains_null_tor_addr(smartlist_t *smartlist)
+{
+ SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
+ if (tor_addr == NULL || tor_addr_is_null(tor_addr)) {
+ return 1;
+ }
+ } SMARTLIST_FOREACH_END(tor_addr);
+
+ return 0;
+}
+
+/** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
* that is an IPv4 address. Otherwise, return 0.
*/
static int
@@ -205,7 +221,7 @@ test_address_ifaddrs_to_smartlist(void *arg)
ifa_ipv6->ifa_dstaddr = NULL;
ifa_ipv6->ifa_data = NULL;
- smartlist = ifaddrs_to_smartlist(ifa);
+ smartlist = ifaddrs_to_smartlist(ifa, AF_UNSPEC);
tt_assert(smartlist);
tt_assert(smartlist_len(smartlist) == 3);
@@ -266,13 +282,25 @@ test_address_get_if_addrs_ifaddrs(void *arg)
(void)arg;
- results = get_interface_addresses_ifaddrs(LOG_ERR);
+ results = get_interface_addresses_ifaddrs(LOG_ERR, AF_UNSPEC);
- tt_int_op(smartlist_len(results),>=,1);
- tt_assert(smartlist_contains_localhost_tor_addr(results));
+ tt_assert(results);
+ /* Some FreeBSD jails don't have localhost IP address. Instead, they only
+ * have the address assigned to the jail (whatever that may be).
+ * And a jail without a network connection might not have any addresses at
+ * all. */
+ tt_assert(!smartlist_contains_null_tor_addr(results));
+
+ /* If there are addresses, they must be IPv4 or IPv6 */
+ if (smartlist_len(results) > 0) {
+ tt_assert(smartlist_contains_ipv4_tor_addr(results)
+ || smartlist_contains_ipv6_tor_addr(results));
+ }
done:
- SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
+ if (results) {
+ SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
+ }
smartlist_free(results);
return;
}
@@ -289,10 +317,17 @@ test_address_get_if_addrs_win32(void *arg)
(void)arg;
- results = get_interface_addresses_win32(LOG_ERR);
+ results = get_interface_addresses_win32(LOG_ERR, AF_UNSPEC);
tt_int_op(smartlist_len(results),>=,1);
tt_assert(smartlist_contains_localhost_tor_addr(results));
+ tt_assert(!smartlist_contains_null_tor_addr(results));
+
+ /* If there are addresses, they must be IPv4 or IPv6 */
+ if (smartlist_len(results) > 0) {
+ tt_assert(smartlist_contains_ipv4_tor_addr(results)
+ || smartlist_contains_ipv6_tor_addr(results));
+ }
done:
SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
@@ -479,14 +514,28 @@ test_address_get_if_addrs_ioctl(void *arg)
(void)arg;
- result = get_interface_addresses_ioctl(LOG_ERR);
+ result = get_interface_addresses_ioctl(LOG_ERR, AF_INET);
+ /* On an IPv6-only system, this will fail and return NULL
tt_assert(result);
- tt_int_op(smartlist_len(result),>=,1);
+ */
- tt_assert(smartlist_contains_localhost_tor_addr(result));
+ /* Some FreeBSD jails don't have localhost IP address. Instead, they only
+ * have the address assigned to the jail (whatever that may be).
+ * And a jail without a network connection might not have any addresses at
+ * all. */
+ if (result) {
+ tt_assert(!smartlist_contains_null_tor_addr(result));
- done:
+ /* If there are addresses, they must be IPv4 or IPv6.
+ * (AIX supports IPv6 from SIOCGIFCONF.) */
+ if (smartlist_len(result) > 0) {
+ tt_assert(smartlist_contains_ipv4_tor_addr(result)
+ || smartlist_contains_ipv6_tor_addr(result));
+ }
+ }
+
+ done:
if (result) {
SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
smartlist_free(result);
@@ -508,18 +557,25 @@ fake_open_socket(int domain, int type, int protocol)
return FAKE_SOCKET_FD;
}
+static int
+fake_close_socket(tor_socket_t s)
+{
+ (void)s;
+ return 0;
+}
+
static int last_connected_socket_fd = 0;
static int connect_retval = 0;
static tor_socket_t
-pretend_to_connect(tor_socket_t socket, const struct sockaddr *address,
+pretend_to_connect(tor_socket_t sock, const struct sockaddr *address,
socklen_t address_len)
{
(void)address;
(void)address_len;
- last_connected_socket_fd = socket;
+ last_connected_socket_fd = sock;
return connect_retval;
}
@@ -527,11 +583,11 @@ pretend_to_connect(tor_socket_t socket, const struct sockaddr *address,
static struct sockaddr *mock_addr = NULL;
static int
-fake_getsockname(tor_socket_t socket, struct sockaddr *address,
+fake_getsockname(tor_socket_t sock, struct sockaddr *address,
socklen_t *address_len)
{
socklen_t bytes_to_copy = 0;
- (void) socket;
+ (void) sock;
if (!mock_addr)
return -1;
@@ -568,6 +624,7 @@ test_address_udp_socket_trick_whitebox(void *arg)
MOCK(tor_open_socket,fake_open_socket);
MOCK(tor_connect_socket,pretend_to_connect);
MOCK(tor_getsockname,fake_getsockname);
+ MOCK(tor_close_socket,fake_close_socket);
mock_addr = tor_malloc_zero(sizeof(struct sockaddr_storage));
sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr);
@@ -598,11 +655,12 @@ test_address_udp_socket_trick_whitebox(void *arg)
tt_assert(sockaddr_in6_are_equal(mock_addr6,ipv6_to_check));
+ done:
UNMOCK(tor_open_socket);
UNMOCK(tor_connect_socket);
UNMOCK(tor_getsockname);
+ UNMOCK(tor_close_socket);
- done:
tor_free(ipv6_to_check);
tor_free(mock_addr);
tor_free(addr_from_hack);
@@ -696,12 +754,13 @@ test_address_get_if_addrs_list_internal(void *arg)
tt_assert(!smartlist_contains_localhost_tor_addr(results));
tt_assert(!smartlist_contains_multicast_tor_addr(results));
/* The list may or may not contain internal addresses */
+ tt_assert(!smartlist_contains_null_tor_addr(results));
- /* Allow unit tests to pass on IPv6-only machines */
+ /* if there are any addresses, they must be IPv4 */
if (smartlist_len(results) > 0) {
- tt_assert(smartlist_contains_ipv4_tor_addr(results)
- || smartlist_contains_ipv6_tor_addr(results));
+ tt_assert(smartlist_contains_ipv4_tor_addr(results));
}
+ tt_assert(!smartlist_contains_ipv6_tor_addr(results));
done:
free_interface_address_list(results);
@@ -724,6 +783,7 @@ test_address_get_if_addrs_list_no_internal(void *arg)
tt_assert(!smartlist_contains_localhost_tor_addr(results));
tt_assert(!smartlist_contains_multicast_tor_addr(results));
tt_assert(!smartlist_contains_internal_tor_addr(results));
+ tt_assert(!smartlist_contains_null_tor_addr(results));
/* if there are any addresses, they must be IPv4 */
if (smartlist_len(results) > 0) {
@@ -743,7 +803,20 @@ test_address_get_if_addrs6_list_internal(void *arg)
(void)arg;
+ /* We might drop a log_err */
+ setup_full_capture_of_logs(LOG_ERR);
results = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_LE, 1);
+ if (smartlist_len(mock_saved_logs()) == 1) {
+ expect_log_msg_containing_either4("connect() failed",
+ "unable to create socket",
+ "Address that we determined via UDP "
+ "socket magic is unsuitable for public "
+ "comms.",
+ "getsockname() to determine interface "
+ "failed");
+ }
+ teardown_capture_of_logs();
tt_assert(results != NULL);
/* Work even on systems without IPv6 interfaces */
@@ -752,6 +825,7 @@ test_address_get_if_addrs6_list_internal(void *arg)
tt_assert(!smartlist_contains_localhost_tor_addr(results));
tt_assert(!smartlist_contains_multicast_tor_addr(results));
/* The list may or may not contain internal addresses */
+ tt_assert(!smartlist_contains_null_tor_addr(results));
/* if there are any addresses, they must be IPv6 */
tt_assert(!smartlist_contains_ipv4_tor_addr(results));
@@ -761,6 +835,7 @@ test_address_get_if_addrs6_list_internal(void *arg)
done:
free_interface_address6_list(results);
+ teardown_capture_of_logs();
return;
}
@@ -771,7 +846,20 @@ test_address_get_if_addrs6_list_no_internal(void *arg)
(void)arg;
+ /* We might drop a log_err */
+ setup_full_capture_of_logs(LOG_ERR);
results = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_LE, 1);
+ if (smartlist_len(mock_saved_logs()) == 1) {
+ expect_log_msg_containing_either4("connect() failed",
+ "unable to create socket",
+ "Address that we determined via UDP "
+ "socket magic is unsuitable for public "
+ "comms.",
+ "getsockname() to determine interface "
+ "failed");
+ }
+ teardown_capture_of_logs();
tt_assert(results != NULL);
/* Work even on systems without IPv6 interfaces */
@@ -780,13 +868,16 @@ test_address_get_if_addrs6_list_no_internal(void *arg)
tt_assert(!smartlist_contains_localhost_tor_addr(results));
tt_assert(!smartlist_contains_multicast_tor_addr(results));
tt_assert(!smartlist_contains_internal_tor_addr(results));
+ tt_assert(!smartlist_contains_null_tor_addr(results));
+ /* if there are any addresses, they must be IPv6 */
tt_assert(!smartlist_contains_ipv4_tor_addr(results));
if (smartlist_len(results) > 0) {
tt_assert(smartlist_contains_ipv6_tor_addr(results));
}
done:
+ teardown_capture_of_logs();
free_interface_address6_list(results);
return;
}
@@ -794,9 +885,10 @@ test_address_get_if_addrs6_list_no_internal(void *arg)
static int called_get_interface_addresses_raw = 0;
static smartlist_t *
-mock_get_interface_addresses_raw_fail(int severity)
+mock_get_interface_addresses_raw_fail(int severity, sa_family_t family)
{
(void)severity;
+ (void)family;
called_get_interface_addresses_raw++;
return smartlist_new();
@@ -848,7 +940,7 @@ test_address_get_if_addrs_internal_fail(void *arg)
rv = get_interface_address(LOG_ERR, &ipv4h_addr);
tt_assert(rv == -1);
-done:
+ done:
UNMOCK(get_interface_addresses_raw);
UNMOCK(get_interface_address6_via_udp_socket_hack);
free_interface_address6_list(results1);
@@ -876,7 +968,7 @@ test_address_get_if_addrs_no_internal_fail(void *arg)
tt_assert(results2 != NULL);
tt_int_op(smartlist_len(results2),==,0);
-done:
+ done:
UNMOCK(get_interface_addresses_raw);
UNMOCK(get_interface_address6_via_udp_socket_hack);
free_interface_address6_list(results1);
@@ -935,6 +1027,118 @@ test_address_get_if_addrs6(void *arg)
return;
}
+static void
+test_address_tor_addr_to_in6(void *ignored)
+{
+ (void)ignored;
+ tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
+ const struct in6_addr *res;
+ uint8_t expected[16] = {42, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ 10, 11, 12, 13, 14, 15};
+
+ a->family = AF_INET;
+ res = tor_addr_to_in6(a);
+ tt_assert(!res);
+
+ a->family = AF_INET6;
+ memcpy(a->addr.in6_addr.s6_addr, expected, 16);
+ res = tor_addr_to_in6(a);
+ tt_assert(res);
+ tt_mem_op(res->s6_addr, OP_EQ, expected, 16);
+
+ done:
+ tor_free(a);
+}
+
+static void
+test_address_tor_addr_to_in(void *ignored)
+{
+ (void)ignored;
+ tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
+ const struct in_addr *res;
+
+ a->family = AF_INET6;
+ res = tor_addr_to_in(a);
+ tt_assert(!res);
+
+ a->family = AF_INET;
+ a->addr.in_addr.s_addr = 44;
+ res = tor_addr_to_in(a);
+ tt_assert(res);
+ tt_int_op(res->s_addr, OP_EQ, 44);
+
+ done:
+ tor_free(a);
+}
+
+static void
+test_address_tor_addr_to_ipv4n(void *ignored)
+{
+ (void)ignored;
+ tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
+ uint32_t res;
+
+ a->family = AF_INET6;
+ res = tor_addr_to_ipv4n(a);
+ tt_assert(!res);
+
+ a->family = AF_INET;
+ a->addr.in_addr.s_addr = 43;
+ res = tor_addr_to_ipv4n(a);
+ tt_assert(res);
+ tt_int_op(res, OP_EQ, 43);
+
+ done:
+ tor_free(a);
+}
+
+static void
+test_address_tor_addr_to_mapped_ipv4h(void *ignored)
+{
+ (void)ignored;
+ tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
+ uint32_t res;
+ uint8_t toset[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 42};
+
+ a->family = AF_INET;
+ res = tor_addr_to_mapped_ipv4h(a);
+ tt_assert(!res);
+
+ a->family = AF_INET6;
+
+ memcpy(a->addr.in6_addr.s6_addr, toset, 16);
+ res = tor_addr_to_mapped_ipv4h(a);
+ tt_assert(res);
+ tt_int_op(res, OP_EQ, 42);
+
+ done:
+ tor_free(a);
+}
+
+static void
+test_address_tor_addr_eq_ipv4h(void *ignored)
+{
+ (void)ignored;
+ tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
+ int res;
+
+ a->family = AF_INET6;
+ res = tor_addr_eq_ipv4h(a, 42);
+ tt_assert(!res);
+
+ a->family = AF_INET;
+ a->addr.in_addr.s_addr = 52;
+ res = tor_addr_eq_ipv4h(a, 42);
+ tt_assert(!res);
+
+ a->addr.in_addr.s_addr = 52;
+ res = tor_addr_eq_ipv4h(a, ntohl(52));
+ tt_assert(res);
+
+ done:
+ tor_free(a);
+}
+
#define ADDRESS_TEST(name, flags) \
{ #name, test_address_ ## name, flags, NULL, NULL }
@@ -944,7 +1148,7 @@ struct testcase_t address_tests[] = {
ADDRESS_TEST(get_if_addrs_list_internal, 0),
ADDRESS_TEST(get_if_addrs_list_no_internal, 0),
ADDRESS_TEST(get_if_addrs6_list_internal, 0),
- ADDRESS_TEST(get_if_addrs6_list_no_internal, 0),
+ ADDRESS_TEST(get_if_addrs6_list_no_internal, TT_FORK),
ADDRESS_TEST(get_if_addrs_internal_fail, 0),
ADDRESS_TEST(get_if_addrs_no_internal_fail, 0),
ADDRESS_TEST(get_if_addrs, 0),
@@ -961,6 +1165,11 @@ struct testcase_t address_tests[] = {
ADDRESS_TEST(get_if_addrs_ioctl, TT_FORK),
ADDRESS_TEST(ifreq_to_smartlist, 0),
#endif
+ ADDRESS_TEST(tor_addr_to_in6, 0),
+ ADDRESS_TEST(tor_addr_to_in, 0),
+ ADDRESS_TEST(tor_addr_to_ipv4n, 0),
+ ADDRESS_TEST(tor_addr_to_mapped_ipv4h, 0),
+ ADDRESS_TEST(tor_addr_eq_ipv4h, 0),
END_OF_TESTCASES
};
diff --git a/src/test/test_bt.sh b/src/test/test_bt.sh
index f55f451f92..312905a4e2 100755
--- a/src/test/test_bt.sh
+++ b/src/test/test_bt.sh
@@ -3,8 +3,11 @@
exitcode=0
-"${builddir:-.}/src/test/test-bt-cl" backtraces || exit 77
-"${builddir:-.}/src/test/test-bt-cl" assert | "${PYTHON:-python}" "${abs_top_srcdir:-.}/src/test/bt_test.py" || exitcode=1
-"${builddir:-.}/src/test/test-bt-cl" crash | "${PYTHON:-python}" "${abs_top_srcdir:-.}/src/test/bt_test.py" || exitcode=1
+export ASAN_OPTIONS="handle_segv=0:allow_user_segv_handler=1"
+"${builddir:-.}/src/test/test-bt-cl" backtraces || exit $?
+"${builddir:-.}/src/test/test-bt-cl" assert 2>&1 | "${PYTHON:-python}" "${abs_top_srcdir:-.}/src/test/bt_test.py" || exitcode="$?"
+"${builddir:-.}/src/test/test-bt-cl" crash 2>&1 | "${PYTHON:-python}" "${abs_top_srcdir:-.}/src/test/bt_test.py" || exitcode="$?"
+
+"${builddir:-.}/src/test/test-bt-cl" none || exitcode="$?"
exit ${exitcode}
diff --git a/src/test/test_bt_cl.c b/src/test/test_bt_cl.c
index dabaee6e0a..95b4f48f11 100644
--- a/src/test/test_bt_cl.c
+++ b/src/test/test_bt_cl.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015, The Tor Project, Inc. */
+/* Copyright (c) 2012-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -28,6 +28,9 @@ int a_tangled_web(int x) NOINLINE;
int we_weave(int x) NOINLINE;
static void abort_handler(int s) NORETURN;
+#ifdef HAVE_CFLAG_WNULL_DEREFERENCE
+DISABLE_GCC_WARNING(null-dereference)
+#endif
int
crash(int x)
{
@@ -47,6 +50,9 @@ crash(int x)
crashtype *= x;
return crashtype;
}
+#ifdef HAVE_CFLAG_WNULL_DEREFERENCE
+ENABLE_GCC_WARNING(null-dereference)
+#endif
int
oh_what(int x)
@@ -119,6 +125,7 @@ main(int argc, char **argv)
printf("%d\n", we_weave(2));
clean_up_backtrace_handler();
+ logs_free_all();
return 0;
}
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index 29ee408616..3408da3aa9 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define BUFFERS_PRIVATE
@@ -178,10 +178,10 @@ test_buffers_basic(void *arg)
/* Try adding a string too long for any freelist. */
{
- char *cp = tor_malloc_zero(65536);
+ char *mem = tor_malloc_zero(65536);
buf = buf_new();
- write_to_buf(cp, 65536, buf);
- tor_free(cp);
+ write_to_buf(mem, 65536, buf);
+ tor_free(mem);
tt_int_op(buf_datalen(buf), OP_EQ, 65536);
buf_free(buf);
@@ -303,42 +303,42 @@ test_buffer_pullup(void *arg)
static void
test_buffer_copy(void *arg)
{
- generic_buffer_t *buf=NULL, *buf2=NULL;
+ buf_t *buf=NULL, *buf2=NULL;
const char *s;
size_t len;
char b[256];
int i;
(void)arg;
- buf = generic_buffer_new();
+ buf = buf_new();
tt_assert(buf);
/* Copy an empty buffer. */
- tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
+ tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
tt_assert(buf2);
- tt_int_op(0, OP_EQ, generic_buffer_len(buf2));
+ tt_int_op(0, OP_EQ, buf_datalen(buf2));
/* Now try with a short buffer. */
s = "And now comes an act of enormous enormance!";
len = strlen(s);
- generic_buffer_add(buf, s, len);
- tt_int_op(len, OP_EQ, generic_buffer_len(buf));
+ write_to_buf(s, len, buf);
+ tt_int_op(len, OP_EQ, buf_datalen(buf));
/* Add junk to buf2 so we can test replacing.*/
- generic_buffer_add(buf2, "BLARG", 5);
- tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
- tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
- generic_buffer_get(buf2, b, len);
+ write_to_buf("BLARG", 5, buf2);
+ tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
+ tt_int_op(len, OP_EQ, buf_datalen(buf2));
+ fetch_from_buf(b, len, buf2);
tt_mem_op(b, OP_EQ, s, len);
/* Now free buf2 and retry so we can test allocating */
- generic_buffer_free(buf2);
+ buf_free(buf2);
buf2 = NULL;
- tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
- tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
- generic_buffer_get(buf2, b, len);
+ tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
+ tt_int_op(len, OP_EQ, buf_datalen(buf2));
+ fetch_from_buf(b, len, buf2);
tt_mem_op(b, OP_EQ, s, len);
/* Clear buf for next test */
- generic_buffer_get(buf, b, len);
- tt_int_op(generic_buffer_len(buf),OP_EQ,0);
+ fetch_from_buf(b, len, buf);
+ tt_int_op(buf_datalen(buf),OP_EQ,0);
/* Okay, now let's try a bigger buffer. */
s = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit "
@@ -347,95 +347,94 @@ test_buffer_copy(void *arg)
len = strlen(s);
for (i = 0; i < 256; ++i) {
b[0]=i;
- generic_buffer_add(buf, b, 1);
- generic_buffer_add(buf, s, len);
+ write_to_buf(b, 1, buf);
+ write_to_buf(s, len, buf);
}
- tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
- tt_int_op(generic_buffer_len(buf2), OP_EQ, generic_buffer_len(buf));
+ tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
+ tt_int_op(buf_datalen(buf2), OP_EQ, buf_datalen(buf));
for (i = 0; i < 256; ++i) {
- generic_buffer_get(buf2, b, len+1);
+ fetch_from_buf(b, len+1, buf2);
tt_int_op((unsigned char)b[0],OP_EQ,i);
tt_mem_op(b+1, OP_EQ, s, len);
}
done:
if (buf)
- generic_buffer_free(buf);
+ buf_free(buf);
if (buf2)
- generic_buffer_free(buf2);
+ buf_free(buf2);
}
static void
test_buffer_ext_or_cmd(void *arg)
{
ext_or_cmd_t *cmd = NULL;
- generic_buffer_t *buf = generic_buffer_new();
+ buf_t *buf = buf_new();
char *tmp = NULL;
(void) arg;
/* Empty -- should give "not there. */
- tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_EQ, cmd);
/* Three bytes: shouldn't work. */
- generic_buffer_add(buf, "\x00\x20\x00", 3);
- tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf("\x00\x20\x00", 3, buf);
+ tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_EQ, cmd);
- tt_int_op(3, OP_EQ, generic_buffer_len(buf));
+ tt_int_op(3, OP_EQ, buf_datalen(buf));
/* 0020 0000: That's a nil command. It should work. */
- generic_buffer_add(buf, "\x00", 1);
- tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf("\x00", 1, buf);
+ tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_NE, cmd);
tt_int_op(0x20, OP_EQ, cmd->cmd);
tt_int_op(0, OP_EQ, cmd->len);
- tt_int_op(0, OP_EQ, generic_buffer_len(buf));
+ tt_int_op(0, OP_EQ, buf_datalen(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
/* Now try a length-6 command with one byte missing. */
- generic_buffer_add(buf, "\x10\x21\x00\x06""abcde", 9);
- tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf("\x10\x21\x00\x06""abcde", 9, buf);
+ tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_EQ, cmd);
- generic_buffer_add(buf, "f", 1);
- tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf("f", 1, buf);
+ tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_NE, cmd);
tt_int_op(0x1021, OP_EQ, cmd->cmd);
tt_int_op(6, OP_EQ, cmd->len);
tt_mem_op("abcdef", OP_EQ, cmd->body, 6);
- tt_int_op(0, OP_EQ, generic_buffer_len(buf));
+ tt_int_op(0, OP_EQ, buf_datalen(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
/* Now try a length-10 command with 4 extra bytes. */
- generic_buffer_add(buf, "\xff\xff\x00\x0a"
- "loremipsum\x10\x00\xff\xff", 18);
- tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf("\xff\xff\x00\x0aloremipsum\x10\x00\xff\xff", 18, buf);
+ tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_NE, cmd);
tt_int_op(0xffff, OP_EQ, cmd->cmd);
tt_int_op(10, OP_EQ, cmd->len);
tt_mem_op("loremipsum", OP_EQ, cmd->body, 10);
- tt_int_op(4, OP_EQ, generic_buffer_len(buf));
+ tt_int_op(4, OP_EQ, buf_datalen(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
/* Finally, let's try a maximum-length command. We already have the header
* waiting. */
- tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tmp = tor_malloc_zero(65535);
- generic_buffer_add(buf, tmp, 65535);
- tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ write_to_buf(tmp, 65535, buf);
+ tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
tt_ptr_op(NULL, OP_NE, cmd);
tt_int_op(0x1000, OP_EQ, cmd->cmd);
tt_int_op(0xffff, OP_EQ, cmd->len);
tt_mem_op(tmp, OP_EQ, cmd->body, 65535);
- tt_int_op(0, OP_EQ, generic_buffer_len(buf));
+ tt_int_op(0, OP_EQ, buf_datalen(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
done:
ext_or_cmd_free(cmd);
- generic_buffer_free(buf);
+ buf_free(buf);
tor_free(tmp);
}
@@ -511,26 +510,26 @@ static void
test_buffer_time_tracking(void *arg)
{
buf_t *buf=NULL, *buf2=NULL;
- struct timeval tv0;
const time_t START = 1389288246;
- const uint32_t START_MSEC = (uint32_t) ((uint64_t)START * 1000);
+ const uint64_t START_NSEC = ((uint64_t)START) * 1000000000;
int i;
char tmp[4096];
(void)arg;
crypto_rand(tmp, sizeof(tmp));
- tv0.tv_sec = START;
- tv0.tv_usec = 0;
+ monotime_enable_test_mocking();
buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
tt_assert(buf);
+ monotime_coarse_set_mock_time_nsec(START_NSEC);
+ const uint32_t START_MSEC = (uint32_t)monotime_coarse_absolute_msec();
+
/* Empty buffer means the timestamp is 0. */
tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC));
tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
- tor_gettimeofday_cache_set(&tv0);
write_to_buf("ABCDEFG", 7, buf);
tt_int_op(1000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
@@ -540,8 +539,7 @@ test_buffer_time_tracking(void *arg)
buf_get_oldest_chunk_timestamp(buf2, START_MSEC+1234));
/* Now add more bytes; enough to overflow the first chunk. */
- tv0.tv_usec += 123 * 1000;
- tor_gettimeofday_cache_set(&tv0);
+ monotime_coarse_set_mock_time_nsec(START_NSEC + 123 * (uint64_t)1000000);
for (i = 0; i < 600; ++i)
write_to_buf("ABCDEFG", 7, buf);
tt_int_op(4207, OP_EQ, buf_datalen(buf));
@@ -562,9 +560,7 @@ test_buffer_time_tracking(void *arg)
/* This time we'll be grabbing a chunk from the freelist, and making sure
its time gets updated */
- tv0.tv_sec += 5;
- tv0.tv_usec = 617*1000;
- tor_gettimeofday_cache_set(&tv0);
+ monotime_coarse_set_mock_time_nsec(START_NSEC + 5617 * (uint64_t)1000000);
for (i = 0; i < 600; ++i)
write_to_buf("ABCDEFG", 7, buf);
tt_int_op(4307, OP_EQ, buf_datalen(buf));
@@ -578,6 +574,7 @@ test_buffer_time_tracking(void *arg)
done:
buf_free(buf);
buf_free(buf2);
+ monotime_disable_test_mocking();
}
static void
@@ -695,9 +692,9 @@ test_buffers_zlib_fin_at_chunk_end(void *arg)
tor_free(msg);
}
-const uint8_t *tls_read_ptr;
-int n_remaining;
-int next_reply_val[16];
+static const uint8_t *tls_read_ptr;
+static int n_remaining;
+static int next_reply_val[16];
static int
mock_tls_read(tor_tls_t *tls, char *cp, size_t len)
@@ -747,6 +744,27 @@ test_buffers_tls_read_mocked(void *arg)
buf_free(buf);
}
+static void
+test_buffers_chunk_size(void *arg)
+{
+ (void)arg;
+ const int min = 256;
+ const int max = 65536;
+ tt_uint_op(preferred_chunk_size(3), OP_EQ, min);
+ tt_uint_op(preferred_chunk_size(25), OP_EQ, min);
+ tt_uint_op(preferred_chunk_size(0), OP_EQ, min);
+ tt_uint_op(preferred_chunk_size(256), OP_EQ, 512);
+ tt_uint_op(preferred_chunk_size(65400), OP_EQ, max);
+ /* Here, we're implicitly saying that the chunk header overhead is
+ * between 1 and 100 bytes. 24..48 would probably be more accurate. */
+ tt_uint_op(preferred_chunk_size(65536), OP_GT, 65536);
+ tt_uint_op(preferred_chunk_size(65536), OP_LT, 65536+100);
+ tt_uint_op(preferred_chunk_size(165536), OP_GT, 165536);
+ tt_uint_op(preferred_chunk_size(165536), OP_LT, 165536+100);
+ done:
+ ;
+}
+
struct testcase_t buffer_tests[] = {
{ "basic", test_buffers_basic, TT_FORK, NULL, NULL },
{ "copy", test_buffer_copy, TT_FORK, NULL, NULL },
@@ -761,6 +779,7 @@ struct testcase_t buffer_tests[] = {
NULL, NULL},
{ "tls_read_mocked", test_buffers_tls_read_mocked, 0,
NULL, NULL },
+ { "chunk_size", test_buffers_chunk_size, 0, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_cell_formats.c b/src/test/test_cell_formats.c
index e86dc0934f..f839a5b72c 100644
--- a/src/test/test_cell_formats.c
+++ b/src/test/test_cell_formats.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -882,8 +882,8 @@ test_cfmt_resolved_cells(void *arg)
memset(&rh, 0, sizeof(rh)); \
} while (0)
#define CLEAR_ADDRS() do { \
- SMARTLIST_FOREACH(addrs, address_ttl_t *, a, \
- address_ttl_free(a); ); \
+ SMARTLIST_FOREACH(addrs, address_ttl_t *, aa_, \
+ address_ttl_free(aa_); ); \
smartlist_clear(addrs); \
} while (0)
#define SET_CELL(s) do { \
diff --git a/src/test/test_cell_queue.c b/src/test/test_cell_queue.c
index ed34df2ea2..93ac9854d8 100644
--- a/src/test/test_cell_queue.c
+++ b/src/test/test_cell_queue.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CIRCUITLIST_PRIVATE
diff --git a/src/test/test_channel.c b/src/test/test_channel.c
index e11ac3f3cc..a9e0634d9e 100644
--- a/src/test/test_channel.c
+++ b/src/test/test_channel.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define TOR_CHANNEL_INTERNAL_
@@ -20,18 +20,17 @@
#include "test.h"
#include "fakechans.h"
-/* This comes from channel.c */
-extern uint64_t estimated_total_queue_size;
-
static int test_chan_accept_cells = 0;
static int test_chan_fixed_cells_recved = 0;
+static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
static int test_chan_var_cells_recved = 0;
+static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
static int test_cells_written = 0;
static int test_destroy_not_pending_calls = 0;
static int test_doesnt_want_writes_count = 0;
static int test_dumpstats_calls = 0;
static int test_has_waiting_cells_count = 0;
-static double test_overhead_estimate = 1.0f;
+static double test_overhead_estimate = 1.0;
static int test_releases_count = 0;
static circuitmux_t *test_target_cmux = NULL;
static unsigned int test_cmux_cells = 0;
@@ -70,6 +69,7 @@ static void test_channel_flushmux(void *arg);
static void test_channel_incoming(void *arg);
static void test_channel_lifecycle(void *arg);
static void test_channel_multi(void *arg);
+static void test_channel_queue_incoming(void *arg);
static void test_channel_queue_size(void *arg);
static void test_channel_write(void *arg);
@@ -179,7 +179,7 @@ chan_test_cell_handler(channel_t *ch,
tt_assert(ch);
tt_assert(cell);
- tor_free(cell);
+ test_chan_last_seen_fixed_cell_ptr = cell;
++test_chan_fixed_cells_recved;
done:
@@ -214,7 +214,7 @@ chan_test_var_cell_handler(channel_t *ch,
tt_assert(ch);
tt_assert(var_cell);
- tor_free(var_cell);
+ test_chan_last_seen_var_cell_ptr = var_cell;
++test_chan_var_cells_recved;
done:
@@ -608,7 +608,7 @@ test_channel_dumpstats(void *arg)
make_fake_cell(cell);
old_count = test_chan_fixed_cells_recved;
channel_queue_cell(ch, cell);
- cell = NULL;
+ tor_free(cell);
tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
tt_assert(ch->n_bytes_recved > 0);
tt_assert(ch->n_cells_recved > 0);
@@ -789,7 +789,7 @@ test_channel_incoming(void *arg)
/* Accept cells to lower layer */
test_chan_accept_cells = 1;
/* Use default overhead factor */
- test_overhead_estimate = 1.0f;
+ test_overhead_estimate = 1.0;
ch = new_fake_channel();
tt_assert(ch);
@@ -819,7 +819,7 @@ test_channel_incoming(void *arg)
make_fake_cell(cell);
old_count = test_chan_fixed_cells_recved;
channel_queue_cell(ch, cell);
- cell = NULL;
+ tor_free(cell);
tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
/* Receive a variable-size cell */
@@ -827,7 +827,7 @@ test_channel_incoming(void *arg)
make_fake_var_cell(var_cell);
old_count = test_chan_var_cells_recved;
channel_queue_var_cell(ch, var_cell);
- var_cell = NULL;
+ tor_free(cell);
tt_int_op(test_chan_var_cells_recved, ==, old_count + 1);
/* Close it */
@@ -878,7 +878,7 @@ test_channel_lifecycle(void *arg)
/* Accept cells to lower layer */
test_chan_accept_cells = 1;
/* Use default overhead factor */
- test_overhead_estimate = 1.0f;
+ test_overhead_estimate = 1.0;
ch1 = new_fake_channel();
tt_assert(ch1);
@@ -986,7 +986,7 @@ test_channel_lifecycle_2(void *arg)
/* Accept cells to lower layer */
test_chan_accept_cells = 1;
/* Use default overhead factor */
- test_overhead_estimate = 1.0f;
+ test_overhead_estimate = 1.0;
ch = new_fake_channel();
tt_assert(ch);
@@ -1133,7 +1133,7 @@ test_channel_multi(void *arg)
/* Accept cells to lower layer */
test_chan_accept_cells = 1;
/* Use default overhead factor */
- test_overhead_estimate = 1.0f;
+ test_overhead_estimate = 1.0;
ch1 = new_fake_channel();
tt_assert(ch1);
@@ -1423,6 +1423,113 @@ test_channel_queue_impossible(void *arg)
}
static void
+test_channel_queue_incoming(void *arg)
+{
+ channel_t *ch = NULL;
+ cell_t *cell = NULL;
+ var_cell_t *var_cell = NULL;
+ int old_fixed_count, old_var_count;
+
+ (void)arg;
+
+ /* Mock these for duration of the test */
+ MOCK(scheduler_channel_doesnt_want_writes,
+ scheduler_channel_doesnt_want_writes_mock);
+ MOCK(scheduler_release_channel,
+ scheduler_release_channel_mock);
+
+ /* Accept cells to lower layer */
+ test_chan_accept_cells = 1;
+ /* Use default overhead factor */
+ test_overhead_estimate = 1.0;
+
+ ch = new_fake_channel();
+ tt_assert(ch);
+ /* Start it off in OPENING */
+ ch->state = CHANNEL_STATE_OPENING;
+ /* We'll need a cmux */
+ ch->cmux = circuitmux_alloc();
+
+ /* Test cell handler getters */
+ tt_ptr_op(channel_get_cell_handler(ch), ==, NULL);
+ tt_ptr_op(channel_get_var_cell_handler(ch), ==, NULL);
+
+ /* Try to register it */
+ channel_register(ch);
+ tt_assert(ch->registered);
+
+ /* Open it */
+ channel_change_state(ch, CHANNEL_STATE_OPEN);
+ tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
+
+ /* Assert that the incoming queue is empty */
+ tt_assert(TOR_SIMPLEQ_EMPTY(&(ch->incoming_queue)));
+
+ /* Queue an incoming fixed-length cell */
+ cell = tor_malloc_zero(sizeof(cell_t));
+ make_fake_cell(cell);
+ channel_queue_cell(ch, cell);
+
+ /* Assert that the incoming queue has one entry */
+ tt_int_op(chan_cell_queue_len(&(ch->incoming_queue)), ==, 1);
+
+ /* Queue an incoming var cell */
+ var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
+ make_fake_var_cell(var_cell);
+ channel_queue_var_cell(ch, var_cell);
+
+ /* Assert that the incoming queue has two entries */
+ tt_int_op(chan_cell_queue_len(&(ch->incoming_queue)), ==, 2);
+
+ /*
+ * Install cell handlers; this will drain the queue, so save the old
+ * cell counters first
+ */
+ old_fixed_count = test_chan_fixed_cells_recved;
+ old_var_count = test_chan_var_cells_recved;
+ channel_set_cell_handlers(ch,
+ chan_test_cell_handler,
+ chan_test_var_cell_handler);
+ tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
+ tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
+
+ /* Assert cells were received */
+ tt_int_op(test_chan_fixed_cells_recved, ==, old_fixed_count + 1);
+ tt_int_op(test_chan_var_cells_recved, ==, old_var_count + 1);
+
+ /*
+ * Assert that the pointers are different from the cells we allocated;
+ * when queueing cells with no incoming cell handlers installed, the
+ * channel layer should copy them to a new buffer, and free them after
+ * delivery. These pointers will have already been freed by the time
+ * we get here, so don't dereference them.
+ */
+ tt_ptr_op(test_chan_last_seen_fixed_cell_ptr, !=, cell);
+ tt_ptr_op(test_chan_last_seen_var_cell_ptr, !=, var_cell);
+
+ /* Assert queue is now empty */
+ tt_assert(TOR_SIMPLEQ_EMPTY(&(ch->incoming_queue)));
+
+ /* Close it; this contains an assertion that the incoming queue is empty */
+ channel_mark_for_close(ch);
+ tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
+ chan_test_finish_close(ch);
+ tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
+ channel_run_cleanup();
+ ch = NULL;
+
+ done:
+ free_fake_channel(ch);
+ tor_free(cell);
+ tor_free(var_cell);
+
+ UNMOCK(scheduler_channel_doesnt_want_writes);
+ UNMOCK(scheduler_release_channel);
+
+ return;
+}
+
+static void
test_channel_queue_size(void *arg)
{
channel_t *ch = NULL;
@@ -1474,16 +1581,16 @@ test_channel_queue_size(void *arg)
/* One cell, times an overhead factor of 1.0 */
tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
/* Try a different overhead factor */
- test_overhead_estimate = 0.5f;
+ test_overhead_estimate = 0.5;
/* This one should be ignored since it's below 1.0 */
channel_update_xmit_queue_size(ch);
tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
/* Now try a larger one */
- test_overhead_estimate = 2.0f;
+ test_overhead_estimate = 2.0;
channel_update_xmit_queue_size(ch);
tt_u64_op(ch->bytes_queued_for_xmit, ==, 1024);
/* Go back to 1.0 */
- test_overhead_estimate = 1.0f;
+ test_overhead_estimate = 1.0;
channel_update_xmit_queue_size(ch);
tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
/* Check the global estimate too */
@@ -1666,6 +1773,7 @@ struct testcase_t channel_tests[] = {
{ "lifecycle_2", test_channel_lifecycle_2, TT_FORK, NULL, NULL },
{ "multi", test_channel_multi, TT_FORK, NULL, NULL },
{ "queue_impossible", test_channel_queue_impossible, TT_FORK, NULL, NULL },
+ { "queue_incoming", test_channel_queue_incoming, TT_FORK, NULL, NULL },
{ "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
{ "write", test_channel_write, TT_FORK, NULL, NULL },
END_OF_TESTCASES
diff --git a/src/test/test_channeltls.c b/src/test/test_channeltls.c
index 016e504ab3..08442e01b6 100644
--- a/src/test/test_channeltls.c
+++ b/src/test/test_channeltls.c
@@ -1,6 +1,8 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
+#include "orconfig.h"
+
#include <math.h>
#define TOR_CHANNEL_INTERNAL_
@@ -123,9 +125,8 @@ test_channeltls_num_bytes_queued(void *arg)
/*
* Next, we have to test ch->num_bytes_queued, which is
* channel_tls_num_bytes_queued_method. We can't mock
- * connection_get_outbuf_len() directly because it's static INLINE
- * in connection.h, but we can mock buf_datalen(). Note that
- * if bufferevents ever work, this will break with them enabled.
+ * connection_get_outbuf_len() directly because it's static inline
+ * in connection.h, but we can mock buf_datalen().
*/
tt_assert(ch->num_bytes_queued != NULL);
@@ -185,7 +186,7 @@ test_channeltls_overhead_estimate(void *arg)
const char test_digest[DIGEST_LEN] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
- float r;
+ double r;
channel_tls_t *tlschan = NULL;
(void)arg;
@@ -206,31 +207,31 @@ test_channeltls_overhead_estimate(void *arg)
ch = channel_tls_connect(&test_addr, 567, test_digest);
tt_assert(ch != NULL);
- /* First case: silly low ratios should get clamped to 1.0f */
+ /* First case: silly low ratios should get clamped to 1.0 */
tlschan = BASE_CHAN_TO_TLS(ch);
tt_assert(tlschan != NULL);
tlschan->conn->bytes_xmitted = 128;
tlschan->conn->bytes_xmitted_by_tls = 64;
r = ch->get_overhead_estimate(ch);
- tt_assert(fabsf(r - 1.0f) < 1E-12);
+ tt_assert(fabs(r - 1.0) < 1E-12);
tlschan->conn->bytes_xmitted_by_tls = 127;
r = ch->get_overhead_estimate(ch);
- tt_assert(fabsf(r - 1.0f) < 1E-12);
+ tt_assert(fabs(r - 1.0) < 1E-12);
/* Now middle of the range */
tlschan->conn->bytes_xmitted_by_tls = 192;
r = ch->get_overhead_estimate(ch);
- tt_assert(fabsf(r - 1.5f) < 1E-12);
+ tt_assert(fabs(r - 1.5) < 1E-12);
- /* Now above the 2.0f clamp */
+ /* Now above the 2.0 clamp */
tlschan->conn->bytes_xmitted_by_tls = 257;
r = ch->get_overhead_estimate(ch);
- tt_assert(fabsf(r - 2.0f) < 1E-12);
+ tt_assert(fabs(r - 2.0) < 1E-12);
tlschan->conn->bytes_xmitted_by_tls = 512;
r = ch->get_overhead_estimate(ch);
- tt_assert(fabsf(r - 2.0f) < 1E-12);
+ tt_assert(fabs(r - 2.0) < 1E-12);
done:
if (ch) {
diff --git a/src/test/test_checkdir.c b/src/test/test_checkdir.c
index d6ef353c87..fbb33f87f6 100644
--- a/src/test/test_checkdir.c
+++ b/src/test/test_checkdir.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
diff --git a/src/test/test_circuitlist.c b/src/test/test_circuitlist.c
index 0760accfc1..e996c42115 100644
--- a/src/test/test_circuitlist.c
+++ b/src/test/test_circuitlist.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define TOR_CHANNEL_INTERNAL_
@@ -9,6 +9,7 @@
#include "circuitbuild.h"
#include "circuitlist.h"
#include "test.h"
+#include "log_test_helpers.h"
static channel_t *
new_fake_channel(void)
@@ -270,6 +271,13 @@ test_rend_token_maps(void *arg)
}
static void
+mock_channel_dump_statistics(channel_t *chan, int severity)
+{
+ (void)chan;
+ (void)severity;
+}
+
+static void
test_pick_circid(void *arg)
{
bitarray_t *ba = NULL;
@@ -278,12 +286,22 @@ test_pick_circid(void *arg)
int i;
(void) arg;
+ MOCK(channel_dump_statistics, mock_channel_dump_statistics);
+
chan1 = tor_malloc_zero(sizeof(channel_t));
chan2 = tor_malloc_zero(sizeof(channel_t));
chan2->wide_circ_ids = 1;
+ chan1->cmux = circuitmux_alloc();
+ chan2->cmux = circuitmux_alloc();
+
+ /* CIRC_ID_TYPE_NEITHER is supposed to create a warning. */
chan1->circ_id_type = CIRC_ID_TYPE_NEITHER;
+ setup_full_capture_of_logs(LOG_WARN);
tt_int_op(0, OP_EQ, get_unique_circ_id_by_chan(chan1));
+ expect_single_log_msg_containing("Trying to pick a circuit ID for a "
+ "connection from a client with no identity.");
+ teardown_capture_of_logs();
/* Basic tests, with no collisions */
chan1->circ_id_type = CIRC_ID_TYPE_LOWER;
@@ -337,10 +355,14 @@ test_pick_circid(void *arg)
}
done:
+ circuitmux_free(chan1->cmux);
+ circuitmux_free(chan2->cmux);
tor_free(chan1);
tor_free(chan2);
bitarray_free(ba);
circuit_free_all();
+ teardown_capture_of_logs();
+ UNMOCK(channel_dump_statistics);
}
struct testcase_t circuitlist_tests[] = {
diff --git a/src/test/test_circuitmux.c b/src/test/test_circuitmux.c
index 6d93731eea..9e8fb54964 100644
--- a/src/test/test_circuitmux.c
+++ b/src/test/test_circuitmux.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define TOR_CHANNEL_INTERNAL_
@@ -36,11 +36,7 @@ test_cmux_destroy_cell_queue(void *arg)
circuit_t *circ = NULL;
cell_queue_t *cq = NULL;
packed_cell_t *pc = NULL;
- tor_libevent_cfg cfg;
- memset(&cfg, 0, sizeof(cfg));
-
- tor_libevent_initialize(&cfg);
scheduler_init();
(void) arg;
diff --git a/src/test/test_compat_libevent.c b/src/test/test_compat_libevent.c
new file mode 100644
index 0000000000..0443cc0b1c
--- /dev/null
+++ b/src/test/test_compat_libevent.c
@@ -0,0 +1,131 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define COMPAT_LIBEVENT_PRIVATE
+#include "orconfig.h"
+#include "or.h"
+
+#include "test.h"
+
+#include "compat_libevent.h"
+
+#include <event2/event.h>
+#include <event2/thread.h>
+
+#include "log_test_helpers.h"
+
+#define NS_MODULE compat_libevent
+
+static void
+test_compat_libevent_logging_callback(void *ignored)
+{
+ (void)ignored;
+ setup_full_capture_of_logs(LOG_DEBUG);
+
+ libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
+ expect_log_msg("Message from libevent: hello world\n");
+ expect_log_severity(LOG_DEBUG);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
+ expect_log_msg("Message from libevent: hello world another time\n");
+ expect_log_severity(LOG_INFO);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
+ expect_log_msg("Warning from libevent: hello world a third time\n");
+ expect_log_severity(LOG_WARN);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
+ expect_log_msg("Error from libevent: hello world a fourth time\n");
+ expect_log_severity(LOG_ERR);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(42, "hello world a fifth time");
+ expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
+ expect_log_severity(LOG_WARN);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_DEBUG,
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ );
+ expect_log_msg("Message from libevent: "
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789"
+ "012345678901234567890123456789\n");
+ expect_log_severity(LOG_DEBUG);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(42, "xxx\n");
+ expect_log_msg("Message [42] from libevent: xxx\n");
+ expect_log_severity(LOG_WARN);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ suppress_libevent_log_msg("something");
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
+ expect_log_msg("Message from libevent: hello there\n");
+ expect_log_severity(LOG_INFO);
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+
+ mock_clean_saved_logs();
+ libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
+ expect_no_log_msg("hello there something else");
+ if (mock_saved_logs())
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 0);
+
+ // No way of verifying the result of this, it seems =/
+ configure_libevent_logging();
+
+ done:
+ suppress_libevent_log_msg(NULL);
+ teardown_capture_of_logs();
+}
+
+static void
+test_compat_libevent_header_version(void *ignored)
+{
+ (void)ignored;
+ const char *res;
+
+ res = tor_libevent_get_header_version_str();
+ tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
+
+ done:
+ (void)0;
+}
+
+struct testcase_t compat_libevent_tests[] = {
+ { "logging_callback", test_compat_libevent_logging_callback,
+ TT_FORK, NULL, NULL },
+ { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_config.c b/src/test/test_config.c
index 28e9fa0f32..2fc37b0bb8 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -1,23 +1,49 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#define CONFIG_PRIVATE
#define PT_PRIVATE
+#define ROUTERSET_PRIVATE
#include "or.h"
+#include "address.h"
#include "addressmap.h"
+#include "circuitmux_ewma.h"
+#include "circuitbuild.h"
#include "config.h"
#include "confparse.h"
+#include "connection.h"
#include "connection_edge.h"
#include "test.h"
#include "util.h"
#include "address.h"
+#include "connection_or.h"
+#include "control.h"
+#include "cpuworker.h"
+#include "dirserv.h"
+#include "dirvote.h"
+#include "dns.h"
#include "entrynodes.h"
#include "transports.h"
+#include "ext_orport.h"
+#include "geoip.h"
+#include "hibernate.h"
+#include "main.h"
+#include "networkstatus.h"
+#include "nodelist.h"
+#include "policies.h"
+#include "rendclient.h"
+#include "rendservice.h"
+#include "router.h"
#include "routerlist.h"
+#include "routerset.h"
+#include "statefile.h"
+#include "test.h"
+#include "transports.h"
+#include "util.h"
static void
test_config_addressmap(void *arg)
@@ -1444,6 +1470,176 @@ test_config_resolve_my_address(void *arg)
UNMOCK(tor_gethostname);
}
+static void
+test_config_adding_trusted_dir_server(void *arg)
+{
+ (void)arg;
+
+ const char digest[DIGEST_LEN] = "";
+ dir_server_t *ds = NULL;
+ tor_addr_port_t ipv6;
+ int rv = -1;
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* create a trusted ds without an IPv6 address and port */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+ tt_assert(get_n_authorities(V3_DIRINFO) == 1);
+ tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
+
+ /* create a trusted ds with an IPv6 address and port */
+ rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
+ tt_assert(rv == 0);
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+ tt_assert(get_n_authorities(V3_DIRINFO) == 2);
+ tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
+
+ done:
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static void
+test_config_adding_fallback_dir_server(void *arg)
+{
+ (void)arg;
+
+ const char digest[DIGEST_LEN] = "";
+ dir_server_t *ds = NULL;
+ tor_addr_t ipv4;
+ tor_addr_port_t ipv6;
+ int rv = -1;
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ rv = tor_addr_parse(&ipv4, "127.0.0.1");
+ tt_assert(rv == AF_INET);
+
+ /* create a trusted ds without an IPv6 address and port */
+ ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+ tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
+
+ /* create a trusted ds with an IPv6 address and port */
+ rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
+ tt_assert(rv == 0);
+ ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+ tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
+
+ done:
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+/* No secrets here:
+ * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
+ * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
+ * with added spaces
+ */
+#define TEST_DIR_AUTH_LINE_START \
+ "foobar orport=12345 " \
+ "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
+#define TEST_DIR_AUTH_LINE_END \
+ "1.2.3.4:54321 " \
+ "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
+#define TEST_DIR_AUTH_IPV6_FLAG \
+ "ipv6=[feed::beef]:9 "
+
+static void
+test_config_parsing_trusted_dir_server(void *arg)
+{
+ (void)arg;
+ int rv = -1;
+
+ /* parse a trusted dir server without an IPv6 address and port */
+ rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
+ TEST_DIR_AUTH_LINE_END,
+ V3_DIRINFO, 1);
+ tt_assert(rv == 0);
+
+ /* parse a trusted dir server with an IPv6 address and port */
+ rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
+ TEST_DIR_AUTH_IPV6_FLAG
+ TEST_DIR_AUTH_LINE_END,
+ V3_DIRINFO, 1);
+ tt_assert(rv == 0);
+
+ /* Since we are only validating, there is no cleanup. */
+ done:
+ ;
+}
+
+#undef TEST_DIR_AUTH_LINE_START
+#undef TEST_DIR_AUTH_LINE_END
+#undef TEST_DIR_AUTH_IPV6_FLAG
+
+/* No secrets here:
+ * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
+ */
+#define TEST_DIR_FALLBACK_LINE \
+ "1.2.3.4:54321 orport=12345 " \
+ "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
+#define TEST_DIR_FALLBACK_IPV6_FLAG \
+ "ipv6=[2015:c0de::deed]:9"
+
+static void
+test_config_parsing_fallback_dir_server(void *arg)
+{
+ (void)arg;
+ int rv = -1;
+
+ /* parse a trusted dir server without an IPv6 address and port */
+ rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
+ tt_assert(rv == 0);
+
+ /* parse a trusted dir server with an IPv6 address and port */
+ rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
+ TEST_DIR_FALLBACK_IPV6_FLAG,
+ 1);
+ tt_assert(rv == 0);
+
+ /* Since we are only validating, there is no cleanup. */
+ done:
+ ;
+}
+
+#undef TEST_DIR_FALLBACK_LINE
+#undef TEST_DIR_FALLBACK_IPV6_FLAG
+
+static void
+test_config_adding_default_trusted_dir_servers(void *arg)
+{
+ (void)arg;
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* Assume we only have one bridge authority */
+ add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
+ tt_assert(get_n_authorities(BRIDGE_DIRINFO) == 1);
+ tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
+
+ /* Assume we have eight V3 authorities */
+ add_default_trusted_dir_authorities(V3_DIRINFO);
+ tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 8);
+ tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 9);
+
+ done:
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
static int n_add_default_fallback_dir_servers_known_default = 0;
/**
@@ -1471,13 +1667,14 @@ add_default_fallback_dir_servers_known_default(void)
n_add_default_fallback_dir_servers_known_default++;
}
+/* Test all the different combinations of adding dir servers */
static void
test_config_adding_dir_servers(void *arg)
{
(void)arg;
/* allocate options */
- or_options_t *options = tor_malloc(sizeof(or_options_t));
+ or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
/* Allocate and populate configuration lines:
*
@@ -1486,8 +1683,7 @@ test_config_adding_dir_servers(void *arg)
* Zeroing the structure has the same effect as initialising to:
* { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
*/
- config_line_t *test_dir_authority = tor_malloc(sizeof(config_line_t));
- memset(test_dir_authority, 0, sizeof(config_line_t));
+ config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
test_dir_authority->key = tor_strdup("DirAuthority");
test_dir_authority->value = tor_strdup(
"D0 orport=9000 "
@@ -1495,16 +1691,16 @@ test_config_adding_dir_servers(void *arg)
"127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
);
- config_line_t *test_alt_bridge_authority = tor_malloc(sizeof(config_line_t));
- memset(test_alt_bridge_authority, 0, sizeof(config_line_t));
+ config_line_t *test_alt_bridge_authority = tor_malloc_zero(
+ sizeof(config_line_t));
test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
test_alt_bridge_authority->value = tor_strdup(
"B1 orport=9001 bridge "
"127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
);
- config_line_t *test_alt_dir_authority = tor_malloc(sizeof(config_line_t));
- memset(test_alt_dir_authority, 0, sizeof(config_line_t));
+ config_line_t *test_alt_dir_authority = tor_malloc_zero(
+ sizeof(config_line_t));
test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
test_alt_dir_authority->value = tor_strdup(
"A2 orport=9002 "
@@ -1513,23 +1709,23 @@ test_config_adding_dir_servers(void *arg)
);
/* Use the format specified in the manual page */
- config_line_t *test_fallback_directory = tor_malloc(sizeof(config_line_t));
- memset(test_fallback_directory, 0, sizeof(config_line_t));
+ config_line_t *test_fallback_directory = tor_malloc_zero(
+ sizeof(config_line_t));
test_fallback_directory->key = tor_strdup("FallbackDir");
test_fallback_directory->value = tor_strdup(
"127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
);
/* We need to know if add_default_fallback_dir_servers is called,
+ * whatever the size of the list in fallback_dirs.inc,
* so we use a version of add_default_fallback_dir_servers that adds
- * one known default fallback directory.
- * There doesn't appear to be any need to test it unmocked. */
+ * one known default fallback directory. */
MOCK(add_default_fallback_dir_servers,
add_default_fallback_dir_servers_known_default);
/* There are 16 different cases, covering each combination of set/NULL for:
* DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
- * FallbackDir.
+ * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
* But validate_dir_servers() ensures that:
* "You cannot set both DirAuthority and Alternate*Authority."
* This reduces the number of cases to 10.
@@ -1543,8 +1739,6 @@ test_config_adding_dir_servers(void *arg)
* The valid cases are cases 0-9 counting using this method, as every case
* greater than or equal to 10 = 1010 is invalid.
*
- * After #15642 - Disable default fallback dirs when any custom dirs set
- *
* 1. Outcome: Use Set Directory Authorities
* - No Default Authorities
* - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
@@ -1581,20 +1775,6 @@ test_config_adding_dir_servers(void *arg)
* Cases expected to yield this outcome:
* 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
* and FallbackDir are all NULL)
- *
- * Before #15642 but after #13163 - Stop using default authorities when both
- * Alternate Dir and Bridge Authority are set
- * (#13163 was committed in 0.2.6 as c1dd43d823c7)
- *
- * The behaviour is different in the following cases
- * where FallbackDir is NULL:
- * 2, 6, 8
- *
- * In these cases, the Default Fallback Directories are applied, even when
- * DirAuthorities or AlternateDirAuthority are set.
- *
- * However, as the list of default fallback directories is currently empty,
- * this change doesn't modify any user-visible behaviour.
*/
/*
@@ -1628,6 +1808,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = NULL;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -1637,6 +1818,9 @@ test_config_adding_dir_servers(void *arg)
/* we must have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* fallback_dir_servers */
const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
@@ -1669,7 +1853,10 @@ test_config_adding_dir_servers(void *arg)
n_default_fallback_dir = (smartlist_len(fallback_servers) -
n_default_alt_bridge_authority -
n_default_alt_dir_authority);
- /* If we have a negative count, something has gone really wrong */
+ /* If we have a negative count, something has gone really wrong,
+ * or some authorities aren't being added as fallback directories.
+ * (networkstatus_consensus_can_use_extra_fallbacks depends on all
+ * authorities being fallback directories.) */
tt_assert(n_default_fallback_dir >= 0);
}
}
@@ -1703,6 +1890,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = NULL;
options->FallbackDir = test_fallback_directory;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -1712,6 +1900,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -1840,6 +2031,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = NULL;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -1849,6 +2041,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we just have the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -1977,6 +2172,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = test_alt_bridge_authority;
options->AlternateDirAuthority = test_alt_dir_authority;
options->FallbackDir = test_fallback_directory;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -1986,6 +2182,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2115,6 +2314,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = test_alt_bridge_authority;
options->AlternateDirAuthority = test_alt_dir_authority;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2124,6 +2324,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2263,6 +2466,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = test_alt_bridge_authority;
options->AlternateDirAuthority = NULL;
options->FallbackDir = test_fallback_directory;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2272,6 +2476,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2413,6 +2620,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = test_alt_bridge_authority;
options->AlternateDirAuthority = NULL;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2422,6 +2630,9 @@ test_config_adding_dir_servers(void *arg)
/* we must have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2572,6 +2783,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = test_alt_dir_authority;
options->FallbackDir = test_fallback_directory;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2581,6 +2793,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2725,6 +2940,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = test_alt_dir_authority;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2734,6 +2950,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we just have the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -2887,6 +3106,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = NULL;
options->FallbackDir = test_fallback_directory;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -2896,6 +3116,9 @@ test_config_adding_dir_servers(void *arg)
/* we must not have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -3046,6 +3269,7 @@ test_config_adding_dir_servers(void *arg)
options->AlternateBridgeAuthority = NULL;
options->AlternateDirAuthority = NULL;
options->FallbackDir = NULL;
+ options->UseDefaultFallbackDirs = 1;
/* parse options - ensure we always update by passing NULL old_options */
consider_adding_dir_servers(options, NULL);
@@ -3055,6 +3279,9 @@ test_config_adding_dir_servers(void *arg)
/* we must have added the default fallback dirs */
tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
+ /* we have more fallbacks than just the authorities */
+ tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
+
{
/* trusted_dir_servers */
const smartlist_t *dir_servers = router_get_trusted_dir_servers();
@@ -3209,11 +3436,1472 @@ test_config_adding_dir_servers(void *arg)
UNMOCK(add_default_fallback_dir_servers);
}
+static void
+test_config_default_dir_servers(void *arg)
+{
+ or_options_t *opts = NULL;
+ (void)arg;
+ int trusted_count = 0;
+ int fallback_count = 0;
+
+ /* new set of options should stop fallback parsing */
+ opts = tor_malloc_zero(sizeof(or_options_t));
+ opts->UseDefaultFallbackDirs = 0;
+ /* set old_options to NULL to force dir update */
+ consider_adding_dir_servers(opts, NULL);
+ trusted_count = smartlist_len(router_get_trusted_dir_servers());
+ fallback_count = smartlist_len(router_get_fallback_dir_servers());
+ or_options_free(opts);
+ opts = NULL;
+
+ /* assume a release will never go out with less than 7 authorities */
+ tt_assert(trusted_count >= 7);
+ /* if we disable the default fallbacks, there must not be any extra */
+ tt_assert(fallback_count == trusted_count);
+
+ opts = tor_malloc_zero(sizeof(or_options_t));
+ opts->UseDefaultFallbackDirs = 1;
+ consider_adding_dir_servers(opts, opts);
+ trusted_count = smartlist_len(router_get_trusted_dir_servers());
+ fallback_count = smartlist_len(router_get_fallback_dir_servers());
+ or_options_free(opts);
+ opts = NULL;
+
+ /* assume a release will never go out with less than 7 authorities */
+ tt_assert(trusted_count >= 7);
+ /* XX/teor - allow for default fallbacks to be added without breaking
+ * the unit tests. Set a minimum fallback count once the list is stable. */
+ tt_assert(fallback_count >= trusted_count);
+
+ done:
+ or_options_free(opts);
+}
+
+static int mock_router_pick_published_address_result = 0;
+
+static int
+mock_router_pick_published_address(const or_options_t *options,
+ uint32_t *addr, int cache_only)
+{
+ (void)options;
+ (void)addr;
+ (void)cache_only;
+ return mock_router_pick_published_address_result;
+}
+
+static int mock_router_my_exit_policy_is_reject_star_result = 0;
+
+static int
+mock_router_my_exit_policy_is_reject_star(void)
+{
+ return mock_router_my_exit_policy_is_reject_star_result;
+}
+
+static int mock_advertised_server_mode_result = 0;
+
+static int
+mock_advertised_server_mode(void)
+{
+ return mock_advertised_server_mode_result;
+}
+
+static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
+
+static const routerinfo_t *
+mock_router_get_my_routerinfo(void)
+{
+ return mock_router_get_my_routerinfo_result;
+}
+
+static void
+test_config_directory_fetch(void *arg)
+{
+ (void)arg;
+
+ /* Test Setup */
+ or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
+ routerinfo_t routerinfo;
+ memset(&routerinfo, 0, sizeof(routerinfo));
+ mock_router_pick_published_address_result = -1;
+ mock_router_my_exit_policy_is_reject_star_result = 1;
+ mock_advertised_server_mode_result = 0;
+ mock_router_get_my_routerinfo_result = NULL;
+ MOCK(router_pick_published_address, mock_router_pick_published_address);
+ MOCK(router_my_exit_policy_is_reject_star,
+ mock_router_my_exit_policy_is_reject_star);
+ MOCK(advertised_server_mode, mock_advertised_server_mode);
+ MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
+
+ /* Clients can use multiple directory mirrors for bootstrap */
+ memset(options, 0, sizeof(or_options_t));
+ options->ClientOnly = 1;
+ tt_assert(server_mode(options) == 0);
+ tt_assert(public_server_mode(options) == 0);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 1);
+
+ /* Bridge Clients can use multiple directory mirrors for bootstrap */
+ memset(options, 0, sizeof(or_options_t));
+ options->UseBridges = 1;
+ tt_assert(server_mode(options) == 0);
+ tt_assert(public_server_mode(options) == 0);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 1);
+
+ /* Bridge Relays (Bridges) must act like clients, and use multiple
+ * directory mirrors for bootstrap */
+ memset(options, 0, sizeof(or_options_t));
+ options->BridgeRelay = 1;
+ options->ORPort_set = 1;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 0);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 1);
+
+ /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
+ * but can use multiple authorities for bootstrap */
+ memset(options, 0, sizeof(or_options_t));
+ options->FetchDirInfoEarly = 1;
+ tt_assert(server_mode(options) == 0);
+ tt_assert(public_server_mode(options) == 0);
+ tt_assert(directory_fetches_from_authorities(options) == 1);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 1);
+
+ /* OR servers only fetch the consensus from the authorities when they don't
+ * know their own address, but never use multiple directories for bootstrap
+ */
+ memset(options, 0, sizeof(or_options_t));
+ options->ORPort_set = 1;
+
+ mock_router_pick_published_address_result = -1;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 1);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ mock_router_pick_published_address_result = 0;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ /* Exit OR servers only fetch the consensus from the authorities when they
+ * refuse unknown exits, but never use multiple directories for bootstrap
+ */
+ memset(options, 0, sizeof(or_options_t));
+ options->ORPort_set = 1;
+ options->ExitRelay = 1;
+ mock_router_pick_published_address_result = 0;
+ mock_router_my_exit_policy_is_reject_star_result = 0;
+ mock_advertised_server_mode_result = 1;
+ mock_router_get_my_routerinfo_result = &routerinfo;
+
+ routerinfo.supports_tunnelled_dir_requests = 1;
+
+ options->RefuseUnknownExits = 1;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 1);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ options->RefuseUnknownExits = 0;
+ mock_router_pick_published_address_result = 0;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ /* Dir servers fetch the consensus from the authorities, unless they are not
+ * advertising themselves (hibernating) or have no routerinfo or are not
+ * advertising their dirport, and never use multiple directories for
+ * bootstrap. This only applies if they are also OR servers.
+ * (We don't care much about the behaviour of non-OR directory servers.) */
+ memset(options, 0, sizeof(or_options_t));
+ options->DirPort_set = 1;
+ options->ORPort_set = 1;
+ options->DirCache = 1;
+ mock_router_pick_published_address_result = 0;
+ mock_router_my_exit_policy_is_reject_star_result = 1;
+
+ mock_advertised_server_mode_result = 1;
+ routerinfo.dir_port = 1;
+ mock_router_get_my_routerinfo_result = &routerinfo;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 1);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ mock_advertised_server_mode_result = 0;
+ routerinfo.dir_port = 1;
+ mock_router_get_my_routerinfo_result = &routerinfo;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ mock_advertised_server_mode_result = 1;
+ mock_router_get_my_routerinfo_result = NULL;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ mock_advertised_server_mode_result = 1;
+ routerinfo.dir_port = 0;
+ routerinfo.supports_tunnelled_dir_requests = 0;
+ mock_router_get_my_routerinfo_result = &routerinfo;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 0);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ mock_advertised_server_mode_result = 1;
+ routerinfo.dir_port = 1;
+ routerinfo.supports_tunnelled_dir_requests = 1;
+ mock_router_get_my_routerinfo_result = &routerinfo;
+ tt_assert(server_mode(options) == 1);
+ tt_assert(public_server_mode(options) == 1);
+ tt_assert(directory_fetches_from_authorities(options) == 1);
+ tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
+ == 0);
+
+ done:
+ tor_free(options);
+ UNMOCK(router_pick_published_address);
+ UNMOCK(router_get_my_routerinfo);
+ UNMOCK(advertised_server_mode);
+ UNMOCK(router_my_exit_policy_is_reject_star);
+}
+
+static void
+test_config_default_fallback_dirs(void *arg)
+{
+ const char *fallback[] = {
+#include "../or/fallback_dirs.inc"
+ NULL
+ };
+
+ int n_included_fallback_dirs = 0;
+ int n_added_fallback_dirs = 0;
+
+ (void)arg;
+ clear_dir_servers();
+
+ while (fallback[n_included_fallback_dirs])
+ n_included_fallback_dirs++;
+
+ add_default_fallback_dir_servers();
+
+ n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
+
+ tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
+
+ done:
+ clear_dir_servers();
+}
+
+static void
+test_config_port_cfg_line_extract_addrport(void *arg)
+{
+ (void)arg;
+ int unixy = 0;
+ const char *rest = NULL;
+ char *a = NULL;
+
+ tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
+ OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "hello");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "flipperwalt");;
+ tt_str_op(rest, OP_EQ, "gersplut");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "flipperwalt");;
+ tt_str_op(rest, OP_EQ, "gersplut");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "flipperwalt");;
+ tt_str_op(rest, OP_EQ, "gersplut");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "flipperwalt");;
+ tt_str_op(rest, OP_EQ, "gersplut");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("lolol",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, "foobar:lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport(":lolol",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 0);
+ tt_str_op(a, OP_EQ, ":lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lolol");;
+ tt_str_op(rest, OP_EQ, "foo ");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lol ol");;
+ tt_str_op(rest, OP_EQ, "foo ");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
+ &a, &unixy, &rest), OP_EQ, 0);
+ tt_int_op(unixy, OP_EQ, 1);
+ tt_str_op(a, OP_EQ, "lol\" ol");;
+ tt_str_op(rest, OP_EQ, "foo ");
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
+ &a, &unixy, &rest), OP_EQ, -1);
+ tor_free(a);
+
+ tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
+ &a, &unixy, &rest), OP_EQ, -1);
+ tor_free(a);
+
+ done:
+ tor_free(a);
+}
+
+static config_line_t *
+mock_config_line(const char *key, const char *val)
+{
+ config_line_t *config_line = tor_malloc(sizeof(config_line_t));
+ memset(config_line, 0, sizeof(config_line_t));
+ config_line->key = tor_strdup(key);
+ config_line->value = tor_strdup(val);
+ return config_line;
+}
+
+static void
+test_config_parse_port_config__listenaddress(void *data)
+{
+ (void)data;
+ int ret;
+ config_line_t *config_listen_address = NULL, *config_listen_address2 = NULL,
+ *config_listen_address3 = NULL;
+ config_line_t *config_port1 = NULL, *config_port2 = NULL,
+ *config_port3 = NULL, *config_port4 = NULL, *config_port5 = NULL;
+ smartlist_t *slout = NULL;
+ port_cfg_t *port_cfg = NULL;
+
+ // Test basic invocation with no arguments
+ ret = parse_port_config(NULL, NULL, NULL, NULL, 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Setup some test data
+ config_listen_address = mock_config_line("DNSListenAddress", "127.0.0.1");
+ config_listen_address2 = mock_config_line("DNSListenAddress", "x$$$:::345");
+ config_listen_address3 = mock_config_line("DNSListenAddress",
+ "127.0.0.1:1442");
+ config_port1 = mock_config_line("DNSPort", "42");
+ config_port2 = mock_config_line("DNSPort", "43");
+ config_port1->next = config_port2;
+ config_port3 = mock_config_line("DNSPort", "auto");
+ config_port4 = mock_config_line("DNSPort", "55542");
+ config_port5 = mock_config_line("DNSPort", "666777");
+
+ // Test failure when we have a ListenAddress line and several
+ // Port lines for the same portname
+ ret = parse_port_config(NULL, config_port1, config_listen_address, "DNS", 0,
+ NULL, 0, 0);
+
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test case when we have a listen address, no default port and allow
+ // spurious listen address lines
+ ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
+ 0, CL_PORT_ALLOW_EXTRA_LISTENADDR);
+ tt_int_op(ret, OP_EQ, 1);
+
+ // Test case when we have a listen address, no default port but doesn't
+ // allow spurious listen address lines
+ ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
+ 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test case when we have a listen address, and a port that points to auto,
+ // should use the AUTO port
+ slout = smartlist_new();
+ ret = parse_port_config(slout, config_port3, config_listen_address, "DNS",
+ 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
+
+ // Test when we have a listen address and a custom port
+ ret = parse_port_config(slout, config_port4, config_listen_address, "DNS",
+ 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 2);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 1);
+ tt_int_op(port_cfg->port, OP_EQ, 55542);
+
+ // Test when we have a listen address and an invalid custom port
+ ret = parse_port_config(slout, config_port5, config_listen_address, "DNS",
+ 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test we get a server port configuration when asked for it
+ ret = parse_port_config(slout, NULL, config_listen_address, "DNS", 0, NULL,
+ 123, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 4);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 2);
+ tt_int_op(port_cfg->port, OP_EQ, 123);
+ tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
+ tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
+
+ // Test an invalid ListenAddress configuration
+ ret = parse_port_config(NULL, NULL, config_listen_address2, "DNS", 0, NULL,
+ 222, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test default to the port in the listen address if available
+ ret = parse_port_config(slout, config_port2, config_listen_address3, "DNS",
+ 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 5);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 4);
+ tt_int_op(port_cfg->port, OP_EQ, 1442);
+
+ // Test we work correctly without an out, but with a listen address
+ // and a port
+ ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
+ 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test warning nonlocal control
+ ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
+ CONN_TYPE_CONTROL_LISTENER, NULL, 0,
+ CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test warning nonlocal ext or listener
+ ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
+ CONN_TYPE_EXT_OR_LISTENER, NULL, 0,
+ CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test warning nonlocal other
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
+ 0, NULL, 0, CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test warning nonlocal control without an out
+ ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
+ CONN_TYPE_CONTROL_LISTENER, NULL, 0,
+ CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ config_free_lines(config_listen_address);
+ config_free_lines(config_listen_address2);
+ config_free_lines(config_listen_address3);
+ config_free_lines(config_port1);
+ /* 2 was linked from 1. */
+ config_free_lines(config_port3);
+ config_free_lines(config_port4);
+ config_free_lines(config_port5);
+ if (slout)
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_free(slout);
+}
+
+static void
+test_config_parse_port_config__ports__no_ports_given(void *data)
+{
+ (void)data;
+ int ret;
+ smartlist_t *slout = NULL;
+ port_cfg_t *port_cfg = NULL;
+
+ slout = smartlist_new();
+
+ // Test no defaultport, no defaultaddress and no out
+ ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test with defaultport, no defaultaddress and no out
+ ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 42, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test no defaultport, with defaultaddress and no out
+ ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test with defaultport, with defaultaddress and no out
+ ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test no defaultport, no defaultaddress and with out
+ ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 0);
+
+ // Test with defaultport, no defaultaddress and with out
+ ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 42, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 0);
+
+ // Test no defaultport, with defaultaddress and with out
+ ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 0);
+
+ // Test with defaultport, with defaultaddress and out, adds a new port cfg
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, 42);
+ tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
+
+ // Test with defaultport, with defaultaddress and out, adds a new port cfg
+ // for a unix address
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "/foo/bar/unixdomain",
+ 42, CL_PORT_IS_UNIXSOCKET);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, 0);
+ tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
+ tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
+
+ done:
+ if (slout)
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_free(slout);
+}
+
+static void
+test_config_parse_port_config__ports__ports_given(void *data)
+{
+ (void)data;
+ int ret;
+ smartlist_t *slout = NULL;
+ port_cfg_t *port_cfg = NULL;
+ config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
+ tor_addr_t addr;
+
+ slout = smartlist_new();
+
+ // Test error when encounters an invalid Port specification
+ config_port_invalid = mock_config_line("DNSPort", "");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test error when encounters an empty unix domain specification
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort", "unix:");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test error when encounters a unix domain specification but the listener
+ // doesn't support domain sockets
+ config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
+ ret = parse_port_config(NULL, config_port_valid, NULL, "DNS",
+ CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test valid unix domain
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0, 0);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, 0);
+ tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
+ tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
+ /* Test entry port defaults as initialised in parse_port_config */
+ tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
+#endif
+
+ // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("SOCKSPort",
+ "unix:/tmp/foo/bar NoIPv4Traffic "
+ "NoOnionTraffic");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure if we have no DNS and we're a DNSPort
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort",
+ "127.0.0.1:80 NoDNSRequest");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS",
+ CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // If we're a DNSPort, DNS only is ok
+ // Use a port because DNSPort doesn't support sockets
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
+ "NoIPv4Traffic NoOnionTraffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
+ CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
+
+ // Test failure if we have DNS but no ipv4 and no ipv6
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("SOCKSPort",
+ "unix:/tmp/foo/bar NoIPv4Traffic");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
+ // options)
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
+ "NoDNSRequest NoIPv4Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
+#endif
+
+ // Test success with quoted unix: address.
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
+ "NoDNSRequest NoIPv4Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
+#endif
+
+ // Test failure with broken quoted unix: address.
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
+ "NoDNSRequest NoIPv4Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure with empty quoted unix: address.
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
+ "NoDNSRequest NoIPv4Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
+ "OnionTrafficOnly");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
+#endif
+
+ // Test success with no ipv4 but take ipv6
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
+ "NoIPv4Traffic IPv6Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
+#endif
+
+ // Test success with both ipv4 and ipv6
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
+ "IPv4Traffic IPv6Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, NULL, 0,
+ CL_PORT_TAKES_HOSTNAMES);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
+#endif
+
+ // Test failure if we specify world writable for an IP Port
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure if we specify group writable for an IP Port
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure if we specify group writable for an IP Port
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with only a port (this will fail without a default address)
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ config_port_valid = mock_config_line("DNSPort", "42");
+ ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with only a port and isolate destination port
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT | ISO_DESTPORT);
+
+ // Test success with a negative isolate destination port, and plural
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT & ~ISO_DESTPORT);
+
+ // Test success with isolate destination address
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT | ISO_DESTADDR);
+
+ // Test success with isolate socks AUTH
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT | ISO_SOCKSAUTH);
+
+ // Test success with isolate client protocol
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT | ISO_CLIENTPROTO);
+
+ // Test success with isolate client address
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
+ ISO_DEFAULT | ISO_CLIENTADDR);
+
+ // Test success with ignored unknown options
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
+ ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with no isolate socks AUTH
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.3", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
+
+ // Test success with prefer ipv6
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort",
+ "42 IPv6Traffic PreferIPv6");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
+ CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
+
+ // Test success with cache ipv4 DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
+
+ // Test success with cache ipv6 DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
+
+ // Test success with no cache ipv4 DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
+
+ // Test success with cache DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
+
+ // Test success with use cached ipv4 DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
+
+ // Test success with use cached ipv6 DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
+
+ // Test success with use cached DNS
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
+
+ // Test success with not preferring ipv6 automap
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
+
+ // Test success with prefer SOCKS no auth
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
+
+ // Test failure with both a zero port and a non-zero port
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "0");
+ config_port_valid = mock_config_line("DNSPort", "42");
+ config_port_invalid->next = config_port_valid;
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.42", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with warn non-local control
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, config_port_valid, NULL, "Control",
+ CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
+ CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with warn non-local listener
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, config_port_valid, NULL, "ExtOR",
+ CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
+ CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with warn non-local other
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with warn non-local other without out
+ ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test success with both ipv4 and ipv6 but without stream options
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
+ "IPv6Traffic");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.44", 0,
+ CL_PORT_TAKES_HOSTNAMES |
+ CL_PORT_NO_STREAM_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
+ tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
+
+ // Test failure for a SessionGroup argument with invalid value
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // TODO: this seems wrong. Shouldn't it be the other way around?
+ // Potential bug.
+ // Test failure for a SessionGroup argument with valid value but with stream
+ // options allowed
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.44", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure for more than one SessionGroup argument
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
+ "SessionGroup=321");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with a sessiongroup options
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
+
+ // Test success with a zero unix domain socket, and doesnt add it to out
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "0");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 0);
+
+ // Test success with a one unix domain socket, and doesnt add it to out
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "something");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
+ tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
+
+ // Test success with a port of auto - it uses the default address
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "auto");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
+ tor_addr_parse(&addr, "127.0.0.46");
+ tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
+
+ // Test success with parsing both an address and an auto port
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
+ tor_addr_parse(&addr, "127.0.0.122");
+ tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
+
+ // Test failure when asked to parse an invalid address followed by auto
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
+ ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with parsing both an address and a real port
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->port, OP_EQ, 656);
+ tor_addr_parse(&addr, "127.0.0.123");
+ tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
+
+ // Test failure if we can't parse anything at all
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "something wrong");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure if we find both an address, a port and an auto
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
+ "127.0.0.46", 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test that default to group writeable default sets group writeable for
+ // domain socket
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
+ ret = parse_port_config(slout, config_port_valid, NULL, "SOCKS",
+ CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
+ CL_PORT_DFLT_GROUP_WRITABLE);
+#ifdef _WIN32
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
+#endif
+
+ done:
+ if (slout)
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_free(slout);
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+}
+
+static void
+test_config_parse_port_config__ports__server_options(void *data)
+{
+ (void)data;
+ int ret;
+ smartlist_t *slout = NULL;
+ port_cfg_t *port_cfg = NULL;
+ config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
+
+ slout = smartlist_new();
+
+ // Test success with NoAdvertise option
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ config_port_valid = mock_config_line("DNSPort",
+ "127.0.0.124:656 NoAdvertise");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
+ CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
+ tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
+
+ // Test success with NoListen option
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
+ CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
+ tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
+
+ // Test failure with both NoAdvertise and NoListen option
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
+ "NoAdvertise");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with IPv4Only
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
+ CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
+ tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
+
+ // Test success with IPv6Only
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
+ CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+ port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
+ tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
+ tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
+
+ // Test failure with both IPv4Only and IPv6Only
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
+ "IPv4Only");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test success with invalid parameter
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
+ ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
+ CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(smartlist_len(slout), OP_EQ, 1);
+
+ // Test failure when asked to bind only to ipv6 but gets an ipv4 address
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort",
+ "127.0.0.124:656 IPv6Only");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test failure when asked to bind only to ipv4 but gets an ipv6 address
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
+ 0, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Check for failure with empty unix: address.
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_clear(slout);
+ config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
+ ret = parse_port_config(slout, config_port_invalid, NULL, "ORPort", 0, NULL,
+ 0, CL_PORT_SERVER_OPTIONS);
+ tt_int_op(ret, OP_EQ, -1);
+
+ done:
+ if (slout)
+ SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
+ smartlist_free(slout);
+ config_free_lines(config_port_invalid); config_port_invalid = NULL;
+ config_free_lines(config_port_valid); config_port_valid = NULL;
+}
+
#define CONFIG_TEST(name, flags) \
{ #name, test_config_ ## name, flags, NULL, NULL }
struct testcase_t config_tests[] = {
+ CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
+ CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
+ CONFIG_TEST(parsing_trusted_dir_server, 0),
+ CONFIG_TEST(parsing_fallback_dir_server, 0),
+ CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
CONFIG_TEST(adding_dir_servers, TT_FORK),
+ CONFIG_TEST(default_dir_servers, TT_FORK),
+ CONFIG_TEST(default_fallback_dirs, 0),
CONFIG_TEST(resolve_my_address, TT_FORK),
CONFIG_TEST(addressmap, 0),
CONFIG_TEST(parse_bridge_line, 0),
@@ -3222,6 +4910,12 @@ struct testcase_t config_tests[] = {
CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
CONFIG_TEST(write_to_data_subdir, TT_FORK),
CONFIG_TEST(fix_my_family, 0),
+ CONFIG_TEST(directory_fetch, 0),
+ CONFIG_TEST(port_cfg_line_extract_addrport, 0),
+ CONFIG_TEST(parse_port_config__listenaddress, 0),
+ CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
+ CONFIG_TEST(parse_port_config__ports__server_options, 0),
+ CONFIG_TEST(parse_port_config__ports__ports_given, 0),
END_OF_TESTCASES
};
diff --git a/src/test/test_connection.c b/src/test/test_connection.c
new file mode 100644
index 0000000000..d394fc9852
--- /dev/null
+++ b/src/test/test_connection.c
@@ -0,0 +1,882 @@
+/* Copyright (c) 2015-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+
+#define CONNECTION_PRIVATE
+#define MAIN_PRIVATE
+
+#include "or.h"
+#include "test.h"
+
+#include "connection.h"
+#include "main.h"
+#include "microdesc.h"
+#include "networkstatus.h"
+#include "rendcache.h"
+#include "directory.h"
+
+static void test_conn_lookup_addr_helper(const char *address,
+ int family,
+ tor_addr_t *addr);
+
+static void * test_conn_get_basic_setup(const struct testcase_t *tc);
+static int test_conn_get_basic_teardown(const struct testcase_t *tc,
+ void *arg);
+
+static void * test_conn_get_rend_setup(const struct testcase_t *tc);
+static int test_conn_get_rend_teardown(const struct testcase_t *tc,
+ void *arg);
+
+static void * test_conn_get_rsrc_setup(const struct testcase_t *tc);
+static int test_conn_get_rsrc_teardown(const struct testcase_t *tc,
+ void *arg);
+
+/* Arbitrary choice - IPv4 Directory Connection to localhost */
+#define TEST_CONN_TYPE (CONN_TYPE_DIR)
+/* We assume every machine has IPv4 localhost, is that ok? */
+#define TEST_CONN_ADDRESS "127.0.0.1"
+#define TEST_CONN_PORT (12345)
+#define TEST_CONN_ADDRESS_PORT "127.0.0.1:12345"
+#define TEST_CONN_FAMILY (AF_INET)
+#define TEST_CONN_STATE (DIR_CONN_STATE_MIN_)
+#define TEST_CONN_ADDRESS_2 "127.0.0.2"
+
+#define TEST_CONN_BASIC_PURPOSE (DIR_PURPOSE_MIN_)
+
+#define TEST_CONN_REND_ADDR "cfs3rltphxxvabci"
+#define TEST_CONN_REND_PURPOSE (DIR_PURPOSE_FETCH_RENDDESC_V2)
+#define TEST_CONN_REND_PURPOSE_SUCCESSFUL (DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2)
+#define TEST_CONN_REND_TYPE_2 (CONN_TYPE_AP)
+#define TEST_CONN_REND_ADDR_2 "icbavxxhptlr3sfc"
+
+#define TEST_CONN_RSRC (networkstatus_get_flavor_name(FLAV_MICRODESC))
+#define TEST_CONN_RSRC_PURPOSE (DIR_PURPOSE_FETCH_CONSENSUS)
+#define TEST_CONN_RSRC_STATE_SUCCESSFUL (DIR_CONN_STATE_CLIENT_FINISHED)
+#define TEST_CONN_RSRC_2 (networkstatus_get_flavor_name(FLAV_NS))
+
+#define TEST_CONN_DL_STATE (DIR_CONN_STATE_CLIENT_READING)
+
+/* see AP_CONN_STATE_IS_UNATTACHED() */
+#define TEST_CONN_UNATTACHED_STATE (AP_CONN_STATE_CIRCUIT_WAIT)
+#define TEST_CONN_ATTACHED_STATE (AP_CONN_STATE_CONNECT_WAIT)
+
+#define TEST_CONN_FD_INIT 50
+static int mock_connection_connect_sockaddr_called = 0;
+static int fake_socket_number = TEST_CONN_FD_INIT;
+
+static int
+mock_connection_connect_sockaddr(connection_t *conn,
+ const struct sockaddr *sa,
+ socklen_t sa_len,
+ const struct sockaddr *bindaddr,
+ socklen_t bindaddr_len,
+ int *socket_error)
+{
+ (void)sa_len;
+ (void)bindaddr;
+ (void)bindaddr_len;
+
+ tor_assert(conn);
+ tor_assert(sa);
+ tor_assert(socket_error);
+
+ mock_connection_connect_sockaddr_called++;
+
+ conn->s = fake_socket_number++;
+ tt_assert(SOCKET_OK(conn->s));
+ /* We really should call tor_libevent_initialize() here. Because we don't,
+ * we are relying on other parts of the code not checking if the_event_base
+ * (and therefore event->ev_base) is NULL. */
+ tt_assert(connection_add_connecting(conn) == 0);
+
+ done:
+ /* Fake "connected" status */
+ return 1;
+}
+
+static int
+fake_close_socket(evutil_socket_t sock)
+{
+ (void)sock;
+ return 0;
+}
+
+static void
+test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
+{
+ int rv = 0;
+
+ tt_assert(addr);
+
+ rv = tor_addr_lookup(address, family, addr);
+ /* XXXX - should we retry on transient failure? */
+ tt_assert(rv == 0);
+ tt_assert(tor_addr_is_loopback(addr));
+ tt_assert(tor_addr_is_v4(addr));
+
+ return;
+
+ done:
+ tor_addr_make_null(addr, TEST_CONN_FAMILY);
+}
+
+static connection_t *
+test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
+{
+ connection_t *conn = NULL;
+ tor_addr_t addr;
+ int socket_err = 0;
+ int in_progress = 0;
+
+ MOCK(connection_connect_sockaddr,
+ mock_connection_connect_sockaddr);
+ MOCK(tor_close_socket, fake_close_socket);
+
+ init_connection_lists();
+
+ conn = connection_new(type, TEST_CONN_FAMILY);
+ tt_assert(conn);
+
+ test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
+ tt_assert(!tor_addr_is_null(&addr));
+
+ tor_addr_copy_tight(&conn->addr, &addr);
+ conn->port = TEST_CONN_PORT;
+ mock_connection_connect_sockaddr_called = 0;
+ in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
+ TEST_CONN_PORT, &socket_err);
+ tt_assert(mock_connection_connect_sockaddr_called == 1);
+ tt_assert(!socket_err);
+ tt_assert(in_progress == 0 || in_progress == 1);
+
+ /* fake some of the attributes so the connection looks OK */
+ conn->state = state;
+ conn->purpose = purpose;
+ assert_connection_ok(conn, time(NULL));
+
+ UNMOCK(connection_connect_sockaddr);
+ UNMOCK(tor_close_socket);
+ return conn;
+
+ /* On failure */
+ done:
+ UNMOCK(connection_connect_sockaddr);
+ UNMOCK(tor_close_socket);
+ return NULL;
+}
+
+static void *
+test_conn_get_basic_setup(const struct testcase_t *tc)
+{
+ (void)tc;
+ return test_conn_get_connection(TEST_CONN_STATE, TEST_CONN_TYPE,
+ TEST_CONN_BASIC_PURPOSE);
+}
+
+static int
+test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
+{
+ (void)tc;
+ connection_t *conn = arg;
+
+ tt_assert(conn);
+ assert_connection_ok(conn, time(NULL));
+
+ /* teardown the connection as fast as possible */
+ if (conn->linked_conn) {
+ assert_connection_ok(conn->linked_conn, time(NULL));
+
+ /* We didn't call tor_libevent_initialize(), so event_base was NULL,
+ * so we can't rely on connection_unregister_events() use of event_del().
+ */
+ if (conn->linked_conn->read_event) {
+ tor_free(conn->linked_conn->read_event);
+ conn->linked_conn->read_event = NULL;
+ }
+ if (conn->linked_conn->write_event) {
+ tor_free(conn->linked_conn->write_event);
+ conn->linked_conn->write_event = NULL;
+ }
+
+ if (!conn->linked_conn->marked_for_close) {
+ connection_close_immediate(conn->linked_conn);
+ if (CONN_IS_EDGE(conn->linked_conn)) {
+ /* Suppress warnings about all the stuff we didn't do */
+ TO_EDGE_CONN(conn->linked_conn)->edge_has_sent_end = 1;
+ TO_EDGE_CONN(conn->linked_conn)->end_reason =
+ END_STREAM_REASON_INTERNAL;
+ if (conn->linked_conn->type == CONN_TYPE_AP) {
+ TO_ENTRY_CONN(conn->linked_conn)->socks_request->has_finished = 1;
+ }
+ }
+ connection_mark_for_close(conn->linked_conn);
+ }
+
+ close_closeable_connections();
+ }
+
+ /* We didn't set the events up properly, so we can't use event_del() in
+ * close_closeable_connections() > connection_free()
+ * > connection_unregister_events() */
+ if (conn->read_event) {
+ tor_free(conn->read_event);
+ conn->read_event = NULL;
+ }
+ if (conn->write_event) {
+ tor_free(conn->write_event);
+ conn->write_event = NULL;
+ }
+
+ if (!conn->marked_for_close) {
+ connection_close_immediate(conn);
+ if (CONN_IS_EDGE(conn)) {
+ /* Suppress warnings about all the stuff we didn't do */
+ TO_EDGE_CONN(conn)->edge_has_sent_end = 1;
+ TO_EDGE_CONN(conn)->end_reason = END_STREAM_REASON_INTERNAL;
+ if (conn->type == CONN_TYPE_AP) {
+ TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
+ }
+ }
+ connection_mark_for_close(conn);
+ }
+
+ close_closeable_connections();
+
+ /* The unit test will fail if we return 0 */
+ return 1;
+
+ /* When conn == NULL, we can't cleanup anything */
+ done:
+ return 0;
+}
+
+static void *
+test_conn_get_rend_setup(const struct testcase_t *tc)
+{
+ dir_connection_t *conn = DOWNCAST(dir_connection_t,
+ test_conn_get_connection(
+ TEST_CONN_STATE,
+ TEST_CONN_TYPE,
+ TEST_CONN_REND_PURPOSE));
+ tt_assert(conn);
+ assert_connection_ok(&conn->base_, time(NULL));
+
+ rend_cache_init();
+
+ /* TODO: use directory_initiate_command_rend() to do this - maybe? */
+ conn->rend_data = tor_malloc_zero(sizeof(rend_data_t));
+ tor_assert(strlen(TEST_CONN_REND_ADDR) == REND_SERVICE_ID_LEN_BASE32);
+ memcpy(conn->rend_data->onion_address,
+ TEST_CONN_REND_ADDR,
+ REND_SERVICE_ID_LEN_BASE32+1);
+ conn->rend_data->hsdirs_fp = smartlist_new();
+
+ assert_connection_ok(&conn->base_, time(NULL));
+ return conn;
+
+ /* On failure */
+ done:
+ test_conn_get_rend_teardown(tc, conn);
+ /* Returning NULL causes the unit test to fail */
+ return NULL;
+}
+
+static int
+test_conn_get_rend_teardown(const struct testcase_t *tc, void *arg)
+{
+ dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
+ int rv = 0;
+
+ tt_assert(conn);
+ assert_connection_ok(&conn->base_, time(NULL));
+
+ /* avoid a last-ditch attempt to refetch the descriptor */
+ conn->base_.purpose = TEST_CONN_REND_PURPOSE_SUCCESSFUL;
+
+ /* connection_free_() cleans up rend_data */
+ rv = test_conn_get_basic_teardown(tc, arg);
+ done:
+ rend_cache_free_all();
+ return rv;
+}
+
+static dir_connection_t *
+test_conn_download_status_add_a_connection(const char *resource)
+{
+ dir_connection_t *conn = DOWNCAST(dir_connection_t,
+ test_conn_get_connection(
+ TEST_CONN_STATE,
+ TEST_CONN_TYPE,
+ TEST_CONN_RSRC_PURPOSE));
+
+ tt_assert(conn);
+ assert_connection_ok(&conn->base_, time(NULL));
+
+ /* Replace the existing resource with the one we want */
+ if (resource) {
+ if (conn->requested_resource) {
+ tor_free(conn->requested_resource);
+ }
+ conn->requested_resource = tor_strdup(resource);
+ assert_connection_ok(&conn->base_, time(NULL));
+ }
+
+ return conn;
+
+ done:
+ test_conn_get_rsrc_teardown(NULL, conn);
+ return NULL;
+}
+
+static void *
+test_conn_get_rsrc_setup(const struct testcase_t *tc)
+{
+ (void)tc;
+ return test_conn_download_status_add_a_connection(TEST_CONN_RSRC);
+}
+
+static int
+test_conn_get_rsrc_teardown(const struct testcase_t *tc, void *arg)
+{
+ int rv = 0;
+
+ connection_t *conn = (connection_t *)arg;
+ tt_assert(conn);
+ assert_connection_ok(conn, time(NULL));
+
+ if (conn->type == CONN_TYPE_DIR) {
+ dir_connection_t *dir_conn = DOWNCAST(dir_connection_t, arg);
+
+ tt_assert(dir_conn);
+ assert_connection_ok(&dir_conn->base_, time(NULL));
+
+ /* avoid a last-ditch attempt to refetch the consensus */
+ dir_conn->base_.state = TEST_CONN_RSRC_STATE_SUCCESSFUL;
+ assert_connection_ok(&dir_conn->base_, time(NULL));
+ }
+
+ /* connection_free_() cleans up requested_resource */
+ rv = test_conn_get_basic_teardown(tc, conn);
+
+ done:
+ return rv;
+}
+
+static void *
+test_conn_download_status_setup(const struct testcase_t *tc)
+{
+ return (void*)tc;
+}
+
+static int
+test_conn_download_status_teardown(const struct testcase_t *tc, void *arg)
+{
+ (void)arg;
+ int rv = 0;
+
+ /* Ignore arg, and just loop through the connection array */
+ SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
+ if (conn) {
+ assert_connection_ok(conn, time(NULL));
+
+ /* connection_free_() cleans up requested_resource */
+ rv = test_conn_get_rsrc_teardown(tc, conn);
+ tt_assert(rv == 1);
+ }
+ } SMARTLIST_FOREACH_END(conn);
+
+ done:
+ return rv;
+}
+
+/* Like connection_ap_make_link(), but does much less */
+static connection_t *
+test_conn_get_linked_connection(connection_t *l_conn, uint8_t state)
+{
+ tt_assert(l_conn);
+ assert_connection_ok(l_conn, time(NULL));
+
+ /* AP connections don't seem to have purposes */
+ connection_t *conn = test_conn_get_connection(state, CONN_TYPE_AP,
+ 0);
+
+ tt_assert(conn);
+ assert_connection_ok(conn, time(NULL));
+
+ conn->linked = 1;
+ l_conn->linked = 1;
+ conn->linked_conn = l_conn;
+ l_conn->linked_conn = conn;
+ /* we never opened a real socket, so we can just overwrite it */
+ conn->s = TOR_INVALID_SOCKET;
+ l_conn->s = TOR_INVALID_SOCKET;
+
+ assert_connection_ok(conn, time(NULL));
+ assert_connection_ok(l_conn, time(NULL));
+
+ return conn;
+
+ done:
+ test_conn_download_status_teardown(NULL, NULL);
+ return NULL;
+}
+
+static struct testcase_setup_t test_conn_get_basic_st = {
+ test_conn_get_basic_setup, test_conn_get_basic_teardown
+};
+
+static struct testcase_setup_t test_conn_get_rend_st = {
+ test_conn_get_rend_setup, test_conn_get_rend_teardown
+};
+
+static struct testcase_setup_t test_conn_get_rsrc_st = {
+ test_conn_get_rsrc_setup, test_conn_get_rsrc_teardown
+};
+
+static struct testcase_setup_t test_conn_download_status_st = {
+ test_conn_download_status_setup, test_conn_download_status_teardown
+};
+
+static void
+test_conn_get_basic(void *arg)
+{
+ connection_t *conn = (connection_t*)arg;
+ tor_addr_t addr, addr2;
+
+ tt_assert(conn);
+ assert_connection_ok(conn, time(NULL));
+
+ test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
+ tt_assert(!tor_addr_is_null(&addr));
+ test_conn_lookup_addr_helper(TEST_CONN_ADDRESS_2, TEST_CONN_FAMILY, &addr2);
+ tt_assert(!tor_addr_is_null(&addr2));
+
+ /* Check that we get this connection back when we search for it by
+ * its attributes, but get NULL when we supply a different value. */
+
+ tt_assert(connection_get_by_global_id(conn->global_identifier) == conn);
+ tt_assert(connection_get_by_global_id(!conn->global_identifier) == NULL);
+
+ tt_assert(connection_get_by_type(conn->type) == conn);
+ tt_assert(connection_get_by_type(TEST_CONN_TYPE) == conn);
+ tt_assert(connection_get_by_type(!conn->type) == NULL);
+ tt_assert(connection_get_by_type(!TEST_CONN_TYPE) == NULL);
+
+ tt_assert(connection_get_by_type_state(conn->type, conn->state)
+ == conn);
+ tt_assert(connection_get_by_type_state(TEST_CONN_TYPE, TEST_CONN_STATE)
+ == conn);
+ tt_assert(connection_get_by_type_state(!conn->type, !conn->state)
+ == NULL);
+ tt_assert(connection_get_by_type_state(!TEST_CONN_TYPE, !TEST_CONN_STATE)
+ == NULL);
+
+ /* Match on the connection fields themselves */
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &conn->addr,
+ conn->port,
+ conn->purpose)
+ == conn);
+ /* Match on the original inputs to the connection */
+ tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
+ &conn->addr,
+ conn->port,
+ conn->purpose)
+ == conn);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &addr,
+ conn->port,
+ conn->purpose)
+ == conn);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &conn->addr,
+ TEST_CONN_PORT,
+ conn->purpose)
+ == conn);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &conn->addr,
+ conn->port,
+ TEST_CONN_BASIC_PURPOSE)
+ == conn);
+ tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
+ &addr,
+ TEST_CONN_PORT,
+ TEST_CONN_BASIC_PURPOSE)
+ == conn);
+ /* Then try each of the not-matching combinations */
+ tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
+ &conn->addr,
+ conn->port,
+ conn->purpose)
+ == NULL);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &addr2,
+ conn->port,
+ conn->purpose)
+ == NULL);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &conn->addr,
+ !conn->port,
+ conn->purpose)
+ == NULL);
+ tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
+ &conn->addr,
+ conn->port,
+ !conn->purpose)
+ == NULL);
+ /* Then try everything not-matching */
+ tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
+ &addr2,
+ !conn->port,
+ !conn->purpose)
+ == NULL);
+ tt_assert(connection_get_by_type_addr_port_purpose(!TEST_CONN_TYPE,
+ &addr2,
+ !TEST_CONN_PORT,
+ !TEST_CONN_BASIC_PURPOSE)
+ == NULL);
+
+ done:
+ ;
+}
+
+static void
+test_conn_get_rend(void *arg)
+{
+ dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
+ tt_assert(conn);
+ assert_connection_ok(&conn->base_, time(NULL));
+
+ tt_assert(connection_get_by_type_state_rendquery(
+ conn->base_.type,
+ conn->base_.state,
+ conn->rend_data->onion_address)
+ == TO_CONN(conn));
+ tt_assert(connection_get_by_type_state_rendquery(
+ TEST_CONN_TYPE,
+ TEST_CONN_STATE,
+ TEST_CONN_REND_ADDR)
+ == TO_CONN(conn));
+ tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
+ !conn->base_.state,
+ "")
+ == NULL);
+ tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
+ !TEST_CONN_STATE,
+ TEST_CONN_REND_ADDR_2)
+ == NULL);
+
+ done:
+ ;
+}
+
+#define sl_is_conn_assert(sl_input, conn) \
+ do { \
+ the_sl = (sl_input); \
+ tt_assert(smartlist_len((the_sl)) == 1); \
+ tt_assert(smartlist_get((the_sl), 0) == (conn)); \
+ smartlist_free(the_sl); the_sl = NULL; \
+ } while (0)
+
+#define sl_no_conn_assert(sl_input) \
+ do { \
+ the_sl = (sl_input); \
+ tt_assert(smartlist_len((the_sl)) == 0); \
+ smartlist_free(the_sl); the_sl = NULL; \
+ } while (0)
+
+static void
+test_conn_get_rsrc(void *arg)
+{
+ dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
+ smartlist_t *the_sl = NULL;
+ tt_assert(conn);
+ assert_connection_ok(&conn->base_, time(NULL));
+
+ sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
+ conn->base_.purpose,
+ conn->requested_resource),
+ conn);
+ sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC),
+ conn);
+ sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
+ !conn->base_.purpose,
+ ""));
+ sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
+ !TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC_2));
+
+ sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
+ conn->base_.purpose,
+ conn->requested_resource,
+ conn->base_.state),
+ conn);
+ sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
+ TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC,
+ TEST_CONN_STATE),
+ conn);
+ sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
+ !conn->base_.purpose,
+ "",
+ !conn->base_.state));
+ sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
+ !TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC_2,
+ !TEST_CONN_STATE));
+
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ conn->base_.purpose,
+ conn->requested_resource)
+ == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC)
+ == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ !conn->base_.purpose,
+ "")
+ == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ !TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC_2)
+ == 0);
+
+ tt_assert(connection_dir_count_by_purpose_resource_and_state(
+ conn->base_.purpose,
+ conn->requested_resource,
+ conn->base_.state)
+ == 1);
+ tt_assert(connection_dir_count_by_purpose_resource_and_state(
+ TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC,
+ TEST_CONN_STATE)
+ == 1);
+ tt_assert(connection_dir_count_by_purpose_resource_and_state(
+ !conn->base_.purpose,
+ "",
+ !conn->base_.state)
+ == 0);
+ tt_assert(connection_dir_count_by_purpose_resource_and_state(
+ !TEST_CONN_RSRC_PURPOSE,
+ TEST_CONN_RSRC_2,
+ !TEST_CONN_STATE)
+ == 0);
+
+ done:
+ smartlist_free(the_sl);
+}
+
+static void
+test_conn_download_status(void *arg)
+{
+ dir_connection_t *conn = NULL;
+ dir_connection_t *conn2 = NULL;
+ dir_connection_t *conn4 = NULL;
+ connection_t *ap_conn = NULL;
+
+ const struct testcase_t *tc = arg;
+ consensus_flavor_t usable_flavor = (consensus_flavor_t)tc->setup_data;
+
+ /* The "other flavor" trick only works if there are two flavors */
+ tor_assert(N_CONSENSUS_FLAVORS == 2);
+ consensus_flavor_t other_flavor = ((usable_flavor == FLAV_NS)
+ ? FLAV_MICRODESC
+ : FLAV_NS);
+ const char *res = networkstatus_get_flavor_name(usable_flavor);
+ const char *other_res = networkstatus_get_flavor_name(other_flavor);
+
+ /* no connections */
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* one connection, not downloading */
+ conn = test_conn_download_status_add_a_connection(res);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* one connection, downloading but not linked (not possible on a client,
+ * but possible on a relay) */
+ conn->base_.state = TEST_CONN_DL_STATE;
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* one connection, downloading and linked, but not yet attached */
+ ap_conn = test_conn_get_linked_connection(TO_CONN(conn),
+ TEST_CONN_UNATTACHED_STATE);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* one connection, downloading and linked and attached */
+ ap_conn->state = TEST_CONN_ATTACHED_STATE;
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* one connection, linked and attached but not downloading */
+ conn->base_.state = TEST_CONN_STATE;
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* two connections, both not downloading */
+ conn2 = test_conn_download_status_add_a_connection(res);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 2);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* two connections, one downloading */
+ conn->base_.state = TEST_CONN_DL_STATE;
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 2);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+ conn->base_.state = TEST_CONN_STATE;
+
+ /* more connections, all not downloading */
+ /* ignore the return value, it's free'd using the connection list */
+ (void)test_conn_download_status_add_a_connection(res);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* more connections, one downloading */
+ conn->base_.state = TEST_CONN_DL_STATE;
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* more connections, two downloading (should never happen, but needs
+ * to be tested for completeness) */
+ conn2->base_.state = TEST_CONN_DL_STATE;
+ /* ignore the return value, it's free'd using the connection list */
+ (void)test_conn_get_linked_connection(TO_CONN(conn2),
+ TEST_CONN_ATTACHED_STATE);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+ conn->base_.state = TEST_CONN_STATE;
+
+ /* more connections, a different one downloading */
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 0);
+
+ /* a connection for the other flavor (could happen if a client is set to
+ * cache directory documents), one preferred flavor downloading
+ */
+ conn4 = test_conn_download_status_add_a_connection(other_res);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 1);
+
+ /* a connection for the other flavor (could happen if a client is set to
+ * cache directory documents), both flavors downloading
+ */
+ conn4->base_.state = TEST_CONN_DL_STATE;
+ /* ignore the return value, it's free'd using the connection list */
+ (void)test_conn_get_linked_connection(TO_CONN(conn4),
+ TEST_CONN_ATTACHED_STATE);
+ tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
+ tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 1);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ res) == 3);
+ tt_assert(connection_dir_count_by_purpose_and_resource(
+ TEST_CONN_RSRC_PURPOSE,
+ other_res) == 1);
+
+ done:
+ /* the teardown function removes all the connections in the global list*/;
+}
+
+#define CONNECTION_TESTCASE(name, fork, setup) \
+ { #name, test_conn_##name, fork, &setup, NULL }
+
+/* where arg is an expression (constant, varaible, compound expression) */
+#define CONNECTION_TESTCASE_ARG(name, fork, setup, arg) \
+ { #name "_" #arg, test_conn_##name, fork, &setup, (void *)arg }
+
+struct testcase_t connection_tests[] = {
+ CONNECTION_TESTCASE(get_basic, TT_FORK, test_conn_get_basic_st),
+ CONNECTION_TESTCASE(get_rend, TT_FORK, test_conn_get_rend_st),
+ CONNECTION_TESTCASE(get_rsrc, TT_FORK, test_conn_get_rsrc_st),
+ CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
+ test_conn_download_status_st, FLAV_MICRODESC),
+ CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
+ test_conn_download_status_st, FLAV_NS),
+//CONNECTION_TESTCASE(func_suffix, TT_FORK, setup_func_pair),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_containers.c b/src/test/test_containers.c
index 1ee240fb0d..d8b82e0661 100644
--- a/src/test/test_containers.c
+++ b/src/test/test_containers.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -132,7 +132,7 @@ test_container_smartlist_strings(void *arg)
tt_str_op("def",OP_EQ, smartlist_get(sl, 5));
tt_str_op(" ",OP_EQ, smartlist_get(sl, 6));
tt_str_op("ghijk",OP_EQ, smartlist_get(sl, 7));
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
@@ -149,7 +149,7 @@ test_container_smartlist_strings(void *arg)
tt_str_op("bnud",OP_EQ, smartlist_get(sl,6));
tt_str_op("",OP_EQ, smartlist_get(sl,7));
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
smartlist_split_string(sl, " ab\tc \td ef ", NULL,
@@ -165,7 +165,7 @@ test_container_smartlist_strings(void *arg)
tt_str_op("ghi",OP_EQ, smartlist_get(sl,4));
tt_str_op("j",OP_EQ, smartlist_get(sl,5));
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
@@ -186,7 +186,7 @@ test_container_smartlist_strings(void *arg)
tt_int_op(5,OP_EQ, smartlist_len(sl));
tt_str_op("z",OP_EQ, smartlist_get(sl, 3));
tt_str_op("zhasd <> <> bnud<>",OP_EQ, smartlist_get(sl, 4));
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
smartlist_split_string(sl, "abcd\n", "\n",
@@ -198,7 +198,7 @@ test_container_smartlist_strings(void *arg)
tt_int_op(2,OP_EQ, smartlist_len(sl));
tt_str_op("efgh",OP_EQ, smartlist_get(sl, 1));
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
/* Test swapping, shuffling, and sorting. */
@@ -286,7 +286,7 @@ test_container_smartlist_strings(void *arg)
tt_str_op(cp_alloc,OP_EQ, "and");
tor_free(cp_alloc);
tt_int_op(smartlist_len(sl),OP_EQ, 6);
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
cp_alloc = smartlist_pop_last(sl);
tt_ptr_op(cp_alloc,OP_EQ, NULL);
@@ -326,7 +326,7 @@ test_container_smartlist_strings(void *arg)
tt_assert(!allsame);
tt_assert(allin);
}
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_clear(sl);
/* Test string_remove and remove and join_strings2 */
@@ -348,7 +348,7 @@ test_container_smartlist_strings(void *arg)
done:
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_free(sl);
tor_free(cp_alloc);
}
@@ -437,7 +437,7 @@ test_container_smartlist_digests(void *arg)
tt_mem_op(smartlist_get(sl, 1),OP_EQ, "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
done:
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_free(sl);
}
@@ -490,7 +490,7 @@ test_container_smartlist_join(void *arg)
smartlist_free(sl3);
SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
smartlist_free(sl2);
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_free(sl);
tor_free(joined);
}
@@ -528,7 +528,7 @@ test_container_smartlist_pos(void *arg)
tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), ==, 6);
done:
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_free(sl);
}
@@ -1140,7 +1140,7 @@ test_container_smartlist_most_frequent(void *arg)
tt_str_op(cp, ==, "def"); /* No tie */
done:
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
smartlist_free(sl);
}
diff --git a/src/test/test_controller.c b/src/test/test_controller.c
index b40825bb5d..f19c846144 100644
--- a/src/test/test_controller.c
+++ b/src/test/test_controller.c
@@ -1,10 +1,13 @@
-/* Copyright (c) 2015, The Tor Project, Inc. */
+/* Copyright (c) 2015-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CONTROL_PRIVATE
#include "or.h"
#include "control.h"
+#include "entrynodes.h"
+#include "networkstatus.h"
#include "rendservice.h"
+#include "routerlist.h"
#include "test.h"
static void
@@ -134,6 +137,8 @@ test_rend_service_parse_port_config(void *arg)
cfg = rend_service_parse_port_config("80,[2001:db8::1]:8080", sep, &err_msg);
tt_assert(cfg);
tt_assert(!err_msg);
+ rend_service_port_config_free(cfg);
+ cfg = NULL;
/* XXX: Someone should add tests for AF_UNIX targets if supported. */
@@ -148,16 +153,1191 @@ test_rend_service_parse_port_config(void *arg)
cfg = rend_service_parse_port_config("90001", sep, &err_msg);
tt_assert(!cfg);
tt_assert(err_msg);
+ tor_free(err_msg);
+
+ /* unix port */
+ cfg = NULL;
+
+ /* quoted unix port */
+ tor_free(err_msg);
+ cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
+ " ", &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+ rend_service_port_config_free(cfg);
+ cfg = NULL;
+
+ /* quoted unix port */
+ tor_free(err_msg);
+ cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
+ " ", &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+ rend_service_port_config_free(cfg);
+ cfg = NULL;
+
+ /* quoted unix port, missing end quote */
+ cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar",
+ " ", &err_msg);
+ tt_assert(!cfg);
+ tt_str_op(err_msg, OP_EQ, "Couldn't process address <unix:\"/tmp/foo bar> "
+ "from hidden service configuration");
+ tor_free(err_msg);
+
+ /* bogus IP address */
+ cfg = rend_service_parse_port_config("100 1.2.3.4.5:9000",
+ " ", &err_msg);
+ tt_assert(!cfg);
+ tt_str_op(err_msg, OP_EQ, "Unparseable address in hidden service port "
+ "configuration.");
+ tor_free(err_msg);
+
+ /* bogus port port */
+ cfg = rend_service_parse_port_config("100 99999",
+ " ", &err_msg);
+ tt_assert(!cfg);
+ tt_str_op(err_msg, OP_EQ, "Unparseable or out-of-range port \"99999\" "
+ "in hidden service port configuration.");
+ tor_free(err_msg);
done:
rend_service_port_config_free(cfg);
tor_free(err_msg);
}
+static void
+test_add_onion_helper_clientauth(void *arg)
+{
+ rend_authorized_client_t *client = NULL;
+ char *err_msg = NULL;
+ int created = 0;
+
+ (void)arg;
+
+ /* Test "ClientName" only. */
+ client = add_onion_helper_clientauth("alice", &created, &err_msg);
+ tt_assert(client);
+ tt_assert(created);
+ tt_assert(!err_msg);
+ rend_authorized_client_free(client);
+
+ /* Test "ClientName:Blob" */
+ client = add_onion_helper_clientauth("alice:475hGBHPlq7Mc0cRZitK/B",
+ &created, &err_msg);
+ tt_assert(client);
+ tt_assert(!created);
+ tt_assert(!err_msg);
+ rend_authorized_client_free(client);
+
+ /* Test invalid client names */
+ client = add_onion_helper_clientauth("no*asterisks*allowed", &created,
+ &err_msg);
+ tt_assert(!client);
+ tt_assert(err_msg);
+ tor_free(err_msg);
+
+ /* Test invalid auth cookie */
+ client = add_onion_helper_clientauth("alice:12345", &created, &err_msg);
+ tt_assert(!client);
+ tt_assert(err_msg);
+ tor_free(err_msg);
+
+ /* Test invalid syntax */
+ client = add_onion_helper_clientauth(":475hGBHPlq7Mc0cRZitK/B", &created,
+ &err_msg);
+ tt_assert(!client);
+ tt_assert(err_msg);
+ tor_free(err_msg);
+
+ done:
+ rend_authorized_client_free(client);
+ tor_free(err_msg);
+}
+
+/* Mocks and data/variables used for GETINFO download status tests */
+
+static const download_status_t dl_status_default =
+ { 0, 0, 0, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
+static download_status_t ns_dl_status[N_CONSENSUS_FLAVORS];
+static download_status_t ns_dl_status_bootstrap[N_CONSENSUS_FLAVORS];
+static download_status_t ns_dl_status_running[N_CONSENSUS_FLAVORS];
+
+/*
+ * These should explore all the possible cases of download_status_to_string()
+ * in control.c
+ */
+static const download_status_t dls_sample_1 =
+ { 1467163900, 0, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_DETERMINISTIC, 0, 0 };
+static const char * dls_sample_1_str =
+ "next-attempt-at 2016-06-29 01:31:40\n"
+ "n-download-failures 0\n"
+ "n-download-attempts 0\n"
+ "schedule DL_SCHED_GENERIC\n"
+ "want-authority DL_WANT_ANY_DIRSERVER\n"
+ "increment-on DL_SCHED_INCREMENT_FAILURE\n"
+ "backoff DL_SCHED_DETERMINISTIC\n";
+static const download_status_t dls_sample_2 =
+ { 1467164400, 1, 2, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_DETERMINISTIC, 0, 0 };
+static const char * dls_sample_2_str =
+ "next-attempt-at 2016-06-29 01:40:00\n"
+ "n-download-failures 1\n"
+ "n-download-attempts 2\n"
+ "schedule DL_SCHED_CONSENSUS\n"
+ "want-authority DL_WANT_AUTHORITY\n"
+ "increment-on DL_SCHED_INCREMENT_FAILURE\n"
+ "backoff DL_SCHED_DETERMINISTIC\n";
+static const download_status_t dls_sample_3 =
+ { 1467154400, 12, 25, DL_SCHED_BRIDGE, DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_ATTEMPT, DL_SCHED_DETERMINISTIC, 0, 0 };
+static const char * dls_sample_3_str =
+ "next-attempt-at 2016-06-28 22:53:20\n"
+ "n-download-failures 12\n"
+ "n-download-attempts 25\n"
+ "schedule DL_SCHED_BRIDGE\n"
+ "want-authority DL_WANT_ANY_DIRSERVER\n"
+ "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
+ "backoff DL_SCHED_DETERMINISTIC\n";
+static const download_status_t dls_sample_4 =
+ { 1467166600, 3, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
+static const char * dls_sample_4_str =
+ "next-attempt-at 2016-06-29 02:16:40\n"
+ "n-download-failures 3\n"
+ "n-download-attempts 0\n"
+ "schedule DL_SCHED_GENERIC\n"
+ "want-authority DL_WANT_ANY_DIRSERVER\n"
+ "increment-on DL_SCHED_INCREMENT_FAILURE\n"
+ "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
+ "last-backoff-position 0\n"
+ "last-delay-used 0\n";
+static const download_status_t dls_sample_5 =
+ { 1467164600, 3, 7, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 1, 2112, };
+static const char * dls_sample_5_str =
+ "next-attempt-at 2016-06-29 01:43:20\n"
+ "n-download-failures 3\n"
+ "n-download-attempts 7\n"
+ "schedule DL_SCHED_CONSENSUS\n"
+ "want-authority DL_WANT_ANY_DIRSERVER\n"
+ "increment-on DL_SCHED_INCREMENT_FAILURE\n"
+ "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
+ "last-backoff-position 1\n"
+ "last-delay-used 2112\n";
+static const download_status_t dls_sample_6 =
+ { 1467164200, 4, 9, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_ATTEMPT, DL_SCHED_RANDOM_EXPONENTIAL, 3, 432 };
+static const char * dls_sample_6_str =
+ "next-attempt-at 2016-06-29 01:36:40\n"
+ "n-download-failures 4\n"
+ "n-download-attempts 9\n"
+ "schedule DL_SCHED_CONSENSUS\n"
+ "want-authority DL_WANT_AUTHORITY\n"
+ "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
+ "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
+ "last-backoff-position 3\n"
+ "last-delay-used 432\n";
+
+/* Simulated auth certs */
+static const char *auth_id_digest_1_str =
+ "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061";
+static download_status_t auth_def_cert_download_status_1;
+static const char *auth_id_digest_2_str =
+ "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99";
+static download_status_t auth_def_cert_download_status_2;
+/* Expected form of digest list returned for GETINFO downloads/cert/fps */
+static const char *auth_id_digest_expected_list =
+ "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061\n"
+ "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99\n";
+
+/* Signing keys for simulated auth 1 */
+static const char *auth_1_sk_1_str =
+ "AA69566029B1F023BA09451B8F1B10952384EB58";
+static download_status_t auth_1_sk_1_dls;
+static const char *auth_1_sk_2_str =
+ "710865C7F06B73C5292695A8C34F1C94F769FF72";
+static download_status_t auth_1_sk_2_dls;
+/*
+ * Expected form of sk digest list for
+ * GETINFO downloads/cert/<auth_id_digest_1_str>/sks
+ */
+static const char *auth_1_sk_digest_expected_list =
+ "AA69566029B1F023BA09451B8F1B10952384EB58\n"
+ "710865C7F06B73C5292695A8C34F1C94F769FF72\n";
+
+/* Signing keys for simulated auth 2 */
+static const char *auth_2_sk_1_str =
+ "4299047E00D070AD6703FE00BE7AA756DB061E62";
+static download_status_t auth_2_sk_1_dls;
+static const char *auth_2_sk_2_str =
+ "9451B8F1B10952384EB58B5F230C0BB701626C9B";
+static download_status_t auth_2_sk_2_dls;
+/*
+ * Expected form of sk digest list for
+ * GETINFO downloads/cert/<auth_id_digest_2_str>/sks
+ */
+static const char *auth_2_sk_digest_expected_list =
+ "4299047E00D070AD6703FE00BE7AA756DB061E62\n"
+ "9451B8F1B10952384EB58B5F230C0BB701626C9B\n";
+
+/* Simulated router descriptor digests or bridge identity digests */
+static const char *descbr_digest_1_str =
+ "616408544C7345822696074A1A3DFA16AB381CBD";
+static download_status_t descbr_digest_1_dl;
+static const char *descbr_digest_2_str =
+ "06E8067246967265DBCB6641631B530EFEC12DC3";
+static download_status_t descbr_digest_2_dl;
+/* Expected form of digest list returned for GETINFO downloads/desc/descs */
+static const char *descbr_expected_list =
+ "616408544C7345822696074A1A3DFA16AB381CBD\n"
+ "06E8067246967265DBCB6641631B530EFEC12DC3\n";
+/*
+ * Flag to make all descbr queries fail, to simulate not being
+ * configured such that such queries make sense.
+ */
+static int disable_descbr = 0;
+
+static void
+reset_mocked_dl_statuses(void)
+{
+ int i;
+
+ for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
+ memcpy(&(ns_dl_status[i]), &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&(ns_dl_status_bootstrap[i]), &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&(ns_dl_status_running[i]), &dl_status_default,
+ sizeof(download_status_t));
+ }
+
+ memcpy(&auth_def_cert_download_status_1, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&auth_def_cert_download_status_2, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&auth_1_sk_1_dls, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&auth_1_sk_2_dls, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&auth_2_sk_1_dls, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&auth_2_sk_2_dls, &dl_status_default,
+ sizeof(download_status_t));
+
+ memcpy(&descbr_digest_1_dl, &dl_status_default,
+ sizeof(download_status_t));
+ memcpy(&descbr_digest_2_dl, &dl_status_default,
+ sizeof(download_status_t));
+}
+
+static download_status_t *
+ns_dl_status_mock(consensus_flavor_t flavor)
+{
+ return &(ns_dl_status[flavor]);
+}
+
+static download_status_t *
+ns_dl_status_bootstrap_mock(consensus_flavor_t flavor)
+{
+ return &(ns_dl_status_bootstrap[flavor]);
+}
+
+static download_status_t *
+ns_dl_status_running_mock(consensus_flavor_t flavor)
+{
+ return &(ns_dl_status_running[flavor]);
+}
+
+static void
+setup_ns_mocks(void)
+{
+ MOCK(networkstatus_get_dl_status_by_flavor, ns_dl_status_mock);
+ MOCK(networkstatus_get_dl_status_by_flavor_bootstrap,
+ ns_dl_status_bootstrap_mock);
+ MOCK(networkstatus_get_dl_status_by_flavor_running,
+ ns_dl_status_running_mock);
+ reset_mocked_dl_statuses();
+}
+
+static void
+clear_ns_mocks(void)
+{
+ UNMOCK(networkstatus_get_dl_status_by_flavor);
+ UNMOCK(networkstatus_get_dl_status_by_flavor_bootstrap);
+ UNMOCK(networkstatus_get_dl_status_by_flavor_running);
+}
+
+static smartlist_t *
+cert_dl_status_auth_ids_mock(void)
+{
+ char digest[DIGEST_LEN], *tmp;
+ int len;
+ smartlist_t *list = NULL;
+
+ /* Just pretend we have only the two hard-coded digests listed above */
+ list = smartlist_new();
+ len = base16_decode(digest, DIGEST_LEN,
+ auth_id_digest_1_str, strlen(auth_id_digest_1_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, digest, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ len = base16_decode(digest, DIGEST_LEN,
+ auth_id_digest_2_str, strlen(auth_id_digest_2_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, digest, DIGEST_LEN);
+ smartlist_add(list, tmp);
+
+ done:
+ return list;
+}
+
+static download_status_t *
+cert_dl_status_def_for_auth_mock(const char *digest)
+{
+ download_status_t *dl = NULL;
+ char digest_str[HEX_DIGEST_LEN+1];
+
+ tt_assert(digest != NULL);
+ base16_encode(digest_str, HEX_DIGEST_LEN + 1,
+ digest, DIGEST_LEN);
+ digest_str[HEX_DIGEST_LEN] = '\0';
+
+ if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
+ dl = &auth_def_cert_download_status_1;
+ } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
+ dl = &auth_def_cert_download_status_2;
+ }
+
+ done:
+ return dl;
+}
+
+static smartlist_t *
+cert_dl_status_sks_for_auth_id_mock(const char *digest)
+{
+ smartlist_t *list = NULL;
+ char sk[DIGEST_LEN];
+ char digest_str[HEX_DIGEST_LEN+1];
+ char *tmp;
+ int len;
+
+ tt_assert(digest != NULL);
+ base16_encode(digest_str, HEX_DIGEST_LEN + 1,
+ digest, DIGEST_LEN);
+ digest_str[HEX_DIGEST_LEN] = '\0';
+
+ /*
+ * Build a list of two hard-coded digests, depending on what we
+ * were just passed.
+ */
+ if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
+ list = smartlist_new();
+ len = base16_decode(sk, DIGEST_LEN,
+ auth_1_sk_1_str, strlen(auth_1_sk_1_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, sk, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ len = base16_decode(sk, DIGEST_LEN,
+ auth_1_sk_2_str, strlen(auth_1_sk_2_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, sk, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
+ list = smartlist_new();
+ len = base16_decode(sk, DIGEST_LEN,
+ auth_2_sk_1_str, strlen(auth_2_sk_1_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, sk, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ len = base16_decode(sk, DIGEST_LEN,
+ auth_2_sk_2_str, strlen(auth_2_sk_2_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, sk, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ }
+
+ done:
+ return list;
+}
+
+static download_status_t *
+cert_dl_status_fp_sk_mock(const char *fp_digest, const char *sk_digest)
+{
+ download_status_t *dl = NULL;
+ char fp_digest_str[HEX_DIGEST_LEN+1], sk_digest_str[HEX_DIGEST_LEN+1];
+
+ /*
+ * Unpack the digests so we can compare them and figure out which
+ * dl status we want.
+ */
+
+ tt_assert(fp_digest != NULL);
+ base16_encode(fp_digest_str, HEX_DIGEST_LEN + 1,
+ fp_digest, DIGEST_LEN);
+ fp_digest_str[HEX_DIGEST_LEN] = '\0';
+ tt_assert(sk_digest != NULL);
+ base16_encode(sk_digest_str, HEX_DIGEST_LEN + 1,
+ sk_digest, DIGEST_LEN);
+ sk_digest_str[HEX_DIGEST_LEN] = '\0';
+
+ if (strcmp(fp_digest_str, auth_id_digest_1_str) == 0) {
+ if (strcmp(sk_digest_str, auth_1_sk_1_str) == 0) {
+ dl = &auth_1_sk_1_dls;
+ } else if (strcmp(sk_digest_str, auth_1_sk_2_str) == 0) {
+ dl = &auth_1_sk_2_dls;
+ }
+ } else if (strcmp(fp_digest_str, auth_id_digest_2_str) == 0) {
+ if (strcmp(sk_digest_str, auth_2_sk_1_str) == 0) {
+ dl = &auth_2_sk_1_dls;
+ } else if (strcmp(sk_digest_str, auth_2_sk_2_str) == 0) {
+ dl = &auth_2_sk_2_dls;
+ }
+ }
+
+ done:
+ return dl;
+}
+
+static void
+setup_cert_mocks(void)
+{
+ MOCK(list_authority_ids_with_downloads, cert_dl_status_auth_ids_mock);
+ MOCK(id_only_download_status_for_authority_id,
+ cert_dl_status_def_for_auth_mock);
+ MOCK(list_sk_digests_for_authority_id,
+ cert_dl_status_sks_for_auth_id_mock);
+ MOCK(download_status_for_authority_id_and_sk,
+ cert_dl_status_fp_sk_mock);
+ reset_mocked_dl_statuses();
+}
+
+static void
+clear_cert_mocks(void)
+{
+ UNMOCK(list_authority_ids_with_downloads);
+ UNMOCK(id_only_download_status_for_authority_id);
+ UNMOCK(list_sk_digests_for_authority_id);
+ UNMOCK(download_status_for_authority_id_and_sk);
+}
+
+static smartlist_t *
+descbr_get_digests_mock(void)
+{
+ char digest[DIGEST_LEN], *tmp;
+ int len;
+ smartlist_t *list = NULL;
+
+ if (!disable_descbr) {
+ /* Just pretend we have only the two hard-coded digests listed above */
+ list = smartlist_new();
+ len = base16_decode(digest, DIGEST_LEN,
+ descbr_digest_1_str, strlen(descbr_digest_1_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, digest, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ len = base16_decode(digest, DIGEST_LEN,
+ descbr_digest_2_str, strlen(descbr_digest_2_str));
+ tt_int_op(len, OP_EQ, DIGEST_LEN);
+ tmp = tor_malloc(DIGEST_LEN);
+ memcpy(tmp, digest, DIGEST_LEN);
+ smartlist_add(list, tmp);
+ }
+
+ done:
+ return list;
+}
+
+static download_status_t *
+descbr_get_dl_by_digest_mock(const char *digest)
+{
+ download_status_t *dl = NULL;
+ char digest_str[HEX_DIGEST_LEN+1];
+
+ if (!disable_descbr) {
+ tt_assert(digest != NULL);
+ base16_encode(digest_str, HEX_DIGEST_LEN + 1,
+ digest, DIGEST_LEN);
+ digest_str[HEX_DIGEST_LEN] = '\0';
+
+ if (strcmp(digest_str, descbr_digest_1_str) == 0) {
+ dl = &descbr_digest_1_dl;
+ } else if (strcmp(digest_str, descbr_digest_2_str) == 0) {
+ dl = &descbr_digest_2_dl;
+ }
+ }
+
+ done:
+ return dl;
+}
+
+static void
+setup_desc_mocks(void)
+{
+ MOCK(router_get_descriptor_digests,
+ descbr_get_digests_mock);
+ MOCK(router_get_dl_status_by_descriptor_digest,
+ descbr_get_dl_by_digest_mock);
+ reset_mocked_dl_statuses();
+}
+
+static void
+clear_desc_mocks(void)
+{
+ UNMOCK(router_get_descriptor_digests);
+ UNMOCK(router_get_dl_status_by_descriptor_digest);
+}
+
+static void
+setup_bridge_mocks(void)
+{
+ disable_descbr = 0;
+
+ MOCK(list_bridge_identities,
+ descbr_get_digests_mock);
+ MOCK(get_bridge_dl_status_by_id,
+ descbr_get_dl_by_digest_mock);
+ reset_mocked_dl_statuses();
+}
+
+static void
+clear_bridge_mocks(void)
+{
+ UNMOCK(list_bridge_identities);
+ UNMOCK(get_bridge_dl_status_by_id);
+
+ disable_descbr = 0;
+}
+
+static void
+test_download_status_consensus(void *arg)
+{
+ /* We just need one of these to pass, it doesn't matter what's in it */
+ control_connection_t dummy;
+ /* Get results out */
+ char *answer = NULL;
+ const char *errmsg = NULL;
+
+ (void)arg;
+
+ /* Check that the unknown prefix case works; no mocks needed yet */
+ getinfo_helper_downloads(&dummy, "downloads/foo", &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_str_op(errmsg, OP_EQ, "Unknown download status query");
+
+ setup_ns_mocks();
+
+ /*
+ * Check returning serialized dlstatuses, and implicitly also test
+ * download_status_to_string().
+ */
+
+ /* Case 1 default/FLAV_NS*/
+ memcpy(&(ns_dl_status[FLAV_NS]), &dls_sample_1,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_1_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 2 default/FLAV_MICRODESC */
+ memcpy(&(ns_dl_status[FLAV_MICRODESC]), &dls_sample_2,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy, "downloads/networkstatus/microdesc",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_2_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 3 bootstrap/FLAV_NS */
+ memcpy(&(ns_dl_status_bootstrap[FLAV_NS]), &dls_sample_3,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns/bootstrap",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_3_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 4 bootstrap/FLAV_MICRODESC */
+ memcpy(&(ns_dl_status_bootstrap[FLAV_MICRODESC]), &dls_sample_4,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy,
+ "downloads/networkstatus/microdesc/bootstrap",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_4_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 5 running/FLAV_NS */
+ memcpy(&(ns_dl_status_running[FLAV_NS]), &dls_sample_5,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy,
+ "downloads/networkstatus/ns/running",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_5_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 6 running/FLAV_MICRODESC */
+ memcpy(&(ns_dl_status_running[FLAV_MICRODESC]), &dls_sample_6,
+ sizeof(download_status_t));
+ getinfo_helper_downloads(&dummy,
+ "downloads/networkstatus/microdesc/running",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_6_str);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Now check the error case */
+ getinfo_helper_downloads(&dummy, "downloads/networkstatus/foo",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "Unknown flavor");
+ errmsg = NULL;
+
+ done:
+ clear_ns_mocks();
+ tor_free(answer);
+
+ return;
+}
+
+static void
+test_download_status_cert(void *arg)
+{
+ /* We just need one of these to pass, it doesn't matter what's in it */
+ control_connection_t dummy;
+ /* Get results out */
+ char *question = NULL;
+ char *answer = NULL;
+ const char *errmsg = NULL;
+
+ (void)arg;
+
+ setup_cert_mocks();
+
+ /*
+ * Check returning serialized dlstatuses and digest lists, and implicitly
+ * also test download_status_to_string() and digest_list_to_string().
+ */
+
+ /* Case 1 - list of authority identity fingerprints */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fps",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, auth_id_digest_expected_list);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 2 - download status for default cert for 1st auth id */
+ memcpy(&auth_def_cert_download_status_1, &dls_sample_1,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_1_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 3 - download status for default cert for 2nd auth id */
+ memcpy(&auth_def_cert_download_status_2, &dls_sample_2,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_2_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 4 - list of signing key digests for 1st auth id */
+ tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, auth_1_sk_digest_expected_list);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 5 - list of signing key digests for 2nd auth id */
+ tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, auth_2_sk_digest_expected_list);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 6 - download status for 1st auth id, 1st sk */
+ memcpy(&auth_1_sk_1_dls, &dls_sample_3,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s/%s",
+ auth_id_digest_1_str, auth_1_sk_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_3_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 7 - download status for 1st auth id, 2nd sk */
+ memcpy(&auth_1_sk_2_dls, &dls_sample_4,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s/%s",
+ auth_id_digest_1_str, auth_1_sk_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_4_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 8 - download status for 2nd auth id, 1st sk */
+ memcpy(&auth_2_sk_1_dls, &dls_sample_5,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s/%s",
+ auth_id_digest_2_str, auth_2_sk_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_5_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 9 - download status for 2nd auth id, 2nd sk */
+ memcpy(&auth_2_sk_2_dls, &dls_sample_6,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/cert/fp/%s/%s",
+ auth_id_digest_2_str, auth_2_sk_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_6_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Now check the error cases */
+
+ /* Case 1 - query is garbage after downloads/cert/ part */
+ getinfo_helper_downloads(&dummy, "downloads/cert/blahdeblah",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "Unknown certificate download status query");
+ errmsg = NULL;
+
+ /*
+ * Case 2 - looks like downloads/cert/fp/<fp>, but <fp> isn't even
+ * the right length for a digest.
+ */
+ getinfo_helper_downloads(&dummy, "downloads/cert/fp/2B1D36D32B2942406",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
+ errmsg = NULL;
+
+ /*
+ * Case 3 - looks like downloads/cert/fp/<fp>, and <fp> is digest-sized,
+ * but not parseable as one.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
+ errmsg = NULL;
+
+ /*
+ * Case 4 - downloads/cert/fp/<fp>, and <fp> is not a known authority
+ * identity digest
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "Failed to get download status for this authority identity digest");
+ errmsg = NULL;
+
+ /*
+ * Case 5 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
+ * parse as a sensible digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1/blah",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
+ errmsg = NULL;
+
+ /*
+ * Case 6 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
+ * parse as a sensible digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/82F52AF55D25/blah",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
+ errmsg = NULL;
+
+ /*
+ * Case 7 - downloads/cert/fp/<fp>/sks, and <fp> is not a known authority
+ * digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/sks",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "Failed to get list of signing key digests for this authority "
+ "identity digest");
+ errmsg = NULL;
+
+ /*
+ * Case 8 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
+ * parse as a signing key digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
+ "82F52AF55D250115FE44D3GC81D49643241D56A1",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
+ errmsg = NULL;
+
+ /*
+ * Case 9 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
+ * parse as a signing key digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
+ "82F52AF55D250115FE44D",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
+ errmsg = NULL;
+
+ /*
+ * Case 10 - downloads/cert/fp/<fp>/<sk>, but <fp> isn't a known
+ * authority identity digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/C6B05DF332F74DB9A13498EE3BBC7AA2F69FCB45/"
+ "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "Failed to get download status for this identity/"
+ "signing key digest pair");
+ errmsg = NULL;
+
+ /*
+ * Case 11 - downloads/cert/fp/<fp>/<sk>, but <sk> isn't a known
+ * signing key digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
+ "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "Failed to get download status for this identity/"
+ "signing key digest pair");
+ errmsg = NULL;
+
+ /*
+ * Case 12 - downloads/cert/fp/<fp>/<sk>, but <sk> is on the list for
+ * a different authority identity digest.
+ */
+ getinfo_helper_downloads(&dummy,
+ "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
+ "9451B8F1B10952384EB58B5F230C0BB701626C9B",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "Failed to get download status for this identity/"
+ "signing key digest pair");
+ errmsg = NULL;
+
+ done:
+ clear_cert_mocks();
+ tor_free(answer);
+
+ return;
+}
+
+static void
+test_download_status_desc(void *arg)
+{
+ /* We just need one of these to pass, it doesn't matter what's in it */
+ control_connection_t dummy;
+ /* Get results out */
+ char *question = NULL;
+ char *answer = NULL;
+ const char *errmsg = NULL;
+
+ (void)arg;
+
+ setup_desc_mocks();
+
+ /*
+ * Check returning serialized dlstatuses and digest lists, and implicitly
+ * also test download_status_to_string() and digest_list_to_string().
+ */
+
+ /* Case 1 - list of router descriptor digests */
+ getinfo_helper_downloads(&dummy,
+ "downloads/desc/descs",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, descbr_expected_list);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 2 - get download status for router descriptor 1 */
+ memcpy(&descbr_digest_1_dl, &dls_sample_1,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/desc/%s", descbr_digest_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_1_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 3 - get download status for router descriptor 1 */
+ memcpy(&descbr_digest_2_dl, &dls_sample_2,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/desc/%s", descbr_digest_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_2_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Now check the error cases */
+
+ /* Case 1 - non-digest-length garbage after downloads/desc */
+ getinfo_helper_downloads(&dummy, "downloads/desc/blahdeblah",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "Unknown router descriptor download status query");
+ errmsg = NULL;
+
+ /* Case 2 - nonparseable digest-shaped thing */
+ getinfo_helper_downloads(
+ &dummy,
+ "downloads/desc/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
+ errmsg = NULL;
+
+ /* Case 3 - digest we have no descriptor for */
+ getinfo_helper_downloads(
+ &dummy,
+ "downloads/desc/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "No such descriptor digest found");
+ errmsg = NULL;
+
+ /* Case 4 - microdescs only */
+ disable_descbr = 1;
+ getinfo_helper_downloads(&dummy,
+ "downloads/desc/descs",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ,
+ "We don't seem to have a networkstatus-flavored consensus");
+ errmsg = NULL;
+ disable_descbr = 0;
+
+ done:
+ clear_desc_mocks();
+ tor_free(answer);
+
+ return;
+}
+
+static void
+test_download_status_bridge(void *arg)
+{
+ /* We just need one of these to pass, it doesn't matter what's in it */
+ control_connection_t dummy;
+ /* Get results out */
+ char *question = NULL;
+ char *answer = NULL;
+ const char *errmsg = NULL;
+
+ (void)arg;
+
+ setup_bridge_mocks();
+
+ /*
+ * Check returning serialized dlstatuses and digest lists, and implicitly
+ * also test download_status_to_string() and digest_list_to_string().
+ */
+
+ /* Case 1 - list of bridge identity digests */
+ getinfo_helper_downloads(&dummy,
+ "downloads/bridge/bridges",
+ &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, descbr_expected_list);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 2 - get download status for bridge descriptor 1 */
+ memcpy(&descbr_digest_1_dl, &dls_sample_3,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_1_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_3_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 3 - get download status for router descriptor 1 */
+ memcpy(&descbr_digest_2_dl, &dls_sample_4,
+ sizeof(download_status_t));
+ tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_2_str);
+ tt_assert(question != NULL);
+ getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
+ tt_assert(answer != NULL);
+ tt_assert(errmsg == NULL);
+ tt_str_op(answer, OP_EQ, dls_sample_4_str);
+ tor_free(question);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Now check the error cases */
+
+ /* Case 1 - non-digest-length garbage after downloads/bridge */
+ getinfo_helper_downloads(&dummy, "downloads/bridge/blahdeblah",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "Unknown bridge descriptor download status query");
+ errmsg = NULL;
+
+ /* Case 2 - nonparseable digest-shaped thing */
+ getinfo_helper_downloads(
+ &dummy,
+ "downloads/bridge/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
+ errmsg = NULL;
+
+ /* Case 3 - digest we have no descriptor for */
+ getinfo_helper_downloads(
+ &dummy,
+ "downloads/bridge/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "No such bridge identity digest found");
+ errmsg = NULL;
+
+ /* Case 4 - bridges disabled */
+ disable_descbr = 1;
+ getinfo_helper_downloads(&dummy,
+ "downloads/bridge/bridges",
+ &answer, &errmsg);
+ tt_assert(answer == NULL);
+ tt_assert(errmsg != NULL);
+ tt_str_op(errmsg, OP_EQ, "We don't seem to be using bridges");
+ errmsg = NULL;
+ disable_descbr = 0;
+
+ done:
+ clear_bridge_mocks();
+ tor_free(answer);
+
+ return;
+}
+
struct testcase_t controller_tests[] = {
{ "add_onion_helper_keyarg", test_add_onion_helper_keyarg, 0, NULL, NULL },
{ "rend_service_parse_port_config", test_rend_service_parse_port_config, 0,
NULL, NULL },
+ { "add_onion_helper_clientauth", test_add_onion_helper_clientauth, 0, NULL,
+ NULL },
+ { "download_status_consensus", test_download_status_consensus, 0, NULL,
+ NULL },
+ { "download_status_cert", test_download_status_cert, 0, NULL,
+ NULL },
+ { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
+ { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_controller_events.c b/src/test/test_controller_events.c
index 7b439d490d..11e1e3dc8f 100644
--- a/src/test/test_controller_events.c
+++ b/src/test/test_controller_events.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CONNECTION_PRIVATE
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index dbaec61ee9..64a46f7914 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -1,10 +1,11 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#define CRYPTO_CURVE25519_PRIVATE
+#define CRYPTO_PRIVATE
#include "or.h"
#include "test.h"
#include "aes.h"
@@ -15,16 +16,14 @@
#include "ed25519_vectors.inc"
#include <openssl/evp.h>
-
-extern const char AUTHORITY_SIGNKEY_3[];
-extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
-extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
+#include <openssl/rand.h>
/** Run unit tests for Diffie-Hellman functionality. */
static void
test_crypto_dh(void *arg)
{
crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
+ crypto_dh_t *dh1_dup = NULL;
crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
char p1[DH_BYTES];
char p2[DH_BYTES];
@@ -39,6 +38,9 @@ test_crypto_dh(void *arg)
memset(p1, 0, DH_BYTES);
memset(p2, 0, DH_BYTES);
tt_mem_op(p1,OP_EQ, p2, DH_BYTES);
+
+ tt_int_op(-1, OP_EQ, crypto_dh_get_public(dh1, p1, 6)); /* too short */
+
tt_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
tt_mem_op(p1,OP_NE, p2, DH_BYTES);
tt_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
@@ -52,15 +54,119 @@ test_crypto_dh(void *arg)
tt_int_op(s1len,OP_EQ, s2len);
tt_mem_op(s1,OP_EQ, s2, s1len);
+ /* test dh_dup; make sure it works the same. */
+ dh1_dup = crypto_dh_dup(dh1);
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1_dup, p2, DH_BYTES, s1, 50);
+ tt_mem_op(s1,OP_EQ, s2, s1len);
+
{
- /* XXXX Now fabricate some bad values and make sure they get caught,
- * Check 0, 1, N-1, >= N, etc.
- */
+ /* Now fabricate some bad values and make sure they get caught. */
+
+ /* 1 and 0 should both fail. */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, "\x01", 1, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, "\x00", 1, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ memset(p1, 0, DH_BYTES); /* 0 with padding. */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ p1[DH_BYTES-1] = 1; /* 1 with padding*/
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ /* 2 is okay, though weird. */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, "\x02", 1, s1, 50);
+ tt_int_op(50, OP_EQ, s1len);
+
+ const char P[] =
+ "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
+ "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
+ "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
+ "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
+ "49286651ECE65381FFFFFFFFFFFFFFFF";
+
+ /* p-1, p, and so on are not okay. */
+ base16_decode(p1, sizeof(p1), P, strlen(P));
+
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ p1[DH_BYTES-1] = 0xFE; /* p-1 */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ p1[DH_BYTES-1] = 0xFD; /* p-2 works fine */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(50, OP_EQ, s1len);
+
+ const char P_plus_one[] =
+ "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
+ "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
+ "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
+ "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
+ "49286651ECE653820000000000000000";
+
+ base16_decode(p1, sizeof(p1), P_plus_one, strlen(P_plus_one));
+
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ p1[DH_BYTES-1] = 0x01; /* p+2 */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ p1[DH_BYTES-1] = 0xff; /* p+256 */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+
+ memset(p1, 0xff, DH_BYTES), /* 2^1024-1 */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p1, DH_BYTES, s1, 50);
+ tt_int_op(-1, OP_EQ, s1len);
+ }
+
+ {
+ /* provoke an error in the openssl DH_compute_key function; make sure we
+ * survive. */
+ tt_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
+
+ crypto_dh_free(dh2);
+ dh2= crypto_dh_new(DH_TYPE_CIRCUIT); /* no private key set */
+ s1len = crypto_dh_compute_secret(LOG_WARN, dh2,
+ p1, DH_BYTES,
+ s1, 50);
+ tt_int_op(s1len, OP_EQ, -1);
}
done:
crypto_dh_free(dh1);
crypto_dh_free(dh2);
+ crypto_dh_free(dh1_dup);
+}
+
+static void
+test_crypto_openssl_version(void *arg)
+{
+ (void)arg;
+ const char *version = crypto_openssl_get_version_str();
+ const char *h_version = crypto_openssl_get_header_version_str();
+ tt_assert(version);
+ tt_assert(h_version);
+ tt_assert(!strcmpstart(version, h_version)); /* "-fips" suffix, etc */
+ tt_assert(!strstr(version, "OpenSSL"));
+ int a=-1,b=-1,c=-1;
+ if (!strcmpstart(version, "LibreSSL") || !strcmpstart(version, "BoringSSL"))
+ return;
+ int r = tor_sscanf(version, "%d.%d.%d", &a,&b,&c);
+ tt_int_op(r, OP_EQ, 3);
+ tt_int_op(a, OP_GE, 0);
+ tt_int_op(b, OP_GE, 0);
+ tt_int_op(c, OP_GE, 0);
+
+ done:
+ ;
}
/** Run unit tests for our random number generation function and its wrappers.
@@ -71,6 +177,7 @@ test_crypto_rng(void *arg)
int i, j, allok;
char data1[100], data2[100];
double d;
+ char *h=NULL;
/* Try out RNG. */
(void)arg;
@@ -102,9 +209,16 @@ test_crypto_rng(void *arg)
allok = 0;
tor_free(host);
}
+
+ /* Make sure crypto_random_hostname clips its inputs properly. */
+ h = crypto_random_hostname(20000, 9000, "www.", ".onion");
+ tt_assert(! strcmpstart(h,"www."));
+ tt_assert(! strcmpend(h,".onion"));
+ tt_int_op(63+4+6, OP_EQ, strlen(h));
+
tt_assert(allok);
done:
- ;
+ tor_free(h);
}
static void
@@ -123,23 +237,141 @@ test_crypto_rng_range(void *arg)
if (x == 8)
got_largest = 1;
}
-
/* These fail with probability 1/10^603. */
tt_assert(got_smallest);
tt_assert(got_largest);
+
+ got_smallest = got_largest = 0;
+ const uint64_t ten_billion = 10 * ((uint64_t)1000000000000);
+ for (i = 0; i < 1000; ++i) {
+ uint64_t x = crypto_rand_uint64_range(ten_billion, ten_billion+10);
+ tt_u64_op(x, OP_GE, ten_billion);
+ tt_u64_op(x, OP_LT, ten_billion+10);
+ if (x == ten_billion)
+ got_smallest = 1;
+ if (x == ten_billion+9)
+ got_largest = 1;
+ }
+
+ tt_assert(got_smallest);
+ tt_assert(got_largest);
+
+ const time_t now = time(NULL);
+ for (i = 0; i < 2000; ++i) {
+ time_t x = crypto_rand_time_range(now, now+60);
+ tt_i64_op(x, OP_GE, now);
+ tt_i64_op(x, OP_LT, now+60);
+ if (x == now)
+ got_smallest = 1;
+ if (x == now+59)
+ got_largest = 1;
+ }
+
+ tt_assert(got_smallest);
+ tt_assert(got_largest);
+ done:
+ ;
+}
+
+static void
+test_crypto_rng_strongest(void *arg)
+{
+ const char *how = arg;
+ int broken = 0;
+
+ if (how == NULL) {
+ ;
+ } else if (!strcmp(how, "nosyscall")) {
+ break_strongest_rng_syscall = 1;
+ } else if (!strcmp(how, "nofallback")) {
+ break_strongest_rng_fallback = 1;
+ } else if (!strcmp(how, "broken")) {
+ broken = break_strongest_rng_syscall = break_strongest_rng_fallback = 1;
+ }
+
+#define N 128
+ uint8_t combine_and[N];
+ uint8_t combine_or[N];
+ int i, j;
+
+ memset(combine_and, 0xff, N);
+ memset(combine_or, 0, N);
+
+ for (i = 0; i < 100; ++i) { /* 2^-100 chances just don't happen. */
+ uint8_t output[N];
+ memset(output, 0, N);
+ if (how == NULL) {
+ /* this one can't fail. */
+ crypto_strongest_rand(output, sizeof(output));
+ } else {
+ int r = crypto_strongest_rand_raw(output, sizeof(output));
+ if (r == -1) {
+ if (broken) {
+ goto done; /* we're fine. */
+ }
+ /* This function is allowed to break, but only if it always breaks. */
+ tt_int_op(i, OP_EQ, 0);
+ tt_skip();
+ } else {
+ tt_assert(! broken);
+ }
+ }
+ for (j = 0; j < N; ++j) {
+ combine_and[j] &= output[j];
+ combine_or[j] |= output[j];
+ }
+ }
+
+ for (j = 0; j < N; ++j) {
+ tt_int_op(combine_and[j], OP_EQ, 0);
+ tt_int_op(combine_or[j], OP_EQ, 0xff);
+ }
+ done:
+ ;
+#undef N
+}
+
+/* Test for rectifying openssl RAND engine. */
+static void
+test_crypto_rng_engine(void *arg)
+{
+ (void)arg;
+ RAND_METHOD dummy_method;
+ memset(&dummy_method, 0, sizeof(dummy_method));
+
+ /* We should be a no-op if we're already on RAND_OpenSSL */
+ tt_int_op(0, ==, crypto_force_rand_ssleay());
+ tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
+
+ /* We should correct the method if it's a dummy. */
+ RAND_set_rand_method(&dummy_method);
+#ifdef LIBRESSL_VERSION_NUMBER
+ /* On libressl, you can't override the RNG. */
+ tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
+ tt_int_op(0, ==, crypto_force_rand_ssleay());
+#else
+ tt_assert(RAND_get_rand_method() == &dummy_method);
+ tt_int_op(1, ==, crypto_force_rand_ssleay());
+#endif
+ tt_assert(RAND_get_rand_method() == RAND_OpenSSL());
+
+ /* Make sure we aren't calling dummy_method */
+ crypto_rand((void *) &dummy_method, sizeof(dummy_method));
+ crypto_rand((void *) &dummy_method, sizeof(dummy_method));
+
done:
;
}
-/** Run unit tests for our AES functionality */
+/** Run unit tests for our AES128 functionality */
static void
-test_crypto_aes(void *arg)
+test_crypto_aes128(void *arg)
{
char *data1 = NULL, *data2 = NULL, *data3 = NULL;
crypto_cipher_t *env1 = NULL, *env2 = NULL;
int i, j;
char *mem_op_hex_tmp=NULL;
-
+ char key[CIPHER_KEY_LEN];
int use_evp = !strcmp(arg,"evp");
evaluate_evp_for_aes(use_evp);
evaluate_ctr_for_aes();
@@ -155,9 +387,10 @@ test_crypto_aes(void *arg)
memset(data2, 0, 1024);
memset(data3, 0, 1024);
- env1 = crypto_cipher_new(NULL);
+ crypto_rand(key, sizeof(key));
+ env1 = crypto_cipher_new(key);
tt_ptr_op(env1, OP_NE, NULL);
- env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
+ env2 = crypto_cipher_new(key);
tt_ptr_op(env2, OP_NE, NULL);
/* Try encrypting 512 chars. */
@@ -188,7 +421,7 @@ test_crypto_aes(void *arg)
env2 = NULL;
memset(data3, 0, 1024);
- env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
+ env2 = crypto_cipher_new(key);
tt_ptr_op(env2, OP_NE, NULL);
for (j = 0; j < 1024-16; j += 17) {
crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
@@ -278,16 +511,83 @@ test_crypto_aes(void *arg)
tor_free(data3);
}
+static void
+test_crypto_aes_ctr_testvec(void *arg)
+{
+ const char *bitstr = arg;
+ char *mem_op_hex_tmp=NULL;
+ crypto_cipher_t *c=NULL;
+
+ /* from NIST SP800-38a, section F.5 */
+ const char ctr16[] = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
+ const char plaintext16[] =
+ "6bc1bee22e409f96e93d7e117393172a"
+ "ae2d8a571e03ac9c9eb76fac45af8e51"
+ "30c81c46a35ce411e5fbc1191a0a52ef"
+ "f69f2445df4f9b17ad2b417be66c3710";
+ const char *ciphertext16;
+ const char *key16;
+ int bits;
+
+ if (!strcmp(bitstr, "128")) {
+ ciphertext16 = /* section F.5.1 */
+ "874d6191b620e3261bef6864990db6ce"
+ "9806f66b7970fdff8617187bb9fffdff"
+ "5ae4df3edbd5d35e5b4f09020db03eab"
+ "1e031dda2fbe03d1792170a0f3009cee";
+ key16 = "2b7e151628aed2a6abf7158809cf4f3c";
+ bits = 128;
+ } else if (!strcmp(bitstr, "192")) {
+ ciphertext16 = /* section F.5.3 */
+ "1abc932417521ca24f2b0459fe7e6e0b"
+ "090339ec0aa6faefd5ccc2c6f4ce8e94"
+ "1e36b26bd1ebc670d1bd1d665620abf7"
+ "4f78a7f6d29809585a97daec58c6b050";
+ key16 = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b";
+ bits = 192;
+ } else if (!strcmp(bitstr, "256")) {
+ ciphertext16 = /* section F.5.5 */
+ "601ec313775789a5b7a7f504bbf3d228"
+ "f443e3ca4d62b59aca84e990cacaf5c5"
+ "2b0930daa23de94ce87017ba2d84988d"
+ "dfc9c58db67aada613c2dd08457941a6";
+ key16 =
+ "603deb1015ca71be2b73aef0857d7781"
+ "1f352c073b6108d72d9810a30914dff4";
+ bits = 256;
+ } else {
+ tt_abort_msg("AES doesn't support this number of bits.");
+ }
+
+ char key[32];
+ char iv[16];
+ char plaintext[16*4];
+ memset(key, 0xf9, sizeof(key)); /* poison extra bytes */
+ base16_decode(key, sizeof(key), key16, strlen(key16));
+ base16_decode(iv, sizeof(iv), ctr16, strlen(ctr16));
+ base16_decode(plaintext, sizeof(plaintext),
+ plaintext16, strlen(plaintext16));
+
+ c = crypto_cipher_new_with_iv_and_bits((uint8_t*)key, (uint8_t*)iv, bits);
+ crypto_cipher_crypt_inplace(c, plaintext, sizeof(plaintext));
+ test_memeq_hex(plaintext, ciphertext16);
+
+ done:
+ tor_free(mem_op_hex_tmp);
+ crypto_cipher_free(c);
+}
+
/** Run unit tests for our SHA-1 functionality */
static void
test_crypto_sha(void *arg)
{
crypto_digest_t *d1 = NULL, *d2 = NULL;
int i;
- char key[160];
- char digest[32];
- char data[50];
- char d_out1[DIGEST_LEN], d_out2[DIGEST256_LEN];
+#define RFC_4231_MAX_KEY_SIZE 131
+ char key[RFC_4231_MAX_KEY_SIZE];
+ char digest[DIGEST256_LEN];
+ char data[DIGEST512_LEN];
+ char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
char *mem_op_hex_tmp=NULL;
/* Test SHA-1 with a test vector from the specification. */
@@ -302,6 +602,13 @@ test_crypto_sha(void *arg)
"96177A9CB410FF61F20015AD");
tt_int_op(i, OP_EQ, 0);
+ /* Test SHA-512 with a test vector from the specification. */
+ i = crypto_digest512(data, "abc", 3, DIGEST_SHA512);
+ test_memeq_hex(data, "ddaf35a193617abacc417349ae20413112e6fa4e89a97"
+ "ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3"
+ "feebbd454d4423643ce80e2a9ac94fa54ca49f");
+ tt_int_op(i, OP_EQ, 0);
+
/* Test HMAC-SHA256 with test cases from wikipedia and RFC 4231 */
/* Case empty (wikipedia) */
@@ -378,15 +685,15 @@ test_crypto_sha(void *arg)
d2 = crypto_digest_dup(d1);
tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6);
- crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
+ crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdefghijkl", 12);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3);
- crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
+ crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdefmno", 9);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
- crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
+ crypto_digest_get_digest(d1, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdef", 6);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
crypto_digest_free(d1);
@@ -399,23 +706,432 @@ test_crypto_sha(void *arg)
d2 = crypto_digest_dup(d1);
tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6);
- crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
+ crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
- tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3);
- crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
+ crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
- tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
- crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
+ crypto_digest_get_digest(d1, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
- tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
+ crypto_digest_free(d1);
+ crypto_digest_free(d2);
+
+ /* Incremental digest code with sha512 */
+ d1 = crypto_digest512_new(DIGEST_SHA512);
+ tt_assert(d1);
+ crypto_digest_add_bytes(d1, "abcdef", 6);
+ d2 = crypto_digest_dup(d1);
+ tt_assert(d2);
+ crypto_digest_add_bytes(d2, "ghijkl", 6);
+ crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_assign(d2, d1);
+ crypto_digest_add_bytes(d2, "mno", 3);
+ crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+
+ done:
+ if (d1)
+ crypto_digest_free(d1);
+ if (d2)
+ crypto_digest_free(d2);
+ tor_free(mem_op_hex_tmp);
+}
+
+static void
+test_crypto_sha3(void *arg)
+{
+ crypto_digest_t *d1 = NULL, *d2 = NULL;
+ int i;
+ char data[DIGEST512_LEN];
+ char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
+ char *mem_op_hex_tmp=NULL;
+ char *large = NULL;
+
+ (void)arg;
+
+ /* Test SHA3-[256,512] with a test vectors from the Keccak Code Package.
+ *
+ * NB: The code package's test vectors have length expressed in bits.
+ */
+
+ /* Len = 8, Msg = CC */
+ const uint8_t keccak_kat_msg8[] = { 0xcc };
+ i = crypto_digest256(data, (const char*)keccak_kat_msg8, 1, DIGEST_SHA3_256);
+ test_memeq_hex(data, "677035391CD3701293D385F037BA3279"
+ "6252BB7CE180B00B582DD9B20AAAD7F0");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, (const char*)keccak_kat_msg8, 1, DIGEST_SHA3_512);
+ test_memeq_hex(data, "3939FCC8B57B63612542DA31A834E5DC"
+ "C36E2EE0F652AC72E02624FA2E5ADEEC"
+ "C7DD6BB3580224B4D6138706FC6E8059"
+ "7B528051230B00621CC2B22999EAA205");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 24, Msg = 1F877C */
+ const uint8_t keccak_kat_msg24[] = { 0x1f, 0x87, 0x7c };
+ i = crypto_digest256(data, (const char*)keccak_kat_msg24, 3,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "BC22345E4BD3F792A341CF18AC0789F1"
+ "C9C966712A501B19D1B6632CCD408EC5");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, (const char*)keccak_kat_msg24, 3,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "CB20DCF54955F8091111688BECCEF48C"
+ "1A2F0D0608C3A575163751F002DB30F4"
+ "0F2F671834B22D208591CFAF1F5ECFE4"
+ "3C49863A53B3225BDFD7C6591BA7658B");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1080, Msg = B771D5CEF... ...C35AC81B5 (SHA3-256 rate - 1) */
+ const uint8_t keccak_kat_msg1080[] = {
+ 0xB7, 0x71, 0xD5, 0xCE, 0xF5, 0xD1, 0xA4, 0x1A, 0x93, 0xD1,
+ 0x56, 0x43, 0xD7, 0x18, 0x1D, 0x2A, 0x2E, 0xF0, 0xA8, 0xE8,
+ 0x4D, 0x91, 0x81, 0x2F, 0x20, 0xED, 0x21, 0xF1, 0x47, 0xBE,
+ 0xF7, 0x32, 0xBF, 0x3A, 0x60, 0xEF, 0x40, 0x67, 0xC3, 0x73,
+ 0x4B, 0x85, 0xBC, 0x8C, 0xD4, 0x71, 0x78, 0x0F, 0x10, 0xDC,
+ 0x9E, 0x82, 0x91, 0xB5, 0x83, 0x39, 0xA6, 0x77, 0xB9, 0x60,
+ 0x21, 0x8F, 0x71, 0xE7, 0x93, 0xF2, 0x79, 0x7A, 0xEA, 0x34,
+ 0x94, 0x06, 0x51, 0x28, 0x29, 0x06, 0x5D, 0x37, 0xBB, 0x55,
+ 0xEA, 0x79, 0x6F, 0xA4, 0xF5, 0x6F, 0xD8, 0x89, 0x6B, 0x49,
+ 0xB2, 0xCD, 0x19, 0xB4, 0x32, 0x15, 0xAD, 0x96, 0x7C, 0x71,
+ 0x2B, 0x24, 0xE5, 0x03, 0x2D, 0x06, 0x52, 0x32, 0xE0, 0x2C,
+ 0x12, 0x74, 0x09, 0xD2, 0xED, 0x41, 0x46, 0xB9, 0xD7, 0x5D,
+ 0x76, 0x3D, 0x52, 0xDB, 0x98, 0xD9, 0x49, 0xD3, 0xB0, 0xFE,
+ 0xD6, 0xA8, 0x05, 0x2F, 0xBB,
+ };
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1080, 135,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "A19EEE92BB2097B64E823D597798AA18"
+ "BE9B7C736B8059ABFD6779AC35AC81B5");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1080, 135,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "7575A1FB4FC9A8F9C0466BD5FCA496D1"
+ "CB78696773A212A5F62D02D14E3259D1"
+ "92A87EBA4407DD83893527331407B6DA"
+ "DAAD920DBC46489B677493CE5F20B595");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1088, Msg = B32D95B0... ...8E380C04 (SHA3-256 rate) */
+ const uint8_t keccak_kat_msg1088[] = {
+ 0xB3, 0x2D, 0x95, 0xB0, 0xB9, 0xAA, 0xD2, 0xA8, 0x81, 0x6D,
+ 0xE6, 0xD0, 0x6D, 0x1F, 0x86, 0x00, 0x85, 0x05, 0xBD, 0x8C,
+ 0x14, 0x12, 0x4F, 0x6E, 0x9A, 0x16, 0x3B, 0x5A, 0x2A, 0xDE,
+ 0x55, 0xF8, 0x35, 0xD0, 0xEC, 0x38, 0x80, 0xEF, 0x50, 0x70,
+ 0x0D, 0x3B, 0x25, 0xE4, 0x2C, 0xC0, 0xAF, 0x05, 0x0C, 0xCD,
+ 0x1B, 0xE5, 0xE5, 0x55, 0xB2, 0x30, 0x87, 0xE0, 0x4D, 0x7B,
+ 0xF9, 0x81, 0x36, 0x22, 0x78, 0x0C, 0x73, 0x13, 0xA1, 0x95,
+ 0x4F, 0x87, 0x40, 0xB6, 0xEE, 0x2D, 0x3F, 0x71, 0xF7, 0x68,
+ 0xDD, 0x41, 0x7F, 0x52, 0x04, 0x82, 0xBD, 0x3A, 0x08, 0xD4,
+ 0xF2, 0x22, 0xB4, 0xEE, 0x9D, 0xBD, 0x01, 0x54, 0x47, 0xB3,
+ 0x35, 0x07, 0xDD, 0x50, 0xF3, 0xAB, 0x42, 0x47, 0xC5, 0xDE,
+ 0x9A, 0x8A, 0xBD, 0x62, 0xA8, 0xDE, 0xCE, 0xA0, 0x1E, 0x3B,
+ 0x87, 0xC8, 0xB9, 0x27, 0xF5, 0xB0, 0x8B, 0xEB, 0x37, 0x67,
+ 0x4C, 0x6F, 0x8E, 0x38, 0x0C, 0x04,
+ };
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1088, 136,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "DF673F4105379FF6B755EEAB20CEB0DC"
+ "77B5286364FE16C59CC8A907AFF07732");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1088, 136,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "2E293765022D48996CE8EFF0BE54E87E"
+ "FB94A14C72DE5ACD10D0EB5ECE029CAD"
+ "FA3BA17A40B2FFA2163991B17786E51C"
+ "ABA79E5E0FFD34CF085E2A098BE8BACB");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1096, Msg = 04410E310... ...601016A0D (SHA3-256 rate + 1) */
+ const uint8_t keccak_kat_msg1096[] = {
+ 0x04, 0x41, 0x0E, 0x31, 0x08, 0x2A, 0x47, 0x58, 0x4B, 0x40,
+ 0x6F, 0x05, 0x13, 0x98, 0xA6, 0xAB, 0xE7, 0x4E, 0x4D, 0xA5,
+ 0x9B, 0xB6, 0xF8, 0x5E, 0x6B, 0x49, 0xE8, 0xA1, 0xF7, 0xF2,
+ 0xCA, 0x00, 0xDF, 0xBA, 0x54, 0x62, 0xC2, 0xCD, 0x2B, 0xFD,
+ 0xE8, 0xB6, 0x4F, 0xB2, 0x1D, 0x70, 0xC0, 0x83, 0xF1, 0x13,
+ 0x18, 0xB5, 0x6A, 0x52, 0xD0, 0x3B, 0x81, 0xCA, 0xC5, 0xEE,
+ 0xC2, 0x9E, 0xB3, 0x1B, 0xD0, 0x07, 0x8B, 0x61, 0x56, 0x78,
+ 0x6D, 0xA3, 0xD6, 0xD8, 0xC3, 0x30, 0x98, 0xC5, 0xC4, 0x7B,
+ 0xB6, 0x7A, 0xC6, 0x4D, 0xB1, 0x41, 0x65, 0xAF, 0x65, 0xB4,
+ 0x45, 0x44, 0xD8, 0x06, 0xDD, 0xE5, 0xF4, 0x87, 0xD5, 0x37,
+ 0x3C, 0x7F, 0x97, 0x92, 0xC2, 0x99, 0xE9, 0x68, 0x6B, 0x7E,
+ 0x58, 0x21, 0xE7, 0xC8, 0xE2, 0x45, 0x83, 0x15, 0xB9, 0x96,
+ 0xB5, 0x67, 0x7D, 0x92, 0x6D, 0xAC, 0x57, 0xB3, 0xF2, 0x2D,
+ 0xA8, 0x73, 0xC6, 0x01, 0x01, 0x6A, 0x0D,
+ };
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1096, 137,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "D52432CF3B6B4B949AA848E058DCD62D"
+ "735E0177279222E7AC0AF8504762FAA0");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1096, 137,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "BE8E14B6757FFE53C9B75F6DDE9A7B6C"
+ "40474041DE83D4A60645A826D7AF1ABE"
+ "1EEFCB7B74B62CA6A514E5F2697D585B"
+ "FECECE12931BBE1D4ED7EBF7B0BE660E");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1144, Msg = EA40E83C... ...66DFAFEC (SHA3-512 rate *2 - 1) */
+ const uint8_t keccak_kat_msg1144[] = {
+ 0xEA, 0x40, 0xE8, 0x3C, 0xB1, 0x8B, 0x3A, 0x24, 0x2C, 0x1E,
+ 0xCC, 0x6C, 0xCD, 0x0B, 0x78, 0x53, 0xA4, 0x39, 0xDA, 0xB2,
+ 0xC5, 0x69, 0xCF, 0xC6, 0xDC, 0x38, 0xA1, 0x9F, 0x5C, 0x90,
+ 0xAC, 0xBF, 0x76, 0xAE, 0xF9, 0xEA, 0x37, 0x42, 0xFF, 0x3B,
+ 0x54, 0xEF, 0x7D, 0x36, 0xEB, 0x7C, 0xE4, 0xFF, 0x1C, 0x9A,
+ 0xB3, 0xBC, 0x11, 0x9C, 0xFF, 0x6B, 0xE9, 0x3C, 0x03, 0xE2,
+ 0x08, 0x78, 0x33, 0x35, 0xC0, 0xAB, 0x81, 0x37, 0xBE, 0x5B,
+ 0x10, 0xCD, 0xC6, 0x6F, 0xF3, 0xF8, 0x9A, 0x1B, 0xDD, 0xC6,
+ 0xA1, 0xEE, 0xD7, 0x4F, 0x50, 0x4C, 0xBE, 0x72, 0x90, 0x69,
+ 0x0B, 0xB2, 0x95, 0xA8, 0x72, 0xB9, 0xE3, 0xFE, 0x2C, 0xEE,
+ 0x9E, 0x6C, 0x67, 0xC4, 0x1D, 0xB8, 0xEF, 0xD7, 0xD8, 0x63,
+ 0xCF, 0x10, 0xF8, 0x40, 0xFE, 0x61, 0x8E, 0x79, 0x36, 0xDA,
+ 0x3D, 0xCA, 0x5C, 0xA6, 0xDF, 0x93, 0x3F, 0x24, 0xF6, 0x95,
+ 0x4B, 0xA0, 0x80, 0x1A, 0x12, 0x94, 0xCD, 0x8D, 0x7E, 0x66,
+ 0xDF, 0xAF, 0xEC,
+ };
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1144, 143,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "3A8E938C45F3F177991296B24565D9A6"
+ "605516615D96A062C8BE53A0D6C5A648"
+ "7BE35D2A8F3CF6620D0C2DBA2C560D68"
+ "295F284BE7F82F3B92919033C9CE5D80");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1144, 143,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "E58A947E98D6DD7E932D2FE02D9992E6"
+ "118C0C2C606BDCDA06E7943D2C95E0E5");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1152, Msg = 157D5B7E... ...79EE00C63 (SHA3-512 rate * 2) */
+ const uint8_t keccak_kat_msg1152[] = {
+ 0x15, 0x7D, 0x5B, 0x7E, 0x45, 0x07, 0xF6, 0x6D, 0x9A, 0x26,
+ 0x74, 0x76, 0xD3, 0x38, 0x31, 0xE7, 0xBB, 0x76, 0x8D, 0x4D,
+ 0x04, 0xCC, 0x34, 0x38, 0xDA, 0x12, 0xF9, 0x01, 0x02, 0x63,
+ 0xEA, 0x5F, 0xCA, 0xFB, 0xDE, 0x25, 0x79, 0xDB, 0x2F, 0x6B,
+ 0x58, 0xF9, 0x11, 0xD5, 0x93, 0xD5, 0xF7, 0x9F, 0xB0, 0x5F,
+ 0xE3, 0x59, 0x6E, 0x3F, 0xA8, 0x0F, 0xF2, 0xF7, 0x61, 0xD1,
+ 0xB0, 0xE5, 0x70, 0x80, 0x05, 0x5C, 0x11, 0x8C, 0x53, 0xE5,
+ 0x3C, 0xDB, 0x63, 0x05, 0x52, 0x61, 0xD7, 0xC9, 0xB2, 0xB3,
+ 0x9B, 0xD9, 0x0A, 0xCC, 0x32, 0x52, 0x0C, 0xBB, 0xDB, 0xDA,
+ 0x2C, 0x4F, 0xD8, 0x85, 0x6D, 0xBC, 0xEE, 0x17, 0x31, 0x32,
+ 0xA2, 0x67, 0x91, 0x98, 0xDA, 0xF8, 0x30, 0x07, 0xA9, 0xB5,
+ 0xC5, 0x15, 0x11, 0xAE, 0x49, 0x76, 0x6C, 0x79, 0x2A, 0x29,
+ 0x52, 0x03, 0x88, 0x44, 0x4E, 0xBE, 0xFE, 0x28, 0x25, 0x6F,
+ 0xB3, 0x3D, 0x42, 0x60, 0x43, 0x9C, 0xBA, 0x73, 0xA9, 0x47,
+ 0x9E, 0xE0, 0x0C, 0x63,
+ };
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1152, 144,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "FE45289874879720CE2A844AE34BB735"
+ "22775DCB6019DCD22B8885994672A088"
+ "9C69E8115C641DC8B83E39F7311815A1"
+ "64DC46E0BA2FCA344D86D4BC2EF2532C");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1152, 144,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "A936FB9AF87FB67857B3EAD5C76226AD"
+ "84DA47678F3C2FFE5A39FDB5F7E63FFB");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Len = 1160, Msg = 836B34B5... ...11044C53 (SHA3-512 rate * 2 + 1) */
+ const uint8_t keccak_kat_msg1160[] = {
+ 0x83, 0x6B, 0x34, 0xB5, 0x15, 0x47, 0x6F, 0x61, 0x3F, 0xE4,
+ 0x47, 0xA4, 0xE0, 0xC3, 0xF3, 0xB8, 0xF2, 0x09, 0x10, 0xAC,
+ 0x89, 0xA3, 0x97, 0x70, 0x55, 0xC9, 0x60, 0xD2, 0xD5, 0xD2,
+ 0xB7, 0x2B, 0xD8, 0xAC, 0xC7, 0x15, 0xA9, 0x03, 0x53, 0x21,
+ 0xB8, 0x67, 0x03, 0xA4, 0x11, 0xDD, 0xE0, 0x46, 0x6D, 0x58,
+ 0xA5, 0x97, 0x69, 0x67, 0x2A, 0xA6, 0x0A, 0xD5, 0x87, 0xB8,
+ 0x48, 0x1D, 0xE4, 0xBB, 0xA5, 0x52, 0xA1, 0x64, 0x57, 0x79,
+ 0x78, 0x95, 0x01, 0xEC, 0x53, 0xD5, 0x40, 0xB9, 0x04, 0x82,
+ 0x1F, 0x32, 0xB0, 0xBD, 0x18, 0x55, 0xB0, 0x4E, 0x48, 0x48,
+ 0xF9, 0xF8, 0xCF, 0xE9, 0xEB, 0xD8, 0x91, 0x1B, 0xE9, 0x57,
+ 0x81, 0xA7, 0x59, 0xD7, 0xAD, 0x97, 0x24, 0xA7, 0x10, 0x2D,
+ 0xBE, 0x57, 0x67, 0x76, 0xB7, 0xC6, 0x32, 0xBC, 0x39, 0xB9,
+ 0xB5, 0xE1, 0x90, 0x57, 0xE2, 0x26, 0x55, 0x2A, 0x59, 0x94,
+ 0xC1, 0xDB, 0xB3, 0xB5, 0xC7, 0x87, 0x1A, 0x11, 0xF5, 0x53,
+ 0x70, 0x11, 0x04, 0x4C, 0x53,
+ };
+ i = crypto_digest512(data, (const char*)keccak_kat_msg1160, 145,
+ DIGEST_SHA3_512);
+ test_memeq_hex(data, "AFF61C6E11B98E55AC213B1A0BC7DE04"
+ "05221AC5EFB1229842E4614F4A029C9B"
+ "D14A0ED7FD99AF3681429F3F309FDB53"
+ "166AA9A3CD9F1F1223D04B4A9015E94A");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest256(data, (const char*)keccak_kat_msg1160, 145,
+ DIGEST_SHA3_256);
+ test_memeq_hex(data, "3A654B88F88086C2751EDAE6D3924814"
+ "3CF6235C6B0B7969342C45A35194B67E");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* SHA3-[256,512] Empty case (wikipedia) */
+ i = crypto_digest256(data, "", 0, DIGEST_SHA3_256);
+ test_memeq_hex(data, "a7ffc6f8bf1ed76651c14756a061d662"
+ "f580ff4de43b49fa82d80a4b80f8434a");
+ tt_int_op(i, OP_EQ, 0);
+ i = crypto_digest512(data, "", 0, DIGEST_SHA3_512);
+ test_memeq_hex(data, "a69f73cca23a9ac5c8b567dc185a756e"
+ "97c982164fe25859e0d1dcc1475c80a6"
+ "15b2123af1f5f94c11e3e9402c3ac558"
+ "f500199d95b6d3e301758586281dcd26");
+ tt_int_op(i, OP_EQ, 0);
+
+ /* Incremental digest code with SHA3-256 */
+ d1 = crypto_digest256_new(DIGEST_SHA3_256);
+ tt_assert(d1);
+ crypto_digest_add_bytes(d1, "abcdef", 6);
+ d2 = crypto_digest_dup(d1);
+ tt_assert(d2);
+ crypto_digest_add_bytes(d2, "ghijkl", 6);
+ crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
+ crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA3_256);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
+ crypto_digest_assign(d2, d1);
+ crypto_digest_add_bytes(d2, "mno", 3);
+ crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
+ crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA3_256);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
+ crypto_digest_get_digest(d1, d_out1, DIGEST256_LEN);
+ crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA3_256);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
+ crypto_digest_free(d1);
+ crypto_digest_free(d2);
+
+ /* Incremental digest code with SHA3-512 */
+ d1 = crypto_digest512_new(DIGEST_SHA3_512);
+ tt_assert(d1);
+ crypto_digest_add_bytes(d1, "abcdef", 6);
+ d2 = crypto_digest_dup(d1);
+ tt_assert(d2);
+ crypto_digest_add_bytes(d2, "ghijkl", 6);
+ crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA3_512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_assign(d2, d1);
+ crypto_digest_add_bytes(d2, "mno", 3);
+ crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA3_512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
+ crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA3_512);
+ tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_free(d1);
+
+ /* Attempt to exercise the incremental hashing code by creating a randomized
+ * 30 KiB buffer, and hashing rand[1, 5 * Rate] bytes at a time. SHA3-512
+ * is used because it has a lowest rate of the family (the code is common,
+ * but the slower rate exercises more of it).
+ */
+ const size_t bufsz = 30 * 1024;
+ size_t j = 0;
+ large = tor_malloc(bufsz);
+ crypto_rand(large, bufsz);
+ d1 = crypto_digest512_new(DIGEST_SHA3_512); /* Running digest. */
+ while (j < bufsz) {
+ /* Pick how much data to add to the running digest. */
+ size_t incr = (size_t)crypto_rand_int_range(1, 72 * 5);
+ incr = MIN(bufsz - j, incr);
+
+ /* Add the data, and calculate the hash. */
+ crypto_digest_add_bytes(d1, large + j, incr);
+ crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
+
+ /* One-shot hash the buffer up to the data that was just added,
+ * and ensure that the values match up.
+ *
+ * XXX/yawning: If this actually fails, it'll be rather difficult to
+ * reproduce. Improvements welcome.
+ */
+ i = crypto_digest512(d_out2, large, j + incr, DIGEST_SHA3_512);
+ tt_int_op(i, OP_EQ, 0);
+ tt_mem_op(d_out1, OP_EQ, d_out2, DIGEST512_LEN);
+
+ j += incr;
+ }
done:
if (d1)
crypto_digest_free(d1);
if (d2)
crypto_digest_free(d2);
+ tor_free(large);
+ tor_free(mem_op_hex_tmp);
+}
+
+/** Run unit tests for our XOF. */
+static void
+test_crypto_sha3_xof(void *arg)
+{
+ uint8_t msg[255];
+ uint8_t out[512];
+ crypto_xof_t *xof;
+ char *mem_op_hex_tmp=NULL;
+
+ (void)arg;
+
+ /* SHAKE256 test vector (Len = 2040) from the Keccak Code Package. */
+ base16_decode((char *)msg, 255,
+ "3A3A819C48EFDE2AD914FBF00E18AB6BC4F14513AB27D0C178A188B61431"
+ "E7F5623CB66B23346775D386B50E982C493ADBBFC54B9A3CD383382336A1"
+ "A0B2150A15358F336D03AE18F666C7573D55C4FD181C29E6CCFDE63EA35F"
+ "0ADF5885CFC0A3D84A2B2E4DD24496DB789E663170CEF74798AA1BBCD457"
+ "4EA0BBA40489D764B2F83AADC66B148B4A0CD95246C127D5871C4F114186"
+ "90A5DDF01246A0C80A43C70088B6183639DCFDA4125BD113A8F49EE23ED3"
+ "06FAAC576C3FB0C1E256671D817FC2534A52F5B439F72E424DE376F4C565"
+ "CCA82307DD9EF76DA5B7C4EB7E085172E328807C02D011FFBF33785378D7"
+ "9DC266F6A5BE6BB0E4A92ECEEBAEB1", 510);
+ const char *squeezed_hex =
+ "8A5199B4A7E133E264A86202720655894D48CFF344A928CF8347F48379CE"
+ "F347DFC5BCFFAB99B27B1F89AA2735E23D30088FFA03B9EDB02B9635470A"
+ "B9F1038985D55F9CA774572DD006470EA65145469609F9FA0831BF1FFD84"
+ "2DC24ACADE27BD9816E3B5BF2876CB112232A0EB4475F1DFF9F5C713D9FF"
+ "D4CCB89AE5607FE35731DF06317949EEF646E9591CF3BE53ADD6B7DD2B60"
+ "96E2B3FB06E662EC8B2D77422DAAD9463CD155204ACDBD38E319613F39F9"
+ "9B6DFB35CA9365160066DB19835888C2241FF9A731A4ACBB5663727AAC34"
+ "A401247FBAA7499E7D5EE5B69D31025E63D04C35C798BCA1262D5673A9CF"
+ "0930B5AD89BD485599DC184528DA4790F088EBD170B635D9581632D2FF90"
+ "DB79665CED430089AF13C9F21F6D443A818064F17AEC9E9C5457001FA8DC"
+ "6AFBADBE3138F388D89D0E6F22F66671255B210754ED63D81DCE75CE8F18"
+ "9B534E6D6B3539AA51E837C42DF9DF59C71E6171CD4902FE1BDC73FB1775"
+ "B5C754A1ED4EA7F3105FC543EE0418DAD256F3F6118EA77114A16C15355B"
+ "42877A1DB2A7DF0E155AE1D8670ABCEC3450F4E2EEC9838F895423EF63D2"
+ "61138BAAF5D9F104CB5A957AEA06C0B9B8C78B0D441796DC0350DDEABB78"
+ "A33B6F1F9E68EDE3D1805C7B7E2CFD54E0FAD62F0D8CA67A775DC4546AF9"
+ "096F2EDB221DB42843D65327861282DC946A0BA01A11863AB2D1DFD16E39"
+ "73D4";
+
+ /* Test oneshot absorb/squeeze. */
+ xof = crypto_xof_new();
+ tt_assert(xof);
+ crypto_xof_add_bytes(xof, msg, sizeof(msg));
+ crypto_xof_squeeze_bytes(xof, out, sizeof(out));
+ test_memeq_hex(out, squeezed_hex);
+ crypto_xof_free(xof);
+ memset(out, 0, sizeof(out));
+
+ /* Test incremental absorb/squeeze. */
+ xof = crypto_xof_new();
+ tt_assert(xof);
+ for (size_t i = 0; i < sizeof(msg); i++)
+ crypto_xof_add_bytes(xof, msg + i, 1);
+ for (size_t i = 0; i < sizeof(out); i++)
+ crypto_xof_squeeze_bytes(xof, out + i, 1);
+ test_memeq_hex(out, squeezed_hex);
+
+ done:
+ if (xof)
+ crypto_xof_free(xof);
tor_free(mem_op_hex_tmp);
}
@@ -633,13 +1349,36 @@ test_crypto_pk_base64(void *arg)
tor_free(encoded);
}
+#ifdef HAVE_TRUNCATE
+#define do_truncate truncate
+#else
+static int
+do_truncate(const char *fname, size_t len)
+{
+ struct stat st;
+ char *bytes;
+
+ bytes = read_file_to_str(fname, RFTS_BIN, &st);
+ if (!bytes)
+ return -1;
+ /* This cast isn't so great, but it should be safe given the actual files
+ * and lengths we're using. */
+ if (st.st_size < (off_t)len)
+ len = MIN(len, (size_t)st.st_size);
+
+ int r = write_bytes_to_file(fname, bytes, len, 1);
+ tor_free(bytes);
+ return r;
+}
+#endif
+
/** Sanity check for crypto pk digests */
static void
test_crypto_digests(void *arg)
{
crypto_pk_t *k = NULL;
ssize_t r;
- digests_t pkey_digests;
+ common_digests_t pkey_digests;
char digest[DIGEST_LEN];
(void)arg;
@@ -653,7 +1392,7 @@ test_crypto_digests(void *arg)
tt_mem_op(hex_str(digest, DIGEST_LEN),OP_EQ,
AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
- r = crypto_pk_get_all_digests(k, &pkey_digests);
+ r = crypto_pk_get_common_digests(k, &pkey_digests);
tt_mem_op(hex_str(pkey_digests.d[DIGEST_SHA1], DIGEST_LEN),OP_EQ,
AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
@@ -663,6 +1402,38 @@ test_crypto_digests(void *arg)
crypto_pk_free(k);
}
+static void
+test_crypto_digest_names(void *arg)
+{
+ static const struct {
+ int a; const char *n;
+ } names[] = {
+ { DIGEST_SHA1, "sha1" },
+ { DIGEST_SHA256, "sha256" },
+ { DIGEST_SHA512, "sha512" },
+ { DIGEST_SHA3_256, "sha3-256" },
+ { DIGEST_SHA3_512, "sha3-512" },
+ { -1, NULL }
+ };
+ (void)arg;
+
+ int i;
+ for (i = 0; names[i].n; ++i) {
+ tt_str_op(names[i].n, OP_EQ,crypto_digest_algorithm_get_name(names[i].a));
+ tt_int_op(names[i].a,
+ OP_EQ,crypto_digest_algorithm_parse_name(names[i].n));
+ }
+ tt_int_op(-1, OP_EQ,
+ crypto_digest_algorithm_parse_name("TimeCubeHash-4444"));
+ done:
+ ;
+}
+
+#ifndef OPENSSL_1_1_API
+#define EVP_ENCODE_CTX_new() tor_malloc_zero(sizeof(EVP_ENCODE_CTX))
+#define EVP_ENCODE_CTX_free(ctx) tor_free(ctx)
+#endif
+
/** Encode src into dest with OpenSSL's EVP Encode interface, returning the
* length of the encoded data in bytes.
*/
@@ -670,12 +1441,13 @@ static int
base64_encode_evp(char *dest, char *src, size_t srclen)
{
const unsigned char *s = (unsigned char*)src;
- EVP_ENCODE_CTX ctx;
+ EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int len, ret;
- EVP_EncodeInit(&ctx);
- EVP_EncodeUpdate(&ctx, (unsigned char *)dest, &len, s, (int)srclen);
- EVP_EncodeFinal(&ctx, (unsigned char *)(dest + len), &ret);
+ EVP_EncodeInit(ctx);
+ EVP_EncodeUpdate(ctx, (unsigned char *)dest, &len, s, (int)srclen);
+ EVP_EncodeFinal(ctx, (unsigned char *)(dest + len), &ret);
+ EVP_ENCODE_CTX_free(ctx);
return ret+ len;
}
@@ -779,7 +1551,7 @@ test_crypto_formats(void *arg)
strlcpy(data1, "f0d678affc000100", 1024);
i = base16_decode(data2, 8, data1, 16);
- tt_int_op(i,OP_EQ, 0);
+ tt_int_op(i,OP_EQ, 8);
tt_mem_op(data2,OP_EQ, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
/* now try some failing base16 decodes */
@@ -1050,13 +1822,98 @@ test_crypto_hkdf_sha256(void *arg)
"b206fa34e5bc78d063fc291501beec53b36e5a0e434561200c"
"5f8bd13e0f88b3459600b4dc21d69363e2895321c06184879d"
"94b18f078411be70b767c7fc40679a9440a0c95ea83a23efbf");
-
done:
tor_free(mem_op_hex_tmp);
#undef EXPAND
}
static void
+test_crypto_hkdf_sha256_testvecs(void *arg)
+{
+ (void) arg;
+ /* Test vectors from RFC5869, sections A.1 through A.3 */
+ const struct {
+ const char *ikm16, *salt16, *info16;
+ int L;
+ const char *okm16;
+ } vecs[] = {
+ { /* from A.1 */
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "000102030405060708090a0b0c",
+ "f0f1f2f3f4f5f6f7f8f9",
+ 42,
+ "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
+ "34007208d5b887185865"
+ },
+ { /* from A.2 */
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
+ "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
+ "404142434445464748494a4b4c4d4e4f",
+ "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
+ "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
+ "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
+ "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
+ "d0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeef"
+ "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
+ 82,
+ "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c"
+ "59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71"
+ "cc30c58179ec3e87c14c01d5c1f3434f1d87"
+ },
+ { /* from A.3 */
+ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
+ "",
+ "",
+ 42,
+ "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d"
+ "9d201395faa4b61a96c8",
+ },
+ { NULL, NULL, NULL, -1, NULL }
+ };
+
+ int i;
+ char *ikm = NULL;
+ char *salt = NULL;
+ char *info = NULL;
+ char *okm = NULL;
+ char *mem_op_hex_tmp = NULL;
+
+ for (i = 0; vecs[i].ikm16; ++i) {
+ size_t ikm_len = strlen(vecs[i].ikm16)/2;
+ size_t salt_len = strlen(vecs[i].salt16)/2;
+ size_t info_len = strlen(vecs[i].info16)/2;
+ size_t okm_len = vecs[i].L;
+
+ ikm = tor_malloc(ikm_len);
+ salt = tor_malloc(salt_len);
+ info = tor_malloc(info_len);
+ okm = tor_malloc(okm_len);
+
+ base16_decode(ikm, ikm_len, vecs[i].ikm16, strlen(vecs[i].ikm16));
+ base16_decode(salt, salt_len, vecs[i].salt16, strlen(vecs[i].salt16));
+ base16_decode(info, info_len, vecs[i].info16, strlen(vecs[i].info16));
+
+ int r = crypto_expand_key_material_rfc5869_sha256(
+ (const uint8_t*)ikm, ikm_len,
+ (const uint8_t*)salt, salt_len,
+ (const uint8_t*)info, info_len,
+ (uint8_t*)okm, okm_len);
+ tt_int_op(r, OP_EQ, 0);
+ test_memeq_hex(okm, vecs[i].okm16);
+ tor_free(ikm);
+ tor_free(salt);
+ tor_free(info);
+ tor_free(okm);
+ }
+ done:
+ tor_free(ikm);
+ tor_free(salt);
+ tor_free(info);
+ tor_free(okm);
+ tor_free(mem_op_hex_tmp);
+}
+
+static void
test_crypto_curve25519_impl(void *arg)
{
/* adapted from curve25519_donna, which adapted it from test-curve25519
@@ -1148,6 +2005,47 @@ test_crypto_curve25519_basepoint(void *arg)
}
static void
+test_crypto_curve25519_testvec(void *arg)
+{
+ (void)arg;
+ char *mem_op_hex_tmp = NULL;
+
+ /* From RFC 7748, section 6.1 */
+ /* Alice's private key, a: */
+ const char a16[] =
+ "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a";
+ /* Alice's public key, X25519(a, 9): */
+ const char a_pub16[] =
+ "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a";
+ /* Bob's private key, b: */
+ const char b16[] =
+ "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb";
+ /* Bob's public key, X25519(b, 9): */
+ const char b_pub16[] =
+ "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f";
+ /* Their shared secret, K: */
+ const char k16[] =
+ "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
+
+ uint8_t a[32], b[32], a_pub[32], b_pub[32], k1[32], k2[32];
+ base16_decode((char*)a, sizeof(a), a16, strlen(a16));
+ base16_decode((char*)b, sizeof(b), b16, strlen(b16));
+ curve25519_basepoint_impl(a_pub, a);
+ curve25519_basepoint_impl(b_pub, b);
+ curve25519_impl(k1, a, b_pub);
+ curve25519_impl(k2, b, a_pub);
+
+ test_memeq_hex(a, a16);
+ test_memeq_hex(b, b16);
+ test_memeq_hex(a_pub, a_pub16);
+ test_memeq_hex(b_pub, b_pub16);
+ test_memeq_hex(k1, k16);
+ test_memeq_hex(k2, k16);
+ done:
+ tor_free(mem_op_hex_tmp);
+}
+
+static void
test_crypto_curve25519_wrappers(void *arg)
{
curve25519_public_key_t pubkey1, pubkey2;
@@ -1356,6 +2254,41 @@ test_crypto_ed25519_simple(void *arg)
tt_int_op(0, OP_EQ, ed25519_checksig_batch(NULL, ch, 2));
}
+ /* Test the string-prefixed sign/checksig functions */
+ {
+ ed25519_signature_t manual_sig;
+ char *prefixed_msg;
+
+ /* Generate a signature with a prefixed msg. */
+ tt_int_op(0, OP_EQ, ed25519_sign_prefixed(&sig1, msg, msg_len,
+ "always in the mood",
+ &kp1));
+
+ /* First, check that ed25519_sign_prefixed() returns the exact same sig as
+ if we had manually prefixed the msg ourselves. */
+ tor_asprintf(&prefixed_msg, "%s%s", "always in the mood", msg);
+ tt_int_op(0, OP_EQ, ed25519_sign(&manual_sig, (uint8_t *)prefixed_msg,
+ strlen(prefixed_msg), &kp1));
+ tor_free(prefixed_msg);
+ tt_assert(fast_memeq(sig1.sig, manual_sig.sig, sizeof(sig1.sig)));
+
+ /* Test that prefixed checksig verifies it properly. */
+ tt_int_op(0, OP_EQ, ed25519_checksig_prefixed(&sig1, msg, msg_len,
+ "always in the mood",
+ &pub1));
+
+ /* Test that checksig with wrong prefix fails. */
+ tt_int_op(-1, OP_EQ, ed25519_checksig_prefixed(&sig1, msg, msg_len,
+ "always in the moo",
+ &pub1));
+ tt_int_op(-1, OP_EQ, ed25519_checksig_prefixed(&sig1, msg, msg_len,
+ "always in the moon",
+ &pub1));
+ tt_int_op(-1, OP_EQ, ed25519_checksig_prefixed(&sig1, msg, msg_len,
+ "always in the mood!",
+ &pub1));
+ }
+
done:
;
}
@@ -1421,7 +2354,67 @@ test_crypto_ed25519_test_vectors(void *arg)
"1fbc1e08682f2cc0c92efe8f4985dec61dcbd54d4b94a22547d24451271c8b00",
"0a688e79be24f866286d4646b5d81c"
},
-
+ /* These come from draft-irtf-cfrg-eddsa-05 section 7.1 */
+ {
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60",
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a",
+ "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155"
+ "5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b",
+ ""
+ },
+ {
+ "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb",
+ "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c",
+ "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da"
+ "085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00",
+ "72"
+ },
+ {
+ "f5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5",
+ "278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e",
+ "0aab4c900501b3e24d7cdf4663326a3a87df5e4843b2cbdb67cbf6e460fec350"
+ "aa5371b1508f9f4528ecea23c436d94b5e8fcd4f681e30a6ac00a9704a188a03",
+ "08b8b2b733424243760fe426a4b54908632110a66c2f6591eabd3345e3e4eb98"
+ "fa6e264bf09efe12ee50f8f54e9f77b1e355f6c50544e23fb1433ddf73be84d8"
+ "79de7c0046dc4996d9e773f4bc9efe5738829adb26c81b37c93a1b270b20329d"
+ "658675fc6ea534e0810a4432826bf58c941efb65d57a338bbd2e26640f89ffbc"
+ "1a858efcb8550ee3a5e1998bd177e93a7363c344fe6b199ee5d02e82d522c4fe"
+ "ba15452f80288a821a579116ec6dad2b3b310da903401aa62100ab5d1a36553e"
+ "06203b33890cc9b832f79ef80560ccb9a39ce767967ed628c6ad573cb116dbef"
+ "efd75499da96bd68a8a97b928a8bbc103b6621fcde2beca1231d206be6cd9ec7"
+ "aff6f6c94fcd7204ed3455c68c83f4a41da4af2b74ef5c53f1d8ac70bdcb7ed1"
+ "85ce81bd84359d44254d95629e9855a94a7c1958d1f8ada5d0532ed8a5aa3fb2"
+ "d17ba70eb6248e594e1a2297acbbb39d502f1a8c6eb6f1ce22b3de1a1f40cc24"
+ "554119a831a9aad6079cad88425de6bde1a9187ebb6092cf67bf2b13fd65f270"
+ "88d78b7e883c8759d2c4f5c65adb7553878ad575f9fad878e80a0c9ba63bcbcc"
+ "2732e69485bbc9c90bfbd62481d9089beccf80cfe2df16a2cf65bd92dd597b07"
+ "07e0917af48bbb75fed413d238f5555a7a569d80c3414a8d0859dc65a46128ba"
+ "b27af87a71314f318c782b23ebfe808b82b0ce26401d2e22f04d83d1255dc51a"
+ "ddd3b75a2b1ae0784504df543af8969be3ea7082ff7fc9888c144da2af58429e"
+ "c96031dbcad3dad9af0dcbaaaf268cb8fcffead94f3c7ca495e056a9b47acdb7"
+ "51fb73e666c6c655ade8297297d07ad1ba5e43f1bca32301651339e22904cc8c"
+ "42f58c30c04aafdb038dda0847dd988dcda6f3bfd15c4b4c4525004aa06eeff8"
+ "ca61783aacec57fb3d1f92b0fe2fd1a85f6724517b65e614ad6808d6f6ee34df"
+ "f7310fdc82aebfd904b01e1dc54b2927094b2db68d6f903b68401adebf5a7e08"
+ "d78ff4ef5d63653a65040cf9bfd4aca7984a74d37145986780fc0b16ac451649"
+ "de6188a7dbdf191f64b5fc5e2ab47b57f7f7276cd419c17a3ca8e1b939ae49e4"
+ "88acba6b965610b5480109c8b17b80e1b7b750dfc7598d5d5011fd2dcc5600a3"
+ "2ef5b52a1ecc820e308aa342721aac0943bf6686b64b2579376504ccc493d97e"
+ "6aed3fb0f9cd71a43dd497f01f17c0e2cb3797aa2a2f256656168e6c496afc5f"
+ "b93246f6b1116398a346f1a641f3b041e989f7914f90cc2c7fff357876e506b5"
+ "0d334ba77c225bc307ba537152f3f1610e4eafe595f6d9d90d11faa933a15ef1"
+ "369546868a7f3a45a96768d40fd9d03412c091c6315cf4fde7cb68606937380d"
+ "b2eaaa707b4c4185c32eddcdd306705e4dc1ffc872eeee475a64dfac86aba41c"
+ "0618983f8741c5ef68d3a101e8a3b8cac60c905c15fc910840b94c00a0b9d0"
+ },
+ {
+ "833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42",
+ "ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf",
+ "dc2a4459e7369633a52b1bf277839a00201009a3efbf3ecb69bea2186c26b589"
+ "09351fc9ac90b3ecfdfbc7c66431e0303dca179c138ac17ad9bef1177331a704",
+ "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
+ "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
+ },
{ NULL, NULL, NULL, NULL}
};
@@ -1591,8 +2584,9 @@ test_crypto_ed25519_testvectors(void *arg)
#define DECODE(p,s) base16_decode((char*)(p),sizeof(p),(s),strlen(s))
#define EQ(a,h) test_memeq_hex((const char*)(a), (h))
- tt_int_op(0, OP_EQ, DECODE(sk, ED25519_SECRET_KEYS[i]));
- tt_int_op(0, OP_EQ, DECODE(blinding_param, ED25519_BLINDING_PARAMS[i]));
+ tt_int_op(sizeof(sk), OP_EQ, DECODE(sk, ED25519_SECRET_KEYS[i]));
+ tt_int_op(sizeof(blinding_param), OP_EQ, DECODE(blinding_param,
+ ED25519_BLINDING_PARAMS[i]));
tt_int_op(0, OP_EQ, ed25519_secret_key_from_seed(&esk, sk));
EQ(esk.seckey, ED25519_EXPANDED_SECRET_KEYS[i]);
@@ -1637,74 +2631,51 @@ test_crypto_ed25519_testvectors(void *arg)
}
static void
-test_crypto_ed25519_fuzz_donna(void *arg)
+test_crypto_ed25519_storage(void *arg)
{
- const unsigned iters = 1024;
- uint8_t msg[1024];
- unsigned i;
(void)arg;
+ ed25519_keypair_t *keypair = NULL;
+ ed25519_public_key_t pub;
+ ed25519_secret_key_t sec;
+ char *fname_1 = tor_strdup(get_fname("ed_seckey_1"));
+ char *fname_2 = tor_strdup(get_fname("ed_pubkey_2"));
+ char *contents = NULL;
+ char *tag = NULL;
- tt_assert(sizeof(msg) == iters);
- crypto_rand((char*) msg, sizeof(msg));
-
- /* Fuzz Ed25519-donna vs ref10, alternating the implementation used to
- * generate keys/sign per iteration.
- */
- for (i = 0; i < iters; ++i) {
- const int use_donna = i & 1;
- uint8_t blinding[32];
- curve25519_keypair_t ckp;
- ed25519_keypair_t kp, kp_blind, kp_curve25519;
- ed25519_public_key_t pk, pk_blind, pk_curve25519;
- ed25519_signature_t sig, sig_blind;
- int bit = 0;
-
- crypto_rand((char*) blinding, sizeof(blinding));
-
- /* Impl. A:
- * 1. Generate a keypair.
- * 2. Blinded the keypair.
- * 3. Sign a message (unblinded).
- * 4. Sign a message (blinded).
- * 5. Generate a curve25519 keypair, and convert it to Ed25519.
- */
- ed25519_set_impl_params(use_donna);
- tt_int_op(0, OP_EQ, ed25519_keypair_generate(&kp, i&1));
- tt_int_op(0, OP_EQ, ed25519_keypair_blind(&kp_blind, &kp, blinding));
- tt_int_op(0, OP_EQ, ed25519_sign(&sig, msg, i, &kp));
- tt_int_op(0, OP_EQ, ed25519_sign(&sig_blind, msg, i, &kp_blind));
-
- tt_int_op(0, OP_EQ, curve25519_keypair_generate(&ckp, i&1));
- tt_int_op(0, OP_EQ, ed25519_keypair_from_curve25519_keypair(
- &kp_curve25519, &bit, &ckp));
-
- /* Impl. B:
- * 1. Validate the public key by rederiving it.
- * 2. Validate the blinded public key by rederiving it.
- * 3. Validate the unblinded signature (and test a invalid signature).
- * 4. Validate the blinded signature.
- * 5. Validate the public key (from Curve25519) by rederiving it.
- */
- ed25519_set_impl_params(!use_donna);
- tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pk, &kp.seckey));
- tt_mem_op(pk.pubkey, OP_EQ, kp.pubkey.pubkey, 32);
-
- tt_int_op(0, OP_EQ, ed25519_public_blind(&pk_blind, &kp.pubkey, blinding));
- tt_mem_op(pk_blind.pubkey, OP_EQ, kp_blind.pubkey.pubkey, 32);
+ keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
+ tt_int_op(0,OP_EQ,ed25519_keypair_generate(keypair, 0));
+ tt_int_op(0,OP_EQ,
+ ed25519_seckey_write_to_file(&keypair->seckey, fname_1, "foo"));
+ tt_int_op(0,OP_EQ,
+ ed25519_pubkey_write_to_file(&keypair->pubkey, fname_2, "bar"));
- tt_int_op(0, OP_EQ, ed25519_checksig(&sig, msg, i, &pk));
- sig.sig[0] ^= 15;
- tt_int_op(-1, OP_EQ, ed25519_checksig(&sig, msg, sizeof(msg), &pk));
+ tt_int_op(-1, OP_EQ, ed25519_pubkey_read_from_file(&pub, &tag, fname_1));
+ tt_ptr_op(tag, OP_EQ, NULL);
+ tt_int_op(-1, OP_EQ, ed25519_seckey_read_from_file(&sec, &tag, fname_2));
+ tt_ptr_op(tag, OP_EQ, NULL);
- tt_int_op(0, OP_EQ, ed25519_checksig(&sig_blind, msg, i, &pk_blind));
+ tt_int_op(0, OP_EQ, ed25519_pubkey_read_from_file(&pub, &tag, fname_2));
+ tt_str_op(tag, OP_EQ, "bar");
+ tor_free(tag);
+ tt_int_op(0, OP_EQ, ed25519_seckey_read_from_file(&sec, &tag, fname_1));
+ tt_str_op(tag, OP_EQ, "foo");
+ tor_free(tag);
- tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
- &pk_curve25519, &ckp.pubkey, bit));
- tt_mem_op(pk_curve25519.pubkey, OP_EQ, kp_curve25519.pubkey.pubkey, 32);
- }
+ /* whitebox test: truncated keys. */
+ tt_int_op(0, ==, do_truncate(fname_1, 40));
+ tt_int_op(0, ==, do_truncate(fname_2, 40));
+ tt_int_op(-1, OP_EQ, ed25519_pubkey_read_from_file(&pub, &tag, fname_2));
+ tt_ptr_op(tag, OP_EQ, NULL);
+ tor_free(tag);
+ tt_int_op(-1, OP_EQ, ed25519_seckey_read_from_file(&sec, &tag, fname_1));
+ tt_ptr_op(tag, OP_EQ, NULL);
done:
- ;
+ tor_free(fname_1);
+ tor_free(fname_2);
+ tor_free(contents);
+ tor_free(tag);
+ ed25519_keypair_free(keypair);
}
static void
@@ -1803,20 +2774,150 @@ test_crypto_siphash(void *arg)
;
}
+/* We want the likelihood that the random buffer exhibits any regular pattern
+ * to be far less than the memory bit error rate in the int return value.
+ * Using 2048 bits provides a failure rate of 1/(3 * 10^616), and we call
+ * 3 functions, leading to an overall error rate of 1/10^616.
+ * This is comparable with the 1/10^603 failure rate of test_crypto_rng_range.
+ */
+#define FAILURE_MODE_BUFFER_SIZE (2048/8)
+
+/** Check crypto_rand for a failure mode where it does nothing to the buffer,
+ * or it sets the buffer to all zeroes. Return 0 when the check passes,
+ * or -1 when it fails. */
+static int
+crypto_rand_check_failure_mode_zero(void)
+{
+ char buf[FAILURE_MODE_BUFFER_SIZE];
+
+ memset(buf, 0, FAILURE_MODE_BUFFER_SIZE);
+ crypto_rand(buf, FAILURE_MODE_BUFFER_SIZE);
+
+ for (size_t i = 0; i < FAILURE_MODE_BUFFER_SIZE; i++) {
+ if (buf[i] != 0) {
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+/** Check crypto_rand for a failure mode where every int64_t in the buffer is
+ * the same. Return 0 when the check passes, or -1 when it fails. */
+static int
+crypto_rand_check_failure_mode_identical(void)
+{
+ /* just in case the buffer size isn't a multiple of sizeof(int64_t) */
+#define FAILURE_MODE_BUFFER_SIZE_I64 \
+ (FAILURE_MODE_BUFFER_SIZE/SIZEOF_INT64_T)
+#define FAILURE_MODE_BUFFER_SIZE_I64_BYTES \
+ (FAILURE_MODE_BUFFER_SIZE_I64*SIZEOF_INT64_T)
+
+#if FAILURE_MODE_BUFFER_SIZE_I64 < 2
+#error FAILURE_MODE_BUFFER_SIZE needs to be at least 2*SIZEOF_INT64_T
+#endif
+
+ int64_t buf[FAILURE_MODE_BUFFER_SIZE_I64];
+
+ memset(buf, 0, FAILURE_MODE_BUFFER_SIZE_I64_BYTES);
+ crypto_rand((char *)buf, FAILURE_MODE_BUFFER_SIZE_I64_BYTES);
+
+ for (size_t i = 1; i < FAILURE_MODE_BUFFER_SIZE_I64; i++) {
+ if (buf[i] != buf[i-1]) {
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+/** Check crypto_rand for a failure mode where it increments the "random"
+ * value by 1 for every byte in the buffer. (This is OpenSSL's PREDICT mode.)
+ * Return 0 when the check passes, or -1 when it fails. */
+static int
+crypto_rand_check_failure_mode_predict(void)
+{
+ unsigned char buf[FAILURE_MODE_BUFFER_SIZE];
+
+ memset(buf, 0, FAILURE_MODE_BUFFER_SIZE);
+ crypto_rand((char *)buf, FAILURE_MODE_BUFFER_SIZE);
+
+ for (size_t i = 1; i < FAILURE_MODE_BUFFER_SIZE; i++) {
+ /* check if the last byte was incremented by 1, including integer
+ * wrapping */
+ if (buf[i] - buf[i-1] != 1 && buf[i-1] - buf[i] != 255) {
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+#undef FAILURE_MODE_BUFFER_SIZE
+
+static void
+test_crypto_failure_modes(void *arg)
+{
+ int rv = 0;
+ (void)arg;
+
+ rv = crypto_early_init();
+ tt_assert(rv == 0);
+
+ /* Check random works */
+ rv = crypto_rand_check_failure_mode_zero();
+ tt_assert(rv == 0);
+
+ rv = crypto_rand_check_failure_mode_identical();
+ tt_assert(rv == 0);
+
+ rv = crypto_rand_check_failure_mode_predict();
+ tt_assert(rv == 0);
+
+ done:
+ ;
+}
+
#define CRYPTO_LEGACY(name) \
{ #name, test_crypto_ ## name , 0, NULL, NULL }
+#define ED25519_TEST_ONE(name, fl, which) \
+ { #name "/ed25519_" which, test_crypto_ed25519_ ## name, (fl), \
+ &ed25519_test_setup, (void*)which }
+
+#define ED25519_TEST(name, fl) \
+ ED25519_TEST_ONE(name, (fl), "donna"), \
+ ED25519_TEST_ONE(name, (fl), "ref10")
+
struct testcase_t crypto_tests[] = {
CRYPTO_LEGACY(formats),
CRYPTO_LEGACY(rng),
{ "rng_range", test_crypto_rng_range, 0, NULL, NULL },
- { "aes_AES", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"aes" },
- { "aes_EVP", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"evp" },
+ { "rng_engine", test_crypto_rng_engine, TT_FORK, NULL, NULL },
+ { "rng_strongest", test_crypto_rng_strongest, TT_FORK, NULL, NULL },
+ { "rng_strongest_nosyscall", test_crypto_rng_strongest, TT_FORK,
+ &passthrough_setup, (void*)"nosyscall" },
+ { "rng_strongest_nofallback", test_crypto_rng_strongest, TT_FORK,
+ &passthrough_setup, (void*)"nofallback" },
+ { "rng_strongest_broken", test_crypto_rng_strongest, TT_FORK,
+ &passthrough_setup, (void*)"broken" },
+ { "openssl_version", test_crypto_openssl_version, TT_FORK, NULL, NULL },
+ { "aes_AES", test_crypto_aes128, TT_FORK, &passthrough_setup, (void*)"aes" },
+ { "aes_EVP", test_crypto_aes128, TT_FORK, &passthrough_setup, (void*)"evp" },
+ { "aes128_ctr_testvec", test_crypto_aes_ctr_testvec, 0,
+ &passthrough_setup, (void*)"128" },
+ { "aes192_ctr_testvec", test_crypto_aes_ctr_testvec, 0,
+ &passthrough_setup, (void*)"192" },
+ { "aes256_ctr_testvec", test_crypto_aes_ctr_testvec, 0,
+ &passthrough_setup, (void*)"256" },
CRYPTO_LEGACY(sha),
CRYPTO_LEGACY(pk),
{ "pk_fingerprints", test_crypto_pk_fingerprints, TT_FORK, NULL, NULL },
{ "pk_base64", test_crypto_pk_base64, TT_FORK, NULL, NULL },
CRYPTO_LEGACY(digests),
+ { "digest_names", test_crypto_digest_names, 0, NULL, NULL },
+ { "sha3", test_crypto_sha3, TT_FORK, NULL, NULL},
+ { "sha3_xof", test_crypto_sha3_xof, TT_FORK, NULL, NULL},
CRYPTO_LEGACY(dh),
{ "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &passthrough_setup,
(void*)"aes" },
@@ -1825,22 +2926,24 @@ struct testcase_t crypto_tests[] = {
CRYPTO_LEGACY(base32_decode),
{ "kdf_TAP", test_crypto_kdf_TAP, 0, NULL, NULL },
{ "hkdf_sha256", test_crypto_hkdf_sha256, 0, NULL, NULL },
+ { "hkdf_sha256_testvecs", test_crypto_hkdf_sha256_testvecs, 0, NULL, NULL },
{ "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
{ "curve25519_impl_hibit", test_crypto_curve25519_impl, 0, NULL, (void*)"y"},
+ { "curve25516_testvec", test_crypto_curve25519_testvec, 0, NULL, NULL },
{ "curve25519_basepoint",
test_crypto_curve25519_basepoint, TT_FORK, NULL, NULL },
{ "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
{ "curve25519_encode", test_crypto_curve25519_encode, 0, NULL, NULL },
{ "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
- { "ed25519_simple", test_crypto_ed25519_simple, 0, NULL, NULL },
- { "ed25519_test_vectors", test_crypto_ed25519_test_vectors, 0, NULL, NULL },
- { "ed25519_encode", test_crypto_ed25519_encode, 0, NULL, NULL },
- { "ed25519_convert", test_crypto_ed25519_convert, 0, NULL, NULL },
- { "ed25519_blinding", test_crypto_ed25519_blinding, 0, NULL, NULL },
- { "ed25519_testvectors", test_crypto_ed25519_testvectors, 0, NULL, NULL },
- { "ed25519_fuzz_donna", test_crypto_ed25519_fuzz_donna, TT_FORK, NULL,
- NULL },
+ ED25519_TEST(simple, 0),
+ ED25519_TEST(test_vectors, 0),
+ ED25519_TEST(encode, 0),
+ ED25519_TEST(convert, 0),
+ ED25519_TEST(blinding, 0),
+ ED25519_TEST(testvectors, 0),
+ { "ed25519_storage", test_crypto_ed25519_storage, 0, NULL, NULL },
{ "siphash", test_crypto_siphash, 0, NULL, NULL },
+ { "failure_modes", test_crypto_failure_modes, TT_FORK, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_crypto_slow.c b/src/test/test_crypto_slow.c
index ab28ef5095..0be58c9389 100644
--- a/src/test/test_crypto_slow.c
+++ b/src/test/test_crypto_slow.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -10,7 +10,8 @@
#include "crypto_s2k.h"
#include "crypto_pwbox.h"
-#if defined(HAVE_LIBSCRYPT_H)
+#if defined(HAVE_LIBSCRYPT_H) && defined(HAVE_LIBSCRYPT_SCRYPT)
+#define HAVE_LIBSCRYPT
#include <libscrypt.h>
#endif
@@ -129,7 +130,7 @@ test_crypto_s2k_general(void *arg)
}
}
-#if defined(HAVE_LIBSCRYPT_H) && defined(HAVE_EVP_PBE_SCRYPT)
+#if defined(HAVE_LIBSCRYPT) && defined(HAVE_EVP_PBE_SCRYPT)
static void
test_libscrypt_eq_openssl(void *arg)
{
@@ -277,7 +278,7 @@ test_crypto_s2k_errors(void *arg)
buf, sizeof(buf), "ABC", 3));
/* Truncated output */
-#ifdef HAVE_LIBSCRYPT_H
+#ifdef HAVE_LIBSCRYPT
tt_int_op(S2K_TRUNCATED, OP_EQ, secret_to_key_new(buf, 50, &sz,
"ABC", 3, 0));
tt_int_op(S2K_TRUNCATED, OP_EQ, secret_to_key_new(buf, 50, &sz,
@@ -288,7 +289,7 @@ test_crypto_s2k_errors(void *arg)
tt_int_op(S2K_TRUNCATED, OP_EQ, secret_to_key_new(buf, 29, &sz,
"ABC", 3, S2K_FLAG_NO_SCRYPT));
-#ifdef HAVE_LIBSCRYPT_H
+#ifdef HAVE_LIBSCRYPT
tt_int_op(S2K_TRUNCATED, OP_EQ, secret_to_key_make_specifier(buf, 18, 0));
tt_int_op(S2K_TRUNCATED, OP_EQ, secret_to_key_make_specifier(buf, 18,
S2K_FLAG_LOW_MEM));
@@ -309,7 +310,7 @@ test_crypto_s2k_errors(void *arg)
secret_to_key_derivekey(buf2, sizeof(buf2),
buf, 18, "ABC", 3));
-#ifdef HAVE_LIBSCRYPT_H
+#ifdef HAVE_LIBSCRYPT
/* It's a bad scrypt buffer if N would overflow uint64 */
memset(buf, 0, sizeof(buf));
buf[0] = 2; /* scrypt */
@@ -330,7 +331,7 @@ test_crypto_scrypt_vectors(void *arg)
uint8_t spec[64], out[64];
(void)arg;
-#ifndef HAVE_LIBSCRYPT_H
+#ifndef HAVE_LIBSCRYPT
if (1)
tt_skip();
#endif
@@ -421,12 +422,16 @@ test_crypto_pbkdf2_vectors(void *arg)
secret_to_key_compute_key(out, 20, spec, 5, "password", 8, 1));
test_memeq_hex(out, "4b007901b765489abead49d926f721d065a429c1");
+ /* This is the very slow one here. When enabled, it accounts for roughly
+ * half the time spent in test-slow. */
+ /*
base16_decode((char*)spec, sizeof(spec),
"73616c74" "18" , 10);
memset(out, 0x00, sizeof(out));
tt_int_op(20, OP_EQ,
secret_to_key_compute_key(out, 20, spec, 5, "password", 8, 1));
test_memeq_hex(out, "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984");
+ */
base16_decode((char*)spec, sizeof(spec),
"73616c7453414c5473616c7453414c5473616c745"
@@ -503,12 +508,91 @@ test_crypto_pwbox(void *arg)
tor_free(decoded);
}
+static void
+test_crypto_ed25519_fuzz_donna(void *arg)
+{
+ const unsigned iters = 1024;
+ uint8_t msg[1024];
+ unsigned i;
+ (void)arg;
+
+ tt_assert(sizeof(msg) == iters);
+ crypto_rand((char*) msg, sizeof(msg));
+
+ /* Fuzz Ed25519-donna vs ref10, alternating the implementation used to
+ * generate keys/sign per iteration.
+ */
+ for (i = 0; i < iters; ++i) {
+ const int use_donna = i & 1;
+ uint8_t blinding[32];
+ curve25519_keypair_t ckp;
+ ed25519_keypair_t kp, kp_blind, kp_curve25519;
+ ed25519_public_key_t pk, pk_blind, pk_curve25519;
+ ed25519_signature_t sig, sig_blind;
+ int bit = 0;
+
+ crypto_rand((char*) blinding, sizeof(blinding));
+
+ /* Impl. A:
+ * 1. Generate a keypair.
+ * 2. Blinded the keypair.
+ * 3. Sign a message (unblinded).
+ * 4. Sign a message (blinded).
+ * 5. Generate a curve25519 keypair, and convert it to Ed25519.
+ */
+ ed25519_set_impl_params(use_donna);
+ tt_int_op(0, OP_EQ, ed25519_keypair_generate(&kp, i&1));
+ tt_int_op(0, OP_EQ, ed25519_keypair_blind(&kp_blind, &kp, blinding));
+ tt_int_op(0, OP_EQ, ed25519_sign(&sig, msg, i, &kp));
+ tt_int_op(0, OP_EQ, ed25519_sign(&sig_blind, msg, i, &kp_blind));
+
+ tt_int_op(0, OP_EQ, curve25519_keypair_generate(&ckp, i&1));
+ tt_int_op(0, OP_EQ, ed25519_keypair_from_curve25519_keypair(
+ &kp_curve25519, &bit, &ckp));
+
+ /* Impl. B:
+ * 1. Validate the public key by rederiving it.
+ * 2. Validate the blinded public key by rederiving it.
+ * 3. Validate the unblinded signature (and test a invalid signature).
+ * 4. Validate the blinded signature.
+ * 5. Validate the public key (from Curve25519) by rederiving it.
+ */
+ ed25519_set_impl_params(!use_donna);
+ tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pk, &kp.seckey));
+ tt_mem_op(pk.pubkey, OP_EQ, kp.pubkey.pubkey, 32);
+
+ tt_int_op(0, OP_EQ, ed25519_public_blind(&pk_blind, &kp.pubkey, blinding));
+ tt_mem_op(pk_blind.pubkey, OP_EQ, kp_blind.pubkey.pubkey, 32);
+
+ tt_int_op(0, OP_EQ, ed25519_checksig(&sig, msg, i, &pk));
+ sig.sig[0] ^= 15;
+ tt_int_op(-1, OP_EQ, ed25519_checksig(&sig, msg, sizeof(msg), &pk));
+
+ tt_int_op(0, OP_EQ, ed25519_checksig(&sig_blind, msg, i, &pk_blind));
+
+ tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
+ &pk_curve25519, &ckp.pubkey, bit));
+ tt_mem_op(pk_curve25519.pubkey, OP_EQ, kp_curve25519.pubkey.pubkey, 32);
+ }
+
+ done:
+ ;
+}
+
#define CRYPTO_LEGACY(name) \
{ #name, test_crypto_ ## name , 0, NULL, NULL }
+#define ED25519_TEST_ONE(name, fl, which) \
+ { #name "/ed25519_" which, test_crypto_ed25519_ ## name, (fl), \
+ &ed25519_test_setup, (void*)which }
+
+#define ED25519_TEST(name, fl) \
+ ED25519_TEST_ONE(name, (fl), "donna"), \
+ ED25519_TEST_ONE(name, (fl), "ref10")
+
struct testcase_t slow_crypto_tests[] = {
CRYPTO_LEGACY(s2k_rfc2440),
-#ifdef HAVE_LIBSCRYPT_H
+#ifdef HAVE_LIBSCRYPT
{ "s2k_scrypt", test_crypto_s2k_general, 0, &passthrough_setup,
(void*)"scrypt" },
{ "s2k_scrypt_low", test_crypto_s2k_general, 0, &passthrough_setup,
@@ -527,6 +611,7 @@ struct testcase_t slow_crypto_tests[] = {
{ "scrypt_vectors", test_crypto_scrypt_vectors, 0, NULL, NULL },
{ "pbkdf2_vectors", test_crypto_pbkdf2_vectors, 0, NULL, NULL },
{ "pwbox", test_crypto_pwbox, 0, NULL, NULL },
+ ED25519_TEST(fuzz_donna, TT_FORK),
END_OF_TESTCASES
};
diff --git a/src/test/test_data.c b/src/test/test_data.c
index 6afba65757..788489a097 100644
--- a/src/test/test_data.c
+++ b/src/test/test_data.c
@@ -1,8 +1,10 @@
/* Copyright 2001-2004 Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
+#include "test.h"
+
/* Our unit test expect that the AUTHORITY_CERT_* public keys will sort
* in this order. */
#define AUTHORITY_CERT_A AUTHORITY_CERT_3
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 855746e749..cdc56acb89 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -1,32 +1,44 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#include <math.h>
+#define CONFIG_PRIVATE
#define DIRSERV_PRIVATE
#define DIRVOTE_PRIVATE
#define ROUTER_PRIVATE
#define ROUTERLIST_PRIVATE
+#define ROUTERPARSE_PRIVATE
#define HIBERNATE_PRIVATE
#define NETWORKSTATUS_PRIVATE
+#define RELAY_PRIVATE
+
#include "or.h"
+#include "confparse.h"
#include "config.h"
#include "crypto_ed25519.h"
#include "directory.h"
#include "dirserv.h"
#include "dirvote.h"
#include "hibernate.h"
+#include "memarea.h"
#include "networkstatus.h"
#include "router.h"
#include "routerkeys.h"
#include "routerlist.h"
#include "routerparse.h"
#include "routerset.h"
+#include "shared_random_state.h"
#include "test.h"
+#include "test_dir_common.h"
#include "torcert.h"
+#include "relay.h"
+#include "log_test_helpers.h"
+
+#define NS_MODULE dir
static void
test_dir_nicknames(void *arg)
@@ -76,6 +88,15 @@ test_dir_nicknames(void *arg)
;
}
+static smartlist_t *mocked_configured_ports = NULL;
+
+/** Returns mocked_configured_ports */
+static const smartlist_t *
+mock_get_configured_ports(void)
+{
+ return mocked_configured_ports;
+}
+
/** Run unit tests for router descriptor generation logic. */
static void
test_dir_formats(void *arg)
@@ -95,6 +116,8 @@ test_dir_formats(void *arg)
or_options_t *options = get_options_mutable();
const addr_policy_t *p;
time_t now = time(NULL);
+ port_cfg_t orport, dirport;
+ char cert_buf[256];
(void)arg;
pk1 = pk_generate(0);
@@ -110,9 +133,15 @@ test_dir_formats(void *arg)
r1->cache_info.published_on = 0;
r1->or_port = 9000;
r1->dir_port = 9003;
+ r1->supports_tunnelled_dir_requests = 1;
tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
r1->ipv6_orport = 9999;
r1->onion_pkey = crypto_pk_dup_key(pk1);
+ /* Fake just enough of an ntor key to get by */
+ curve25519_keypair_t r1_onion_keypair;
+ curve25519_keypair_generate(&r1_onion_keypair, 0);
+ r1->onion_curve25519_pkey = tor_memdup(&r1_onion_keypair.pubkey,
+ sizeof(curve25519_public_key_t));
r1->identity_pkey = crypto_pk_dup_key(pk2);
r1->bandwidthrate = 1000;
r1->bandwidthburst = 5000;
@@ -140,20 +169,16 @@ test_dir_formats(void *arg)
ed25519_secret_key_from_seed(&kp2.seckey,
(const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
ed25519_public_key_generate(&kp2.pubkey, &kp2.seckey);
- r2->signing_key_cert = tor_cert_create(&kp1,
+ r2->cache_info.signing_key_cert = tor_cert_create(&kp1,
CERT_TYPE_ID_SIGNING,
&kp2.pubkey,
now, 86400,
CERT_FLAG_INCLUDE_SIGNING_KEY);
- char cert_buf[256];
- base64_encode(cert_buf, sizeof(cert_buf),
- (const char*)r2->signing_key_cert->encoded,
- r2->signing_key_cert->encoded_len,
- BASE64_ENCODE_MULTILINE);
r2->platform = tor_strdup(platform);
r2->cache_info.published_on = 5;
r2->or_port = 9005;
r2->dir_port = 0;
+ r2->supports_tunnelled_dir_requests = 1;
r2->onion_pkey = crypto_pk_dup_key(pk2);
curve25519_keypair_t r2_onion_keypair;
curve25519_keypair_generate(&r2_onion_keypair, 0);
@@ -171,10 +196,34 @@ test_dir_formats(void *arg)
tt_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
&pk2_str_len));
- /* XXXX025 router_dump_to_string should really take this from ri.*/
+ /* XXXX+++ router_dump_to_string should really take this from ri.*/
options->ContactInfo = tor_strdup("Magri White "
"<magri@elsewhere.example.com>");
+ /* Skip reachability checks for DirPort and tunnelled-dir-server */
+ options->AssumeReachable = 1;
+
+ /* Fake just enough of an ORPort and DirPort to get by */
+ MOCK(get_configured_ports, mock_get_configured_ports);
+ mocked_configured_ports = smartlist_new();
+
+ memset(&orport, 0, sizeof(orport));
+ orport.type = CONN_TYPE_OR_LISTENER;
+ orport.addr.family = AF_INET;
+ orport.port = 9000;
+ smartlist_add(mocked_configured_ports, &orport);
+
+ memset(&dirport, 0, sizeof(dirport));
+ dirport.type = CONN_TYPE_DIR_LISTENER;
+ dirport.addr.family = AF_INET;
+ dirport.port = 9003;
+ smartlist_add(mocked_configured_ports, &dirport);
+
buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
+
+ UNMOCK(get_configured_ports);
+ smartlist_free(mocked_configured_ports);
+ mocked_configured_ports = NULL;
+
tor_free(options->ContactInfo);
tt_assert(buf);
@@ -183,7 +232,6 @@ test_dir_formats(void *arg)
"platform Tor "VERSION" on ", sizeof(buf2));
strlcat(buf2, get_uname(), sizeof(buf2));
strlcat(buf2, "\n"
- "protocols Link 1 2 Circuit 1\n"
"published 1970-01-01 00:00:00\n"
"fingerprint ", sizeof(buf2));
tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
@@ -200,7 +248,13 @@ test_dir_formats(void *arg)
strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
sizeof(buf2));
- strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
+ strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
+ base64_encode(cert_buf, sizeof(cert_buf),
+ (const char*)r1_onion_keypair.pubkey.public_key, 32,
+ BASE64_ENCODE_MULTILINE);
+ strlcat(buf2, cert_buf, sizeof(buf2));
+ strlcat(buf2, "reject *:*\n", sizeof(buf2));
+ strlcat(buf2, "tunnelled-dir-server\nrouter-signature\n", sizeof(buf2));
buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
* twice */
@@ -214,12 +268,13 @@ test_dir_formats(void *arg)
tt_assert(rp1);
tt_int_op(rp1->addr,OP_EQ, r1->addr);
tt_int_op(rp1->or_port,OP_EQ, r1->or_port);
- //test_eq(rp1->dir_port, r1->dir_port);
+ tt_int_op(rp1->dir_port,OP_EQ, r1->dir_port);
tt_int_op(rp1->bandwidthrate,OP_EQ, r1->bandwidthrate);
tt_int_op(rp1->bandwidthburst,OP_EQ, r1->bandwidthburst);
tt_int_op(rp1->bandwidthcapacity,OP_EQ, r1->bandwidthcapacity);
tt_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
tt_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
+ tt_assert(rp1->supports_tunnelled_dir_requests);
//tt_assert(rp1->exit_policy == NULL);
tor_free(buf);
@@ -227,12 +282,17 @@ test_dir_formats(void *arg)
"router Fred 10.3.2.1 9005 0 0\n"
"identity-ed25519\n"
"-----BEGIN ED25519 CERT-----\n", sizeof(buf2));
+ base64_encode(cert_buf, sizeof(cert_buf),
+ (const char*)r2->cache_info.signing_key_cert->encoded,
+ r2->cache_info.signing_key_cert->encoded_len,
+ BASE64_ENCODE_MULTILINE);
strlcat(buf2, cert_buf, sizeof(buf2));
strlcat(buf2, "-----END ED25519 CERT-----\n", sizeof(buf2));
strlcat(buf2, "master-key-ed25519 ", sizeof(buf2));
{
char k[ED25519_BASE64_LEN+1];
- tt_assert(ed25519_public_to_base64(k, &r2->signing_key_cert->signing_key)
+ tt_assert(ed25519_public_to_base64(k,
+ &r2->cache_info.signing_key_cert->signing_key)
>= 0);
strlcat(buf2, k, sizeof(buf2));
strlcat(buf2, "\n", sizeof(buf2));
@@ -240,7 +300,6 @@ test_dir_formats(void *arg)
strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2));
strlcat(buf2, get_uname(), sizeof(buf2));
strlcat(buf2, "\n"
- "protocols Link 1 2 Circuit 1\n"
"published 1970-01-01 00:00:05\n"
"fingerprint ", sizeof(buf2));
tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
@@ -290,8 +349,19 @@ test_dir_formats(void *arg)
BASE64_ENCODE_MULTILINE);
strlcat(buf2, cert_buf, sizeof(buf2));
strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
+ strlcat(buf2, "tunnelled-dir-server\n", sizeof(buf2));
strlcat(buf2, "router-sig-ed25519 ", sizeof(buf2));
+ /* Fake just enough of an ORPort to get by */
+ MOCK(get_configured_ports, mock_get_configured_ports);
+ mocked_configured_ports = smartlist_new();
+
+ memset(&orport, 0, sizeof(orport));
+ orport.type = CONN_TYPE_OR_LISTENER;
+ orport.addr.family = AF_INET;
+ orport.port = 9005;
+ smartlist_add(mocked_configured_ports, &orport);
+
buf = router_dump_router_to_string(r2, pk1, pk2, &r2_onion_keypair, &kp2);
tt_assert(buf);
buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
@@ -301,6 +371,12 @@ test_dir_formats(void *arg)
tor_free(buf);
buf = router_dump_router_to_string(r2, pk1, NULL, NULL, NULL);
+
+ UNMOCK(get_configured_ports);
+ smartlist_free(mocked_configured_ports);
+ mocked_configured_ports = NULL;
+
+ /* Reset for later */
cp = buf;
rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
tt_assert(rp2);
@@ -315,6 +391,7 @@ test_dir_formats(void *arg)
CURVE25519_PUBKEY_LEN);
tt_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
tt_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
+ tt_assert(rp2->supports_tunnelled_dir_requests);
tt_int_op(smartlist_len(rp2->exit_policy),OP_EQ, 2);
@@ -514,7 +591,7 @@ test_dir_extrainfo_parsing(void *arg)
crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
tt_assert(! crypto_pk_read_public_key_from_string(pk, \
name##_KEY, strlen(name##_KEY))); \
- tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
+ tt_int_op(20,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
digestmap_set((digestmap_t*)map, d, ri); \
ri = NULL; \
} while (0)
@@ -580,6 +657,7 @@ test_dir_extrainfo_parsing(void *arg)
#undef CHECK_FAIL
done:
+ escaped(NULL);
extrainfo_free(ei);
routerinfo_free(ri);
digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
@@ -635,8 +713,8 @@ test_dir_parse_router_list(void *arg)
"9a651ee03b64325959e8f1b46f2b689b30750b4c");
/* Now tidy up */
- SMARTLIST_FOREACH(dest, routerinfo_t *, ri, routerinfo_free(ri));
- SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
+ SMARTLIST_FOREACH(dest, routerinfo_t *, rinfo, routerinfo_free(rinfo));
+ SMARTLIST_FOREACH(invalid, uint8_t *, dig, tor_free(dig));
smartlist_clear(dest);
smartlist_clear(invalid);
@@ -672,9 +750,9 @@ test_dir_parse_router_list(void *arg)
else
SMARTLIST_FOREACH(dest, extrainfo_t *, ei, extrainfo_free(ei));
smartlist_free(dest);
- SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
+ SMARTLIST_FOREACH(invalid, uint8_t *, dig, tor_free(dig));
smartlist_free(invalid);
- SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
+ SMARTLIST_FOREACH(chunks, char *, chunk, tor_free(chunk));
smartlist_free(chunks);
routerinfo_free(ri);
if (map) {
@@ -1100,7 +1178,7 @@ test_dir_fp_pairs(void *arg)
tt_mem_op(pair->second,OP_EQ, "Use AES-256 instead.", DIGEST_LEN);
done:
- SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
+ SMARTLIST_FOREACH(sl, fp_pair_t *, pair_to_free, tor_free(pair_to_free));
smartlist_free(sl);
}
@@ -1368,6 +1446,20 @@ test_dir_measured_bw_kb_cache(void *arg)
return;
}
+static char *
+my_dirvote_compute_params(smartlist_t *votes, int method,
+ int total_authorities)
+{
+ smartlist_t *s = dirvote_compute_params(votes, method, total_authorities);
+ tor_assert(s);
+ char *res = smartlist_join_strings(s, " ", 0, NULL);
+ SMARTLIST_FOREACH(s, char *, cp, tor_free(cp));
+ smartlist_free(s);
+ return res;
+}
+
+#define dirvote_compute_params my_dirvote_compute_params
+
static void
test_dir_param_voting(void *arg)
{
@@ -1477,12 +1569,66 @@ test_dir_param_voting(void *arg)
return;
}
-extern const char AUTHORITY_CERT_1[];
-extern const char AUTHORITY_SIGNKEY_1[];
-extern const char AUTHORITY_CERT_2[];
-extern const char AUTHORITY_SIGNKEY_2[];
-extern const char AUTHORITY_CERT_3[];
-extern const char AUTHORITY_SIGNKEY_3[];
+static void
+test_dir_param_voting_lookup(void *arg)
+{
+ (void)arg;
+ smartlist_t *lst = smartlist_new();
+
+ smartlist_split_string(lst,
+ "moomin=9 moomin=10 moomintroll=5 fred "
+ "jack= electricity=sdk opa=6z abc=9 abcd=99",
+ NULL, 0, 0);
+
+ tt_int_op(1000,
+ OP_EQ, dirvote_get_intermediate_param_value(lst, "ab", 1000));
+ tt_int_op(9, OP_EQ, dirvote_get_intermediate_param_value(lst, "abc", 1000));
+ tt_int_op(99, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "abcd", 1000));
+
+ /* moomin appears twice. That's a bug. */
+ tor_capture_bugs_(1);
+ tt_int_op(-100, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "moomin", -100));
+ tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
+ tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
+ "!(n_found > 1)");
+ tor_end_capture_bugs_();
+ /* There is no 'fred=', so that is treated as not existing. */
+ tt_int_op(-100, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "fred", -100));
+ /* jack is truncated */
+ tor_capture_bugs_(1);
+ tt_int_op(-100, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "jack", -100));
+ tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
+ tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
+ "!(! ok)");
+ tor_end_capture_bugs_();
+ /* electricity and opa aren't integers. */
+ tor_capture_bugs_(1);
+ tt_int_op(-100, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "electricity", -100));
+ tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
+ tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
+ "!(! ok)");
+ tor_end_capture_bugs_();
+
+ tor_capture_bugs_(1);
+ tt_int_op(-100, OP_EQ,
+ dirvote_get_intermediate_param_value(lst, "opa", -100));
+ tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
+ tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
+ "!(! ok)");
+ tor_end_capture_bugs_();
+
+ done:
+ SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
+ smartlist_free(lst);
+ tor_end_capture_bugs_();
+}
+
+#undef dirvote_compute_params
/** Helper: Test that two networkstatus_voter_info_t do in fact represent the
* same voting authority, and that they do in fact have all the same
@@ -1503,42 +1649,6 @@ test_same_voter(networkstatus_voter_info_t *v1,
;
}
-/** Helper: Make a new routerinfo containing the right information for a
- * given vote_routerstatus_t. */
-static routerinfo_t *
-generate_ri_from_rs(const vote_routerstatus_t *vrs)
-{
- routerinfo_t *r;
- const routerstatus_t *rs = &vrs->status;
- static time_t published = 0;
-
- r = tor_malloc_zero(sizeof(routerinfo_t));
- r->cert_expiration_time = TIME_MAX;
- memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
- memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
- DIGEST_LEN);
- r->cache_info.do_not_cache = 1;
- r->cache_info.routerlist_index = -1;
- r->cache_info.signed_descriptor_body =
- tor_strdup("123456789012345678901234567890123");
- r->cache_info.signed_descriptor_len =
- strlen(r->cache_info.signed_descriptor_body);
- r->exit_policy = smartlist_new();
- r->cache_info.published_on = ++published + time(NULL);
- if (rs->has_bandwidth) {
- /*
- * Multiply by 1000 because the routerinfo_t and the routerstatus_t
- * seem to use different units (*sigh*) and because we seem stuck on
- * icky and perverse decimal kilobytes (*double sigh*) - see
- * router_get_advertised_bandwidth_capped() of routerlist.c and
- * routerstatus_format_entry() of dirserv.c.
- */
- r->bandwidthrate = rs->bandwidth_kb * 1000;
- r->bandwidthcapacity = rs->bandwidth_kb * 1000;
- }
- return r;
-}
-
/** Helper: get a detached signatures document for one or two
* consensuses. */
static char *
@@ -1556,100 +1666,6 @@ get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
return r;
}
-/**
- * Generate a routerstatus for v3_networkstatus test
- */
-static vote_routerstatus_t *
-gen_routerstatus_for_v3ns(int idx, time_t now)
-{
- vote_routerstatus_t *vrs=NULL;
- routerstatus_t *rs;
- tor_addr_t addr_ipv6;
-
- switch (idx) {
- case 0:
- /* Generate the first routerstatus. */
- vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
- rs = &vrs->status;
- vrs->version = tor_strdup("0.1.2.14");
- rs->published_on = now-1500;
- strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
- memset(rs->identity_digest, 3, DIGEST_LEN);
- memset(rs->descriptor_digest, 78, DIGEST_LEN);
- rs->addr = 0x99008801;
- rs->or_port = 443;
- rs->dir_port = 8000;
- /* all flags but running cleared */
- rs->is_flagged_running = 1;
- break;
- case 1:
- /* Generate the second routerstatus. */
- vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
- rs = &vrs->status;
- vrs->version = tor_strdup("0.2.0.5");
- rs->published_on = now-1000;
- strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
- memset(rs->identity_digest, 5, DIGEST_LEN);
- memset(rs->descriptor_digest, 77, DIGEST_LEN);
- rs->addr = 0x99009901;
- rs->or_port = 443;
- rs->dir_port = 0;
- tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
- tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
- rs->ipv6_orport = 4711;
- rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
- rs->is_valid = rs->is_possible_guard = 1;
- break;
- case 2:
- /* Generate the third routerstatus. */
- vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
- rs = &vrs->status;
- vrs->version = tor_strdup("0.1.0.3");
- rs->published_on = now-1000;
- strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
- memset(rs->identity_digest, 33, DIGEST_LEN);
- memset(rs->descriptor_digest, 79, DIGEST_LEN);
- rs->addr = 0xAA009901;
- rs->or_port = 400;
- rs->dir_port = 9999;
- rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
- rs->is_flagged_running = rs->is_valid =
- rs->is_possible_guard = 1;
- break;
- case 3:
- /* Generate a fourth routerstatus that is not running. */
- vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
- rs = &vrs->status;
- vrs->version = tor_strdup("0.1.6.3");
- rs->published_on = now-1000;
- strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
- memset(rs->identity_digest, 34, DIGEST_LEN);
- memset(rs->descriptor_digest, 47, DIGEST_LEN);
- rs->addr = 0xC0000203;
- rs->or_port = 500;
- rs->dir_port = 1999;
- /* Running flag (and others) cleared */
- break;
- case 4:
- /* No more for this test; return NULL */
- vrs = NULL;
- break;
- default:
- /* Shouldn't happen */
- tt_assert(0);
- }
- if (vrs) {
- vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
- tor_asprintf(&vrs->microdesc->microdesc_hash_line,
- "m 9,10,11,12,13,14,15,16,17 "
- "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
- idx);
- }
-
- done:
- return vrs;
-}
-
/** Apply tweaks to the vote list for each voter */
static int
vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
@@ -1681,7 +1697,7 @@ vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
vrs = smartlist_get(v->routerstatus_list, 0);
memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
tt_assert(router_add_to_routerlist(
- generate_ri_from_rs(vrs), &msg,0,0) >= 0);
+ dir_common_generate_ri_from_rs(vrs), &msg,0,0) >= 0);
}
}
@@ -1720,8 +1736,8 @@ test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
tt_int_op(rs->addr,OP_EQ, 0x99008801);
tt_int_op(rs->or_port,OP_EQ, 443);
tt_int_op(rs->dir_port,OP_EQ, 8000);
- /* no flags except "running" (16) and "v2dir" (64) */
- tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(80));
+ /* no flags except "running" (16) and "v2dir" (64) and "valid" (128) */
+ tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(0xd0));
} else if (tor_memeq(rs->identity_digest,
"\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
"\x5\x5\x5\x5",
@@ -1746,11 +1762,11 @@ test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
if (voter == 1) {
- /* all except "authority" (1) and "v2dir" (64) */
- tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(190));
+ /* all except "authority" (1) */
+ tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(254));
} else {
- /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) - v2dir(256) */
- tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(718));
+ /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
+ tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(974));
}
} else if (tor_memeq(rs->identity_digest,
"\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
@@ -1818,8 +1834,9 @@ test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
tt_assert(!rs->is_stable);
/* (If it wasn't running it wouldn't be here) */
tt_assert(rs->is_flagged_running);
- tt_assert(!rs->is_valid);
+ tt_assert(rs->is_valid);
tt_assert(!rs->is_named);
+ tt_assert(rs->is_v2_dir);
/* XXXX check version */
} else if (tor_memeq(rs->identity_digest,
"\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
@@ -1845,6 +1862,7 @@ test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
tt_assert(rs->is_stable);
tt_assert(rs->is_flagged_running);
tt_assert(rs->is_valid);
+ tt_assert(rs->is_v2_dir);
tt_assert(!rs->is_named);
/* XXXX check version */
} else {
@@ -1856,6 +1874,15 @@ test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
return;
}
+static authority_cert_t *mock_cert;
+
+static authority_cert_t *
+get_my_v3_authority_cert_m(void)
+{
+ tor_assert(mock_cert);
+ return mock_cert;
+}
+
/** Run a unit tests for generating and parsing networkstatuses, with
* the supply test fns. */
static void
@@ -1869,7 +1896,6 @@ test_a_networkstatus(
authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
crypto_pk_t *sign_skey_leg1=NULL;
- const char *msg=NULL;
/*
* Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
* returns non-zero, it changed net_params and we should skip the tests for
@@ -1885,8 +1911,7 @@ test_a_networkstatus(
vote_routerstatus_t *vrs;
routerstatus_t *rs;
int idx, n_rs, n_vrs;
- char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL,
- *cp=NULL;
+ char *consensus_text=NULL, *cp=NULL;
smartlist_t *votes = smartlist_new();
/* For generating the two other consensuses. */
@@ -1901,8 +1926,10 @@ test_a_networkstatus(
tt_assert(rs_test);
tt_assert(vrs_test);
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+
/* Parse certificates and keys. */
- cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
tt_assert(cert1);
cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
tt_assert(cert2);
@@ -1912,6 +1939,7 @@ test_a_networkstatus(
sign_skey_2 = crypto_pk_new();
sign_skey_3 = crypto_pk_new();
sign_skey_leg1 = pk_generate(4);
+ sr_state_init(0, 0);
tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
AUTHORITY_SIGNKEY_1, -1));
@@ -1923,57 +1951,8 @@ test_a_networkstatus(
tt_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
tt_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
- /*
- * Set up a vote; generate it; try to parse it.
- */
- vote = tor_malloc_zero(sizeof(networkstatus_t));
- vote->type = NS_TYPE_VOTE;
- vote->published = now;
- vote->valid_after = now+1000;
- vote->fresh_until = now+2000;
- vote->valid_until = now+3000;
- vote->vote_seconds = 100;
- vote->dist_seconds = 200;
- vote->supported_methods = smartlist_new();
- smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
- vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
- vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
- vote->known_flags = smartlist_new();
- smartlist_split_string(vote->known_flags,
- "Authority Exit Fast Guard Running Stable V2Dir Valid",
- 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- vote->voters = smartlist_new();
- voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
- voter->nickname = tor_strdup("Voter1");
- voter->address = tor_strdup("1.2.3.4");
- voter->addr = 0x01020304;
- voter->dir_port = 80;
- voter->or_port = 9000;
- voter->contact = tor_strdup("voter@example.com");
- crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
- smartlist_add(vote->voters, voter);
- vote->cert = authority_cert_dup(cert1);
- vote->net_params = smartlist_new();
- smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
- NULL, 0, 0);
- vote->routerstatus_list = smartlist_new();
- /* add routerstatuses */
- idx = 0;
- do {
- vrs = vrs_gen(idx, now);
- if (vrs) {
- smartlist_add(vote->routerstatus_list, vrs);
- tt_assert(router_add_to_routerlist(generate_ri_from_rs(vrs),
- &msg,0,0)>=0);
- ++idx;
- }
- } while (vrs);
- n_vrs = idx;
-
- /* dump the vote and try to parse it. */
- v1_text = format_networkstatus_vote(sign_skey_1, vote);
- tt_assert(v1_text);
- v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
+ tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
+ &v1, &n_vrs, now, 1));
tt_assert(v1);
/* Make sure the parsed thing was right. */
@@ -2000,6 +1979,8 @@ test_a_networkstatus(
tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
tor_free(cp);
tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
+ networkstatus_vote_free(vote);
+ vote = NULL;
if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
@@ -2011,33 +1992,10 @@ test_a_networkstatus(
}
/* Generate second vote. It disagrees on some of the times,
- * and doesn't list versions, and knows some crazy flags */
- vote->published = now+1;
- vote->fresh_until = now+3005;
- vote->dist_seconds = 300;
- authority_cert_free(vote->cert);
- vote->cert = authority_cert_dup(cert2);
- SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
- smartlist_clear(vote->net_params);
- smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
- NULL, 0, 0);
- tor_free(vote->client_versions);
- tor_free(vote->server_versions);
- voter = smartlist_get(vote->voters, 0);
- tor_free(voter->nickname);
- tor_free(voter->address);
- voter->nickname = tor_strdup("Voter2");
- voter->address = tor_strdup("2.3.4.5");
- voter->addr = 0x02030405;
- crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
- smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
- smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
- smartlist_sort_strings(vote->known_flags);
-
- /* generate and parse v2. */
- v2_text = format_networkstatus_vote(sign_skey_2, vote);
- tt_assert(v2_text);
- v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
+ * and doesn't list versions, and knows some crazy flags.
+ * Generate and parse v2. */
+ tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
+ &v2, &n_vrs, now, 1));
tt_assert(v2);
if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
@@ -2055,34 +2013,12 @@ test_a_networkstatus(
tt_assert(vrs);
vrs_test(vrs, 2, now);
}
+ networkstatus_vote_free(vote);
+ vote = NULL;
- /* Generate the third vote. */
- vote->published = now;
- vote->fresh_until = now+2003;
- vote->dist_seconds = 250;
- authority_cert_free(vote->cert);
- vote->cert = authority_cert_dup(cert3);
- SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
- smartlist_clear(vote->net_params);
- smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
- NULL, 0, 0);
- smartlist_add(vote->supported_methods, tor_strdup("4"));
- vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
- vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
- voter = smartlist_get(vote->voters, 0);
- tor_free(voter->nickname);
- tor_free(voter->address);
- voter->nickname = tor_strdup("Voter3");
- voter->address = tor_strdup("3.4.5.6");
- voter->addr = 0x03040506;
- crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
- /* This one has a legacy id. */
- memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
-
- v3_text = format_networkstatus_vote(sign_skey_3, vote);
- tt_assert(v3_text);
-
- v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
+ /* Generate the third vote with a legacy id. */
+ tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
+ &v3, &n_vrs, now, 1));
tt_assert(v3);
if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
@@ -2138,9 +2074,9 @@ test_a_networkstatus(
tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
/* The voter id digests should be in this order. */
- tt_assert(memcmp(cert2->cache_info.identity_digest,
+ tt_assert(fast_memcmp(cert2->cache_info.identity_digest,
cert1->cache_info.identity_digest,DIGEST_LEN)<0);
- tt_assert(memcmp(cert1->cache_info.identity_digest,
+ tt_assert(fast_memcmp(cert1->cache_info.identity_digest,
cert3->cache_info.identity_digest,DIGEST_LEN)<0);
test_same_voter(smartlist_get(con->voters, 1),
smartlist_get(v2->voters, 0));
@@ -2153,12 +2089,20 @@ test_a_networkstatus(
/* Check the routerstatuses. */
n_rs = smartlist_len(con->routerstatus_list);
+ tt_assert(n_rs);
for (idx = 0; idx < n_rs; ++idx) {
rs = smartlist_get(con->routerstatus_list, idx);
tt_assert(rs);
rs_test(rs, now);
}
+ n_rs = smartlist_len(con_md->routerstatus_list);
+ tt_assert(n_rs);
+ for (idx = 0; idx < n_rs; ++idx) {
+ rs = smartlist_get(con_md->routerstatus_list, idx);
+ tt_assert(rs);
+ }
+
/* Check signatures. the first voter is a pseudo-entry with a legacy key.
* The second one hasn't signed. The fourth one has signed: validate it. */
voter = smartlist_get(con->voters, 1);
@@ -2215,11 +2159,13 @@ test_a_networkstatus(
tt_assert(con_md3);
/* All three should have the same digest. */
- tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(digests_t));
- tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(digests_t));
+ tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(common_digests_t));
+ tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(common_digests_t));
- tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests, sizeof(digests_t));
- tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests, sizeof(digests_t));
+ tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests,
+ sizeof(common_digests_t));
+ tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests,
+ sizeof(common_digests_t));
/* Extract a detached signature from con3. */
detached_text1 = get_detached_sigs(con3, con_md3);
@@ -2233,7 +2179,7 @@ test_a_networkstatus(
tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
{
- digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
+ common_digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
tt_assert(dsig_digests);
tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
@@ -2309,38 +2255,22 @@ test_a_networkstatus(
done:
tor_free(cp);
smartlist_free(votes);
- tor_free(v1_text);
- tor_free(v2_text);
- tor_free(v3_text);
tor_free(consensus_text);
tor_free(consensus_text_md);
- if (vote)
- networkstatus_vote_free(vote);
- if (v1)
- networkstatus_vote_free(v1);
- if (v2)
- networkstatus_vote_free(v2);
- if (v3)
- networkstatus_vote_free(v3);
- if (con)
- networkstatus_vote_free(con);
- if (con_md)
- networkstatus_vote_free(con_md);
- if (sign_skey_1)
- crypto_pk_free(sign_skey_1);
- if (sign_skey_2)
- crypto_pk_free(sign_skey_2);
- if (sign_skey_3)
- crypto_pk_free(sign_skey_3);
- if (sign_skey_leg1)
- crypto_pk_free(sign_skey_leg1);
- if (cert1)
- authority_cert_free(cert1);
- if (cert2)
- authority_cert_free(cert2);
- if (cert3)
- authority_cert_free(cert3);
+ networkstatus_vote_free(vote);
+ networkstatus_vote_free(v1);
+ networkstatus_vote_free(v2);
+ networkstatus_vote_free(v3);
+ networkstatus_vote_free(con);
+ networkstatus_vote_free(con_md);
+ crypto_pk_free(sign_skey_1);
+ crypto_pk_free(sign_skey_2);
+ crypto_pk_free(sign_skey_3);
+ crypto_pk_free(sign_skey_leg1);
+ authority_cert_free(cert1);
+ authority_cert_free(cert2);
+ authority_cert_free(cert3);
tor_free(consensus_text2);
tor_free(consensus_text3);
@@ -2348,18 +2278,13 @@ test_a_networkstatus(
tor_free(consensus_text_md3);
tor_free(detached_text1);
tor_free(detached_text2);
- if (con2)
- networkstatus_vote_free(con2);
- if (con3)
- networkstatus_vote_free(con3);
- if (con_md2)
- networkstatus_vote_free(con_md2);
- if (con_md3)
- networkstatus_vote_free(con_md3);
- if (dsig1)
- ns_detached_signatures_free(dsig1);
- if (dsig2)
- ns_detached_signatures_free(dsig2);
+
+ networkstatus_vote_free(con2);
+ networkstatus_vote_free(con3);
+ networkstatus_vote_free(con_md2);
+ networkstatus_vote_free(con_md3);
+ ns_detached_signatures_free(dsig1);
+ ns_detached_signatures_free(dsig2);
}
/** Run unit tests for generating and parsing V3 consensus networkstatus
@@ -2368,7 +2293,7 @@ static void
test_dir_v3_networkstatus(void *arg)
{
(void)arg;
- test_a_networkstatus(gen_routerstatus_for_v3ns,
+ test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
vote_tweaks_for_v3ns,
test_vrs_for_v3ns,
test_consensus_for_v3ns,
@@ -2386,56 +2311,57 @@ test_dir_scale_bw(void *testdata)
1.0/7,
12.0,
24.0 };
- u64_dbl_t vals[8];
+ double vals_dbl[8];
+ uint64_t vals_u64[8];
uint64_t total;
int i;
(void) testdata;
for (i=0; i<8; ++i)
- vals[i].dbl = v[i];
+ vals_dbl[i] = v[i];
- scale_array_elements_to_u64(vals, 8, &total);
+ scale_array_elements_to_u64(vals_u64, vals_dbl, 8, &total);
tt_int_op((int)total, OP_EQ, 48);
total = 0;
for (i=0; i<8; ++i) {
- total += vals[i].u64;
+ total += vals_u64[i];
}
tt_assert(total >= (U64_LITERAL(1)<<60));
tt_assert(total <= (U64_LITERAL(1)<<62));
for (i=0; i<8; ++i) {
/* vals[2].u64 is the scaled value of 1.0 */
- double ratio = ((double)vals[i].u64) / vals[2].u64;
+ double ratio = ((double)vals_u64[i]) / vals_u64[2];
tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
}
/* test handling of no entries */
total = 1;
- scale_array_elements_to_u64(vals, 0, &total);
+ scale_array_elements_to_u64(vals_u64, vals_dbl, 0, &total);
tt_assert(total == 0);
/* make sure we don't read the array when we have no entries
* may require compiler flags to catch NULL dereferences */
total = 1;
- scale_array_elements_to_u64(NULL, 0, &total);
+ scale_array_elements_to_u64(NULL, NULL, 0, &total);
tt_assert(total == 0);
- scale_array_elements_to_u64(NULL, 0, NULL);
+ scale_array_elements_to_u64(NULL, NULL, 0, NULL);
/* test handling of zero totals */
total = 1;
- vals[0].dbl = 0.0;
- scale_array_elements_to_u64(vals, 1, &total);
+ vals_dbl[0] = 0.0;
+ scale_array_elements_to_u64(vals_u64, vals_dbl, 1, &total);
tt_assert(total == 0);
- tt_assert(vals[0].u64 == 0);
+ tt_assert(vals_u64[0] == 0);
- vals[0].dbl = 0.0;
- vals[1].dbl = 0.0;
- scale_array_elements_to_u64(vals, 2, NULL);
- tt_assert(vals[0].u64 == 0);
- tt_assert(vals[1].u64 == 0);
+ vals_dbl[0] = 0.0;
+ vals_dbl[1] = 0.0;
+ scale_array_elements_to_u64(vals_u64, vals_dbl, 2, NULL);
+ tt_assert(vals_u64[0] == 0);
+ tt_assert(vals_u64[1] == 0);
done:
;
@@ -2446,7 +2372,7 @@ test_dir_random_weighted(void *testdata)
{
int histogram[10];
uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
- u64_dbl_t inp[10];
+ uint64_t inp_u64[10];
int i, choice;
const int n = 50000;
double max_sq_error;
@@ -2456,12 +2382,12 @@ test_dir_random_weighted(void *testdata)
* in a scrambled order to make sure we don't depend on order. */
memset(histogram,0,sizeof(histogram));
for (i=0; i<10; ++i) {
- inp[i].u64 = vals[i];
+ inp_u64[i] = vals[i];
total += vals[i];
}
tt_u64_op(total, OP_EQ, 45);
for (i=0; i<n; ++i) {
- choice = choose_array_element_by_weight(inp, 10);
+ choice = choose_array_element_by_weight(inp_u64, 10);
tt_int_op(choice, OP_GE, 0);
tt_int_op(choice, OP_LT, 10);
histogram[choice]++;
@@ -2488,16 +2414,16 @@ test_dir_random_weighted(void *testdata)
/* Now try a singleton; do we choose it? */
for (i = 0; i < 100; ++i) {
- choice = choose_array_element_by_weight(inp, 1);
+ choice = choose_array_element_by_weight(inp_u64, 1);
tt_int_op(choice, OP_EQ, 0);
}
/* Now try an array of zeros. We should choose randomly. */
memset(histogram,0,sizeof(histogram));
for (i = 0; i < 5; ++i)
- inp[i].u64 = 0;
+ inp_u64[i] = 0;
for (i = 0; i < n; ++i) {
- choice = choose_array_element_by_weight(inp, 5);
+ choice = choose_array_element_by_weight(inp_u64, 5);
tt_int_op(choice, OP_GE, 0);
tt_int_op(choice, OP_LT, 5);
histogram[choice]++;
@@ -2965,6 +2891,7 @@ test_dir_fmt_control_ns(void *arg)
rs.is_fast = 1;
rs.is_flagged_running = 1;
rs.has_bandwidth = 1;
+ rs.is_v2_dir = 1;
rs.bandwidth_kb = 1000;
s = networkstatus_getinfo_helper_single(&rs);
@@ -3036,7 +2963,7 @@ test_dir_dirserv_set_routerstatus_testing(void *arg)
(void)arg;
/* Init options */
- mock_options = malloc(sizeof(or_options_t));
+ mock_options = tor_malloc(sizeof(or_options_t));
reset_options(mock_options, &mock_get_options_calls);
MOCK(get_options, mock_get_options);
@@ -3054,10 +2981,10 @@ test_dir_dirserv_set_routerstatus_testing(void *arg)
routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
/* Init routerstatuses */
- routerstatus_t *rs_a = malloc(sizeof(routerstatus_t));
+ routerstatus_t *rs_a = tor_malloc(sizeof(routerstatus_t));
reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
- routerstatus_t *rs_b = malloc(sizeof(routerstatus_t));
+ routerstatus_t *rs_b = tor_malloc(sizeof(routerstatus_t));
reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
/* Sanity check that routersets correspond to routerstatuses.
@@ -3242,7 +3169,7 @@ test_dir_dirserv_set_routerstatus_testing(void *arg)
tt_assert(rs_b->is_hs_dir == 1);
done:
- free(mock_options);
+ tor_free(mock_options);
mock_options = NULL;
UNMOCK(get_options);
@@ -3251,8 +3178,8 @@ test_dir_dirserv_set_routerstatus_testing(void *arg)
routerset_free(routerset_a);
routerset_free(routerset_none);
- free(rs_a);
- free(rs_b);
+ tor_free(rs_a);
+ tor_free(rs_b);
}
static void
@@ -3340,13 +3267,40 @@ static void
test_dir_fetch_type(void *arg)
{
(void)arg;
- tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
- NULL) == MICRODESC_DIRINFO);
- tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
- NULL) == BRIDGE_DIRINFO);
- tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
- "microdesc") == (V3_DIRINFO | MICRODESC_DIRINFO));
- done: ;
+
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
+ NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
+ NULL), OP_EQ, EXTRAINFO_DIRINFO | V3_DIRINFO);
+
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
+ NULL), OP_EQ, BRIDGE_DIRINFO);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC,
+ ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
+
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE,
+ ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
+ ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE,
+ ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
+
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
+ "microdesc"), OP_EQ, V3_DIRINFO|MICRODESC_DIRINFO);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
+ NULL), OP_EQ, V3_DIRINFO);
+
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
+ NULL), OP_EQ, MICRODESC_DIRINFO);
+
+ /* This will give a warning, because this function isn't supposed to be
+ * used for HS descriptors. */
+ setup_full_capture_of_logs(LOG_WARN);
+ tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
+ ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
+ expect_single_log_msg_containing("Unexpected purpose");
+ done:
+ teardown_capture_of_logs();
}
static void
@@ -3453,7 +3407,7 @@ test_dir_packages(void *arg)
ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
- /* Five votes for A ... all from the same guy. Three for B. */
+ /* Five votes for A ... all from the same authority. Three for B. */
ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
@@ -3494,12 +3448,2005 @@ test_dir_packages(void *arg)
tor_free(res);
}
-#define DIR_LEGACY(name) \
+static void
+test_dir_download_status_schedule(void *arg)
+{
+ (void)arg;
+ download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
+ DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_FAILURE,
+ DL_SCHED_DETERMINISTIC, 0, 0 };
+ download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_CONSENSUS,
+ DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_ATTEMPT,
+ DL_SCHED_DETERMINISTIC, 0, 0 };
+ download_status_t dls_bridge = { 0, 0, 0, DL_SCHED_BRIDGE,
+ DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_FAILURE,
+ DL_SCHED_DETERMINISTIC, 0, 0 };
+ int increment = -1;
+ int expected_increment = -1;
+ time_t current_time = time(NULL);
+ int delay1 = -1;
+ int delay2 = -1;
+ smartlist_t *schedule = smartlist_new();
+
+ /* Make a dummy schedule */
+ smartlist_add(schedule, (void *)&delay1);
+ smartlist_add(schedule, (void *)&delay2);
+
+ /* check a range of values */
+ delay1 = 1000;
+ increment = download_status_schedule_get_delay(&dls_failure,
+ schedule,
+ 0, INT_MAX,
+ TIME_MIN);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_failure.next_attempt_at == TIME_MIN + expected_increment);
+
+ delay1 = INT_MAX;
+ increment = download_status_schedule_get_delay(&dls_failure,
+ schedule,
+ 0, INT_MAX,
+ -1);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_failure.next_attempt_at == TIME_MAX);
+
+ delay1 = 0;
+ increment = download_status_schedule_get_delay(&dls_attempt,
+ schedule,
+ 0, INT_MAX,
+ 0);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_attempt.next_attempt_at == 0 + expected_increment);
+
+ delay1 = 1000;
+ increment = download_status_schedule_get_delay(&dls_attempt,
+ schedule,
+ 0, INT_MAX,
+ 1);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_attempt.next_attempt_at == 1 + expected_increment);
+
+ delay1 = INT_MAX;
+ increment = download_status_schedule_get_delay(&dls_bridge,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
+
+ delay1 = 1;
+ increment = download_status_schedule_get_delay(&dls_bridge,
+ schedule,
+ 0, INT_MAX,
+ TIME_MAX);
+ expected_increment = delay1;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
+
+ /* see what happens when we reach the end */
+ dls_attempt.n_download_attempts++;
+ dls_bridge.n_download_failures++;
+
+ delay2 = 100;
+ increment = download_status_schedule_get_delay(&dls_attempt,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = delay2;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
+
+ delay2 = 1;
+ increment = download_status_schedule_get_delay(&dls_bridge,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = delay2;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
+
+ /* see what happens when we try to go off the end */
+ dls_attempt.n_download_attempts++;
+ dls_bridge.n_download_failures++;
+
+ delay2 = 5;
+ increment = download_status_schedule_get_delay(&dls_attempt,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = delay2;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
+
+ delay2 = 17;
+ increment = download_status_schedule_get_delay(&dls_bridge,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = delay2;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
+
+ /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
+ dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
+ dls_bridge.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
+
+ delay2 = 35;
+ increment = download_status_schedule_get_delay(&dls_attempt,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = INT_MAX;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_attempt.next_attempt_at == TIME_MAX);
+
+ delay2 = 99;
+ increment = download_status_schedule_get_delay(&dls_bridge,
+ schedule,
+ 0, INT_MAX,
+ current_time);
+ expected_increment = INT_MAX;
+ tt_assert(increment == expected_increment);
+ tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
+
+ done:
+ /* the pointers in schedule are allocated on the stack */
+ smartlist_free(schedule);
+}
+
+static void
+test_dir_download_status_random_backoff(void *arg)
+{
+ download_status_t dls_random =
+ { 0, 0, 0, DL_SCHED_GENERIC, DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
+ int increment = -1;
+ int old_increment;
+ time_t current_time = time(NULL);
+ const int min_delay = 0;
+ const int max_delay = 1000000;
+
+ (void)arg;
+
+ /* Check the random backoff cases */
+ old_increment = 0;
+ do {
+ increment = download_status_schedule_get_delay(&dls_random,
+ NULL,
+ min_delay, max_delay,
+ current_time);
+ /* Test */
+ tt_int_op(increment, OP_GE, min_delay);
+ tt_int_op(increment, OP_LE, max_delay);
+ tt_int_op(increment, OP_GE, old_increment);
+ /* We at most quadruple, and maybe add one */
+ tt_int_op(increment, OP_LE, 4 * old_increment + 1);
+
+ /* Advance */
+ current_time += increment;
+ ++(dls_random.n_download_attempts);
+ ++(dls_random.n_download_failures);
+
+ /* Try another maybe */
+ old_increment = increment;
+ } while (increment < max_delay);
+
+ done:
+ return;
+}
+
+static void
+test_dir_download_status_increment(void *arg)
+{
+ (void)arg;
+ download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
+ DL_WANT_AUTHORITY,
+ DL_SCHED_INCREMENT_FAILURE,
+ DL_SCHED_DETERMINISTIC, 0, 0 };
+ download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_BRIDGE,
+ DL_WANT_ANY_DIRSERVER,
+ DL_SCHED_INCREMENT_ATTEMPT,
+ DL_SCHED_DETERMINISTIC, 0, 0 };
+ int delay0 = -1;
+ int delay1 = -1;
+ int delay2 = -1;
+ smartlist_t *schedule = smartlist_new();
+ or_options_t test_options;
+ time_t next_at = TIME_MAX;
+ time_t current_time = time(NULL);
+
+ /* Provide some values for the schedule */
+ delay0 = 10;
+ delay1 = 99;
+ delay2 = 20;
+
+ /* Make the schedule */
+ smartlist_add(schedule, (void *)&delay0);
+ smartlist_add(schedule, (void *)&delay1);
+ smartlist_add(schedule, (void *)&delay2);
+
+ /* Put it in the options */
+ mock_options = &test_options;
+ reset_options(mock_options, &mock_get_options_calls);
+ mock_options->TestingClientDownloadSchedule = schedule;
+ mock_options->TestingBridgeDownloadSchedule = schedule;
+
+ MOCK(get_options, mock_get_options);
+
+ /* Check that a failure reset works */
+ mock_get_options_calls = 0;
+ download_status_reset(&dls_failure);
+ /* we really want to test that it's equal to time(NULL) + delay0, but that's
+ * an unrealiable test, because time(NULL) might change. */
+ tt_assert(download_status_get_next_attempt_at(&dls_failure)
+ >= current_time + delay0);
+ tt_assert(download_status_get_next_attempt_at(&dls_failure)
+ != TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* avoid timing inconsistencies */
+ dls_failure.next_attempt_at = current_time + delay0;
+
+ /* check that a reset schedule becomes ready at the right time */
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay0 - 1,
+ 1) == 0);
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay0,
+ 1) == 1);
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay0 + 1,
+ 1) == 1);
+
+ /* Check that a failure increment works */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
+ current_time);
+ tt_assert(next_at == current_time + delay1);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 1);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 1);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* check that an incremented schedule becomes ready at the right time */
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay1 - 1,
+ 1) == 0);
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay1,
+ 1) == 1);
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay1 + 1,
+ 1) == 1);
+
+ /* check that a schedule isn't ready if it's had too many failures */
+ tt_assert(download_status_is_ready(&dls_failure,
+ current_time + delay1 + 10,
+ 0) == 0);
+
+ /* Check that failure increments do happen on 503 for clients, and
+ * attempt increments do too. */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_failure(&dls_failure, 503, "test", 0,
+ current_time);
+ tt_i64_op(next_at, ==, current_time + delay2);
+ tt_int_op(download_status_get_n_failures(&dls_failure), ==, 2);
+ tt_int_op(download_status_get_n_attempts(&dls_failure), ==, 2);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check that failure increments do happen on 503 for servers */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_failure(&dls_failure, 503, "test", 1,
+ current_time);
+ tt_assert(next_at == current_time + delay2);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 3);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 3);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check what happens when we run off the end of the schedule */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
+ current_time);
+ tt_assert(next_at == current_time + delay2);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 4);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 4);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check what happens when we hit the failure limit */
+ mock_get_options_calls = 0;
+ download_status_mark_impossible(&dls_failure);
+ next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
+ current_time);
+ tt_assert(next_at == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_failure)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(download_status_get_n_attempts(&dls_failure)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check that a failure reset doesn't reset at the limit */
+ mock_get_options_calls = 0;
+ download_status_reset(&dls_failure);
+ tt_assert(download_status_get_next_attempt_at(&dls_failure)
+ == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_failure)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(download_status_get_n_attempts(&dls_failure)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(mock_get_options_calls == 0);
+
+ /* Check that a failure reset resets just before the limit */
+ mock_get_options_calls = 0;
+ dls_failure.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
+ dls_failure.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
+ download_status_reset(&dls_failure);
+ /* we really want to test that it's equal to time(NULL) + delay0, but that's
+ * an unrealiable test, because time(NULL) might change. */
+ tt_assert(download_status_get_next_attempt_at(&dls_failure)
+ >= current_time + delay0);
+ tt_assert(download_status_get_next_attempt_at(&dls_failure)
+ != TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check that failure increments do happen on attempt-based schedules,
+ * but that the retry is set at the end of time */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_failure(&dls_attempt, 404, "test", 0,
+ current_time);
+ tt_assert(next_at == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 1);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
+ tt_assert(mock_get_options_calls == 0);
+
+ /* Check that an attempt reset works */
+ mock_get_options_calls = 0;
+ download_status_reset(&dls_attempt);
+ /* we really want to test that it's equal to time(NULL) + delay0, but that's
+ * an unrealiable test, because time(NULL) might change. */
+ tt_assert(download_status_get_next_attempt_at(&dls_attempt)
+ >= current_time + delay0);
+ tt_assert(download_status_get_next_attempt_at(&dls_attempt)
+ != TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* avoid timing inconsistencies */
+ dls_attempt.next_attempt_at = current_time + delay0;
+
+ /* check that a reset schedule becomes ready at the right time */
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay0 - 1,
+ 1) == 0);
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay0,
+ 1) == 1);
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay0 + 1,
+ 1) == 1);
+
+ /* Check that an attempt increment works */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_attempt(&dls_attempt, "test",
+ current_time);
+ tt_assert(next_at == current_time + delay1);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 1);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* check that an incremented schedule becomes ready at the right time */
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay1 - 1,
+ 1) == 0);
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay1,
+ 1) == 1);
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay1 + 1,
+ 1) == 1);
+
+ /* check that a schedule isn't ready if it's had too many attempts */
+ tt_assert(download_status_is_ready(&dls_attempt,
+ current_time + delay1 + 10,
+ 0) == 0);
+
+ /* Check what happens when we reach then run off the end of the schedule */
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_attempt(&dls_attempt, "test",
+ current_time);
+ tt_assert(next_at == current_time + delay2);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 2);
+ tt_assert(mock_get_options_calls >= 1);
+
+ mock_get_options_calls = 0;
+ next_at = download_status_increment_attempt(&dls_attempt, "test",
+ current_time);
+ tt_assert(next_at == current_time + delay2);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 3);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check what happens when we hit the attempt limit */
+ mock_get_options_calls = 0;
+ download_status_mark_impossible(&dls_attempt);
+ next_at = download_status_increment_attempt(&dls_attempt, "test",
+ current_time);
+ tt_assert(next_at == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_attempt)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(download_status_get_n_attempts(&dls_attempt)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check that an attempt reset doesn't reset at the limit */
+ mock_get_options_calls = 0;
+ download_status_reset(&dls_attempt);
+ tt_assert(download_status_get_next_attempt_at(&dls_attempt)
+ == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_attempt)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(download_status_get_n_attempts(&dls_attempt)
+ == IMPOSSIBLE_TO_DOWNLOAD);
+ tt_assert(mock_get_options_calls == 0);
+
+ /* Check that an attempt reset resets just before the limit */
+ mock_get_options_calls = 0;
+ dls_attempt.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
+ dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
+ download_status_reset(&dls_attempt);
+ /* we really want to test that it's equal to time(NULL) + delay0, but that's
+ * an unrealiable test, because time(NULL) might change. */
+ tt_assert(download_status_get_next_attempt_at(&dls_attempt)
+ >= current_time + delay0);
+ tt_assert(download_status_get_next_attempt_at(&dls_attempt)
+ != TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
+ tt_assert(mock_get_options_calls >= 1);
+
+ /* Check that attempt increments don't happen on failure-based schedules,
+ * and that the attempt is set at the end of time */
+ mock_get_options_calls = 0;
+ setup_full_capture_of_logs(LOG_WARN);
+ next_at = download_status_increment_attempt(&dls_failure, "test",
+ current_time);
+ expect_single_log_msg_containing(
+ "Tried to launch an attempt-based connection on a failure-based "
+ "schedule.");
+ teardown_capture_of_logs();
+ tt_assert(next_at == TIME_MAX);
+ tt_assert(download_status_get_n_failures(&dls_failure) == 0);
+ tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
+ tt_assert(mock_get_options_calls == 0);
+
+ done:
+ /* the pointers in schedule are allocated on the stack */
+ smartlist_free(schedule);
+ UNMOCK(get_options);
+ mock_options = NULL;
+ mock_get_options_calls = 0;
+ teardown_capture_of_logs();
+}
+
+static void
+test_dir_authdir_type_to_string(void *data)
+{
+ (void)data;
+ char *res;
+
+ tt_str_op(res = authdir_type_to_string(NO_DIRINFO), OP_EQ,
+ "[Not an authority]");
+ tor_free(res);
+
+ tt_str_op(res = authdir_type_to_string(EXTRAINFO_DIRINFO), OP_EQ,
+ "[Not an authority]");
+ tor_free(res);
+
+ tt_str_op(res = authdir_type_to_string(MICRODESC_DIRINFO), OP_EQ,
+ "[Not an authority]");
+ tor_free(res);
+
+ tt_str_op(res = authdir_type_to_string(V3_DIRINFO), OP_EQ, "V3");
+ tor_free(res);
+
+ tt_str_op(res = authdir_type_to_string(BRIDGE_DIRINFO), OP_EQ, "Bridge");
+ tor_free(res);
+
+ tt_str_op(res = authdir_type_to_string(
+ V3_DIRINFO | BRIDGE_DIRINFO | EXTRAINFO_DIRINFO), OP_EQ,
+ "V3, Bridge");
+ done:
+ tor_free(res);
+}
+
+static void
+test_dir_conn_purpose_to_string(void *data)
+{
+ (void)data;
+
+#define EXPECT_CONN_PURPOSE(purpose, expected) \
+ tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
+
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR, "server descriptor upload");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE, "server vote upload");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES,
+ "consensus signature upload");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC, "server descriptor fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO, "extra-info fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS,
+ "consensus network-status fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE, "authority cert fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE, "status vote fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
+ "consensus signature fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2,
+ "hidden-service v2 descriptor fetch");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
+ "hidden-service v2 descriptor upload");
+ EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
+
+ /* This will give a warning, because there is no purpose 1024. */
+ setup_full_capture_of_logs(LOG_WARN);
+ EXPECT_CONN_PURPOSE(1024, "(unknown)");
+ expect_single_log_msg_containing("Called with unknown purpose 1024");
+
+ done:
+ teardown_capture_of_logs();
+}
+
+NS_DECL(int,
+public_server_mode, (const or_options_t *options));
+
+static int
+NS(public_server_mode)(const or_options_t *options)
+{
+ (void)options;
+
+ if (CALLED(public_server_mode)++ == 0) {
+ return 1;
+ }
+
+ return 0;
+}
+
+static void
+test_dir_should_use_directory_guards(void *data)
+{
+ or_options_t *options;
+ char *errmsg = NULL;
+ (void)data;
+
+ NS_MOCK(public_server_mode);
+
+ options = options_new();
+ options_init(options);
+
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 1);
+
+ options->UseEntryGuardsAsDirGuards = 1;
+ options->UseEntryGuards = 1;
+ options->DownloadExtraInfo = 0;
+ options->FetchDirInfoEarly = 0;
+ options->FetchDirInfoExtraEarly = 0;
+ options->FetchUselessDescriptors = 0;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 1);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 2);
+
+ options->UseEntryGuards = 0;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 3);
+ options->UseEntryGuards = 1;
+
+ options->UseEntryGuardsAsDirGuards = 0;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 4);
+ options->UseEntryGuardsAsDirGuards = 1;
+
+ options->DownloadExtraInfo = 1;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 5);
+ options->DownloadExtraInfo = 0;
+
+ options->FetchDirInfoEarly = 1;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 6);
+ options->FetchDirInfoEarly = 0;
+
+ options->FetchDirInfoExtraEarly = 1;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 7);
+ options->FetchDirInfoExtraEarly = 0;
+
+ options->FetchUselessDescriptors = 1;
+ tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
+ tt_int_op(CALLED(public_server_mode), OP_EQ, 8);
+ options->FetchUselessDescriptors = 0;
+
+ done:
+ NS_UNMOCK(public_server_mode);
+ or_options_free(options);
+ tor_free(errmsg);
+}
+
+NS_DECL(void,
+directory_initiate_command_routerstatus, (const routerstatus_t *status,
+ uint8_t dir_purpose,
+ uint8_t router_purpose,
+ dir_indirection_t indirection,
+ const char *resource,
+ const char *payload,
+ size_t payload_len,
+ time_t if_modified_since));
+
+static void
+test_dir_should_not_init_request_to_ourselves(void *data)
+{
+ char digest[DIGEST_LEN];
+ dir_server_t *ourself = NULL;
+ crypto_pk_t *key = pk_generate(2);
+ (void) data;
+
+ NS_MOCK(directory_initiate_command_routerstatus);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ set_server_identity_key(key);
+ crypto_pk_get_digest(key, (char*) &digest);
+ ourself = trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
+ NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+
+ tt_assert(ourself);
+ dir_server_add(ourself);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
+ NULL);
+
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
+
+ done:
+ NS_UNMOCK(directory_initiate_command_routerstatus);
+ clear_dir_servers();
+ routerlist_free_all();
+ crypto_pk_free(key);
+}
+
+static void
+test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data)
+{
+ dir_server_t *ds = NULL;
+ dirinfo_type_t dirinfo_type = BRIDGE_DIRINFO | EXTRAINFO_DIRINFO \
+ | MICRODESC_DIRINFO;
+ (void) data;
+
+ NS_MOCK(directory_initiate_command_routerstatus);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
+ "12345678901234567890", NULL, dirinfo_type, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
+ NULL);
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
+
+ done:
+ NS_UNMOCK(directory_initiate_command_routerstatus);
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static void
+test_dir_should_init_request_to_dir_auths(void *data)
+{
+ dir_server_t *ds = NULL;
+ (void) data;
+
+ NS_MOCK(directory_initiate_command_routerstatus);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
+ "12345678901234567890", NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 1);
+
+ directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
+ NULL);
+ tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 2);
+
+ done:
+ NS_UNMOCK(directory_initiate_command_routerstatus);
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+void
+NS(directory_initiate_command_routerstatus)(const routerstatus_t *status,
+ uint8_t dir_purpose,
+ uint8_t router_purpose,
+ dir_indirection_t indirection,
+ const char *resource,
+ const char *payload,
+ size_t payload_len,
+ time_t if_modified_since)
+{
+ (void)status;
+ (void)dir_purpose;
+ (void)router_purpose;
+ (void)indirection;
+ (void)resource;
+ (void)payload;
+ (void)payload_len;
+ (void)if_modified_since;
+ CALLED(directory_initiate_command_routerstatus)++;
+}
+
+static void
+test_dir_choose_compression_level(void* data)
+{
+ (void)data;
+
+ /* It starts under_memory_pressure */
+ tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);
+
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
+ tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
+ tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
+
+ /* Reset under_memory_pressure timer */
+ cell_queues_check_size();
+ tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);
+
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
+ tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
+
+ done: ;
+}
+
+/*
+ * Mock check_private_dir(), and always succeed - no need to actually
+ * look at or create anything on the filesystem.
+ */
+
+static int
+mock_check_private_dir(const char *dirname, cpd_check_t check,
+ const char *effective_user)
+{
+ (void)dirname;
+ (void)check;
+ (void)effective_user;
+
+ return 0;
+}
+
+/*
+ * This really mocks options_get_datadir_fname2_suffix(), but for testing
+ * dump_desc(), we only care about get_datadir_fname(sub1), which is defined
+ * in config.h as:
+ *
+ * options_get_datadir_fname2_suffix(get_options(), sub1, NULL, NULL)
+ */
+
+static char *
+mock_get_datadir_fname(const or_options_t *options,
+ const char *sub1, const char *sub2,
+ const char *suffix)
+{
+ char *rv = NULL;
+
+ /*
+ * Assert we were called like get_datadir_fname2() or get_datadir_fname(),
+ * since that's all we implement here.
+ */
+ tt_assert(options != NULL);
+ tt_assert(sub1 != NULL);
+ /*
+ * No particular assertions about sub2, since we could be in the
+ * get_datadir_fname() or get_datadir_fname2() case.
+ */
+ tt_assert(suffix == NULL);
+
+ /* Just duplicate the basename and return it for this mock */
+ if (sub2) {
+ /* If we have sub2, it's the basename, otherwise sub1 */
+ rv = tor_strdup(sub2);
+ } else {
+ rv = tor_strdup(sub1);
+ }
+
+ done:
+ return rv;
+}
+
+static char *last_unlinked_path = NULL;
+static int unlinked_count = 0;
+
+static void
+mock_unlink_reset(void)
+{
+ tor_free(last_unlinked_path);
+ unlinked_count = 0;
+}
+
+static int
+mock_unlink(const char *path)
+{
+ tt_assert(path != NULL);
+
+ tor_free(last_unlinked_path);
+ last_unlinked_path = tor_strdup(path);
+ ++unlinked_count;
+
+ done:
+ return 0;
+}
+
+static char *last_write_str_path = NULL;
+static uint8_t last_write_str_hash[DIGEST256_LEN];
+static int write_str_count = 0;
+
+static void
+mock_write_str_to_file_reset(void)
+{
+ tor_free(last_write_str_path);
+ write_str_count = 0;
+}
+
+static int
+mock_write_str_to_file(const char *path, const char *str, int bin)
+{
+ size_t len;
+ uint8_t hash[DIGEST256_LEN];
+
+ (void)bin;
+
+ tt_assert(path != NULL);
+ tt_assert(str != NULL);
+
+ len = strlen(str);
+ crypto_digest256((char *)hash, str, len, DIGEST_SHA256);
+
+ tor_free(last_write_str_path);
+ last_write_str_path = tor_strdup(path);
+ memcpy(last_write_str_hash, hash, sizeof(last_write_str_hash));
+ ++write_str_count;
+
+ done:
+ return 0;
+}
+
+static void
+test_dir_dump_unparseable_descriptors(void *data)
+{
+ /*
+ * These bogus descriptors look nothing at all like real bogus descriptors
+ * we might see, but we're only testing dump_desc() here, not the parser.
+ */
+ const char *test_desc_type = "squamous";
+ /* strlen(test_desc_1) = 583 bytes */
+ const char *test_desc_1 =
+ "The most merciful thing in the world, I think, is the inability of the "
+ "human mind to correlate all its contents. We live on a placid island of"
+ " ignorance in the midst of black seas of infinity, and it was not meant"
+ " that we should voyage far. The sciences, each straining in its own dir"
+ "ection, have hitherto harmed us little; but some day the piecing togeth"
+ "er of dissociated knowledge will open up such terrifying vistas of real"
+ "ity, and of our frightful position therein, that we shall either go mad"
+ "from the revelation or flee from the light into the peace and safety of"
+ "a new dark age.";
+ uint8_t test_desc_1_hash[DIGEST256_LEN];
+ char test_desc_1_hash_str[HEX_DIGEST256_LEN+1];
+ /* strlen(test_desc_2) = 650 bytes */
+ const char *test_desc_2 =
+ "I think their predominant colour was a greyish-green, though they had w"
+ "hite bellies. They were mostly shiny and slippery, but the ridges of th"
+ "eir backs were scaly. Their forms vaguely suggested the anthropoid, whi"
+ "le their heads were the heads of fish, with prodigious bulging eyes tha"
+ "t never closed. At the sides of their necks were palpitating gills, and"
+ "their long paws were webbed. They hopped irregularly, sometimes on two "
+ "legs and sometimes on four. I was somehow glad that they had no more th"
+ "an four limbs. Their croaking, baying voices, clearly wed tar articulat"
+ "e speech, held all the dark shades of expression which their staring fa"
+ "ces lacked.";
+ uint8_t test_desc_2_hash[DIGEST256_LEN];
+ char test_desc_2_hash_str[HEX_DIGEST256_LEN+1];
+ /* strlen(test_desc_3) = 700 bytes */
+ const char *test_desc_3 =
+ "Without knowing what futurism is like, Johansen achieved something very"
+ "close to it when he spoke of the city; for instead of describing any de"
+ "finite structure or building, he dwells only on broad impressions of va"
+ "st angles and stone surfaces - surfaces too great to belong to anything"
+ "right or proper for this earth, and impious with horrible images and hi"
+ "eroglyphs. I mention his talk about angles because it suggests somethin"
+ "g Wilcox had told me of his awful dreams. He said that the geometry of "
+ "the dream-place he saw was abnormal, non-Euclidean, and loathsomely red"
+ "olent of spheres and dimensions apart from ours. Now an unlettered seam"
+ "an felt the same thing whilst gazing at the terrible reality.";
+ uint8_t test_desc_3_hash[DIGEST256_LEN];
+ char test_desc_3_hash_str[HEX_DIGEST256_LEN+1];
+ /* strlen(test_desc_3) = 604 bytes */
+ const char *test_desc_4 =
+ "So we glanced back simultaneously, it would appear; though no doubt the"
+ "incipient motion of one prompted the imitation of the other. As we did "
+ "so we flashed both torches full strength at the momentarily thinned mis"
+ "t; either from sheer primitive anxiety to see all we could, or in a les"
+ "s primitive but equally unconscious effort to dazzle the entity before "
+ "we dimmed our light and dodged among the penguins of the labyrinth cent"
+ "er ahead. Unhappy act! Not Orpheus himself, or Lot's wife, paid much mo"
+ "re dearly for a backward glance. And again came that shocking, wide-ran"
+ "ged piping - \"Tekeli-li! Tekeli-li!\"";
+ uint8_t test_desc_4_hash[DIGEST256_LEN];
+ char test_desc_4_hash_str[HEX_DIGEST256_LEN+1];
+ (void)data;
+
+ /*
+ * Set up options mock so we can force a tiny FIFO size and generate
+ * cleanups.
+ */
+ mock_options = tor_malloc(sizeof(or_options_t));
+ reset_options(mock_options, &mock_get_options_calls);
+ mock_options->MaxUnparseableDescSizeToLog = 1536;
+ MOCK(get_options, mock_get_options);
+ MOCK(check_private_dir, mock_check_private_dir);
+ MOCK(options_get_datadir_fname2_suffix,
+ mock_get_datadir_fname);
+
+ /*
+ * Set up unlink and write mocks
+ */
+ MOCK(tor_unlink, mock_unlink);
+ mock_unlink_reset();
+ MOCK(write_str_to_file, mock_write_str_to_file);
+ mock_write_str_to_file_reset();
+
+ /*
+ * Compute hashes we'll need to recognize which descriptor is which
+ */
+ crypto_digest256((char *)test_desc_1_hash, test_desc_1,
+ strlen(test_desc_1), DIGEST_SHA256);
+ base16_encode(test_desc_1_hash_str, sizeof(test_desc_1_hash_str),
+ (const char *)test_desc_1_hash,
+ sizeof(test_desc_1_hash));
+ crypto_digest256((char *)test_desc_2_hash, test_desc_2,
+ strlen(test_desc_2), DIGEST_SHA256);
+ base16_encode(test_desc_2_hash_str, sizeof(test_desc_2_hash_str),
+ (const char *)test_desc_2_hash,
+ sizeof(test_desc_2_hash));
+ crypto_digest256((char *)test_desc_3_hash, test_desc_3,
+ strlen(test_desc_3), DIGEST_SHA256);
+ base16_encode(test_desc_3_hash_str, sizeof(test_desc_3_hash_str),
+ (const char *)test_desc_3_hash,
+ sizeof(test_desc_3_hash));
+ crypto_digest256((char *)test_desc_4_hash, test_desc_4,
+ strlen(test_desc_4), DIGEST_SHA256);
+ base16_encode(test_desc_4_hash_str, sizeof(test_desc_4_hash_str),
+ (const char *)test_desc_4_hash,
+ sizeof(test_desc_4_hash));
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * (1) Fire off dump_desc() once; these descriptors should all be safely
+ * smaller than configured FIFO size.
+ */
+
+ dump_desc(test_desc_1, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (2) Fire off dump_desc() twice; this still should trigger no cleanup.
+ */
+
+ /* First time */
+ dump_desc(test_desc_2, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_3, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (3) Three calls to dump_desc cause a FIFO cleanup
+ */
+
+ /* First time */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_1, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4) + strlen(test_desc_1));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
+
+ /* Third time - we should unlink the dump of test_desc_4 here */
+ dump_desc(test_desc_2, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 1);
+ tt_int_op(write_str_count, ==, 3);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (4) But repeating one (A B B) doesn't overflow and cleanup
+ */
+
+ /* First time */
+ dump_desc(test_desc_3, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /* Third time */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (5) Same for the (A B A) repetition
+ */
+
+ /* First time */
+ dump_desc(test_desc_1, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_2, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
+
+ /* Third time */
+ dump_desc(test_desc_1, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_1) + strlen(test_desc_2));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (6) (A B B C) triggering overflow on C causes A, not B to be unlinked
+ */
+
+ /* First time */
+ dump_desc(test_desc_3, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /* Third time */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_3) + strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /* Fourth time - we should unlink the dump of test_desc_3 here */
+ dump_desc(test_desc_1, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_4) + strlen(test_desc_1));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 1);
+ tt_int_op(write_str_count, ==, 3);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_1_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ /*
+ * (7) (A B A C) triggering overflow on C causes B, not A to be unlinked
+ */
+
+ /* First time */
+ dump_desc(test_desc_2, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 1);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 1);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_2_hash, DIGEST_SHA256);
+
+ /* Second time */
+ dump_desc(test_desc_3, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
+
+ /* Third time */
+ dump_desc(test_desc_2, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_3));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 2);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_3_hash, DIGEST_SHA256);
+
+ /* Fourth time - we should unlink the dump of test_desc_3 here */
+ dump_desc(test_desc_4, test_desc_type);
+
+ /*
+ * Assert things about the FIFO state
+ */
+ tt_u64_op(len_descs_dumped, ==, strlen(test_desc_2) + strlen(test_desc_4));
+ tt_assert(descs_dumped != NULL && smartlist_len(descs_dumped) == 2);
+
+ /*
+ * Assert things about the mocks
+ */
+ tt_int_op(unlinked_count, ==, 1);
+ tt_int_op(write_str_count, ==, 3);
+ tt_mem_op(last_write_str_hash, OP_EQ, test_desc_4_hash, DIGEST_SHA256);
+
+ /*
+ * Reset the FIFO and check its state
+ */
+ dump_desc_fifo_cleanup();
+ tt_u64_op(len_descs_dumped, ==, 0);
+ tt_assert(descs_dumped == NULL || smartlist_len(descs_dumped) == 0);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ mock_write_str_to_file_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ tt_int_op(write_str_count, ==, 0);
+
+ done:
+
+ /* Clean up the fifo */
+ dump_desc_fifo_cleanup();
+
+ /* Remove mocks */
+ UNMOCK(tor_unlink);
+ mock_unlink_reset();
+ UNMOCK(write_str_to_file);
+ mock_write_str_to_file_reset();
+ UNMOCK(options_get_datadir_fname2_suffix);
+ UNMOCK(check_private_dir);
+ UNMOCK(get_options);
+ tor_free(mock_options);
+ mock_options = NULL;
+
+ return;
+}
+
+/* Variables for reset_read_file_to_str_mock() */
+
+static int enforce_expected_filename = 0;
+static char *expected_filename = NULL;
+static char *file_content = NULL;
+static size_t file_content_len = 0;
+static struct stat file_stat;
+static int read_count = 0, read_call_count = 0;
+
+static void
+reset_read_file_to_str_mock(void)
+{
+ tor_free(expected_filename);
+ tor_free(file_content);
+ file_content_len = 0;
+ memset(&file_stat, 0, sizeof(file_stat));
+ read_count = 0;
+ read_call_count = 0;
+}
+
+static char *
+read_file_to_str_mock(const char *filename, int flags,
+ struct stat *stat_out) {
+ char *result = NULL;
+
+ /* Insist we got a filename */
+ tt_assert(filename != NULL);
+
+ /* We ignore flags */
+ (void)flags;
+
+ /* Bump the call count */
+ ++read_call_count;
+
+ if (enforce_expected_filename) {
+ tt_assert(expected_filename);
+ tt_str_op(filename, OP_EQ, expected_filename);
+ }
+
+ if (expected_filename != NULL &&
+ file_content != NULL &&
+ strcmp(filename, expected_filename) == 0) {
+ /* You asked for it, you got it */
+
+ /*
+ * This is the same behavior as the real read_file_to_str();
+ * if there's a NUL, the real size ends up in stat_out.
+ */
+ result = tor_malloc(file_content_len + 1);
+ if (file_content_len > 0) {
+ memcpy(result, file_content, file_content_len);
+ }
+ result[file_content_len] = '\0';
+
+ /* Do we need to set up stat_out? */
+ if (stat_out != NULL) {
+ memcpy(stat_out, &file_stat, sizeof(file_stat));
+ /* We always return the correct length here */
+ stat_out->st_size = file_content_len;
+ }
+
+ /* Wooo, we have a return value - bump the counter */
+ ++read_count;
+ }
+ /* else no match, return NULL */
+
+ done:
+ return result;
+}
+
+/* This one tests dump_desc_populate_one_file() */
+static void
+test_dir_populate_dump_desc_fifo(void *data)
+{
+ const char *dirname = "foo";
+ const char *fname = NULL;
+ dumped_desc_t *ent;
+
+ (void)data;
+
+ /*
+ * Set up unlink and read_file_to_str mocks
+ */
+ MOCK(tor_unlink, mock_unlink);
+ mock_unlink_reset();
+ MOCK(read_file_to_str, read_file_to_str_mock);
+ reset_read_file_to_str_mock();
+
+ /* Check state of unlink mock */
+ tt_int_op(unlinked_count, ==, 0);
+
+ /* Some cases that should fail before trying to read the file */
+ ent = dump_desc_populate_one_file(dirname, "bar");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 1);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ ent = dump_desc_populate_one_file(dirname, "unparseable-desc");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 2);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ ent = dump_desc_populate_one_file(dirname, "unparseable-desc.baz");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 3);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ ent = dump_desc_populate_one_file(
+ dirname,
+ "unparseable-desc.08AE85E90461F59E");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 4);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ ent = dump_desc_populate_one_file(
+ dirname,
+ "unparseable-desc.08AE85E90461F59EDF0981323F3A70D02B55AB54B44B04F"
+ "287D72F7B72F242E85C8CB0EDA8854A99");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 5);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ /* This is a correct-length digest but base16_decode() will fail */
+ ent = dump_desc_populate_one_file(
+ dirname,
+ "unparseable-desc.68219B8BGE64B705A6FFC728C069DC596216D60A7D7520C"
+ "D5ECE250D912E686B");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 6);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 0);
+
+ /* This one has a correctly formed filename and should try reading */
+
+ /* Read fails */
+ ent = dump_desc_populate_one_file(
+ dirname,
+ "unparseable-desc.DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E"
+ "85C8CB0EDA8854A99");
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 7);
+ tt_int_op(read_count, ==, 0);
+ tt_int_op(read_call_count, ==, 1);
+
+ /* This read will succeed but the digest won't match the file content */
+ fname =
+ "unparseable-desc."
+ "DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E85C8CB0EDA8854A99";
+ enforce_expected_filename = 1;
+ tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
+ file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
+ file_content_len = strlen(file_content);
+ file_stat.st_mtime = 123456;
+ ent = dump_desc_populate_one_file(dirname, fname);
+ enforce_expected_filename = 0;
+ tt_assert(ent == NULL);
+ tt_int_op(unlinked_count, ==, 8);
+ tt_int_op(read_count, ==, 1);
+ tt_int_op(read_call_count, ==, 2);
+ tor_free(expected_filename);
+ tor_free(file_content);
+
+ /* This one will match */
+ fname =
+ "unparseable-desc."
+ "0786C7173447B7FB033FFCA2FC47C3CF71C30DD47CA8236D3FC7FF35853271C6";
+ tor_asprintf(&expected_filename, "%s%s%s", dirname, PATH_SEPARATOR, fname);
+ file_content = tor_strdup("hanc culpam maiorem an illam dicam?");
+ file_content_len = strlen(file_content);
+ file_stat.st_mtime = 789012;
+ ent = dump_desc_populate_one_file(dirname, fname);
+ tt_assert(ent != NULL);
+ tt_int_op(unlinked_count, ==, 8);
+ tt_int_op(read_count, ==, 2);
+ tt_int_op(read_call_count, ==, 3);
+ tt_str_op(ent->filename, OP_EQ, expected_filename);
+ tt_int_op(ent->len, ==, file_content_len);
+ tt_int_op(ent->when, ==, file_stat.st_mtime);
+ tor_free(ent->filename);
+ tor_free(ent);
+ tor_free(expected_filename);
+
+ /*
+ * Reset the mocks and check their state
+ */
+ mock_unlink_reset();
+ tt_int_op(unlinked_count, ==, 0);
+ reset_read_file_to_str_mock();
+ tt_int_op(read_count, ==, 0);
+
+ done:
+
+ UNMOCK(tor_unlink);
+ mock_unlink_reset();
+ UNMOCK(read_file_to_str);
+ reset_read_file_to_str_mock();
+
+ tor_free(file_content);
+
+ return;
+}
+
+static smartlist_t *
+listdir_mock(const char *dname)
+{
+ smartlist_t *l;
+
+ /* Ignore the name, always return this list */
+ (void)dname;
+
+ l = smartlist_new();
+ smartlist_add(l, tor_strdup("foo"));
+ smartlist_add(l, tor_strdup("bar"));
+ smartlist_add(l, tor_strdup("baz"));
+
+ return l;
+}
+
+static dumped_desc_t *
+pop_one_mock(const char *dirname, const char *f)
+{
+ dumped_desc_t *ent = NULL;
+
+ if (dirname != NULL && strcmp(dirname, "d") == 0) {
+ if (f != NULL && strcmp(f, "foo") == 0) {
+ ent = tor_malloc_zero(sizeof(*ent));
+ ent->filename = tor_strdup("d/foo");
+ ent->len = 123;
+ ent->digest_sha256[0] = 1;
+ ent->when = 1024;
+ } else if (f != NULL && strcmp(f, "bar") == 0) {
+ ent = tor_malloc_zero(sizeof(*ent));
+ ent->filename = tor_strdup("d/bar");
+ ent->len = 456;
+ ent->digest_sha256[0] = 2;
+ /*
+ * Note that the timestamps are in a different order than
+ * listdir_mock() returns; we're testing the sort order.
+ */
+ ent->when = 512;
+ } else if (f != NULL && strcmp(f, "baz") == 0) {
+ ent = tor_malloc_zero(sizeof(*ent));
+ ent->filename = tor_strdup("d/baz");
+ ent->len = 789;
+ ent->digest_sha256[0] = 3;
+ ent->when = 768;
+ }
+ }
+
+ return ent;
+}
+
+/* This one tests dump_desc_populate_fifo_from_directory() */
+static void
+test_dir_populate_dump_desc_fifo_2(void *data)
+{
+ dumped_desc_t *ent = NULL;
+
+ (void)data;
+
+ /* Set up the mocks */
+ MOCK(tor_listdir, listdir_mock);
+ MOCK(dump_desc_populate_one_file, pop_one_mock);
+
+ /* Run dump_desc_populate_fifo_from_directory() */
+ descs_dumped = NULL;
+ len_descs_dumped = 0;
+ dump_desc_populate_fifo_from_directory("d");
+ tt_assert(descs_dumped != NULL);
+ tt_int_op(smartlist_len(descs_dumped), OP_EQ, 3);
+ tt_u64_op(len_descs_dumped, OP_EQ, 1368);
+ ent = smartlist_get(descs_dumped, 0);
+ tt_str_op(ent->filename, OP_EQ, "d/bar");
+ tt_int_op(ent->len, OP_EQ, 456);
+ tt_int_op(ent->when, OP_EQ, 512);
+ ent = smartlist_get(descs_dumped, 1);
+ tt_str_op(ent->filename, OP_EQ, "d/baz");
+ tt_int_op(ent->len, OP_EQ, 789);
+ tt_int_op(ent->when, OP_EQ, 768);
+ ent = smartlist_get(descs_dumped, 2);
+ tt_str_op(ent->filename, OP_EQ, "d/foo");
+ tt_int_op(ent->len, OP_EQ, 123);
+ tt_int_op(ent->when, OP_EQ, 1024);
+
+ done:
+ dump_desc_fifo_cleanup();
+
+ UNMOCK(dump_desc_populate_one_file);
+ UNMOCK(tor_listdir);
+
+ return;
+}
+
+static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
+static int
+mock_networkstatus_consensus_is_bootstrapping(time_t now)
+{
+ (void)now;
+ return mock_networkstatus_consensus_is_bootstrapping_value;
+}
+
+static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
+static int
+mock_networkstatus_consensus_can_use_extra_fallbacks(
+ const or_options_t *options)
+{
+ (void)options;
+ return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
+}
+
+/* data is a 2 character nul-terminated string.
+ * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
+ * If data[1] is 'f', set extra fallbacks, anything else means no extra
+ * fallbacks.
+ */
+static void
+test_dir_find_dl_schedule(void* data)
+{
+ const char *str = (const char *)data;
+
+ tt_assert(strlen(data) == 2);
+
+ if (str[0] == 'b') {
+ mock_networkstatus_consensus_is_bootstrapping_value = 1;
+ } else {
+ mock_networkstatus_consensus_is_bootstrapping_value = 0;
+ }
+
+ if (str[1] == 'f') {
+ mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
+ } else {
+ mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
+ }
+
+ MOCK(networkstatus_consensus_is_bootstrapping,
+ mock_networkstatus_consensus_is_bootstrapping);
+ MOCK(networkstatus_consensus_can_use_extra_fallbacks,
+ mock_networkstatus_consensus_can_use_extra_fallbacks);
+
+ download_status_t dls;
+ smartlist_t server, client, server_cons, client_cons;
+ smartlist_t client_boot_auth_only_cons, client_boot_auth_cons;
+ smartlist_t client_boot_fallback_cons, bridge;
+
+ mock_options = tor_malloc(sizeof(or_options_t));
+ reset_options(mock_options, &mock_get_options_calls);
+ MOCK(get_options, mock_get_options);
+
+ mock_options->TestingServerDownloadSchedule = &server;
+ mock_options->TestingClientDownloadSchedule = &client;
+ mock_options->TestingServerConsensusDownloadSchedule = &server_cons;
+ mock_options->TestingClientConsensusDownloadSchedule = &client_cons;
+ mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule =
+ &client_boot_auth_only_cons;
+ mock_options->ClientBootstrapConsensusAuthorityDownloadSchedule =
+ &client_boot_auth_cons;
+ mock_options->ClientBootstrapConsensusFallbackDownloadSchedule =
+ &client_boot_fallback_cons;
+ mock_options->TestingBridgeDownloadSchedule = &bridge;
+
+ dls.schedule = DL_SCHED_GENERIC;
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client);
+ mock_options->ClientOnly = 0;
+
+ /* dir mode */
+ mock_options->DirPort_set = 1;
+ mock_options->DirCache = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server);
+ mock_options->DirPort_set = 0;
+ mock_options->DirCache = 0;
+
+ dls.schedule = DL_SCHED_CONSENSUS;
+ /* public server mode */
+ mock_options->ORPort_set = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server_cons);
+ mock_options->ORPort_set = 0;
+
+ /* client and bridge modes */
+ if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
+ if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
+ dls.want_authority = 1;
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_auth_cons);
+ mock_options->ClientOnly = 0;
+
+ /* bridge relay */
+ mock_options->ORPort_set = 1;
+ mock_options->BridgeRelay = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_auth_cons);
+ mock_options->ORPort_set = 0;
+ mock_options->BridgeRelay = 0;
+
+ dls.want_authority = 0;
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_fallback_cons);
+ mock_options->ClientOnly = 0;
+
+ /* bridge relay */
+ mock_options->ORPort_set = 1;
+ mock_options->BridgeRelay = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_fallback_cons);
+ mock_options->ORPort_set = 0;
+ mock_options->BridgeRelay = 0;
+
+ } else {
+ /* dls.want_authority is ignored */
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_auth_only_cons);
+ mock_options->ClientOnly = 0;
+
+ /* bridge relay */
+ mock_options->ORPort_set = 1;
+ mock_options->BridgeRelay = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_boot_auth_only_cons);
+ mock_options->ORPort_set = 0;
+ mock_options->BridgeRelay = 0;
+ }
+ } else {
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_cons);
+ mock_options->ClientOnly = 0;
+
+ /* bridge relay */
+ mock_options->ORPort_set = 1;
+ mock_options->BridgeRelay = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+ &client_cons);
+ mock_options->ORPort_set = 0;
+ mock_options->BridgeRelay = 0;
+ }
+
+ dls.schedule = DL_SCHED_BRIDGE;
+ /* client */
+ mock_options->ClientOnly = 1;
+ tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
+
+ done:
+ UNMOCK(networkstatus_consensus_is_bootstrapping);
+ UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
+ UNMOCK(get_options);
+ tor_free(mock_options);
+ mock_options = NULL;
+}
+
+static void
+test_dir_assumed_flags(void *arg)
+{
+ (void)arg;
+ smartlist_t *tokens = smartlist_new();
+ memarea_t *area = memarea_new();
+ routerstatus_t *rs = NULL;
+
+ /* First, we should always assume that the Running flag is set, even
+ * when it isn't listed, since the consensus method is always
+ * higher than 4. */
+ const char *str1 =
+ "r example hereiswhereyouridentitygoes 2015-08-30 12:00:00 "
+ "192.168.0.1 9001 0\n"
+ "m thisoneislongerbecauseitisa256bitmddigest33\n"
+ "s Fast Guard Stable\n";
+
+ const char *cp = str1;
+ rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL,
+ 23, FLAV_MICRODESC);
+ tt_assert(rs);
+ tt_assert(rs->is_flagged_running);
+ tt_assert(! rs->is_valid);
+ tt_assert(! rs->is_exit);
+ tt_assert(rs->is_fast);
+ routerstatus_free(rs);
+
+ /* With method 24 or later, we can assume "valid" is set. */
+ cp = str1;
+ rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL,
+ 24, FLAV_MICRODESC);
+ tt_assert(rs);
+ tt_assert(rs->is_flagged_running);
+ tt_assert(rs->is_valid);
+ tt_assert(! rs->is_exit);
+ tt_assert(rs->is_fast);
+
+ done:
+ smartlist_free(tokens);
+ memarea_drop_all(area);
+ routerstatus_free(rs);
+}
+
+#define DIR_LEGACY(name) \
{ #name, test_dir_ ## name , TT_FORK, NULL, NULL }
#define DIR(name,flags) \
{ #name, test_dir_##name, (flags), NULL, NULL }
+/* where arg is a string constant */
+#define DIR_ARG(name,flags,arg) \
+ { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
+
struct testcase_t dir_tests[] = {
DIR_LEGACY(nicknames),
DIR_LEGACY(formats),
@@ -3514,6 +5461,7 @@ struct testcase_t dir_tests[] = {
DIR_LEGACY(measured_bw_kb),
DIR_LEGACY(measured_bw_kb_cache),
DIR_LEGACY(param_voting),
+ DIR(param_voting_lookup, 0),
DIR_LEGACY(v3_networkstatus),
DIR(random_weighted, 0),
DIR(scale_bw, 0),
@@ -3525,6 +5473,24 @@ struct testcase_t dir_tests[] = {
DIR(purpose_needs_anonymity, 0),
DIR(fetch_type, 0),
DIR(packages, 0),
+ DIR(download_status_schedule, 0),
+ DIR(download_status_random_backoff, 0),
+ DIR(download_status_increment, 0),
+ DIR(authdir_type_to_string, 0),
+ DIR(conn_purpose_to_string, 0),
+ DIR(should_use_directory_guards, 0),
+ DIR(should_not_init_request_to_ourselves, TT_FORK),
+ DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
+ DIR(should_init_request_to_dir_auths, 0),
+ DIR(choose_compression_level, 0),
+ DIR(dump_unparseable_descriptors, 0),
+ DIR(populate_dump_desc_fifo, 0),
+ DIR(populate_dump_desc_fifo_2, 0),
+ DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
+ DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
+ DIR_ARG(find_dl_schedule, TT_FORK, "cf"),
+ DIR_ARG(find_dl_schedule, TT_FORK, "ca"),
+ DIR(assumed_flags, 0),
END_OF_TESTCASES
};
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
new file mode 100644
index 0000000000..ca43dd4c04
--- /dev/null
+++ b/src/test/test_dir_common.c
@@ -0,0 +1,419 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#define DIRVOTE_PRIVATE
+#include "crypto.h"
+#include "test.h"
+#include "container.h"
+#include "or.h"
+#include "dirvote.h"
+#include "nodelist.h"
+#include "routerlist.h"
+#include "test_dir_common.h"
+
+void dir_common_setup_vote(networkstatus_t **vote, time_t now);
+networkstatus_t * dir_common_add_rs_and_parse(networkstatus_t *vote,
+ networkstatus_t **vote_out,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ crypto_pk_t *sign_skey, int *n_vrs,
+ time_t now, int clear_rl);
+
+/** Initialize and set auth certs and keys
+ * Returns 0 on success, -1 on failure. Clean up handled by caller.
+ */
+int
+dir_common_authority_pk_init(authority_cert_t **cert1,
+ authority_cert_t **cert2,
+ authority_cert_t **cert3,
+ crypto_pk_t **sign_skey_1,
+ crypto_pk_t **sign_skey_2,
+ crypto_pk_t **sign_skey_3)
+{
+ /* Parse certificates and keys. */
+ authority_cert_t *cert;
+ cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ tt_assert(cert);
+ tt_assert(cert->identity_key);
+ *cert1 = cert;
+ tt_assert(*cert1);
+ *cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
+ tt_assert(*cert2);
+ *cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
+ tt_assert(*cert3);
+ *sign_skey_1 = crypto_pk_new();
+ *sign_skey_2 = crypto_pk_new();
+ *sign_skey_3 = crypto_pk_new();
+
+ tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_1,
+ AUTHORITY_SIGNKEY_1, -1));
+ tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_2,
+ AUTHORITY_SIGNKEY_2, -1));
+ tt_assert(!crypto_pk_read_private_key_from_string(*sign_skey_3,
+ AUTHORITY_SIGNKEY_3, -1));
+
+ tt_assert(!crypto_pk_cmp_keys(*sign_skey_1, (*cert1)->signing_key));
+ tt_assert(!crypto_pk_cmp_keys(*sign_skey_2, (*cert2)->signing_key));
+
+ return 0;
+ done:
+ return -1;
+}
+
+/**
+ * Generate a routerstatus for v3_networkstatus test.
+ */
+vote_routerstatus_t *
+dir_common_gen_routerstatus_for_v3ns(int idx, time_t now)
+{
+ vote_routerstatus_t *vrs=NULL;
+ routerstatus_t *rs = NULL;
+ tor_addr_t addr_ipv6;
+ char *method_list = NULL;
+
+ switch (idx) {
+ case 0:
+ /* Generate the first routerstatus. */
+ vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
+ rs = &vrs->status;
+ vrs->version = tor_strdup("0.1.2.14");
+ rs->published_on = now-1500;
+ strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
+ memset(rs->identity_digest, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
+ memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_1, DIGEST_LEN);
+ rs->addr = 0x99008801;
+ rs->or_port = 443;
+ rs->dir_port = 8000;
+ /* all flags but running and v2dir cleared */
+ rs->is_flagged_running = 1;
+ rs->is_v2_dir = 1;
+ rs->is_valid = 1; /* xxxxx */
+ break;
+ case 1:
+ /* Generate the second routerstatus. */
+ vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
+ rs = &vrs->status;
+ vrs->version = tor_strdup("0.2.0.5");
+ rs->published_on = now-1000;
+ strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
+ memset(rs->identity_digest, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
+ memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_2, DIGEST_LEN);
+ rs->addr = 0x99009901;
+ rs->or_port = 443;
+ rs->dir_port = 0;
+ tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
+ tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
+ rs->ipv6_orport = 4711;
+ rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
+ rs->is_valid = rs->is_possible_guard = rs->is_v2_dir = 1;
+ break;
+ case 2:
+ /* Generate the third routerstatus. */
+ vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
+ rs = &vrs->status;
+ vrs->version = tor_strdup("0.1.0.3");
+ rs->published_on = now-1000;
+ strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
+ memset(rs->identity_digest, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
+ memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_3, DIGEST_LEN);
+ rs->addr = 0xAA009901;
+ rs->or_port = 400;
+ rs->dir_port = 9999;
+ rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
+ rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
+ rs->is_possible_guard = 1;
+ break;
+ case 3:
+ /* Generate a fourth routerstatus that is not running. */
+ vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
+ rs = &vrs->status;
+ vrs->version = tor_strdup("0.1.6.3");
+ rs->published_on = now-1000;
+ strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
+ memset(rs->identity_digest, TEST_DIR_ROUTER_ID_4, DIGEST_LEN);
+ memset(rs->descriptor_digest, TEST_DIR_ROUTER_DD_4, DIGEST_LEN);
+ rs->addr = 0xC0000203;
+ rs->or_port = 500;
+ rs->dir_port = 1999;
+ rs->is_v2_dir = 1;
+ /* Running flag (and others) cleared */
+ break;
+ case 4:
+ /* No more for this test; return NULL */
+ vrs = NULL;
+ break;
+ default:
+ /* Shouldn't happen */
+ tt_assert(0);
+ }
+ if (vrs) {
+ vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
+ method_list = make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
+ MAX_SUPPORTED_CONSENSUS_METHOD,
+ ",");
+ tor_asprintf(&vrs->microdesc->microdesc_hash_line,
+ "m %s "
+ "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
+ method_list, idx);
+ }
+
+ done:
+ tor_free(method_list);
+ return vrs;
+}
+
+/** Initialize networkstatus vote object attributes. */
+void
+dir_common_setup_vote(networkstatus_t **vote, time_t now)
+{
+ *vote = tor_malloc_zero(sizeof(networkstatus_t));
+ (*vote)->type = NS_TYPE_VOTE;
+ (*vote)->published = now;
+ (*vote)->supported_methods = smartlist_new();
+ (*vote)->known_flags = smartlist_new();
+ (*vote)->net_params = smartlist_new();
+ (*vote)->routerstatus_list = smartlist_new();
+ (*vote)->voters = smartlist_new();
+}
+
+/** Helper: Make a new routerinfo containing the right information for a
+ * given vote_routerstatus_t. */
+routerinfo_t *
+dir_common_generate_ri_from_rs(const vote_routerstatus_t *vrs)
+{
+ routerinfo_t *r;
+ const routerstatus_t *rs = &vrs->status;
+ static time_t published = 0;
+
+ r = tor_malloc_zero(sizeof(routerinfo_t));
+ r->cert_expiration_time = TIME_MAX;
+ memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
+ memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
+ DIGEST_LEN);
+ r->cache_info.do_not_cache = 1;
+ r->cache_info.routerlist_index = -1;
+ r->cache_info.signed_descriptor_body =
+ tor_strdup("123456789012345678901234567890123");
+ r->cache_info.signed_descriptor_len =
+ strlen(r->cache_info.signed_descriptor_body);
+ r->exit_policy = smartlist_new();
+ r->cache_info.published_on = ++published + time(NULL);
+ if (rs->has_bandwidth) {
+ /*
+ * Multiply by 1000 because the routerinfo_t and the routerstatus_t
+ * seem to use different units (*sigh*) and because we seem stuck on
+ * icky and perverse decimal kilobytes (*double sigh*) - see
+ * router_get_advertised_bandwidth_capped() of routerlist.c and
+ * routerstatus_format_entry() of dirserv.c.
+ */
+ r->bandwidthrate = rs->bandwidth_kb * 1000;
+ r->bandwidthcapacity = rs->bandwidth_kb * 1000;
+ }
+ return r;
+}
+
+/** Create routerstatuses and signed vote.
+ * Create routerstatuses using *vrs_gen* and add them to global routerlist.
+ * Next, create signed vote using *sign_skey* and *vote*, which should have
+ * predefined header fields.
+ * Setting *clear_rl* clears the global routerlist before adding the new
+ * routers.
+ * Return the signed vote, same as *vote_out*. Save the number of routers added
+ * in *n_vrs*.
+ */
+networkstatus_t *
+dir_common_add_rs_and_parse(networkstatus_t *vote, networkstatus_t **vote_out,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ crypto_pk_t *sign_skey, int *n_vrs, time_t now,
+ int clear_rl)
+{
+ vote_routerstatus_t *vrs;
+ char *v_text=NULL;
+ const char *msg=NULL;
+ int idx;
+ was_router_added_t router_added = -1;
+ *vote_out = NULL;
+
+ if (clear_rl) {
+ nodelist_free_all();
+ routerlist_free_all();
+ }
+
+ idx = 0;
+ do {
+ vrs = vrs_gen(idx, now);
+ if (vrs) {
+ smartlist_add(vote->routerstatus_list, vrs);
+ router_added =
+ router_add_to_routerlist(dir_common_generate_ri_from_rs(vrs),
+ &msg,0,0);
+ tt_assert(router_added >= 0);
+ ++idx;
+ }
+ } while (vrs);
+ *n_vrs = idx;
+
+ /* dump the vote and try to parse it. */
+ v_text = format_networkstatus_vote(sign_skey, vote);
+ tt_assert(v_text);
+ *vote_out = networkstatus_parse_vote_from_string(v_text, NULL, NS_TYPE_VOTE);
+
+ done:
+ if (v_text)
+ tor_free(v_text);
+
+ return *vote_out;
+}
+
+/** Create a fake *vote* where *cert* describes the signer, *sign_skey*
+ * is the signing key, and *vrs_gen* is the function we'll use to create the
+ * routers on which we're voting.
+ * We pass *vote_out*, *n_vrs*, and *clear_rl* directly to vrs_gen().
+ * Return 0 on success, return -1 on failure.
+ */
+int
+dir_common_construct_vote_1(networkstatus_t **vote, authority_cert_t *cert,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs,
+ time_t now, int clear_rl)
+{
+ networkstatus_voter_info_t *voter;
+
+ dir_common_setup_vote(vote, now);
+ (*vote)->valid_after = now+1000;
+ (*vote)->fresh_until = now+2000;
+ (*vote)->valid_until = now+3000;
+ (*vote)->vote_seconds = 100;
+ (*vote)->dist_seconds = 200;
+ smartlist_split_string((*vote)->supported_methods, "1 2 3", NULL, 0, -1);
+ (*vote)->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
+ (*vote)->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
+ smartlist_split_string((*vote)->known_flags,
+ "Authority Exit Fast Guard Running Stable V2Dir Valid",
+ 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+ voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
+ voter->nickname = tor_strdup("Voter1");
+ voter->address = tor_strdup("1.2.3.4");
+ voter->addr = 0x01020304;
+ voter->dir_port = 80;
+ voter->or_port = 9000;
+ voter->contact = tor_strdup("voter@example.com");
+ crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
+ /*
+ * Set up a vote; generate it; try to parse it.
+ */
+ smartlist_add((*vote)->voters, voter);
+ (*vote)->cert = authority_cert_dup(cert);
+ smartlist_split_string((*vote)->net_params, "circuitwindow=101 foo=990",
+ NULL, 0, 0);
+ *n_vrs = 0;
+ /* add routerstatuses */
+ if (!dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
+ n_vrs, now, clear_rl))
+ return -1;
+
+ return 0;
+}
+
+/** See dir_common_construct_vote_1.
+ * Produces a vote with slightly different values.
+ */
+int
+dir_common_construct_vote_2(networkstatus_t **vote, authority_cert_t *cert,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs,
+ time_t now, int clear_rl)
+{
+ networkstatus_voter_info_t *voter;
+
+ dir_common_setup_vote(vote, now);
+ (*vote)->type = NS_TYPE_VOTE;
+ (*vote)->published += 1;
+ (*vote)->valid_after = now+1000;
+ (*vote)->fresh_until = now+3005;
+ (*vote)->valid_until = now+3000;
+ (*vote)->vote_seconds = 100;
+ (*vote)->dist_seconds = 300;
+ smartlist_split_string((*vote)->supported_methods, "1 2 3", NULL, 0, -1);
+ smartlist_split_string((*vote)->known_flags,
+ "Authority Exit Fast Guard MadeOfCheese MadeOfTin "
+ "Running Stable V2Dir Valid", 0,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+ voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
+ voter->nickname = tor_strdup("Voter2");
+ voter->address = tor_strdup("2.3.4.5");
+ voter->addr = 0x02030405;
+ voter->dir_port = 80;
+ voter->or_port = 9000;
+ voter->contact = tor_strdup("voter@example.com");
+ crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
+ /*
+ * Set up a vote; generate it; try to parse it.
+ */
+ smartlist_add((*vote)->voters, voter);
+ (*vote)->cert = authority_cert_dup(cert);
+ if (! (*vote)->net_params)
+ (*vote)->net_params = smartlist_new();
+ smartlist_split_string((*vote)->net_params,
+ "bar=2000000000 circuitwindow=20",
+ NULL, 0, 0);
+ /* add routerstatuses */
+ /* dump the vote and try to parse it. */
+ dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
+ n_vrs, now, clear_rl);
+
+ return 0;
+}
+
+/** See dir_common_construct_vote_1.
+ * Produces a vote with slightly different values. Adds a legacy key.
+ */
+int
+dir_common_construct_vote_3(networkstatus_t **vote, authority_cert_t *cert,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs,
+ time_t now, int clear_rl)
+{
+ networkstatus_voter_info_t *voter;
+
+ dir_common_setup_vote(vote, now);
+ (*vote)->valid_after = now+1000;
+ (*vote)->fresh_until = now+2003;
+ (*vote)->valid_until = now+3000;
+ (*vote)->vote_seconds = 100;
+ (*vote)->dist_seconds = 250;
+ smartlist_split_string((*vote)->supported_methods, "1 2 3 4", NULL, 0, -1);
+ (*vote)->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
+ (*vote)->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
+ smartlist_split_string((*vote)->known_flags,
+ "Authority Exit Fast Guard Running Stable V2Dir Valid",
+ 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+ voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
+ voter->nickname = tor_strdup("Voter2");
+ voter->address = tor_strdup("3.4.5.6");
+ voter->addr = 0x03040506;
+ voter->dir_port = 80;
+ voter->or_port = 9000;
+ voter->contact = tor_strdup("voter@example.com");
+ crypto_pk_get_digest(cert->identity_key, voter->identity_digest);
+ memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
+ /*
+ * Set up a vote; generate it; try to parse it.
+ */
+ smartlist_add((*vote)->voters, voter);
+ (*vote)->cert = authority_cert_dup(cert);
+ smartlist_split_string((*vote)->net_params, "circuitwindow=80 foo=660",
+ NULL, 0, 0);
+ /* add routerstatuses */
+ /* dump the vote and try to parse it. */
+ dir_common_add_rs_and_parse(*vote, vote_out, vrs_gen, sign_skey,
+ n_vrs, now, clear_rl);
+
+ return 0;
+}
+
diff --git a/src/test/test_dir_common.h b/src/test/test_dir_common.h
new file mode 100644
index 0000000000..9682b0db49
--- /dev/null
+++ b/src/test/test_dir_common.h
@@ -0,0 +1,52 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+#include "networkstatus.h"
+#include "routerparse.h"
+
+#define TEST_DIR_ROUTER_ID_1 3
+#define TEST_DIR_ROUTER_ID_2 5
+#define TEST_DIR_ROUTER_ID_3 33
+#define TEST_DIR_ROUTER_ID_4 34
+
+#define TEST_DIR_ROUTER_DD_1 78
+#define TEST_DIR_ROUTER_DD_2 77
+#define TEST_DIR_ROUTER_DD_3 79
+#define TEST_DIR_ROUTER_DD_4 44
+
+int dir_common_authority_pk_init(authority_cert_t **cert1,
+ authority_cert_t **cert2,
+ authority_cert_t **cert3,
+ crypto_pk_t **sign_skey_1,
+ crypto_pk_t **sign_skey_2,
+ crypto_pk_t **sign_skey_3);
+
+routerinfo_t * dir_common_generate_ri_from_rs(const vote_routerstatus_t *vrs);
+
+vote_routerstatus_t * dir_common_gen_routerstatus_for_v3ns(int idx,
+ time_t now);
+
+int dir_common_construct_vote_1(networkstatus_t **vote,
+ authority_cert_t *cert1,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs, time_t now,
+ int clear_rl);
+
+int dir_common_construct_vote_2(networkstatus_t **vote,
+ authority_cert_t *cert2,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs, time_t now,
+ int clear_rl);
+
+int dir_common_construct_vote_3(networkstatus_t **vote,
+ authority_cert_t *cert3,
+ crypto_pk_t *sign_skey,
+ vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
+ networkstatus_t **vote_out, int *n_vrs, time_t now,
+ int clear_rl);
+
diff --git a/src/test/test_dir_handle_get.c b/src/test/test_dir_handle_get.c
new file mode 100644
index 0000000000..a0f22f1f0c
--- /dev/null
+++ b/src/test/test_dir_handle_get.c
@@ -0,0 +1,2510 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define RENDCOMMON_PRIVATE
+#define GEOIP_PRIVATE
+#define CONNECTION_PRIVATE
+#define CONFIG_PRIVATE
+#define RENDCACHE_PRIVATE
+
+#include "or.h"
+#include "config.h"
+#include "connection.h"
+#include "directory.h"
+#include "test.h"
+#include "connection.h"
+#include "rendcommon.h"
+#include "rendcache.h"
+#include "router.h"
+#include "routerlist.h"
+#include "rend_test_helpers.h"
+#include "microdesc.h"
+#include "test_helpers.h"
+#include "nodelist.h"
+#include "entrynodes.h"
+#include "routerparse.h"
+#include "networkstatus.h"
+#include "geoip.h"
+#include "dirserv.h"
+#include "torgzip.h"
+#include "dirvote.h"
+
+#ifdef _WIN32
+/* For mkdir() */
+#include <direct.h>
+#else
+#include <dirent.h>
+#endif
+
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+DISABLE_GCC_WARNING(overlength-strings)
+/* We allow huge string constants in the unit tests, but not in the code
+ * at large. */
+#endif
+#include "vote_descriptors.inc"
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+ENABLE_GCC_WARNING(overlength-strings)
+#endif
+
+#define NS_MODULE dir_handle_get
+
+static void
+connection_write_to_buf_mock(const char *string, size_t len,
+ connection_t *conn, int zlib)
+{
+ (void) zlib;
+
+ tor_assert(string);
+ tor_assert(conn);
+
+ write_to_buf(string, len, conn->outbuf);
+}
+
+#define GET(path) "GET " path " HTTP/1.0\r\n\r\n"
+#define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
+#define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
+#define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
+#define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
+ "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
+
+static dir_connection_t *
+new_dir_conn(void)
+{
+ dir_connection_t *conn = dir_connection_new(AF_INET);
+ tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
+ return conn;
+}
+
+static void
+test_dir_handle_get_bad_request(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(directory_handle_command_get(conn, "", NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(header, OP_EQ, BAD_REQUEST);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_v1_command_not_found(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ // no frontpage configured
+ tt_ptr_op(get_dirportfrontpage(), OP_EQ, NULL);
+
+ /* V1 path */
+ tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
+ OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static const char*
+mock_get_dirportfrontpage(void)
+{
+ return "HELLO FROM FRONTPAGE";
+}
+
+static void
+test_dir_handle_get_v1_command(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0, body_len = 0;
+ const char *exp_body = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ MOCK(get_dirportfrontpage, mock_get_dirportfrontpage);
+
+ exp_body = get_dirportfrontpage();
+ body_len = strlen(exp_body);
+
+ conn = new_dir_conn();
+ tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
+ OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, body_len+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/html\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 20\r\n"));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_str_op(body, OP_EQ, exp_body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ UNMOCK(get_dirportfrontpage);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+}
+
+static void
+test_dir_handle_get_not_found(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ /* Unrecognized path */
+ tt_int_op(directory_handle_command_get(conn, GET("/anything"), NULL, 0),
+ OP_EQ, 0);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_robots_txt(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ tt_int_op(directory_handle_command_get(conn, GET("/tor/robots.txt"),
+ NULL, 0), OP_EQ, 0);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, 29, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 28\r\n"));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_str_op(body, OP_EQ, "User-agent: *\r\nDisallow: /\r\n");
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+}
+
+#define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
+static void
+test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ // connection is not encrypted
+ tt_assert(!connection_dir_is_encrypted(conn))
+
+ tt_int_op(directory_handle_command_get(conn, RENDEZVOUS2_GET(), NULL, 0),
+ OP_EQ, 0);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
+ void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ conn = new_dir_conn();
+
+ // connection is encrypted
+ TO_CONN(conn)->linked = 1;
+ tt_assert(connection_dir_is_encrypted(conn));
+
+ tt_int_op(directory_handle_command_get(conn,
+ RENDEZVOUS2_GET("invalid-desc-id"), NULL, 0), OP_EQ, 0);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(header, OP_EQ, BAD_REQUEST);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ conn = new_dir_conn();
+
+ // connection is encrypted
+ TO_CONN(conn)->linked = 1;
+ tt_assert(connection_dir_is_encrypted(conn));
+
+ //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
+ //case to happen. This test is the same as
+ //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
+ //We should refactor to remove the case from the switch.
+
+ const char *req = RENDEZVOUS2_GET("1bababababababababababababababab");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(header, OP_EQ, BAD_REQUEST);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_rendezvous2_not_found(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ conn = new_dir_conn();
+
+ rend_cache_init();
+
+ // connection is encrypted
+ TO_CONN(conn)->linked = 1;
+ tt_assert(connection_dir_is_encrypted(conn));
+
+ const char *req = RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ rend_cache_free_all();
+}
+
+NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
+
+static routerinfo_t *mock_routerinfo;
+
+static const routerinfo_t *
+NS(router_get_my_routerinfo)(void)
+{
+ if (!mock_routerinfo) {
+ mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
+ }
+
+ return mock_routerinfo;
+}
+
+static void
+test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ char buff[30];
+ char req[70];
+ rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
+ char *service_id = NULL;
+ char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+ size_t body_len = 0;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ NS_MOCK(router_get_my_routerinfo);
+
+ rend_cache_init();
+
+ /* create a valid rend service descriptor */
+ #define RECENT_TIME -10
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+
+ tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str),
+ OP_EQ, 0);
+
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+
+ conn = new_dir_conn();
+
+ // connection is encrypted
+ TO_CONN(conn)->linked = 1;
+ tt_assert(connection_dir_is_encrypted(conn));
+
+ tor_snprintf(req, sizeof(req), RENDEZVOUS2_GET("%s"), desc_id_base32);
+
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ body_len = strlen(desc_holder->desc_str);
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, body_len+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Pragma: no-cache\r\n"));
+ tor_snprintf(buff, sizeof(buff), "Content-Length: %ld\r\n", (long) body_len);
+ tt_assert(strstr(header, buff));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_str_op(body, OP_EQ, desc_holder->desc_str);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ NS_UNMOCK(router_get_my_routerinfo);
+
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_cache_free_all();
+}
+
+#define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
+static void
+test_dir_handle_get_micro_d_not_found(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
+ #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
+ conn = new_dir_conn();
+
+ const char *req = MICRODESC_GET(B64_256_1 "-" B64_256_2);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static or_options_t *mock_options = NULL;
+static void
+init_mock_options(void)
+{
+ mock_options = tor_malloc(sizeof(or_options_t));
+ memset(mock_options, 0, sizeof(or_options_t));
+ mock_options->TestingTorNetwork = 1;
+}
+
+static const or_options_t *
+mock_get_options(void)
+{
+ tor_assert(mock_options);
+ return mock_options;
+}
+
+static const char microdesc[] =
+ "onion-key\n"
+ "-----BEGIN RSA PUBLIC KEY-----\n"
+ "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
+ "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
+ "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
+ "-----END RSA PUBLIC KEY-----\n";
+
+static void
+test_dir_handle_get_micro_d(void *data)
+{
+ dir_connection_t *conn = NULL;
+ microdesc_cache_t *mc = NULL ;
+ smartlist_t *list = NULL;
+ char digest[DIGEST256_LEN];
+ char digest_base64[128];
+ char path[80];
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* SETUP */
+ init_mock_options();
+ const char *fn = get_fname("dir_handle_datadir_test1");
+ mock_options->DataDirectory = tor_strdup(fn);
+
+#ifdef _WIN32
+ tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory));
+#else
+ tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory, 0700));
+#endif
+
+ /* Add microdesc to cache */
+ crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
+ base64_encode_nopad(digest_base64, sizeof(digest_base64),
+ (uint8_t *) digest, DIGEST256_LEN);
+
+ mc = get_microdesc_cache();
+ list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
+ time(NULL), NULL);
+ tt_int_op(1, OP_EQ, smartlist_len(list));
+
+ /* Make the request */
+ conn = new_dir_conn();
+
+ tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
+ tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(microdesc)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_str_op(body, OP_EQ, microdesc);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ smartlist_free(list);
+ microdesc_free_all();
+}
+
+static void
+test_dir_handle_get_micro_d_server_busy(void *data)
+{
+ dir_connection_t *conn = NULL;
+ microdesc_cache_t *mc = NULL ;
+ smartlist_t *list = NULL;
+ char digest[DIGEST256_LEN];
+ char digest_base64[128];
+ char path[80];
+ char *header = NULL;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* SETUP */
+ init_mock_options();
+ const char *fn = get_fname("dir_handle_datadir_test2");
+ mock_options->DataDirectory = tor_strdup(fn);
+
+#ifdef _WIN32
+ tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory));
+#else
+ tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory, 0700));
+#endif
+
+ /* Add microdesc to cache */
+ crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
+ base64_encode_nopad(digest_base64, sizeof(digest_base64),
+ (uint8_t *) digest, DIGEST256_LEN);
+
+ mc = get_microdesc_cache();
+ list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
+ time(NULL), NULL);
+ tt_int_op(1, OP_EQ, smartlist_len(list));
+
+ //Make it busy
+ mock_options->CountPrivateBandwidth = 1;
+
+ /* Make the request */
+ conn = new_dir_conn();
+
+ tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
+ tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ smartlist_free(list);
+ microdesc_free_all();
+}
+
+#define BRIDGES_PATH "/tor/networkstatus-bridges"
+static void
+test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* SETUP */
+ init_mock_options();
+ mock_options->BridgeAuthoritativeDir = 1;
+ mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
+
+ conn = new_dir_conn();
+ TO_CONN(conn)->linked = 1;
+
+ const char *req = GET(BRIDGES_PATH);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_networkstatus_bridges(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* SETUP */
+ init_mock_options();
+ mock_options->BridgeAuthoritativeDir = 1;
+ mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
+ crypto_digest256(mock_options->BridgePassword_AuthDigest_,
+ "abcdefghijklm12345", 18, DIGEST_SHA256);
+
+ conn = new_dir_conn();
+ TO_CONN(conn)->linked = 1;
+
+ const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
+ "Authorization: Basic abcdefghijklm12345\r\n\r\n";
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 0\r\n"));
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* SETUP */
+ init_mock_options();
+ mock_options->BridgeAuthoritativeDir = 1;
+ mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
+ crypto_digest256(mock_options->BridgePassword_AuthDigest_,
+ "abcdefghijklm12345", 18, DIGEST_SHA256);
+
+ conn = new_dir_conn();
+ TO_CONN(conn)->linked = 1;
+
+ const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
+ "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+#define SERVER_DESC_GET(id) GET("/tor/server/" id)
+static void
+test_dir_handle_get_server_descriptors_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = SERVER_DESC_GET("invalid");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_SERVER_BY_FP);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ or_options_free(mock_options); mock_options = NULL;
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_server_descriptors_all(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ /* Setup fake routerlist. */
+ helper_setup_fake_routerlist();
+
+ //TODO: change to router_get_my_extrainfo when testing "extra" path
+ NS_MOCK(router_get_my_routerinfo);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ // We are one of the routers
+ routerlist_t *our_routerlist = router_get_routerlist();
+ tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
+ mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
+ set_server_identity_key(mock_routerinfo->identity_pkey);
+
+ /* Treat "all" requests as if they were unencrypted */
+ mock_routerinfo->cache_info.send_unencrypted = 1;
+
+ conn = new_dir_conn();
+
+ const char *req = SERVER_DESC_GET("all");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ //TODO: Is this a BUG?
+ //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
+ //which is smaller than that by annotation_len bytes
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used,
+ mock_routerinfo->cache_info.signed_descriptor_len+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+
+ //TODO: Is this a BUG?
+ //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_int_op(body_used, OP_EQ,
+ mock_routerinfo->cache_info.signed_descriptor_len);
+
+ tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
+ mock_routerinfo->cache_info.annotations_len);
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+
+ routerlist_free_all();
+ nodelist_free_all();
+ entry_guards_free_all();
+}
+
+static char
+TEST_DESCRIPTOR[] =
+"@uploaded-at 2014-06-08 19:20:11\n"
+"@source \"127.0.0.1\"\n"
+"router test000a 127.0.0.1 5000 0 7000\n"
+"platform Tor 0.2.5.3-alpha-dev on Linux\n"
+"protocols Link 1 2 Circuit 1\n"
+"published 2014-06-08 19:20:11\n"
+"fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
+"uptime 0\n"
+"bandwidth 1073741824 1073741824 0\n"
+"extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
+"caches-extra-info\n"
+"onion-key\n"
+"-----BEGIN RSA PUBLIC KEY-----\n"
+"MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
+"HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
+"DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
+"-----END RSA PUBLIC KEY-----\n"
+"signing-key\n"
+"-----BEGIN RSA PUBLIC KEY-----\n"
+"MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
+"O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
+"3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
+"-----END RSA PUBLIC KEY-----\n"
+"hidden-service-dir\n"
+"contact auth0@test.test\n"
+"ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
+"reject *:*\n"
+"router-signature\n"
+"-----BEGIN SIGNATURE-----\n"
+"rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
+"aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
+"eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
+"-----END SIGNATURE-----\n";
+
+static void
+test_dir_handle_get_server_descriptors_authority(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ crypto_pk_t *identity_pkey = pk_generate(0);
+ (void) data;
+
+ NS_MOCK(router_get_my_routerinfo);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* init mock */
+ router_get_my_routerinfo();
+ crypto_pk_get_digest(identity_pkey,
+ mock_routerinfo->cache_info.identity_digest);
+
+ // the digest is mine (the channel is unnecrypted, so we must allow sending)
+ set_server_identity_key(identity_pkey);
+ mock_routerinfo->cache_info.send_unencrypted = 1;
+
+ /* Setup descriptor */
+ long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
+ mock_routerinfo->cache_info.signed_descriptor_body =
+ tor_strdup(TEST_DESCRIPTOR);
+ mock_routerinfo->cache_info.signed_descriptor_len =
+ strlen(TEST_DESCRIPTOR) - annotation_len;;
+ mock_routerinfo->cache_info.annotations_len = annotation_len;
+
+ conn = new_dir_conn();
+
+ const char *req = SERVER_DESC_GET("authority");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ //TODO: Is this a BUG?
+ //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
+ //is smaller than that by annotation_len bytes
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+
+ tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ UNMOCK(connection_write_to_buf_impl_);
+ tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
+ tor_free(mock_routerinfo);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ crypto_pk_free(identity_pkey);
+}
+
+static void
+test_dir_handle_get_server_descriptors_fp(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ crypto_pk_t *identity_pkey = pk_generate(0);
+ (void) data;
+
+ NS_MOCK(router_get_my_routerinfo);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* init mock */
+ router_get_my_routerinfo();
+ crypto_pk_get_digest(identity_pkey,
+ mock_routerinfo->cache_info.identity_digest);
+
+ // the digest is mine (the channel is unnecrypted, so we must allow sending)
+ set_server_identity_key(identity_pkey);
+ mock_routerinfo->cache_info.send_unencrypted = 1;
+
+ /* Setup descriptor */
+ long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
+ mock_routerinfo->cache_info.signed_descriptor_body =
+ tor_strdup(TEST_DESCRIPTOR);
+ mock_routerinfo->cache_info.signed_descriptor_len =
+ strlen(TEST_DESCRIPTOR) - annotation_len;
+ mock_routerinfo->cache_info.annotations_len = annotation_len;
+
+ conn = new_dir_conn();
+
+ #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
+ #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
+ const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
+ DIGEST_LEN);
+
+ char req[155];
+ tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
+ hex_digest);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ //TODO: Is this a BUG?
+ //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
+ //is smaller than that by annotation_len bytes
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+
+ tt_int_op(body_used, OP_EQ, strlen(body));
+
+ tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ UNMOCK(connection_write_to_buf_impl_);
+ tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
+ tor_free(mock_routerinfo);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ crypto_pk_free(identity_pkey);
+}
+
+#define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
+#define HEX2 "Deadbeef99999991111119999911111111f00ba4"
+
+static void
+test_dir_handle_get_server_descriptors_d(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ crypto_pk_t *identity_pkey = pk_generate(0);
+ (void) data;
+
+ /* Setup fake routerlist. */
+ helper_setup_fake_routerlist();
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* Get one router's signed_descriptor_digest */
+ routerlist_t *our_routerlist = router_get_routerlist();
+ tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
+ routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
+ const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
+ DIGEST_LEN);
+
+ conn = new_dir_conn();
+
+ char req_header[155]; /* XXX Why 155? What kind of number is that?? */
+ tor_snprintf(req_header, sizeof(req_header),
+ SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
+ tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
+
+ //TODO: Is this a BUG?
+ //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
+ //which is smaller than that by annotation_len bytes
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used,
+ router->cache_info.signed_descriptor_len+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+
+ //TODO: Is this a BUG?
+ //This is what should be expected:
+ //tt_int_op(body_used, OP_EQ, strlen(body));
+ tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
+
+ tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
+ router->cache_info.annotations_len);
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ tor_free(mock_routerinfo);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ crypto_pk_free(identity_pkey);
+
+ routerlist_free_all();
+ nodelist_free_all();
+ entry_guards_free_all();
+}
+
+static void
+test_dir_handle_get_server_descriptors_busy(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ crypto_pk_t *identity_pkey = pk_generate(0);
+ (void) data;
+
+ /* Setup fake routerlist. */
+ helper_setup_fake_routerlist();
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ //Make it busy
+ MOCK(get_options, mock_get_options);
+ init_mock_options();
+ mock_options->CountPrivateBandwidth = 1;
+
+ /* Get one router's signed_descriptor_digest */
+ routerlist_t *our_routerlist = router_get_routerlist();
+ tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
+ routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
+ const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
+ DIGEST_LEN);
+
+ conn = new_dir_conn();
+
+ #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
+ #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
+ char req_header[155]; /* XXX 155? Why 155? */
+ tor_snprintf(req_header, sizeof(req_header),
+ SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
+ tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+ tor_free(mock_routerinfo);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ crypto_pk_free(identity_pkey);
+
+ routerlist_free_all();
+ nodelist_free_all();
+ entry_guards_free_all();
+}
+
+static void
+test_dir_handle_get_server_keys_bad_req(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(BAD_REQUEST, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_server_keys_all_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/all");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+#define TEST_CERTIFICATE AUTHORITY_CERT_3
+#define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
+
+static const char TEST_CERT_IDENT_KEY[] =
+ "D867ACF56A9D229B35C25F0090BC9867E906BE69";
+
+static void
+test_dir_handle_get_server_keys_all(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ const char digest[DIGEST_LEN] = "";
+
+ dir_server_t *ds = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/all");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 1883\r\n"));
+
+ tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static void
+test_dir_handle_get_server_keys_authority_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/authority");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static authority_cert_t * mock_cert = NULL;
+
+static authority_cert_t *
+get_my_v3_authority_cert_m(void)
+{
+ tor_assert(mock_cert);
+ return mock_cert;
+}
+
+static void
+test_dir_handle_get_server_keys_authority(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
+
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/authority");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 1883\r\n"));
+
+ tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
+
+ done:
+ UNMOCK(get_my_v3_authority_cert);
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ authority_cert_free(mock_cert); mock_cert = NULL;
+}
+
+static void
+test_dir_handle_get_server_keys_fp_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/fp/somehex");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_server_keys_fp(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ dir_server_t *ds = NULL;
+ const char digest[DIGEST_LEN] = "";
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ conn = new_dir_conn();
+ char req[71];
+ tor_snprintf(req, sizeof(req),
+ GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 1883\r\n"));
+
+ tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static void
+test_dir_handle_get_server_keys_sk_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/sk/somehex");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_server_keys_sk(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ conn = new_dir_conn();
+ char req[71];
+ tor_snprintf(req, sizeof(req),
+ GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 1883\r\n"));
+
+ tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
+
+ done:
+ UNMOCK(get_my_v3_authority_cert);
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ authority_cert_free(mock_cert); mock_cert = NULL;
+ tor_free(header);
+ tor_free(body);
+}
+
+static void
+test_dir_handle_get_server_keys_fpsk_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+
+ const char *req = GET("/tor/keys/fp-sk/somehex");
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_server_keys_fpsk(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ dir_server_t *ds = NULL;
+ const char digest[DIGEST_LEN] = "";
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+ dir_server_add(ds);
+
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ conn = new_dir_conn();
+
+ char req[115];
+ tor_snprintf(req, sizeof(req),
+ GET("/tor/keys/fp-sk/%s-%s"),
+ TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
+
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
+
+ tt_assert(header);
+ tt_assert(body);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 1883\r\n"));
+
+ tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static void
+test_dir_handle_get_server_keys_busy(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ dir_server_t *ds = NULL;
+ const char digest[DIGEST_LEN] = "";
+ (void) data;
+
+ clear_dir_servers();
+ routerlist_free_all();
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+ dir_server_add(ds);
+
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* setup busy server */
+ init_mock_options();
+ mock_options->CountPrivateBandwidth = 1;
+
+ conn = new_dir_conn();
+ char req[71];
+ tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
+ tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ done:
+ UNMOCK(get_options);
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ or_options_free(mock_options); mock_options = NULL;
+
+ clear_dir_servers();
+ routerlist_free_all();
+}
+
+static networkstatus_t *mock_ns_val = NULL;
+static networkstatus_t *
+mock_ns_get_by_flavor(consensus_flavor_t f)
+{
+ (void)f;
+ return mock_ns_val;
+}
+
+static void
+test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *stats = NULL;
+ (void) d;
+
+ /* init mock */
+ mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
+ mock_ns_val->flavor = FLAV_NS;
+ mock_ns_val->voters = smartlist_new();
+
+ /* init mock */
+ init_mock_options();
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+ MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
+
+ /* start gathering stats */
+ mock_options->DirReqStatistics = 1;
+ geoip_dirreq_stats_init(time(NULL));
+
+ conn = new_dir_conn();
+
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+
+ tt_assert(header);
+ tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
+
+ stats = geoip_format_dirreq_stats(time(NULL));
+ tt_assert(stats);
+ tt_assert(strstr(stats, "not-enough-sigs=8"));
+
+ done:
+ UNMOCK(networkstatus_get_latest_consensus_by_flavor);
+ UNMOCK(connection_write_to_buf_impl_);
+ UNMOCK(get_options);
+
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(stats);
+ smartlist_free(mock_ns_val->voters);
+ tor_free(mock_ns_val);
+ or_options_free(mock_options); mock_options = NULL;
+}
+
+static void
+test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ char *stats = NULL;
+ (void) data;
+
+ init_mock_options();
+
+ MOCK(get_options, mock_get_options);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ /* start gathering stats */
+ mock_options->DirReqStatistics = 1;
+ geoip_dirreq_stats_init(time(NULL));
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ stats = geoip_format_dirreq_stats(time(NULL));
+ tt_assert(stats);
+ tt_assert(strstr(stats, "not-found=8"));
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ UNMOCK(get_options);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(stats);
+ or_options_free(mock_options); mock_options = NULL;
+}
+
+NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
+
+int
+NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
+{
+ (void)addr;
+ CALLED(geoip_get_country_by_addr)++;
+ return 1;
+}
+
+static void
+status_vote_current_consensus_ns_test(char **header, char **body,
+ size_t *body_len)
+{
+ common_digests_t digests;
+ dir_connection_t *conn = NULL;
+
+ #define NETWORK_STATUS "some network status string"
+ dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
+ time(NULL));
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ tt_assert(mock_options);
+ mock_options->DirReqStatistics = 1;
+ geoip_dirreq_stats_init(time(NULL));
+
+ /* init geoip database */
+ geoip_parse_entry("10,50,AB", AF_INET);
+ tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
+
+ conn = new_dir_conn();
+ TO_CONN(conn)->address = tor_strdup("127.0.0.1");
+
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
+ body, body_len, strlen(NETWORK_STATUS)+7, 0);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+}
+
+static void
+test_dir_handle_get_status_vote_current_consensus_ns(void* data)
+{
+ char *header = NULL;
+ char *body = NULL, *comp_body = NULL;
+ size_t body_used = 0, comp_body_used = 0;
+ char *stats = NULL, *hist = NULL;
+ (void) data;
+
+ dirserv_free_all();
+ clear_geoip_db();
+
+ NS_MOCK(geoip_get_country_by_addr);
+ MOCK(get_options, mock_get_options);
+
+ init_mock_options();
+
+ status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
+ tt_assert(header);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Pragma: no-cache\r\n"));
+
+ compress_method_t compression = detect_compression_method(comp_body,
+ comp_body_used);
+ tt_int_op(ZLIB_METHOD, OP_EQ, compression);
+
+ tor_gzip_uncompress(&body, &body_used, comp_body, comp_body_used,
+ compression, 0, LOG_PROTOCOL_WARN);
+
+ tt_str_op(NETWORK_STATUS, OP_EQ, body);
+ tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
+
+ stats = geoip_format_dirreq_stats(time(NULL));
+ tt_assert(stats);
+
+ tt_assert(strstr(stats, "ok=8"));
+ tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
+ tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
+ tt_assert(strstr(stats, "dirreq-v3-direct-dl"
+ " complete=0,timeout=0,running=4"));
+
+ hist = geoip_get_request_history();
+ tt_assert(hist);
+ tt_str_op("ab=8", OP_EQ, hist);
+
+ done:
+ NS_UNMOCK(geoip_get_country_by_addr);
+ UNMOCK(get_options);
+ tor_free(header);
+ tor_free(comp_body);
+ tor_free(body);
+ tor_free(stats);
+ tor_free(hist);
+ or_options_free(mock_options); mock_options = NULL;
+
+ dirserv_free_all();
+ clear_geoip_db();
+}
+
+static void
+test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
+{
+ char *header = NULL;
+ char *body = NULL;
+ size_t body_used = 0;
+ char *stats = NULL;
+ (void) data;
+
+ dirserv_free_all();
+ clear_geoip_db();
+
+ MOCK(get_options, mock_get_options);
+
+ // Make it busy
+ init_mock_options();
+ mock_options->CountPrivateBandwidth = 1;
+
+ status_vote_current_consensus_ns_test(&header, &body, &body_used);
+ tt_assert(header);
+
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ stats = geoip_format_dirreq_stats(time(NULL));
+ tt_assert(stats);
+ tt_assert(strstr(stats, "busy=8"));
+
+ done:
+ UNMOCK(get_options);
+ tor_free(header);
+ tor_free(body);
+ or_options_free(mock_options); mock_options = NULL;
+
+ tor_free(stats);
+ dirserv_free_all();
+ clear_geoip_db();
+}
+
+static void
+test_dir_handle_get_status_vote_current_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/" HEX1), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+#define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
+
+static void
+status_vote_current_d_test(char **header, char **body, size_t *body_l)
+{
+ dir_connection_t *conn = NULL;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
+ body, body_l, strlen(VOTE_BODY_V3)+1, 0);
+ tt_assert(header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+}
+
+static void
+status_vote_next_d_test(char **header, char **body, size_t *body_l)
+{
+ dir_connection_t *conn = NULL;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
+ body, body_l, strlen(VOTE_BODY_V3)+1, 0);
+ tt_assert(header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+}
+
+static void
+test_dir_handle_get_status_vote_current_d_not_found(void* data)
+{
+ char *header = NULL;
+ (void) data;
+
+ status_vote_current_d_test(&header, NULL, NULL);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_status_vote_next_d_not_found(void* data)
+{
+ char *header = NULL;
+ (void) data;
+
+ status_vote_next_d_test(&header, NULL, NULL);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_status_vote_d(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used = 0;
+ dir_server_t *ds = NULL;
+ const char digest[DIGEST_LEN] = "";
+ (void) data;
+
+ clear_dir_servers();
+ dirvote_free_all();
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+
+ init_mock_options();
+ mock_options->AuthoritativeDir = 1;
+ mock_options->V3AuthoritativeDir = 1;
+ mock_options->TestingV3AuthVotingStartOffset = 0;
+ mock_options->TestingV3AuthInitialVotingInterval = 1;
+ mock_options->TestingV3AuthInitialVoteDelay = 1;
+ mock_options->TestingV3AuthInitialDistDelay = 1;
+
+ time_t now = 1441223455 -1;
+ dirvote_recalculate_timing(mock_options, now);
+
+ const char *msg_out = NULL;
+ int status_out = 0;
+ struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
+ &status_out);
+ tt_assert(pv);
+
+ status_vote_current_d_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 4135\r\n"));
+
+ tt_str_op(VOTE_BODY_V3, OP_EQ, body);
+
+ tor_free(header);
+ tor_free(body);
+
+ status_vote_next_d_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 4135\r\n"));
+
+ tt_str_op(VOTE_BODY_V3, OP_EQ, body);
+
+ done:
+ tor_free(header);
+ tor_free(body);
+ or_options_free(mock_options); mock_options = NULL;
+
+ clear_dir_servers();
+ dirvote_free_all();
+}
+
+static void
+test_dir_handle_get_status_vote_next_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/" HEX1), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
+{
+ dir_connection_t *conn = NULL;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/consensus"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
+ body, body_used, 18, 0);
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used;
+ (void) data;
+
+ status_vote_next_consensus_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ tor_free(header);
+ tor_free(body);
+}
+
+static void
+test_dir_handle_get_status_vote_current_authority_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/authority"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+static void
+test_dir_handle_get_status_vote_next_authority_not_found(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL;
+ (void) data;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/authority"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ NULL, NULL, 1, 0);
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+}
+
+NS_DECL(const char*,
+dirvote_get_pending_consensus, (consensus_flavor_t flav));
+
+const char*
+NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
+{
+ (void)flav;
+ return "pending consensus";
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ NS_MOCK(dirvote_get_pending_consensus);
+
+ status_vote_next_consensus_test(&header, &body, &body_used);
+ tt_assert(header);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 17\r\n"));
+
+ tt_str_op("pending consensus", OP_EQ, body);
+
+ done:
+ NS_UNMOCK(dirvote_get_pending_consensus);
+ tor_free(header);
+ tor_free(body);
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus_busy(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ MOCK(get_options, mock_get_options);
+ NS_MOCK(dirvote_get_pending_consensus);
+
+ //Make it busy
+ init_mock_options();
+ mock_options->CountPrivateBandwidth = 1;
+
+ status_vote_next_consensus_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ done:
+ NS_UNMOCK(dirvote_get_pending_consensus);
+ UNMOCK(get_options);
+ tor_free(header);
+ tor_free(body);
+ or_options_free(mock_options); mock_options = NULL;
+}
+
+static void
+status_vote_next_consensus_signatures_test(char **header, char **body,
+ size_t *body_used)
+{
+ dir_connection_t *conn = NULL;
+
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
+ body, body_used, 22, 0);
+
+ done:
+ connection_free_(TO_CONN(conn));
+ UNMOCK(connection_write_to_buf_impl_);
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used;
+ (void) data;
+
+ status_vote_next_consensus_signatures_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_str_op(NOT_FOUND, OP_EQ, header);
+
+ done:
+ tor_free(header);
+ tor_free(body);
+}
+
+NS_DECL(const char*,
+dirvote_get_pending_detached_signatures, (void));
+
+const char*
+NS(dirvote_get_pending_detached_signatures)(void)
+{
+ return "pending detached sigs";
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used = 0;
+ (void) data;
+
+ NS_MOCK(dirvote_get_pending_detached_signatures);
+
+ status_vote_next_consensus_signatures_test(&header, &body, &body_used);
+ tt_assert(header);
+
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 21\r\n"));
+
+ tt_str_op("pending detached sigs", OP_EQ, body);
+
+ done:
+ NS_UNMOCK(dirvote_get_pending_detached_signatures);
+ tor_free(header);
+ tor_free(body);
+}
+
+static void
+test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
+{
+ char *header = NULL, *body = NULL;
+ size_t body_used;
+ (void) data;
+
+ NS_MOCK(dirvote_get_pending_detached_signatures);
+ MOCK(get_options, mock_get_options);
+
+ //Make it busy
+ init_mock_options();
+ mock_options->CountPrivateBandwidth = 1;
+
+ status_vote_next_consensus_signatures_test(&header, &body, &body_used);
+
+ tt_assert(header);
+ tt_str_op(SERVER_BUSY, OP_EQ, header);
+
+ done:
+ UNMOCK(get_options);
+ NS_UNMOCK(dirvote_get_pending_detached_signatures);
+ tor_free(header);
+ tor_free(body);
+ or_options_free(mock_options); mock_options = NULL;
+}
+
+static void
+test_dir_handle_get_status_vote_next_authority(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL, *body = NULL;
+ const char *msg_out = NULL;
+ int status_out = 0;
+ size_t body_used = 0;
+ dir_server_t *ds = NULL;
+ const char digest[DIGEST_LEN] = "";
+ (void) data;
+
+ clear_dir_servers();
+ routerlist_free_all();
+ dirvote_free_all();
+
+ mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ init_mock_options();
+ mock_options->AuthoritativeDir = 1;
+ mock_options->V3AuthoritativeDir = 1;
+ mock_options->TestingV3AuthVotingStartOffset = 0;
+ mock_options->TestingV3AuthInitialVotingInterval = 1;
+ mock_options->TestingV3AuthInitialVoteDelay = 1;
+ mock_options->TestingV3AuthInitialDistDelay = 1;
+
+ time_t now = 1441223455 -1;
+ dirvote_recalculate_timing(mock_options, now);
+
+ struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
+ &status_out);
+ tt_assert(vote);
+
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/next/authority"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
+
+ tt_assert(header);
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 4135\r\n"));
+
+ tt_str_op(VOTE_BODY_V3, OP_EQ, body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ UNMOCK(get_my_v3_authority_cert);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ authority_cert_free(mock_cert); mock_cert = NULL;
+ or_options_free(mock_options); mock_options = NULL;
+
+ clear_dir_servers();
+ routerlist_free_all();
+ dirvote_free_all();
+}
+
+static void
+test_dir_handle_get_status_vote_current_authority(void* data)
+{
+ dir_connection_t *conn = NULL;
+ char *header = NULL, *body = NULL;
+ const char *msg_out = NULL;
+ int status_out = 0;
+ size_t body_used = 0;
+ const char digest[DIGEST_LEN] = "";
+
+ dir_server_t *ds = NULL;
+ (void) data;
+
+ clear_dir_servers();
+ routerlist_free_all();
+ dirvote_free_all();
+
+ mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
+
+ /* create a trusted ds */
+ ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
+ NULL, V3_DIRINFO, 1.0);
+ tt_assert(ds);
+ dir_server_add(ds);
+
+ /* ds v3_identity_digest is the certificate's identity_key */
+ base16_decode(ds->v3_identity_digest, DIGEST_LEN,
+ TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
+
+ tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
+ TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
+
+ init_mock_options();
+ mock_options->AuthoritativeDir = 1;
+ mock_options->V3AuthoritativeDir = 1;
+ mock_options->TestingV3AuthVotingStartOffset = 0;
+ mock_options->TestingV3AuthInitialVotingInterval = 1;
+ mock_options->TestingV3AuthInitialVoteDelay = 1;
+ mock_options->TestingV3AuthInitialDistDelay = 1;
+
+ time_t now = 1441223455;
+ dirvote_recalculate_timing(mock_options, now-1);
+
+ struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
+ &status_out);
+ tt_assert(vote);
+
+ // move the pending vote to previous vote
+ dirvote_act(mock_options, now+1);
+
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
+
+ conn = new_dir_conn();
+ tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
+ GET("/tor/status-vote/current/authority"), NULL, 0));
+
+ fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
+ &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
+
+ tt_assert(header);
+ tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
+ tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
+ tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
+ tt_assert(strstr(header, "Content-Length: 4135\r\n"));
+
+ tt_str_op(VOTE_BODY_V3, OP_EQ, body);
+
+ done:
+ UNMOCK(connection_write_to_buf_impl_);
+ UNMOCK(get_my_v3_authority_cert);
+ connection_free_(TO_CONN(conn));
+ tor_free(header);
+ tor_free(body);
+ authority_cert_free(mock_cert); mock_cert = NULL;
+ or_options_free(mock_options); mock_options = NULL;
+
+ clear_dir_servers();
+ routerlist_free_all();
+ dirvote_free_all();
+}
+
+#define DIR_HANDLE_CMD(name,flags) \
+ { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
+
+struct testcase_t dir_handle_get_tests[] = {
+ DIR_HANDLE_CMD(not_found, 0),
+ DIR_HANDLE_CMD(bad_request, 0),
+ DIR_HANDLE_CMD(v1_command_not_found, 0),
+ DIR_HANDLE_CMD(v1_command, 0),
+ DIR_HANDLE_CMD(robots_txt, 0),
+ DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
+ DIR_HANDLE_CMD(rendezvous2_not_found, 0),
+ DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
+ DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
+ DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
+ DIR_HANDLE_CMD(micro_d_not_found, 0),
+ DIR_HANDLE_CMD(micro_d_server_busy, 0),
+ DIR_HANDLE_CMD(micro_d, 0),
+ DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
+ DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
+ DIR_HANDLE_CMD(networkstatus_bridges, 0),
+ DIR_HANDLE_CMD(server_descriptors_not_found, 0),
+ DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
+ DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
+ DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
+ DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
+ DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
+ DIR_HANDLE_CMD(server_keys_bad_req, 0),
+ DIR_HANDLE_CMD(server_keys_busy, 0),
+ DIR_HANDLE_CMD(server_keys_all_not_found, 0),
+ DIR_HANDLE_CMD(server_keys_all, 0),
+ DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
+ DIR_HANDLE_CMD(server_keys_authority, 0),
+ DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
+ DIR_HANDLE_CMD(server_keys_fp, 0),
+ DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
+ DIR_HANDLE_CMD(server_keys_sk, 0),
+ DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
+ DIR_HANDLE_CMD(server_keys_fpsk, 0),
+ DIR_HANDLE_CMD(status_vote_current_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_next_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_current_authority, 0),
+ DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_next_authority, 0),
+ DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, 0),
+ DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, 0),
+ DIR_HANDLE_CMD(status_vote_current_consensus_ns, 0),
+ DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_d, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
+ DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_dns.c b/src/test/test_dns.c
index ad81914ccb..8346c0a33f 100644
--- a/src/test/test_dns.c
+++ b/src/test/test_dns.c
@@ -5,9 +5,14 @@
#include "dns.h"
#include "connection.h"
+#include "router.h"
+
+#define NS_MODULE dns
+
+#define NS_SUBMODULE clip_ttl
static void
-test_dns_clip_ttl(void *arg)
+NS(test_main)(void *arg)
{
(void)arg;
@@ -21,8 +26,12 @@ test_dns_clip_ttl(void *arg)
return;
}
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE expiry_ttl
+
static void
-test_dns_expiry_ttl(void *arg)
+NS(test_main)(void *arg)
{
(void)arg;
@@ -36,13 +45,22 @@ test_dns_expiry_ttl(void *arg)
return;
}
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE resolve
+
static int resolve_retval = 0;
static int resolve_made_conn_pending = 0;
static char *resolved_name = NULL;
-static cached_resolve_t *cache_entry = NULL;
+static cached_resolve_t *cache_entry_mock = NULL;
static int n_fake_impl = 0;
+NS_DECL(int, dns_resolve_impl, (edge_connection_t *exitconn, int is_resolve,
+ or_circuit_t *oncirc, char **hostname_out,
+ int *made_connection_pending_out,
+ cached_resolve_t **resolve_out));
+
/** This will be our configurable substitute for <b>dns_resolve_impl</b> in
* dns.c. It will return <b>resolve_retval</b>,
* and set <b>resolve_made_conn_pending</b> to
@@ -52,10 +70,10 @@ static int n_fake_impl = 0;
* 1.
*/
static int
-dns_resolve_fake_impl(edge_connection_t *exitconn, int is_resolve,
- or_circuit_t *oncirc, char **hostname_out,
- int *made_connection_pending_out,
- cached_resolve_t **resolve_out)
+NS(dns_resolve_impl)(edge_connection_t *exitconn, int is_resolve,
+ or_circuit_t *oncirc, char **hostname_out,
+ int *made_connection_pending_out,
+ cached_resolve_t **resolve_out)
{
(void)oncirc;
(void)exitconn;
@@ -67,8 +85,8 @@ dns_resolve_fake_impl(edge_connection_t *exitconn, int is_resolve,
if (hostname_out && resolved_name)
*hostname_out = tor_strdup(resolved_name);
- if (resolve_out && cache_entry)
- *resolve_out = cache_entry;
+ if (resolve_out && cache_entry_mock)
+ *resolve_out = cache_entry_mock;
n_fake_impl++;
@@ -82,8 +100,8 @@ static uint8_t last_answer_type = 0;
static cached_resolve_t *last_resolved;
static void
-send_resolved_cell_replacement(edge_connection_t *conn, uint8_t answer_type,
- const cached_resolve_t *resolved)
+NS(send_resolved_cell)(edge_connection_t *conn, uint8_t answer_type,
+ const cached_resolve_t *resolved)
{
conn_for_resolved_cell = conn;
@@ -98,8 +116,8 @@ static int n_send_resolved_hostname_cell_replacement = 0;
static char *last_resolved_hostname = NULL;
static void
-send_resolved_hostname_cell_replacement(edge_connection_t *conn,
- const char *hostname)
+NS(send_resolved_hostname_cell)(edge_connection_t *conn,
+ const char *hostname)
{
conn_for_resolved_cell = conn;
@@ -112,7 +130,7 @@ send_resolved_hostname_cell_replacement(edge_connection_t *conn,
static int n_dns_cancel_pending_resolve_replacement = 0;
static void
-dns_cancel_pending_resolve_replacement(const char *address)
+NS(dns_cancel_pending_resolve)(const char *address)
{
(void) address;
n_dns_cancel_pending_resolve_replacement++;
@@ -122,7 +140,7 @@ static int n_connection_free = 0;
static connection_t *last_freed_conn = NULL;
static void
-connection_free_replacement(connection_t *conn)
+NS(connection_free)(connection_t *conn)
{
n_connection_free++;
@@ -130,7 +148,7 @@ connection_free_replacement(connection_t *conn)
}
static void
-test_dns_resolve_outer(void *arg)
+NS(test_main)(void *arg)
{
(void) arg;
int retval;
@@ -149,9 +167,9 @@ test_dns_resolve_outer(void *arg)
memset(exitconn,0,sizeof(edge_connection_t));
memset(nextconn,0,sizeof(edge_connection_t));
- MOCK(dns_resolve_impl,dns_resolve_fake_impl);
- MOCK(send_resolved_cell,send_resolved_cell_replacement);
- MOCK(send_resolved_hostname_cell,send_resolved_hostname_cell_replacement);
+ NS_MOCK(dns_resolve_impl);
+ NS_MOCK(send_resolved_cell);
+ NS_MOCK(send_resolved_hostname_cell);
/*
* CASE 1: dns_resolve_impl returns 1 and sets a hostname. purpose is
@@ -195,7 +213,7 @@ test_dns_resolve_outer(void *arg)
exitconn->on_circuit = &(on_circuit->base_);
- cache_entry = fake_resolved;
+ cache_entry_mock = fake_resolved;
prev_n_send_resolved_cell_replacement =
n_send_resolved_cell_replacement;
@@ -264,8 +282,8 @@ test_dns_resolve_outer(void *arg)
* on exitconn with type being RESOLVED_TYPE_ERROR.
*/
- MOCK(dns_cancel_pending_resolve,dns_cancel_pending_resolve_replacement);
- MOCK(connection_free,connection_free_replacement);
+ NS_MOCK(dns_cancel_pending_resolve);
+ NS_MOCK(connection_free);
exitconn->on_circuit = &(on_circuit->base_);
exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;
@@ -288,11 +306,11 @@ test_dns_resolve_outer(void *arg)
tt_assert(last_freed_conn == TO_CONN(exitconn));
done:
- UNMOCK(dns_resolve_impl);
- UNMOCK(send_resolved_cell);
- UNMOCK(send_resolved_hostname_cell);
- UNMOCK(dns_cancel_pending_resolve);
- UNMOCK(connection_free);
+ NS_UNMOCK(dns_resolve_impl);
+ NS_UNMOCK(send_resolved_cell);
+ NS_UNMOCK(send_resolved_hostname_cell);
+ NS_UNMOCK(dns_cancel_pending_resolve);
+ NS_UNMOCK(connection_free);
tor_free(on_circuit);
tor_free(exitconn);
tor_free(nextconn);
@@ -302,10 +320,446 @@ test_dns_resolve_outer(void *arg)
return;
}
+#undef NS_SUBMODULE
+
+/** Create an <b>edge_connection_t</b> instance that is considered a
+ * valid exit connection by asserts in dns_resolve_impl.
+ */
+static edge_connection_t *
+create_valid_exitconn(void)
+{
+ edge_connection_t *exitconn = tor_malloc_zero(sizeof(edge_connection_t));
+ TO_CONN(exitconn)->type = CONN_TYPE_EXIT;
+ TO_CONN(exitconn)->magic = EDGE_CONNECTION_MAGIC;
+ TO_CONN(exitconn)->purpose = EXIT_PURPOSE_RESOLVE;
+ TO_CONN(exitconn)->state = EXIT_CONN_STATE_RESOLVING;
+ exitconn->base_.s = TOR_INVALID_SOCKET;
+
+ return exitconn;
+}
+
+#define NS_SUBMODULE ASPECT(resolve_impl, addr_is_ip_no_need_to_resolve)
+
+/*
+ * Given that <b>exitconn->base_.address</b> is IP address string, we
+ * want dns_resolve_impl() to parse it and store in
+ * <b>exitconn->base_.addr</b>. We expect dns_resolve_impl to return 1.
+ * Lastly, we want it to set the TTL value to default one for DNS queries.
+ */
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending;
+ const tor_addr_t *resolved_addr;
+ tor_addr_t addr_to_compare;
+
+ (void)arg;
+
+ tor_addr_parse(&addr_to_compare, "8.8.8.8");
+
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+
+ TO_CONN(exitconn)->address = tor_strdup("8.8.8.8");
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ resolved_addr = &(exitconn->base_.addr);
+
+ tt_int_op(retval,==,1);
+ tt_assert(tor_addr_eq(resolved_addr, (const tor_addr_t *)&addr_to_compare));
+ tt_int_op(exitconn->address_ttl,==,DEFAULT_DNS_TTL);
+
+ done:
+ tor_free(on_circ);
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(exitconn);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, non_exit)
+
+/** Given that Tor instance is not configured as an exit node, we want
+ * dns_resolve_impl() to fail with return value -1.
+ */
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 1;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ (void)arg;
+
+ TO_CONN(exitconn)->address = tor_strdup("torproject.org");
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,-1);
+
+ done:
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(exitconn);
+ tor_free(on_circ);
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, addr_is_invalid_dest)
+
+/** Given that address is not a valid destination (as judged by
+ * address_is_invalid_destination() function), we want dns_resolve_impl()
+ * function to fail with return value -1.
+ */
+
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 0;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ (void)arg;
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+
+ TO_CONN(exitconn)->address = tor_strdup("invalid#@!.org");
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,-1);
+
+ done:
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(exitconn);
+ tor_free(on_circ);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, malformed_ptr)
+
+/** Given that address is a malformed PTR name, we want dns_resolve_impl to
+ * fail.
+ */
+
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 0;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ (void)arg;
+
+ TO_CONN(exitconn)->address = tor_strdup("1.0.0.127.in-addr.arpa");
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,-1);
+
+ tor_free(TO_CONN(exitconn)->address);
+
+ TO_CONN(exitconn)->address =
+ tor_strdup("z01234567890123456789.in-addr.arpa");
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,-1);
+
+ done:
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(exitconn);
+ tor_free(on_circ);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, cache_hit_pending)
+
+/* Given that there is already a pending resolve for the given address,
+ * we want dns_resolve_impl to append our exit connection to list
+ * of pending connections for the pending DNS request and return 0.
+ */
+
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 0;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending = 0;
+
+ pending_connection_t *pending_conn = NULL;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ cached_resolve_t *cache_entry = tor_malloc_zero(sizeof(cached_resolve_t));
+ cache_entry->magic = CACHED_RESOLVE_MAGIC;
+ cache_entry->state = CACHE_STATE_PENDING;
+ cache_entry->minheap_idx = -1;
+ cache_entry->expire = time(NULL) + 60 * 60;
+
+ (void)arg;
+
+ TO_CONN(exitconn)->address = tor_strdup("torproject.org");
+
+ strlcpy(cache_entry->address, TO_CONN(exitconn)->address,
+ sizeof(cache_entry->address));
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+
+ dns_init();
+
+ dns_insert_cache_entry(cache_entry);
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,0);
+ tt_int_op(made_pending,==,1);
+
+ pending_conn = cache_entry->pending_connections;
+
+ tt_assert(pending_conn != NULL);
+ tt_assert(pending_conn->conn == exitconn);
+
+ done:
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ tor_free(on_circ);
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(cache_entry->pending_connections);
+ tor_free(cache_entry);
+ tor_free(exitconn);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, cache_hit_cached)
+
+/* Given that a finished DNS resolve is available in our cache, we want
+ * dns_resolve_impl() return it to called via resolve_out and pass the
+ * handling to set_exitconn_info_from_resolve function.
+ */
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 0;
+}
+
+static edge_connection_t *last_exitconn = NULL;
+static cached_resolve_t *last_resolve = NULL;
+
+static int
+NS(set_exitconn_info_from_resolve)(edge_connection_t *exitconn,
+ const cached_resolve_t *resolve,
+ char **hostname_out)
+{
+ last_exitconn = exitconn;
+ last_resolve = (cached_resolve_t *)resolve;
+
+ (void)hostname_out;
+
+ return 0;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending = 0;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ cached_resolve_t *resolve_out = NULL;
+
+ cached_resolve_t *cache_entry = tor_malloc_zero(sizeof(cached_resolve_t));
+ cache_entry->magic = CACHED_RESOLVE_MAGIC;
+ cache_entry->state = CACHE_STATE_CACHED;
+ cache_entry->minheap_idx = -1;
+ cache_entry->expire = time(NULL) + 60 * 60;
+
+ (void)arg;
+
+ TO_CONN(exitconn)->address = tor_strdup("torproject.org");
+
+ strlcpy(cache_entry->address, TO_CONN(exitconn)->address,
+ sizeof(cache_entry->address));
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+ NS_MOCK(set_exitconn_info_from_resolve);
+
+ dns_init();
+
+ dns_insert_cache_entry(cache_entry);
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ &resolve_out);
+
+ tt_int_op(retval,==,0);
+ tt_int_op(made_pending,==,0);
+ tt_assert(resolve_out == cache_entry);
+
+ tt_assert(last_exitconn == exitconn);
+ tt_assert(last_resolve == cache_entry);
+
+ done:
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ NS_UNMOCK(set_exitconn_info_from_resolve);
+ tor_free(on_circ);
+ tor_free(TO_CONN(exitconn)->address);
+ tor_free(cache_entry->pending_connections);
+ tor_free(cache_entry);
+ return;
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE ASPECT(resolve_impl, cache_miss)
+
+/* Given that there are neither pending nor pre-cached resolve for a given
+ * address, we want dns_resolve_impl() to create a new cached_resolve_t
+ * object, mark it as pending, insert it into the cache, attach the exit
+ * connection to list of pending connections and call launch_resolve()
+ * with the cached_resolve_t object it created.
+ */
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+ return 0;
+}
+
+static cached_resolve_t *last_launched_resolve = NULL;
+
+static int
+NS(launch_resolve)(cached_resolve_t *resolve)
+{
+ last_launched_resolve = resolve;
+
+ return 0;
+}
+
+static void
+NS(test_main)(void *arg)
+{
+ int retval;
+ int made_pending = 0;
+
+ pending_connection_t *pending_conn = NULL;
+
+ edge_connection_t *exitconn = create_valid_exitconn();
+ or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+ cached_resolve_t *cache_entry = NULL;
+ cached_resolve_t query;
+
+ (void)arg;
+
+ TO_CONN(exitconn)->address = tor_strdup("torproject.org");
+
+ strlcpy(query.address, TO_CONN(exitconn)->address, sizeof(query.address));
+
+ NS_MOCK(router_my_exit_policy_is_reject_star);
+ NS_MOCK(launch_resolve);
+
+ dns_init();
+
+ retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+ NULL);
+
+ tt_int_op(retval,==,0);
+ tt_int_op(made_pending,==,1);
+
+ cache_entry = dns_get_cache_entry(&query);
+
+ tt_assert(cache_entry);
+
+ pending_conn = cache_entry->pending_connections;
+
+ tt_assert(pending_conn != NULL);
+ tt_assert(pending_conn->conn == exitconn);
+
+ tt_assert(last_launched_resolve == cache_entry);
+ tt_str_op(cache_entry->address,==,TO_CONN(exitconn)->address);
+
+ done:
+ NS_UNMOCK(router_my_exit_policy_is_reject_star);
+ NS_UNMOCK(launch_resolve);
+ tor_free(on_circ);
+ tor_free(TO_CONN(exitconn)->address);
+ if (cache_entry)
+ tor_free(cache_entry->pending_connections);
+ tor_free(cache_entry);
+ tor_free(exitconn);
+ return;
+}
+
+#undef NS_SUBMODULE
+
struct testcase_t dns_tests[] = {
- { "clip_ttl", test_dns_clip_ttl, 0, NULL, NULL },
- { "expiry_ttl", test_dns_expiry_ttl, 0, NULL, NULL },
- { "resolve_outer", test_dns_resolve_outer, TT_FORK, NULL, NULL },
+ TEST_CASE(clip_ttl),
+ TEST_CASE(expiry_ttl),
+ TEST_CASE(resolve),
+ TEST_CASE_ASPECT(resolve_impl, addr_is_ip_no_need_to_resolve),
+ TEST_CASE_ASPECT(resolve_impl, non_exit),
+ TEST_CASE_ASPECT(resolve_impl, addr_is_invalid_dest),
+ TEST_CASE_ASPECT(resolve_impl, malformed_ptr),
+ TEST_CASE_ASPECT(resolve_impl, cache_hit_pending),
+ TEST_CASE_ASPECT(resolve_impl, cache_hit_cached),
+ TEST_CASE_ASPECT(resolve_impl, cache_miss),
END_OF_TESTCASES
};
+#undef NS_MODULE
+
diff --git a/src/test/test_entryconn.c b/src/test/test_entryconn.c
index 6edc166743..9580a1fd3f 100644
--- a/src/test/test_entryconn.c
+++ b/src/test/test_entryconn.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
diff --git a/src/test/test_entrynodes.c b/src/test/test_entrynodes.c
index 0011d3698a..b1c3accfab 100644
--- a/src/test/test_entrynodes.c
+++ b/src/test/test_entrynodes.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -9,14 +9,16 @@
#include "or.h"
#include "test.h"
+
+#include "config.h"
#include "entrynodes.h"
-#include "routerparse.h"
#include "nodelist.h"
-#include "util.h"
+#include "policies.h"
#include "routerlist.h"
+#include "routerparse.h"
#include "routerset.h"
#include "statefile.h"
-#include "config.h"
+#include "util.h"
#include "test_helpers.h"
@@ -70,6 +72,14 @@ fake_network_setup(const struct testcase_t *testcase)
return dummy_state;
}
+static or_options_t mocked_options;
+
+static const or_options_t *
+mock_get_options(void)
+{
+ return &mocked_options;
+}
+
/** Test choose_random_entry() with none of our routers being guard nodes. */
static void
test_choose_random_entry_no_guards(void *arg)
@@ -78,6 +88,14 @@ test_choose_random_entry_no_guards(void *arg)
(void) arg;
+ MOCK(get_options, mock_get_options);
+
+ /* Check that we get a guard if it passes preferred
+ * address settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientPreferIPv6ORPort = 0;
+
/* Try to pick an entry even though none of our routers are guards. */
chosen_entry = choose_random_entry(NULL);
@@ -86,8 +104,55 @@ test_choose_random_entry_no_guards(void *arg)
can't find a proper entry guard. */
tt_assert(chosen_entry);
+ /* And with the other IP version active */
+ mocked_options.ClientUseIPv6 = 1;
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
+ /* And with the preference on auto */
+ mocked_options.ClientPreferIPv6ORPort = -1;
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
+ /* Check that we don't get a guard if it doesn't pass mandatory address
+ * settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 0;
+ mocked_options.ClientPreferIPv6ORPort = 0;
+
+ chosen_entry = choose_random_entry(NULL);
+
+ /* If we don't allow IPv4 at all, we don't get a guard*/
+ tt_assert(!chosen_entry);
+
+ /* Check that we get a guard if it passes allowed but not preferred address
+ * settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientUseIPv6 = 1;
+ mocked_options.ClientPreferIPv6ORPort = 1;
+
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
+ /* Check that we get a guard if it passes preferred address settings when
+ * they're auto */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientPreferIPv6ORPort = -1;
+
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
+ /* And with IPv6 active */
+ mocked_options.ClientUseIPv6 = 1;
+
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
done:
- ;
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ UNMOCK(get_options);
}
/** Test choose_random_entry() with only one of our routers being a
@@ -101,17 +166,78 @@ test_choose_random_entry_one_possible_guard(void *arg)
(void) arg;
+ MOCK(get_options, mock_get_options);
+
/* Set one of the nodes to be a guard. */
our_nodelist = nodelist_get_list();
the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
the_guard->is_possible_guard = 1;
+ /* Check that we get the guard if it passes preferred
+ * address settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientPreferIPv6ORPort = 0;
+
/* Pick an entry. Make sure we pick the node we marked as guard. */
chosen_entry = choose_random_entry(NULL);
tt_ptr_op(chosen_entry, OP_EQ, the_guard);
+ /* And with the other IP version active */
+ mocked_options.ClientUseIPv6 = 1;
+ chosen_entry = choose_random_entry(NULL);
+ tt_ptr_op(chosen_entry, OP_EQ, the_guard);
+
+ /* And with the preference on auto */
+ mocked_options.ClientPreferIPv6ORPort = -1;
+ chosen_entry = choose_random_entry(NULL);
+ tt_ptr_op(chosen_entry, OP_EQ, the_guard);
+
+ /* Check that we don't get a guard if it doesn't pass mandatory address
+ * settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 0;
+ mocked_options.ClientPreferIPv6ORPort = 0;
+
+ chosen_entry = choose_random_entry(NULL);
+
+ /* If we don't allow IPv4 at all, we don't get a guard*/
+ tt_assert(!chosen_entry);
+
+ /* Check that we get a node if it passes allowed but not preferred
+ * address settings */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientUseIPv6 = 1;
+ mocked_options.ClientPreferIPv6ORPort = 1;
+
+ chosen_entry = choose_random_entry(NULL);
+
+ /* We disable the guard check and the preferred address check at the same
+ * time, so we can't be sure we get the guard */
+ tt_assert(chosen_entry);
+
+ /* Check that we get a node if it is allowed but not preferred when settings
+ * are auto */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientPreferIPv6ORPort = -1;
+
+ chosen_entry = choose_random_entry(NULL);
+
+ /* We disable the guard check and the preferred address check at the same
+ * time, so we can't be sure we get the guard */
+ tt_assert(chosen_entry);
+
+ /* and with IPv6 active */
+ mocked_options.ClientUseIPv6 = 1;
+
+ chosen_entry = choose_random_entry(NULL);
+ tt_assert(chosen_entry);
+
done:
- ;
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ UNMOCK(get_options);
}
/** Helper to conduct tests for populate_live_entry_guards().
@@ -624,6 +750,93 @@ test_entry_is_live(void *arg)
; /* XXX */
}
+#define TEST_IPV4_ADDR "123.45.67.89"
+#define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
+
+static void
+test_node_preferred_orport(void *arg)
+{
+ (void)arg;
+ tor_addr_t ipv4_addr;
+ const uint16_t ipv4_port = 4444;
+ tor_addr_t ipv6_addr;
+ const uint16_t ipv6_port = 6666;
+ routerinfo_t node_ri;
+ node_t node;
+ tor_addr_port_t ap;
+
+ /* Setup options */
+ memset(&mocked_options, 0, sizeof(mocked_options));
+ /* We don't test ClientPreferIPv6ORPort here, because it's used in
+ * nodelist_set_consensus to setup node.ipv6_preferred, which we set
+ * directly. */
+ MOCK(get_options, mock_get_options);
+
+ /* Setup IP addresses */
+ tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
+ tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
+
+ /* Setup node_ri */
+ memset(&node_ri, 0, sizeof(node_ri));
+ node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
+ node_ri.or_port = ipv4_port;
+ tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
+ node_ri.ipv6_orport = ipv6_port;
+
+ /* Setup node */
+ memset(&node, 0, sizeof(node));
+ node.ri = &node_ri;
+
+ /* Check the preferred address is IPv4 if we're only using IPv4, regardless
+ * of whether we prefer it or not */
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientUseIPv6 = 0;
+ node.ipv6_preferred = 0;
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
+ tt_assert(ap.port == ipv4_port);
+
+ node.ipv6_preferred = 1;
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
+ tt_assert(ap.port == ipv4_port);
+
+ /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
+ * don't prefer the IPv6 address */
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientUseIPv6 = 1;
+ node.ipv6_preferred = 0;
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
+ tt_assert(ap.port == ipv4_port);
+
+ /* Check the preferred address is IPv6 if we prefer it and
+ * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
+ mocked_options.ClientUseIPv4 = 1;
+ mocked_options.ClientUseIPv6 = 1;
+ node.ipv6_preferred = 1;
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
+ tt_assert(ap.port == ipv6_port);
+
+ mocked_options.ClientUseIPv4 = 0;
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
+ tt_assert(ap.port == ipv6_port);
+
+ /* Check the preferred address is IPv6 if we don't prefer it, but
+ * ClientUseIPv4 is 0 */
+ mocked_options.ClientUseIPv4 = 0;
+ mocked_options.ClientUseIPv6 = 1;
+ node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
+ node_get_pref_orport(&node, &ap);
+ tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
+ tt_assert(ap.port == ipv6_port);
+
+ done:
+ UNMOCK(get_options);
+}
+
static const struct testcase_setup_t fake_network = {
fake_network_setup, fake_network_cleanup
};
@@ -654,6 +867,9 @@ struct testcase_t entrynodes_tests[] = {
{ "entry_is_live",
test_entry_is_live,
TT_FORK, &fake_network, NULL },
+ { "node_preferred_orport",
+ test_node_preferred_orport,
+ 0, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_extorport.c b/src/test/test_extorport.c
index 2e5a32eef3..1f92780177 100644
--- a/src/test/test_extorport.c
+++ b/src/test/test_extorport.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CONNECTION_PRIVATE
@@ -309,15 +309,14 @@ test_ext_or_cookie_auth(void *arg)
tor_free(client_hash2);
}
-static int
+static void
crypto_rand_return_tse_str(char *to, size_t n)
{
if (n != 32) {
TT_FAIL(("Asked for %d bytes, not 32", (int)n));
- return -1;
+ return;
}
memcpy(to, "te road There is always another ", 32);
- return 0;
}
static void
diff --git a/src/test/test_guardfraction.c b/src/test/test_guardfraction.c
index 57063c9085..8173e44d47 100644
--- a/src/test/test_guardfraction.c
+++ b/src/test/test_guardfraction.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define DIRSERV_PRIVATE
@@ -17,6 +17,7 @@
#include "test.h"
#include "test_helpers.h"
+#include "log_test_helpers.h"
/** Generate a vote_routerstatus_t for a router with identity digest
* <b>digest_in_hex</b>. */
@@ -40,7 +41,7 @@ gen_vote_routerstatus_for_tests(const char *digest_in_hex, int is_guard)
tt_int_op(strlen(digest_in_hex), ==, HEX_DIGEST_LEN);
retval = base16_decode(digest_tmp, sizeof(digest_tmp),
digest_in_hex, HEX_DIGEST_LEN);
- tt_int_op(retval, ==, 0);
+ tt_int_op(retval, ==, sizeof(digest_tmp));
memcpy(rs->identity_digest, digest_tmp, DIGEST_LEN);
}
@@ -304,11 +305,15 @@ test_parse_guardfraction_consensus(void *arg)
memset(&rs_no_guard, 0, sizeof(routerstatus_t));
tt_assert(!rs_no_guard.is_possible_guard);
+ setup_full_capture_of_logs(LOG_WARN);
retval = routerstatus_parse_guardfraction(guardfraction_str_good,
NULL, NULL,
&rs_no_guard);
tt_int_op(retval, ==, 0);
tt_assert(!rs_no_guard.has_guardfraction);
+ expect_single_log_msg_containing("Got GuardFraction for non-guard . "
+ "This is not supposed to happen.");
+ teardown_capture_of_logs();
}
{ /* Bad GuardFraction. Function should fail and not apply. */
@@ -334,7 +339,7 @@ test_parse_guardfraction_consensus(void *arg)
}
done:
- ;
+ teardown_capture_of_logs();
}
/** Make sure that we use GuardFraction information when we should,
diff --git a/src/test/test_handles.c b/src/test/test_handles.c
new file mode 100644
index 0000000000..536a478689
--- /dev/null
+++ b/src/test/test_handles.c
@@ -0,0 +1,95 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "test.h"
+
+#include "util.h"
+#include "handles.h"
+
+typedef struct demo_t {
+ HANDLE_ENTRY(demo, demo_t);
+ int val;
+} demo_t;
+
+HANDLE_DECL(demo, demo_t, static)
+HANDLE_IMPL(demo, demo_t, static)
+
+static demo_t *
+demo_new(int val)
+{
+ demo_t *d = tor_malloc_zero(sizeof(demo_t));
+ d->val = val;
+ return d;
+}
+
+static void
+demo_free(demo_t *d)
+{
+ if (d == NULL)
+ return;
+ demo_handles_clear(d);
+ tor_free(d);
+}
+
+static void
+test_handle_basic(void *arg)
+{
+ (void) arg;
+ demo_t *d1 = NULL, *d2 = NULL;
+ demo_handle_t *wr1 = NULL, *wr2 = NULL, *wr3 = NULL, *wr4 = NULL;
+
+ d1 = demo_new(9000);
+ d2 = demo_new(9009);
+
+ wr1 = demo_handle_new(d1);
+ wr2 = demo_handle_new(d1);
+ wr3 = demo_handle_new(d1);
+ wr4 = demo_handle_new(d2);
+
+ tt_assert(wr1);
+ tt_assert(wr2);
+ tt_assert(wr3);
+ tt_assert(wr4);
+
+ tt_ptr_op(demo_handle_get(wr1), OP_EQ, d1);
+ tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
+ tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
+ tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
+
+ demo_handle_free(wr1);
+ wr1 = NULL;
+ tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
+ tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
+ tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
+
+ demo_free(d1);
+ d1 = NULL;
+ tt_ptr_op(demo_handle_get(wr2), OP_EQ, NULL);
+ tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
+ tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
+
+ demo_handle_free(wr2);
+ wr2 = NULL;
+ tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
+ tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
+
+ demo_handle_free(wr3);
+ wr3 = NULL;
+ done:
+ demo_handle_free(wr1);
+ demo_handle_free(wr2);
+ demo_handle_free(wr3);
+ demo_handle_free(wr4);
+ demo_free(d1);
+ demo_free(d2);
+}
+
+#define HANDLE_TEST(name, flags) \
+ { #name, test_handle_ ##name, (flags), NULL, NULL }
+
+struct testcase_t handle_tests[] = {
+ HANDLE_TEST(basic, 0),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_helpers.c b/src/test/test_helpers.c
index c3ca0c3554..ae9fc7a243 100644
--- a/src/test/test_helpers.c
+++ b/src/test/test_helpers.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
@@ -16,7 +16,15 @@
#include "test.h"
#include "test_helpers.h"
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+DISABLE_GCC_WARNING(overlength-strings)
+/* We allow huge string constants in the unit tests, but not in the code
+ * at large. */
+#endif
#include "test_descriptors.inc"
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+ENABLE_GCC_WARNING(overlength-strings)
+#endif
/* Return a statically allocated string representing yesterday's date
* in ISO format. We use it so that state file items are not found to
diff --git a/src/test/test_helpers.h b/src/test/test_helpers.h
index 369243b459..684375e1b1 100644
--- a/src/test/test_helpers.h
+++ b/src/test/test_helpers.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#ifndef TOR_TEST_HELPERS_H
diff --git a/src/test/test_hs.c b/src/test/test_hs.c
index 126e211858..8237bbc50e 100644
--- a/src/test/test_hs.c
+++ b/src/test/test_hs.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2015, The Tor Project, Inc. */
+/* Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
@@ -8,12 +8,14 @@
#define CONTROL_PRIVATE
#define CIRCUITBUILD_PRIVATE
+#define RENDSERVICE_PRIVATE
#include "or.h"
#include "test.h"
#include "control.h"
#include "config.h"
#include "rendcommon.h"
+#include "rendservice.h"
#include "routerset.h"
#include "circuitbuild.h"
#include "test_helpers.h"
@@ -435,6 +437,347 @@ test_hs_rend_data(void *arg)
rend_data_free(client_dup);
}
+/* Test encoding and decoding service authorization cookies */
+static void
+test_hs_auth_cookies(void *arg)
+{
+#define TEST_COOKIE_RAW ((const uint8_t *) "abcdefghijklmnop")
+#define TEST_COOKIE_ENCODED "YWJjZGVmZ2hpamtsbW5vcA"
+#define TEST_COOKIE_ENCODED_STEALTH "YWJjZGVmZ2hpamtsbW5vcB"
+#define TEST_COOKIE_ENCODED_INVALID "YWJjZGVmZ2hpamtsbW5vcD"
+
+ char *encoded_cookie;
+ uint8_t raw_cookie[REND_DESC_COOKIE_LEN];
+ rend_auth_type_t auth_type;
+ char *err_msg;
+ int re;
+
+ (void)arg;
+
+ /* Test that encoding gives the expected result */
+ encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_BASIC_AUTH);
+ tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED);
+ tor_free(encoded_cookie);
+
+ encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_STEALTH_AUTH);
+ tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED_STEALTH);
+ tor_free(encoded_cookie);
+
+ /* Decoding should give the original value */
+ re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED, raw_cookie, &auth_type,
+ &err_msg);
+ tt_assert(!re);
+ tt_assert(!err_msg);
+ tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
+ tt_int_op(auth_type, OP_EQ, REND_BASIC_AUTH);
+ memset(raw_cookie, 0, sizeof(raw_cookie));
+
+ re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_STEALTH, raw_cookie,
+ &auth_type, &err_msg);
+ tt_assert(!re);
+ tt_assert(!err_msg);
+ tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
+ tt_int_op(auth_type, OP_EQ, REND_STEALTH_AUTH);
+ memset(raw_cookie, 0, sizeof(raw_cookie));
+
+ /* Decoding with padding characters should also work */
+ re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED "==", raw_cookie, NULL,
+ &err_msg);
+ tt_assert(!re);
+ tt_assert(!err_msg);
+ tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
+
+ /* Decoding with an unknown type should fail */
+ re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_INVALID, raw_cookie,
+ &auth_type, &err_msg);
+ tt_int_op(re, OP_LT, 0);
+ tt_assert(err_msg);
+ tor_free(err_msg);
+
+ done:
+ return;
+}
+
+static int mock_get_options_calls = 0;
+static or_options_t *mock_options = NULL;
+
+static void
+reset_options(or_options_t *options, int *get_options_calls)
+{
+ memset(options, 0, sizeof(or_options_t));
+ options->TestingTorNetwork = 1;
+
+ *get_options_calls = 0;
+}
+
+static const or_options_t *
+mock_get_options(void)
+{
+ ++mock_get_options_calls;
+ tor_assert(mock_options);
+ return mock_options;
+}
+
+/* arg can't be 0 (the test fails) or 2 (the test is skipped) */
+#define CREATE_HS_DIR_NONE ((intptr_t)0x04)
+#define CREATE_HS_DIR1 ((intptr_t)0x08)
+#define CREATE_HS_DIR2 ((intptr_t)0x10)
+
+/* Test that single onion poisoning works. */
+static void
+test_single_onion_poisoning(void *arg)
+{
+ or_options_t opt;
+ mock_options = &opt;
+ reset_options(mock_options, &mock_get_options_calls);
+ MOCK(get_options, mock_get_options);
+
+ int ret = -1;
+ intptr_t create_dir_mask = (intptr_t)arg;
+ /* Get directories with a random suffix so we can repeat the tests */
+ mock_options->DataDirectory = tor_strdup(get_fname_rnd("test_data_dir"));
+ rend_service_t *service_1 = tor_malloc_zero(sizeof(rend_service_t));
+ char *dir1 = tor_strdup(get_fname_rnd("test_hs_dir1"));
+ rend_service_t *service_2 = tor_malloc_zero(sizeof(rend_service_t));
+ char *dir2 = tor_strdup(get_fname_rnd("test_hs_dir2"));
+ smartlist_t *services = smartlist_new();
+ char *poison_path = NULL;
+
+ /* No services, no service to verify, no problem! */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_config_services(mock_options, 1);
+ tt_assert(ret == 0);
+
+ /* Either way, no problem. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_config_services(mock_options, 1);
+ tt_assert(ret == 0);
+
+ /* Create the data directory, and, if the correct bit in arg is set,
+ * create a directory for that service.
+ * The data directory is required for the lockfile, which is used when
+ * loading keys. */
+ ret = check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
+ tt_assert(ret == 0);
+ if (create_dir_mask & CREATE_HS_DIR1) {
+ ret = check_private_dir(dir1, CPD_CREATE, NULL);
+ tt_assert(ret == 0);
+ }
+ if (create_dir_mask & CREATE_HS_DIR2) {
+ ret = check_private_dir(dir2, CPD_CREATE, NULL);
+ tt_assert(ret == 0);
+ }
+
+ service_1->directory = dir1;
+ service_2->directory = dir2;
+ /* The services own the directory pointers now */
+ dir1 = dir2 = NULL;
+ /* Add port to service 1 */
+ service_1->ports = smartlist_new();
+ service_2->ports = smartlist_new();
+ char *err_msg = NULL;
+ rend_service_port_config_t *port1 = rend_service_parse_port_config("80", " ",
+ &err_msg);
+ tt_assert(port1);
+ tt_assert(!err_msg);
+ smartlist_add(service_1->ports, port1);
+
+ rend_service_port_config_t *port2 = rend_service_parse_port_config("90", " ",
+ &err_msg);
+ /* Add port to service 2 */
+ tt_assert(port2);
+ tt_assert(!err_msg);
+ smartlist_add(service_2->ports, port2);
+
+ /* No services, a service to verify, no problem! */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Either way, no problem. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Add the first service */
+ ret = rend_service_check_dir_and_add(services, mock_options, service_1, 0);
+ tt_assert(ret == 0);
+ /* But don't add the second service yet. */
+
+ /* Service directories, but no previous keys, no problem! */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Either way, no problem. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Poison! Poison! Poison!
+ * This can only be done in HiddenServiceSingleHopMode. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
+ tt_assert(ret == 0);
+ /* Poisoning twice is a no-op. */
+ ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
+ tt_assert(ret == 0);
+
+ /* Poisoned service directories, but no previous keys, no problem! */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Either way, no problem. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Now add some keys, and we'll have a problem. */
+ ret = rend_service_load_all_keys(services);
+ tt_assert(ret == 0);
+
+ /* Poisoned service directories with previous keys are not allowed. */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret < 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* But they are allowed if we're in non-anonymous mode. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Re-poisoning directories with existing keys is a no-op, because
+ * directories with existing keys are ignored. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
+ tt_assert(ret == 0);
+ /* And it keeps the poison. */
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Now add the second service: it has no key and no poison file */
+ ret = rend_service_check_dir_and_add(services, mock_options, service_2, 0);
+ tt_assert(ret == 0);
+
+ /* A new service, and an existing poisoned service. Not ok. */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret < 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* But ok to add in non-anonymous mode. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Now remove the poisoning from the first service, and we have the opposite
+ * problem. */
+ poison_path = rend_service_sos_poison_path(service_1);
+ tt_assert(poison_path);
+ ret = unlink(poison_path);
+ tt_assert(ret == 0);
+
+ /* Unpoisoned service directories with previous keys are ok, as are empty
+ * directories. */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* But the existing unpoisoned key is not ok in non-anonymous mode, even if
+ * there is an empty service. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret < 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Poisoning directories with existing keys is a no-op, because directories
+ * with existing keys are ignored. But the new directory should poison. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
+ tt_assert(ret == 0);
+ /* And the old directory remains unpoisoned. */
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret < 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* And the new directory should be ignored, because it has no key. */
+ mock_options->HiddenServiceSingleHopMode = 0;
+ mock_options->HiddenServiceNonAnonymousMode = 0;
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ /* Re-poisoning directories without existing keys is a no-op. */
+ mock_options->HiddenServiceSingleHopMode = 1;
+ mock_options->HiddenServiceNonAnonymousMode = 1;
+ ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
+ tt_assert(ret == 0);
+ ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
+ tt_assert(ret == 0);
+ /* And the old directory remains unpoisoned. */
+ ret = rend_service_verify_single_onion_poison(service_1, mock_options);
+ tt_assert(ret < 0);
+ ret = rend_service_verify_single_onion_poison(service_2, mock_options);
+ tt_assert(ret == 0);
+
+ done:
+ /* The test harness deletes the directories at exit */
+ tor_free(poison_path);
+ tor_free(dir1);
+ tor_free(dir2);
+ smartlist_free(services);
+ rend_service_free(service_1);
+ rend_service_free(service_2);
+ UNMOCK(get_options);
+ tor_free(mock_options->DataDirectory);
+}
+
struct testcase_t hs_tests[] = {
{ "hs_rend_data", test_hs_rend_data, TT_FORK,
NULL, NULL },
@@ -445,6 +788,16 @@ struct testcase_t hs_tests[] = {
{ "pick_bad_tor2web_rendezvous_node",
test_pick_bad_tor2web_rendezvous_node, TT_FORK,
NULL, NULL },
+ { "hs_auth_cookies", test_hs_auth_cookies, TT_FORK,
+ NULL, NULL },
+ { "single_onion_poisoning_create_dir_none", test_single_onion_poisoning,
+ TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR_NONE) },
+ { "single_onion_poisoning_create_dir1", test_single_onion_poisoning,
+ TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1) },
+ { "single_onion_poisoning_create_dir2", test_single_onion_poisoning,
+ TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR2) },
+ { "single_onion_poisoning_create_dir_both", test_single_onion_poisoning,
+ TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1 | CREATE_HS_DIR2) },
END_OF_TESTCASES
};
diff --git a/src/test/test_introduce.c b/src/test/test_introduce.c
index 0cab8ef4cc..810b03c93d 100644
--- a/src/test/test_introduce.c
+++ b/src/test/test_introduce.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015, The Tor Project, Inc. */
+/* Copyright (c) 2012-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -9,8 +9,6 @@
#define RENDSERVICE_PRIVATE
#include "rendservice.h"
-extern const char AUTHORITY_SIGNKEY_1[];
-
static uint8_t v0_test_plaintext[] =
/* 20 bytes of rendezvous point nickname */
{ 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65,
diff --git a/src/test/test_keypin.c b/src/test/test_keypin.c
index bd0f6fdd52..95657349c6 100644
--- a/src/test/test_keypin.c
+++ b/src/test/test_keypin.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
diff --git a/src/test/test_link_handshake.c b/src/test/test_link_handshake.c
index 7ad2c30d0f..ddf66f4d34 100644
--- a/src/test/test_link_handshake.c
+++ b/src/test/test_link_handshake.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -15,8 +15,9 @@
#include "scheduler.h"
#include "test.h"
+#include "log_test_helpers.h"
-var_cell_t *mock_got_var_cell = NULL;
+static var_cell_t *mock_got_var_cell = NULL;
static void
mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
@@ -65,6 +66,14 @@ mock_send_authenticate(or_connection_t *conn, int type)
return 0;
}
+static tor_x509_cert_t *mock_own_cert = NULL;
+static tor_x509_cert_t *
+mock_get_own_cert(tor_tls_t *tls)
+{
+ (void)tls;
+ return tor_x509_cert_dup(mock_own_cert);
+}
+
/* Test good certs cells */
static void
test_link_handshake_certs_ok(void *arg)
@@ -83,6 +92,7 @@ test_link_handshake_certs_ok(void *arg)
MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
MOCK(connection_or_send_netinfo, mock_send_netinfo);
+ MOCK(tor_tls_get_own_cert, mock_get_own_cert);
key1 = pk_generate(2);
key2 = pk_generate(3);
@@ -93,6 +103,12 @@ test_link_handshake_certs_ok(void *arg)
tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
key1, key2, 86400), ==, 0);
+ {
+ const tor_x509_cert_t *link_cert = NULL;
+ tt_assert(!tor_tls_get_my_certs(1, &link_cert, NULL));
+ mock_own_cert = tor_x509_cert_dup(link_cert);
+ }
+
c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
c1->link_proto = 3;
tt_int_op(connection_init_or_handshake_state(c1, 1), ==, 0);
@@ -173,6 +189,11 @@ test_link_handshake_certs_ok(void *arg)
UNMOCK(tor_tls_cert_matches_key);
UNMOCK(connection_or_write_var_cell_to_buf);
UNMOCK(connection_or_send_netinfo);
+ UNMOCK(tor_tls_get_own_cert);
+ tor_x509_cert_free(mock_own_cert);
+ mock_own_cert = NULL;
+ memset(c1->identity_digest, 0, sizeof(c1->identity_digest));
+ memset(c2->identity_digest, 0, sizeof(c2->identity_digest));
connection_free_(TO_CONN(c1));
connection_free_(TO_CONN(c2));
tor_free(cell1);
@@ -209,6 +230,7 @@ recv_certs_cleanup(const struct testcase_t *test, void *obj)
if (d) {
tor_free(d->cell);
certs_cell_free(d->ccell);
+ connection_or_remove_from_identity_map(d->c);
connection_free_(TO_CONN(d->c));
circuitmux_free(d->chan->base_.cmux);
tor_free(d->chan);
@@ -332,30 +354,50 @@ test_link_handshake_recv_certs_ok_server(void *arg)
test_link_handshake_recv_certs_ ## name(void *arg) \
{ \
certs_data_t *d = arg; \
+ const char *require_failure_message = NULL; \
+ setup_capture_of_logs(LOG_INFO); \
{ code ; } \
channel_tls_process_certs_cell(d->cell, d->chan); \
tt_int_op(1, ==, mock_close_called); \
tt_int_op(0, ==, mock_send_authenticate_called); \
tt_int_op(0, ==, mock_send_netinfo_called); \
+ if (require_failure_message) { \
+ expect_log_msg_containing(require_failure_message); \
+ } \
done: \
- ; \
+ teardown_capture_of_logs(); \
}
-CERTS_FAIL(badstate, d->c->base_.state = OR_CONN_STATE_CONNECTING)
-CERTS_FAIL(badproto, d->c->link_proto = 2)
-CERTS_FAIL(duplicate, d->c->handshake_state->received_certs_cell = 1)
+CERTS_FAIL(badstate,
+ require_failure_message = "We're not doing a v3 handshake!";
+ d->c->base_.state = OR_CONN_STATE_CONNECTING;)
+CERTS_FAIL(badproto,
+ require_failure_message = "not using link protocol >= 3";
+ d->c->link_proto = 2)
+CERTS_FAIL(duplicate,
+ require_failure_message = "We already got one";
+ d->c->handshake_state->received_certs_cell = 1)
CERTS_FAIL(already_authenticated,
+ require_failure_message = "We're already authenticated!";
d->c->handshake_state->authenticated = 1)
-CERTS_FAIL(empty, d->cell->payload_len = 0)
-CERTS_FAIL(bad_circid, d->cell->circ_id = 1)
-CERTS_FAIL(truncated_1, d->cell->payload[0] = 5)
+CERTS_FAIL(empty,
+ require_failure_message = "It had no body";
+ d->cell->payload_len = 0)
+CERTS_FAIL(bad_circid,
+ require_failure_message = "It had a nonzero circuit ID";
+ d->cell->circ_id = 1)
+CERTS_FAIL(truncated_1,
+ require_failure_message = "It couldn't be parsed";
+ d->cell->payload[0] = 5)
CERTS_FAIL(truncated_2,
{
+ require_failure_message = "It couldn't be parsed";
d->cell->payload_len = 4;
memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
})
CERTS_FAIL(truncated_3,
{
+ require_failure_message = "It couldn't be parsed";
d->cell->payload_len = 7;
memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
})
@@ -367,30 +409,35 @@ CERTS_FAIL(truncated_3,
CERTS_FAIL(not_x509,
{
+ require_failure_message = "Received undecodable certificate";
certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
REENCODE();
})
CERTS_FAIL(both_link,
{
+ require_failure_message = "Duplicate x509 certificate";
certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
REENCODE();
})
CERTS_FAIL(both_id_rsa,
{
+ require_failure_message = "Duplicate x509 certificate";
certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
REENCODE();
})
CERTS_FAIL(both_auth,
{
+ require_failure_message = "Duplicate x509 certificate";
certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
REENCODE();
})
CERTS_FAIL(wrong_labels_1,
{
+ require_failure_message = "The link certificate was not valid";
certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
REENCODE();
@@ -401,6 +448,7 @@ CERTS_FAIL(wrong_labels_2,
const tor_x509_cert_t *b;
const uint8_t *enca;
size_t lena;
+ require_failure_message = "The link certificate was not valid";
tor_tls_get_my_certs(1, &a, &b);
tor_x509_cert_get_der(a, &enca, &lena);
certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
@@ -411,16 +459,20 @@ CERTS_FAIL(wrong_labels_2,
})
CERTS_FAIL(wrong_labels_3,
{
+ require_failure_message = "The certs we wanted were missing";
certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
REENCODE();
})
CERTS_FAIL(server_missing_certs,
{
+ require_failure_message = "The certs we wanted were missing";
d->c->handshake_state->started_here = 0;
})
CERTS_FAIL(server_wrong_labels_1,
{
+ require_failure_message =
+ "The authentication certificate was not valid";
d->c->handshake_state->started_here = 0;
certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
@@ -579,38 +631,55 @@ test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
test_link_handshake_recv_authchallenge_ ## name(void *arg) \
{ \
authchallenge_data_t *d = arg; \
+ const char *require_failure_message = NULL; \
+ setup_capture_of_logs(LOG_INFO); \
{ code ; } \
channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
tt_int_op(1, ==, mock_close_called); \
tt_int_op(0, ==, mock_send_authenticate_called); \
tt_int_op(0, ==, mock_send_netinfo_called); \
+ if (require_failure_message) { \
+ expect_log_msg_containing(require_failure_message); \
+ } \
done: \
- ; \
+ teardown_capture_of_logs(); \
}
AUTHCHALLENGE_FAIL(badstate,
+ require_failure_message = "We're not currently doing a "
+ "v3 handshake";
d->c->base_.state = OR_CONN_STATE_CONNECTING)
AUTHCHALLENGE_FAIL(badproto,
+ require_failure_message = "not using link protocol >= 3";
d->c->link_proto = 2)
AUTHCHALLENGE_FAIL(as_server,
+ require_failure_message = "We didn't originate this "
+ "connection";
d->c->handshake_state->started_here = 0;)
AUTHCHALLENGE_FAIL(duplicate,
+ require_failure_message = "We already received one";
d->c->handshake_state->received_auth_challenge = 1)
AUTHCHALLENGE_FAIL(nocerts,
+ require_failure_message = "We haven't gotten a CERTS "
+ "cell yet";
d->c->handshake_state->received_certs_cell = 0)
AUTHCHALLENGE_FAIL(tooshort,
+ require_failure_message = "It was not well-formed";
d->cell->payload_len = 33)
AUTHCHALLENGE_FAIL(truncated,
+ require_failure_message = "It was not well-formed";
d->cell->payload_len = 34)
AUTHCHALLENGE_FAIL(nonzero_circid,
+ require_failure_message = "It had a nonzero circuit ID";
d->cell->circ_id = 1337)
static tor_x509_cert_t *mock_peer_cert = NULL;
+
static tor_x509_cert_t *
mock_get_peer_cert(tor_tls_t *tls)
{
(void)tls;
- return mock_peer_cert;
+ return tor_x509_cert_dup(mock_peer_cert);
}
static int
@@ -644,12 +713,15 @@ authenticate_data_cleanup(const struct testcase_t *test, void *arg)
(void) test;
UNMOCK(connection_or_write_var_cell_to_buf);
UNMOCK(tor_tls_get_peer_cert);
+ UNMOCK(tor_tls_get_own_cert);
UNMOCK(tor_tls_get_tlssecrets);
UNMOCK(connection_or_close_for_error);
UNMOCK(channel_set_circid_type);
authenticate_data_t *d = arg;
if (d) {
tor_free(d->cell);
+ connection_or_remove_from_identity_map(d->c1);
+ connection_or_remove_from_identity_map(d->c2);
connection_free_(TO_CONN(d->c1));
connection_free_(TO_CONN(d->c2));
circuitmux_free(d->chan2->base_.cmux);
@@ -658,7 +730,10 @@ authenticate_data_cleanup(const struct testcase_t *test, void *arg)
crypto_pk_free(d->key2);
tor_free(d);
}
+ tor_x509_cert_free(mock_peer_cert);
+ tor_x509_cert_free(mock_own_cert);
mock_peer_cert = NULL;
+ mock_own_cert = NULL;
return 1;
}
@@ -672,11 +747,14 @@ authenticate_data_setup(const struct testcase_t *test)
MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
+ MOCK(tor_tls_get_own_cert, mock_get_own_cert);
MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
MOCK(connection_or_close_for_error, mock_close_for_err);
MOCK(channel_set_circid_type, mock_set_circid_type);
d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
+ tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
+ tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
d->key1 = pk_generate(2);
d->key2 = pk_generate(3);
@@ -719,6 +797,8 @@ authenticate_data_setup(const struct testcase_t *test)
tor_x509_cert_get_der(link_cert, &der, &sz);
mock_peer_cert = tor_x509_cert_decode(der, sz);
tt_assert(mock_peer_cert);
+ mock_own_cert = tor_x509_cert_decode(der, sz);
+ tt_assert(mock_own_cert);
tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
tor_x509_cert_get_der(auth_cert, &der, &sz);
d->c2->handshake_state->auth_cert = tor_x509_cert_decode(der, sz);
@@ -798,57 +878,83 @@ test_link_handshake_auth_cell(void *arg)
test_link_handshake_auth_ ## name(void *arg) \
{ \
authenticate_data_t *d = arg; \
+ const char *require_failure_message = NULL; \
+ setup_capture_of_logs(LOG_INFO); \
{ code ; } \
tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
channel_tls_process_authenticate_cell(d->cell, d->chan2); \
tt_int_op(mock_close_called, ==, 1); \
tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
- done: \
- ; \
+ if (require_failure_message) { \
+ expect_log_msg_containing(require_failure_message); \
+ } \
+ done: \
+ teardown_capture_of_logs(); \
}
AUTHENTICATE_FAIL(badstate,
+ require_failure_message = "We're not doing a v3 handshake";
d->c2->base_.state = OR_CONN_STATE_CONNECTING)
AUTHENTICATE_FAIL(badproto,
+ require_failure_message = "not using link protocol >= 3";
d->c2->link_proto = 2)
AUTHENTICATE_FAIL(atclient,
+ require_failure_message = "We originated this connection";
d->c2->handshake_state->started_here = 1)
AUTHENTICATE_FAIL(duplicate,
+ require_failure_message = "We already got one";
d->c2->handshake_state->received_authenticate = 1)
static void
test_link_handshake_auth_already_authenticated(void *arg)
{
authenticate_data_t *d = arg;
+ setup_capture_of_logs(LOG_INFO);
d->c2->handshake_state->authenticated = 1;
channel_tls_process_authenticate_cell(d->cell, d->chan2);
tt_int_op(mock_close_called, ==, 1);
tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
+ expect_log_msg_containing("The peer is already authenticated");
done:
- ;
+ teardown_capture_of_logs();
}
+
AUTHENTICATE_FAIL(nocerts,
+ require_failure_message = "We never got a certs cell";
d->c2->handshake_state->received_certs_cell = 0)
AUTHENTICATE_FAIL(noidcert,
+ require_failure_message = "We never got an identity "
+ "certificate";
tor_x509_cert_free(d->c2->handshake_state->id_cert);
d->c2->handshake_state->id_cert = NULL)
AUTHENTICATE_FAIL(noauthcert,
+ require_failure_message = "We never got an authentication "
+ "certificate";
tor_x509_cert_free(d->c2->handshake_state->auth_cert);
d->c2->handshake_state->auth_cert = NULL)
AUTHENTICATE_FAIL(tooshort,
+ require_failure_message = "Cell was way too short";
d->cell->payload_len = 3)
AUTHENTICATE_FAIL(badtype,
+ require_failure_message = "Authenticator type was not "
+ "recognized";
d->cell->payload[0] = 0xff)
AUTHENTICATE_FAIL(truncated_1,
+ require_failure_message = "Authenticator was truncated";
d->cell->payload[2]++)
AUTHENTICATE_FAIL(truncated_2,
+ require_failure_message = "Authenticator was truncated";
d->cell->payload[3]++)
AUTHENTICATE_FAIL(tooshort_1,
+ require_failure_message = "Authenticator was too short";
tt_int_op(d->cell->payload_len, >=, 260);
d->cell->payload[2] -= 1;
d->cell->payload_len -= 256;)
AUTHENTICATE_FAIL(badcontent,
+ require_failure_message = "Some field in the AUTHENTICATE "
+ "cell body was not as expected";
d->cell->payload[10] ^= 0xff)
AUTHENTICATE_FAIL(badsig_1,
+ require_failure_message = "Signature wasn't valid";
d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
#define TEST(name, flags) \
diff --git a/src/test/test_logging.c b/src/test/test_logging.c
index 6205b3bdc5..15471e46d0 100644
--- a/src/test/test_logging.c
+++ b/src/test/test_logging.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -127,9 +127,47 @@ test_sigsafe_err(void *arg)
smartlist_free(lines);
}
+static void
+test_ratelim(void *arg)
+{
+ (void) arg;
+ ratelim_t ten_min = RATELIM_INIT(10*60);
+
+ const time_t start = 1466091600;
+ time_t now = start;
+ /* Initially, we're ready. */
+
+ char *msg = NULL;
+
+ msg = rate_limit_log(&ten_min, now);
+ tt_assert(msg != NULL);
+ tt_str_op(msg, OP_EQ, ""); /* nothing was suppressed. */
+
+ tt_int_op(ten_min.last_allowed, OP_EQ, now);
+ tor_free(msg);
+
+ int i;
+ for (i = 0; i < 9; ++i) {
+ now += 60; /* one minute has passed. */
+ msg = rate_limit_log(&ten_min, now);
+ tt_assert(msg == NULL);
+ tt_int_op(ten_min.last_allowed, OP_EQ, start);
+ tt_int_op(ten_min.n_calls_since_last_time, OP_EQ, i + 1);
+ }
+
+ now += 240; /* Okay, we can be done. */
+ msg = rate_limit_log(&ten_min, now);
+ tt_assert(msg != NULL);
+ tt_str_op(msg, OP_EQ,
+ " [9 similar message(s) suppressed in last 600 seconds]");
+ done:
+ tor_free(msg);
+}
+
struct testcase_t logging_tests[] = {
{ "sigsafe_err_fds", test_get_sigsafe_err_fds, TT_FORK, NULL, NULL },
{ "sigsafe_err", test_sigsafe_err, TT_FORK, NULL, NULL },
+ { "ratelim", test_ratelim, 0, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c
index b205eff24e..2ae605b8db 100644
--- a/src/test/test_microdesc.c
+++ b/src/test/test_microdesc.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2015, The Tor Project, Inc. */
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -14,9 +14,11 @@
#include "test.h"
+DISABLE_GCC_WARNING(redundant-decls)
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
+ENABLE_GCC_WARNING(redundant-decls)
#ifdef _WIN32
/* For mkdir() */
@@ -483,13 +485,18 @@ test_md_generate(void *arg)
md = dirvote_create_microdescriptor(ri, 21);
tt_str_op(md->body, ==, test_md2_21);
tt_assert(ed25519_pubkey_eq(md->ed25519_identity_pkey,
- &ri->signing_key_cert->signing_key));
+ &ri->cache_info.signing_key_cert->signing_key));
done:
microdesc_free(md);
routerinfo_free(ri);
}
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+DISABLE_GCC_WARNING(overlength-strings)
+/* We allow huge string constants in the unit tests, but not in the code
+ * at large. */
+#endif
/* Taken at random from my ~/.tor/cached-microdescs file and then
* hand-munged */
static const char MD_PARSE_TEST_DATA[] =
@@ -645,6 +652,9 @@ static const char MD_PARSE_TEST_DATA[] =
"id rsa1024 2A8wYpHxnkKJ92orocvIQBzeHlE\n"
"p6 allow 80\n"
;
+#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
+ENABLE_GCC_WARNING(overlength-strings)
+#endif
/** More tests for parsing different kinds of microdescriptors, and getting
* invalid digests trackd from them. */
@@ -706,7 +716,7 @@ test_md_parse(void *arg)
tt_int_op(md->ipv6_orport, OP_EQ, 9090);
done:
- SMARTLIST_FOREACH(mds, microdesc_t *, md, microdesc_free(md));
+ SMARTLIST_FOREACH(mds, microdesc_t *, mdsc, microdesc_free(mdsc));
smartlist_free(mds);
SMARTLIST_FOREACH(invalid, char *, cp, tor_free(cp));
smartlist_free(invalid);
@@ -773,7 +783,8 @@ test_md_reject_cache(void *arg)
mc = get_microdesc_cache();
#define ADD(hex) \
do { \
- tt_int_op(0,OP_EQ,base16_decode(buf,sizeof(buf),hex,strlen(hex))); \
+ tt_int_op(sizeof(buf),OP_EQ,base16_decode(buf,sizeof(buf), \
+ hex,strlen(hex)));\
smartlist_add(wanted, tor_memdup(buf, DIGEST256_LEN)); \
} while (0)
diff --git a/src/test/test_nodelist.c b/src/test/test_nodelist.c
index a8693ec9b5..d58f8a7fca 100644
--- a/src/test/test_nodelist.c
+++ b/src/test/test_nodelist.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2015, The Tor Project, Inc. */
+/* Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
@@ -60,12 +60,53 @@ test_nodelist_node_get_verbose_nickname_not_named(void *arg)
return;
}
+/** A node should be considered a directory server if it has an open dirport
+ * of it accepts tunnelled directory requests.
+ */
+static void
+test_nodelist_node_is_dir(void *arg)
+{
+ (void)arg;
+
+ routerstatus_t rs;
+ routerinfo_t ri;
+ node_t node;
+ memset(&node, 0, sizeof(node_t));
+ memset(&rs, 0, sizeof(routerstatus_t));
+ memset(&ri, 0, sizeof(routerinfo_t));
+
+ tt_assert(!node_is_dir(&node));
+
+ node.rs = &rs;
+ tt_assert(!node_is_dir(&node));
+
+ rs.is_v2_dir = 1;
+ tt_assert(node_is_dir(&node));
+
+ rs.is_v2_dir = 0;
+ rs.dir_port = 1;
+ tt_assert(! node_is_dir(&node));
+
+ node.rs = NULL;
+ tt_assert(!node_is_dir(&node));
+ node.ri = &ri;
+ ri.supports_tunnelled_dir_requests = 1;
+ tt_assert(node_is_dir(&node));
+ ri.supports_tunnelled_dir_requests = 0;
+ ri.dir_port = 1;
+ tt_assert(! node_is_dir(&node));
+
+ done:
+ return;
+}
+
#define NODE(name, flags) \
{ #name, test_nodelist_##name, (flags), NULL, NULL }
struct testcase_t nodelist_tests[] = {
NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
NODE(node_get_verbose_nickname_not_named, TT_FORK),
+ NODE(node_is_dir, TT_FORK),
END_OF_TESTCASES
};
diff --git a/src/test/test_ntor_cl.c b/src/test/test_ntor_cl.c
index bfbf13a476..a560e5fc5e 100644
--- a/src/test/test_ntor_cl.c
+++ b/src/test/test_ntor_cl.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015, The Tor Project, Inc. */
+/* Copyright (c) 2012-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -21,7 +21,7 @@
} STMT_END
#define BASE16(idx, var, n) STMT_BEGIN { \
const char *s = argv[(idx)]; \
- if (base16_decode((char*)var, n, s, strlen(s)) < 0 ) { \
+ if (base16_decode((char*)var, n, s, strlen(s)) < (int)n ) { \
fprintf(stderr, "couldn't decode argument %d (%s)\n",idx,s); \
return 1; \
} \
@@ -106,6 +106,7 @@ server1(int argc, char **argv)
done:
tor_free(keys);
tor_free(hexkeys);
+ dimap_free(keymap, NULL);
return result;
}
@@ -152,7 +153,10 @@ main(int argc, char **argv)
if (argc < 2) {
fprintf(stderr, "I need arguments. Read source for more info.\n");
return 1;
- } else if (!strcmp(argv[1], "client1")) {
+ }
+
+ curve25519_init();
+ if (!strcmp(argv[1], "client1")) {
return client1(argc, argv);
} else if (!strcmp(argv[1], "server1")) {
return server1(argc, argv);
diff --git a/src/test/test_oom.c b/src/test/test_oom.c
index 41cfcdbd81..6102af01f5 100644
--- a/src/test/test_oom.c
+++ b/src/test/test_oom.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/* Unit tests for OOM handling logic */
@@ -77,14 +77,14 @@ dummy_origin_circuit_new(int n_cells)
}
static void
-add_bytes_to_buf(generic_buffer_t *buf, size_t n_bytes)
+add_bytes_to_buf(buf_t *buf, size_t n_bytes)
{
char b[3000];
while (n_bytes) {
size_t this_add = n_bytes > sizeof(b) ? sizeof(b) : n_bytes;
crypto_rand(b, this_add);
- generic_buffer_add(buf, b, this_add);
+ write_to_buf(b, this_add, buf);
n_bytes -= this_add;
}
}
@@ -94,20 +94,15 @@ dummy_edge_conn_new(circuit_t *circ,
int type, size_t in_bytes, size_t out_bytes)
{
edge_connection_t *conn;
- generic_buffer_t *inbuf, *outbuf;
+ buf_t *inbuf, *outbuf;
if (type == CONN_TYPE_EXIT)
conn = edge_connection_new(type, AF_INET);
else
conn = ENTRY_TO_EDGE_CONN(entry_connection_new(type, AF_INET));
-#ifdef USE_BUFFEREVENTS
- inbuf = bufferevent_get_input(TO_CONN(conn)->bufev);
- outbuf = bufferevent_get_output(TO_CONN(conn)->bufev);
-#else
inbuf = TO_CONN(conn)->inbuf;
outbuf = TO_CONN(conn)->outbuf;
-#endif
/* We add these bytes directly to the buffers, to avoid all the
* edge connection read/write machinery. */
@@ -134,10 +129,12 @@ test_oom_circbuf(void *arg)
{
or_options_t *options = get_options_mutable();
circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL;
- struct timeval tv = { 1389631048, 0 };
+ uint64_t now_ns = 1389631048 * (uint64_t)1000000000;
+ const uint64_t start_ns = now_ns;
(void) arg;
+ monotime_enable_test_mocking();
MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
/* Far too low for real life. */
@@ -150,11 +147,11 @@ test_oom_circbuf(void *arg)
/* Now we're going to fake up some circuits and get them added to the global
circuit list. */
- tv.tv_usec = 0;
- tor_gettimeofday_cache_set(&tv);
+ monotime_coarse_set_mock_time_nsec(now_ns);
c1 = dummy_origin_circuit_new(30);
- tv.tv_usec = 10*1000;
- tor_gettimeofday_cache_set(&tv);
+
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
c2 = dummy_or_circuit_new(20, 20);
tt_int_op(packed_cell_mem_cost(), OP_EQ,
@@ -163,15 +160,15 @@ test_oom_circbuf(void *arg)
packed_cell_mem_cost() * 70);
tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */
- tv.tv_usec = 20*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
c3 = dummy_or_circuit_new(100, 85);
tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */
tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
packed_cell_mem_cost() * 255);
- tv.tv_usec = 30*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
/* Adding this cell will trigger our OOM handler. */
c4 = dummy_or_circuit_new(2, 0);
@@ -189,12 +186,12 @@ test_oom_circbuf(void *arg)
packed_cell_mem_cost() * (257 - 30));
circuit_free(c1);
- tv.tv_usec = 0;
- tor_gettimeofday_cache_set(&tv); /* go back in time */
+
+ monotime_coarse_set_mock_time_nsec(start_ns); /* go back in time */
c1 = dummy_or_circuit_new(90, 0);
- tv.tv_usec = 40*1000; /* go back to the future */
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */
@@ -213,6 +210,7 @@ test_oom_circbuf(void *arg)
circuit_free(c4);
UNMOCK(circuit_mark_for_close_);
+ monotime_disable_test_mocking();
}
/** Run unit tests for buffers.c */
@@ -221,12 +219,14 @@ test_oom_streambuf(void *arg)
{
or_options_t *options = get_options_mutable();
circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL;
- struct timeval tv = { 1389641159, 0 };
uint32_t tvms;
int i;
smartlist_t *edgeconns = smartlist_new();
+ const uint64_t start_ns = 1389641159 * (uint64_t)1000000000;
+ uint64_t now_ns = start_ns;
(void) arg;
+ monotime_enable_test_mocking();
MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
@@ -238,54 +238,56 @@ test_oom_streambuf(void *arg)
tt_int_op(cell_queues_get_total_allocation(), OP_EQ, 0);
tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
+ monotime_coarse_set_mock_time_nsec(start_ns);
+
/* Start all circuits with a bit of data queued in cells */
- tv.tv_usec = 500*1000; /* go halfway into the second. */
- tor_gettimeofday_cache_set(&tv);
+
+ /* go halfway into the second. */
+ monotime_coarse_set_mock_time_nsec(start_ns + 500 * 1000000);
c1 = dummy_or_circuit_new(10,10);
- tv.tv_usec = 510*1000;
- tor_gettimeofday_cache_set(&tv);
+
+ monotime_coarse_set_mock_time_nsec(start_ns + 510 * 1000000);
c2 = dummy_origin_circuit_new(20);
- tv.tv_usec = 520*1000;
- tor_gettimeofday_cache_set(&tv);
+ monotime_coarse_set_mock_time_nsec(start_ns + 520 * 1000000);
c3 = dummy_or_circuit_new(20,20);
- tv.tv_usec = 530*1000;
- tor_gettimeofday_cache_set(&tv);
+ monotime_coarse_set_mock_time_nsec(start_ns + 530 * 1000000);
c4 = dummy_or_circuit_new(0,0);
tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
packed_cell_mem_cost() * 80);
- tv.tv_usec = 600*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns = start_ns + 600 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
/* Add some connections to c1...c4. */
for (i = 0; i < 4; ++i) {
edge_connection_t *ec;
/* link it to a circuit */
- tv.tv_usec += 10*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000);
tt_assert(ec);
smartlist_add(edgeconns, ec);
- tv.tv_usec += 10*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000);
tt_assert(ec);
smartlist_add(edgeconns, ec);
- tv.tv_usec += 10*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/
tt_assert(ec);
smartlist_add(edgeconns, ec);
- tv.tv_usec += 10*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 10 * 1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
smartlist_add(edgeconns, ec);
tt_assert(ec);
}
- tv.tv_sec += 1;
- tv.tv_usec = 0;
- tvms = (uint32_t) tv_to_msec(&tv);
+ now_ns -= now_ns % 1000000000;
+ now_ns += 1000000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
+ tvms = (uint32_t) monotime_coarse_absolute_msec();
tt_int_op(circuit_max_queued_cell_age(c1, tvms), OP_EQ, 500);
tt_int_op(circuit_max_queued_cell_age(c2, tvms), OP_EQ, 490);
@@ -309,9 +311,8 @@ test_oom_streambuf(void *arg)
/* Now give c4 a very old buffer of modest size */
{
edge_connection_t *ec;
- tv.tv_sec -= 1;
- tv.tv_usec = 0;
- tor_gettimeofday_cache_set(&tv);
+ now_ns -= 1000000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
tt_assert(ec);
smartlist_add(edgeconns, ec);
@@ -322,8 +323,8 @@ test_oom_streambuf(void *arg)
tt_int_op(cell_queues_check_size(), OP_EQ, 0);
/* And run over the limit. */
- tv.tv_usec = 800*1000;
- tor_gettimeofday_cache_set(&tv);
+ now_ns += 800*1000000;
+ monotime_coarse_set_mock_time_nsec(now_ns);
c5 = dummy_or_circuit_new(0,5);
tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
@@ -355,6 +356,7 @@ test_oom_streambuf(void *arg)
smartlist_free(edgeconns);
UNMOCK(circuit_mark_for_close_);
+ monotime_disable_test_mocking();
}
struct testcase_t oom_tests[] = {
diff --git a/src/test/test_oos.c b/src/test/test_oos.c
new file mode 100644
index 0000000000..db06625116
--- /dev/null
+++ b/src/test/test_oos.c
@@ -0,0 +1,456 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/* Unit tests for OOS handler */
+
+#define CONNECTION_PRIVATE
+
+#include "or.h"
+#include "config.h"
+#include "connection.h"
+#include "connection_or.h"
+#include "main.h"
+#include "test.h"
+
+static or_options_t mock_options;
+
+static void
+reset_options_mock(void)
+{
+ memset(&mock_options, 0, sizeof(or_options_t));
+}
+
+static const or_options_t *
+mock_get_options(void)
+{
+ return &mock_options;
+}
+
+static int moribund_calls = 0;
+static int moribund_conns = 0;
+
+static int
+mock_connection_count_moribund(void)
+{
+ ++moribund_calls;
+
+ return moribund_conns;
+}
+
+/*
+ * For unit test purposes it's sufficient to tell that
+ * kill_conn_list_for_oos() was called with an approximately
+ * sane argument; it's just the thing we returned from the
+ * mock for pick_oos_victims().
+ */
+
+static int kill_conn_list_calls = 0;
+static int kill_conn_list_killed = 0;
+
+static void
+kill_conn_list_mock(smartlist_t *conns)
+{
+ ++kill_conn_list_calls;
+
+ tt_assert(conns != NULL);
+
+ kill_conn_list_killed += smartlist_len(conns);
+
+ done:
+ return;
+}
+
+static int pick_oos_mock_calls = 0;
+static int pick_oos_mock_fail = 0;
+static int pick_oos_mock_last_n = 0;
+
+static smartlist_t *
+pick_oos_victims_mock(int n)
+{
+ smartlist_t *l = NULL;
+ int i;
+
+ ++pick_oos_mock_calls;
+
+ tt_int_op(n, OP_GT, 0);
+
+ if (!pick_oos_mock_fail) {
+ /*
+ * connection_check_oos() just passes the list onto
+ * kill_conn_list_for_oos(); we don't need to simulate
+ * its content for this mock, just its existence, but
+ * we do need to check the parameter.
+ */
+ l = smartlist_new();
+ for (i = 0; i < n; ++i) smartlist_add(l, NULL);
+ } else {
+ l = NULL;
+ }
+
+ pick_oos_mock_last_n = n;
+
+ done:
+ return l;
+}
+
+/** Unit test for the logic in connection_check_oos(), which is concerned
+ * with comparing thresholds and connection counts to decide if an OOS has
+ * occurred and if so, how many connections to try to kill, and then using
+ * pick_oos_victims() and kill_conn_list_for_oos() to carry out its grim
+ * duty.
+ */
+static void
+test_oos_connection_check_oos(void *arg)
+{
+ (void)arg;
+
+ /* Set up mocks */
+ reset_options_mock();
+ /* OOS handling is only sensitive to these fields */
+ mock_options.ConnLimit = 32;
+ mock_options.ConnLimit_ = 64;
+ mock_options.ConnLimit_high_thresh = 60;
+ mock_options.ConnLimit_low_thresh = 50;
+ MOCK(get_options, mock_get_options);
+ moribund_calls = 0;
+ moribund_conns = 0;
+ MOCK(connection_count_moribund, mock_connection_count_moribund);
+ kill_conn_list_calls = 0;
+ kill_conn_list_killed = 0;
+ MOCK(kill_conn_list_for_oos, kill_conn_list_mock);
+ pick_oos_mock_calls = 0;
+ pick_oos_mock_fail = 0;
+ MOCK(pick_oos_victims, pick_oos_victims_mock);
+
+ /* No OOS case */
+ connection_check_oos(50, 0);
+ tt_int_op(moribund_calls, OP_EQ, 0);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 0);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 0);
+
+ /* OOS from socket count, nothing moribund */
+ connection_check_oos(62, 0);
+ tt_int_op(moribund_calls, OP_EQ, 1);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 1);
+ /* 12 == 62 - ConnLimit_low_thresh */
+ tt_int_op(pick_oos_mock_last_n, OP_EQ, 12);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 1);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 12);
+
+ /* OOS from socket count, some are moribund */
+ kill_conn_list_killed = 0;
+ moribund_conns = 5;
+ connection_check_oos(62, 0);
+ tt_int_op(moribund_calls, OP_EQ, 2);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 2);
+ /* 7 == 62 - ConnLimit_low_thresh - moribund_conns */
+ tt_int_op(pick_oos_mock_last_n, OP_EQ, 7);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 2);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 7);
+
+ /* OOS from socket count, but pick fails */
+ kill_conn_list_killed = 0;
+ moribund_conns = 0;
+ pick_oos_mock_fail = 1;
+ connection_check_oos(62, 0);
+ tt_int_op(moribund_calls, OP_EQ, 3);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 3);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 2);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 0);
+ pick_oos_mock_fail = 0;
+
+ /*
+ * OOS from socket count with so many moribund conns
+ * we have none to kill.
+ */
+ kill_conn_list_killed = 0;
+ moribund_conns = 15;
+ connection_check_oos(62, 0);
+ tt_int_op(moribund_calls, OP_EQ, 4);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 3);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 2);
+
+ /*
+ * OOS from socket exhaustion; OOS handler will try to
+ * kill 1/10 (5) of the connections.
+ */
+ kill_conn_list_killed = 0;
+ moribund_conns = 0;
+ connection_check_oos(50, 1);
+ tt_int_op(moribund_calls, OP_EQ, 5);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 4);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 3);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 5);
+
+ /* OOS from socket exhaustion with moribund conns */
+ kill_conn_list_killed = 0;
+ moribund_conns = 2;
+ connection_check_oos(50, 1);
+ tt_int_op(moribund_calls, OP_EQ, 6);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 5);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 4);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 3);
+
+ /* OOS from socket exhaustion with many moribund conns */
+ kill_conn_list_killed = 0;
+ moribund_conns = 7;
+ connection_check_oos(50, 1);
+ tt_int_op(moribund_calls, OP_EQ, 7);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 5);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 4);
+
+ /* OOS with both socket exhaustion and above-threshold */
+ kill_conn_list_killed = 0;
+ moribund_conns = 0;
+ connection_check_oos(62, 1);
+ tt_int_op(moribund_calls, OP_EQ, 8);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 6);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 5);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 12);
+
+ /*
+ * OOS with both socket exhaustion and above-threshold with some
+ * moribund conns
+ */
+ kill_conn_list_killed = 0;
+ moribund_conns = 5;
+ connection_check_oos(62, 1);
+ tt_int_op(moribund_calls, OP_EQ, 9);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 7);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 6);
+ tt_int_op(kill_conn_list_killed, OP_EQ, 7);
+
+ /*
+ * OOS with both socket exhaustion and above-threshold with many
+ * moribund conns
+ */
+ kill_conn_list_killed = 0;
+ moribund_conns = 15;
+ connection_check_oos(62, 1);
+ tt_int_op(moribund_calls, OP_EQ, 10);
+ tt_int_op(pick_oos_mock_calls, OP_EQ, 7);
+ tt_int_op(kill_conn_list_calls, OP_EQ, 6);
+
+ done:
+
+ UNMOCK(pick_oos_victims);
+ UNMOCK(kill_conn_list_for_oos);
+ UNMOCK(connection_count_moribund);
+ UNMOCK(get_options);
+
+ return;
+}
+
+static int cfe_calls = 0;
+
+static void
+close_for_error_mock(or_connection_t *orconn, int flush)
+{
+ (void)flush;
+
+ tt_assert(orconn != NULL);
+ ++cfe_calls;
+
+ done:
+ return;
+}
+
+static int mark_calls = 0;
+
+static void
+mark_for_close_oos_mock(connection_t *conn,
+ int line, const char *file)
+{
+ (void)line;
+ (void)file;
+
+ tt_assert(conn != NULL);
+ ++mark_calls;
+
+ done:
+ return;
+}
+
+static void
+test_oos_kill_conn_list(void *arg)
+{
+ connection_t *c1, *c2;
+ or_connection_t *or_c1 = NULL;
+ dir_connection_t *dir_c2 = NULL;
+ smartlist_t *l = NULL;
+ (void)arg;
+
+ /* Set up mocks */
+ mark_calls = 0;
+ MOCK(connection_mark_for_close_internal_, mark_for_close_oos_mock);
+ cfe_calls = 0;
+ MOCK(connection_or_close_for_error, close_for_error_mock);
+
+ /* Make fake conns */
+ or_c1 = tor_malloc_zero(sizeof(*or_c1));
+ or_c1->base_.magic = OR_CONNECTION_MAGIC;
+ or_c1->base_.type = CONN_TYPE_OR;
+ c1 = TO_CONN(or_c1);
+ dir_c2 = tor_malloc_zero(sizeof(*dir_c2));
+ dir_c2->base_.magic = DIR_CONNECTION_MAGIC;
+ dir_c2->base_.type = CONN_TYPE_DIR;
+ dir_c2->base_.state = DIR_CONN_STATE_MIN_;
+ dir_c2->base_.purpose = DIR_PURPOSE_MIN_;
+ c2 = TO_CONN(dir_c2);
+
+ tt_assert(c1 != NULL);
+ tt_assert(c2 != NULL);
+
+ /* Make list */
+ l = smartlist_new();
+ smartlist_add(l, c1);
+ smartlist_add(l, c2);
+
+ /* Run kill_conn_list_for_oos() */
+ kill_conn_list_for_oos(l);
+
+ /* Check call counters */
+ tt_int_op(mark_calls, OP_EQ, 1);
+ tt_int_op(cfe_calls, OP_EQ, 1);
+
+ done:
+
+ UNMOCK(connection_or_close_for_error);
+ UNMOCK(connection_mark_for_close_internal_);
+
+ if (l) smartlist_free(l);
+ tor_free(or_c1);
+ tor_free(dir_c2);
+
+ return;
+}
+
+static smartlist_t *conns_for_mock = NULL;
+
+static smartlist_t *
+get_conns_mock(void)
+{
+ return conns_for_mock;
+}
+
+/*
+ * For this mock, we pretend all conns have either zero or one circuits,
+ * depending on if this appears on the list of things to say have a circuit.
+ */
+
+static smartlist_t *conns_with_circs = NULL;
+
+static int
+get_num_circuits_mock(or_connection_t *conn)
+{
+ int circs = 0;
+
+ tt_assert(conn != NULL);
+
+ if (conns_with_circs &&
+ smartlist_contains(conns_with_circs, TO_CONN(conn))) {
+ circs = 1;
+ }
+
+ done:
+ return circs;
+}
+
+static void
+test_oos_pick_oos_victims(void *arg)
+{
+ (void)arg;
+ or_connection_t *ortmp;
+ dir_connection_t *dirtmp;
+ smartlist_t *picked;
+
+ /* Set up mocks */
+ conns_for_mock = smartlist_new();
+ MOCK(get_connection_array, get_conns_mock);
+ conns_with_circs = smartlist_new();
+ MOCK(connection_or_get_num_circuits, get_num_circuits_mock);
+
+ /* Make some fake connections */
+ ortmp = tor_malloc_zero(sizeof(*ortmp));
+ ortmp->base_.magic = OR_CONNECTION_MAGIC;
+ ortmp->base_.type = CONN_TYPE_OR;
+ smartlist_add(conns_for_mock, TO_CONN(ortmp));
+ /* We'll pretend this one has a circuit too */
+ smartlist_add(conns_with_circs, TO_CONN(ortmp));
+ /* Next one */
+ ortmp = tor_malloc_zero(sizeof(*ortmp));
+ ortmp->base_.magic = OR_CONNECTION_MAGIC;
+ ortmp->base_.type = CONN_TYPE_OR;
+ smartlist_add(conns_for_mock, TO_CONN(ortmp));
+ /* Next one is moribund */
+ ortmp = tor_malloc_zero(sizeof(*ortmp));
+ ortmp->base_.magic = OR_CONNECTION_MAGIC;
+ ortmp->base_.type = CONN_TYPE_OR;
+ ortmp->base_.marked_for_close = 1;
+ smartlist_add(conns_for_mock, TO_CONN(ortmp));
+ /* Last one isn't an orconn */
+ dirtmp = tor_malloc_zero(sizeof(*dirtmp));
+ dirtmp->base_.magic = DIR_CONNECTION_MAGIC;
+ dirtmp->base_.type = CONN_TYPE_DIR;
+ smartlist_add(conns_for_mock, TO_CONN(dirtmp));
+
+ /* Try picking one */
+ picked = pick_oos_victims(1);
+ /* It should be the one with circuits */
+ tt_assert(picked != NULL);
+ tt_int_op(smartlist_len(picked), OP_EQ, 1);
+ tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
+ smartlist_free(picked);
+
+ /* Try picking none */
+ picked = pick_oos_victims(0);
+ /* We should get an empty list */
+ tt_assert(picked != NULL);
+ tt_int_op(smartlist_len(picked), OP_EQ, 0);
+ smartlist_free(picked);
+
+ /* Try picking two */
+ picked = pick_oos_victims(2);
+ /* We should get both active orconns */
+ tt_assert(picked != NULL);
+ tt_int_op(smartlist_len(picked), OP_EQ, 2);
+ tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
+ tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 1)));
+ smartlist_free(picked);
+
+ /* Try picking three - only two are eligible */
+ picked = pick_oos_victims(3);
+ tt_int_op(smartlist_len(picked), OP_EQ, 2);
+ tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
+ tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 1)));
+ smartlist_free(picked);
+
+ done:
+
+ /* Free leftover stuff */
+ if (conns_with_circs) {
+ smartlist_free(conns_with_circs);
+ conns_with_circs = NULL;
+ }
+
+ UNMOCK(connection_or_get_num_circuits);
+
+ if (conns_for_mock) {
+ SMARTLIST_FOREACH(conns_for_mock, connection_t *, c, tor_free(c));
+ smartlist_free(conns_for_mock);
+ conns_for_mock = NULL;
+ }
+
+ UNMOCK(get_connection_array);
+
+ return;
+}
+
+struct testcase_t oos_tests[] = {
+ { "connection_check_oos", test_oos_connection_check_oos,
+ TT_FORK, NULL, NULL },
+ { "kill_conn_list", test_oos_kill_conn_list, TT_FORK, NULL, NULL },
+ { "pick_oos_victims", test_oos_pick_oos_victims, TT_FORK, NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_options.c b/src/test/test_options.c
index a8ebadb14b..e85e11805b 100644
--- a/src/test/test_options.c
+++ b/src/test/test_options.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CONFIG_PRIVATE
@@ -8,6 +8,18 @@
#include "confparse.h"
#include "config.h"
#include "test.h"
+#include "geoip.h"
+
+#define ROUTERSET_PRIVATE
+#include "routerset.h"
+#include "main.h"
+#include "log_test_helpers.h"
+
+#include "sandbox.h"
+#include "memarea.h"
+#include "policies.h"
+
+#define NS_MODULE test_options
typedef struct {
int severity;
@@ -38,6 +50,7 @@ setup_log_callback(void)
lst.masks[LOG_WARN - LOG_ERR] = ~0;
lst.masks[LOG_NOTICE - LOG_ERR] = ~0;
add_callback_log(&lst, log_cback);
+ mark_logs_temp();
}
static char *
@@ -69,27 +82,46 @@ clear_log_messages(void)
messages = NULL;
}
+#define setup_options(opt,dflt) \
+ do { \
+ opt = options_new(); \
+ opt->command = CMD_RUN_TOR; \
+ options_init(opt); \
+ \
+ dflt = config_dup(&options_format, opt); \
+ clear_log_messages(); \
+ } while (0)
+
+#define VALID_DIR_AUTH "DirAuthority dizum orport=443 v3ident=E8A9C45" \
+ "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
+ " 083C 538F 4403 8BBF A077 587D D755\n"
+#define VALID_ALT_BRIDGE_AUTH \
+ "AlternateBridgeAuthority dizum orport=443 v3ident=E8A9C45" \
+ "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
+ " 083C 538F 4403 8BBF A077 587D D755\n"
+#define VALID_ALT_DIR_AUTH \
+ "AlternateDirAuthority dizum orport=443 v3ident=E8A9C45" \
+ "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
+ " 083C 538F 4403 8BBF A077 587D D755\n"
+
static void
test_options_validate_impl(const char *configuration,
const char *expect_errmsg,
int expect_log_severity,
const char *expect_log)
{
- or_options_t *opt = options_new();
+ or_options_t *opt=NULL;
or_options_t *dflt;
config_line_t *cl=NULL;
char *msg=NULL;
int r;
- opt->command = CMD_RUN_TOR;
- options_init(opt);
- dflt = config_dup(&options_format, opt);
- clear_log_messages();
+ setup_options(opt, dflt);
r = config_get_lines(configuration, &cl, 1);
tt_int_op(r, OP_EQ, 0);
- r = config_assign(&options_format, opt, cl, 0, 0, &msg);
+ r = config_assign(&options_format, opt, cl, 0, &msg);
tt_int_op(r, OP_EQ, 0);
r = options_validate(NULL, opt, dflt, 0, &msg);
@@ -126,6 +158,8 @@ test_options_validate_impl(const char *configuration,
}
done:
+ escaped(NULL);
+ policies_free_all();
config_free_lines(cl);
or_options_free(opt);
or_options_free(dflt);
@@ -147,6 +181,7 @@ test_options_validate(void *arg)
{
(void)arg;
setup_log_callback();
+ sandbox_disable_getaddrinfo_cache();
WANT_ERR("ExtORPort 500000", "Invalid ExtORPort");
@@ -159,12 +194,4358 @@ test_options_validate(void *arg)
"ServerTransportOptions did not parse",
LOG_WARN, "\"slingsnappy\" is not a k=v");
+ WANT_ERR("DirPort 8080\nDirCache 0",
+ "DirPort configured but DirCache disabled.");
+ WANT_ERR("BridgeRelay 1\nDirCache 0",
+ "We're a bridge but DirCache is disabled.");
+
+ close_temp_logs();
+ clear_log_messages();
+ return;
+}
+
+#define MEGABYTEIFY(mb) (U64_LITERAL(mb) << 20)
+static void
+test_have_enough_mem_for_dircache(void *arg)
+{
+ (void)arg;
+ or_options_t *opt=NULL;
+ or_options_t *dflt=NULL;
+ config_line_t *cl=NULL;
+ char *msg=NULL;;
+ int r;
+ const char *configuration = "ORPort 8080\nDirCache 1", *expect_errmsg;
+
+ setup_options(opt, dflt);
+ setup_log_callback();
+ (void)dflt;
+
+ r = config_get_lines(configuration, &cl, 1);
+ tt_int_op(r, OP_EQ, 0);
+
+ r = config_assign(&options_format, opt, cl, 0, &msg);
+ tt_int_op(r, OP_EQ, 0);
+
+ /* 300 MB RAM available, DirCache enabled */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
+ tt_int_op(r, OP_EQ, 0);
+ tt_assert(!msg);
+
+ /* 200 MB RAM available, DirCache enabled */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
+ tt_int_op(r, OP_EQ, -1);
+ expect_errmsg = "Being a directory cache (default) with less than ";
+ if (!strstr(msg, expect_errmsg)) {
+ TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
+ expect_errmsg, configuration, msg));
+ }
+ tor_free(msg);
+
+ config_free_lines(cl); cl = NULL;
+ configuration = "ORPort 8080\nDirCache 1\nBridgeRelay 1";
+ r = config_get_lines(configuration, &cl, 1);
+ tt_int_op(r, OP_EQ, 0);
+
+ r = config_assign(&options_format, opt, cl, 0, &msg);
+ tt_int_op(r, OP_EQ, 0);
+
+ /* 300 MB RAM available, DirCache enabled, Bridge */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
+ tt_int_op(r, OP_EQ, 0);
+ tt_assert(!msg);
+
+ /* 200 MB RAM available, DirCache enabled, Bridge */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
+ tt_int_op(r, OP_EQ, -1);
+ expect_errmsg = "Running a Bridge with less than ";
+ if (!strstr(msg, expect_errmsg)) {
+ TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
+ expect_errmsg, configuration, msg));
+ }
+ tor_free(msg);
+
+ config_free_lines(cl); cl = NULL;
+ configuration = "ORPort 8080\nDirCache 0";
+ r = config_get_lines(configuration, &cl, 1);
+ tt_int_op(r, OP_EQ, 0);
+
+ r = config_assign(&options_format, opt, cl, 0, &msg);
+ tt_int_op(r, OP_EQ, 0);
+
+ /* 200 MB RAM available, DirCache disabled */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
+ tt_int_op(r, OP_EQ, 0);
+ tt_assert(!msg);
+
+ /* 300 MB RAM available, DirCache disabled */
+ r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
+ tt_int_op(r, OP_EQ, -1);
+ expect_errmsg = "DirCache is disabled and we are configured as a ";
+ if (!strstr(msg, expect_errmsg)) {
+ TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
+ expect_errmsg, configuration, msg));
+ }
+ tor_free(msg);
+
clear_log_messages();
+
+ done:
+ if (msg)
+ tor_free(msg);
+ or_options_free(dflt);
+ or_options_free(opt);
+ config_free_lines(cl);
return;
}
+static const char *fixed_get_uname_result = NULL;
+
+static const char *
+fixed_get_uname(void)
+{
+ return fixed_get_uname_result;
+}
+
+#define TEST_OPTIONS_OLD_VALUES "TestingV3AuthInitialVotingInterval 1800\n" \
+ "ClientBootstrapConsensusMaxDownloadTries 7\n" \
+ "ClientBootstrapConsensusAuthorityOnlyMaxDownloadTries 4\n" \
+ "ClientBootstrapConsensusMaxInProgressTries 3\n" \
+ "TestingV3AuthInitialVoteDelay 300\n" \
+ "TestingV3AuthInitialDistDelay 300\n" \
+ "TestingClientMaxIntervalWithoutRequest 600\n" \
+ "TestingDirConnectionMaxStall 600\n" \
+ "TestingConsensusMaxDownloadTries 8\n" \
+ "TestingDescriptorMaxDownloadTries 8\n" \
+ "TestingMicrodescMaxDownloadTries 8\n" \
+ "TestingCertMaxDownloadTries 8\n"
+
+#define TEST_OPTIONS_DEFAULT_VALUES TEST_OPTIONS_OLD_VALUES \
+ "MaxClientCircuitsPending 1\n" \
+ "RendPostPeriod 1000\n" \
+ "KeepAlivePeriod 1\n" \
+ "ConnLimit 1\n" \
+ "V3AuthVotingInterval 300\n" \
+ "V3AuthVoteDelay 20\n" \
+ "V3AuthDistDelay 20\n" \
+ "V3AuthNIntervalsValid 3\n" \
+ "ClientUseIPv4 1\n" \
+ "VirtualAddrNetworkIPv4 127.192.0.0/10\n" \
+ "VirtualAddrNetworkIPv6 [FE80::]/10\n" \
+ "SchedulerHighWaterMark__ 42\n" \
+ "SchedulerLowWaterMark__ 10\n"
+
+typedef struct {
+ or_options_t *old_opt;
+ or_options_t *opt;
+ or_options_t *def_opt;
+} options_test_data_t;
+
+static void free_options_test_data(options_test_data_t *td);
+
+static options_test_data_t *
+get_options_test_data(const char *conf)
+{
+ int rv = -1;
+ char *msg = NULL;
+ config_line_t *cl=NULL;
+ options_test_data_t *result = tor_malloc(sizeof(options_test_data_t));
+ result->opt = options_new();
+ result->old_opt = options_new();
+ result->def_opt = options_new();
+ rv = config_get_lines(conf, &cl, 1);
+ tt_assert(rv == 0);
+ rv = config_assign(&options_format, result->opt, cl, 0, &msg);
+ if (msg) {
+ /* Display the parse error message by comparing it with an empty string */
+ tt_str_op(msg, OP_EQ, "");
+ }
+ tt_assert(rv == 0);
+ config_free_lines(cl);
+ result->opt->LogTimeGranularity = 1;
+ result->opt->TokenBucketRefillInterval = 1;
+ rv = config_get_lines(TEST_OPTIONS_OLD_VALUES, &cl, 1);
+ tt_assert(rv == 0);
+ rv = config_assign(&options_format, result->def_opt, cl, 0, &msg);
+ if (msg) {
+ /* Display the parse error message by comparing it with an empty string */
+ tt_str_op(msg, OP_EQ, "");
+ }
+ tt_assert(rv == 0);
+
+ done:
+ config_free_lines(cl);
+ if (rv != 0) {
+ free_options_test_data(result);
+ result = NULL;
+ /* Callers expect a non-NULL result, so just die if we can't provide one.
+ */
+ tor_assert(0);
+ }
+ return result;
+}
+
+static void
+free_options_test_data(options_test_data_t *td)
+{
+ if (!td) return;
+ or_options_free(td->old_opt);
+ or_options_free(td->opt);
+ or_options_free(td->def_opt);
+ tor_free(td);
+}
+
+static void
+test_options_validate__uname_for_server(void *ignored)
+{
+ (void)ignored;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "ORListenAddress 127.0.0.1:5555");
+ setup_capture_of_logs(LOG_WARN);
+
+ MOCK(get_uname, fixed_get_uname);
+ fixed_get_uname_result = "Windows 95";
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("Tor is running as a server, but you"
+ " are running Windows 95; this probably won't work. See https://www"
+ ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
+ tor_free(msg);
+
+ fixed_get_uname_result = "Windows 98";
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("Tor is running as a server, but you"
+ " are running Windows 98; this probably won't work. See https://www"
+ ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
+ tor_free(msg);
+
+ fixed_get_uname_result = "Windows Me";
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("Tor is running as a server, but you"
+ " are running Windows Me; this probably won't work. See https://www"
+ ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
+ tor_free(msg);
+
+ fixed_get_uname_result = "Windows 2000";
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_entry();
+ tor_free(msg);
+
+ done:
+ UNMOCK(get_uname);
+ free_options_test_data(tdata);
+ tor_free(msg);
+ teardown_capture_of_logs();
+}
+
+static void
+test_options_validate__outbound_addresses(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "OutboundBindAddress xxyy!!!sdfaf");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__data_directory(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "DataDirectory longreallyl"
+ "ongLONGLONGlongreallylong"
+ "LONGLONGlongreallylongLON"
+ "GLONGlongreallylongLONGLO"
+ "NGlongreallylongLONGLONGl"
+ "ongreallylongLONGLONGlong"
+ "reallylongLONGLONGlongrea"
+ "llylongLONGLONGlongreally"
+ "longLONGLONGlongreallylon"
+ "gLONGLONGlongreallylongLO"
+ "NGLONGlongreallylongLONGL"
+ "ONGlongreallylongLONGLONG"
+ "longreallylongLONGLONGlon"
+ "greallylongLONGLONGlongre"
+ "allylongLONGLONGlongreall"
+ "ylongLONGLONGlongreallylo"
+ "ngLONGLONGlongreallylongL"
+ "ONGLONGlongreallylongLONG"
+ "LONG"); // 440 characters
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Invalid DataDirectory");
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__nickname(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "Nickname ThisNickNameIsABitTooLong");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Nickname 'ThisNickNameIsABitTooLong', nicknames must be between "
+ "1 and 19 characters inclusive, and must contain only the "
+ "characters [a-zA-Z0-9].");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("Nickname AMoreValidNick");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(!msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("DataDirectory /tmp/somewhere");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(!msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__contactinfo(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "ORListenAddress 127.0.0.1:5555\nORPort 955");
+ setup_capture_of_logs(LOG_DEBUG);
+ tdata->opt->ContactInfo = NULL;
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg(
+ "Your ContactInfo config option is not"
+ " set. Please consider setting it, so we can contact you if your"
+ " server is misconfigured or something else goes wrong.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ORListenAddress 127.0.0.1:5555\nORPort 955\n"
+ "ContactInfo hella@example.org");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_no_log_msg(
+ "Your ContactInfo config option is not"
+ " set. Please consider setting it, so we can contact you if your"
+ " server is misconfigured or something else goes wrong.\n");
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__logs(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ (void)ret;
+ char *msg;
+ int orig_quiet_level = quiet_level;
+ options_test_data_t *tdata = get_options_test_data("");
+ tdata->opt->Logs = NULL;
+ tdata->opt->RunAsDaemon = 0;
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(tdata->opt->Logs->key, OP_EQ, "Log");
+ tt_str_op(tdata->opt->Logs->value, OP_EQ, "notice stdout");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("");
+ tdata->opt->Logs = NULL;
+ tdata->opt->RunAsDaemon = 0;
+ quiet_level = 1;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(tdata->opt->Logs->key, OP_EQ, "Log");
+ tt_str_op(tdata->opt->Logs->value, OP_EQ, "warn stdout");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("");
+ tdata->opt->Logs = NULL;
+ tdata->opt->RunAsDaemon = 0;
+ quiet_level = 2;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_assert(!tdata->opt->Logs);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("");
+ tdata->opt->Logs = NULL;
+ tdata->opt->RunAsDaemon = 0;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 1, &msg);
+ tt_assert(!tdata->opt->Logs);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("");
+ tdata->opt->Logs = NULL;
+ tdata->opt->RunAsDaemon = 1;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_assert(!tdata->opt->Logs);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("");
+ tdata->opt->RunAsDaemon = 0;
+ config_line_t *cl=NULL;
+ config_get_lines("Log foo", &cl, 1);
+ tdata->opt->Logs = cl;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op((intptr_t)tdata->opt->Logs, OP_EQ, (intptr_t)cl);
+
+ done:
+ quiet_level = orig_quiet_level;
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+/* static config_line_t * */
+/* mock_config_line(const char *key, const char *val) */
+/* { */
+/* config_line_t *config_line = tor_malloc(sizeof(config_line_t)); */
+/* memset(config_line, 0, sizeof(config_line_t)); */
+/* config_line->key = tor_strdup(key); */
+/* config_line->value = tor_strdup(val); */
+/* return config_line; */
+/* } */
+
+static void
+test_options_validate__authdir(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_INFO);
+ options_test_data_t *tdata = get_options_test_data(
+ "AuthoritativeDirectory 1\n"
+ "Address this.should.not_exist.example.org");
+
+ sandbox_disable_getaddrinfo_cache();
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Failed to resolve/guess local address. See logs for"
+ " details.");
+ expect_log_msg("Could not resolve local Address "
+ "'this.should.not_exist.example.org'. Failing.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(!msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Authoritative directory servers must set ContactInfo");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "TestingTorNetwork 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
+ "AuthoritativeDir is set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
+ "AuthoritativeDir is set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "RecommendedVersions 1.2, 3.14\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(tdata->opt->RecommendedClientVersions->value, OP_EQ, "1.2, 3.14");
+ tt_str_op(tdata->opt->RecommendedServerVersions->value, OP_EQ, "1.2, 3.14");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "RecommendedVersions 1.2, 3.14\n"
+ "RecommendedClientVersions 25\n"
+ "RecommendedServerVersions 4.18\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(tdata->opt->RecommendedClientVersions->value, OP_EQ, "25");
+ tt_str_op(tdata->opt->RecommendedServerVersions->value, OP_EQ, "4.18");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "VersioningAuthoritativeDirectory 1\n"
+ "RecommendedVersions 1.2, 3.14\n"
+ "RecommendedClientVersions 25\n"
+ "RecommendedServerVersions 4.18\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
+ "AuthoritativeDir is set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "VersioningAuthoritativeDirectory 1\n"
+ "RecommendedServerVersions 4.18\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ, "Versioning authoritative dir servers must set "
+ "Recommended*Versions.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "VersioningAuthoritativeDirectory 1\n"
+ "RecommendedClientVersions 4.18\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ, "Versioning authoritative dir servers must set "
+ "Recommended*Versions.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "UseEntryGuards 1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("Authoritative directory servers "
+ "can't set UseEntryGuards. Disabling.\n");
+ tt_int_op(tdata->opt->UseEntryGuards, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "V3AuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("Authoritative directories always try"
+ " to download extra-info documents. Setting DownloadExtraInfo.\n");
+ tt_int_op(tdata->opt->DownloadExtraInfo, OP_EQ, 1);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "DownloadExtraInfo 1\n"
+ "V3AuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_no_log_msg("Authoritative directories always try"
+ " to download extra-info documents. Setting DownloadExtraInfo.\n");
+ tt_int_op(tdata->opt->DownloadExtraInfo, OP_EQ, 1);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
+ "AuthoritativeDir is set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "V3BandwidthsFile non-existant-file\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no DirPort set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "V3BandwidthsFile non-existant-file\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(NULL, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no DirPort set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "GuardfractionFile non-existant-file\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no DirPort set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "GuardfractionFile non-existant-file\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ options_validate(NULL, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no DirPort set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no DirPort set.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AuthoritativeDirectory 1\n"
+ "Address 100.200.10.1\n"
+ "DirPort 999\n"
+ "BridgeAuthoritativeDir 1\n"
+ "ContactInfo hello@hello.com\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Running as authoritative directory, but no ORPort set.");
+ tor_free(msg);
+
+ // TODO: This case can't be reached, since clientonly is used to
+ // check when parsing port lines as well.
+ /* free_options_test_data(tdata); */
+ /* tdata = get_options_test_data("AuthoritativeDirectory 1\n" */
+ /* "Address 100.200.10.1\n" */
+ /* "DirPort 999\n" */
+ /* "ORPort 888\n" */
+ /* "ClientOnly 1\n" */
+ /* "BridgeAuthoritativeDir 1\n" */
+ /* "ContactInfo hello@hello.com\n" */
+ /* "SchedulerHighWaterMark__ 42\n" */
+ /* "SchedulerLowWaterMark__ 10\n"); */
+ /* mock_clean_saved_logs(); */
+ /* ret = options_validate(tdata->old_opt, tdata->opt, */
+ /* tdata->def_opt, 0, &msg); */
+ /* tt_int_op(ret, OP_EQ, -1); */
+ /* tt_str_op(msg, OP_EQ, "Running as authoritative directory, " */
+ /* "but ClientOnly also set."); */
+
+ done:
+ teardown_capture_of_logs();
+ // sandbox_free_getaddrinfo_cache();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__relay_with_hidden_services(void *ignored)
+{
+ (void)ignored;
+ char *msg;
+ setup_capture_of_logs(LOG_DEBUG);
+ options_test_data_t *tdata = get_options_test_data(
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "HiddenServiceDir "
+ "/Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg(
+ "Tor is currently configured as a relay and a hidden service. "
+ "That's not very secure: you should probably run your hidden servi"
+ "ce in a separate Tor process, at least -- see "
+ "https://trac.torproject.org/8742\n");
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+// TODO: it doesn't seem possible to hit the case of having no port lines at
+// all, since there will be a default created for SocksPort
+/* static void */
+/* test_options_validate__ports(void *ignored) */
+/* { */
+/* (void)ignored; */
+/* int ret; */
+/* char *msg; */
+/* setup_capture_of_logs(LOG_WARN); */
+/* options_test_data_t *tdata = get_options_test_data(""); */
+/* ret = options_validate(tdata->old_opt, tdata->opt, */
+/* tdata->def_opt, 0, &msg); */
+/* expect_log_msg("SocksPort, TransPort, NATDPort, DNSPort, and ORPort " */
+/* "are all undefined, and there aren't any hidden services " */
+/* "configured. " */
+/* " Tor will still run, but probably won't do anything.\n"); */
+/* done: */
+/* teardown_capture_of_logs(); */
+/* free_options_test_data(tdata); */
+/* tor_free(msg); */
+/* } */
+
+static void
+test_options_validate__transproxy(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata;
+
+#ifdef USE_TRANSPARENT
+ // Test default trans proxy
+ tdata = get_options_test_data("TransProxyType default\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_DEFAULT);
+ tor_free(msg);
+
+ // Test pf-divert trans proxy
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TransProxyType pf-divert\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+
+#if !defined(__OpenBSD__) && !defined( DARWIN )
+ tt_str_op(msg, OP_EQ,
+ "pf-divert is a OpenBSD-specific and OS X/Darwin-specific feature.");
+#else
+ tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_PF_DIVERT);
+ tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without "
+ "any valid TransPort or TransListenAddress.");
+#endif
+ tor_free(msg);
+
+ // Test tproxy trans proxy
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TransProxyType tproxy\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+
+#if !defined(__linux__)
+ tt_str_op(msg, OP_EQ, "TPROXY is a Linux-specific feature.");
+#else
+ tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_TPROXY);
+ tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without any valid "
+ "TransPort or TransListenAddress.");
+#endif
+ tor_free(msg);
+
+ // Test ipfw trans proxy
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TransProxyType ipfw\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+
+#ifndef KERNEL_MAY_SUPPORT_IPFW
+ tt_str_op(msg, OP_EQ, "ipfw is a FreeBSD-specific and OS X/Darwin-specific "
+ "feature.");
+#else
+ tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_IPFW);
+ tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without any valid "
+ "TransPort or TransListenAddress.");
+#endif
+ tor_free(msg);
+
+ // Test unknown trans proxy
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TransProxyType non-existant\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Unrecognized value for TransProxyType");
+ tor_free(msg);
+
+ // Test trans proxy success
+ free_options_test_data(tdata);
+ tdata = NULL;
+
+#if defined(__linux__)
+ tdata = get_options_test_data("TransProxyType tproxy\n"
+ "TransPort 127.0.0.1:123\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ if (msg) {
+ TT_DIE(("Expected NULL but got '%s'", msg));
+ }
+#elif defined(KERNEL_MAY_SUPPORT_IPFW)
+ tdata = get_options_test_data("TransProxyType ipfw\n"
+ "TransPort 127.0.0.1:123\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ if (msg) {
+ TT_DIE(("Expected NULL but got '%s'", msg));
+ }
+#elif defined(__OpenBSD__)
+ tdata = get_options_test_data("TransProxyType pf-divert\n"
+ "TransPort 127.0.0.1:123\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ if (msg) {
+ TT_DIE(("Expected NULL but got '%s'", msg));
+ }
+#elif defined(__NetBSD__)
+ tdata = get_options_test_data("TransProxyType default\n"
+ "TransPort 127.0.0.1:123\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ if (msg) {
+ TT_DIE(("Expected NULL but got '%s'", msg));
+ }
+#endif
+
+ // Assert that a test has run for some TransProxyType
+ tt_assert(tdata);
+
+#else
+ tdata = get_options_test_data("TransPort 127.0.0.1:555\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TransPort and TransListenAddress are disabled in "
+ "this build.");
+ tor_free(msg);
+#endif
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+NS_DECL(country_t, geoip_get_country, (const char *country));
+
+static country_t
+NS(geoip_get_country)(const char *countrycode)
+{
+ (void)countrycode;
+ CALLED(geoip_get_country)++;
+
+ return 1;
+}
+
+static void
+test_options_validate__exclude_nodes(void *ignored)
+{
+ (void)ignored;
+
+ NS_MOCK(geoip_get_country);
+
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+ options_test_data_t *tdata = get_options_test_data(
+ "ExcludeExitNodes {us}\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 1);
+ tt_str_op((char *)
+ (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
+ OP_EQ, "{us}");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ExcludeNodes {cn}\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 1);
+ tt_str_op((char *)
+ (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
+ OP_EQ, "{cn}");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ExcludeNodes {cn}\n"
+ "ExcludeExitNodes {us} {cn}\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 2);
+ tt_str_op((char *)
+ (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
+ OP_EQ, "{us} {cn}");
+ tt_str_op((char *)
+ (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 1)),
+ OP_EQ, "{cn}");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ExcludeNodes {cn}\n"
+ "StrictNodes 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg(
+ "You have asked to exclude certain relays from all positions "
+ "in your circuits. Expect hidden services and other Tor "
+ "features to be broken in unpredictable ways.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ExcludeNodes {cn}\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_no_log_msg(
+ "You have asked to exclude certain relays from all positions "
+ "in your circuits. Expect hidden services and other Tor "
+ "features to be broken in unpredictable ways.\n");
+ tor_free(msg);
+
+ done:
+ NS_UNMOCK(geoip_get_country);
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__scheduler(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_DEBUG);
+ options_test_data_t *tdata = get_options_test_data(
+ "SchedulerLowWaterMark__ 0\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Bad SchedulerLowWaterMark__ option\n");
+ tor_free(msg);
+
+ // TODO: this test cannot run on platforms where UINT32_MAX == UINT64_MAX.
+ // I suspect it's unlikely this branch can actually happen
+ /* free_options_test_data(tdata); */
+ /* tdata = get_options_test_data( */
+ /* "SchedulerLowWaterMark 10000000000000000000\n"); */
+ /* tdata->opt->SchedulerLowWaterMark__ = (uint64_t)UINT32_MAX; */
+ /* tdata->opt->SchedulerLowWaterMark__++; */
+ /* mock_clean_saved_logs(); */
+ /* ret = options_validate(tdata->old_opt, tdata->opt, */
+ /* tdata->def_opt, 0, &msg); */
+ /* tt_int_op(ret, OP_EQ, -1); */
+ /* expect_log_msg("Bad SchedulerLowWaterMark__ option\n"); */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SchedulerLowWaterMark__ 42\n"
+ "SchedulerHighWaterMark__ 42\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Bad SchedulerHighWaterMark option\n");
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__node_families(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "NodeFamily flux, flax\n"
+ "NodeFamily somewhere\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(tdata->opt->NodeFamilySets);
+ tt_int_op(smartlist_len(tdata->opt->NodeFamilySets), OP_EQ, 2);
+ tt_str_op((char *)(smartlist_get(
+ ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 0))->list, 0)),
+ OP_EQ, "flux");
+ tt_str_op((char *)(smartlist_get(
+ ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 0))->list, 1)),
+ OP_EQ, "flax");
+ tt_str_op((char *)(smartlist_get(
+ ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 1))->list, 0)),
+ OP_EQ, "somewhere");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(!tdata->opt->NodeFamilySets);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("NodeFamily !flux\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(tdata->opt->NodeFamilySets);
+ tt_int_op(smartlist_len(tdata->opt->NodeFamilySets), OP_EQ, 0);
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__tlsec(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_DEBUG);
+ options_test_data_t *tdata = get_options_test_data(
+ "TLSECGroup ed25519\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Unrecognized TLSECGroup: Falling back to the default.\n");
+ tt_assert(!tdata->opt->TLSECGroup);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TLSECGroup P224\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_no_log_msg(
+ "Unrecognized TLSECGroup: Falling back to the default.\n");
+ tt_assert(tdata->opt->TLSECGroup);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("TLSECGroup P256\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_no_log_msg(
+ "Unrecognized TLSECGroup: Falling back to the default.\n");
+ tt_assert(tdata->opt->TLSECGroup);
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__token_bucket(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data("");
+
+ tdata->opt->TokenBucketRefillInterval = 0;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "TokenBucketRefillInterval must be between 1 and 1000 inclusive.");
+ tor_free(msg);
+
+ tdata->opt->TokenBucketRefillInterval = 1001;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "TokenBucketRefillInterval must be between 1 and 1000 inclusive.");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__recommended_packages(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+ options_test_data_t *tdata = get_options_test_data(
+ "RecommendedPackages foo 1.2 http://foo.com sha1=123123123123\n"
+ "RecommendedPackages invalid-package-line\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_no_log_msg("Invalid RecommendedPackage line "
+ "invalid-package-line will be ignored\n");
+
+ done:
+ escaped(NULL); // This will free the leaking memory from the previous escaped
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__fetch_dir(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "FetchDirInfoExtraEarly 1\n"
+ "FetchDirInfoEarly 0\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "FetchDirInfoExtraEarly requires that you"
+ " also set FetchDirInfoEarly");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("FetchDirInfoExtraEarly 1\n"
+ "FetchDirInfoEarly 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_NE, "FetchDirInfoExtraEarly requires that you"
+ " also set FetchDirInfoEarly");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__conn_limit(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "ConnLimit 0\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "ConnLimit must be greater than 0, but was set to 0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024, "
+ "but was set to 0");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__paths_needed(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+ options_test_data_t *tdata = get_options_test_data(
+ "PathsNeededToBuildCircuits 0.1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.24 &&
+ tdata->opt->PathsNeededToBuildCircuits < 0.26);
+ expect_log_msg("PathsNeededToBuildCircuits is too low. "
+ "Increasing to 0.25\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data("PathsNeededToBuildCircuits 0.99\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.94 &&
+ tdata->opt->PathsNeededToBuildCircuits < 0.96);
+ expect_log_msg("PathsNeededToBuildCircuits is "
+ "too high. Decreasing to 0.95\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data("PathsNeededToBuildCircuits 0.91\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.90 &&
+ tdata->opt->PathsNeededToBuildCircuits < 0.92);
+ expect_no_log_entry();
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__max_client_circuits(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "MaxClientCircuitsPending 0\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024,"
+ " but was set to 0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("MaxClientCircuitsPending 1025\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024,"
+ " but was set to 1025");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__ports(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "FirewallPorts 65537\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Port '65537' out of range in FirewallPorts");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("FirewallPorts 1\n"
+ "LongLivedPorts 124444\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Port '124444' out of range in LongLivedPorts");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("FirewallPorts 1\n"
+ "LongLivedPorts 2\n"
+ "RejectPlaintextPorts 112233\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Port '112233' out of range in RejectPlaintextPorts");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("FirewallPorts 1\n"
+ "LongLivedPorts 2\n"
+ "RejectPlaintextPorts 3\n"
+ "WarnPlaintextPorts 65536\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Port '65536' out of range in WarnPlaintextPorts");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("FirewallPorts 1\n"
+ "LongLivedPorts 2\n"
+ "RejectPlaintextPorts 3\n"
+ "WarnPlaintextPorts 4\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__reachable_addresses(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_NOTICE);
+ options_test_data_t *tdata = get_options_test_data(
+ "FascistFirewall 1\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Converting FascistFirewall config "
+ "option to new format: \"ReachableDirAddresses *:80\"\n");
+ tt_str_op(tdata->opt->ReachableDirAddresses->value, OP_EQ, "*:80");
+ expect_log_msg("Converting FascistFirewall config "
+ "option to new format: \"ReachableORAddresses *:443\"\n");
+ tt_str_op(tdata->opt->ReachableORAddresses->value, OP_EQ, "*:443");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data("FascistFirewall 1\n"
+ "ReachableDirAddresses *:81\n"
+ "ReachableORAddresses *:444\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+ tdata->opt->FirewallPorts = smartlist_new();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_entry();
+ tt_str_op(tdata->opt->ReachableDirAddresses->value, OP_EQ, "*:81");
+ tt_str_op(tdata->opt->ReachableORAddresses->value, OP_EQ, "*:444");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data("FascistFirewall 1\n"
+ "FirewallPort 123\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Converting FascistFirewall and "
+ "FirewallPorts config options to new format: "
+ "\"ReachableAddresses *:123\"\n");
+ tt_str_op(tdata->opt->ReachableAddresses->value, OP_EQ, "*:123");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data("FascistFirewall 1\n"
+ "ReachableAddresses *:82\n"
+ "ReachableAddresses *:83\n"
+ "ReachableAddresses reject *:*\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_entry();
+ tt_str_op(tdata->opt->ReachableAddresses->value, OP_EQ, "*:82");
+ tor_free(msg);
+
+#define SERVERS_REACHABLE_MSG "Servers must be able to freely connect to" \
+ " the rest of the Internet, so they must not set Reachable*Addresses or" \
+ " FascistFirewall or FirewallPorts or ClientUseIPv4 0."
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ReachableAddresses *:82\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ReachableORAddresses *:82\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ReachableDirAddresses *:82\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("ClientUseIPv4 0\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
+ tor_free(msg);
+
+ /* Test IPv4-only clients setting IPv6 preferences */
+
+#define WARN_PLEASE_USE_IPV6_OR_LOG_MSG \
+ "ClientPreferIPv6ORPort 1 is ignored unless tor is using IPv6. " \
+ "Please set ClientUseIPv6 1, ClientUseIPv4 0, or configure bridges.\n"
+
+#define WARN_PLEASE_USE_IPV6_DIR_LOG_MSG \
+ "ClientPreferIPv6DirPort 1 is ignored unless tor is using IPv6. " \
+ "Please set ClientUseIPv6 1, ClientUseIPv4 0, or configure bridges.\n"
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientUseIPv4 1\n"
+ "ClientUseIPv6 0\n"
+ "UseBridges 0\n"
+ "ClientPreferIPv6ORPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(WARN_PLEASE_USE_IPV6_OR_LOG_MSG);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientUseIPv4 1\n"
+ "ClientUseIPv6 0\n"
+ "UseBridges 0\n"
+ "ClientPreferIPv6DirPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(WARN_PLEASE_USE_IPV6_DIR_LOG_MSG);
+ tor_free(msg);
+
+ /* Now test an IPv4/IPv6 client setting IPv6 preferences */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientUseIPv4 1\n"
+ "ClientUseIPv6 1\n"
+ "ClientPreferIPv6ORPort 1\n"
+ "ClientPreferIPv6DirPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+
+ /* Now test an IPv6 client setting IPv6 preferences */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientUseIPv6 1\n"
+ "ClientPreferIPv6ORPort 1\n"
+ "ClientPreferIPv6DirPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+
+ /* And an implicit (IPv4 disabled) IPv6 client setting IPv6 preferences */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientUseIPv4 0\n"
+ "ClientPreferIPv6ORPort 1\n"
+ "ClientPreferIPv6DirPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+
+ /* And an implicit (bridge) client setting IPv6 preferences */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "UseBridges 1\n"
+ "Bridge 127.0.0.1:12345\n"
+ "ClientPreferIPv6ORPort 1\n"
+ "ClientPreferIPv6DirPort 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__use_bridges(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "UseBridges 1\n"
+ "ClientUseIPv4 1\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Servers must be able to freely connect to the rest of"
+ " the Internet, so they must not set UseBridges.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("UseBridges 1\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_NE, "Servers must be able to freely connect to the rest of"
+ " the Internet, so they must not set UseBridges.");
+ tor_free(msg);
+
+ NS_MOCK(geoip_get_country);
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("UseBridges 1\n"
+ "EntryNodes {cn}\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "You cannot set both UseBridges and EntryNodes.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "UseBridges 1\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "If you set UseBridges, you must specify at least one bridge.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "UseBridges 1\n"
+ "Bridge 10.0.0.1\n"
+ "Bridge !!!\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Bridge line did not parse. See logs for details.");
+ tor_free(msg);
+
+ done:
+ NS_UNMOCK(geoip_get_country);
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__entry_nodes(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ NS_MOCK(geoip_get_country);
+ options_test_data_t *tdata = get_options_test_data(
+ "EntryNodes {cn}\n"
+ "UseEntryGuards 0\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "If EntryNodes is set, UseEntryGuards must be enabled.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("EntryNodes {cn}\n"
+ "UseEntryGuards 1\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
+ tor_free(msg);
+
+ done:
+ NS_UNMOCK(geoip_get_country);
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__invalid_nodes(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "AllowInvalidNodes something_stupid\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Unrecognized value 'something_stupid' in AllowInvalidNodes");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AllowInvalidNodes entry, middle, exit\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->AllowInvalid_, OP_EQ, ALLOW_INVALID_ENTRY |
+ ALLOW_INVALID_EXIT | ALLOW_INVALID_MIDDLE);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("AllowInvalidNodes introduction, rendezvous\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->AllowInvalid_, OP_EQ, ALLOW_INVALID_INTRODUCTION |
+ ALLOW_INVALID_RENDEZVOUS);
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__safe_logging(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = get_options_test_data(
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_NONE);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SafeLogging 0\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_NONE);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SafeLogging Relay\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_RELAY);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SafeLogging 1\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_ALL);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("SafeLogging stuffy\n"
+ "MaxClientCircuitsPending 1\n"
+ "ConnLimit 1\n"
+ "SchedulerHighWaterMark__ 42\n"
+ "SchedulerLowWaterMark__ 10\n");
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Unrecognized value '\"stuffy\"' in SafeLogging");
+ tor_free(msg);
+
+ done:
+ escaped(NULL); // This will free the leaking memory from the previous escaped
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__publish_server_descriptor(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+ options_test_data_t *tdata = get_options_test_data(
+ "PublishServerDescriptor bridge\n" TEST_OPTIONS_DEFAULT_VALUES
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("PublishServerDescriptor humma\n"
+ TEST_OPTIONS_DEFAULT_VALUES);
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Unrecognized value in PublishServerDescriptor");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("PublishServerDescriptor bridge, v3\n"
+ TEST_OPTIONS_DEFAULT_VALUES);
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Bridges are not supposed to publish router "
+ "descriptors to the directory authorities. Please correct your "
+ "PublishServerDescriptor line.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("BridgeRelay 1\n"
+ "PublishServerDescriptor v3\n"
+ TEST_OPTIONS_DEFAULT_VALUES);
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Bridges are not supposed to publish router "
+ "descriptors to the directory authorities. Please correct your "
+ "PublishServerDescriptor line.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("BridgeRelay 1\n" TEST_OPTIONS_DEFAULT_VALUES);
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_NE, "Bridges are not supposed to publish router "
+ "descriptors to the directory authorities. Please correct your "
+ "PublishServerDescriptor line.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data("BridgeRelay 1\n"
+ "DirPort 999\n" TEST_OPTIONS_DEFAULT_VALUES);
+
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ expect_log_msg("Can't set a DirPort on a bridge "
+ "relay; disabling DirPort\n");
+ tt_assert(!tdata->opt->DirPort_lines);
+ tt_assert(!tdata->opt->DirPort_set);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__testing(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+#define ENSURE_DEFAULT(varname, varval) \
+ STMT_BEGIN \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
+ #varname " " #varval "\n"); \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ tt_str_op(msg, OP_EQ, \
+ #varname " may only be changed in testing Tor networks!"); \
+ tt_int_op(ret, OP_EQ, -1); \
+ tor_free(msg); \
+ \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
+ #varname " " #varval "\n" \
+ VALID_DIR_AUTH \
+ "TestingTorNetwork 1\n"); \
+ \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ if (msg) { \
+ tt_str_op(msg, OP_NE, \
+ #varname " may only be changed in testing Tor networks!"); \
+ tor_free(msg); \
+ } \
+ \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
+ #varname " " #varval "\n" \
+ "___UsingTestNetworkDefaults 1\n"); \
+ \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ if (msg) { \
+ tt_str_op(msg, OP_NE, \
+ #varname " may only be changed in testing Tor networks!"); \
+ tor_free(msg); \
+ } \
+ STMT_END
+
+ ENSURE_DEFAULT(TestingV3AuthInitialVotingInterval, 3600);
+ ENSURE_DEFAULT(TestingV3AuthInitialVoteDelay, 3000);
+ ENSURE_DEFAULT(TestingV3AuthInitialDistDelay, 3000);
+ ENSURE_DEFAULT(TestingV3AuthVotingStartOffset, 3000);
+ ENSURE_DEFAULT(TestingAuthDirTimeToLearnReachability, 3000);
+ ENSURE_DEFAULT(TestingEstimatedDescriptorPropagationTime, 3000);
+ ENSURE_DEFAULT(TestingServerDownloadSchedule, 3000);
+ ENSURE_DEFAULT(TestingClientDownloadSchedule, 3000);
+ ENSURE_DEFAULT(TestingServerConsensusDownloadSchedule, 3000);
+ ENSURE_DEFAULT(TestingClientConsensusDownloadSchedule, 3000);
+ ENSURE_DEFAULT(TestingBridgeDownloadSchedule, 3000);
+ ENSURE_DEFAULT(TestingClientMaxIntervalWithoutRequest, 3000);
+ ENSURE_DEFAULT(TestingDirConnectionMaxStall, 3000);
+ ENSURE_DEFAULT(TestingConsensusMaxDownloadTries, 3000);
+ ENSURE_DEFAULT(TestingDescriptorMaxDownloadTries, 3000);
+ ENSURE_DEFAULT(TestingMicrodescMaxDownloadTries, 3000);
+ ENSURE_DEFAULT(TestingCertMaxDownloadTries, 3000);
+ ENSURE_DEFAULT(TestingAuthKeyLifetime, 3000);
+ ENSURE_DEFAULT(TestingLinkCertLifetime, 3000);
+ ENSURE_DEFAULT(TestingSigningKeySlop, 3000);
+ ENSURE_DEFAULT(TestingAuthKeySlop, 3000);
+ ENSURE_DEFAULT(TestingLinkKeySlop, 3000);
+
+ done:
+ escaped(NULL); // This will free the leaking memory from the previous escaped
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__hidserv(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+
+ options_test_data_t *tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES);
+ tdata->opt->MinUptimeHidServDirectoryV2 = -1;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("MinUptimeHidServDirectoryV2 "
+ "option must be at least 0 seconds. Changing to 0.\n");
+ tt_int_op(tdata->opt->MinUptimeHidServDirectoryV2, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RendPostPeriod 1\n" );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("RendPostPeriod option is too short;"
+ " raising to 600 seconds.\n");
+ tt_int_op(tdata->opt->RendPostPeriod, OP_EQ, 600);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RendPostPeriod 302401\n" );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("RendPostPeriod is too large; "
+ "clipping to 302400s.\n");
+ tt_int_op(tdata->opt->RendPostPeriod, OP_EQ, 302400);
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__predicted_ports(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ setup_capture_of_logs(LOG_WARN);
+
+ options_test_data_t *tdata = get_options_test_data(
+ "PredictedPortsRelevanceTime 100000000\n"
+ TEST_OPTIONS_DEFAULT_VALUES);
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("PredictedPortsRelevanceTime is too "
+ "large; clipping to 3600s.\n");
+ tt_int_op(tdata->opt->PredictedPortsRelevanceTime, OP_EQ, 3600);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__path_bias(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+
+ options_test_data_t *tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "PathBiasNoticeRate 1.1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "PathBiasNoticeRate is too high. It must be between 0 and 1.0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PathBiasWarnRate 1.1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "PathBiasWarnRate is too high. It must be between 0 and 1.0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PathBiasExtremeRate 1.1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "PathBiasExtremeRate is too high. It must be between 0 and 1.0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PathBiasNoticeUseRate 1.1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "PathBiasNoticeUseRate is too high. It must be between 0 and 1.0");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PathBiasExtremeUseRate 1.1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "PathBiasExtremeUseRate is too high. It must be between 0 and 1.0");
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__bandwidth(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+#define ENSURE_BANDWIDTH_PARAM(p) \
+ STMT_BEGIN \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES #p " 3Gb\n"); \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ tt_int_op(ret, OP_EQ, -1); \
+ tt_mem_op(msg, OP_EQ, #p " (3221225471) must be at most 2147483647", 40); \
+ tor_free(msg); \
+ STMT_END
+
+ ENSURE_BANDWIDTH_PARAM(BandwidthRate);
+ ENSURE_BANDWIDTH_PARAM(BandwidthBurst);
+ ENSURE_BANDWIDTH_PARAM(MaxAdvertisedBandwidth);
+ ENSURE_BANDWIDTH_PARAM(RelayBandwidthRate);
+ ENSURE_BANDWIDTH_PARAM(RelayBandwidthBurst);
+ ENSURE_BANDWIDTH_PARAM(PerConnBWRate);
+ ENSURE_BANDWIDTH_PARAM(PerConnBWBurst);
+ ENSURE_BANDWIDTH_PARAM(AuthDirFastGuarantee);
+ ENSURE_BANDWIDTH_PARAM(AuthDirGuardBWGuarantee);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RelayBandwidthRate 1000\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_u64_op(tdata->opt->RelayBandwidthBurst, OP_EQ, 1000);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RelayBandwidthBurst 1001\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_u64_op(tdata->opt->RelayBandwidthRate, OP_EQ, 1001);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RelayBandwidthRate 1001\n"
+ "RelayBandwidthBurst 1000\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "RelayBandwidthBurst must be at least equal to "
+ "RelayBandwidthRate.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "BandwidthRate 1001\n"
+ "BandwidthBurst 1000\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "BandwidthBurst must be at least equal to BandwidthRate.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RelayBandwidthRate 1001\n"
+ "BandwidthRate 1000\n"
+ "BandwidthBurst 1000\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_u64_op(tdata->opt->BandwidthRate, OP_EQ, 1001);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "RelayBandwidthRate 1001\n"
+ "BandwidthRate 1000\n"
+ "RelayBandwidthBurst 1001\n"
+ "BandwidthBurst 1000\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_u64_op(tdata->opt->BandwidthBurst, OP_EQ, 1001);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "BandwidthRate is set to 1 bytes/second. For servers,"
+ " it must be at least 76800.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76800\n"
+ "MaxAdvertisedBandwidth 30000\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "MaxAdvertisedBandwidth is set to 30000 bytes/second."
+ " For servers, it must be at least 38400.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76800\n"
+ "RelayBandwidthRate 1\n"
+ "MaxAdvertisedBandwidth 38400\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "RelayBandwidthRate is set to 1 bytes/second. For "
+ "servers, it must be at least 76800.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76800\n"
+ "BandwidthBurst 76800\n"
+ "RelayBandwidthRate 76800\n"
+ "MaxAdvertisedBandwidth 38400\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__circuits(void *ignored)
+{
+ (void)ignored;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "MaxCircuitDirtiness 2592001\n");
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("MaxCircuitDirtiness option is too "
+ "high; setting to 30 days.\n");
+ tt_int_op(tdata->opt->MaxCircuitDirtiness, OP_EQ, 2592000);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CircuitStreamTimeout 1\n");
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("CircuitStreamTimeout option is too"
+ " short; raising to 10 seconds.\n");
+ tt_int_op(tdata->opt->CircuitStreamTimeout, OP_EQ, 10);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CircuitStreamTimeout 111\n");
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_no_log_msg("CircuitStreamTimeout option is too"
+ " short; raising to 10 seconds.\n");
+ tt_int_op(tdata->opt->CircuitStreamTimeout, OP_EQ, 111);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HeartbeatPeriod 1\n");
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("HeartbeatPeriod option is too short;"
+ " raising to 1800 seconds.\n");
+ tt_int_op(tdata->opt->HeartbeatPeriod, OP_EQ, 1800);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HeartbeatPeriod 1982\n");
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_no_log_msg("HeartbeatPeriod option is too short;"
+ " raising to 1800 seconds.\n");
+ tt_int_op(tdata->opt->HeartbeatPeriod, OP_EQ, 1982);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CircuitBuildTimeout 1\n"
+ );
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_log_msg("CircuitBuildTimeout is shorter (1"
+ " seconds) than the recommended minimum (10 seconds), and "
+ "LearnCircuitBuildTimeout is disabled. If tor isn't working, "
+ "raise this value or enable LearnCircuitBuildTimeout.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ mock_clean_saved_logs();
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CircuitBuildTimeout 11\n"
+ );
+ options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ expect_no_log_msg("CircuitBuildTimeout is shorter (1 "
+ "seconds) than the recommended minimum (10 seconds), and "
+ "LearnCircuitBuildTimeout is disabled. If tor isn't working, "
+ "raise this value or enable LearnCircuitBuildTimeout.\n");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__port_forwarding(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PortForwarding 1\nSandbox 1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "PortForwarding is not compatible with Sandbox;"
+ " at most one can be set");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "PortForwarding 1\nSandbox 0\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ done:
+ free_options_test_data(tdata);
+ policies_free_all();
+ tor_free(msg);
+}
+
+static void
+test_options_validate__tor2web(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Tor2webRendezvousPoints 1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Tor2webRendezvousPoints cannot be set without Tor2webMode.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Tor2webRendezvousPoints 1\nTor2webMode 1\n");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__rend(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "UseEntryGuards 0\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("UseEntryGuards is disabled, but you"
+ " have configured one or more hidden services on this Tor "
+ "instance. Your hidden services will be very easy to locate using"
+ " a well-known attack -- see http://freehaven.net/anonbib/#hs-"
+ "attack06 for details.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "UseEntryGuards 1\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("UseEntryGuards is disabled, but you"
+ " have configured one or more hidden services on this Tor "
+ "instance. Your hidden services will be very easy to locate using"
+ " a well-known attack -- see http://freehaven.net/anonbib/#hs-"
+ "attack06 for details.\n");
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Failed to configure rendezvous options. See logs for details.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HidServAuth failed\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Failed to configure client authorization for hidden "
+ "services. See logs for details.");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__single_onion(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ /* Test that HiddenServiceSingleHopMode must come with
+ * HiddenServiceNonAnonymousMode */
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 0\n"
+ "HiddenServiceSingleHopMode 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HiddenServiceSingleHopMode does not provide any "
+ "server anonymity. It must be used with "
+ "HiddenServiceNonAnonymousMode set to 1.");
+ tor_free(msg);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 0\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "HiddenServiceNonAnonymousMode 0\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HiddenServiceSingleHopMode does not provide any "
+ "server anonymity. It must be used with "
+ "HiddenServiceNonAnonymousMode set to 1.");
+ tor_free(msg);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 0\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "HiddenServiceNonAnonymousMode 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+ free_options_test_data(tdata);
+
+ /* Test that SOCKSPort must come with Tor2webMode if
+ * HiddenServiceSingleHopMode is 1 */
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 5000\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "HiddenServiceNonAnonymousMode 1\n"
+ "Tor2webMode 0\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode is incompatible with "
+ "using Tor as an anonymous client. Please set "
+ "Socks/Trans/NATD/DNSPort to 0, or HiddenServiceNonAnonymousMode "
+ "to 0, or use the non-anonymous Tor2webMode.");
+ tor_free(msg);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 0\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "HiddenServiceNonAnonymousMode 1\n"
+ "Tor2webMode 0\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 5000\n"
+ "HiddenServiceSingleHopMode 0\n"
+ "Tor2webMode 0\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "SOCKSPort 5000\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "HiddenServiceNonAnonymousMode 1\n"
+ "Tor2webMode 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+ free_options_test_data(tdata);
+
+ /* Test that a hidden service can't be run with Tor2web
+ * Use HiddenServiceNonAnonymousMode instead of Tor2webMode, because
+ * Tor2webMode requires a compilation #define */
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceNonAnonymousMode 1\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode does not provide any "
+ "server anonymity. It must be used with "
+ "HiddenServiceSingleHopMode set to 1.");
+ tor_free(msg);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceNonAnonymousMode 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode does not provide any "
+ "server anonymity. It must be used with "
+ "HiddenServiceSingleHopMode set to 1.");
+ tor_free(msg);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+ free_options_test_data(tdata);
+
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceNonAnonymousMode 1\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ "HiddenServiceSingleHopMode 1\n"
+ "SOCKSPort 0\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_ptr_op(msg, OP_EQ, NULL);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__accounting(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccountingRule something_bad\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "AccountingRule must be 'sum', 'max', 'in', or 'out'");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccountingRule sum\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->AccountingRule, OP_EQ, ACCT_SUM);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccountingRule max\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->AccountingRule, OP_EQ, ACCT_MAX);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccountingStart fail\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Failed to parse accounting options. See logs for details.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccountingMax 10\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76800\n"
+ "BandwidthBurst 76800\n"
+ "MaxAdvertisedBandwidth 38400\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ "AccountingMax 10\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("Using accounting with a hidden "
+ "service and an ORPort is risky: your hidden service(s) and "
+ "your public address will all turn off at the same time, "
+ "which may alert observers that they are being run by the "
+ "same party.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ "AccountingMax 10\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("Using accounting with a hidden "
+ "service and an ORPort is risky: your hidden service(s) and "
+ "your public address will all turn off at the same time, "
+ "which may alert observers that they are being run by the "
+ "same party.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
+ "HiddenServicePort 80 127.0.0.1:8080\n"
+ "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service2/\n"
+ "HiddenServicePort 81 127.0.0.1:8081\n"
+ "AccountingMax 10\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("Using accounting with multiple "
+ "hidden services is risky: they will all turn off at the same"
+ " time, which may alert observers that they are being run by "
+ "the same party.\n");
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__proxy(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ sandbox_disable_getaddrinfo_cache();
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 127.0.42.1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HTTPProxyPort, OP_EQ, 80);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 127.0.42.1:444\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HTTPProxyPort, OP_EQ, 444);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy not_so_valid!\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HTTPProxy failed to parse or resolve. Please fix.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxyAuthenticator "
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreeonetwothreeonetwothree"
+
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HTTPProxyAuthenticator is too long (>= 512 chars).");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxyAuthenticator validauth\n"
+
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpsProxy 127.0.42.1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HTTPSProxyPort, OP_EQ, 443);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpsProxy 127.0.42.1:444\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HTTPSProxyPort, OP_EQ, 444);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpsProxy not_so_valid!\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HTTPSProxy failed to parse or resolve. Please fix.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpsProxyAuthenticator "
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreonetwothreonetwothreonetwothre"
+ "onetwothreonetwothreonetwothreonetwothreonetw"
+ "othreonetwothreeonetwothreeonetwothree"
+
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "HTTPSProxyAuthenticator is too long (>= 512 chars).");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpsProxyAuthenticator validauth\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks4Proxy 127.0.42.1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->Socks4ProxyPort, OP_EQ, 1080);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks4Proxy 127.0.42.1:444\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->Socks4ProxyPort, OP_EQ, 444);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks4Proxy not_so_valid!\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Socks4Proxy failed to parse or resolve. Please fix.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5Proxy 127.0.42.1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->Socks5ProxyPort, OP_EQ, 1080);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5Proxy 127.0.42.1:444\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->Socks5ProxyPort, OP_EQ, 444);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5Proxy not_so_valid!\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Socks5Proxy failed to parse or resolve. Please fix.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks4Proxy 215.1.1.1\n"
+ "Socks5Proxy 215.1.1.2\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "You have configured more than one proxy type. "
+ "(Socks4Proxy|Socks5Proxy|HTTPSProxy)");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 215.1.1.1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("HTTPProxy configured, but no SOCKS "
+ "proxy or HTTPS proxy configured. Watch out: this configuration "
+ "will proxy unencrypted directory connections only.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 215.1.1.1\n"
+ "Socks4Proxy 215.1.1.1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("HTTPProxy configured, but no SOCKS "
+ "proxy or HTTPS proxy configured. Watch out: this configuration "
+ "will proxy unencrypted directory connections only.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 215.1.1.1\n"
+ "Socks5Proxy 215.1.1.1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("HTTPProxy configured, but no SOCKS "
+ "proxy or HTTPS proxy configured. Watch out: this configuration "
+ "will proxy unencrypted directory connections only.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HttpProxy 215.1.1.1\n"
+ "HttpsProxy 215.1.1.1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "HTTPProxy configured, but no SOCKS proxy or HTTPS proxy "
+ "configured. Watch out: this configuration will proxy "
+ "unencrypted directory connections only.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ );
+ tdata->opt->Socks5ProxyUsername = tor_strdup("");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Socks5ProxyUsername must be between 1 and 255 characters.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ );
+ tdata->opt->Socks5ProxyUsername =
+ tor_strdup("ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789AB"
+ "CDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCD"
+ "EABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEA"
+ "BCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABC"
+ "DE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Socks5ProxyUsername must be between 1 and 255 characters.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5ProxyUsername hello_world\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Socks5ProxyPassword must be included with "
+ "Socks5ProxyUsername.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5ProxyUsername hello_world\n"
+ );
+ tdata->opt->Socks5ProxyPassword = tor_strdup("");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Socks5ProxyPassword must be between 1 and 255 characters.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5ProxyUsername hello_world\n"
+ );
+ tdata->opt->Socks5ProxyPassword =
+ tor_strdup("ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789AB"
+ "CDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCD"
+ "EABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEA"
+ "BCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABC"
+ "DE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789");
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Socks5ProxyPassword must be between 1 and 255 characters.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5ProxyUsername hello_world\n"
+ "Socks5ProxyPassword world_hello\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "Socks5ProxyPassword hello_world\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Socks5ProxyPassword must be included with "
+ "Socks5ProxyUsername.");
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ policies_free_all();
+ // sandbox_free_getaddrinfo_cache();
+ tor_free(msg);
+}
+
+static void
+test_options_validate__control(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HashedControlPassword something_incorrect\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Bad HashedControlPassword: wrong length or bad encoding");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "HashedControlPassword 16:872860B76453A77D60CA"
+ "2BB8C1A7042072093276A3D701AD684053EC4C\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "__HashedControlSessionPassword something_incorrect\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Bad HashedControlSessionPassword: wrong length or "
+ "bad encoding");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "__HashedControlSessionPassword 16:872860B7645"
+ "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
+ "4C\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(
+ TEST_OPTIONS_DEFAULT_VALUES
+ "__OwningControllerProcess something_incorrect\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Bad OwningControllerProcess: invalid PID");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "__OwningControllerProcess 123\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlPort 127.0.0.1:1234\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "ControlPort is open, but no authentication method has been "
+ "configured. This means that any program on your computer can "
+ "reconfigure your Tor. That's bad! You should upgrade your Tor"
+ " controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlPort 127.0.0.1:1234\n"
+ "HashedControlPassword 16:872860B76453A77D60CA"
+ "2BB8C1A7042072093276A3D701AD684053EC4C\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlPort is open, but no authentication method has been "
+ "configured. This means that any program on your computer can "
+ "reconfigure your Tor. That's bad! You should upgrade your Tor "
+ "controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlPort 127.0.0.1:1234\n"
+ "__HashedControlSessionPassword 16:872860B7645"
+ "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
+ "4C\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlPort is open, but no authentication method has been "
+ "configured. This means that any program on your computer can "
+ "reconfigure your Tor. That's bad! You should upgrade your Tor "
+ "controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlPort 127.0.0.1:1234\n"
+ "CookieAuthentication 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlPort is open, but no authentication method has been "
+ "configured. This means that any program on your computer can "
+ "reconfigure your Tor. That's bad! You should upgrade your Tor "
+ "controller as soon as possible.\n");
+ tor_free(msg);
+
+#ifdef HAVE_SYS_UN_H
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlSocket unix:/tmp WorldWritable\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "ControlSocket is world writable, but no authentication method has"
+ " been configured. This means that any program on your computer "
+ "can reconfigure your Tor. That's bad! You should upgrade your "
+ "Tor controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlSocket unix:/tmp WorldWritable\n"
+ "HashedControlPassword 16:872860B76453A77D60CA"
+ "2BB8C1A7042072093276A3D701AD684053EC4C\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlSocket is world writable, but no authentication method has"
+ " been configured. This means that any program on your computer "
+ "can reconfigure your Tor. That's bad! You should upgrade your "
+ "Tor controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlSocket unix:/tmp WorldWritable\n"
+ "__HashedControlSessionPassword 16:872860B7645"
+ "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
+ "4C\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlSocket is world writable, but no authentication method has"
+ " been configured. This means that any program on your computer "
+ "can reconfigure your Tor. That's bad! You should upgrade your "
+ "Tor controller as soon as possible.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ControlSocket unix:/tmp WorldWritable\n"
+ "CookieAuthentication 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "ControlSocket is world writable, but no authentication method has"
+ " been configured. This means that any program on your computer "
+ "can reconfigure your Tor. That's bad! You should upgrade your "
+ "Tor controller as soon as possible.\n");
+ tor_free(msg);
+#endif
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CookieAuthFileGroupReadable 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "CookieAuthFileGroupReadable is set, but will have no effect: you "
+ "must specify an explicit CookieAuthFile to have it "
+ "group-readable.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "CookieAuthFileGroupReadable 1\n"
+ "CookieAuthFile /tmp/somewhere\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "CookieAuthFileGroupReadable is set, but will have no effect: you "
+ "must specify an explicit CookieAuthFile to have it "
+ "group-readable.\n");
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__families(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "MyFamily home\n"
+ "BridgeRelay 1\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 51300\n"
+ "BandwidthBurst 51300\n"
+ "MaxAdvertisedBandwidth 25700\n"
+ "DirCache 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "Listing a family for a bridge relay is not supported: it can "
+ "reveal bridge fingerprints to censors. You should also make sure "
+ "you aren't listing this bridge's fingerprint in any other "
+ "MyFamily.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "MyFamily home\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "Listing a family for a bridge relay is not supported: it can "
+ "reveal bridge fingerprints to censors. You should also make sure "
+ "you aren't listing this bridge's fingerprint in any other "
+ "MyFamily.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "MyFamily !\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Invalid nickname '!' in MyFamily line");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "NodeFamily foo\n"
+ "NodeFamily !\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ done:
+ teardown_capture_of_logs();
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__addr_policies(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ExitPolicy !!!\n"
+ "ExitRelay 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Error in ExitPolicy entry.");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__dir_auth(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_DIR_AUTH
+ VALID_ALT_DIR_AUTH
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Directory authority/fallback line did not parse. See logs for "
+ "details.");
+ expect_log_msg(
+ "You cannot set both DirAuthority and Alternate*Authority.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "TestingTorNetwork may only be configured in combination with a "
+ "non-default set of DirAuthority or both of AlternateDirAuthority "
+ "and AlternateBridgeAuthority configured.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingTorNetwork 1\n"
+ VALID_ALT_DIR_AUTH
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "TestingTorNetwork may only be configured in combination with a "
+ "non-default set of DirAuthority or both of AlternateDirAuthority "
+ "and AlternateBridgeAuthority configured.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingTorNetwork 1\n"
+ VALID_ALT_BRIDGE_AUTH
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingTorNetwork may only be configured in "
+ "combination with a non-default set of DirAuthority or both of "
+ "AlternateDirAuthority and AlternateBridgeAuthority configured.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_ALT_DIR_AUTH
+ VALID_ALT_BRIDGE_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__transport(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_NOTICE);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientTransportPlugin !!\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Invalid client transport line. See logs for details.");
+ expect_log_msg(
+ "Too few arguments on ClientTransportPlugin line.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ClientTransportPlugin foo exec bar\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportPlugin !!\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Invalid server transport line. See logs for details.");
+ expect_log_msg(
+ "Too few arguments on ServerTransportPlugin line.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportPlugin foo exec bar\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "Tor is not configured as a relay but you specified a "
+ "ServerTransportPlugin line (\"foo exec bar\"). The "
+ "ServerTransportPlugin line will be ignored.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportPlugin foo exec bar\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76900\n"
+ "BandwidthBurst 76900\n"
+ "MaxAdvertisedBandwidth 38500\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "Tor is not configured as a relay but you specified a "
+ "ServerTransportPlugin line (\"foo exec bar\"). The "
+ "ServerTransportPlugin line will be ignored.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportListenAddr foo 127.0.0.42:55\n"
+ "ServerTransportListenAddr !\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "ServerTransportListenAddr did not parse. See logs for details.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportListenAddr foo 127.0.0.42:55\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg(
+ "You need at least a single managed-proxy to specify a transport "
+ "listen address. The ServerTransportListenAddr line will be "
+ "ignored.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ServerTransportListenAddr foo 127.0.0.42:55\n"
+ "ServerTransportPlugin foo exec bar\n"
+ "ORListenAddress 127.0.0.1:5555\n"
+ "ORPort 955\n"
+ "BandwidthRate 76900\n"
+ "BandwidthBurst 76900\n"
+ "MaxAdvertisedBandwidth 38500\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "You need at least a single managed-proxy to specify a transport "
+ "listen address. The ServerTransportListenAddr line will be "
+ "ignored.\n");
+
+ done:
+ escaped(NULL); // This will free the leaking memory from the previous escaped
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__constrained_sockets(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ConstrainedSockets 1\n"
+ "ConstrainedSockSize 0\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
+ "between 2048 and 262144 in 1024 byte increments.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ConstrainedSockets 1\n"
+ "ConstrainedSockSize 263168\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
+ "between 2048 and 262144 in 1024 byte increments.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ConstrainedSockets 1\n"
+ "ConstrainedSockSize 2047\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
+ "between 2048 and 262144 in 1024 byte increments.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ConstrainedSockets 1\n"
+ "ConstrainedSockSize 2048\n"
+ "DirPort 999\n"
+ "DirCache 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("You have requested constrained "
+ "socket buffers while also serving directory entries via DirPort."
+ " It is strongly suggested that you disable serving directory"
+ " requests when system TCP buffer resources are scarce.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "ConstrainedSockets 1\n"
+ "ConstrainedSockSize 2048\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg(
+ "You have requested constrained socket buffers while also serving"
+ " directory entries via DirPort. It is strongly suggested that "
+ "you disable serving directory requests when system TCP buffer "
+ "resources are scarce.\n");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__v3_auth(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 1000\n"
+ "V3AuthDistDelay 1000\n"
+ "V3AuthVotingInterval 1000\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "V3AuthVoteDelay plus V3AuthDistDelay must be less than half "
+ "V3AuthVotingInterval");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthVoteDelay is way too low.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 1\n"
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthVoteDelay is way too low.");
+ tor_free(msg);
+
+ // TODO: we can't reach the case of v3authvotedelay lower
+ // than MIN_VOTE_SECONDS but not lower than MIN_VOTE_SECONDS_TESTING,
+ // since they are the same
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthDistDelay 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthDistDelay is way too low.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthDistDelay 1\n"
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthDistDelay is way too low.");
+ tor_free(msg);
+
+ // TODO: we can't reach the case of v3authdistdelay lower than
+ // MIN_DIST_SECONDS but not lower than MIN_DIST_SECONDS_TESTING,
+ // since they are the same
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthNIntervalsValid 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthNIntervalsValid must be at least 2.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 49\n"
+ "V3AuthDistDelay 49\n"
+ "V3AuthVotingInterval 200\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely low.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 49\n"
+ "V3AuthDistDelay 49\n"
+ "V3AuthVotingInterval 200000\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely high.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 49\n"
+ "V3AuthDistDelay 49\n"
+ "V3AuthVotingInterval 1441\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("V3AuthVotingInterval does not divide"
+ " evenly into 24 hours.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 49\n"
+ "V3AuthDistDelay 49\n"
+ "V3AuthVotingInterval 1440\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("V3AuthVotingInterval does not divide"
+ " evenly into 24 hours.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "V3AuthVoteDelay 49\n"
+ "V3AuthDistDelay 49\n"
+ "V3AuthVotingInterval 299\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("V3AuthVotingInterval is very low. "
+ "This may lead to failure to synchronise for a consensus.\n");
+ tor_free(msg);
+
+ // TODO: It is impossible to reach the case of testingtor network, with
+ // v3authvotinginterval too low
+ /* free_options_test_data(tdata); */
+ /* tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES */
+ /* "V3AuthVoteDelay 1\n" */
+ /* "V3AuthDistDelay 1\n" */
+ /* "V3AuthVotingInterval 9\n" */
+ /* VALID_DIR_AUTH */
+ /* "TestingTorNetwork 1\n" */
+ /* ); */
+ /* ret = options_validate(tdata->old_opt, tdata->opt, */
+ /* tdata->def_opt, 0, &msg); */
+ /* tt_int_op(ret, OP_EQ, -1); */
+ /* tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely low."); */
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingV3AuthInitialVoteDelay 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingV3AuthInitialVoteDelay is way too low.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingV3AuthInitialDistDelay 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingV3AuthInitialDistDelay is way too low.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ tdata->opt->TestingV3AuthVotingStartOffset = 100000;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingV3AuthVotingStartOffset is higher than the "
+ "voting interval.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ );
+ tdata->opt->TestingV3AuthVotingStartOffset = -1;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "TestingV3AuthVotingStartOffset must be non-negative.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ "TestingV3AuthInitialVotingInterval 4\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingV3AuthInitialVotingInterval is insanely low.");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__virtual_addr(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "VirtualAddrNetworkIPv4 !!"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Error parsing VirtualAddressNetwork !!");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "VirtualAddrNetworkIPv6 !!"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "Error parsing VirtualAddressNetworkIPv6 !!");
+ tor_free(msg);
+
+ done:
+ escaped(NULL); // This will free the leaking memory from the previous escaped
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__exits(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AllowSingleHopExits 1"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_log_msg("You have set AllowSingleHopExits; "
+ "now your relay will allow others to make one-hop exits. However,"
+ " since by default most clients avoid relays that set this option,"
+ " most clients will ignore you.\n");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AllowSingleHopExits 1\n"
+ VALID_DIR_AUTH
+ );
+ mock_clean_saved_logs();
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_msg("You have set AllowSingleHopExits; "
+ "now your relay will allow others to make one-hop exits. However,"
+ " since by default most clients avoid relays that set this option,"
+ " most clients will ignore you.\n");
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__testing_options(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+ setup_capture_of_logs(LOG_WARN);
+
+#define TEST_TESTING_OPTION(name, low_val, high_val, err_low) \
+ STMT_BEGIN \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
+ VALID_DIR_AUTH \
+ "TestingTorNetwork 1\n" \
+ ); \
+ tdata->opt-> name = low_val; \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ tt_int_op(ret, OP_EQ, -1); \
+ tt_str_op(msg, OP_EQ, #name " " err_low); \
+ tor_free(msg); \
+ \
+ free_options_test_data(tdata); \
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
+ VALID_DIR_AUTH \
+ "TestingTorNetwork 1\n" \
+ ); \
+ tdata->opt-> name = high_val; \
+ mock_clean_saved_logs(); \
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
+ tt_int_op(ret, OP_EQ, 0); \
+ expect_log_msg( #name " is insanely high.\n"); \
+ tor_free(msg); \
+ STMT_END
+
+ TEST_TESTING_OPTION(TestingAuthDirTimeToLearnReachability, -1, 8000,
+ "must be non-negative.");
+ TEST_TESTING_OPTION(TestingEstimatedDescriptorPropagationTime, -1, 3601,
+ "must be non-negative.");
+ TEST_TESTING_OPTION(TestingClientMaxIntervalWithoutRequest, -1, 3601,
+ "is way too low.");
+ TEST_TESTING_OPTION(TestingDirConnectionMaxStall, 1, 3601,
+ "is way too low.");
+ // TODO: I think this points to a bug/regression in options_validate
+ TEST_TESTING_OPTION(TestingConsensusMaxDownloadTries, 1, 801,
+ "must be greater than 2.");
+ TEST_TESTING_OPTION(TestingDescriptorMaxDownloadTries, 1, 801,
+ "must be greater than 1.");
+ TEST_TESTING_OPTION(TestingMicrodescMaxDownloadTries, 1, 801,
+ "must be greater than 1.");
+ TEST_TESTING_OPTION(TestingCertMaxDownloadTries, 1, 801,
+ "must be greater than 1.");
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableConnBwEvent 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingEnableConnBwEvent may only be changed in "
+ "testing Tor networks!");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableConnBwEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ "___UsingTestNetworkDefaults 0\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableConnBwEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 0\n"
+ "___UsingTestNetworkDefaults 1\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableCellStatsEvent 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingEnableCellStatsEvent may only be changed in "
+ "testing Tor networks!");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableCellStatsEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ "___UsingTestNetworkDefaults 0\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableCellStatsEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 0\n"
+ "___UsingTestNetworkDefaults 1\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableTbEmptyEvent 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ, "TestingEnableTbEmptyEvent may only be changed "
+ "in testing Tor networks!");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableTbEmptyEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 1\n"
+ "___UsingTestNetworkDefaults 0\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "TestingEnableTbEmptyEvent 1\n"
+ VALID_DIR_AUTH
+ "TestingTorNetwork 0\n"
+ "___UsingTestNetworkDefaults 1\n"
+ );
+
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(!msg);
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ teardown_capture_of_logs();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+static void
+test_options_validate__accel(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ char *msg;
+ options_test_data_t *tdata = NULL;
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccelName foo\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HardwareAccel, OP_EQ, 1);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccelName foo\n"
+ );
+ tdata->opt->HardwareAccel = 2;
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tdata->opt->HardwareAccel, OP_EQ, 2);
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccelDir 1\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, -1);
+ tt_str_op(msg, OP_EQ,
+ "Can't use hardware crypto accelerator dir without engine name.");
+ tor_free(msg);
+
+ free_options_test_data(tdata);
+ tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
+ "AccelDir 1\n"
+ "AccelName something\n"
+ );
+ ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(msg);
+
+ done:
+ policies_free_all();
+ free_options_test_data(tdata);
+ tor_free(msg);
+}
+
+#define LOCAL_VALIDATE_TEST(name) \
+ { "validate__" #name, test_options_validate__ ## name, TT_FORK, NULL, NULL }
+
struct testcase_t options_tests[] = {
{ "validate", test_options_validate, TT_FORK, NULL, NULL },
- END_OF_TESTCASES
+ { "mem_dircache", test_have_enough_mem_for_dircache, TT_FORK, NULL, NULL },
+ LOCAL_VALIDATE_TEST(uname_for_server),
+ LOCAL_VALIDATE_TEST(outbound_addresses),
+ LOCAL_VALIDATE_TEST(data_directory),
+ LOCAL_VALIDATE_TEST(nickname),
+ LOCAL_VALIDATE_TEST(contactinfo),
+ LOCAL_VALIDATE_TEST(logs),
+ LOCAL_VALIDATE_TEST(authdir),
+ LOCAL_VALIDATE_TEST(relay_with_hidden_services),
+ LOCAL_VALIDATE_TEST(transproxy),
+ LOCAL_VALIDATE_TEST(exclude_nodes),
+ LOCAL_VALIDATE_TEST(scheduler),
+ LOCAL_VALIDATE_TEST(node_families),
+ LOCAL_VALIDATE_TEST(tlsec),
+ LOCAL_VALIDATE_TEST(token_bucket),
+ LOCAL_VALIDATE_TEST(recommended_packages),
+ LOCAL_VALIDATE_TEST(fetch_dir),
+ LOCAL_VALIDATE_TEST(conn_limit),
+ LOCAL_VALIDATE_TEST(paths_needed),
+ LOCAL_VALIDATE_TEST(max_client_circuits),
+ LOCAL_VALIDATE_TEST(ports),
+ LOCAL_VALIDATE_TEST(reachable_addresses),
+ LOCAL_VALIDATE_TEST(use_bridges),
+ LOCAL_VALIDATE_TEST(entry_nodes),
+ LOCAL_VALIDATE_TEST(invalid_nodes),
+ LOCAL_VALIDATE_TEST(safe_logging),
+ LOCAL_VALIDATE_TEST(publish_server_descriptor),
+ LOCAL_VALIDATE_TEST(testing),
+ LOCAL_VALIDATE_TEST(hidserv),
+ LOCAL_VALIDATE_TEST(predicted_ports),
+ LOCAL_VALIDATE_TEST(path_bias),
+ LOCAL_VALIDATE_TEST(bandwidth),
+ LOCAL_VALIDATE_TEST(circuits),
+ LOCAL_VALIDATE_TEST(port_forwarding),
+ LOCAL_VALIDATE_TEST(tor2web),
+ LOCAL_VALIDATE_TEST(rend),
+ LOCAL_VALIDATE_TEST(single_onion),
+ LOCAL_VALIDATE_TEST(accounting),
+ LOCAL_VALIDATE_TEST(proxy),
+ LOCAL_VALIDATE_TEST(control),
+ LOCAL_VALIDATE_TEST(families),
+ LOCAL_VALIDATE_TEST(addr_policies),
+ LOCAL_VALIDATE_TEST(dir_auth),
+ LOCAL_VALIDATE_TEST(transport),
+ LOCAL_VALIDATE_TEST(constrained_sockets),
+ LOCAL_VALIDATE_TEST(v3_auth),
+ LOCAL_VALIDATE_TEST(virtual_addr),
+ LOCAL_VALIDATE_TEST(exits),
+ LOCAL_VALIDATE_TEST(testing_options),
+ LOCAL_VALIDATE_TEST(accel),
+ END_OF_TESTCASES /* */
};
diff --git a/src/test/test_policy.c b/src/test/test_policy.c
index 37c36fed99..1ffdc2cd51 100644
--- a/src/test/test_policy.c
+++ b/src/test/test_policy.c
@@ -1,9 +1,12 @@
-/* Copyright (c) 2013-2015, The Tor Project, Inc. */
+/* Copyright (c) 2013-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
+#define CONFIG_PRIVATE
+#include "config.h"
#include "router.h"
#include "routerparse.h"
+#define POLICIES_PRIVATE
#include "policies.h"
#include "test.h"
@@ -29,12 +32,14 @@ test_short_policy_parse(const char *input,
short_policy_free(short_policy);
}
-/** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
- * that policies_summarize() produces the string <b>expected_summary</b> from
- * it. */
+/** Helper: Parse the exit policy string in <b>policy_str</b> with
+ * <b>options</b>, and make sure that policies_summarize() produces the string
+ * <b>expected_summary</b> from it when called with family. */
static void
-test_policy_summary_helper(const char *policy_str,
- const char *expected_summary)
+test_policy_summary_helper_family_flags(const char *policy_str,
+ const char *expected_summary,
+ sa_family_t family,
+ exit_policy_parser_cfg_t options)
{
config_line_t line;
smartlist_t *policy = smartlist_new();
@@ -42,17 +47,17 @@ test_policy_summary_helper(const char *policy_str,
char *summary_after = NULL;
int r;
short_policy_t *short_policy = NULL;
+ int success = 0;
line.key = (char*)"foo";
line.value = (char *)policy_str;
line.next = NULL;
r = policies_parse_exit_policy(&line, &policy,
- EXIT_POLICY_IPV6_ENABLED |
- EXIT_POLICY_ADD_DEFAULT, 0, NULL, 0);
+ options, NULL);
tt_int_op(r,OP_EQ, 0);
- summary = policy_summarize(policy, AF_INET);
+ summary = policy_summarize(policy, family);
tt_assert(summary != NULL);
tt_str_op(summary,OP_EQ, expected_summary);
@@ -62,7 +67,12 @@ test_policy_summary_helper(const char *policy_str,
summary_after = write_short_policy(short_policy);
tt_str_op(summary,OP_EQ, summary_after);
+ success = 1;
done:
+ /* If we don't print the flags on failure, it's very hard to diagnose bugs */
+ if (!success)
+ TT_DECLARE("CTXT", ("\n IPv%d\n Options: %x\n Policy: %s",
+ family == AF_INET ? 4 : 6, options, policy_str));
tor_free(summary_after);
tor_free(summary);
if (policy)
@@ -70,6 +80,50 @@ test_policy_summary_helper(const char *policy_str,
short_policy_free(short_policy);
}
+/** Like test_policy_summary_helper_family_flags, but tries all the different
+ * flag combinations */
+static void
+test_policy_summary_helper_family(const char *policy_str,
+ const char *expected_summary,
+ sa_family_t family)
+{
+ for (exit_policy_parser_cfg_t opt = 0;
+ opt <= EXIT_POLICY_OPTION_ALL;
+ opt++) {
+ if (family == AF_INET6 && !(opt & EXIT_POLICY_IPV6_ENABLED))
+ /* Skip the test: IPv6 addresses need IPv6 enabled */
+ continue;
+
+ if (opt & EXIT_POLICY_REJECT_LOCAL_INTERFACES)
+ /* Skip the test: local interfaces are machine-specific */
+ continue;
+
+ test_policy_summary_helper_family_flags(policy_str, expected_summary,
+ family, opt);
+ }
+}
+
+/** Like test_policy_summary_helper_family, but uses expected_summary for
+ * both IPv4 and IPv6. */
+static void
+test_policy_summary_helper(const char *policy_str,
+ const char *expected_summary)
+{
+ test_policy_summary_helper_family(policy_str, expected_summary, AF_INET);
+ test_policy_summary_helper_family(policy_str, expected_summary, AF_INET6);
+}
+
+/** Like test_policy_summary_helper_family, but uses expected_summary4 for
+ * IPv4 and expected_summary6 for IPv6. */
+static void
+test_policy_summary_helper6(const char *policy_str,
+ const char *expected_summary4,
+ const char *expected_summary6)
+{
+ test_policy_summary_helper_family(policy_str, expected_summary4, AF_INET);
+ test_policy_summary_helper_family(policy_str, expected_summary6, AF_INET6);
+}
+
/** Run unit tests for generating summary lines of exit policies */
static void
test_policies_general(void *arg)
@@ -80,7 +134,8 @@ test_policies_general(void *arg)
*policy7 = NULL, *policy8 = NULL, *policy9 = NULL,
*policy10 = NULL, *policy11 = NULL, *policy12 = NULL;
addr_policy_t *p;
- tor_addr_t tar;
+ tor_addr_t tar, tar2;
+ smartlist_t *addr_list = NULL;
config_line_t line;
smartlist_t *sm = NULL;
char *policy_str = NULL;
@@ -115,17 +170,22 @@ test_policies_general(void *arg)
tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy2,
EXIT_POLICY_IPV6_ENABLED |
EXIT_POLICY_REJECT_PRIVATE |
- EXIT_POLICY_ADD_DEFAULT, 0,
- NULL, 0));
+ EXIT_POLICY_ADD_DEFAULT, NULL));
tt_assert(policy2);
- tor_addr_parse(&tar, "[2000::1234]");
+ tor_addr_from_ipv4h(&tar, 0x0306090cu);
+ tor_addr_parse(&tar2, "[2000::1234]");
+ addr_list = smartlist_new();
+ smartlist_add(addr_list, &tar);
+ smartlist_add(addr_list, &tar2);
tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy12,
EXIT_POLICY_IPV6_ENABLED |
EXIT_POLICY_REJECT_PRIVATE |
EXIT_POLICY_ADD_DEFAULT,
- 0x0306090cu, &tar, 1));
+ addr_list));
+ smartlist_free(addr_list);
+ addr_list = NULL;
tt_assert(policy12);
@@ -206,15 +266,15 @@ test_policies_general(void *arg)
tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy8,
EXIT_POLICY_IPV6_ENABLED |
EXIT_POLICY_REJECT_PRIVATE |
- EXIT_POLICY_ADD_DEFAULT, 0,
- NULL, 0));
+ EXIT_POLICY_ADD_DEFAULT,
+ NULL));
tt_assert(policy8);
tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy9,
EXIT_POLICY_REJECT_PRIVATE |
- EXIT_POLICY_ADD_DEFAULT, 0,
- NULL, 0));
+ EXIT_POLICY_ADD_DEFAULT,
+ NULL));
tt_assert(policy9);
@@ -244,23 +304,112 @@ test_policies_general(void *arg)
tt_assert(!exit_policy_is_general_exit(policy10));
tt_assert(!exit_policy_is_general_exit(policy11));
- tt_assert(cmp_addr_policies(policy, policy2));
- tt_assert(cmp_addr_policies(policy, NULL));
- tt_assert(!cmp_addr_policies(policy2, policy2));
- tt_assert(!cmp_addr_policies(NULL, NULL));
-
- tt_assert(!policy_is_reject_star(policy2, AF_INET));
- tt_assert(policy_is_reject_star(policy, AF_INET));
- tt_assert(policy_is_reject_star(policy10, AF_INET));
- tt_assert(!policy_is_reject_star(policy10, AF_INET6));
- tt_assert(policy_is_reject_star(policy11, AF_INET));
- tt_assert(policy_is_reject_star(policy11, AF_INET6));
- tt_assert(policy_is_reject_star(NULL, AF_INET));
- tt_assert(policy_is_reject_star(NULL, AF_INET6));
+ tt_assert(!addr_policies_eq(policy, policy2));
+ tt_assert(!addr_policies_eq(policy, NULL));
+ tt_assert(addr_policies_eq(policy2, policy2));
+ tt_assert(addr_policies_eq(NULL, NULL));
+
+ tt_assert(!policy_is_reject_star(policy2, AF_INET, 1));
+ tt_assert(policy_is_reject_star(policy, AF_INET, 1));
+ tt_assert(policy_is_reject_star(policy10, AF_INET, 1));
+ tt_assert(!policy_is_reject_star(policy10, AF_INET6, 1));
+ tt_assert(policy_is_reject_star(policy11, AF_INET, 1));
+ tt_assert(policy_is_reject_star(policy11, AF_INET6, 1));
+ tt_assert(policy_is_reject_star(NULL, AF_INET, 1));
+ tt_assert(policy_is_reject_star(NULL, AF_INET6, 1));
+ tt_assert(!policy_is_reject_star(NULL, AF_INET, 0));
+ tt_assert(!policy_is_reject_star(NULL, AF_INET6, 0));
addr_policy_list_free(policy);
policy = NULL;
+ /* make sure assume_action works */
+ malformed_list = 0;
+ p = router_parse_addr_policy_item_from_string("127.0.0.1",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("127.0.0.1:*",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("[::]",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("[::]:*",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("[face::b]",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("[b::aaaa]",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("*",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("*4",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ p = router_parse_addr_policy_item_from_string("*6",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(p);
+ addr_policy_free(p);
+ tt_assert(!malformed_list);
+
+ /* These are all ambiguous IPv6 addresses, it's good that we reject them */
+ p = router_parse_addr_policy_item_from_string("acce::abcd",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(!p);
+ tt_assert(malformed_list);
+ malformed_list = 0;
+
+ p = router_parse_addr_policy_item_from_string("7:1234",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(!p);
+ tt_assert(malformed_list);
+ malformed_list = 0;
+
+ p = router_parse_addr_policy_item_from_string("::",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(!p);
+ tt_assert(malformed_list);
+ malformed_list = 0;
+
/* make sure compacting logic works. */
policy = NULL;
line.key = (char*)"foo";
@@ -268,8 +417,7 @@ test_policies_general(void *arg)
line.next = NULL;
tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
EXIT_POLICY_IPV6_ENABLED |
- EXIT_POLICY_ADD_DEFAULT, 0,
- NULL, 0));
+ EXIT_POLICY_ADD_DEFAULT, NULL));
tt_assert(policy);
//test_streq(policy->string, "accept *:80");
@@ -297,13 +445,14 @@ test_policies_general(void *arg)
"reject 14.0.0.0/9:80,"
"reject 15.0.0.0:81,"
"accept *:*", "accept 1-65535");
- test_policy_summary_helper("reject 11.0.0.0/9:80,"
- "reject 12.0.0.0/9:80,"
- "reject 13.0.0.0/9:80,"
- "reject 14.0.0.0/9:80,"
- "reject 15.0.0.0:80,"
- "accept *:*",
- "reject 80");
+ test_policy_summary_helper6("reject 11.0.0.0/9:80,"
+ "reject 12.0.0.0/9:80,"
+ "reject 13.0.0.0/9:80,"
+ "reject 14.0.0.0/9:80,"
+ "reject 15.0.0.0:80,"
+ "accept *:*",
+ "reject 80",
+ "accept 1-65535");
/* no exits */
test_policy_summary_helper("accept 11.0.0.0/9:80,"
"reject *:*",
@@ -334,6 +483,458 @@ test_policies_general(void *arg)
"reject *:7,"
"accept *:*",
"reject 1,3,5,7");
+ /* long policies */
+ /* standard long policy on many exits */
+ test_policy_summary_helper("accept *:20-23,"
+ "accept *:43,"
+ "accept *:53,"
+ "accept *:79-81,"
+ "accept *:88,"
+ "accept *:110,"
+ "accept *:143,"
+ "accept *:194,"
+ "accept *:220,"
+ "accept *:389,"
+ "accept *:443,"
+ "accept *:464,"
+ "accept *:531,"
+ "accept *:543-544,"
+ "accept *:554,"
+ "accept *:563,"
+ "accept *:636,"
+ "accept *:706,"
+ "accept *:749,"
+ "accept *:873,"
+ "accept *:902-904,"
+ "accept *:981,"
+ "accept *:989-995,"
+ "accept *:1194,"
+ "accept *:1220,"
+ "accept *:1293,"
+ "accept *:1500,"
+ "accept *:1533,"
+ "accept *:1677,"
+ "accept *:1723,"
+ "accept *:1755,"
+ "accept *:1863,"
+ "accept *:2082,"
+ "accept *:2083,"
+ "accept *:2086-2087,"
+ "accept *:2095-2096,"
+ "accept *:2102-2104,"
+ "accept *:3128,"
+ "accept *:3389,"
+ "accept *:3690,"
+ "accept *:4321,"
+ "accept *:4643,"
+ "accept *:5050,"
+ "accept *:5190,"
+ "accept *:5222-5223,"
+ "accept *:5228,"
+ "accept *:5900,"
+ "accept *:6660-6669,"
+ "accept *:6679,"
+ "accept *:6697,"
+ "accept *:8000,"
+ "accept *:8008,"
+ "accept *:8074,"
+ "accept *:8080,"
+ "accept *:8087-8088,"
+ "accept *:8332-8333,"
+ "accept *:8443,"
+ "accept *:8888,"
+ "accept *:9418,"
+ "accept *:9999,"
+ "accept *:10000,"
+ "accept *:11371,"
+ "accept *:12350,"
+ "accept *:19294,"
+ "accept *:19638,"
+ "accept *:23456,"
+ "accept *:33033,"
+ "accept *:64738,"
+ "reject *:*",
+ "accept 20-23,43,53,79-81,88,110,143,194,220,389,"
+ "443,464,531,543-544,554,563,636,706,749,873,"
+ "902-904,981,989-995,1194,1220,1293,1500,1533,"
+ "1677,1723,1755,1863,2082-2083,2086-2087,"
+ "2095-2096,2102-2104,3128,3389,3690,4321,4643,"
+ "5050,5190,5222-5223,5228,5900,6660-6669,6679,"
+ "6697,8000,8008,8074,8080,8087-8088,8332-8333,"
+ "8443,8888,9418,9999-10000,11371,12350,19294,"
+ "19638,23456,33033,64738");
+ /* short policy with configured addresses */
+ test_policy_summary_helper("reject 149.56.1.1:*,"
+ "reject [2607:5300:1:1::1:0]:*,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "accept 80,443");
+ /* short policy with configured and local interface addresses */
+ test_policy_summary_helper("reject 149.56.1.0:*,"
+ "reject 149.56.1.1:*,"
+ "reject 149.56.1.2:*,"
+ "reject 149.56.1.3:*,"
+ "reject 149.56.1.4:*,"
+ "reject 149.56.1.5:*,"
+ "reject 149.56.1.6:*,"
+ "reject 149.56.1.7:*,"
+ "reject [2607:5300:1:1::1:0]:*,"
+ "reject [2607:5300:1:1::1:1]:*,"
+ "reject [2607:5300:1:1::1:2]:*,"
+ "reject [2607:5300:1:1::1:3]:*,"
+ "reject [2607:5300:1:1::2:0]:*,"
+ "reject [2607:5300:1:1::2:1]:*,"
+ "reject [2607:5300:1:1::2:2]:*,"
+ "reject [2607:5300:1:1::2:3]:*,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "accept 80,443");
+ /* short policy with configured netblocks */
+ test_policy_summary_helper("reject 149.56.0.0/16,"
+ "reject6 2607:5300::/32,"
+ "reject6 2608:5300::/64,"
+ "reject6 2609:5300::/96,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "accept 80,443");
+ /* short policy with large netblocks that do not count as a rejection */
+ test_policy_summary_helper("reject 148.0.0.0/7,"
+ "reject6 2600::/16,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "accept 80,443");
+ /* short policy with large netblocks that count as a rejection */
+ test_policy_summary_helper("reject 148.0.0.0/6,"
+ "reject6 2600::/15,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "reject 1-65535");
+ /* short policy with huge netblocks that count as a rejection */
+ test_policy_summary_helper("reject 128.0.0.0/1,"
+ "reject6 8000::/1,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "reject 1-65535");
+ /* short policy which blocks everything using netblocks */
+ test_policy_summary_helper("reject 0.0.0.0/0,"
+ "reject6 ::/0,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "reject 1-65535");
+ /* short policy which has repeated redundant netblocks */
+ test_policy_summary_helper("reject 0.0.0.0/0,"
+ "reject 0.0.0.0/0,"
+ "reject 0.0.0.0/0,"
+ "reject 0.0.0.0/0,"
+ "reject 0.0.0.0/0,"
+ "reject6 ::/0,"
+ "reject6 ::/0,"
+ "reject6 ::/0,"
+ "reject6 ::/0,"
+ "reject6 ::/0,"
+ "accept *:80,"
+ "accept *:443,"
+ "reject *:*",
+ "reject 1-65535");
+
+ /* longest possible policy
+ * (1-2,4-5,... is longer, but gets reduced to 3,6,... )
+ * Going all the way to 65535 is incredibly slow, so we just go slightly
+ * more than the expected length */
+ test_policy_summary_helper("accept *:1,"
+ "accept *:3,"
+ "accept *:5,"
+ "accept *:7,"
+ "accept *:9,"
+ "accept *:11,"
+ "accept *:13,"
+ "accept *:15,"
+ "accept *:17,"
+ "accept *:19,"
+ "accept *:21,"
+ "accept *:23,"
+ "accept *:25,"
+ "accept *:27,"
+ "accept *:29,"
+ "accept *:31,"
+ "accept *:33,"
+ "accept *:35,"
+ "accept *:37,"
+ "accept *:39,"
+ "accept *:41,"
+ "accept *:43,"
+ "accept *:45,"
+ "accept *:47,"
+ "accept *:49,"
+ "accept *:51,"
+ "accept *:53,"
+ "accept *:55,"
+ "accept *:57,"
+ "accept *:59,"
+ "accept *:61,"
+ "accept *:63,"
+ "accept *:65,"
+ "accept *:67,"
+ "accept *:69,"
+ "accept *:71,"
+ "accept *:73,"
+ "accept *:75,"
+ "accept *:77,"
+ "accept *:79,"
+ "accept *:81,"
+ "accept *:83,"
+ "accept *:85,"
+ "accept *:87,"
+ "accept *:89,"
+ "accept *:91,"
+ "accept *:93,"
+ "accept *:95,"
+ "accept *:97,"
+ "accept *:99,"
+ "accept *:101,"
+ "accept *:103,"
+ "accept *:105,"
+ "accept *:107,"
+ "accept *:109,"
+ "accept *:111,"
+ "accept *:113,"
+ "accept *:115,"
+ "accept *:117,"
+ "accept *:119,"
+ "accept *:121,"
+ "accept *:123,"
+ "accept *:125,"
+ "accept *:127,"
+ "accept *:129,"
+ "accept *:131,"
+ "accept *:133,"
+ "accept *:135,"
+ "accept *:137,"
+ "accept *:139,"
+ "accept *:141,"
+ "accept *:143,"
+ "accept *:145,"
+ "accept *:147,"
+ "accept *:149,"
+ "accept *:151,"
+ "accept *:153,"
+ "accept *:155,"
+ "accept *:157,"
+ "accept *:159,"
+ "accept *:161,"
+ "accept *:163,"
+ "accept *:165,"
+ "accept *:167,"
+ "accept *:169,"
+ "accept *:171,"
+ "accept *:173,"
+ "accept *:175,"
+ "accept *:177,"
+ "accept *:179,"
+ "accept *:181,"
+ "accept *:183,"
+ "accept *:185,"
+ "accept *:187,"
+ "accept *:189,"
+ "accept *:191,"
+ "accept *:193,"
+ "accept *:195,"
+ "accept *:197,"
+ "accept *:199,"
+ "accept *:201,"
+ "accept *:203,"
+ "accept *:205,"
+ "accept *:207,"
+ "accept *:209,"
+ "accept *:211,"
+ "accept *:213,"
+ "accept *:215,"
+ "accept *:217,"
+ "accept *:219,"
+ "accept *:221,"
+ "accept *:223,"
+ "accept *:225,"
+ "accept *:227,"
+ "accept *:229,"
+ "accept *:231,"
+ "accept *:233,"
+ "accept *:235,"
+ "accept *:237,"
+ "accept *:239,"
+ "accept *:241,"
+ "accept *:243,"
+ "accept *:245,"
+ "accept *:247,"
+ "accept *:249,"
+ "accept *:251,"
+ "accept *:253,"
+ "accept *:255,"
+ "accept *:257,"
+ "accept *:259,"
+ "accept *:261,"
+ "accept *:263,"
+ "accept *:265,"
+ "accept *:267,"
+ "accept *:269,"
+ "accept *:271,"
+ "accept *:273,"
+ "accept *:275,"
+ "accept *:277,"
+ "accept *:279,"
+ "accept *:281,"
+ "accept *:283,"
+ "accept *:285,"
+ "accept *:287,"
+ "accept *:289,"
+ "accept *:291,"
+ "accept *:293,"
+ "accept *:295,"
+ "accept *:297,"
+ "accept *:299,"
+ "accept *:301,"
+ "accept *:303,"
+ "accept *:305,"
+ "accept *:307,"
+ "accept *:309,"
+ "accept *:311,"
+ "accept *:313,"
+ "accept *:315,"
+ "accept *:317,"
+ "accept *:319,"
+ "accept *:321,"
+ "accept *:323,"
+ "accept *:325,"
+ "accept *:327,"
+ "accept *:329,"
+ "accept *:331,"
+ "accept *:333,"
+ "accept *:335,"
+ "accept *:337,"
+ "accept *:339,"
+ "accept *:341,"
+ "accept *:343,"
+ "accept *:345,"
+ "accept *:347,"
+ "accept *:349,"
+ "accept *:351,"
+ "accept *:353,"
+ "accept *:355,"
+ "accept *:357,"
+ "accept *:359,"
+ "accept *:361,"
+ "accept *:363,"
+ "accept *:365,"
+ "accept *:367,"
+ "accept *:369,"
+ "accept *:371,"
+ "accept *:373,"
+ "accept *:375,"
+ "accept *:377,"
+ "accept *:379,"
+ "accept *:381,"
+ "accept *:383,"
+ "accept *:385,"
+ "accept *:387,"
+ "accept *:389,"
+ "accept *:391,"
+ "accept *:393,"
+ "accept *:395,"
+ "accept *:397,"
+ "accept *:399,"
+ "accept *:401,"
+ "accept *:403,"
+ "accept *:405,"
+ "accept *:407,"
+ "accept *:409,"
+ "accept *:411,"
+ "accept *:413,"
+ "accept *:415,"
+ "accept *:417,"
+ "accept *:419,"
+ "accept *:421,"
+ "accept *:423,"
+ "accept *:425,"
+ "accept *:427,"
+ "accept *:429,"
+ "accept *:431,"
+ "accept *:433,"
+ "accept *:435,"
+ "accept *:437,"
+ "accept *:439,"
+ "accept *:441,"
+ "accept *:443,"
+ "accept *:445,"
+ "accept *:447,"
+ "accept *:449,"
+ "accept *:451,"
+ "accept *:453,"
+ "accept *:455,"
+ "accept *:457,"
+ "accept *:459,"
+ "accept *:461,"
+ "accept *:463,"
+ "accept *:465,"
+ "accept *:467,"
+ "accept *:469,"
+ "accept *:471,"
+ "accept *:473,"
+ "accept *:475,"
+ "accept *:477,"
+ "accept *:479,"
+ "accept *:481,"
+ "accept *:483,"
+ "accept *:485,"
+ "accept *:487,"
+ "accept *:489,"
+ "accept *:491,"
+ "accept *:493,"
+ "accept *:495,"
+ "accept *:497,"
+ "accept *:499,"
+ "accept *:501,"
+ "accept *:503,"
+ "accept *:505,"
+ "accept *:507,"
+ "accept *:509,"
+ "accept *:511,"
+ "accept *:513,"
+ "accept *:515,"
+ "accept *:517,"
+ "accept *:519,"
+ "accept *:521,"
+ "accept *:523,"
+ "accept *:525,"
+ "accept *:527,"
+ "accept *:529,"
+ "reject *:*",
+ "accept 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,"
+ "31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,"
+ "63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,"
+ "95,97,99,101,103,105,107,109,111,113,115,117,"
+ "119,121,123,125,127,129,131,133,135,137,139,141,"
+ "143,145,147,149,151,153,155,157,159,161,163,165,"
+ "167,169,171,173,175,177,179,181,183,185,187,189,"
+ "191,193,195,197,199,201,203,205,207,209,211,213,"
+ "215,217,219,221,223,225,227,229,231,233,235,237,"
+ "239,241,243,245,247,249,251,253,255,257,259,261,"
+ "263,265,267,269,271,273,275,277,279,281,283,285,"
+ "287,289,291,293,295,297,299,301,303,305,307,309,"
+ "311,313,315,317,319,321,323,325,327,329,331,333,"
+ "335,337,339,341,343,345,347,349,351,353,355,357,"
+ "359,361,363,365,367,369,371,373,375,377,379,381,"
+ "383,385,387,389,391,393,395,397,399,401,403,405,"
+ "407,409,411,413,415,417,419,421,423,425,427,429,"
+ "431,433,435,437,439,441,443,445,447,449,451,453,"
+ "455,457,459,461,463,465,467,469,471,473,475,477,"
+ "479,481,483,485,487,489,491,493,495,497,499,501,"
+ "503,505,507,509,511,513,515,517,519,521,523");
/* Short policies with unrecognized formats should get accepted. */
test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
@@ -428,18 +1029,17 @@ test_policies_general(void *arg)
/* Test a too-long policy. */
{
- int i;
- char *policy = NULL;
+ char *policy_strng = NULL;
smartlist_t *chunks = smartlist_new();
smartlist_add(chunks, tor_strdup("accept "));
for (i=1; i<10000; ++i)
smartlist_add_asprintf(chunks, "%d,", i);
smartlist_add(chunks, tor_strdup("20000"));
- policy = smartlist_join_strings(chunks, "", 0, NULL);
+ policy_strng = smartlist_join_strings(chunks, "", 0, NULL);
SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
smartlist_free(chunks);
- short_parsed = parse_short_policy(policy);/* shouldn't be accepted */
- tor_free(policy);
+ short_parsed = parse_short_policy(policy_strng);/* shouldn't be accepted */
+ tor_free(policy_strng);
tt_ptr_op(NULL, OP_EQ, short_parsed);
}
@@ -489,6 +1089,323 @@ test_policies_general(void *arg)
short_policy_free(short_parsed);
}
+/** Helper: Check that policy_list contains address */
+static int
+test_policy_has_address_helper(const smartlist_t *policy_list,
+ const tor_addr_t *addr)
+{
+ int found = 0;
+
+ tt_assert(policy_list);
+ tt_assert(addr);
+
+ SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
+ if (tor_addr_eq(&p->addr, addr)) {
+ found = 1;
+ }
+ } SMARTLIST_FOREACH_END(p);
+
+ return found;
+
+ done:
+ return 0;
+}
+
+#define TEST_IPV4_ADDR (0x01020304)
+#define TEST_IPV6_ADDR ("2002::abcd")
+
+/** Run unit tests for rejecting the configured addresses on this exit relay
+ * using policies_parse_exit_policy_reject_private */
+static void
+test_policies_reject_exit_address(void *arg)
+{
+ smartlist_t *policy = NULL;
+ tor_addr_t ipv4_addr, ipv6_addr;
+ smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
+ (void)arg;
+
+ tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
+ tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
+
+ ipv4_list = smartlist_new();
+ ipv6_list = smartlist_new();
+ both_list = smartlist_new();
+ dupl_list = smartlist_new();
+
+ smartlist_add(ipv4_list, &ipv4_addr);
+ smartlist_add(both_list, &ipv4_addr);
+ smartlist_add(dupl_list, &ipv4_addr);
+ smartlist_add(dupl_list, &ipv4_addr);
+ smartlist_add(dupl_list, &ipv4_addr);
+
+ smartlist_add(ipv6_list, &ipv6_addr);
+ smartlist_add(both_list, &ipv6_addr);
+ smartlist_add(dupl_list, &ipv6_addr);
+ smartlist_add(dupl_list, &ipv6_addr);
+
+ /* IPv4-Only Exits */
+
+ /* test that IPv4 addresses are rejected on an IPv4-only exit */
+ policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
+ * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
+ * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
+ * need to do anything) */
+ policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
+ tt_assert(policy == NULL);
+
+ /* test that only IPv4 addresses are rejected on an IPv4-only exit */
+ policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* Test that lists with duplicate entries produce the same results */
+ policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* IPv4/IPv6 Exits */
+
+ /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
+ policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
+ policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
+ policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 2);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* Test that lists with duplicate entries produce the same results */
+ policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 2);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
+ tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ done:
+ addr_policy_list_free(policy);
+ smartlist_free(ipv4_list);
+ smartlist_free(ipv6_list);
+ smartlist_free(both_list);
+ smartlist_free(dupl_list);
+}
+
+static smartlist_t *test_configured_ports = NULL;
+
+/** Returns test_configured_ports */
+static const smartlist_t *
+mock_get_configured_ports(void)
+{
+ return test_configured_ports;
+}
+
+/** Run unit tests for rejecting publicly routable configured port addresses
+ * on this exit relay using policies_parse_exit_policy_reject_private */
+static void
+test_policies_reject_port_address(void *arg)
+{
+ smartlist_t *policy = NULL;
+ port_cfg_t *ipv4_port = NULL;
+ port_cfg_t *ipv6_port = NULL;
+ (void)arg;
+
+ test_configured_ports = smartlist_new();
+
+ ipv4_port = port_cfg_new(0);
+ tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
+ smartlist_add(test_configured_ports, ipv4_port);
+
+ ipv6_port = port_cfg_new(0);
+ tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
+ smartlist_add(test_configured_ports, ipv6_port);
+
+ MOCK(get_configured_ports, mock_get_configured_ports);
+
+ /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
+ * is NOT rejected (all IPv6 addresses are rejected by
+ * policies_parse_exit_policy_internal on IPv4-only exits, so
+ * policies_parse_exit_policy_reject_private doesn't need to do anything
+ * with IPv6 addresses on IPv4-only exits) */
+ policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 1);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
+ policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == 2);
+ tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
+ tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ done:
+ addr_policy_list_free(policy);
+ if (test_configured_ports) {
+ SMARTLIST_FOREACH(test_configured_ports,
+ port_cfg_t *, p, port_cfg_free(p));
+ smartlist_free(test_configured_ports);
+ test_configured_ports = NULL;
+ }
+ UNMOCK(get_configured_ports);
+}
+
+static smartlist_t *mock_ipv4_addrs = NULL;
+static smartlist_t *mock_ipv6_addrs = NULL;
+
+/* mock get_interface_address6_list, returning a deep copy of the template
+ * address list ipv4_interface_address_list or ipv6_interface_address_list */
+static smartlist_t *
+mock_get_interface_address6_list(int severity,
+ sa_family_t family,
+ int include_internal)
+{
+ (void)severity;
+ (void)include_internal;
+ smartlist_t *clone_list = smartlist_new();
+ smartlist_t *template_list = NULL;
+
+ if (family == AF_INET) {
+ template_list = mock_ipv4_addrs;
+ } else if (family == AF_INET6) {
+ template_list = mock_ipv6_addrs;
+ } else {
+ return NULL;
+ }
+
+ tt_assert(template_list);
+
+ SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
+ tor_addr_t *dest_addr = tor_malloc(sizeof(tor_addr_t));
+ memset(dest_addr, 0, sizeof(*dest_addr));
+ tor_addr_copy_tight(dest_addr, src_addr);
+ smartlist_add(clone_list, dest_addr);
+ } SMARTLIST_FOREACH_END(src_addr);
+
+ return clone_list;
+
+ done:
+ free_interface_address6_list(clone_list);
+ return NULL;
+}
+
+/** Run unit tests for rejecting publicly routable interface addresses on this
+ * exit relay using policies_parse_exit_policy_reject_private */
+static void
+test_policies_reject_interface_address(void *arg)
+{
+ smartlist_t *policy = NULL;
+ smartlist_t *public_ipv4_addrs =
+ get_interface_address6_list(LOG_INFO, AF_INET, 0);
+ smartlist_t *public_ipv6_addrs =
+ get_interface_address6_list(LOG_INFO, AF_INET6, 0);
+ tor_addr_t ipv4_addr, ipv6_addr;
+ (void)arg;
+
+ /* test that no addresses are rejected when none are supplied/requested */
+ policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
+ tt_assert(policy == NULL);
+
+ /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
+ * (and allow for duplicates)
+ */
+ policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
+ if (policy) {
+ tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
+ addr_policy_list_free(policy);
+ policy = NULL;
+ }
+
+ /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
+ * exit (and allow for duplicates) */
+ policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
+ if (policy) {
+ tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
+ + smartlist_len(public_ipv6_addrs)));
+ addr_policy_list_free(policy);
+ policy = NULL;
+ }
+
+ /* Now do it all again, but mocked */
+ tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
+ mock_ipv4_addrs = smartlist_new();
+ smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
+
+ tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
+ mock_ipv6_addrs = smartlist_new();
+ smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
+
+ MOCK(get_interface_address6_list, mock_get_interface_address6_list);
+
+ /* test that no addresses are rejected when none are supplied/requested */
+ policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
+ tt_assert(policy == NULL);
+
+ /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
+ */
+ policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
+ * exit */
+ policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
+ tt_assert(policy);
+ tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
+ + smartlist_len(mock_ipv6_addrs)));
+ addr_policy_list_free(policy);
+ policy = NULL;
+
+ done:
+ addr_policy_list_free(policy);
+ free_interface_address6_list(public_ipv4_addrs);
+ free_interface_address6_list(public_ipv6_addrs);
+
+ UNMOCK(get_interface_address6_list);
+ /* we don't use free_interface_address6_list on these lists because their
+ * address pointers are stack-based */
+ smartlist_free(mock_ipv4_addrs);
+ smartlist_free(mock_ipv6_addrs);
+}
+
+#undef TEST_IPV4_ADDR
+#undef TEST_IPV6_ADDR
+
static void
test_dump_exit_policy_to_string(void *arg)
{
@@ -578,10 +1495,893 @@ test_dump_exit_policy_to_string(void *arg)
tor_free(ep);
}
+static routerinfo_t *mock_desc_routerinfo = NULL;
+static const routerinfo_t *
+mock_router_get_my_routerinfo(void)
+{
+ return mock_desc_routerinfo;
+}
+
+#define DEFAULT_POLICY_STRING "reject *:*"
+#define TEST_IPV4_ADDR (0x02040608)
+#define TEST_IPV6_ADDR ("2003::ef01")
+
+static or_options_t mock_options;
+
+static const or_options_t *
+mock_get_options(void)
+{
+ return &mock_options;
+}
+
+/** Run unit tests for generating summary lines of exit policies */
+static void
+test_policies_getinfo_helper_policies(void *arg)
+{
+ (void)arg;
+ int rv = 0;
+ size_t ipv4_len = 0, ipv6_len = 0;
+ char *answer = NULL;
+ const char *errmsg = NULL;
+ routerinfo_t mock_my_routerinfo;
+
+ memset(&mock_my_routerinfo, 0, sizeof(mock_my_routerinfo));
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tor_free(answer);
+
+ memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
+ MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
+ mock_my_routerinfo.exit_policy = smartlist_new();
+ mock_desc_routerinfo = &mock_my_routerinfo;
+
+ memset(&mock_options, 0, sizeof(or_options_t));
+ MOCK(get_options, mock_get_options);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) == 0);
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ ipv4_len = strlen(answer);
+ tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
+ tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ ipv6_len = strlen(answer);
+ tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
+ tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ /* It's either empty or it's the default */
+ tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
+ tor_free(answer);
+
+ mock_my_routerinfo.addr = TEST_IPV4_ADDR;
+ tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
+ append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
+ append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
+
+ mock_options.IPv6Exit = 1;
+ tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
+ tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
+
+ mock_options.ExitPolicyRejectPrivate = 1;
+ mock_options.ExitPolicyRejectLocalInterfaces = 1;
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tor_free(answer);
+
+ mock_options.ExitPolicyRejectPrivate = 1;
+ mock_options.ExitPolicyRejectLocalInterfaces = 0;
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tor_free(answer);
+
+ mock_options.ExitPolicyRejectPrivate = 0;
+ mock_options.ExitPolicyRejectLocalInterfaces = 1;
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tor_free(answer);
+
+ mock_options.ExitPolicyRejectPrivate = 0;
+ mock_options.ExitPolicyRejectLocalInterfaces = 0;
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
+ &answer, &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) == 0);
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ ipv4_len = strlen(answer);
+ tt_assert(ipv4_len > 0);
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ ipv6_len = strlen(answer);
+ tt_assert(ipv6_len > 0);
+ tor_free(answer);
+
+ rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
+ &errmsg);
+ tt_assert(rv == 0);
+ tt_assert(answer != NULL);
+ tt_assert(strlen(answer) > 0);
+ tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
+ tor_free(answer);
+
+ done:
+ tor_free(answer);
+ UNMOCK(get_options);
+ UNMOCK(router_get_my_routerinfo);
+ addr_policy_list_free(mock_my_routerinfo.exit_policy);
+}
+
+#undef DEFAULT_POLICY_STRING
+#undef TEST_IPV4_ADDR
+#undef TEST_IPV6_ADDR
+
+#define TEST_IPV4_ADDR_STR "1.2.3.4"
+#define TEST_IPV6_ADDR_STR "[1002::4567]"
+#define REJECT_IPv4_FINAL_STR "reject 0.0.0.0/0:*"
+#define REJECT_IPv6_FINAL_STR "reject [::]/0:*"
+
+#define OTHER_IPV4_ADDR_STR "6.7.8.9"
+#define OTHER_IPV6_ADDR_STR "[afff::]"
+
+/** Run unit tests for fascist_firewall_allows_address */
+static void
+test_policies_fascist_firewall_allows_address(void *arg)
+{
+ (void)arg;
+ tor_addr_t ipv4_addr, ipv6_addr, r_ipv4_addr, r_ipv6_addr;
+ tor_addr_t n_ipv4_addr, n_ipv6_addr;
+ const uint16_t port = 1234;
+ smartlist_t *policy = NULL;
+ smartlist_t *e_policy = NULL;
+ addr_policy_t *item = NULL;
+ int malformed_list = 0;
+
+ /* Setup the options and the items in the policies */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ MOCK(get_options, mock_get_options);
+
+ policy = smartlist_new();
+ item = router_parse_addr_policy_item_from_string("accept "
+ TEST_IPV4_ADDR_STR ":*",
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(item);
+ tt_assert(!malformed_list);
+ smartlist_add(policy, item);
+ item = router_parse_addr_policy_item_from_string("accept "
+ TEST_IPV6_ADDR_STR,
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(item);
+ tt_assert(!malformed_list);
+ smartlist_add(policy, item);
+ /* Normally, policy_expand_unspec would do this for us */
+ item = router_parse_addr_policy_item_from_string(REJECT_IPv4_FINAL_STR,
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(item);
+ tt_assert(!malformed_list);
+ smartlist_add(policy, item);
+ item = router_parse_addr_policy_item_from_string(REJECT_IPv6_FINAL_STR,
+ ADDR_POLICY_ACCEPT,
+ &malformed_list);
+ tt_assert(item);
+ tt_assert(!malformed_list);
+ smartlist_add(policy, item);
+ item = NULL;
+
+ e_policy = smartlist_new();
+
+ /*
+ char *polstr = policy_dump_to_string(policy, 1, 1);
+ printf("%s\n", polstr);
+ tor_free(polstr);
+ */
+
+ /* Parse the addresses */
+ tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR_STR);
+ tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR_STR);
+ tor_addr_parse(&r_ipv4_addr, OTHER_IPV4_ADDR_STR);
+ tor_addr_parse(&r_ipv6_addr, OTHER_IPV6_ADDR_STR);
+ tor_addr_make_null(&n_ipv4_addr, AF_INET);
+ tor_addr_make_null(&n_ipv6_addr, AF_INET6);
+
+ /* Test the function's address matching with IPv4 and IPv6 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 0;
+
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Preferring IPv4 */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
+ == 0);
+
+ /* Preferring IPv6 */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
+ == 0);
+
+ /* Test the function's address matching with UseBridges on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 1;
+
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Preferring IPv4 */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 0)
+ == 0);
+
+ /* Preferring IPv6 */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 1, 1)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 1, 1)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 1, 1)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 1, 1)
+ == 0);
+
+ /* bridge clients always use IPv6, regardless of ClientUseIPv6 */
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 0;
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Test the function's address matching with IPv4 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 0;
+ mock_options.UseBridges = 0;
+
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Test the function's address matching with IPv6 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 0;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 0;
+
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Test the function's address matching with ClientUseIPv4 0.
+ * This means "use IPv6" regardless of the other settings. */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 0;
+ mock_options.ClientUseIPv6 = 0;
+ mock_options.UseBridges = 0;
+
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&r_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&r_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* Test the function's address matching for unusual inputs */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 1;
+
+ /* NULL and tor_addr_is_null addresses are rejected */
+ tt_assert(fascist_firewall_allows_address(NULL, port, policy, 0, 0) == 0);
+ tt_assert(fascist_firewall_allows_address(&n_ipv4_addr, port, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&n_ipv6_addr, port, policy, 0, 0)
+ == 0);
+
+ /* zero ports are rejected */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, 0, policy, 0, 0)
+ == 0);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, 0, policy, 0, 0)
+ == 0);
+
+ /* NULL and empty policies accept everything */
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, NULL, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, NULL, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv4_addr, port, e_policy, 0, 0)
+ == 1);
+ tt_assert(fascist_firewall_allows_address(&ipv6_addr, port, e_policy, 0, 0)
+ == 1);
+
+ done:
+ addr_policy_free(item);
+ addr_policy_list_free(policy);
+ addr_policy_list_free(e_policy);
+ UNMOCK(get_options);
+}
+
+#undef REJECT_IPv4_FINAL_STR
+#undef REJECT_IPv6_FINAL_STR
+#undef OTHER_IPV4_ADDR_STR
+#undef OTHER_IPV6_ADDR_STR
+
+#define TEST_IPV4_OR_PORT 1234
+#define TEST_IPV4_DIR_PORT 2345
+#define TEST_IPV6_OR_PORT 61234
+#define TEST_IPV6_DIR_PORT 62345
+
+/* Check that fascist_firewall_choose_address_rs() returns the expected
+ * results. */
+#define CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
+ expect_ap) \
+ STMT_BEGIN \
+ tor_addr_port_t chosen_rs_ap; \
+ tor_addr_make_null(&chosen_rs_ap.addr, AF_INET); \
+ chosen_rs_ap.port = 0; \
+ tt_int_op(fascist_firewall_choose_address_rs(&(fake_rs), \
+ (fw_connection), \
+ (pref_only), \
+ &chosen_rs_ap), \
+ OP_EQ, (expect_rv)); \
+ tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_rs_ap.addr)); \
+ tt_int_op((expect_ap).port, OP_EQ, chosen_rs_ap.port); \
+ STMT_END
+
+/* Check that fascist_firewall_choose_address_node() returns the expected
+ * results. */
+#define CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, \
+ expect_rv, expect_ap) \
+ STMT_BEGIN \
+ tor_addr_port_t chosen_node_ap; \
+ tor_addr_make_null(&chosen_node_ap.addr, AF_INET); \
+ chosen_node_ap.port = 0; \
+ tt_int_op(fascist_firewall_choose_address_node(&(fake_node), \
+ (fw_connection), \
+ (pref_only), \
+ &chosen_node_ap), \
+ OP_EQ, (expect_rv)); \
+ tt_assert(tor_addr_eq(&(expect_ap).addr, &chosen_node_ap.addr)); \
+ tt_int_op((expect_ap).port, OP_EQ, chosen_node_ap.port); \
+ STMT_END
+
+/* Check that fascist_firewall_choose_address_rs and
+ * fascist_firewall_choose_address_node() both return the expected results. */
+#define CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, fw_connection, pref_only, \
+ expect_rv, expect_ap) \
+ STMT_BEGIN \
+ CHECK_CHOSEN_ADDR_RS(fake_rs, fw_connection, pref_only, expect_rv, \
+ expect_ap); \
+ CHECK_CHOSEN_ADDR_NODE(fake_node, fw_connection, pref_only, expect_rv, \
+ expect_ap); \
+ STMT_END
+
+/** Run unit tests for fascist_firewall_choose_address */
+static void
+test_policies_fascist_firewall_choose_address(void *arg)
+{
+ (void)arg;
+ tor_addr_port_t ipv4_or_ap, ipv4_dir_ap, ipv6_or_ap, ipv6_dir_ap;
+ tor_addr_port_t n_ipv4_ap, n_ipv6_ap;
+
+ /* Setup the options */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ MOCK(get_options, mock_get_options);
+
+ /* Parse the addresses */
+ tor_addr_parse(&ipv4_or_ap.addr, TEST_IPV4_ADDR_STR);
+ ipv4_or_ap.port = TEST_IPV4_OR_PORT;
+ tor_addr_parse(&ipv4_dir_ap.addr, TEST_IPV4_ADDR_STR);
+ ipv4_dir_ap.port = TEST_IPV4_DIR_PORT;
+
+ tor_addr_parse(&ipv6_or_ap.addr, TEST_IPV6_ADDR_STR);
+ ipv6_or_ap.port = TEST_IPV6_OR_PORT;
+ tor_addr_parse(&ipv6_dir_ap.addr, TEST_IPV6_ADDR_STR);
+ ipv6_dir_ap.port = TEST_IPV6_DIR_PORT;
+
+ tor_addr_make_null(&n_ipv4_ap.addr, AF_INET);
+ n_ipv4_ap.port = 0;
+ tor_addr_make_null(&n_ipv6_ap.addr, AF_INET6);
+ n_ipv6_ap.port = 0;
+
+ /* Sanity check fascist_firewall_choose_address with IPv4 and IPv6 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 0;
+
+ /* Prefer IPv4 */
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
+ FIREWALL_OR_CONNECTION, 0, 0)
+ == &ipv4_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
+ FIREWALL_OR_CONNECTION, 1, 0)
+ == &ipv4_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
+ FIREWALL_DIR_CONNECTION, 0, 0)
+ == &ipv4_dir_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 1,
+ FIREWALL_DIR_CONNECTION, 1, 0)
+ == &ipv4_dir_ap);
+
+ /* Prefer IPv6 */
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
+ FIREWALL_OR_CONNECTION, 0, 1)
+ == &ipv6_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
+ FIREWALL_OR_CONNECTION, 1, 1)
+ == &ipv6_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
+ FIREWALL_DIR_CONNECTION, 0, 1)
+ == &ipv6_dir_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &ipv6_dir_ap, 0,
+ FIREWALL_DIR_CONNECTION, 1, 1)
+ == &ipv6_dir_ap);
+
+ /* Unusual inputs */
+
+ /* null preferred OR addresses */
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &n_ipv6_ap, 0,
+ FIREWALL_OR_CONNECTION, 0, 1)
+ == &ipv4_or_ap);
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_or_ap, 1,
+ FIREWALL_OR_CONNECTION, 0, 0)
+ == &ipv6_or_ap);
+
+ /* null both OR addresses */
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
+ FIREWALL_OR_CONNECTION, 0, 1)
+ == NULL);
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
+ FIREWALL_OR_CONNECTION, 0, 0)
+ == NULL);
+
+ /* null preferred Dir addresses */
+ tt_assert(fascist_firewall_choose_address(&ipv4_dir_ap, &n_ipv6_ap, 0,
+ FIREWALL_DIR_CONNECTION, 0, 1)
+ == &ipv4_dir_ap);
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &ipv6_dir_ap, 1,
+ FIREWALL_DIR_CONNECTION, 0, 0)
+ == &ipv6_dir_ap);
+
+ /* null both Dir addresses */
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 0,
+ FIREWALL_DIR_CONNECTION, 0, 1)
+ == NULL);
+ tt_assert(fascist_firewall_choose_address(&n_ipv4_ap, &n_ipv6_ap, 1,
+ FIREWALL_DIR_CONNECTION, 0, 0)
+ == NULL);
+
+ /* Prefer IPv4 but want IPv6 (contradictory) */
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
+ FIREWALL_OR_CONNECTION, 0, 0)
+ == &ipv4_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 0,
+ FIREWALL_OR_CONNECTION, 1, 0)
+ == &ipv4_or_ap);
+
+ /* Prefer IPv6 but want IPv4 (contradictory) */
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
+ FIREWALL_OR_CONNECTION, 0, 1)
+ == &ipv6_or_ap);
+ tt_assert(fascist_firewall_choose_address(&ipv4_or_ap, &ipv6_or_ap, 1,
+ FIREWALL_OR_CONNECTION, 1, 1)
+ == &ipv6_or_ap);
+
+ /* Make a fake rs. There will be no corresponding node.
+ * This is what happens when there's no consensus and we're bootstrapping
+ * from authorities / fallbacks. */
+ routerstatus_t fake_rs;
+ memset(&fake_rs, 0, sizeof(routerstatus_t));
+ /* In a routerstatus, the OR and Dir addresses are the same */
+ fake_rs.addr = tor_addr_to_ipv4h(&ipv4_or_ap.addr);
+ fake_rs.or_port = ipv4_or_ap.port;
+ fake_rs.dir_port = ipv4_dir_ap.port;
+
+ tor_addr_copy(&fake_rs.ipv6_addr, &ipv6_or_ap.addr);
+ fake_rs.ipv6_orport = ipv6_or_ap.port;
+ /* In a routerstatus, the IPv4 and IPv6 DirPorts are the same.*/
+ ipv6_dir_ap.port = TEST_IPV4_DIR_PORT;
+
+ /* Make a fake node. Even though it contains the fake_rs, a lookup won't
+ * find the node from the rs, because they're not in the hash table. */
+ node_t fake_node;
+ memset(&fake_node, 0, sizeof(node_t));
+ fake_node.rs = &fake_rs;
+
+ /* Choose an address with IPv4 and IPv6 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.UseBridges = 0;
+
+ /* Preferring IPv4 */
+ mock_options.ClientPreferIPv6ORPort = 0;
+ mock_options.ClientPreferIPv6DirPort = 0;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Auto (Preferring IPv4) */
+ mock_options.ClientPreferIPv6ORPort = -1;
+ mock_options.ClientPreferIPv6DirPort = -1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Preferring IPv6 */
+ mock_options.ClientPreferIPv6ORPort = 1;
+ mock_options.ClientPreferIPv6DirPort = 1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv6_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv6_dir_ap);
+
+ /* Preferring IPv4 OR / IPv6 Dir */
+ mock_options.ClientPreferIPv6ORPort = 0;
+ mock_options.ClientPreferIPv6DirPort = 1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv6_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv6_dir_ap);
+
+ /* Preferring IPv6 OR / IPv4 Dir */
+ mock_options.ClientPreferIPv6ORPort = 1;
+ mock_options.ClientPreferIPv6DirPort = 0;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Choose an address with UseBridges on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.UseBridges = 1;
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 1;
+
+ /* Preferring IPv4 */
+ mock_options.ClientPreferIPv6ORPort = 0;
+ mock_options.ClientPreferIPv6DirPort = 0;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Auto:
+ * - bridge clients prefer the configured bridge OR address from the node,
+ * (the configured address family sets node.ipv6_preferred)
+ * - other clients prefer IPv4 OR by default (see above),
+ * - all clients, including bridge clients, prefer IPv4 Dir by default.
+ */
+ mock_options.ClientPreferIPv6ORPort = -1;
+ mock_options.ClientPreferIPv6DirPort = -1;
+
+ /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
+ * configured with an IPv4 address */
+ fake_node.ipv6_preferred = 0;
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
+ * configured with an IPv6 address */
+ fake_node.ipv6_preferred = 1;
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* When a rs has no node, it defaults to IPv4 under auto. */
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
+
+ /* Preferring IPv6 */
+ mock_options.ClientPreferIPv6ORPort = 1;
+ mock_options.ClientPreferIPv6DirPort = 1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv6_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv6_dir_ap);
+
+ /* In the default configuration (Auto / IPv6 off), bridge clients should
+ * use both IPv4 and IPv6, but only prefer IPv6 for bridges configured with
+ * an IPv6 address, regardless of ClientUseIPv6. (See above.) */
+ mock_options.ClientUseIPv6 = 0;
+ mock_options.ClientPreferIPv6ORPort = -1;
+ mock_options.ClientPreferIPv6DirPort = -1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
+ * configured with an IPv4 address */
+ fake_node.ipv6_preferred = 0;
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Simulate the initialisation of fake_node.ipv6_preferred with a bridge
+ * configured with an IPv6 address */
+ fake_node.ipv6_preferred = 1;
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 0, 1, ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_OR_CONNECTION, 1, 1, ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_NODE(fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* When a rs has no node, it defaults to IPv4 under auto. */
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 0, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_OR_CONNECTION, 1, 1, ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 0, 1, ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RS(fake_rs, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap);
+
+ /* Choose an address with IPv4 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 1;
+ mock_options.ClientUseIPv6 = 0;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ /* Choose an address with IPv6 on */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 0;
+ mock_options.ClientUseIPv6 = 1;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv6_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv6_dir_ap);
+
+ /* Choose an address with ClientUseIPv4 0.
+ * This means "use IPv6" regardless of the other settings. */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ClientUseIPv4 = 0;
+ mock_options.ClientUseIPv6 = 0;
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv6_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv6_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv6_dir_ap);
+
+ /* Choose an address with ORPort_set 1 (server mode).
+ * This means "use IPv4" regardless of the other settings. */
+ memset(&mock_options, 0, sizeof(or_options_t));
+ mock_options.ORPort_set = 1;
+ mock_options.ClientUseIPv4 = 0;
+ mock_options.ClientUseIPv6 = 1;
+ mock_options.ClientPreferIPv6ORPort = 1;
+ mock_options.ClientPreferIPv6DirPort = 1;
+
+ /* Simulate the initialisation of fake_node.ipv6_preferred */
+ fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(
+ &mock_options);
+
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1,
+ ipv4_or_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 0, 1,
+ ipv4_dir_ap);
+ CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1,
+ ipv4_dir_ap);
+
+ done:
+ UNMOCK(get_options);
+}
+
+#undef TEST_IPV4_ADDR_STR
+#undef TEST_IPV6_ADDR_STR
+#undef TEST_IPV4_OR_PORT
+#undef TEST_IPV4_DIR_PORT
+#undef TEST_IPV6_OR_PORT
+#undef TEST_IPV6_DIR_PORT
+
+#undef CHECK_CHOSEN_ADDR_RS
+#undef CHECK_CHOSEN_ADDR_NODE
+#undef CHECK_CHOSEN_ADDR_RN
+
struct testcase_t policy_tests[] = {
{ "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
NULL, NULL },
{ "general", test_policies_general, 0, NULL, NULL },
+ { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
+ NULL },
+ { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
+ { "reject_interface_address", test_policies_reject_interface_address, 0,
+ NULL, NULL },
+ { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
+ { "fascist_firewall_allows_address",
+ test_policies_fascist_firewall_allows_address, 0, NULL, NULL },
+ { "fascist_firewall_choose_address",
+ test_policies_fascist_firewall_choose_address, 0, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_procmon.c b/src/test/test_procmon.c
new file mode 100644
index 0000000000..9e63fc006d
--- /dev/null
+++ b/src/test/test_procmon.c
@@ -0,0 +1,58 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define PROCMON_PRIVATE
+#include "orconfig.h"
+#include "or.h"
+#include "test.h"
+
+#include "procmon.h"
+
+#include "log_test_helpers.h"
+
+#define NS_MODULE procmon
+
+struct event_base;
+
+static void
+test_procmon_tor_process_monitor_new(void *ignored)
+{
+ (void)ignored;
+ tor_process_monitor_t *res;
+ const char *msg;
+
+ res = tor_process_monitor_new(NULL, "probably invalid", 0, NULL, NULL, &msg);
+ tt_assert(!res);
+ tt_str_op(msg, OP_EQ, "invalid PID");
+
+ res = tor_process_monitor_new(NULL, "243443535345454", 0, NULL, NULL, &msg);
+ tt_assert(!res);
+ tt_str_op(msg, OP_EQ, "invalid PID");
+
+ res = tor_process_monitor_new(tor_libevent_get_base(), "43", 0,
+ NULL, NULL, &msg);
+ tt_assert(res);
+ tt_assert(!msg);
+ tor_process_monitor_free(res);
+
+ res = tor_process_monitor_new(tor_libevent_get_base(), "44 hello", 0,
+ NULL, NULL, &msg);
+ tt_assert(res);
+ tt_assert(!msg);
+ tor_process_monitor_free(res);
+
+ res = tor_process_monitor_new(tor_libevent_get_base(), "45:hello", 0,
+ NULL, NULL, &msg);
+ tt_assert(res);
+ tt_assert(!msg);
+
+ done:
+ tor_process_monitor_free(res);
+}
+
+struct testcase_t procmon_tests[] = {
+ { "tor_process_monitor_new", test_procmon_tor_process_monitor_new,
+ TT_FORK, NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_protover.c b/src/test/test_protover.c
new file mode 100644
index 0000000000..f00955d1b4
--- /dev/null
+++ b/src/test/test_protover.c
@@ -0,0 +1,195 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define PROTOVER_PRIVATE
+
+#include "orconfig.h"
+#include "test.h"
+
+#include "protover.h"
+
+static void
+test_protover_parse(void *arg)
+{
+ (void) arg;
+ char *re_encoded = NULL;
+
+ const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
+ smartlist_t *elts = parse_protocol_list(orig);
+
+ tt_assert(elts);
+ tt_int_op(smartlist_len(elts), OP_EQ, 4);
+
+ const proto_entry_t *e;
+ const proto_range_t *r;
+ e = smartlist_get(elts, 0);
+ tt_str_op(e->name, OP_EQ, "Foo");
+ tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
+ {
+ r = smartlist_get(e->ranges, 0);
+ tt_int_op(r->low, OP_EQ, 1);
+ tt_int_op(r->high, OP_EQ, 1);
+
+ r = smartlist_get(e->ranges, 1);
+ tt_int_op(r->low, OP_EQ, 3);
+ tt_int_op(r->high, OP_EQ, 3);
+ }
+
+ e = smartlist_get(elts, 1);
+ tt_str_op(e->name, OP_EQ, "Bar");
+ tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
+ {
+ r = smartlist_get(e->ranges, 0);
+ tt_int_op(r->low, OP_EQ, 3);
+ tt_int_op(r->high, OP_EQ, 3);
+ }
+
+ e = smartlist_get(elts, 2);
+ tt_str_op(e->name, OP_EQ, "Baz");
+ tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
+
+ e = smartlist_get(elts, 3);
+ tt_str_op(e->name, OP_EQ, "Quux");
+ tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
+ {
+ r = smartlist_get(e->ranges, 0);
+ tt_int_op(r->low, OP_EQ, 9);
+ tt_int_op(r->high, OP_EQ, 12);
+
+ r = smartlist_get(e->ranges, 1);
+ tt_int_op(r->low, OP_EQ, 14);
+ tt_int_op(r->high, OP_EQ, 14);
+
+ r = smartlist_get(e->ranges, 2);
+ tt_int_op(r->low, OP_EQ, 15);
+ tt_int_op(r->high, OP_EQ, 16);
+
+ r = smartlist_get(e->ranges, 3);
+ tt_int_op(r->low, OP_EQ, 900);
+ tt_int_op(r->high, OP_EQ, 900);
+ }
+
+ re_encoded = encode_protocol_list(elts);
+ tt_assert(re_encoded);
+ tt_str_op(re_encoded, OP_EQ, orig);
+
+ done:
+ if (elts)
+ SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
+ smartlist_free(elts);
+ tor_free(re_encoded);
+}
+
+static void
+test_protover_parse_fail(void *arg)
+{
+ (void)arg;
+ smartlist_t *elts;
+
+ /* random junk */
+ elts = parse_protocol_list("!!3@*");
+ tt_assert(elts == NULL);
+
+ /* Missing equals sign in an entry */
+ elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
+ tt_assert(elts == NULL);
+
+ /* Missing word. */
+ elts = parse_protocol_list("Link=4 =3 Desc=9");
+ tt_assert(elts == NULL);
+
+ /* Broken numbers */
+ elts = parse_protocol_list("Link=fred");
+ tt_assert(elts == NULL);
+ elts = parse_protocol_list("Link=1,fred");
+ tt_assert(elts == NULL);
+ elts = parse_protocol_list("Link=1,fred,3");
+ tt_assert(elts == NULL);
+
+ /* Broken range */
+ elts = parse_protocol_list("Link=1,9-8,3");
+ tt_assert(elts == NULL);
+
+ done:
+ ;
+}
+
+static void
+test_protover_vote(void *arg)
+{
+ (void) arg;
+
+ smartlist_t *lst = smartlist_new();
+ char *result = protover_compute_vote(lst, 1);
+
+ tt_str_op(result, OP_EQ, "");
+ tor_free(result);
+
+ smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
+ result = protover_compute_vote(lst, 1);
+ tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
+ tor_free(result);
+
+ smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
+ result = protover_compute_vote(lst, 1);
+ tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
+ tor_free(result);
+
+ result = protover_compute_vote(lst, 2);
+ tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
+ tor_free(result);
+
+ done:
+ tor_free(result);
+ smartlist_free(lst);
+}
+
+static void
+test_protover_all_supported(void *arg)
+{
+ (void)arg;
+ char *msg = NULL;
+
+ tt_assert(protover_all_supported(NULL, &msg));
+ tt_assert(msg == NULL);
+
+ tt_assert(protover_all_supported("", &msg));
+ tt_assert(msg == NULL);
+
+ // Some things that we do support
+ tt_assert(protover_all_supported("Link=3-4", &msg));
+ tt_assert(msg == NULL);
+ tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
+ tt_assert(msg == NULL);
+
+ // Some things we don't support
+ tt_assert(! protover_all_supported("Wombat=9", &msg));
+ tt_str_op(msg, OP_EQ, "Wombat=9");
+ tor_free(msg);
+ tt_assert(! protover_all_supported("Link=999", &msg));
+ tt_str_op(msg, OP_EQ, "Link=999");
+ tor_free(msg);
+
+ // Mix of things we support and things we don't
+ tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
+ tt_str_op(msg, OP_EQ, "Wombat=9");
+ tor_free(msg);
+ tt_assert(! protover_all_supported("Link=3-999", &msg));
+ tt_str_op(msg, OP_EQ, "Link=3-999");
+ tor_free(msg);
+
+ done:
+ tor_free(msg);
+}
+
+#define PV_TEST(name, flags) \
+ { #name, test_protover_ ##name, (flags), NULL, NULL }
+
+struct testcase_t protover_tests[] = {
+ PV_TEST(parse, 0),
+ PV_TEST(parse_fail, 0),
+ PV_TEST(vote, 0),
+ PV_TEST(all_supported, 0),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_pt.c b/src/test/test_pt.c
index 6c9aefc487..e5cdc5f3cd 100644
--- a/src/test/test_pt.c
+++ b/src/test/test_pt.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -107,12 +107,12 @@ test_pt_parsing(void *arg)
tt_assert(parse_smethod_line(line, mp) == 0);
tt_int_op(1, OP_EQ, smartlist_len(mp->transports));
{
- const transport_t *transport = smartlist_get(mp->transports, 0);
- tt_assert(transport);
- tt_str_op(transport->name, OP_EQ, "trebuchet");
- tt_int_op(transport->port, OP_EQ, 9999);
- tt_str_op(fmt_addr(&transport->addr), OP_EQ, "127.0.0.1");
- tt_str_op(transport->extra_info_args, OP_EQ,
+ const transport_t *transport_ = smartlist_get(mp->transports, 0);
+ tt_assert(transport_);
+ tt_str_op(transport_->name, OP_EQ, "trebuchet");
+ tt_int_op(transport_->port, OP_EQ, 9999);
+ tt_str_op(fmt_addr(&transport_->addr), OP_EQ, "127.0.0.1");
+ tt_str_op(transport_->extra_info_args, OP_EQ,
"counterweight=3,sling=snappy");
}
reset_mp(mp);
diff --git a/src/test/test_pubsub.c b/src/test/test_pubsub.c
new file mode 100644
index 0000000000..547d6c6b32
--- /dev/null
+++ b/src/test/test_pubsub.c
@@ -0,0 +1,85 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file test_pubsub.c
+ * \brief Unit tests for publish-subscribe abstraction.
+ **/
+
+#include "or.h"
+#include "test.h"
+#include "pubsub.h"
+
+DECLARE_PUBSUB_STRUCT_TYPES(foobar)
+DECLARE_PUBSUB_TOPIC(foobar)
+DECLARE_NOTIFY_PUBSUB_TOPIC(static, foobar)
+IMPLEMENT_PUBSUB_TOPIC(static, foobar)
+
+struct foobar_event_data_t {
+ unsigned u;
+ const char *s;
+};
+
+struct foobar_subscriber_data_t {
+ const char *name;
+ long l;
+};
+
+static int
+foobar_sub1(foobar_event_data_t *ev, foobar_subscriber_data_t *mine)
+{
+ ev->u += 10;
+ mine->l += 100;
+ return 0;
+}
+
+static int
+foobar_sub2(foobar_event_data_t *ev, foobar_subscriber_data_t *mine)
+{
+ ev->u += 5;
+ mine->l += 50;
+ return 0;
+}
+
+static void
+test_pubsub_basic(void *arg)
+{
+ (void)arg;
+ foobar_subscriber_data_t subdata1 = { "hi", 0 };
+ foobar_subscriber_data_t subdata2 = { "wow", 0 };
+ const foobar_subscriber_t *sub1;
+ const foobar_subscriber_t *sub2;
+ foobar_event_data_t ed = { 0, "x" };
+ foobar_event_data_t ed2 = { 0, "y" };
+ sub1 = foobar_subscribe(foobar_sub1, &subdata1, SUBSCRIBE_ATSTART, 100);
+ tt_assert(sub1);
+
+ foobar_notify(&ed, 0);
+ tt_int_op(subdata1.l, OP_EQ, 100);
+ tt_int_op(subdata2.l, OP_EQ, 0);
+ tt_int_op(ed.u, OP_EQ, 10);
+
+ sub2 = foobar_subscribe(foobar_sub2, &subdata2, 0, 5);
+ tt_assert(sub2);
+
+ foobar_notify(&ed2, 0);
+ tt_int_op(subdata1.l, OP_EQ, 200);
+ tt_int_op(subdata2.l, OP_EQ, 50);
+ tt_int_op(ed2.u, OP_EQ, 15);
+
+ foobar_unsubscribe(sub1);
+
+ foobar_notify(&ed, 0);
+ tt_int_op(subdata1.l, OP_EQ, 200);
+ tt_int_op(subdata2.l, OP_EQ, 100);
+ tt_int_op(ed.u, OP_EQ, 15);
+
+ done:
+ foobar_clear();
+}
+
+struct testcase_t pubsub_tests[] = {
+ { "pubsub_basic", test_pubsub_basic, TT_FORK, NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_relay.c b/src/test/test_relay.c
index 6081956d46..4713c79ea5 100644
--- a/src/test/test_relay.c
+++ b/src/test/test_relay.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
@@ -74,6 +74,10 @@ test_relay_append_cell_to_circuit_queue(void *arg)
/* Make a fake orcirc */
orcirc = new_fake_orcirc(nchan, pchan);
tt_assert(orcirc);
+ circuitmux_attach_circuit(nchan->cmux, TO_CIRCUIT(orcirc),
+ CELL_DIRECTION_OUT);
+ circuitmux_attach_circuit(pchan->cmux, TO_CIRCUIT(orcirc),
+ CELL_DIRECTION_IN);
/* Make a cell */
cell = tor_malloc_zero(sizeof(cell_t));
@@ -109,8 +113,12 @@ test_relay_append_cell_to_circuit_queue(void *arg)
done:
tor_free(cell);
- cell_queue_clear(&orcirc->base_.n_chan_cells);
- cell_queue_clear(&orcirc->p_chan_cells);
+ if (orcirc) {
+ circuitmux_detach_circuit(nchan->cmux, TO_CIRCUIT(orcirc));
+ circuitmux_detach_circuit(pchan->cmux, TO_CIRCUIT(orcirc));
+ cell_queue_clear(&orcirc->base_.n_chan_cells);
+ cell_queue_clear(&orcirc->p_chan_cells);
+ }
tor_free(orcirc);
free_fake_channel(nchan);
free_fake_channel(pchan);
diff --git a/src/test/test_relaycell.c b/src/test/test_relaycell.c
index 0a6fef729c..fb6748965a 100644
--- a/src/test/test_relaycell.c
+++ b/src/test/test_relaycell.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/* Unit tests for handling different kinds of relay cell */
@@ -95,7 +95,7 @@ test_relaycell_resolved(void *arg)
tt_int_op(srm_ncalls, OP_EQ, 1); \
tt_ptr_op(srm_conn, OP_EQ, entryconn); \
tt_int_op(srm_atype, OP_EQ, (atype)); \
- if (answer) { \
+ if ((answer) != NULL) { \
tt_int_op(srm_alen, OP_EQ, sizeof(answer)-1); \
tt_int_op(srm_alen, OP_LT, 512); \
tt_int_op(srm_answer_is_set, OP_EQ, 1); \
diff --git a/src/test/test_rendcache.c b/src/test/test_rendcache.c
new file mode 100644
index 0000000000..a5d3f351f8
--- /dev/null
+++ b/src/test/test_rendcache.c
@@ -0,0 +1,1273 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "or.h"
+
+#include "test.h"
+#define RENDCACHE_PRIVATE
+#include "rendcache.h"
+#include "router.h"
+#include "routerlist.h"
+#include "config.h"
+#include <openssl/rsa.h>
+#include "rend_test_helpers.h"
+#include "log_test_helpers.h"
+
+#define NS_MODULE rend_cache
+
+static const int RECENT_TIME = -10;
+static const int TIME_IN_THE_PAST = -(REND_CACHE_MAX_AGE + \
+ REND_CACHE_MAX_SKEW + 60);
+static const int TIME_IN_THE_FUTURE = REND_CACHE_MAX_SKEW + 60;
+
+static rend_data_t *
+mock_rend_data(const char *onion_address)
+{
+ rend_data_t *rend_query = tor_malloc_zero(sizeof(rend_data_t));
+
+ strlcpy(rend_query->onion_address, onion_address,
+ sizeof(rend_query->onion_address));
+ rend_query->auth_type = REND_NO_AUTH;
+ rend_query->hsdirs_fp = smartlist_new();
+ smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
+ DIGEST_LEN));
+
+ return rend_query;
+}
+
+static void
+test_rend_cache_lookup_entry(void *data)
+{
+ int ret;
+ rend_data_t *mock_rend_query = NULL;
+ char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+ rend_cache_entry_t *entry = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
+ char *service_id = NULL;
+ (void)data;
+
+ rend_cache_init();
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+
+ ret = rend_cache_lookup_entry("abababababababab", 0, NULL);
+ tt_int_op(ret, OP_EQ, -ENOENT);
+
+ ret = rend_cache_lookup_entry("invalid query", 2, NULL);
+ tt_int_op(ret, OP_EQ, -EINVAL);
+
+ ret = rend_cache_lookup_entry("abababababababab", 2, NULL);
+ tt_int_op(ret, OP_EQ, -ENOENT);
+
+ ret = rend_cache_lookup_entry("abababababababab", 4224, NULL);
+ tt_int_op(ret, OP_EQ, -ENOENT);
+
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
+ mock_rend_query, NULL);
+
+ ret = rend_cache_lookup_entry(service_id, 2, NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ret = rend_cache_lookup_entry(service_id, 2, &entry);
+ tt_assert(entry);
+ tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
+ tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);
+
+ done:
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_cache_free_all();
+ rend_data_free(mock_rend_query);
+}
+
+static void
+test_rend_cache_store_v2_desc_as_client(void *data)
+{
+ int ret;
+ rend_data_t *mock_rend_query;
+ char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+ rend_cache_entry_t *entry = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
+ char *service_id = NULL;
+ char client_cookie[REND_DESC_COOKIE_LEN];
+ (void)data;
+
+ rend_cache_init();
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+
+ // Test success
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ &entry);
+
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(entry);
+ tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
+ tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);
+
+ // Test various failure modes
+
+ // TODO: a too long desc_id_base32 argument crashes the function
+ /* ret = rend_cache_store_v2_desc_as_client( */
+ /* desc_holder->desc_str, */
+ /* "3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG", */
+ /* &mock_rend_query, NULL); */
+ /* tt_int_op(ret, OP_EQ, -1); */
+
+ // Test bad base32 failure
+ // This causes an assertion failure if we're running with assertions.
+ // But when building without asserts, we can test it.
+#ifdef DISABLE_ASSERTS_IN_UNIT_TESTS
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ "!xqunszqnaolrrfmtzgaki7mxelgvkj", mock_rend_query, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+#endif
+
+ // Test invalid descriptor
+ ret = rend_cache_store_v2_desc_as_client("invalid descriptor",
+ "3xqunszqnaolrrfmtzgaki7mxelgvkje", mock_rend_query, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // TODO: it doesn't seem to be possible to test invalid service ID condition.
+ // that means it is likely not possible to have that condition without
+ // earlier conditions failing first (such as signature checking of the desc)
+
+ rend_cache_free_all();
+
+ // Test mismatch between service ID and onion address
+ rend_cache_init();
+ strncpy(mock_rend_query->onion_address, "abc", REND_SERVICE_ID_LEN_BASE32+1);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32,
+ mock_rend_query, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+ rend_cache_free_all();
+ rend_data_free(mock_rend_query);
+
+ // Test incorrect descriptor ID
+ rend_cache_init();
+ mock_rend_query = mock_rend_data(service_id);
+ desc_id_base32[0]++;
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, -1);
+ desc_id_base32[0]--;
+ rend_cache_free_all();
+
+ // Test too old descriptor
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32,
+ mock_rend_query, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+ rend_cache_free_all();
+
+ // Test too new descriptor (in the future)
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, -1);
+ rend_cache_free_all();
+
+ // Test when a descriptor is already in the cache
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+
+ rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
+ mock_rend_query, NULL);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ &entry);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(entry);
+ rend_cache_free_all();
+
+ // Test unsuccessful decrypting of introduction points
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+ mock_rend_query = mock_rend_data(service_id);
+ mock_rend_query->auth_type = REND_BASIC_AUTH;
+ client_cookie[0] = 'A';
+ memcpy(mock_rend_query->descriptor_cookie, client_cookie,
+ REND_DESC_COOKIE_LEN);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+ rend_cache_free_all();
+
+ // Test successful run when we have REND_BASIC_AUTH but not cookie
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+ mock_rend_query = mock_rend_data(service_id);
+ mock_rend_query->auth_type = REND_BASIC_AUTH;
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ rend_cache_free_all();
+
+ // Test when we have no introduction points
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 0);
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, -1);
+ rend_cache_free_all();
+
+ // Test when we have too many intro points
+ rend_cache_init();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_data_free(mock_rend_query);
+
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, MAX_INTRO_POINTS+1);
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ done:
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_cache_free_all();
+ rend_data_free(mock_rend_query);
+}
+
+static void
+test_rend_cache_store_v2_desc_as_client_with_different_time(void *data)
+{
+ int ret;
+ rend_data_t *mock_rend_query;
+ char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+ rend_service_descriptor_t *generated = NULL;
+ smartlist_t *descs = smartlist_new();
+ time_t t;
+ char *service_id = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder_newer;
+ rend_encoded_v2_service_descriptor_t *desc_holder_older;
+
+ t = time(NULL);
+ rend_cache_init();
+
+ create_descriptor(&generated, &service_id, 3);
+
+ generated->timestamp = t + RECENT_TIME;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ descs = smartlist_new();
+
+ generated->timestamp = (t + RECENT_TIME) - 20;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+ (void)data;
+
+ // Test when a descriptor is already in the cache and it is newer than the
+ // one we submit
+ mock_rend_query = mock_rend_data(service_id);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32),
+ desc_holder_newer->desc_id, DIGEST_LEN);
+ rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
+ desc_id_base32, mock_rend_query, NULL);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ rend_cache_free_all();
+
+ // Test when an old descriptor is in the cache and we submit a newer one
+ rend_cache_init();
+ rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
+ desc_id_base32, mock_rend_query, NULL);
+ ret = rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
+ desc_id_base32, mock_rend_query,
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ rend_encoded_v2_service_descriptor_free(desc_holder_newer);
+ rend_encoded_v2_service_descriptor_free(desc_holder_older);
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ rend_service_descriptor_free(generated);
+ tor_free(service_id);
+ rend_cache_free_all();
+ rend_data_free(mock_rend_query);
+}
+
+#define NS_SUBMODULE lookup_v2_desc_as_dir
+NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
+
+static routerinfo_t *mock_routerinfo;
+
+static const routerinfo_t *
+NS(router_get_my_routerinfo)(void)
+{
+ if (!mock_routerinfo) {
+ mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
+ }
+
+ return mock_routerinfo;
+}
+
+static void
+test_rend_cache_lookup_v2_desc_as_dir(void *data)
+{
+ int ret;
+ char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+ rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
+ char *service_id = NULL;
+ const char *ret_desc = NULL;
+
+ (void)data;
+
+ NS_MOCK(router_get_my_routerinfo);
+
+ rend_cache_init();
+
+ // Test invalid base32
+ ret = rend_cache_lookup_v2_desc_as_dir("!bababababababab", NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test non-existent descriptor but well formed
+ ret = rend_cache_lookup_v2_desc_as_dir("3xqunszqnaolrrfmtzgaki7mxelgvkje",
+ NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test existing descriptor
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+ rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
+ base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
+ DIGEST_LEN);
+ ret = rend_cache_lookup_v2_desc_as_dir(desc_id_base32, &ret_desc);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_assert(ret_desc);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ tor_free(mock_routerinfo);
+ rend_cache_free_all();
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+}
+
+#undef NS_SUBMODULE
+
+#define NS_SUBMODULE store_v2_desc_as_dir
+NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
+
+static const routerinfo_t *
+NS(router_get_my_routerinfo)(void)
+{
+ return mock_routerinfo;
+}
+
+static void
+test_rend_cache_store_v2_desc_as_dir(void *data)
+{
+ (void)data;
+ int ret;
+ rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
+ char *service_id = NULL;
+
+ NS_MOCK(router_get_my_routerinfo);
+
+ rend_cache_init();
+
+ // Test when we can't parse the descriptor
+ mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
+ ret = rend_cache_store_v2_desc_as_dir("unparseable");
+ tt_int_op(ret, OP_EQ, -1);
+
+ // Test when we have an old descriptor
+ generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+
+ // Test when we have a descriptor in the future
+ generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+
+ // Test when two descriptors
+ generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+
+ // Test when asking for hidden service statistics HiddenServiceStatistics
+ rend_cache_purge();
+ generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
+ get_options_mutable()->HiddenServiceStatistics = 1;
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ rend_encoded_v2_service_descriptor_free(desc_holder);
+ tor_free(service_id);
+ rend_cache_free_all();
+ tor_free(mock_routerinfo);
+}
+
+static void
+test_rend_cache_store_v2_desc_as_dir_with_different_time(void *data)
+{
+ (void)data;
+
+ int ret;
+ rend_service_descriptor_t *generated = NULL;
+ smartlist_t *descs = smartlist_new();
+ time_t t;
+ char *service_id = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder_newer;
+ rend_encoded_v2_service_descriptor_t *desc_holder_older;
+
+ NS_MOCK(router_get_my_routerinfo);
+
+ rend_cache_init();
+
+ t = time(NULL);
+
+ create_descriptor(&generated, &service_id, 3);
+ generated->timestamp = t + RECENT_TIME;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ descs = smartlist_new();
+
+ generated->timestamp = (t + RECENT_TIME) - 20;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+
+ // Test when we have a newer descriptor stored
+ mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
+ rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test when we have an old descriptor stored
+ rend_cache_purge();
+ rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ rend_cache_free_all();
+ rend_service_descriptor_free(generated);
+ tor_free(service_id);
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ rend_encoded_v2_service_descriptor_free(desc_holder_newer);
+ rend_encoded_v2_service_descriptor_free(desc_holder_older);
+ tor_free(mock_routerinfo);
+}
+
+static void
+test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data)
+{
+ (void)data;
+
+ int ret;
+ rend_service_descriptor_t *generated = NULL;
+ smartlist_t *descs = smartlist_new();
+ time_t t;
+ char *service_id = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder_one = NULL;
+ rend_encoded_v2_service_descriptor_t *desc_holder_two = NULL;
+
+ NS_MOCK(router_get_my_routerinfo);
+
+ rend_cache_init();
+
+ t = time(NULL);
+
+ create_descriptor(&generated, &service_id, 3);
+ generated->timestamp = t + RECENT_TIME;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_one = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ descs = smartlist_new();
+
+ generated->timestamp = t + RECENT_TIME;
+ generated->protocols = 41;
+ rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
+ REND_NO_AUTH, NULL, NULL);
+ desc_holder_two = ((rend_encoded_v2_service_descriptor_t *)
+ smartlist_get(descs, 0));
+ smartlist_set(descs, 0, NULL);
+
+ // Test when we have another descriptor stored, with a different descriptor
+ mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
+ rend_cache_store_v2_desc_as_dir(desc_holder_one->desc_str);
+ ret = rend_cache_store_v2_desc_as_dir(desc_holder_two->desc_str);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ NS_UNMOCK(router_get_my_routerinfo);
+ rend_cache_free_all();
+ rend_service_descriptor_free(generated);
+ tor_free(service_id);
+ SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
+ rend_encoded_v2_service_descriptor_free(d));
+ smartlist_free(descs);
+ rend_encoded_v2_service_descriptor_free(desc_holder_one);
+ rend_encoded_v2_service_descriptor_free(desc_holder_two);
+}
+
+#undef NS_SUBMODULE
+
+static void
+test_rend_cache_init(void *data)
+{
+ (void)data;
+
+ tt_assert_msg(!rend_cache, "rend_cache should be NULL when starting");
+ tt_assert_msg(!rend_cache_v2_dir, "rend_cache_v2_dir should be NULL "
+ "when starting");
+ tt_assert_msg(!rend_cache_failure, "rend_cache_failure should be NULL when "
+ "starting");
+
+ rend_cache_init();
+
+ tt_assert_msg(rend_cache, "rend_cache should not be NULL after initing");
+ tt_assert_msg(rend_cache_v2_dir, "rend_cache_v2_dir should not be NULL "
+ "after initing");
+ tt_assert_msg(rend_cache_failure, "rend_cache_failure should not be NULL "
+ "after initing");
+
+ tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_decrement_allocation(void *data)
+{
+ (void)data;
+
+ // Test when the cache has enough allocations
+ rend_cache_total_allocation = 10;
+ rend_cache_decrement_allocation(3);
+ tt_int_op(rend_cache_total_allocation, OP_EQ, 7);
+
+ // Test when there are not enough allocations
+ rend_cache_total_allocation = 1;
+ setup_full_capture_of_logs(LOG_WARN);
+ rend_cache_decrement_allocation(2);
+ tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
+ expect_single_log_msg_containing(
+ "Underflow in rend_cache_decrement_allocation");
+ teardown_capture_of_logs();
+
+ // And again
+ rend_cache_decrement_allocation(2);
+ tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
+
+ done:
+ teardown_capture_of_logs();
+}
+
+static void
+test_rend_cache_increment_allocation(void *data)
+{
+ (void)data;
+
+ // Test when the cache is not overflowing
+ rend_cache_total_allocation = 5;
+ rend_cache_increment_allocation(3);
+ tt_int_op(rend_cache_total_allocation, OP_EQ, 8);
+
+ // Test when there are too many allocations
+ rend_cache_total_allocation = SIZE_MAX-1;
+ setup_full_capture_of_logs(LOG_WARN);
+ rend_cache_increment_allocation(2);
+ tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
+ expect_single_log_msg_containing(
+ "Overflow in rend_cache_increment_allocation");
+ teardown_capture_of_logs();
+
+ // And again
+ rend_cache_increment_allocation(2);
+ tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
+
+ done:
+ teardown_capture_of_logs();
+}
+
+static void
+test_rend_cache_failure_intro_entry_new(void *data)
+{
+ time_t now;
+ rend_cache_failure_intro_t *entry;
+ rend_intro_point_failure_t failure;
+
+ (void)data;
+
+ failure = INTRO_POINT_FAILURE_TIMEOUT;
+ now = time(NULL);
+ entry = rend_cache_failure_intro_entry_new(failure);
+
+ tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
+ tt_int_op(entry->created_ts, OP_GE, now-5);
+ tt_int_op(entry->created_ts, OP_LE, now+5);
+
+ done:
+ tor_free(entry);
+}
+
+static void
+test_rend_cache_failure_intro_lookup(void *data)
+{
+ (void)data;
+ int ret;
+ rend_cache_failure_t *failure;
+ rend_cache_failure_intro_t *ip;
+ rend_cache_failure_intro_t *entry;
+ const char key_ip_one[DIGEST_LEN] = "ip1";
+ const char key_ip_two[DIGEST_LEN] = "ip2";
+ const char key_foo[DIGEST_LEN] = "foo1";
+
+ rend_cache_init();
+
+ failure = rend_cache_failure_entry_new();
+ ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ digestmap_set(failure->intro_failures, key_ip_one, ip);
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+
+ // Test not found
+ ret = cache_failure_intro_lookup((const uint8_t *) key_foo, "foo2", NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test found with no intro failures in it
+ ret = cache_failure_intro_lookup((const uint8_t *) key_ip_two, "foo1", NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Test found
+ ret = cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", NULL);
+ tt_int_op(ret, OP_EQ, 1);
+
+ // Test found and asking for entry
+ cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", &entry);
+ tt_assert(entry);
+ tt_assert(entry == ip);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_clean(void *data)
+{
+ rend_cache_entry_t *one, *two;
+ rend_service_descriptor_t *desc_one, *desc_two;
+ strmap_iter_t *iter = NULL;
+ const char *key;
+ void *val;
+
+ (void)data;
+
+ rend_cache_init();
+
+ // Test with empty rendcache
+ rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
+ tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
+
+ // Test with two old entries
+ one = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ two = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ one->parsed = desc_one;
+ two->parsed = desc_two;
+
+ desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
+ desc_two->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
+ desc_one->pk = pk_generate(0);
+ desc_two->pk = pk_generate(1);
+
+ strmap_set_lc(rend_cache, "foo1", one);
+ strmap_set_lc(rend_cache, "foo2", two);
+
+ rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
+ tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
+
+ // Test with one old entry and one newer entry
+ one = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ two = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ one->parsed = desc_one;
+ two->parsed = desc_two;
+
+ desc_one->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
+ desc_two->timestamp = time(NULL) - 100;
+ desc_one->pk = pk_generate(0);
+ desc_two->pk = pk_generate(1);
+
+ strmap_set_lc(rend_cache, "foo1", one);
+ strmap_set_lc(rend_cache, "foo2", two);
+
+ rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
+ tt_int_op(strmap_size(rend_cache), OP_EQ, 1);
+
+ iter = strmap_iter_init(rend_cache);
+ strmap_iter_get(iter, &key, &val);
+ tt_str_op(key, OP_EQ, "foo2");
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_failure_entry_new(void *data)
+{
+ rend_cache_failure_t *failure;
+
+ (void)data;
+
+ failure = rend_cache_failure_entry_new();
+ tt_assert(failure);
+ tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 0);
+
+ done:
+ rend_cache_failure_entry_free(failure);
+}
+
+static void
+test_rend_cache_failure_entry_free(void *data)
+{
+ (void)data;
+
+ // Test that it can deal with a NULL argument
+ rend_cache_failure_entry_free(NULL);
+
+ /* done: */
+ /* (void)0; */
+}
+
+static void
+test_rend_cache_failure_clean(void *data)
+{
+ rend_cache_failure_t *failure;
+ rend_cache_failure_intro_t *ip_one, *ip_two;
+
+ const char key_one[DIGEST_LEN] = "ip1";
+ const char key_two[DIGEST_LEN] = "ip2";
+
+ (void)data;
+
+ rend_cache_init();
+
+ // Test with empty failure cache
+ rend_cache_failure_clean(time(NULL));
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
+
+ // Test with one empty failure entry
+ failure = rend_cache_failure_entry_new();
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+ rend_cache_failure_clean(time(NULL));
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
+
+ // Test with one new intro point
+ failure = rend_cache_failure_entry_new();
+ ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ digestmap_set(failure->intro_failures, key_one, ip_one);
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+ rend_cache_failure_clean(time(NULL));
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
+
+ // Test with one old intro point
+ rend_cache_failure_purge();
+ failure = rend_cache_failure_entry_new();
+ ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ ip_one->created_ts = time(NULL) - 7*60;
+ digestmap_set(failure->intro_failures, key_one, ip_one);
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+ rend_cache_failure_clean(time(NULL));
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
+
+ // Test with one old intro point and one new one
+ rend_cache_failure_purge();
+ failure = rend_cache_failure_entry_new();
+ ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ ip_one->created_ts = time(NULL) - 7*60;
+ digestmap_set(failure->intro_failures, key_one, ip_one);
+ ip_two = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ ip_two->created_ts = time(NULL) - 2*60;
+ digestmap_set(failure->intro_failures, key_two, ip_two);
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+ rend_cache_failure_clean(time(NULL));
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
+ tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 1);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_failure_remove(void *data)
+{
+ rend_service_descriptor_t *desc;
+ (void)data;
+
+ rend_cache_init();
+
+ // Test that it deals well with a NULL desc
+ rend_cache_failure_remove(NULL);
+
+ // Test a descriptor that isn't in the cache
+ desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc->pk = pk_generate(0);
+ rend_cache_failure_remove(desc);
+
+ // There seems to not exist any way of getting rend_cache_failure_remove()
+ // to fail because of a problem with rend_get_service_id from here
+ rend_cache_free_all();
+
+ rend_service_descriptor_free(desc);
+ /* done: */
+ /* (void)0; */
+}
+
+static void
+test_rend_cache_free_all(void *data)
+{
+ rend_cache_failure_t *failure;
+ rend_cache_entry_t *one;
+ rend_service_descriptor_t *desc_one;
+
+ (void)data;
+
+ rend_cache_init();
+
+ failure = rend_cache_failure_entry_new();
+ strmap_set_lc(rend_cache_failure, "foo1", failure);
+
+ one = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ one->parsed = desc_one;
+ desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
+ desc_one->pk = pk_generate(0);
+ strmap_set_lc(rend_cache, "foo1", one);
+
+ rend_cache_free_all();
+
+ tt_assert(!rend_cache);
+ tt_assert(!rend_cache_v2_dir);
+ tt_assert(!rend_cache_failure);
+ tt_assert(!rend_cache_total_allocation);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_entry_free(void *data)
+{
+ (void)data;
+ rend_cache_entry_t *e;
+
+ // Handles NULL correctly
+ rend_cache_entry_free(NULL);
+
+ // Handles NULL descriptor correctly
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ rend_cache_entry_free(e);
+
+ // Handles non-NULL descriptor correctly
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ e->desc = tor_malloc(10);
+ rend_cache_entry_free(e);
+
+ /* done: */
+ /* (void)0; */
+}
+
+static void
+test_rend_cache_purge(void *data)
+{
+ (void)data;
+
+ // Deals with a NULL rend_cache
+ rend_cache_purge();
+ tt_assert(rend_cache);
+ tt_assert(strmap_size(rend_cache) == 0);
+
+ // Deals with existing rend_cache
+ rend_cache_free_all();
+ rend_cache_init();
+ tt_assert(rend_cache);
+ tt_assert(strmap_size(rend_cache) == 0);
+
+ rend_cache_purge();
+ tt_assert(rend_cache);
+ tt_assert(strmap_size(rend_cache) == 0);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_failure_intro_add(void *data)
+{
+ (void)data;
+ rend_cache_failure_t *fail_entry;
+ rend_cache_failure_intro_t *entry;
+ const char identity[DIGEST_LEN] = "foo1";
+
+ rend_cache_init();
+
+ // Adds non-existing entry
+ cache_failure_intro_add((const uint8_t *) identity, "foo2",
+ INTRO_POINT_FAILURE_TIMEOUT);
+ fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
+ tt_assert(fail_entry);
+ tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
+ entry = digestmap_get(fail_entry->intro_failures, identity);
+ tt_assert(entry);
+
+ // Adds existing entry
+ cache_failure_intro_add((const uint8_t *) identity, "foo2",
+ INTRO_POINT_FAILURE_TIMEOUT);
+ fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
+ tt_assert(fail_entry);
+ tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
+ entry = digestmap_get(fail_entry->intro_failures, identity);
+ tt_assert(entry);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_intro_failure_note(void *data)
+{
+ (void)data;
+ rend_cache_failure_t *fail_entry;
+ rend_cache_failure_intro_t *entry;
+ const char key[DIGEST_LEN] = "foo1";
+
+ rend_cache_init();
+
+ // Test not found
+ rend_cache_intro_failure_note(INTRO_POINT_FAILURE_TIMEOUT,
+ (const uint8_t *) key, "foo2");
+ fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
+ tt_assert(fail_entry);
+ tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
+ entry = digestmap_get(fail_entry->intro_failures, key);
+ tt_assert(entry);
+ tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
+
+ // Test found
+ rend_cache_intro_failure_note(INTRO_POINT_FAILURE_UNREACHABLE,
+ (const uint8_t *) key, "foo2");
+ tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_UNREACHABLE);
+
+ done:
+ rend_cache_free_all();
+}
+
+#define NS_SUBMODULE clean_v2_descs_as_dir
+
+static void
+test_rend_cache_clean_v2_descs_as_dir(void *data)
+{
+ rend_cache_entry_t *e;
+ time_t now;
+ rend_service_descriptor_t *desc;
+ now = time(NULL);
+ const char key[DIGEST_LEN] = "abcde";
+
+ (void)data;
+
+ rend_cache_init();
+
+ // Test running with an empty cache
+ rend_cache_clean_v2_descs_as_dir(now, 0);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
+
+ // Test with only one new entry
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ e->last_served = now;
+ desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc->timestamp = now;
+ desc->pk = pk_generate(0);
+ e->parsed = desc;
+ digestmap_set(rend_cache_v2_dir, key, e);
+
+ rend_cache_clean_v2_descs_as_dir(now, 0);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 1);
+
+ // Test with one old entry
+ desc->timestamp = now - (REND_CACHE_MAX_AGE + REND_CACHE_MAX_SKEW + 1000);
+ rend_cache_clean_v2_descs_as_dir(now, 0);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
+
+ // Test with one entry that has an old last served
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ e->last_served = now - (REND_CACHE_MAX_AGE + REND_CACHE_MAX_SKEW + 1000);
+ desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc->timestamp = now;
+ desc->pk = pk_generate(0);
+ e->parsed = desc;
+ digestmap_set(rend_cache_v2_dir, key, e);
+
+ rend_cache_clean_v2_descs_as_dir(now, 0);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
+
+ // Test a run through asking for a large force_remove
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ e->last_served = now;
+ desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
+ desc->timestamp = now;
+ desc->pk = pk_generate(0);
+ e->parsed = desc;
+ digestmap_set(rend_cache_v2_dir, key, e);
+
+ rend_cache_clean_v2_descs_as_dir(now, 20000);
+ tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 1);
+
+ done:
+ rend_cache_free_all();
+}
+
+#undef NS_SUBMODULE
+
+static void
+test_rend_cache_entry_allocation(void *data)
+{
+ (void)data;
+
+ size_t ret;
+ rend_cache_entry_t *e = NULL;
+
+ // Handles a null argument
+ ret = rend_cache_entry_allocation(NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ // Handles a non-null argument
+ e = tor_malloc_zero(sizeof(rend_cache_entry_t));
+ ret = rend_cache_entry_allocation(e);
+ tt_int_op(ret, OP_GT, sizeof(rend_cache_entry_t));
+
+ done:
+ tor_free(e);
+}
+
+static void
+test_rend_cache_failure_intro_entry_free(void *data)
+{
+ (void)data;
+ rend_cache_failure_intro_t *entry;
+
+ // Handles a null argument
+ rend_cache_failure_intro_entry_free(NULL);
+
+ // Handles a non-null argument
+ entry = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ rend_cache_failure_intro_entry_free(entry);
+}
+
+static void
+test_rend_cache_failure_purge(void *data)
+{
+ (void)data;
+
+ // Handles a null failure cache
+ strmap_free(rend_cache_failure, rend_cache_failure_entry_free_);
+ rend_cache_failure = NULL;
+
+ rend_cache_failure_purge();
+
+ tt_ptr_op(rend_cache_failure, OP_NE, NULL);
+ tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
+
+ done:
+ rend_cache_free_all();
+}
+
+static void
+test_rend_cache_validate_intro_point_failure(void *data)
+{
+ (void)data;
+ rend_service_descriptor_t *desc = NULL;
+ char *service_id = NULL;
+ rend_intro_point_t *intro = NULL;
+ const char *identity = NULL;
+ rend_cache_failure_t *failure;
+ rend_cache_failure_intro_t *ip;
+
+ rend_cache_init();
+
+ create_descriptor(&desc, &service_id, 3);
+ desc->timestamp = time(NULL) + RECENT_TIME;
+
+ intro = (rend_intro_point_t *)smartlist_get(desc->intro_nodes, 0);
+ identity = intro->extend_info->identity_digest;
+
+ failure = rend_cache_failure_entry_new();
+ ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
+ digestmap_set(failure->intro_failures, identity, ip);
+ strmap_set_lc(rend_cache_failure, service_id, failure);
+
+ // Test when we have an intro point in our cache
+ validate_intro_point_failure(desc, service_id);
+ tt_int_op(smartlist_len(desc->intro_nodes), OP_EQ, 2);
+
+ done:
+ rend_cache_free_all();
+ rend_service_descriptor_free(desc);
+ tor_free(service_id);
+}
+
+struct testcase_t rend_cache_tests[] = {
+ { "init", test_rend_cache_init, 0, NULL, NULL },
+ { "decrement_allocation", test_rend_cache_decrement_allocation, 0,
+ NULL, NULL },
+ { "increment_allocation", test_rend_cache_increment_allocation, 0,
+ NULL, NULL },
+ { "clean", test_rend_cache_clean, TT_FORK, NULL, NULL },
+ { "clean_v2_descs_as_dir", test_rend_cache_clean_v2_descs_as_dir, 0,
+ NULL, NULL },
+ { "entry_allocation", test_rend_cache_entry_allocation, 0, NULL, NULL },
+ { "entry_free", test_rend_cache_entry_free, 0, NULL, NULL },
+ { "failure_intro_entry_free", test_rend_cache_failure_intro_entry_free, 0,
+ NULL, NULL },
+ { "free_all", test_rend_cache_free_all, 0, NULL, NULL },
+ { "purge", test_rend_cache_purge, 0, NULL, NULL },
+ { "failure_clean", test_rend_cache_failure_clean, 0, NULL, NULL },
+ { "failure_entry_new", test_rend_cache_failure_entry_new, 0, NULL, NULL },
+ { "failure_entry_free", test_rend_cache_failure_entry_free, 0, NULL, NULL },
+ { "failure_intro_add", test_rend_cache_failure_intro_add, 0, NULL, NULL },
+ { "failure_intro_entry_new", test_rend_cache_failure_intro_entry_new, 0,
+ NULL, NULL },
+ { "failure_intro_lookup", test_rend_cache_failure_intro_lookup, 0,
+ NULL, NULL },
+ { "failure_purge", test_rend_cache_failure_purge, 0, NULL, NULL },
+ { "failure_remove", test_rend_cache_failure_remove, 0, NULL, NULL },
+ { "intro_failure_note", test_rend_cache_intro_failure_note, 0, NULL, NULL },
+ { "lookup", test_rend_cache_lookup_entry, 0, NULL, NULL },
+ { "lookup_v2_desc_as_dir", test_rend_cache_lookup_v2_desc_as_dir, 0,
+ NULL, NULL },
+ { "store_v2_desc_as_client", test_rend_cache_store_v2_desc_as_client, 0,
+ NULL, NULL },
+ { "store_v2_desc_as_client_with_different_time",
+ test_rend_cache_store_v2_desc_as_client_with_different_time, 0,
+ NULL, NULL },
+ { "store_v2_desc_as_dir", test_rend_cache_store_v2_desc_as_dir, 0,
+ NULL, NULL },
+ { "store_v2_desc_as_dir_with_different_time",
+ test_rend_cache_store_v2_desc_as_dir_with_different_time, 0, NULL, NULL },
+ { "store_v2_desc_as_dir_with_different_content",
+ test_rend_cache_store_v2_desc_as_dir_with_different_content, 0,
+ NULL, NULL },
+ { "validate_intro_point_failure",
+ test_rend_cache_validate_intro_point_failure, 0, NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_replay.c b/src/test/test_replay.c
index a02c160365..e882bc6164 100644
--- a/src/test/test_replay.c
+++ b/src/test/test_replay.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015, The Tor Project, Inc. */
+/* Copyright (c) 2012-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define REPLAYCACHE_PRIVATE
@@ -17,6 +17,20 @@ static const char *test_buffer =
" occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
" mollit anim id est laborum.";
+static const char *test_buffer_2 =
+ "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis"
+ " praesentium voluptatum deleniti atque corrupti quos dolores et quas"
+ " molestias excepturi sint occaecati cupiditate non provident, similique"
+ " sunt in culpa qui officia deserunt mollitia animi, id est laborum et"
+ " dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio."
+ " Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil"
+ " impedit quo minus id quod maxime placeat facere possimus, omnis voluptas"
+ " assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut"
+ " officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates"
+ " repudiandae sint et molestiae non recusandae. Itaque earum rerum hic"
+ " tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias"
+ " consequatur aut perferendis doloribus asperiores repellat.";
+
static void
test_replaycache_alloc(void *arg)
{
@@ -83,6 +97,12 @@ test_replaycache_miss(void *arg)
strlen(test_buffer), NULL);
tt_int_op(result,OP_EQ, 0);
+ /* make sure a different buffer misses as well */
+ result =
+ replaycache_add_and_test_internal(1200, NULL, test_buffer_2,
+ strlen(test_buffer_2), NULL);
+ tt_int_op(result,OP_EQ, 0);
+
/* poke the bad-parameter error case too */
result =
replaycache_add_and_test_internal(1200, NULL, test_buffer,
@@ -115,6 +135,18 @@ test_replaycache_hit(void *arg)
strlen(test_buffer), NULL);
tt_int_op(result,OP_EQ, 1);
+ /* make sure a different buffer misses then hits as well */
+
+ result =
+ replaycache_add_and_test_internal(1200, r, test_buffer_2,
+ strlen(test_buffer_2), NULL);
+ tt_int_op(result,OP_EQ, 0);
+
+ result =
+ replaycache_add_and_test_internal(1300, r, test_buffer_2,
+ strlen(test_buffer_2), NULL);
+ tt_int_op(result,OP_EQ, 1);
+
done:
if (r) replaycache_free(r);
@@ -245,7 +277,7 @@ test_replaycache_scrub(void *arg)
/* Make sure we hit the aging-out case too */
replaycache_scrub_if_needed_internal(1500, r);
/* Assert that we aged it */
- tt_int_op(digestmap_size(r->digests_seen),OP_EQ, 0);
+ tt_int_op(digest256map_size(r->digests_seen),OP_EQ, 0);
done:
if (r) replaycache_free(r);
diff --git a/src/test/test_routerkeys.c b/src/test/test_routerkeys.c
index a60cba746e..24b0da1c46 100644
--- a/src/test/test_routerkeys.c
+++ b/src/test/test_routerkeys.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index 381a592c5b..088bd257c3 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -1,22 +1,54 @@
-/* Copyright (c) 2014, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
+#include "orconfig.h"
+#include <math.h>
+#include <time.h>
+
+#define DIRVOTE_PRIVATE
+#define NETWORKSTATUS_PRIVATE
#define ROUTERLIST_PRIVATE
+#define TOR_UNIT_TESTING
#include "or.h"
-#include "routerlist.h"
+#include "config.h"
+#include "connection.h"
+#include "container.h"
#include "directory.h"
+#include "dirvote.h"
+#include "microdesc.h"
+#include "networkstatus.h"
+#include "nodelist.h"
+#include "policies.h"
+#include "router.h"
+#include "routerlist.h"
+#include "routerparse.h"
+#include "shared_random.h"
#include "test.h"
+#include "test_dir_common.h"
+
+void construct_consensus(char **consensus_text_md);
+
+static authority_cert_t *mock_cert;
+
+static authority_cert_t *
+get_my_v3_authority_cert_m(void)
+{
+ tor_assert(mock_cert);
+ return mock_cert;
+}
/* 4 digests + 3 sep + pre + post + NULL */
static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
static void
mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
- const char *resource, int pds_flags)
+ const char *resource, int pds_flags,
+ download_want_authority_t want_authority)
{
(void)dir_purpose;
(void)router_purpose;
(void)pds_flags;
+ (void)want_authority;
tt_assert(resource);
strlcpy(output, resource, sizeof(output));
done:
@@ -92,12 +124,398 @@ test_routerlist_launch_descriptor_downloads(void *arg)
smartlist_free(downloadable);
}
+void
+construct_consensus(char **consensus_text_md)
+{
+ networkstatus_t *vote = NULL;
+ networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
+ networkstatus_voter_info_t *voter = NULL;
+ authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
+ crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
+ crypto_pk_t *sign_skey_leg=NULL;
+ time_t now = time(NULL);
+ smartlist_t *votes = NULL;
+ int n_vrs;
+
+ tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
+ &sign_skey_1, &sign_skey_2,
+ &sign_skey_3));
+ sign_skey_leg = pk_generate(4);
+
+ dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
+ &dir_common_gen_routerstatus_for_v3ns,
+ &v1, &n_vrs, now, 1);
+ networkstatus_vote_free(vote);
+ tt_assert(v1);
+ tt_int_op(n_vrs, ==, 4);
+ tt_int_op(smartlist_len(v1->routerstatus_list), ==, 4);
+
+ dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
+ &dir_common_gen_routerstatus_for_v3ns,
+ &v2, &n_vrs, now, 1);
+ networkstatus_vote_free(vote);
+ tt_assert(v2);
+ tt_int_op(n_vrs, ==, 4);
+ tt_int_op(smartlist_len(v2->routerstatus_list), ==, 4);
+
+ dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
+ &dir_common_gen_routerstatus_for_v3ns,
+ &v3, &n_vrs, now, 1);
+
+ tt_assert(v3);
+ tt_int_op(n_vrs, ==, 4);
+ tt_int_op(smartlist_len(v3->routerstatus_list), ==, 4);
+ networkstatus_vote_free(vote);
+ votes = smartlist_new();
+ smartlist_add(votes, v1);
+ smartlist_add(votes, v2);
+ smartlist_add(votes, v3);
+
+ *consensus_text_md = networkstatus_compute_consensus(votes, 3,
+ cert1->identity_key,
+ sign_skey_1,
+ "AAAAAAAAAAAAAAAAAAAA",
+ sign_skey_leg,
+ FLAV_MICRODESC);
+
+ tt_assert(*consensus_text_md);
+
+ done:
+ tor_free(voter);
+ networkstatus_vote_free(v1);
+ networkstatus_vote_free(v2);
+ networkstatus_vote_free(v3);
+ smartlist_free(votes);
+ authority_cert_free(cert1);
+ authority_cert_free(cert2);
+ authority_cert_free(cert3);
+ crypto_pk_free(sign_skey_1);
+ crypto_pk_free(sign_skey_2);
+ crypto_pk_free(sign_skey_3);
+ crypto_pk_free(sign_skey_leg);
+}
+
+static int mock_usable_consensus_flavor_value = FLAV_NS;
+
+static int
+mock_usable_consensus_flavor(void)
+{
+ return mock_usable_consensus_flavor_value;
+}
+
+static void
+test_router_pick_directory_server_impl(void *arg)
+{
+ (void)arg;
+
+ networkstatus_t *con_md = NULL;
+ char *consensus_text_md = NULL;
+ int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
+ or_options_t *options = get_options_mutable();
+ const routerstatus_t *rs = NULL;
+ options->UseMicrodescriptors = 1;
+ char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
+ node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
+ config_line_t *policy_line = NULL;
+ time_t now = time(NULL);
+ int tmp_dirport1, tmp_dirport3;
+
+ (void)arg;
+
+ MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
+
+ /* With no consensus, we must be bootstrapping, regardless of time or flavor
+ */
+ mock_usable_consensus_flavor_value = FLAV_NS;
+ tt_assert(networkstatus_consensus_is_bootstrapping(now));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+ mock_usable_consensus_flavor_value = FLAV_MICRODESC;
+ tt_assert(networkstatus_consensus_is_bootstrapping(now));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+ /* Init SR subsystem. */
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ sr_init(0);
+ UNMOCK(get_my_v3_authority_cert);
+
+ /* No consensus available, fail early */
+ rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
+ tt_assert(rs == NULL);
+
+ construct_consensus(&consensus_text_md);
+ tt_assert(consensus_text_md);
+ con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
+ NS_TYPE_CONSENSUS);
+ tt_assert(con_md);
+ tt_int_op(con_md->flavor,==, FLAV_MICRODESC);
+ tt_assert(con_md->routerstatus_list);
+ tt_int_op(smartlist_len(con_md->routerstatus_list), ==, 3);
+ tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
+ "microdesc"));
+
+ /* If the consensus time or flavor doesn't match, we are still
+ * bootstrapping */
+ mock_usable_consensus_flavor_value = FLAV_NS;
+ tt_assert(networkstatus_consensus_is_bootstrapping(now));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+ /* With a valid consensus for the current time and flavor, we stop
+ * bootstrapping, even if we have no certificates */
+ mock_usable_consensus_flavor_value = FLAV_MICRODESC;
+ tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
+ tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
+ tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
+ tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
+ + 24*60*60));
+ /* These times are outside the test validity period */
+ tt_assert(networkstatus_consensus_is_bootstrapping(now));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+ tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+ nodelist_set_consensus(con_md);
+ nodelist_assert_ok();
+
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ /* We should not fail now we have a consensus and routerstatus_list
+ * and nodelist are populated. */
+ tt_assert(rs != NULL);
+
+ /* Manipulate the nodes so we get the dir server we expect */
+ router1_id = tor_malloc(DIGEST_LEN);
+ memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
+ router2_id = tor_malloc(DIGEST_LEN);
+ memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
+ router3_id = tor_malloc(DIGEST_LEN);
+ memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
+
+ node_router1 = node_get_mutable_by_id(router1_id);
+ node_router2 = node_get_mutable_by_id(router2_id);
+ node_router3 = node_get_mutable_by_id(router3_id);
+
+ node_router1->is_possible_guard = 1;
+
+ node_router1->is_running = 0;
+ node_router3->is_running = 0;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
+ rs = NULL;
+ node_router1->is_running = 1;
+ node_router3->is_running = 1;
+
+ node_router1->rs->is_v2_dir = 0;
+ node_router3->rs->is_v2_dir = 0;
+ tmp_dirport1 = node_router1->rs->dir_port;
+ tmp_dirport3 = node_router3->rs->dir_port;
+ node_router1->rs->dir_port = 0;
+ node_router3->rs->dir_port = 0;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
+ rs = NULL;
+ node_router1->rs->is_v2_dir = 1;
+ node_router3->rs->is_v2_dir = 1;
+ node_router1->rs->dir_port = tmp_dirport1;
+ node_router3->rs->dir_port = tmp_dirport3;
+
+ node_router1->is_valid = 0;
+ node_router3->is_valid = 0;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
+ rs = NULL;
+ node_router1->is_valid = 1;
+ node_router3->is_valid = 1;
+
+ flags |= PDS_FOR_GUARD;
+ node_router1->using_as_guard = 1;
+ node_router2->using_as_guard = 1;
+ node_router3->using_as_guard = 1;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs == NULL);
+ node_router1->using_as_guard = 0;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
+ rs = NULL;
+ node_router2->using_as_guard = 0;
+ node_router3->using_as_guard = 0;
+
+ /* One not valid, one guard. This should leave one remaining */
+ node_router1->is_valid = 0;
+ node_router2->using_as_guard = 1;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
+ rs = NULL;
+ node_router1->is_valid = 1;
+ node_router2->using_as_guard = 0;
+
+ /* Manipulate overloaded */
+
+ node_router2->rs->last_dir_503_at = now;
+ node_router3->rs->last_dir_503_at = now;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
+ node_router2->rs->last_dir_503_at = 0;
+ node_router3->rs->last_dir_503_at = 0;
+
+ /* Set a Fascist firewall */
+ flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
+ policy_line = tor_malloc_zero(sizeof(config_line_t));
+ policy_line->key = tor_strdup("ReachableORAddresses");
+ policy_line->value = tor_strdup("accept *:442, reject *:*");
+ options->ReachableORAddresses = policy_line;
+ policies_parse_from_options(options);
+
+ node_router1->rs->or_port = 444;
+ node_router2->rs->or_port = 443;
+ node_router3->rs->or_port = 442;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
+ node_router1->rs->or_port = 442;
+ node_router2->rs->or_port = 443;
+ node_router3->rs->or_port = 444;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
+
+ /* Fascist firewall and overloaded */
+ node_router1->rs->or_port = 442;
+ node_router2->rs->or_port = 443;
+ node_router3->rs->or_port = 442;
+ node_router3->rs->last_dir_503_at = now;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
+ node_router3->rs->last_dir_503_at = 0;
+
+ /* Fascists against OR and Dir */
+ policy_line = tor_malloc_zero(sizeof(config_line_t));
+ policy_line->key = tor_strdup("ReachableAddresses");
+ policy_line->value = tor_strdup("accept *:80, reject *:*");
+ options->ReachableDirAddresses = policy_line;
+ policies_parse_from_options(options);
+ node_router1->rs->or_port = 442;
+ node_router2->rs->or_port = 441;
+ node_router3->rs->or_port = 443;
+ node_router1->rs->dir_port = 80;
+ node_router2->rs->dir_port = 80;
+ node_router3->rs->dir_port = 81;
+ node_router1->rs->last_dir_503_at = now;
+ rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
+ tt_assert(rs != NULL);
+ tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
+ node_router1->rs->last_dir_503_at = 0;
+
+ done:
+ UNMOCK(usable_consensus_flavor);
+ if (router1_id)
+ tor_free(router1_id);
+ if (router2_id)
+ tor_free(router2_id);
+ if (router3_id)
+ tor_free(router3_id);
+ if (options->ReachableORAddresses ||
+ options->ReachableDirAddresses)
+ policies_free_all();
+ tor_free(consensus_text_md);
+ networkstatus_vote_free(con_md);
+}
+
+static connection_t *mocked_connection = NULL;
+
+/* Mock connection_get_by_type_addr_port_purpose by returning
+ * mocked_connection. */
+static connection_t *
+mock_connection_get_by_type_addr_port_purpose(int type,
+ const tor_addr_t *addr,
+ uint16_t port, int purpose)
+{
+ (void)type;
+ (void)addr;
+ (void)port;
+ (void)purpose;
+
+ return mocked_connection;
+}
+
+#define TEST_ADDR_STR "127.0.0.1"
+#define TEST_DIR_PORT 12345
+
+static void
+test_routerlist_router_is_already_dir_fetching(void *arg)
+{
+ (void)arg;
+ tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
+
+ /* Setup */
+ tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
+ test_ap.port = TEST_DIR_PORT;
+ tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
+ null_addr_ap.port = TEST_DIR_PORT;
+ tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
+ zero_port_ap.port = 0;
+ MOCK(connection_get_by_type_addr_port_purpose,
+ mock_connection_get_by_type_addr_port_purpose);
+
+ /* Test that we never get 1 from a NULL connection */
+ mocked_connection = NULL;
+ tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 0);
+ tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 0);
+ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 0);
+ /* We always expect 0 in these cases */
+ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0);
+ tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0);
+ tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0);
+ tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0);
+
+ /* Test that we get 1 with a connection in the appropriate circumstances */
+ mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
+ tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 1);
+ tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 1);
+ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 1);
+
+ /* Test that we get 0 even with a connection in the appropriate
+ * circumstances */
+ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0);
+ tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0);
+ tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0);
+ tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0);
+
+ done:
+ /* If a connection is never set up, connection_free chokes on it. */
+ if (mocked_connection) {
+ buf_free(mocked_connection->inbuf);
+ buf_free(mocked_connection->outbuf);
+ }
+ tor_free(mocked_connection);
+ UNMOCK(connection_get_by_type_addr_port_purpose);
+}
+
+#undef TEST_ADDR_STR
+#undef TEST_DIR_PORT
+
#define NODE(name, flags) \
{ #name, test_routerlist_##name, (flags), NULL, NULL }
+#define ROUTER(name,flags) \
+ { #name, test_router_##name, (flags), NULL, NULL }
struct testcase_t routerlist_tests[] = {
NODE(initiate_descriptor_downloads, 0),
NODE(launch_descriptor_downloads, 0),
+ NODE(router_is_already_dir_fetching, TT_FORK),
+ ROUTER(pick_directory_server_impl, TT_FORK),
END_OF_TESTCASES
};
diff --git a/src/test/test_routerset.c b/src/test/test_routerset.c
index 90dfb28c6b..1b526d430b 100644
--- a/src/test/test_routerset.c
+++ b/src/test/test_routerset.c
@@ -423,16 +423,16 @@ NS(test_main)(void *arg)
}
#undef NS_SUBMODULE
-#define NS_SUBMODULE ASPECT(routerset_parse, policy)
+#define NS_SUBMODULE ASPECT(routerset_parse, policy_wildcard)
/*
- * Structural test for routerset_parse, when given a valid policy.
+ * Structural test for routerset_parse, when given a valid wildcard policy.
*/
NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
(const char *s, int assume_action, int *malformed_list));
-addr_policy_t *NS(mock_addr_policy);
+static addr_policy_t *NS(mock_addr_policy);
static void
NS(test_main)(void *arg)
@@ -470,6 +470,100 @@ NS(router_parse_addr_policy_item_from_string)(const char *s,
}
#undef NS_SUBMODULE
+#define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv4)
+
+/*
+ * Structural test for routerset_parse, when given a valid IPv4 address
+ * literal policy.
+ */
+
+NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
+ (const char *s, int assume_action, int *bogus));
+
+static addr_policy_t *NS(mock_addr_policy);
+
+static void
+NS(test_main)(void *arg)
+{
+ routerset_t *set;
+ const char *s;
+ int r;
+ (void)arg;
+
+ NS_MOCK(router_parse_addr_policy_item_from_string);
+ NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
+
+ set = routerset_new();
+ s = "127.0.0.1";
+ r = routerset_parse(set, s, "");
+ tt_int_op(r, OP_EQ, 0);
+ tt_int_op(smartlist_len(set->policies), OP_NE, 0);
+ tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
+
+ done:
+ routerset_free(set);
+}
+
+addr_policy_t *
+NS(router_parse_addr_policy_item_from_string)(const char *s, int assume_action,
+ int *bogus)
+{
+ (void)s;
+ (void)assume_action;
+ CALLED(router_parse_addr_policy_item_from_string)++;
+ *bogus = 0;
+
+ return NS(mock_addr_policy);
+}
+
+#undef NS_SUBMODULE
+#define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv6)
+
+/*
+ * Structural test for routerset_parse, when given a valid IPv6 address
+ * literal policy.
+ */
+
+NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
+ (const char *s, int assume_action, int *bad));
+
+static addr_policy_t *NS(mock_addr_policy);
+
+static void
+NS(test_main)(void *arg)
+{
+ routerset_t *set;
+ const char *s;
+ int r;
+ (void)arg;
+
+ NS_MOCK(router_parse_addr_policy_item_from_string);
+ NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
+
+ set = routerset_new();
+ s = "::1";
+ r = routerset_parse(set, s, "");
+ tt_int_op(r, OP_EQ, 0);
+ tt_int_op(smartlist_len(set->policies), OP_NE, 0);
+ tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
+
+ done:
+ routerset_free(set);
+}
+
+addr_policy_t *
+NS(router_parse_addr_policy_item_from_string)(const char *s,
+ int assume_action, int *bad)
+{
+ (void)s;
+ (void)assume_action;
+ CALLED(router_parse_addr_policy_item_from_string)++;
+ *bad = 0;
+
+ return NS(mock_addr_policy);
+}
+
+#undef NS_SUBMODULE
#define NS_SUBMODULE ASPECT(routerset_union, source_bad)
/*
@@ -1383,7 +1477,7 @@ NS(test_main)(void *arg)
* routerset or routerinfo.
*/
-node_t NS(mock_node);
+static node_t NS(mock_node);
static void
NS(test_main)(void *arg)
@@ -1410,7 +1504,7 @@ NS(test_main)(void *arg)
* routerset and no routerinfo.
*/
-node_t NS(mock_node);
+static node_t NS(mock_node);
static void
NS(test_main)(void *arg)
@@ -1509,7 +1603,7 @@ NS(test_main)(void *arg)
NS_DECL(const node_t *, node_get_by_nickname,
(const char *nickname, int warn_if_unused));
-const char *NS(mock_nickname);
+static const char *NS(mock_nickname);
static void
NS(test_main)(void *arg)
@@ -1558,8 +1652,8 @@ NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
NS_DECL(const node_t *, node_get_by_nickname,
(const char *nickname, int warn_if_unused));
-const char *NS(mock_nickname);
-node_t NS(mock_node);
+static const char *NS(mock_nickname);
+static node_t NS(mock_node);
static void
NS(test_main)(void *arg)
@@ -1608,8 +1702,8 @@ NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
NS_DECL(const node_t *, node_get_by_nickname,
(const char *nickname, int warn_if_unused));
-char *NS(mock_nickname);
-node_t NS(mock_node);
+static char *NS(mock_nickname);
+static node_t NS(mock_node);
static void
NS(test_main)(void *arg)
@@ -1660,7 +1754,7 @@ NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
NS_DECL(smartlist_t *, nodelist_get_list, (void));
-smartlist_t *NS(mock_smartlist);
+static smartlist_t *NS(mock_smartlist);
static void
NS(test_main)(void *arg)
@@ -1706,8 +1800,8 @@ NS(nodelist_get_list)(void)
NS_DECL(smartlist_t *, nodelist_get_list, (void));
-smartlist_t *NS(mock_smartlist);
-node_t NS(mock_node);
+static smartlist_t *NS(mock_smartlist);
+static node_t NS(mock_node);
static void
NS(test_main)(void *arg)
@@ -2109,7 +2203,9 @@ struct testcase_t routerset_tests[] = {
TEST_CASE_ASPECT(routerset_parse, valid_hexdigest),
TEST_CASE_ASPECT(routerset_parse, valid_nickname),
TEST_CASE_ASPECT(routerset_parse, get_countryname),
- TEST_CASE_ASPECT(routerset_parse, policy),
+ TEST_CASE_ASPECT(routerset_parse, policy_wildcard),
+ TEST_CASE_ASPECT(routerset_parse, policy_ipv4),
+ TEST_CASE_ASPECT(routerset_parse, policy_ipv6),
TEST_CASE(routerset_subtract_nodes),
TEST_CASE_ASPECT(routerset_subtract_nodes, null_routerset),
TEST_CASE(routerset_to_string),
diff --git a/src/test/test_scheduler.c b/src/test/test_scheduler.c
index 79a5534505..05ea8e86e8 100644
--- a/src/test/test_scheduler.c
+++ b/src/test/test_scheduler.c
@@ -1,16 +1,10 @@
-/* Copyright (c) 2014-2015, The Tor Project, Inc. */
+/* Copyright (c) 2014-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
-#include <math.h>
-
#include "orconfig.h"
-/* Libevent stuff */
-#ifdef HAVE_EVENT2_EVENT_H
+#include <math.h>
#include <event2/event.h>
-#else
-#include <event.h>
-#endif
#define TOR_CHANNEL_INTERNAL_
#define CHANNEL_PRIVATE_
@@ -24,12 +18,6 @@
#include "test.h"
#include "fakechans.h"
-/* Statics in scheduler.c exposed to the test suite */
-extern smartlist_t *channels_pending;
-extern struct event *run_sched_ev;
-extern uint64_t queue_heuristic;
-extern time_t queue_heuristic_timestamp;
-
/* Event base for scheduelr tests */
static struct event_base *mock_event_base = NULL;
@@ -96,9 +84,7 @@ mock_event_free_all(void)
static void
mock_event_init(void)
{
-#ifdef HAVE_EVENT2_EVENT_H
struct event_config *cfg = NULL;
-#endif
tt_ptr_op(mock_event_base, ==, NULL);
@@ -108,7 +94,6 @@ mock_event_init(void)
*/
if (!mock_event_base) {
-#ifdef HAVE_EVENT2_EVENT_H
cfg = event_config_new();
#if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
/* We can enable changelist support with epoll, since we don't give
@@ -117,9 +102,6 @@ mock_event_init(void)
#endif
mock_event_base = event_base_new_with_config(cfg);
event_config_free(cfg);
-#else
- mock_event_base = event_init();
-#endif
}
tt_assert(mock_event_base != NULL);
@@ -156,7 +138,7 @@ channel_flush_some_cells_mock_free_all(void)
static void
channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
{
- flush_mock_channel_t *flush_mock_ch = NULL;
+ int found = 0;
if (!chan) return;
if (num_cells <= 0) return;
@@ -172,6 +154,7 @@ channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
if (flush_mock_ch->chan == chan) {
/* Found it */
flush_mock_ch->cells = num_cells;
+ found = 1;
break;
}
} else {
@@ -181,8 +164,9 @@ channel_flush_some_cells_mock_set(channel_t *chan, ssize_t num_cells)
}
} SMARTLIST_FOREACH_END(flush_mock_ch);
- if (!flush_mock_ch) {
+ if (! found) {
/* The loop didn't find it */
+ flush_mock_channel_t *flush_mock_ch;
flush_mock_ch = tor_malloc_zero(sizeof(*flush_mock_ch));
flush_mock_ch->chan = chan;
flush_mock_ch->cells = num_cells;
@@ -461,11 +445,11 @@ test_scheduler_compare_channels(void *arg)
/*
* This is to test the different-policies case, which uses the policy
- * cast to an intptr_t as an arbitrary but definite thing to compare.
+ * cast to an uintptr_t as an arbitrary but definite thing to compare.
*/
mock_cgp_val_1 = tor_malloc_zero(16);
mock_cgp_val_2 = tor_malloc_zero(16);
- if ( ((intptr_t) mock_cgp_val_1) > ((intptr_t) mock_cgp_val_2) ) {
+ if ( ((uintptr_t) mock_cgp_val_1) > ((uintptr_t) mock_cgp_val_2) ) {
void *tmp = mock_cgp_val_1;
mock_cgp_val_1 = mock_cgp_val_2;
mock_cgp_val_2 = tmp;
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
new file mode 100644
index 0000000000..056f199b94
--- /dev/null
+++ b/src/test/test_shared_random.c
@@ -0,0 +1,1290 @@
+#define SHARED_RANDOM_PRIVATE
+#define SHARED_RANDOM_STATE_PRIVATE
+#define CONFIG_PRIVATE
+#define DIRVOTE_PRIVATE
+
+#include "or.h"
+#include "test.h"
+#include "config.h"
+#include "dirvote.h"
+#include "shared_random.h"
+#include "shared_random_state.h"
+#include "routerkeys.h"
+#include "routerlist.h"
+#include "router.h"
+#include "routerparse.h"
+#include "networkstatus.h"
+#include "log_test_helpers.h"
+
+static authority_cert_t *mock_cert;
+
+static authority_cert_t *
+get_my_v3_authority_cert_m(void)
+{
+ tor_assert(mock_cert);
+ return mock_cert;
+}
+
+static dir_server_t ds;
+
+static dir_server_t *
+trusteddirserver_get_by_v3_auth_digest_m(const char *digest)
+{
+ (void) digest;
+ /* The shared random code only need to know if a valid pointer to a dir
+ * server object has been found so this is safe because it won't use the
+ * pointer at all never. */
+ return &ds;
+}
+
+/* Setup a minimal dirauth environment by initializing the SR state and
+ * making sure the options are set to be an authority directory. */
+static void
+init_authority_state(void)
+{
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+
+ or_options_t *options = get_options_mutable();
+ mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ tt_assert(mock_cert);
+ options->AuthoritativeDir = 1;
+ tt_int_op(0, ==, load_ed_keys(options, time(NULL)));
+ sr_state_init(0, 0);
+ /* It's possible a commit has been generated in our state depending on
+ * the phase we are currently in which uses "now" as the starting
+ * timestamp. Delete it before we do any testing below. */
+ sr_state_delete_commits();
+
+ done:
+ UNMOCK(get_my_v3_authority_cert);
+}
+
+static void
+test_get_sr_protocol_phase(void *arg)
+{
+ time_t the_time;
+ sr_phase_t phase;
+ int retval;
+
+ (void) arg;
+
+ /* Initialize SR state */
+ init_authority_state();
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 23:59:00 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_REVEAL);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:00 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_COMMIT);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:01 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_COMMIT);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 11:59:00 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_COMMIT);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:00 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_REVEAL);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:01 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_REVEAL);
+ }
+
+ {
+ retval = parse_rfc1123_time("Wed, 20 Apr 2015 13:00:00 UTC", &the_time);
+ tt_int_op(retval, ==, 0);
+
+ phase = get_sr_protocol_phase(the_time);
+ tt_int_op(phase, ==, SR_PHASE_REVEAL);
+ }
+
+ done:
+ ;
+}
+
+static networkstatus_t *mock_consensus = NULL;
+
+static void
+test_get_state_valid_until_time(void *arg)
+{
+ time_t current_time;
+ time_t valid_until_time;
+ char tbuf[ISO_TIME_LEN + 1];
+ int retval;
+
+ (void) arg;
+
+ {
+ /* Get the valid until time if called at 00:00:01 */
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_until_time = get_state_valid_until_time(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, valid_until_time);
+ tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 19:22:00 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_until_time = get_state_valid_until_time(current_time);
+
+ format_iso_time(tbuf, valid_until_time);
+ tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:00 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_until_time = get_state_valid_until_time(current_time);
+
+ format_iso_time(tbuf, valid_until_time);
+ tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_until_time = get_state_valid_until_time(current_time);
+
+ format_iso_time(tbuf, valid_until_time);
+ tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
+ }
+
+ done:
+ ;
+}
+
+/* Mock function to immediately return our local 'mock_consensus'. */
+static networkstatus_t *
+mock_networkstatus_get_live_consensus(time_t now)
+{
+ (void) now;
+ return mock_consensus;
+}
+
+/** Test the get_next_valid_after_time() function. */
+static void
+test_get_next_valid_after_time(void *arg)
+{
+ time_t current_time;
+ time_t valid_after_time;
+ char tbuf[ISO_TIME_LEN + 1];
+ int retval;
+
+ (void) arg;
+
+ {
+ /* Setup a fake consensus just to get the times out of it, since
+ get_next_valid_after_time() needs them. */
+ mock_consensus = tor_malloc_zero(sizeof(networkstatus_t));
+
+ retval = parse_rfc1123_time("Mon, 13 Jan 2016 16:00:00 UTC",
+ &mock_consensus->fresh_until);
+ tt_int_op(retval, ==, 0);
+
+ retval = parse_rfc1123_time("Mon, 13 Jan 2016 15:00:00 UTC",
+ &mock_consensus->valid_after);
+ tt_int_op(retval, ==, 0);
+
+ MOCK(networkstatus_get_live_consensus,
+ mock_networkstatus_get_live_consensus);
+ }
+
+ {
+ /* Get the valid after time if called at 00:00:00 */
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_after_time = get_next_valid_after_time(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, valid_after_time);
+ tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ /* Get the valid until time if called at 00:00:01 */
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_after_time = get_next_valid_after_time(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, valid_after_time);
+ tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:30:01 UTC",
+ &current_time);
+ tt_int_op(retval, ==, 0);
+ valid_after_time = get_next_valid_after_time(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, valid_after_time);
+ tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
+ }
+
+ done:
+ networkstatus_vote_free(mock_consensus);
+}
+
+/* In this test we are going to generate a sr_commit_t object and validate
+ * it. We first generate our values, and then we parse them as if they were
+ * received from the network. After we parse both the commit and the reveal,
+ * we verify that they indeed match. */
+static void
+test_sr_commit(void *arg)
+{
+ authority_cert_t *auth_cert = NULL;
+ time_t now = time(NULL);
+ sr_commit_t *our_commit = NULL;
+ smartlist_t *args = smartlist_new();
+ sr_commit_t *parsed_commit = NULL;
+
+ (void) arg;
+
+ { /* Setup a minimal dirauth environment for this test */
+ or_options_t *options = get_options_mutable();
+
+ auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ tt_assert(auth_cert);
+
+ options->AuthoritativeDir = 1;
+ tt_int_op(0, ==, load_ed_keys(options, now));
+ }
+
+ /* Generate our commit object and validate it has the appropriate field
+ * that we can then use to build a representation that we'll find in a
+ * vote coming from the network. */
+ {
+ sr_commit_t test_commit;
+ our_commit = sr_generate_our_commit(now, auth_cert);
+ tt_assert(our_commit);
+ /* Default and only supported algorithm for now. */
+ tt_assert(our_commit->alg == DIGEST_SHA3_256);
+ /* We should have a reveal value. */
+ tt_assert(commit_has_reveal_value(our_commit));
+ /* We should have a random value. */
+ tt_assert(!tor_mem_is_zero((char *) our_commit->random_number,
+ sizeof(our_commit->random_number)));
+ /* Commit and reveal timestamp should be the same. */
+ tt_u64_op(our_commit->commit_ts, ==, our_commit->reveal_ts);
+ /* We should have a hashed reveal. */
+ tt_assert(!tor_mem_is_zero(our_commit->hashed_reveal,
+ sizeof(our_commit->hashed_reveal)));
+ /* Do we have a valid encoded commit and reveal. Note the following only
+ * tests if the generated values are correct. Their could be a bug in
+ * the decode function but we test them seperately. */
+ tt_int_op(0, ==, reveal_decode(our_commit->encoded_reveal,
+ &test_commit));
+ tt_int_op(0, ==, commit_decode(our_commit->encoded_commit,
+ &test_commit));
+ tt_int_op(0, ==, verify_commit_and_reveal(our_commit));
+ }
+
+ /* Let's make sure our verify commit and reveal function works. We'll
+ * make it fail a bit with known failure case. */
+ {
+ /* Copy our commit so we don't alter it for the rest of testing. */
+ sr_commit_t test_commit;
+ memcpy(&test_commit, our_commit, sizeof(test_commit));
+
+ /* Timestamp MUST match. */
+ test_commit.commit_ts = test_commit.reveal_ts - 42;
+ setup_full_capture_of_logs(LOG_WARN);
+ tt_int_op(-1, ==, verify_commit_and_reveal(&test_commit));
+ expect_log_msg_containing("doesn't match reveal timestamp");
+ teardown_capture_of_logs();
+ memcpy(&test_commit, our_commit, sizeof(test_commit));
+ tt_int_op(0, ==, verify_commit_and_reveal(&test_commit));
+
+ /* Hashed reveal must match the H(encoded_reveal). */
+ memset(test_commit.hashed_reveal, 'X',
+ sizeof(test_commit.hashed_reveal));
+ setup_full_capture_of_logs(LOG_WARN);
+ tt_int_op(-1, ==, verify_commit_and_reveal(&test_commit));
+ expect_single_log_msg_containing("doesn't match the commit value");
+ teardown_capture_of_logs();
+ memcpy(&test_commit, our_commit, sizeof(test_commit));
+ tt_int_op(0, ==, verify_commit_and_reveal(&test_commit));
+ }
+
+ /* We'll build a list of values from our commit that our parsing function
+ * takes from a vote line and see if we can parse it correctly. */
+ {
+ smartlist_add(args, tor_strdup("1"));
+ smartlist_add(args,
+ tor_strdup(crypto_digest_algorithm_get_name(our_commit->alg)));
+ smartlist_add(args, tor_strdup(sr_commit_get_rsa_fpr(our_commit)));
+ smartlist_add(args, tor_strdup(our_commit->encoded_commit));
+ smartlist_add(args, tor_strdup(our_commit->encoded_reveal));
+ parsed_commit = sr_parse_commit(args);
+ tt_assert(parsed_commit);
+ /* That parsed commit should be _EXACTLY_ like our original commit (we
+ * have to explicitly set the valid flag though). */
+ parsed_commit->valid = 1;
+ tt_mem_op(parsed_commit, OP_EQ, our_commit, sizeof(*parsed_commit));
+ /* Cleanup */
+ }
+
+ done:
+ teardown_capture_of_logs();
+ SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
+ smartlist_free(args);
+ sr_commit_free(our_commit);
+ sr_commit_free(parsed_commit);
+ authority_cert_free(auth_cert);
+}
+
+/* Test the encoding and decoding function for commit and reveal values. */
+static void
+test_encoding(void *arg)
+{
+ (void) arg;
+ int ret;
+ /* Random number is 32 bytes. */
+ char raw_rand[32];
+ time_t ts = 1454333590;
+ char hashed_rand[DIGEST256_LEN], hashed_reveal[DIGEST256_LEN];
+ sr_commit_t parsed_commit;
+
+ /* Those values were generated by sr_commit_calc_ref.py where the random
+ * value is 32 'A' and timestamp is the one in ts. */
+ static const char *encoded_reveal =
+ "AAAAAFavXpZJxbwTupvaJCTeIUCQmOPxAMblc7ChL5H2nZKuGchdaA==";
+ static const char *encoded_commit =
+ "AAAAAFavXpbkBMzMQG7aNoaGLFNpm2Wkk1ozXhuWWqL//GynltxVAg==";
+
+ /* Set up our raw random bytes array. */
+ memset(raw_rand, 'A', sizeof(raw_rand));
+ /* Hash random number because we don't expose bytes of the RNG. */
+ ret = crypto_digest256(hashed_rand, raw_rand,
+ sizeof(raw_rand), SR_DIGEST_ALG);
+ tt_int_op(0, ==, ret);
+ /* Hash reveal value. */
+ tt_int_op(SR_REVEAL_BASE64_LEN, ==, strlen(encoded_reveal));
+ ret = crypto_digest256(hashed_reveal, encoded_reveal,
+ strlen(encoded_reveal), SR_DIGEST_ALG);
+ tt_int_op(0, ==, ret);
+ tt_int_op(SR_COMMIT_BASE64_LEN, ==, strlen(encoded_commit));
+
+ /* Test our commit/reveal decode functions. */
+ {
+ /* Test the reveal encoded value. */
+ tt_int_op(0, ==, reveal_decode(encoded_reveal, &parsed_commit));
+ tt_u64_op(ts, ==, parsed_commit.reveal_ts);
+ tt_mem_op(hashed_rand, OP_EQ, parsed_commit.random_number,
+ sizeof(hashed_rand));
+
+ /* Test the commit encoded value. */
+ memset(&parsed_commit, 0, sizeof(parsed_commit));
+ tt_int_op(0, ==, commit_decode(encoded_commit, &parsed_commit));
+ tt_u64_op(ts, ==, parsed_commit.commit_ts);
+ tt_mem_op(encoded_commit, OP_EQ, parsed_commit.encoded_commit,
+ sizeof(parsed_commit.encoded_commit));
+ tt_mem_op(hashed_reveal, OP_EQ, parsed_commit.hashed_reveal,
+ sizeof(hashed_reveal));
+ }
+
+ /* Test our commit/reveal encode functions. */
+ {
+ /* Test the reveal encode. */
+ char encoded[SR_REVEAL_BASE64_LEN + 1];
+ parsed_commit.reveal_ts = ts;
+ memcpy(parsed_commit.random_number, hashed_rand,
+ sizeof(parsed_commit.random_number));
+ ret = reveal_encode(&parsed_commit, encoded, sizeof(encoded));
+ tt_int_op(SR_REVEAL_BASE64_LEN, ==, ret);
+ tt_mem_op(encoded_reveal, OP_EQ, encoded, strlen(encoded_reveal));
+ }
+
+ {
+ /* Test the commit encode. */
+ char encoded[SR_COMMIT_BASE64_LEN + 1];
+ parsed_commit.commit_ts = ts;
+ memcpy(parsed_commit.hashed_reveal, hashed_reveal,
+ sizeof(parsed_commit.hashed_reveal));
+ ret = commit_encode(&parsed_commit, encoded, sizeof(encoded));
+ tt_int_op(SR_COMMIT_BASE64_LEN, ==, ret);
+ tt_mem_op(encoded_commit, OP_EQ, encoded, strlen(encoded_commit));
+ }
+
+ done:
+ ;
+}
+
+/** Setup some SRVs in our SR state. If <b>also_current</b> is set, then set
+ * both current and previous SRVs.
+ * Helper of test_vote() and test_sr_compute_srv(). */
+static void
+test_sr_setup_srv(int also_current)
+{
+ sr_srv_t *srv = tor_malloc_zero(sizeof(sr_srv_t));
+ srv->num_reveals = 42;
+ memcpy(srv->value,
+ "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
+ sizeof(srv->value));
+
+ sr_state_set_previous_srv(srv);
+
+ if (also_current) {
+ srv = tor_malloc_zero(sizeof(sr_srv_t));
+ srv->num_reveals = 128;
+ memcpy(srv->value,
+ "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN",
+ sizeof(srv->value));
+
+ sr_state_set_current_srv(srv);
+ }
+}
+
+/* Test anything that has to do with SR protocol and vote. */
+static void
+test_vote(void *arg)
+{
+ int ret;
+ time_t now = time(NULL);
+ sr_commit_t *our_commit = NULL;
+
+ (void) arg;
+
+ MOCK(trusteddirserver_get_by_v3_auth_digest,
+ trusteddirserver_get_by_v3_auth_digest_m);
+
+ { /* Setup a minimal dirauth environment for this test */
+ init_authority_state();
+ /* Set ourself in reveal phase so we can parse the reveal value in the
+ * vote as well. */
+ set_sr_phase(SR_PHASE_REVEAL);
+ }
+
+ /* Generate our commit object and validate it has the appropriate field
+ * that we can then use to build a representation that we'll find in a
+ * vote coming from the network. */
+ {
+ sr_commit_t *saved_commit;
+ our_commit = sr_generate_our_commit(now, mock_cert);
+ tt_assert(our_commit);
+ sr_state_add_commit(our_commit);
+ /* Make sure it's there. */
+ saved_commit = sr_state_get_commit(our_commit->rsa_identity);
+ tt_assert(saved_commit);
+ }
+
+ /* Also setup the SRVs */
+ test_sr_setup_srv(1);
+
+ { /* Now test the vote generation */
+ smartlist_t *chunks = smartlist_new();
+ smartlist_t *tokens = smartlist_new();
+ /* Get our vote line and validate it. */
+ char *lines = sr_get_string_for_vote();
+ tt_assert(lines);
+ /* Split the lines. We expect 2 here. */
+ ret = smartlist_split_string(chunks, lines, "\n", SPLIT_IGNORE_BLANK, 0);
+ tt_int_op(ret, ==, 4);
+ tt_str_op(smartlist_get(chunks, 0), OP_EQ, "shared-rand-participate");
+ /* Get our commitment line and will validate it agains our commit. The
+ * format is as follow:
+ * "shared-rand-commitment" SP version SP algname SP identity
+ * SP COMMIT [SP REVEAL] NL
+ */
+ char *commit_line = smartlist_get(chunks, 1);
+ tt_assert(commit_line);
+ ret = smartlist_split_string(tokens, commit_line, " ", 0, 0);
+ tt_int_op(ret, ==, 6);
+ tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-commit");
+ tt_str_op(smartlist_get(tokens, 1), OP_EQ, "1");
+ tt_str_op(smartlist_get(tokens, 2), OP_EQ,
+ crypto_digest_algorithm_get_name(DIGEST_SHA3_256));
+ char digest[DIGEST_LEN];
+ base16_decode(digest, sizeof(digest), smartlist_get(tokens, 3),
+ HEX_DIGEST_LEN);
+ tt_mem_op(digest, ==, our_commit->rsa_identity, sizeof(digest));
+ tt_str_op(smartlist_get(tokens, 4), OP_EQ, our_commit->encoded_commit);
+ tt_str_op(smartlist_get(tokens, 5), OP_EQ, our_commit->encoded_reveal)
+;
+ /* Finally, does this vote line creates a valid commit object? */
+ smartlist_t *args = smartlist_new();
+ smartlist_add(args, smartlist_get(tokens, 1));
+ smartlist_add(args, smartlist_get(tokens, 2));
+ smartlist_add(args, smartlist_get(tokens, 3));
+ smartlist_add(args, smartlist_get(tokens, 4));
+ smartlist_add(args, smartlist_get(tokens, 5));
+ sr_commit_t *parsed_commit = sr_parse_commit(args);
+ tt_assert(parsed_commit);
+ /* Set valid flag explicitly here to compare since it's not set by
+ * simply parsing the commit. */
+ parsed_commit->valid = 1;
+ tt_mem_op(parsed_commit, ==, our_commit, sizeof(*our_commit));
+
+ /* minor cleanup */
+ SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
+ smartlist_clear(tokens);
+
+ /* Now test the previous SRV */
+ char *prev_srv_line = smartlist_get(chunks, 2);
+ tt_assert(prev_srv_line);
+ ret = smartlist_split_string(tokens, prev_srv_line, " ", 0, 0);
+ tt_int_op(ret, ==, 3);
+ tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-previous-value");
+ tt_str_op(smartlist_get(tokens, 1), OP_EQ, "42");
+ tt_str_op(smartlist_get(tokens, 2), OP_EQ,
+ "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlo=");
+
+ /* minor cleanup */
+ SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
+ smartlist_clear(tokens);
+
+ /* Now test the current SRV */
+ char *current_srv_line = smartlist_get(chunks, 3);
+ tt_assert(current_srv_line);
+ ret = smartlist_split_string(tokens, current_srv_line, " ", 0, 0);
+ tt_int_op(ret, ==, 3);
+ tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-current-value");
+ tt_str_op(smartlist_get(tokens, 1), OP_EQ, "128");
+ tt_str_op(smartlist_get(tokens, 2), OP_EQ,
+ "Tk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk4=");
+
+ /* Clean up */
+ sr_commit_free(parsed_commit);
+ SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
+ smartlist_free(chunks);
+ SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
+ smartlist_free(tokens);
+ smartlist_clear(args);
+ smartlist_free(args);
+ tor_free(lines);
+ }
+
+ done:
+ sr_commit_free(our_commit);
+ UNMOCK(trusteddirserver_get_by_v3_auth_digest);
+}
+
+static const char *sr_state_str = "Version 1\n"
+ "TorVersion 0.2.9.0-alpha-dev\n"
+ "ValidAfter 2037-04-19 07:16:00\n"
+ "ValidUntil 2037-04-20 07:16:00\n"
+ "Commit 1 sha3-256 FA3CEC2C99DC68D3166B9B6E4FA21A4026C2AB1C "
+ "7M8GdubCAAdh7WUG0DiwRyxTYRKji7HATa7LLJEZ/UAAAAAAVmfUSg== "
+ "AAAAAFZn1EojfIheIw42bjK3VqkpYyjsQFSbv/dxNna3Q8hUEPKpOw==\n"
+ "Commit 1 sha3-256 41E89EDFBFBA44983E21F18F2230A4ECB5BFB543 "
+ "17aUsYuMeRjd2N1r8yNyg7aHqRa6gf4z7QPoxxAZbp0AAAAAVmfUSg==\n"
+ "Commit 1 sha3-256 36637026573A04110CF3E6B1D201FB9A98B88734 "
+ "DDDYtripvdOU+XPEUm5xpU64d9IURSds1xSwQsgeB8oAAAAAVmfUSg==\n"
+ "SharedRandPreviousValue 4 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=\n"
+ "SharedRandCurrentValue 3 8dWeW12KEzTGEiLGgO1UVJ7Z91CekoRcxt6Q9KhnOFI=\n";
+
+/** Create an SR disk state, parse it and validate that the parsing went
+ * well. Yes! */
+static void
+test_state_load_from_disk(void *arg)
+{
+ int ret;
+ char *dir = tor_strdup(get_fname("test_sr_state"));
+ char *sr_state_path = tor_strdup(get_fname("test_sr_state/sr_state"));
+ sr_state_t *the_sr_state = NULL;
+
+ (void) arg;
+
+ MOCK(trusteddirserver_get_by_v3_auth_digest,
+ trusteddirserver_get_by_v3_auth_digest_m);
+
+ /* First try with a nonexistent path. */
+ ret = disk_state_load_from_disk_impl("NONEXISTENTNONEXISTENT");
+ tt_assert(ret == -ENOENT);
+
+ /* Now create a mock state directory and state file */
+#ifdef _WIN32
+ ret = mkdir(dir);
+#else
+ ret = mkdir(dir, 0700);
+#endif
+ tt_assert(ret == 0);
+ ret = write_str_to_file(sr_state_path, sr_state_str, 0);
+ tt_assert(ret == 0);
+
+ /* Try to load the directory itself. Should fail. */
+ ret = disk_state_load_from_disk_impl(dir);
+ tt_int_op(ret, OP_LT, 0);
+
+ /* State should be non-existent at this point. */
+ the_sr_state = get_sr_state();
+ tt_assert(!the_sr_state);
+
+ /* Now try to load the correct file! */
+ ret = disk_state_load_from_disk_impl(sr_state_path);
+ tt_assert(ret == 0);
+
+ /* Check the content of the state */
+ /* XXX check more deeply!!! */
+ the_sr_state = get_sr_state();
+ tt_assert(the_sr_state);
+ tt_assert(the_sr_state->version == 1);
+ tt_assert(digestmap_size(the_sr_state->commits) == 3);
+ tt_assert(the_sr_state->current_srv);
+ tt_assert(the_sr_state->current_srv->num_reveals == 3);
+ tt_assert(the_sr_state->previous_srv);
+
+ /* XXX Now also try loading corrupted state files and make sure parsing
+ fails */
+
+ done:
+ tor_free(dir);
+ tor_free(sr_state_path);
+ UNMOCK(trusteddirserver_get_by_v3_auth_digest);
+}
+
+/** Generate three specially crafted commits (based on the test
+ * vector at sr_srv_calc_ref.py). Helper of test_sr_compute_srv(). */
+static void
+test_sr_setup_commits(void)
+{
+ time_t now = time(NULL);
+ sr_commit_t *commit_a, *commit_b, *commit_c, *commit_d;
+ sr_commit_t *place_holder = tor_malloc_zero(sizeof(*place_holder));
+ authority_cert_t *auth_cert = NULL;
+
+ { /* Setup a minimal dirauth environment for this test */
+ or_options_t *options = get_options_mutable();
+
+ auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ tt_assert(auth_cert);
+
+ options->AuthoritativeDir = 1;
+ tt_int_op(0, ==, load_ed_keys(options, now));
+ }
+
+ /* Generate three dummy commits according to sr_srv_calc_ref.py . Then
+ register them to the SR state. Also register a fourth commit 'd' with no
+ reveal info, to make sure that it will get ignored during SRV
+ calculation. */
+
+ { /* Commit from auth 'a' */
+ commit_a = sr_generate_our_commit(now, auth_cert);
+ tt_assert(commit_a);
+
+ /* Do some surgery on the commit */
+ memset(commit_a->rsa_identity, 'A', sizeof(commit_a->rsa_identity));
+ base16_encode(commit_a->rsa_identity_hex,
+ sizeof(commit_a->rsa_identity_hex), commit_a->rsa_identity,
+ sizeof(commit_a->rsa_identity));
+ strlcpy(commit_a->encoded_reveal,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ sizeof(commit_a->encoded_reveal));
+ memcpy(commit_a->hashed_reveal,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
+ sizeof(commit_a->hashed_reveal));
+ }
+
+ { /* Commit from auth 'b' */
+ commit_b = sr_generate_our_commit(now, auth_cert);
+ tt_assert(commit_b);
+
+ /* Do some surgery on the commit */
+ memset(commit_b->rsa_identity, 'B', sizeof(commit_b->rsa_identity));
+ base16_encode(commit_b->rsa_identity_hex,
+ sizeof(commit_b->rsa_identity_hex), commit_b->rsa_identity,
+ sizeof(commit_b->rsa_identity));
+ strlcpy(commit_b->encoded_reveal,
+ "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
+ sizeof(commit_b->encoded_reveal));
+ memcpy(commit_b->hashed_reveal,
+ "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
+ sizeof(commit_b->hashed_reveal));
+ }
+
+ { /* Commit from auth 'c' */
+ commit_c = sr_generate_our_commit(now, auth_cert);
+ tt_assert(commit_c);
+
+ /* Do some surgery on the commit */
+ memset(commit_c->rsa_identity, 'C', sizeof(commit_c->rsa_identity));
+ base16_encode(commit_c->rsa_identity_hex,
+ sizeof(commit_c->rsa_identity_hex), commit_c->rsa_identity,
+ sizeof(commit_c->rsa_identity));
+ strlcpy(commit_c->encoded_reveal,
+ "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
+ sizeof(commit_c->encoded_reveal));
+ memcpy(commit_c->hashed_reveal,
+ "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
+ sizeof(commit_c->hashed_reveal));
+ }
+
+ { /* Commit from auth 'd' */
+ commit_d = sr_generate_our_commit(now, auth_cert);
+ tt_assert(commit_d);
+
+ /* Do some surgery on the commit */
+ memset(commit_d->rsa_identity, 'D', sizeof(commit_d->rsa_identity));
+ base16_encode(commit_d->rsa_identity_hex,
+ sizeof(commit_d->rsa_identity_hex), commit_d->rsa_identity,
+ sizeof(commit_d->rsa_identity));
+ strlcpy(commit_d->encoded_reveal,
+ "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
+ sizeof(commit_d->encoded_reveal));
+ memcpy(commit_d->hashed_reveal,
+ "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
+ sizeof(commit_d->hashed_reveal));
+ /* Clean up its reveal info */
+ memcpy(place_holder, commit_d, sizeof(*place_holder));
+ memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
+ tt_assert(!commit_has_reveal_value(commit_d));
+ }
+
+ /* Register commits to state (during commit phase) */
+ set_sr_phase(SR_PHASE_COMMIT);
+ save_commit_to_state(commit_a);
+ save_commit_to_state(commit_b);
+ save_commit_to_state(commit_c);
+ save_commit_to_state(commit_d);
+ tt_int_op(digestmap_size(get_sr_state()->commits), ==, 4);
+
+ /* Now during REVEAL phase save commit D by restoring its reveal. */
+ set_sr_phase(SR_PHASE_REVEAL);
+ save_commit_to_state(place_holder);
+ tt_str_op(commit_d->encoded_reveal, OP_EQ,
+ "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
+ /* Go back to an empty encoded reveal value. */
+ memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
+ memset(commit_d->random_number, 0, sizeof(commit_d->random_number));
+ tt_assert(!commit_has_reveal_value(commit_d));
+
+ done:
+ authority_cert_free(auth_cert);
+}
+
+/** Verify that the SRV generation procedure is proper by testing it against
+ * the test vector from ./sr_srv_calc_ref.py. */
+static void
+test_sr_compute_srv(void *arg)
+{
+ (void) arg;
+ const sr_srv_t *current_srv = NULL;
+
+#define SRV_TEST_VECTOR \
+ "2A9B1D6237DAB312A40F575DA85C147663E7ED3F80E9555395F15B515C74253D"
+
+ MOCK(trusteddirserver_get_by_v3_auth_digest,
+ trusteddirserver_get_by_v3_auth_digest_m);
+
+ init_authority_state();
+
+ /* Setup the commits for this unittest */
+ test_sr_setup_commits();
+ test_sr_setup_srv(0);
+
+ /* Now switch to reveal phase */
+ set_sr_phase(SR_PHASE_REVEAL);
+
+ /* Compute the SRV */
+ sr_compute_srv();
+
+ /* Check the result against the test vector */
+ current_srv = sr_state_get_current_srv();
+ tt_assert(current_srv);
+ tt_u64_op(current_srv->num_reveals, ==, 3);
+ tt_str_op(hex_str((char*)current_srv->value, 32),
+ ==,
+ SRV_TEST_VECTOR);
+
+ done:
+ UNMOCK(trusteddirserver_get_by_v3_auth_digest);
+}
+
+/** Return a minimal vote document with a current SRV value set to
+ * <b>srv</b>. */
+static networkstatus_t *
+get_test_vote_with_curr_srv(const char *srv)
+{
+ networkstatus_t *vote = tor_malloc_zero(sizeof(networkstatus_t));
+
+ vote->type = NS_TYPE_VOTE;
+ vote->sr_info.participate = 1;
+ vote->sr_info.current_srv = tor_malloc_zero(sizeof(sr_srv_t));
+ vote->sr_info.current_srv->num_reveals = 42;
+ memcpy(vote->sr_info.current_srv->value,
+ srv,
+ sizeof(vote->sr_info.current_srv->value));
+
+ return vote;
+}
+
+/* Test the function that picks the right SRV given a bunch of votes. Make sure
+ * that the function returns an SRV iff the majority/agreement requirements are
+ * met. */
+static void
+test_sr_get_majority_srv_from_votes(void *arg)
+{
+ sr_srv_t *chosen_srv;
+ smartlist_t *votes = smartlist_new();
+
+#define SRV_1 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+#define SRV_2 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
+
+ (void) arg;
+
+ init_authority_state();
+ /* Make sure our SRV is fresh so we can consider the super majority with
+ * the consensus params of number of agreements needed. */
+ sr_state_set_fresh_srv();
+
+ /* The test relies on the dirauth list being initialized. */
+ clear_dir_servers();
+ add_default_trusted_dir_authorities(V3_DIRINFO);
+
+ { /* Prepare voting environment with just a single vote. */
+ networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
+ smartlist_add(votes, vote);
+ }
+
+ /* Since it's only one vote with an SRV, it should not achieve majority and
+ hence no SRV will be returned. */
+ chosen_srv = get_majority_srv_from_votes(votes, 1);
+ tt_assert(!chosen_srv);
+
+ { /* Now put in 8 more votes. Let SRV_1 have majority. */
+ int i;
+ /* Now 7 votes believe in SRV_1 */
+ for (i = 0; i < 3; i++) {
+ networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
+ smartlist_add(votes, vote);
+ }
+ /* and 2 votes believe in SRV_2 */
+ for (i = 0; i < 2; i++) {
+ networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_2);
+ smartlist_add(votes, vote);
+ }
+ for (i = 0; i < 3; i++) {
+ networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
+ smartlist_add(votes, vote);
+ }
+
+ tt_int_op(smartlist_len(votes), ==, 9);
+ }
+
+ /* Now we achieve majority for SRV_1, but not the AuthDirNumSRVAgreements
+ requirement. So still not picking an SRV. */
+ set_num_srv_agreements(8);
+ chosen_srv = get_majority_srv_from_votes(votes, 1);
+ tt_assert(!chosen_srv);
+
+ /* We will now lower the AuthDirNumSRVAgreements requirement by tweaking the
+ * consensus parameter and we will try again. This time it should work. */
+ set_num_srv_agreements(7);
+ chosen_srv = get_majority_srv_from_votes(votes, 1);
+ tt_assert(chosen_srv);
+ tt_u64_op(chosen_srv->num_reveals, ==, 42);
+ tt_mem_op(chosen_srv->value, OP_EQ, SRV_1, sizeof(chosen_srv->value));
+
+ done:
+ SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
+ networkstatus_vote_free(vote));
+ smartlist_free(votes);
+}
+
+static void
+test_utils(void *arg)
+{
+ (void) arg;
+
+ /* Testing srv_dup(). */
+ {
+ sr_srv_t *srv = NULL, *dup_srv = NULL;
+ const char *srv_value =
+ "1BDB7C3E973936E4D13A49F37C859B3DC69C429334CF9412E3FEF6399C52D47A";
+ srv = tor_malloc_zero(sizeof(*srv));
+ srv->num_reveals = 42;
+ memcpy(srv->value, srv_value, sizeof(srv->value));
+ dup_srv = srv_dup(srv);
+ tt_assert(dup_srv);
+ tt_u64_op(dup_srv->num_reveals, ==, srv->num_reveals);
+ tt_mem_op(dup_srv->value, OP_EQ, srv->value, sizeof(srv->value));
+ tor_free(srv);
+ tor_free(dup_srv);
+ }
+
+ /* Testing commitments_are_the_same(). Currently, the check is to test the
+ * value of the encoded commit so let's make sure that actually works. */
+ {
+ /* Payload of 57 bytes that is the length of sr_commit_t->encoded_commit.
+ * 56 bytes of payload and a NUL terminated byte at the end ('\x00')
+ * which comes down to SR_COMMIT_BASE64_LEN + 1. */
+ const char *payload =
+ "\x5d\xb9\x60\xb6\xcc\x51\x68\x52\x31\xd9\x88\x88\x71\x71\xe0\x30"
+ "\x59\x55\x7f\xcd\x61\xc0\x4b\x05\xb8\xcd\xc1\x48\xe9\xcd\x16\x1f"
+ "\x70\x15\x0c\xfc\xd3\x1a\x75\xd0\x93\x6c\xc4\xe0\x5c\xbe\xe2\x18"
+ "\xc7\xaf\x72\xb6\x7c\x9b\x52\x00";
+ sr_commit_t commit1, commit2;
+ memcpy(commit1.encoded_commit, payload, sizeof(commit1.encoded_commit));
+ memcpy(commit2.encoded_commit, payload, sizeof(commit2.encoded_commit));
+ tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 1);
+ /* Let's corrupt one of them. */
+ memset(commit1.encoded_commit, 'A', sizeof(commit1.encoded_commit));
+ tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 0);
+ }
+
+ /* Testing commit_is_authoritative(). */
+ {
+ crypto_pk_t *k = crypto_pk_new();
+ char digest[DIGEST_LEN];
+ sr_commit_t commit;
+
+ tt_assert(!crypto_pk_generate_key(k));
+
+ tt_int_op(0, ==, crypto_pk_get_digest(k, digest));
+ memcpy(commit.rsa_identity, digest, sizeof(commit.rsa_identity));
+ tt_int_op(commit_is_authoritative(&commit, digest), ==, 1);
+ /* Change the pubkey. */
+ memset(commit.rsa_identity, 0, sizeof(commit.rsa_identity));
+ tt_int_op(commit_is_authoritative(&commit, digest), ==, 0);
+ crypto_pk_free(k);
+ }
+
+ /* Testing get_phase_str(). */
+ {
+ tt_str_op(get_phase_str(SR_PHASE_REVEAL), ==, "reveal");
+ tt_str_op(get_phase_str(SR_PHASE_COMMIT), ==, "commit");
+ }
+
+ /* Testing phase transition */
+ {
+ init_authority_state();
+ set_sr_phase(SR_PHASE_COMMIT);
+ tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 1);
+ tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 0);
+ set_sr_phase(SR_PHASE_REVEAL);
+ tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 0);
+ tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 1);
+ /* Junk. */
+ tt_int_op(is_phase_transition(42), ==, 1);
+ }
+
+ done:
+ return;
+}
+
+static void
+test_state_transition(void *arg)
+{
+ sr_state_t *state = NULL;
+ time_t now = time(NULL);
+
+ (void) arg;
+
+ { /* Setup a minimal dirauth environment for this test */
+ init_authority_state();
+ state = get_sr_state();
+ tt_assert(state);
+ }
+
+ /* Test our state reset for a new protocol run. */
+ {
+ /* Add a commit to the state so we can test if the reset cleans the
+ * commits. Also, change all params that we expect to be updated. */
+ sr_commit_t *commit = sr_generate_our_commit(now, mock_cert);
+ tt_assert(commit);
+ sr_state_add_commit(commit);
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ /* Let's test our delete feature. */
+ sr_state_delete_commits();
+ tt_int_op(digestmap_size(state->commits), ==, 0);
+ /* Add it back so we can continue the rest of the test because after
+ * deletiong our commit will be freed so generate a new one. */
+ commit = sr_generate_our_commit(now, mock_cert);
+ tt_assert(commit);
+ sr_state_add_commit(commit);
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ state->n_reveal_rounds = 42;
+ state->n_commit_rounds = 43;
+ state->n_protocol_runs = 44;
+ reset_state_for_new_protocol_run(now);
+ tt_int_op(state->n_reveal_rounds, ==, 0);
+ tt_int_op(state->n_commit_rounds, ==, 0);
+ tt_u64_op(state->n_protocol_runs, ==, 45);
+ tt_int_op(digestmap_size(state->commits), ==, 0);
+ }
+
+ /* Test SRV rotation in our state. */
+ {
+ const sr_srv_t *cur, *prev;
+ test_sr_setup_srv(1);
+ cur = sr_state_get_current_srv();
+ tt_assert(cur);
+ /* After, current srv should be the previous and then set to NULL. */
+ state_rotate_srv();
+ prev = sr_state_get_previous_srv();
+ tt_assert(prev == cur);
+ tt_assert(!sr_state_get_current_srv());
+ sr_state_clean_srvs();
+ }
+
+ /* New protocol run. */
+ {
+ const sr_srv_t *cur;
+ /* Setup some new SRVs so we can confirm that a new protocol run
+ * actually makes them rotate and compute new ones. */
+ test_sr_setup_srv(1);
+ cur = sr_state_get_current_srv();
+ tt_assert(cur);
+ set_sr_phase(SR_PHASE_REVEAL);
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ new_protocol_run(now);
+ UNMOCK(get_my_v3_authority_cert);
+ /* Rotation happened. */
+ tt_assert(sr_state_get_previous_srv() == cur);
+ /* We are going into COMMIT phase so we had to rotate our SRVs. Usually
+ * our current SRV would be NULL but a new protocol run should make us
+ * compute a new SRV. */
+ tt_assert(sr_state_get_current_srv());
+ /* Also, make sure we did change the current. */
+ tt_assert(sr_state_get_current_srv() != cur);
+ /* We should have our commitment alone. */
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ tt_int_op(state->n_reveal_rounds, ==, 0);
+ tt_int_op(state->n_commit_rounds, ==, 0);
+ /* 46 here since we were at 45 just before. */
+ tt_u64_op(state->n_protocol_runs, ==, 46);
+ }
+
+ /* Cleanup of SRVs. */
+ {
+ sr_state_clean_srvs();
+ tt_assert(!sr_state_get_current_srv());
+ tt_assert(!sr_state_get_previous_srv());
+ }
+
+ done:
+ return;
+}
+
+static void
+test_keep_commit(void *arg)
+{
+ char fp[FINGERPRINT_LEN + 1];
+ sr_commit_t *commit = NULL, *dup_commit = NULL;
+ sr_state_t *state;
+ time_t now = time(NULL);
+ crypto_pk_t *k = NULL;
+
+ (void) arg;
+
+ MOCK(trusteddirserver_get_by_v3_auth_digest,
+ trusteddirserver_get_by_v3_auth_digest_m);
+
+ {
+ k = pk_generate(1);
+ /* Setup a minimal dirauth environment for this test */
+ /* Have a key that is not the one from our commit. */
+ init_authority_state();
+ state = get_sr_state();
+ }
+
+ /* Test this very important function that tells us if we should keep a
+ * commit or not in our state. Most of it depends on the phase and what's
+ * in the commit so we'll change the commit as we go. */
+ commit = sr_generate_our_commit(now, mock_cert);
+ tt_assert(commit);
+ /* Set us in COMMIT phase for starter. */
+ set_sr_phase(SR_PHASE_COMMIT);
+ /* We should never keep a commit from a non authoritative authority. */
+ tt_int_op(should_keep_commit(commit, fp, SR_PHASE_COMMIT), ==, 0);
+ /* This should NOT be kept because it has a reveal value in it. */
+ tt_assert(commit_has_reveal_value(commit));
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_COMMIT), ==, 0);
+ /* Add it to the state which should return to not keep it. */
+ sr_state_add_commit(commit);
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_COMMIT), ==, 0);
+ /* Remove it from state so we can continue our testing. */
+ digestmap_remove(state->commits, commit->rsa_identity);
+ /* Let's remove our reveal value which should make it OK to keep it. */
+ memset(commit->encoded_reveal, 0, sizeof(commit->encoded_reveal));
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_COMMIT), ==, 1);
+
+ /* Let's reset our commit and go into REVEAL phase. */
+ sr_commit_free(commit);
+ commit = sr_generate_our_commit(now, mock_cert);
+ tt_assert(commit);
+ /* Dup the commit so we have one with and one without a reveal value. */
+ dup_commit = tor_malloc_zero(sizeof(*dup_commit));
+ memcpy(dup_commit, commit, sizeof(*dup_commit));
+ memset(dup_commit->encoded_reveal, 0, sizeof(dup_commit->encoded_reveal));
+ set_sr_phase(SR_PHASE_REVEAL);
+ /* We should never keep a commit from a non authoritative authority. */
+ tt_int_op(should_keep_commit(commit, fp, SR_PHASE_REVEAL), ==, 0);
+ /* We shouldn't accept a commit that is not in our state. */
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_REVEAL), ==, 0);
+ /* Important to add the commit _without_ the reveal here. */
+ sr_state_add_commit(dup_commit);
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ /* Our commit should be valid that is authoritative, contains a reveal, be
+ * in the state and commitment and reveal values match. */
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_REVEAL), ==, 1);
+ /* The commit shouldn't be kept if it's not verified that is no matchin
+ * hashed reveal. */
+ {
+ /* Let's save the hash reveal so we can restore it. */
+ sr_commit_t place_holder;
+ memcpy(place_holder.hashed_reveal, commit->hashed_reveal,
+ sizeof(place_holder.hashed_reveal));
+ memset(commit->hashed_reveal, 0, sizeof(commit->hashed_reveal));
+ setup_full_capture_of_logs(LOG_WARN);
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_REVEAL), ==, 0);
+ expect_log_msg_containing("doesn't match the commit value.");
+ expect_log_msg_containing("has an invalid reveal value.");
+ assert_log_predicate(mock_saved_log_n_entries() == 2,
+ "expected 2 log entries");
+ teardown_capture_of_logs();
+ memcpy(commit->hashed_reveal, place_holder.hashed_reveal,
+ sizeof(commit->hashed_reveal));
+ }
+ /* We shouldn't keep a commit that has no reveal. */
+ tt_int_op(should_keep_commit(dup_commit, dup_commit->rsa_identity,
+ SR_PHASE_REVEAL), ==, 0);
+ /* We must not keep a commit that is not the same from the commit phase. */
+ memset(commit->encoded_commit, 0, sizeof(commit->encoded_commit));
+ tt_int_op(should_keep_commit(commit, commit->rsa_identity,
+ SR_PHASE_REVEAL), ==, 0);
+
+ done:
+ teardown_capture_of_logs();
+ sr_commit_free(commit);
+ sr_commit_free(dup_commit);
+ crypto_pk_free(k);
+ UNMOCK(trusteddirserver_get_by_v3_auth_digest);
+}
+
+static void
+test_state_update(void *arg)
+{
+ time_t commit_phase_time = 1452076000;
+ time_t reveal_phase_time = 1452086800;
+ sr_state_t *state;
+
+ (void) arg;
+
+ {
+ init_authority_state();
+ state = get_sr_state();
+ set_sr_phase(SR_PHASE_COMMIT);
+ /* We'll cheat a bit here and reset the creation time of the state which
+ * will avoid us to compute a valid_after time that fits the commit
+ * phase. */
+ state->valid_after = 0;
+ state->n_reveal_rounds = 0;
+ state->n_commit_rounds = 0;
+ state->n_protocol_runs = 0;
+ }
+
+ /* We need to mock for the state update function call. */
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+
+ /* We are in COMMIT phase here and we'll trigger a state update but no
+ * transition. */
+ sr_state_update(commit_phase_time);
+ tt_int_op(state->valid_after, ==, commit_phase_time);
+ tt_int_op(state->n_commit_rounds, ==, 1);
+ tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+
+ /* We are still in the COMMIT phase here but we'll trigger a state
+ * transition to the REVEAL phase. */
+ sr_state_update(reveal_phase_time);
+ tt_int_op(state->phase, ==, SR_PHASE_REVEAL);
+ tt_int_op(state->valid_after, ==, reveal_phase_time);
+ /* Only our commit should be in there. */
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ tt_int_op(state->n_reveal_rounds, ==, 1);
+
+ /* We can't update a state with a valid after _lower_ than the creation
+ * time so here it is. */
+ sr_state_update(commit_phase_time);
+ tt_int_op(state->valid_after, ==, reveal_phase_time);
+
+ /* Finally, let's go back in COMMIT phase so we can test the state update
+ * of a new protocol run. */
+ state->valid_after = 0;
+ sr_state_update(commit_phase_time);
+ tt_int_op(state->valid_after, ==, commit_phase_time);
+ tt_int_op(state->n_commit_rounds, ==, 1);
+ tt_int_op(state->n_reveal_rounds, ==, 0);
+ tt_u64_op(state->n_protocol_runs, ==, 1);
+ tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
+ tt_int_op(digestmap_size(state->commits), ==, 1);
+ tt_assert(state->current_srv);
+
+ done:
+ sr_state_free();
+ UNMOCK(get_my_v3_authority_cert);
+}
+
+struct testcase_t sr_tests[] = {
+ { "get_sr_protocol_phase", test_get_sr_protocol_phase, TT_FORK,
+ NULL, NULL },
+ { "sr_commit", test_sr_commit, TT_FORK,
+ NULL, NULL },
+ { "keep_commit", test_keep_commit, TT_FORK,
+ NULL, NULL },
+ { "encoding", test_encoding, TT_FORK,
+ NULL, NULL },
+ { "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
+ NULL, NULL },
+ { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
+ NULL, NULL },
+ { "vote", test_vote, TT_FORK,
+ NULL, NULL },
+ { "state_load_from_disk", test_state_load_from_disk, TT_FORK,
+ NULL, NULL },
+ { "sr_compute_srv", test_sr_compute_srv, TT_FORK, NULL, NULL },
+ { "sr_get_majority_srv_from_votes", test_sr_get_majority_srv_from_votes,
+ TT_FORK, NULL, NULL },
+ { "utils", test_utils, TT_FORK, NULL, NULL },
+ { "state_transition", test_state_transition, TT_FORK, NULL, NULL },
+ { "state_update", test_state_update, TT_FORK,
+ NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_slow.c b/src/test/test_slow.c
index 32386b485e..7c9f0b1cc2 100644
--- a/src/test/test_slow.c
+++ b/src/test/test_slow.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
@@ -18,9 +18,6 @@
#include "or.h"
#include "test.h"
-extern struct testcase_t slow_crypto_tests[];
-extern struct testcase_t slow_util_tests[];
-
struct testgroup_t testgroups[] = {
{ "slow/crypto/", slow_crypto_tests },
{ "slow/util/", slow_util_tests },
diff --git a/src/test/test_socks.c b/src/test/test_socks.c
index 465e427930..62ff12fe15 100644
--- a/src/test/test_socks.c
+++ b/src/test/test_socks.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
@@ -34,7 +34,7 @@ socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
return 1;
}
-const struct testcase_setup_t socks_setup = {
+static const struct testcase_setup_t socks_setup = {
socks_test_setup, socks_test_cleanup
};
diff --git a/src/test/test_status.c b/src/test/test_status.c
index cbc8af188c..a3b1a2af87 100644
--- a/src/test/test_status.c
+++ b/src/test/test_status.c
@@ -3,6 +3,8 @@
#define LOG_PRIVATE
#define REPHIST_PRIVATE
+#include "orconfig.h"
+
#include <float.h>
#include <math.h>
@@ -310,8 +312,6 @@ NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
NS_DECL(int, server_mode, (const or_options_t *options));
static routerinfo_t *mock_routerinfo;
-extern int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1];
-extern int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1];
static void
NS(test_main)(void *arg)
@@ -707,15 +707,18 @@ NS(logv)(int severity, log_domain_mask_t domain,
tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
tt_ptr_op(suffix, OP_EQ, NULL);
tt_str_op(format, OP_EQ,
- "Heartbeat: Accounting enabled. Sent: %s / %s, Received: %s / %s. "
- "The current accounting interval ends on %s, in %s.");
+ "Heartbeat: Accounting enabled. Sent: %s, Received: %s, Used: %s / "
+ "%s, Rule: %s. The current accounting interval ends on %s, in %s.");
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_sent */
- tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_rcvd */
+ tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_used */
tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
- /* format_local_iso_time uses local tz, just check mins and secs. */
- tt_ptr_op(strstr(va_arg(ap, char *), ":01:00"),
- OP_NE, NULL); /* end_buf */
+ tt_str_op(va_arg(ap, char *), OP_EQ, "max"); /* acc_rule */
+ /* format_local_iso_time uses local tz, so we can't just compare
+ * the string against a constant */
+ char datetime[ISO_TIME_LEN+1];
+ format_local_iso_time(datetime, 60);
+ tt_str_op(va_arg(ap, char *), OP_EQ, datetime); /* end_buf */
tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours"); /* remaining */
break;
case 2:
diff --git a/src/test/test_switch_id.c b/src/test/test_switch_id.c
new file mode 100644
index 0000000000..e12205bb2e
--- /dev/null
+++ b/src/test/test_switch_id.c
@@ -0,0 +1,192 @@
+/* Copyright (c) 2015-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+
+#ifdef HAVE_SYS_CAPABILITY_H
+#include <sys/capability.h>
+#endif
+
+#define TEST_BUILT_WITH_CAPS 0
+#define TEST_HAVE_CAPS 1
+#define TEST_ROOT_CAN_BIND_LOW 2
+#define TEST_SETUID 3
+#define TEST_SETUID_KEEPCAPS 4
+#define TEST_SETUID_STRICT 5
+
+static const struct {
+ const char *name;
+ int test_id;
+} which_test[] = {
+ { "built-with-caps", TEST_BUILT_WITH_CAPS },
+ { "have-caps", TEST_HAVE_CAPS },
+ { "root-bind-low", TEST_ROOT_CAN_BIND_LOW },
+ { "setuid", TEST_SETUID },
+ { "setuid-keepcaps", TEST_SETUID_KEEPCAPS },
+ { "setuid-strict", TEST_SETUID_STRICT },
+ { NULL, 0 }
+};
+
+#if !defined(_WIN32)
+/* 0 on no, 1 on yes, -1 on failure. */
+static int
+check_can_bind_low_ports(void)
+{
+ int port;
+ struct sockaddr_in sin;
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+
+ for (port = 600; port < 1024; ++port) {
+ sin.sin_port = htons(port);
+ tor_socket_t fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (! SOCKET_OK(fd)) {
+ perror("socket");
+ return -1;
+ }
+
+ int one = 1;
+ if (setsockopt(fd, SOL_SOCKET,SO_REUSEADDR, (void*)&one,
+ (socklen_t)sizeof(one))) {
+ perror("setsockopt");
+ tor_close_socket_simple(fd);
+ return -1;
+ }
+
+ int res = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
+ tor_close_socket_simple(fd);
+
+ if (res == 0) {
+ /* bind was successful */
+ return 1;
+ } else if (errno == EACCES || errno == EPERM) {
+ /* Got a permission-denied error. */
+ return 0;
+ } else if (errno == EADDRINUSE) {
+ /* Huh; somebody is using that port. */
+ } else {
+ perror("bind");
+ }
+ }
+
+ return -1;
+}
+#endif
+
+int
+main(int argc, char **argv)
+{
+#if defined(_WIN32)
+ (void) argc;
+ (void) argv;
+ (void) which_test;
+
+ fprintf(stderr, "This test is not supported on your OS.\n");
+ return 77;
+#else
+ const char *username;
+ const char *testname;
+ if (argc != 3) {
+ fprintf(stderr, "I want 2 arguments: a username and a command.\n");
+ return 1;
+ }
+ if (getuid() != 0) {
+ fprintf(stderr, "This test only works when it's run as root.\n");
+ return 1;
+ }
+ username = argv[1];
+ testname = argv[2];
+ int test_id = -1;
+ int i;
+ for (i = 0; which_test[i].name; ++i) {
+ if (!strcmp(which_test[i].name, testname)) {
+ test_id = which_test[i].test_id;
+ break;
+ }
+ }
+ if (test_id == -1) {
+ fprintf(stderr, "Unrecognized test '%s'\n", testname);
+ return 1;
+ }
+
+#ifdef HAVE_LINUX_CAPABILITIES
+ const int have_cap_support = 1;
+#else
+ const int have_cap_support = 0;
+#endif
+
+ int okay;
+
+ init_logging(1);
+ log_severity_list_t sev;
+ memset(&sev, 0, sizeof(sev));
+ set_log_severity_config(LOG_WARN, LOG_ERR, &sev);
+ add_stream_log(&sev, "", fileno(stderr));
+
+ switch (test_id)
+ {
+ case TEST_BUILT_WITH_CAPS:
+ /* Succeed if we were built with capability support. */
+ okay = have_cap_support;
+ break;
+ case TEST_HAVE_CAPS:
+ /* Succeed if "capabilities work" == "we were built with capability
+ * support." */
+ okay = have_cap_support == have_capability_support();
+ break;
+ case TEST_ROOT_CAN_BIND_LOW:
+ /* Succeed if root can bind low ports. */
+ okay = check_can_bind_low_ports() == 1;
+ break;
+ case TEST_SETUID:
+ /* Succeed if we can do a setuid with no capability retention, and doing
+ * so makes us lose the ability to bind low ports */
+ case TEST_SETUID_KEEPCAPS:
+ /* Succeed if we can do a setuid with capability retention, and doing so
+ * does not make us lose the ability to bind low ports */
+ {
+ int keepcaps = (test_id == TEST_SETUID_KEEPCAPS);
+ okay = switch_id(username, keepcaps ? SWITCH_ID_KEEP_BINDLOW : 0) == 0;
+ if (okay) {
+ okay = check_can_bind_low_ports() == keepcaps;
+ }
+ break;
+ }
+ case TEST_SETUID_STRICT:
+ /* Succeed if, after a setuid, we cannot setuid back, and we cannot
+ * re-grab any capabilities. */
+ okay = switch_id(username, SWITCH_ID_KEEP_BINDLOW) == 0;
+ if (okay) {
+ /* We'd better not be able to setuid back! */
+ if (setuid(0) == 0 || errno != EPERM) {
+ okay = 0;
+ }
+ }
+#ifdef HAVE_LINUX_CAPABILITIES
+ if (okay) {
+ cap_t caps = cap_get_proc();
+ const cap_value_t caplist[] = {
+ CAP_SETUID,
+ };
+ cap_set_flag(caps, CAP_PERMITTED, 1, caplist, CAP_SET);
+ if (cap_set_proc(caps) == 0 || errno != EPERM) {
+ okay = 0;
+ }
+ cap_free(caps);
+ }
+#endif
+ break;
+ default:
+ fprintf(stderr, "Unsupported test '%s'\n", testname);
+ okay = 0;
+ break;
+ }
+
+ if (!okay) {
+ fprintf(stderr, "Test %s failed!\n", testname);
+ }
+
+ return (okay ? 0 : 1);
+#endif
+}
+
diff --git a/src/test/test_switch_id.sh b/src/test/test_switch_id.sh
new file mode 100755
index 0000000000..79c44f2eb1
--- /dev/null
+++ b/src/test/test_switch_id.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+if test "`id -u`" != '0'; then
+ echo "This test only works when run as root. Skipping." >&2
+ exit 77
+fi
+
+if test "`id -u nobody`" = ""; then
+ echo "This test requires that your system have a 'nobody' user. Sorry." >&2
+ exit 1
+fi
+
+if test "$OVERRIDE_GCDA_PERMISSIONS_HACK" = "yes"; then
+ find src -type f -name '*gcda' -print0 | xargs -0 chmod 0666
+fi
+
+"${builddir:-.}/src/test/test-switch-id" nobody setuid || exit 1
+"${builddir:-.}/src/test/test-switch-id" nobody root-bind-low || exit 1
+"${builddir:-.}/src/test/test-switch-id" nobody setuid-strict || exit 1
+"${builddir:-.}/src/test/test-switch-id" nobody built-with-caps || exit 0
+# ... Go beyond this point only if we were built with capability support.
+
+"${builddir:-.}/src/test/test-switch-id" nobody have-caps || exit 1
+"${builddir:-.}/src/test/test-switch-id" nobody setuid-keepcaps || exit 1
+
+if test "$OVERRIDE_GCDA_PERMISSIONS_HACK" = "yes"; then
+ find src -type f -name '*gcda' -print0 | xargs -0 chmod 0644
+fi
+
+echo "All okay"
+
+exit 0
diff --git a/src/test/test_threads.c b/src/test/test_threads.c
index 35f5dc8ea3..ebbc95c7ca 100644
--- a/src/test/test_threads.c
+++ b/src/test/test_threads.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
@@ -73,6 +73,8 @@ thread_test_func_(void* _s)
++thread_fns_failed;
tor_mutex_release(thread_test_mutex_);
+ tor_free(mycount);
+
tor_mutex_release(m);
spawn_exit();
@@ -85,11 +87,6 @@ test_threads_basic(void *arg)
char *s1 = NULL, *s2 = NULL;
int done = 0, timedout = 0;
time_t started;
-#ifndef _WIN32
- struct timeval tv;
- tv.tv_sec=0;
- tv.tv_usec=100*1000;
-#endif
(void) arg;
tt_int_op(tor_threadlocal_init(&count), OP_EQ, 0);
@@ -118,10 +115,8 @@ test_threads_basic(void *arg)
timedout = done = 1;
}
tor_mutex_release(thread_test_mutex_);
-#ifndef _WIN32
/* Prevent the main thread from starving the worker threads. */
- select(0, NULL, NULL, NULL, &tv);
-#endif
+ tor_sleep_msec(10);
}
tor_mutex_acquire(thread_test_start1_);
tor_mutex_release(thread_test_start1_);
@@ -284,16 +279,7 @@ test_threads_conditionvar(void *arg)
if (!timeout) {
tt_int_op(ti->n_shutdown, ==, 4);
} else {
-#ifdef _WIN32
- Sleep(500); /* msec */
-#elif defined(HAVE_USLEEP)
- usleep(500*1000); /* usec */
-#else
- {
- struct tv = { 0, 500*1000 };
- select(0, NULL, NULL, NULL, &tv);
- }
-#endif
+ tor_sleep_msec(200);
tor_mutex_acquire(ti->mutex);
tt_int_op(ti->n_shutdown, ==, 2);
tt_int_op(ti->n_timeouts, ==, 2);
diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c
new file mode 100644
index 0000000000..47455cff83
--- /dev/null
+++ b/src/test/test_tortls.c
@@ -0,0 +1,2825 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define TORTLS_PRIVATE
+#define LOG_PRIVATE
+#include "orconfig.h"
+
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+#include <math.h>
+
+#include "compat.h"
+
+/* Some versions of OpenSSL declare SSL_get_selected_srtp_profile twice in
+ * srtp.h. Suppress the GCC warning so we can build with -Wredundant-decl. */
+DISABLE_GCC_WARNING(redundant-decls)
+
+#include <openssl/opensslv.h>
+
+#include <openssl/ssl.h>
+#include <openssl/ssl3.h>
+#include <openssl/err.h>
+#include <openssl/asn1t.h>
+#include <openssl/x509.h>
+#include <openssl/rsa.h>
+#include <openssl/evp.h>
+#include <openssl/bn.h>
+
+ENABLE_GCC_WARNING(redundant-decls)
+
+#include "or.h"
+#include "torlog.h"
+#include "config.h"
+#include "tortls.h"
+
+#include "test.h"
+#include "log_test_helpers.h"
+#define NS_MODULE tortls
+
+#ifndef HAVE_SSL_STATE
+#define OPENSSL_OPAQUE
+#endif
+
+#if defined(OPENSSL_OPAQUE) && !defined(LIBRESSL_VERSION_NUMBER)
+#define SSL_STATE_STR "before SSL initialization"
+#else
+#define SSL_STATE_STR "before/accept initialization"
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static SSL_METHOD *
+give_me_a_test_method(void)
+{
+ SSL_METHOD *method = tor_malloc_zero(sizeof(SSL_METHOD));
+ memcpy(method, TLSv1_method(), sizeof(SSL_METHOD));
+ return method;
+}
+
+static int
+fake_num_ciphers(void)
+{
+ return 0;
+}
+#endif
+
+static void
+test_tortls_errno_to_tls_error(void *data)
+{
+ (void) data;
+ tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNRESET)),OP_EQ,
+ TOR_TLS_ERROR_CONNRESET);
+ tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ETIMEDOUT)),OP_EQ,
+ TOR_TLS_ERROR_TIMEOUT);
+ tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(EHOSTUNREACH)),OP_EQ,
+ TOR_TLS_ERROR_NO_ROUTE);
+ tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ENETUNREACH)),OP_EQ,
+ TOR_TLS_ERROR_NO_ROUTE);
+ tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNREFUSED)),OP_EQ,
+ TOR_TLS_ERROR_CONNREFUSED);
+ tt_int_op(tor_errno_to_tls_error(0),OP_EQ,TOR_TLS_ERROR_MISC);
+ done:
+ (void)1;
+}
+
+static void
+test_tortls_err_to_string(void *data)
+{
+ (void) data;
+ tt_str_op(tor_tls_err_to_string(1),OP_EQ,"[Not an error.]");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_MISC),OP_EQ,"misc error");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_IO),OP_EQ,"unexpected close");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNREFUSED),OP_EQ,
+ "connection refused");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNRESET),OP_EQ,
+ "connection reset");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_NO_ROUTE),OP_EQ,
+ "host unreachable");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_TIMEOUT),OP_EQ,
+ "connection timed out");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_CLOSE),OP_EQ,"closed");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTREAD),OP_EQ,"want to read");
+ tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTWRITE),OP_EQ,"want to write");
+ tt_str_op(tor_tls_err_to_string(-100),OP_EQ,"(unknown error code)");
+ done:
+ (void)1;
+}
+
+static int
+mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
+{
+ (void) tls;
+ (void) cert; // XXXX look at this.
+ return 1;
+}
+
+static void
+test_tortls_tor_tls_new(void *data)
+{
+ (void) data;
+ MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
+ crypto_pk_t *key1 = NULL, *key2 = NULL;
+ SSL_METHOD *method = NULL;
+
+ key1 = pk_generate(2);
+ key2 = pk_generate(3);
+
+ tor_tls_t *tls = NULL;
+ tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
+ key1, key2, 86400), OP_EQ, 0);
+ tls = tor_tls_new(-1, 0);
+ tt_want(tls);
+ tor_tls_free(tls); tls = NULL;
+
+ SSL_CTX_free(client_tls_context->ctx);
+ client_tls_context->ctx = NULL;
+ tls = tor_tls_new(-1, 0);
+ tt_assert(!tls);
+
+#ifndef OPENSSL_OPAQUE
+ method = give_me_a_test_method();
+ SSL_CTX *ctx = SSL_CTX_new(method);
+ method->num_ciphers = fake_num_ciphers;
+ client_tls_context->ctx = ctx;
+ tls = tor_tls_new(-1, 0);
+ tt_assert(!tls);
+#endif
+
+ done:
+ UNMOCK(tor_tls_cert_matches_key);
+ crypto_pk_free(key1);
+ crypto_pk_free(key2);
+ tor_tls_free(tls);
+ tor_free(method);
+ tor_tls_free_all();
+}
+
+#define NS_MODULE tortls
+NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
+ const char *funcname, const char *suffix,
+ const char *format, va_list ap));
+
+static void
+NS(logv)(int severity, log_domain_mask_t domain,
+ const char *funcname, const char *suffix, const char *format,
+ va_list ap)
+{
+ (void) severity;
+ (void) domain;
+ (void) funcname;
+ (void) suffix;
+ (void) format;
+ (void) ap; // XXXX look at this.
+ CALLED(logv)++;
+}
+
+static void
+test_tortls_tor_tls_get_error(void *data)
+{
+ (void) data;
+ MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
+ crypto_pk_t *key1 = NULL, *key2 = NULL;
+ key1 = pk_generate(2);
+ key2 = pk_generate(3);
+
+ tor_tls_t *tls = NULL;
+ tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
+ key1, key2, 86400), OP_EQ, 0);
+ tls = tor_tls_new(-1, 0);
+ NS_MOCK(logv);
+ tt_int_op(CALLED(logv), OP_EQ, 0);
+ tor_tls_get_error(tls, 0, 0,
+ (const char *)"test", 0, 0);
+ tt_int_op(CALLED(logv), OP_EQ, 1);
+
+ done:
+ UNMOCK(tor_tls_cert_matches_key);
+ NS_UNMOCK(logv);
+ crypto_pk_free(key1);
+ crypto_pk_free(key2);
+ tor_tls_free(tls);
+}
+
+static void
+test_tortls_get_state_description(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ char *buf;
+ SSL_CTX *ctx;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(SSLv23_method());
+
+ buf = tor_malloc_zero(1000);
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tor_tls_get_state_description(NULL, buf, 20);
+ tt_str_op(buf, OP_EQ, "(No SSL object)");
+
+ SSL_free(tls->ssl);
+ tls->ssl = NULL;
+ tor_tls_get_state_description(tls, buf, 20);
+ tt_str_op(buf, OP_EQ, "(No SSL object)");
+
+ tls->ssl = SSL_new(ctx);
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in HANDSHAKE");
+
+ tls->state = TOR_TLS_ST_OPEN;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in OPEN");
+
+ tls->state = TOR_TLS_ST_GOTCLOSE;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in GOTCLOSE");
+
+ tls->state = TOR_TLS_ST_SENTCLOSE;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in SENTCLOSE");
+
+ tls->state = TOR_TLS_ST_CLOSED;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in CLOSED");
+
+ tls->state = TOR_TLS_ST_RENEGOTIATE;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in RENEGOTIATE");
+
+ tls->state = TOR_TLS_ST_BUFFEREVENT;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR);
+
+ tls->state = 7;
+ tor_tls_get_state_description(tls, buf, 200);
+ tt_str_op(buf, OP_EQ, SSL_STATE_STR " in unknown TLS state");
+
+ done:
+ SSL_CTX_free(ctx);
+ SSL_free(tls->ssl);
+ tor_free(buf);
+ tor_free(tls);
+}
+
+static void
+test_tortls_get_by_ssl(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ tor_tls_t *res;
+ SSL_CTX *ctx;
+ SSL *ssl;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+
+ ctx = SSL_CTX_new(SSLv23_method());
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->magic = TOR_TLS_MAGIC;
+
+ ssl = SSL_new(ctx);
+
+ res = tor_tls_get_by_ssl(ssl);
+ tt_assert(!res);
+
+ SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
+
+ res = tor_tls_get_by_ssl(ssl);
+ tt_assert(res == tls);
+
+ done:
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+ tor_free(tls);
+}
+
+static void
+test_tortls_allocate_tor_tls_object_ex_data_index(void *ignored)
+{
+ (void)ignored;
+ int first;
+
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+
+ first = tor_tls_object_ex_data_index;
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+ tt_int_op(first, OP_EQ, tor_tls_object_ex_data_index);
+
+ done:
+ (void)0;
+}
+
+static void
+test_tortls_log_one_error(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ SSL_CTX *ctx;
+ SSL *ssl = NULL;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(SSLv23_method());
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ setup_capture_of_logs(LOG_INFO);
+
+ tor_tls_log_one_error(NULL, 0, LOG_WARN, 0, "something");
+ expect_log_msg("TLS error while something: "
+ "(null) (in (null):(null):---)\n");
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
+ expect_log_msg("TLS error: (null) "
+ "(in (null):(null):---)\n");
+
+ mock_clean_saved_logs();
+ tls->address = tor_strdup("127.hello");
+ tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
+ expect_log_msg("TLS error with 127.hello: "
+ "(null) (in (null):(null):---)\n");
+ tor_free(tls->address);
+
+ mock_clean_saved_logs();
+ tls->address = tor_strdup("127.hello");
+ tor_tls_log_one_error(tls, 0, LOG_WARN, 0, "blarg");
+ expect_log_msg("TLS error while blarg with "
+ "127.hello: (null) (in (null):(null):---)\n");
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, 3), LOG_WARN, 0, NULL);
+ expect_log_msg("TLS error with 127.hello: "
+ "BN lib (in unknown library:(null):---)\n");
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTP_REQUEST),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTPS_PROXY_REQUEST),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_LENGTH_MISMATCH),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+
+#ifndef OPENSSL_1_1_API
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_TOO_LARGE),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+#endif
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNKNOWN_PROTOCOL),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNSUPPORTED_PROTOCOL),
+ LOG_WARN, 0, NULL);
+ expect_log_severity(LOG_INFO);
+
+ tls->ssl = SSL_new(ctx);
+
+ mock_clean_saved_logs();
+ tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
+ expect_log_msg("TLS error with 127.hello: (null)"
+ " (in (null):(null):" SSL_STATE_STR ")\n");
+
+ done:
+ teardown_capture_of_logs();
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+ if (tls && tls->ssl)
+ SSL_free(tls->ssl);
+ if (tls)
+ tor_free(tls->address);
+ tor_free(tls);
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_get_error(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ int ret;
+ SSL_CTX *ctx;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(SSLv23_method());
+ setup_capture_of_logs(LOG_INFO);
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = SSL_new(ctx);
+ SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
+
+ ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_IO);
+ expect_log_msg("TLS error: unexpected close while"
+ " something (before/accept initialization)\n");
+
+ mock_clean_saved_logs();
+ ret = tor_tls_get_error(tls, 2, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ expect_no_log_entry();
+
+ mock_clean_saved_logs();
+ ret = tor_tls_get_error(tls, 0, 1, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, -11);
+ expect_no_log_entry();
+
+ mock_clean_saved_logs();
+ ERR_clear_error();
+ ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
+ ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+ expect_log_msg("TLS error while something: (null)"
+ " (in bignum routines:(null):before/accept initialization)\n");
+
+ mock_clean_saved_logs();
+ ERR_clear_error();
+ tls->ssl->rwstate = SSL_READING;
+ SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
+ ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
+ expect_no_log_entry();
+
+ mock_clean_saved_logs();
+ ERR_clear_error();
+ tls->ssl->rwstate = SSL_READING;
+ SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
+ ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
+ expect_no_log_entry();
+
+ mock_clean_saved_logs();
+ ERR_clear_error();
+ tls->ssl->rwstate = 0;
+ tls->ssl->shutdown = SSL_RECEIVED_SHUTDOWN;
+ tls->ssl->s3->warn_alert =SSL_AD_CLOSE_NOTIFY;
+ ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
+ expect_log_entry();
+
+ mock_clean_saved_logs();
+ ret = tor_tls_get_error(tls, 0, 2, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, -10);
+ expect_no_log_entry();
+
+ mock_clean_saved_logs();
+ ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
+ ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
+ tt_int_op(ret, OP_EQ, -9);
+ expect_log_msg("TLS error while something: (null) (in system library:"
+ "connect:before/accept initialization)\n");
+
+ done:
+ teardown_capture_of_logs();
+ SSL_free(tls->ssl);
+ tor_free(tls);
+ SSL_CTX_free(ctx);
+}
+#endif
+
+static void
+test_tortls_always_accept_verify_cb(void *ignored)
+{
+ (void)ignored;
+ int ret;
+
+ ret = always_accept_verify_cb(0, NULL);
+ tt_int_op(ret, OP_EQ, 1);
+
+ done:
+ (void)0;
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_x509_cert_free(void *ignored)
+{
+ (void)ignored;
+ tor_x509_cert_t *cert;
+
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ tor_x509_cert_free(cert);
+
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ cert->cert = tor_malloc_zero(sizeof(X509));
+ cert->encoded = tor_malloc_zero(1);
+ tor_x509_cert_free(cert);
+}
+#endif
+
+static void
+test_tortls_x509_cert_get_id_digests(void *ignored)
+{
+ (void)ignored;
+ tor_x509_cert_t *cert;
+ common_digests_t *d;
+ const common_digests_t *res;
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ d = tor_malloc_zero(sizeof(common_digests_t));
+ d->d[0][0] = 42;
+
+ res = tor_x509_cert_get_id_digests(cert);
+ tt_assert(!res);
+
+ cert->pkey_digests_set = 1;
+ cert->pkey_digests = *d;
+ res = tor_x509_cert_get_id_digests(cert);
+ tt_int_op(res->d[0][0], OP_EQ, 42);
+
+ done:
+ tor_free(cert);
+ tor_free(d);
+}
+
+#ifndef OPENSSL_OPAQUE
+static int
+fixed_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
+{
+ (void) a; (void) b;
+ return 1;
+}
+
+static void
+fake_x509_free(X509 *cert)
+{
+ if (cert) {
+ if (cert->cert_info) {
+ if (cert->cert_info->key) {
+ if (cert->cert_info->key->pkey) {
+ tor_free(cert->cert_info->key->pkey);
+ }
+ tor_free(cert->cert_info->key);
+ }
+ tor_free(cert->cert_info);
+ }
+ tor_free(cert);
+ }
+}
+
+static void
+test_tortls_cert_matches_key(void *ignored)
+{
+ (void)ignored;
+ int res;
+ tor_tls_t *tls;
+ tor_x509_cert_t *cert;
+ X509 *one = NULL, *two = NULL;
+ EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_new(999, 0, NULL, NULL);
+ EVP_PKEY_asn1_set_public(meth, NULL, NULL, fixed_pub_cmp, NULL, NULL, NULL);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ one = tor_malloc_zero(sizeof(X509));
+ one->references = 1;
+ two = tor_malloc_zero(sizeof(X509));
+ two->references = 1;
+
+ res = tor_tls_cert_matches_key(tls, cert);
+ tt_int_op(res, OP_EQ, 0);
+
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+ tls->ssl->session->peer = one;
+ res = tor_tls_cert_matches_key(tls, cert);
+ tt_int_op(res, OP_EQ, 0);
+
+ cert->cert = two;
+ res = tor_tls_cert_matches_key(tls, cert);
+ tt_int_op(res, OP_EQ, 0);
+
+ one->cert_info = tor_malloc_zero(sizeof(X509_CINF));
+ one->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
+ one->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
+ one->cert_info->key->pkey->references = 1;
+ one->cert_info->key->pkey->ameth = meth;
+ one->cert_info->key->pkey->type = 1;
+
+ two->cert_info = tor_malloc_zero(sizeof(X509_CINF));
+ two->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
+ two->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
+ two->cert_info->key->pkey->references = 1;
+ two->cert_info->key->pkey->ameth = meth;
+ two->cert_info->key->pkey->type = 2;
+
+ res = tor_tls_cert_matches_key(tls, cert);
+ tt_int_op(res, OP_EQ, 0);
+
+ one->cert_info->key->pkey->type = 1;
+ two->cert_info->key->pkey->type = 1;
+ res = tor_tls_cert_matches_key(tls, cert);
+ tt_int_op(res, OP_EQ, 1);
+
+ done:
+ EVP_PKEY_asn1_free(meth);
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ tor_free(cert);
+ fake_x509_free(one);
+ fake_x509_free(two);
+}
+
+static void
+test_tortls_cert_get_key(void *ignored)
+{
+ (void)ignored;
+ tor_x509_cert_t *cert = NULL;
+ crypto_pk_t *res = NULL;
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ X509 *key = NULL;
+ key = tor_malloc_zero(sizeof(X509));
+ key->references = 1;
+
+ res = tor_tls_cert_get_key(cert);
+ tt_assert(!res);
+
+ cert->cert = key;
+ key->cert_info = tor_malloc_zero(sizeof(X509_CINF));
+ key->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
+ key->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
+ key->cert_info->key->pkey->references = 1;
+ key->cert_info->key->pkey->type = 2;
+ res = tor_tls_cert_get_key(cert);
+ tt_assert(!res);
+
+ done:
+ fake_x509_free(key);
+ tor_free(cert);
+ crypto_pk_free(res);
+}
+#endif
+
+static void
+test_tortls_get_my_client_auth_key(void *ignored)
+{
+ (void)ignored;
+ crypto_pk_t *ret;
+ crypto_pk_t *expected;
+ tor_tls_context_t *ctx;
+ RSA *k = RSA_new();
+
+ ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
+ expected = crypto_new_pk_from_rsa_(k);
+ ctx->auth_key = expected;
+
+ client_tls_context = NULL;
+ ret = tor_tls_get_my_client_auth_key();
+ tt_assert(!ret);
+
+ client_tls_context = ctx;
+ ret = tor_tls_get_my_client_auth_key();
+ tt_assert(ret == expected);
+
+ done:
+ RSA_free(k);
+ tor_free(expected);
+ tor_free(ctx);
+}
+
+static void
+test_tortls_get_my_certs(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_context_t *ctx;
+ const tor_x509_cert_t *link_cert_out = NULL;
+ const tor_x509_cert_t *id_cert_out = NULL;
+
+ ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
+
+ client_tls_context = NULL;
+ ret = tor_tls_get_my_certs(0, NULL, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ server_tls_context = NULL;
+ ret = tor_tls_get_my_certs(1, NULL, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ client_tls_context = ctx;
+ ret = tor_tls_get_my_certs(0, NULL, NULL);
+ tt_int_op(ret, OP_EQ, 0);
+
+ client_tls_context = ctx;
+ ret = tor_tls_get_my_certs(0, &link_cert_out, &id_cert_out);
+ tt_int_op(ret, OP_EQ, 0);
+
+ server_tls_context = ctx;
+ ret = tor_tls_get_my_certs(1, &link_cert_out, &id_cert_out);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ (void)1;
+}
+
+#ifndef HAVE_SSL_GET_CLIENT_CIPHERS
+static SSL_CIPHER *
+get_cipher_by_name(const char *name)
+{
+ int i;
+ const SSL_METHOD *method = SSLv23_method();
+ int num = method->num_ciphers();
+
+ for (i = 0; i < num; ++i) {
+ const SSL_CIPHER *cipher = method->get_cipher(i);
+ const char *ciphername = SSL_CIPHER_get_name(cipher);
+ if (!strcmp(ciphername, name)) {
+ return (SSL_CIPHER *)cipher;
+ }
+ }
+
+ return NULL;
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_get_ciphersuite_name(void *ignored)
+{
+ (void)ignored;
+ const char *ret;
+ tor_tls_t *ctx;
+ ctx = tor_malloc_zero(sizeof(tor_tls_t));
+ ctx->ssl = tor_malloc_zero(sizeof(SSL));
+
+ ret = tor_tls_get_ciphersuite_name(ctx);
+ tt_str_op(ret, OP_EQ, "(NONE)");
+
+ done:
+ tor_free(ctx->ssl);
+ tor_free(ctx);
+}
+
+static SSL_CIPHER *
+get_cipher_by_id(uint16_t id)
+{
+ int i;
+ const SSL_METHOD *method = SSLv23_method();
+ int num = method->num_ciphers();
+ for (i = 0; i < num; ++i) {
+ const SSL_CIPHER *cipher = method->get_cipher(i);
+ if (id == (SSL_CIPHER_get_id(cipher) & 0xffff)) {
+ return (SSL_CIPHER *)cipher;
+ }
+ }
+
+ return NULL;
+}
+
+static void
+test_tortls_classify_client_ciphers(void *ignored)
+{
+ (void)ignored;
+ int i;
+ int ret;
+ SSL_CTX *ctx;
+ SSL *ssl;
+ tor_tls_t *tls;
+ STACK_OF(SSL_CIPHER) *ciphers;
+ SSL_CIPHER *tmp_cipher;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->magic = TOR_TLS_MAGIC;
+
+ ctx = SSL_CTX_new(TLSv1_method());
+ ssl = SSL_new(ctx);
+ tls->ssl = ssl;
+
+ ciphers = sk_SSL_CIPHER_new_null();
+
+ ret = tor_tls_classify_client_ciphers(ssl, NULL);
+ tt_int_op(ret, OP_EQ, -1);
+
+ SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
+ tls->client_cipher_list_type = 42;
+
+ ret = tor_tls_classify_client_ciphers(ssl, NULL);
+ tt_int_op(ret, OP_EQ, 42);
+
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
+
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, SSL_get_ciphers(ssl));
+ tt_int_op(ret, OP_EQ, 3);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
+
+ SSL_CIPHER *one = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA),
+ *two = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_256_SHA),
+ *three = get_cipher_by_name(SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA),
+ *four = NULL;
+ sk_SSL_CIPHER_push(ciphers, one);
+ sk_SSL_CIPHER_push(ciphers, two);
+ sk_SSL_CIPHER_push(ciphers, three);
+ sk_SSL_CIPHER_push(ciphers, four);
+
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
+
+ sk_SSL_CIPHER_zero(ciphers);
+
+ one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
+ one->id = 0x00ff;
+ two = get_cipher_by_name("ECDHE-RSA-AES128-GCM-SHA256");
+ two->id = 0x0000;
+ sk_SSL_CIPHER_push(ciphers, one);
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 3);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
+
+ sk_SSL_CIPHER_push(ciphers, two);
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 3);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
+
+ one->id = 0xC00A;
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 3);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
+
+ sk_SSL_CIPHER_zero(ciphers);
+ for (i=0; v2_cipher_list[i]; i++) {
+ tmp_cipher = get_cipher_by_id(v2_cipher_list[i]);
+ tt_assert(tmp_cipher);
+ sk_SSL_CIPHER_push(ciphers, tmp_cipher);
+ }
+ tls->client_cipher_list_type = 0;
+ ret = tor_tls_classify_client_ciphers(ssl, ciphers);
+ tt_int_op(ret, OP_EQ, 2);
+ tt_int_op(tls->client_cipher_list_type, OP_EQ, 2);
+
+ done:
+ sk_SSL_CIPHER_free(ciphers);
+ SSL_free(tls->ssl);
+ tor_free(tls);
+ SSL_CTX_free(ctx);
+}
+#endif
+
+static void
+test_tortls_client_is_using_v2_ciphers(void *ignored)
+{
+ (void)ignored;
+
+#ifdef HAVE_SSL_GET_CLIENT_CIPHERS
+ tt_skip();
+ done:
+ (void)1;
+#else
+ int ret;
+ SSL_CTX *ctx;
+ SSL *ssl;
+ SSL_SESSION *sess;
+ STACK_OF(SSL_CIPHER) *ciphers;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(TLSv1_method());
+ ssl = SSL_new(ctx);
+ sess = SSL_SESSION_new();
+
+ ret = tor_tls_client_is_using_v2_ciphers(ssl);
+ tt_int_op(ret, OP_EQ, -1);
+
+ ssl->session = sess;
+ ret = tor_tls_client_is_using_v2_ciphers(ssl);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ciphers = sk_SSL_CIPHER_new_null();
+ SSL_CIPHER *one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
+ one->id = 0x00ff;
+ sk_SSL_CIPHER_push(ciphers, one);
+ sess->ciphers = ciphers;
+ ret = tor_tls_client_is_using_v2_ciphers(ssl);
+ tt_int_op(ret, OP_EQ, 1);
+ done:
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+#endif
+}
+
+#ifndef OPENSSL_OPAQUE
+static X509 *fixed_try_to_extract_certs_from_tls_cert_out_result = NULL;
+static X509 *fixed_try_to_extract_certs_from_tls_id_cert_out_result = NULL;
+
+static void
+fixed_try_to_extract_certs_from_tls(int severity, tor_tls_t *tls,
+ X509 **cert_out, X509 **id_cert_out)
+{
+ (void) severity;
+ (void) tls;
+ *cert_out = fixed_try_to_extract_certs_from_tls_cert_out_result;
+ *id_cert_out = fixed_try_to_extract_certs_from_tls_id_cert_out_result;
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static const char* notCompletelyValidCertString =
+ "-----BEGIN CERTIFICATE-----\n"
+ "MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n"
+ "A1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\n"
+ "MRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\n"
+ "YiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\n"
+ "ODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\n"
+ "CAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\n"
+ "ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI/ZdzD\n"
+ "+6Pktmurn+F2kQYbtc7XQh8/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\n"
+ "MwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq/4/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\n"
+ "C/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\n"
+ "kXCqKWpnZ/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\n"
+ "jC9UeuErhaA/zzWi8ewMTFZW/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\n"
+ "evnAhf0cwULaebn+lMs8Pdl7y37+sfluVok=\n"
+ "-----END CERTIFICATE-----\n";
+#endif
+
+static const char* validCertString = "-----BEGIN CERTIFICATE-----\n"
+ "MIIDpTCCAY0CAg3+MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNVBAYTAlVTMREwDwYD\n"
+ "VQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIGA1UECgwLVG9yIFRl\n"
+ "c3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkwNjEzMzk1OVoXDTQz\n"
+ "MDEyMjEzMzk1OVowVjELMAkGA1UEBhMCVVMxEDAOBgNVBAcMB0NoaWNhZ28xFDAS\n"
+ "BgNVBAoMC1RvciBUZXN0aW5nMR8wHQYDVQQDDBZ0ZXN0aW5nLnRvcnByb2plY3Qu\n"
+ "b3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDoT6uyVVhWyOF3wkHjjYbd\n"
+ "nKaykyRv4JVtKQdZ4OpEErmX1zw4MmyzpQNV6iR4bQnWiyLfzyVJMZDIC/WILBfX\n"
+ "w2Pza/yuLgUvDc3twMuhOACzOQVO8PrEF/aVv2+hbCCy2udXvKhnYn+CCXl3ozc8\n"
+ "XcKYvujTXDyvGWY3xwAjlQIDAQABMA0GCSqGSIb3DQEBBQUAA4ICAQCUvnhzQWuQ\n"
+ "MrN+pERkE+zcTI/9dGS90rUMMLgu8VDNqTa0TUQh8uO0EQ6uDvI8Js6e8tgwS0BR\n"
+ "UBahqb7ZHv+rejGCBr5OudqD+x4STiiuPNJVs86JTLN8SpM9CHjIBH5WCCN2KOy3\n"
+ "mevNoRcRRyYJzSFULCunIK6FGulszigMYGscrO4oiTkZiHPh9KvWT40IMiHfL+Lw\n"
+ "EtEWiLex6064LcA2YQ1AMuSZyCexks63lcfaFmQbkYOKqXa1oLkIRuDsOaSVjTfe\n"
+ "vec+X6jvf12cFTKS5WIeqkKF2Irt+dJoiHEGTe5RscUMN/f+gqHPzfFz5dR23sxo\n"
+ "g+HC6MZHlFkLAOx3wW6epPS8A/m1mw3zMPoTnb2U2YYt8T0dJMMlUn/7Y1sEAa+a\n"
+ "dSTMaeUf6VnJ//11m454EZl1to9Z7oJOgqmFffSrdD4BGIWe8f7hhW6L1Enmqe/J\n"
+ "BKL3wbzZh80O1W0bndAwhnEEhlzneFY84cbBo9pmVxpODHkUcStpr5Z7pBDrcL21\n"
+ "Ss/aB/1YrsVXhdvJdOGxl3Mnl9dUY57CympLGlT8f0pPS6GAKOelECOhFMHmJd8L\n"
+ "dj3XQSmKtYHevZ6IvuMXSlB/fJvSjSlkCuLo5+kJoaqPuRu+i/S1qxeRy3CBwmnE\n"
+ "LdSNdcX4N79GQJ996PA8+mUCQG7YRtK+WA==\n"
+ "-----END CERTIFICATE-----\n";
+
+static const char* caCertString = "-----BEGIN CERTIFICATE-----\n"
+ "MIIFjzCCA3egAwIBAgIJAKd5WgyfPMYRMA0GCSqGSIb3DQEBCwUAMF4xCzAJBgNV\n"
+ "BAYTAlVTMREwDwYDVQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIG\n"
+ "A1UECgwLVG9yIFRlc3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkw\n"
+ "NjEzMzc0MVoXDTQzMDEyMjEzMzc0MVowXjELMAkGA1UEBhMCVVMxETAPBgNVBAgM\n"
+ "CElsbGlub2lzMRAwDgYDVQQHDAdDaGljYWdvMRQwEgYDVQQKDAtUb3IgVGVzdGlu\n"
+ "ZzEUMBIGA1UEAwwLVG9yIFRlc3RpbmcwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\n"
+ "ggIKAoICAQCpLMUEiLW5leUgBZoEJms2V7lZRhIAjnJBhVMHD0e3UubNknmaQoxf\n"
+ "ARz3rvqOaRd0JlV+qM9qE0DjiYcCVP1cAfqAo9d83uS1vwY3YMVJzADlaIiHfyVW\n"
+ "uEgBy0vvkeUBqaua24dYlcwsemOiXYLu41yM1wkcGHW1AhBNHppY6cznb8TyLgNM\n"
+ "2x3SGUdzc5XMyAFx51faKGBA3wjs+Hg1PLY7d30nmCgEOBavpm5I1disM/0k+Mcy\n"
+ "YmAKEo/iHJX/rQzO4b9znP69juLlR8PDBUJEVIG/CYb6+uw8MjjUyiWXYoqfVmN2\n"
+ "hm/lH8b6rXw1a2Aa3VTeD0DxaWeacMYHY/i01fd5n7hCoDTRNdSw5KJ0L3Z0SKTu\n"
+ "0lzffKzDaIfyZGlpW5qdouACkWYzsaitQOePVE01PIdO30vUfzNTFDfy42ccx3Di\n"
+ "59UCu+IXB+eMtrBfsok0Qc63vtF1linJgjHW1z/8ujk8F7/qkOfODhk4l7wngc2A\n"
+ "EmwWFIFoGaiTEZHB9qteXr4unbXZ0AHpM02uGGwZEGohjFyebEb73M+J57WKKAFb\n"
+ "PqbLcGUksL1SHNBNAJcVLttX55sO4nbidOS/kA3m+F1R04MBTyQF9qA6YDDHqdI3\n"
+ "h/3pw0Z4fxVouTYT4/NfRnX4JTP4u+7Mpcoof28VME0qWqD1LnRhFQIDAQABo1Aw\n"
+ "TjAdBgNVHQ4EFgQUMoAgIXH7pZ3QMRwTjT+DM9Yo/v0wHwYDVR0jBBgwFoAUMoAg\n"
+ "IXH7pZ3QMRwTjT+DM9Yo/v0wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n"
+ "AgEAUJxacjXR9sT+Xs6ISFiUsyd0T6WVKMnV46xrYJHirGfx+krWHrjxMY+ZtxYD\n"
+ "DBDGlo11Qc4v6QrclNf5QUBfIiGQsP9Cm6hHcQ+Tpg9HHCgSqG1YNPwCPReCR4br\n"
+ "BLvLfrfkcBL2IWM0PdQdCze+59DBfipsULD2mEn9fjYRXQEwb2QWtQ9qRc20Yb/x\n"
+ "Q4b/+CvUodLkaq7B8MHz0BV8HHcBoph6DYaRmO/N+hPauIuSp6XyaGYcEefGKVKj\n"
+ "G2+fcsdyXsoijNdL8vNKwm4j2gVwCBnw16J00yfFoV46YcbfqEdJB2je0XSvwXqt\n"
+ "14AOTngxso2h9k9HLtrfpO1ZG/B5AcCMs1lzbZ2fp5DPHtjvvmvA2RJqgo3yjw4W\n"
+ "4DHAuTglYFlC3mDHNfNtcGP20JvepcQNzNP2UzwcpOc94hfKikOFw+gf9Vf1qd0y\n"
+ "h/Sk6OZHn2+JVUPiWHIQV98Vtoh4RmUZDJD+b55ia3fQGTGzt4z1XFzQYSva5sfs\n"
+ "wocS/papthqWldQU7x+3wofNd5CNU1x6WKXG/yw30IT/4F8ADJD6GeygNT8QJYvt\n"
+ "u/8lAkbOy6B9xGmSvr0Kk1oq9P2NshA6kalxp1Oz/DTNDdL4AeBXV3JmM6WWCjGn\n"
+ "Yy1RT69d0rwYc5u/vnqODz1IjvT90smsrkBumGt791FAFeg=\n"
+ "-----END CERTIFICATE-----\n";
+
+static X509 *
+read_cert_from(const char *str)
+{
+ BIO *bio = BIO_new(BIO_s_mem());
+ BIO_write(bio, str, (int) strlen(str));
+ X509 *res = PEM_read_bio_X509(bio, NULL, NULL, NULL);
+ BIO_free(bio);
+ return res;
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_verify(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ crypto_pk_t *k = NULL;
+ X509 *cert1 = NULL, *cert2 = NULL, *invalidCert = NULL,
+ *validCert = NULL, *caCert = NULL;
+
+ cert1 = tor_malloc_zero(sizeof(X509));
+ cert1->references = 10;
+
+ cert2 = tor_malloc_zero(sizeof(X509));
+ cert2->references = 10;
+
+ validCert = read_cert_from(validCertString);
+ caCert = read_cert_from(caCertString);
+ invalidCert = read_cert_from(notCompletelyValidCertString);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ ret = tor_tls_verify(LOG_WARN, tls, &k);
+ tt_int_op(ret, OP_EQ, -1);
+
+ MOCK(try_to_extract_certs_from_tls, fixed_try_to_extract_certs_from_tls);
+
+ fixed_try_to_extract_certs_from_tls_cert_out_result = cert1;
+ ret = tor_tls_verify(LOG_WARN, tls, &k);
+ tt_int_op(ret, OP_EQ, -1);
+
+ fixed_try_to_extract_certs_from_tls_id_cert_out_result = cert2;
+ ret = tor_tls_verify(LOG_WARN, tls, &k);
+ tt_int_op(ret, OP_EQ, -1);
+
+ fixed_try_to_extract_certs_from_tls_cert_out_result = invalidCert;
+ fixed_try_to_extract_certs_from_tls_id_cert_out_result = invalidCert;
+
+ ret = tor_tls_verify(LOG_WARN, tls, &k);
+ tt_int_op(ret, OP_EQ, -1);
+
+ fixed_try_to_extract_certs_from_tls_cert_out_result = validCert;
+ fixed_try_to_extract_certs_from_tls_id_cert_out_result = caCert;
+
+ ret = tor_tls_verify(LOG_WARN, tls, &k);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_assert(k);
+
+ done:
+ UNMOCK(try_to_extract_certs_from_tls);
+ tor_free(cert1);
+ tor_free(cert2);
+ tor_free(tls);
+ tor_free(k);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_check_lifetime(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ X509 *validCert = read_cert_from(validCertString);
+ time_t now = time(NULL);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+ tls->ssl->session->peer = validCert;
+ ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ASN1_STRING_free(validCert->cert_info->validity->notBefore);
+ validCert->cert_info->validity->notBefore = ASN1_TIME_set(NULL, now-10);
+ ASN1_STRING_free(validCert->cert_info->validity->notAfter);
+ validCert->cert_info->validity->notAfter = ASN1_TIME_set(NULL, now+60);
+
+ ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, -1000);
+ tt_int_op(ret, OP_EQ, -1);
+
+ ret = tor_tls_check_lifetime(LOG_WARN, tls, -1000, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ done:
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ X509_free(validCert);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static int fixed_ssl_pending_result = 0;
+
+static int
+fixed_ssl_pending(const SSL *ignored)
+{
+ (void)ignored;
+ return fixed_ssl_pending_result;
+}
+
+static void
+test_tortls_get_pending_bytes(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ SSL_METHOD *method;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ method = tor_malloc_zero(sizeof(SSL_METHOD));
+ method->ssl_pending = fixed_ssl_pending;
+ tls->ssl->method = method;
+
+ fixed_ssl_pending_result = 42;
+ ret = tor_tls_get_pending_bytes(tls);
+ tt_int_op(ret, OP_EQ, 42);
+
+ done:
+ tor_free(method);
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+static void
+test_tortls_get_forced_write_size(void *ignored)
+{
+ (void)ignored;
+ long ret;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tls->wantwrite_n = 43;
+ ret = tor_tls_get_forced_write_size(tls);
+ tt_int_op(ret, OP_EQ, 43);
+
+ done:
+ tor_free(tls);
+}
+
+static void
+test_tortls_get_write_overhead_ratio(void *ignored)
+{
+ (void)ignored;
+ double ret;
+
+ total_bytes_written_over_tls = 0;
+ ret = tls_get_write_overhead_ratio();
+ tt_double_op(fabs(ret - 1.0), OP_LT, 1E-12);
+
+ total_bytes_written_by_tls = 10;
+ total_bytes_written_over_tls = 1;
+ ret = tls_get_write_overhead_ratio();
+ tt_double_op(fabs(ret - 10.0), OP_LT, 1E-12);
+
+ total_bytes_written_by_tls = 10;
+ total_bytes_written_over_tls = 2;
+ ret = tls_get_write_overhead_ratio();
+ tt_double_op(fabs(ret - 5.0), OP_LT, 1E-12);
+
+ done:
+ (void)0;
+}
+
+static void
+test_tortls_used_v1_handshake(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ // These tests assume both V2 handshake server and client are enabled
+ tls->wasV2Handshake = 0;
+ ret = tor_tls_used_v1_handshake(tls);
+ tt_int_op(ret, OP_EQ, 1);
+
+ tls->wasV2Handshake = 1;
+ ret = tor_tls_used_v1_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ tor_free(tls);
+}
+
+static void
+test_tortls_get_num_server_handshakes(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tls->server_handshake_count = 3;
+ ret = tor_tls_get_num_server_handshakes(tls);
+ tt_int_op(ret, OP_EQ, 3);
+
+ done:
+ tor_free(tls);
+}
+
+static void
+test_tortls_server_got_renegotiate(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tls->got_renegotiate = 1;
+ ret = tor_tls_server_got_renegotiate(tls);
+ tt_int_op(ret, OP_EQ, 1);
+
+ done:
+ tor_free(tls);
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_SSL_SESSION_get_master_key(void *ignored)
+{
+ (void)ignored;
+ size_t ret;
+ tor_tls_t *tls;
+ uint8_t *out;
+ out = tor_malloc_zero(1);
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+ tls->ssl->session->master_key_length = 1;
+
+#ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
+ tls->ssl->session->master_key[0] = 43;
+ ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 0);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_int_op(out[0], OP_EQ, 0);
+
+ ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 1);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_int_op(out[0], OP_EQ, 43);
+
+ done:
+#endif
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ tor_free(out);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_get_tlssecrets(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ uint8_t *secret_out = tor_malloc_zero(DIGEST256_LEN);;
+ tor_tls_t *tls;
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+ tls->ssl->session->master_key_length = 1;
+ tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
+
+ ret = tor_tls_get_tlssecrets(tls, secret_out);
+ tt_int_op(ret, OP_EQ, 0);
+
+ done:
+ tor_free(secret_out);
+ tor_free(tls->ssl->s3);
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_get_buffer_sizes(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ size_t rbuf_c=-1, rbuf_b=-1, wbuf_c=-1, wbuf_b=-1;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
+
+ tls->ssl->s3->rbuf.buf = NULL;
+ tls->ssl->s3->rbuf.len = 1;
+ tls->ssl->s3->rbuf.offset = 0;
+ tls->ssl->s3->rbuf.left = 42;
+
+ tls->ssl->s3->wbuf.buf = NULL;
+ tls->ssl->s3->wbuf.len = 2;
+ tls->ssl->s3->wbuf.offset = 0;
+ tls->ssl->s3->wbuf.left = 43;
+
+ ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
+#if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0)
+ tt_int_op(ret, OP_EQ, -1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(rbuf_c, OP_EQ, 0);
+ tt_int_op(wbuf_c, OP_EQ, 0);
+ tt_int_op(rbuf_b, OP_EQ, 42);
+ tt_int_op(wbuf_b, OP_EQ, 43);
+
+ tls->ssl->s3->rbuf.buf = tor_malloc_zero(1);
+ tls->ssl->s3->wbuf.buf = tor_malloc_zero(1);
+ ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(rbuf_c, OP_EQ, 1);
+ tt_int_op(wbuf_c, OP_EQ, 2);
+
+#endif
+
+ done:
+ tor_free(tls->ssl->s3->rbuf.buf);
+ tor_free(tls->ssl->s3->wbuf.buf);
+ tor_free(tls->ssl->s3);
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+static void
+test_tortls_evaluate_ecgroup_for_tls(void *ignored)
+{
+ (void)ignored;
+ int ret;
+
+ ret = evaluate_ecgroup_for_tls(NULL);
+ tt_int_op(ret, OP_EQ, 1);
+
+ ret = evaluate_ecgroup_for_tls("foobar");
+ tt_int_op(ret, OP_EQ, 0);
+
+ ret = evaluate_ecgroup_for_tls("P256");
+ tt_int_op(ret, OP_EQ, 1);
+
+ ret = evaluate_ecgroup_for_tls("P224");
+ // tt_int_op(ret, OP_EQ, 1); This varies between machines
+
+ done:
+ (void)0;
+}
+
+#ifndef OPENSSL_OPAQUE
+typedef struct cert_pkey_st_local
+{
+ X509 *x509;
+ EVP_PKEY *privatekey;
+ const EVP_MD *digest;
+} CERT_PKEY_local;
+
+typedef struct sess_cert_st_local
+{
+ STACK_OF(X509) *cert_chain;
+ int peer_cert_type;
+ CERT_PKEY_local *peer_key;
+ CERT_PKEY_local peer_pkeys[8];
+ int references;
+} SESS_CERT_local;
+
+static void
+test_tortls_try_to_extract_certs_from_tls(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ X509 *cert = NULL, *id_cert = NULL, *c1 = NULL, *c2 = NULL;
+ SESS_CERT_local *sess = NULL;
+
+ c1 = read_cert_from(validCertString);
+ c2 = read_cert_from(caCertString);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+ sess = tor_malloc_zero(sizeof(SESS_CERT_local));
+ tls->ssl->session->sess_cert = (void *)sess;
+
+ try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
+ tt_assert(!cert);
+ tt_assert(!id_cert);
+
+ tls->ssl->session->peer = c1;
+ try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
+ tt_assert(cert == c1);
+ tt_assert(!id_cert);
+ X509_free(cert); /* decrease refcnt */
+
+ sess->cert_chain = sk_X509_new_null();
+ try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
+ tt_assert(cert == c1);
+ tt_assert(!id_cert);
+ X509_free(cert); /* decrease refcnt */
+
+ sk_X509_push(sess->cert_chain, c1);
+ sk_X509_push(sess->cert_chain, c2);
+
+ try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
+ tt_assert(cert == c1);
+ tt_assert(id_cert);
+ X509_free(cert); /* decrease refcnt */
+
+ done:
+ sk_X509_free(sess->cert_chain);
+ tor_free(sess);
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ X509_free(c1);
+ X509_free(c2);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_get_peer_cert(void *ignored)
+{
+ (void)ignored;
+ tor_x509_cert_t *ret;
+ tor_tls_t *tls;
+ X509 *cert = NULL;
+
+ cert = read_cert_from(validCertString);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+
+ ret = tor_tls_get_peer_cert(tls);
+ tt_assert(!ret);
+
+ tls->ssl->session->peer = cert;
+ ret = tor_tls_get_peer_cert(tls);
+ tt_assert(ret);
+ tt_assert(ret->cert == cert);
+
+ done:
+ tor_x509_cert_free(ret);
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ X509_free(cert);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_peer_has_cert(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ X509 *cert = NULL;
+
+ cert = read_cert_from(validCertString);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
+
+ ret = tor_tls_peer_has_cert(tls);
+ tt_assert(!ret);
+
+ tls->ssl->session->peer = cert;
+ ret = tor_tls_peer_has_cert(tls);
+ tt_assert(ret);
+
+ done:
+ tor_free(tls->ssl->session);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ X509_free(cert);
+}
+#endif
+
+static void
+test_tortls_is_server(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ int ret;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->isServer = 1;
+ ret = tor_tls_is_server(tls);
+ tt_int_op(ret, OP_EQ, 1);
+
+ done:
+ tor_free(tls);
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_session_secret_cb(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ SSL_CTX *ctx;
+ STACK_OF(SSL_CIPHER) *ciphers = NULL;
+ SSL_CIPHER *one;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tls->magic = TOR_TLS_MAGIC;
+
+ ctx = SSL_CTX_new(TLSv1_method());
+ tls->ssl = SSL_new(ctx);
+ SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
+
+ SSL_set_session_secret_cb(tls->ssl, tor_tls_session_secret_cb, NULL);
+
+ tor_tls_session_secret_cb(tls->ssl, NULL, NULL, NULL, NULL, NULL);
+ tt_assert(!tls->ssl->tls_session_secret_cb);
+
+ one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
+ one->id = 0x00ff;
+ ciphers = sk_SSL_CIPHER_new_null();
+ sk_SSL_CIPHER_push(ciphers, one);
+
+ tls->client_cipher_list_type = 0;
+ tor_tls_session_secret_cb(tls->ssl, NULL, NULL, ciphers, NULL, NULL);
+ tt_assert(!tls->ssl->tls_session_secret_cb);
+
+ done:
+ sk_SSL_CIPHER_free(ciphers);
+ SSL_free(tls->ssl);
+ SSL_CTX_free(ctx);
+ tor_free(tls);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+/* TODO: It seems block_renegotiation and unblock_renegotiation and
+ * using different blags. This might not be correct */
+static void
+test_tortls_block_renegotiation(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
+#ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
+#define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
+#endif
+
+ tls->ssl->s3->flags = SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
+
+ tor_tls_block_renegotiation(tls);
+
+#ifndef OPENSSL_1_1_API
+ tt_assert(!(tls->ssl->s3->flags &
+ SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION));
+#endif
+
+ done:
+ tor_free(tls->ssl->s3);
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+
+static void
+test_tortls_unblock_renegotiation(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tor_tls_unblock_renegotiation(tls);
+
+ tt_uint_op(SSL_get_options(tls->ssl) &
+ SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, OP_EQ,
+ SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
+
+ done:
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_assert_renegotiation_unblocked(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tor_tls_unblock_renegotiation(tls);
+ tor_tls_assert_renegotiation_unblocked(tls);
+ /* No assertion here - this test will fail if tor_assert is turned on
+ * and things are bad. */
+
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+static void
+test_tortls_set_logged_address(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+
+ tor_tls_set_logged_address(tls, "foo bar");
+
+ tt_str_op(tls->address, OP_EQ, "foo bar");
+
+ tor_tls_set_logged_address(tls, "foo bar 2");
+ tt_str_op(tls->address, OP_EQ, "foo bar 2");
+
+ done:
+ tor_free(tls->address);
+ tor_free(tls);
+}
+
+#ifndef OPENSSL_OPAQUE
+static void
+example_cb(tor_tls_t *t, void *arg)
+{
+ (void)t;
+ (void)arg;
+}
+
+static void
+test_tortls_set_renegotiate_callback(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ const char *arg = "hello";
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+
+ tor_tls_set_renegotiate_callback(tls, example_cb, (void*)arg);
+ tt_assert(tls->negotiated_callback == example_cb);
+ tt_assert(tls->callback_arg == arg);
+ tt_assert(!tls->got_renegotiate);
+
+ /* Assumes V2_HANDSHAKE_SERVER */
+ tt_assert(tls->ssl->info_callback == tor_tls_server_info_callback);
+
+ tor_tls_set_renegotiate_callback(tls, NULL, (void*)arg);
+ tt_assert(tls->ssl->info_callback == tor_tls_debug_state_callback);
+
+ done:
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static SSL_CIPHER *fixed_cipher1 = NULL;
+static SSL_CIPHER *fixed_cipher2 = NULL;
+static const SSL_CIPHER *
+fake_get_cipher(unsigned ncipher)
+{
+
+ switch (ncipher) {
+ case 1:
+ return fixed_cipher1;
+ case 2:
+ return fixed_cipher2;
+ default:
+ return NULL;
+ }
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_find_cipher_by_id(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ SSL *ssl;
+ SSL_CTX *ctx;
+ const SSL_METHOD *m = TLSv1_method();
+ SSL_METHOD *empty_method = tor_malloc_zero(sizeof(SSL_METHOD));
+
+ fixed_cipher1 = tor_malloc_zero(sizeof(SSL_CIPHER));
+ fixed_cipher2 = tor_malloc_zero(sizeof(SSL_CIPHER));
+ fixed_cipher2->id = 0xC00A;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(m);
+ ssl = SSL_new(ctx);
+
+ ret = find_cipher_by_id(ssl, NULL, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ ret = find_cipher_by_id(ssl, m, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ ret = find_cipher_by_id(ssl, m, 0xFFFF);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ ret = find_cipher_by_id(ssl, empty_method, 0xFFFF);
+#ifdef HAVE_SSL_CIPHER_FIND
+ tt_int_op(ret, OP_EQ, 0);
+#else
+ tt_int_op(ret, OP_EQ, 1);
+#endif
+
+ empty_method->get_cipher = fake_get_cipher;
+ ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ empty_method->get_cipher = m->get_cipher;
+ empty_method->num_ciphers = m->num_ciphers;
+ ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ empty_method->get_cipher = fake_get_cipher;
+ empty_method->num_ciphers = m->num_ciphers;
+ ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
+ tt_int_op(ret, OP_EQ, 1);
+
+ empty_method->num_ciphers = fake_num_ciphers;
+ ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
+#ifdef HAVE_SSL_CIPHER_FIND
+ tt_int_op(ret, OP_EQ, 1);
+#else
+ tt_int_op(ret, OP_EQ, 0);
+#endif
+
+ done:
+ tor_free(empty_method);
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+ tor_free(fixed_cipher1);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_debug_state_callback(void *ignored)
+{
+ (void)ignored;
+ SSL *ssl;
+ char *buf = tor_malloc_zero(1000);
+ int n;
+
+ setup_capture_of_logs(LOG_DEBUG);
+
+ ssl = tor_malloc_zero(sizeof(SSL));
+
+ tor_tls_debug_state_callback(ssl, 32, 45);
+
+ n = tor_snprintf(buf, 1000, "SSL %p is now in state unknown"
+ " state [type=32,val=45].\n", ssl);
+ /* tor's snprintf returns -1 on error */
+ tt_int_op(n, OP_NE, -1);
+ expect_log_msg(buf);
+
+ done:
+ teardown_capture_of_logs();
+ tor_free(buf);
+ tor_free(ssl);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_server_info_callback(void *ignored)
+{
+ (void)ignored;
+ tor_tls_t *tls;
+ SSL_CTX *ctx;
+ SSL *ssl;
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(TLSv1_method());
+ ssl = SSL_new(ctx);
+
+ tor_tls_allocate_tor_tls_object_ex_data_index();
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->magic = TOR_TLS_MAGIC;
+ tls->ssl = ssl;
+
+ setup_full_capture_of_logs(LOG_WARN);
+ SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_A);
+ mock_clean_saved_logs();
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
+
+ SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
+ mock_clean_saved_logs();
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
+
+ SSL_set_state(ssl, 99);
+ mock_clean_saved_logs();
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ expect_no_log_entry();
+ teardown_capture_of_logs();
+
+ SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
+ SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
+ tls->negotiated_callback = 0;
+ tls->server_handshake_count = 120;
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ tt_int_op(tls->server_handshake_count, OP_EQ, 121);
+
+ tls->server_handshake_count = 127;
+ tls->negotiated_callback = (void *)1;
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ tt_int_op(tls->server_handshake_count, OP_EQ, 127);
+ tt_int_op(tls->got_renegotiate, OP_EQ, 1);
+
+ tls->ssl->session = SSL_SESSION_new();
+ tls->wasV2Handshake = 0;
+ tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
+ tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
+
+ done:
+ teardown_capture_of_logs();
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+ tor_free(tls);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static int fixed_ssl_read_result_index;
+static int fixed_ssl_read_result[5];
+static int fixed_ssl_shutdown_result;
+
+static int
+fixed_ssl_read(SSL *s, void *buf, int len)
+{
+ (void)s;
+ (void)buf;
+ (void)len;
+ return fixed_ssl_read_result[fixed_ssl_read_result_index++];
+}
+
+static int
+fixed_ssl_shutdown(SSL *s)
+{
+ (void)s;
+ return fixed_ssl_shutdown_result;
+}
+
+#ifndef LIBRESSL_VERSION_NUMBER
+static int fixed_ssl_state_to_set;
+static tor_tls_t *fixed_tls;
+
+static int
+setting_version_ssl_shutdown(SSL *s)
+{
+ s->version = SSL2_VERSION;
+ return fixed_ssl_shutdown_result;
+}
+
+static int
+setting_version_and_state_ssl_shutdown(SSL *s)
+{
+ fixed_tls->state = fixed_ssl_state_to_set;
+ s->version = SSL2_VERSION;
+ return fixed_ssl_shutdown_result;
+}
+#endif
+
+static int
+dummy_handshake_func(SSL *s)
+{
+ (void)s;
+ return 1;
+}
+
+static void
+test_tortls_shutdown(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ SSL_METHOD *method = give_me_a_test_method();
+ setup_capture_of_logs(LOG_WARN);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->ssl->method = method;
+ method->ssl_read = fixed_ssl_read;
+ method->ssl_shutdown = fixed_ssl_shutdown;
+
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, -9);
+
+ tls->state = TOR_TLS_ST_SENTCLOSE;
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = -1;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, -9);
+
+#ifndef LIBRESSL_VERSION_NUMBER
+ tls->ssl->handshake_func = dummy_handshake_func;
+
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = 42;
+ fixed_ssl_read_result[2] = 0;
+ fixed_ssl_shutdown_result = 1;
+ ERR_clear_error();
+ tls->ssl->version = SSL2_VERSION;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
+ tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
+
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = 42;
+ fixed_ssl_read_result[2] = 0;
+ fixed_ssl_shutdown_result = 0;
+ ERR_clear_error();
+ tls->ssl->version = 0;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
+ tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
+
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = 42;
+ fixed_ssl_read_result[2] = 0;
+ fixed_ssl_shutdown_result = 0;
+ ERR_clear_error();
+ tls->ssl->version = 0;
+ method->ssl_shutdown = setting_version_ssl_shutdown;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = 42;
+ fixed_ssl_read_result[2] = 0;
+ fixed_ssl_shutdown_result = 0;
+ fixed_tls = tls;
+ fixed_ssl_state_to_set = TOR_TLS_ST_GOTCLOSE;
+ ERR_clear_error();
+ tls->ssl->version = 0;
+ method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 10;
+ fixed_ssl_read_result[1] = 42;
+ fixed_ssl_read_result[2] = 0;
+ fixed_ssl_read_result[3] = -1;
+ fixed_ssl_shutdown_result = 0;
+ fixed_tls = tls;
+ fixed_ssl_state_to_set = 0;
+ ERR_clear_error();
+ tls->ssl->version = 0;
+ method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
+ ret = tor_tls_shutdown(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+#endif
+
+ done:
+ teardown_capture_of_logs();
+ tor_free(method);
+ tor_free(tls->ssl);
+ tor_free(tls);
+}
+
+static int negotiated_callback_called;
+
+static void
+negotiated_callback_setter(tor_tls_t *t, void *arg)
+{
+ (void)t;
+ (void)arg;
+ negotiated_callback_called++;
+}
+
+static void
+test_tortls_read(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ char buf[100];
+ SSL_METHOD *method = give_me_a_test_method();
+ setup_capture_of_logs(LOG_WARN);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->state = TOR_TLS_ST_OPEN;
+
+ ret = tor_tls_read(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, -9);
+
+ /* These tests assume that V2_HANDSHAKE_SERVER is set */
+ tls->ssl->handshake_func = dummy_handshake_func;
+ tls->ssl->method = method;
+ method->ssl_read = fixed_ssl_read;
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 42;
+ tls->state = TOR_TLS_ST_OPEN;
+ ERR_clear_error();
+ ret = tor_tls_read(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, 42);
+
+ tls->state = TOR_TLS_ST_OPEN;
+ tls->got_renegotiate = 1;
+ fixed_ssl_read_result_index = 0;
+ ERR_clear_error();
+ ret = tor_tls_read(tls, buf, 10);
+ tt_int_op(tls->got_renegotiate, OP_EQ, 0);
+
+ tls->state = TOR_TLS_ST_OPEN;
+ tls->got_renegotiate = 1;
+ negotiated_callback_called = 0;
+ tls->negotiated_callback = negotiated_callback_setter;
+ fixed_ssl_read_result_index = 0;
+ ERR_clear_error();
+ ret = tor_tls_read(tls, buf, 10);
+ tt_int_op(negotiated_callback_called, OP_EQ, 1);
+
+#ifndef LIBRESSL_VERSION_NUMBER
+ fixed_ssl_read_result_index = 0;
+ fixed_ssl_read_result[0] = 0;
+ tls->ssl->version = SSL2_VERSION;
+ ERR_clear_error();
+ ret = tor_tls_read(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
+ tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
+#endif
+ // TODO: fill up
+
+ done:
+ teardown_capture_of_logs();
+ tor_free(tls->ssl);
+ tor_free(tls);
+ tor_free(method);
+}
+
+static int fixed_ssl_write_result;
+
+static int
+fixed_ssl_write(SSL *s, const void *buf, int len)
+{
+ (void)s;
+ (void)buf;
+ (void)len;
+ return fixed_ssl_write_result;
+}
+
+static void
+test_tortls_write(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ SSL_METHOD *method = give_me_a_test_method();
+ char buf[100];
+ setup_capture_of_logs(LOG_WARN);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = tor_malloc_zero(sizeof(SSL));
+ tls->state = TOR_TLS_ST_OPEN;
+
+ ret = tor_tls_write(tls, buf, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ ret = tor_tls_write(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, -9);
+
+ tls->ssl->method = method;
+ tls->wantwrite_n = 1;
+ ret = tor_tls_write(tls, buf, 10);
+ tt_int_op(tls->wantwrite_n, OP_EQ, 0);
+
+ method->ssl_write = fixed_ssl_write;
+ tls->ssl->handshake_func = dummy_handshake_func;
+ fixed_ssl_write_result = 1;
+ ERR_clear_error();
+ ret = tor_tls_write(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, 1);
+
+ fixed_ssl_write_result = -1;
+ ERR_clear_error();
+ tls->ssl->rwstate = SSL_READING;
+ SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
+ SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
+ ret = tor_tls_write(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
+
+ ERR_clear_error();
+ tls->ssl->rwstate = SSL_READING;
+ SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
+ SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
+ ret = tor_tls_write(tls, buf, 10);
+ tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
+
+ done:
+ teardown_capture_of_logs();
+ BIO_free(tls->ssl->rbio);
+ tor_free(tls->ssl);
+ tor_free(tls);
+ tor_free(method);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static int fixed_ssl_accept_result;
+static int fixed_ssl_connect_result;
+
+static int
+setting_error_ssl_accept(SSL *ssl)
+{
+ (void)ssl;
+ ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
+ ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
+ return fixed_ssl_accept_result;
+}
+
+static int
+setting_error_ssl_connect(SSL *ssl)
+{
+ (void)ssl;
+ ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
+ ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
+ return fixed_ssl_connect_result;
+}
+
+static int
+fixed_ssl_accept(SSL *ssl)
+{
+ (void) ssl;
+ return fixed_ssl_accept_result;
+}
+
+static void
+test_tortls_handshake(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ SSL_CTX *ctx;
+ SSL_METHOD *method = give_me_a_test_method();
+ setup_capture_of_logs(LOG_INFO);
+
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ ctx = SSL_CTX_new(TLSv1_method());
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = SSL_new(ctx);
+ tls->state = TOR_TLS_ST_HANDSHAKE;
+
+ ret = tor_tls_handshake(tls);
+ tt_int_op(ret, OP_EQ, -9);
+
+ tls->isServer = 1;
+ tls->state = TOR_TLS_ST_HANDSHAKE;
+ ret = tor_tls_handshake(tls);
+ tt_int_op(ret, OP_EQ, -9);
+
+ tls->ssl->method = method;
+ method->ssl_accept = fixed_ssl_accept;
+ fixed_ssl_accept_result = 2;
+ ERR_clear_error();
+ tls->state = TOR_TLS_ST_HANDSHAKE;
+ ret = tor_tls_handshake(tls);
+ tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_OPEN);
+
+ method->ssl_accept = setting_error_ssl_accept;
+ fixed_ssl_accept_result = 1;
+ ERR_clear_error();
+ mock_clean_saved_logs();
+ tls->state = TOR_TLS_ST_HANDSHAKE;
+ ret = tor_tls_handshake(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+ expect_log_entry();
+ /* This fails on jessie. Investigate why! */
+#if 0
+ expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
+ "(null):SSLv3 write client hello B)\n");
+ expect_log_msg("TLS error while handshaking: (null) (in system library:"
+ "connect:SSLv3 write client hello B)\n");
+#endif
+ expect_log_severity(LOG_INFO);
+
+ tls->isServer = 0;
+ method->ssl_connect = setting_error_ssl_connect;
+ fixed_ssl_connect_result = 1;
+ ERR_clear_error();
+ mock_clean_saved_logs();
+ tls->state = TOR_TLS_ST_HANDSHAKE;
+ ret = tor_tls_handshake(tls);
+ tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
+ expect_log_entry();
+#if 0
+ /* See above */
+ expect_log_msg("TLS error while handshaking: "
+ "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
+ expect_log_msg("TLS error while handshaking: "
+ "(null) (in system library:connect:SSLv3 write client hello B)\n");
+#endif
+ expect_log_severity(LOG_WARN);
+
+ done:
+ teardown_capture_of_logs();
+ SSL_free(tls->ssl);
+ SSL_CTX_free(ctx);
+ tor_free(tls);
+ tor_free(method);
+}
+#endif
+
+#ifndef OPENSSL_OPAQUE
+static void
+test_tortls_finish_handshake(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_t *tls;
+ SSL_CTX *ctx;
+ SSL_METHOD *method = give_me_a_test_method();
+ SSL_library_init();
+ SSL_load_error_strings();
+
+ X509 *c1 = read_cert_from(validCertString);
+ SESS_CERT_local *sess = NULL;
+
+ ctx = SSL_CTX_new(method);
+
+ tls = tor_malloc_zero(sizeof(tor_tls_t));
+ tls->ssl = SSL_new(ctx);
+ tls->state = TOR_TLS_ST_OPEN;
+
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+
+ tls->isServer = 1;
+ tls->wasV2Handshake = 0;
+ setup_full_capture_of_logs(LOG_WARN);
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
+ expect_single_log_msg_containing("For some reason, wasV2Handshake didn't "
+ "get set.");
+ teardown_capture_of_logs();
+
+ tls->wasV2Handshake = 1;
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
+
+ tls->wasV2Handshake = 1;
+ tls->ssl->session = SSL_SESSION_new();
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
+
+ tls->isServer = 0;
+
+ sess = tor_malloc_zero(sizeof(SESS_CERT_local));
+ tls->ssl->session->sess_cert = (void *)sess;
+ sess->cert_chain = sk_X509_new_null();
+ sk_X509_push(sess->cert_chain, c1);
+ tls->ssl->session->peer = c1;
+ tls->wasV2Handshake = 0;
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, 0);
+ tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
+
+ method->num_ciphers = fake_num_ciphers;
+ ret = tor_tls_finish_handshake(tls);
+ tt_int_op(ret, OP_EQ, -9);
+
+ done:
+ if (sess)
+ sk_X509_free(sess->cert_chain);
+ if (tls->ssl && tls->ssl->session) {
+ tor_free(tls->ssl->session->sess_cert);
+ }
+ SSL_free(tls->ssl);
+ tor_free(tls);
+ SSL_CTX_free(ctx);
+ tor_free(method);
+ teardown_capture_of_logs();
+}
+#endif
+
+static int fixed_crypto_pk_new_result_index;
+static crypto_pk_t *fixed_crypto_pk_new_result[5];
+
+static crypto_pk_t *
+fixed_crypto_pk_new(void)
+{
+ return fixed_crypto_pk_new_result[fixed_crypto_pk_new_result_index++];
+}
+
+#ifndef OPENSSL_OPAQUE
+static int fixed_crypto_pk_generate_key_with_bits_result_index;
+static int fixed_crypto_pk_generate_key_with_bits_result[5];
+static int fixed_tor_tls_create_certificate_result_index;
+static X509 *fixed_tor_tls_create_certificate_result[5];
+static int fixed_tor_x509_cert_new_result_index;
+static tor_x509_cert_t *fixed_tor_x509_cert_new_result[5];
+
+static int
+fixed_crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
+{
+ (void)env;
+ (void)bits;
+ return fixed_crypto_pk_generate_key_with_bits_result[
+ fixed_crypto_pk_generate_key_with_bits_result_index++];
+}
+
+static X509 *
+fixed_tor_tls_create_certificate(crypto_pk_t *rsa,
+ crypto_pk_t *rsa_sign,
+ const char *cname,
+ const char *cname_sign,
+ unsigned int cert_lifetime)
+{
+ (void)rsa;
+ (void)rsa_sign;
+ (void)cname;
+ (void)cname_sign;
+ (void)cert_lifetime;
+ return fixed_tor_tls_create_certificate_result[
+ fixed_tor_tls_create_certificate_result_index++];
+}
+
+static tor_x509_cert_t *
+fixed_tor_x509_cert_new(X509 *x509_cert)
+{
+ (void) x509_cert;
+ return fixed_tor_x509_cert_new_result[
+ fixed_tor_x509_cert_new_result_index++];
+}
+
+static void
+test_tortls_context_new(void *ignored)
+{
+ (void)ignored;
+ tor_tls_context_t *ret;
+ crypto_pk_t *pk1, *pk2, *pk3, *pk4, *pk5, *pk6, *pk7, *pk8, *pk9, *pk10,
+ *pk11, *pk12, *pk13, *pk14, *pk15, *pk16, *pk17, *pk18;
+
+ pk1 = crypto_pk_new();
+ pk2 = crypto_pk_new();
+ pk3 = crypto_pk_new();
+ pk4 = crypto_pk_new();
+ pk5 = crypto_pk_new();
+ pk6 = crypto_pk_new();
+ pk7 = crypto_pk_new();
+ pk8 = crypto_pk_new();
+ pk9 = crypto_pk_new();
+ pk10 = crypto_pk_new();
+ pk11 = crypto_pk_new();
+ pk12 = crypto_pk_new();
+ pk13 = crypto_pk_new();
+ pk14 = crypto_pk_new();
+ pk15 = crypto_pk_new();
+ pk16 = crypto_pk_new();
+ pk17 = crypto_pk_new();
+ pk18 = crypto_pk_new();
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = NULL;
+ MOCK(crypto_pk_new, fixed_crypto_pk_new);
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ /* note: we already override this in testing_common.c, so we
+ * run this unit test in a subprocess. */
+ MOCK(crypto_pk_generate_key_with_bits,
+ fixed_crypto_pk_generate_key_with_bits);
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk1;
+ fixed_crypto_pk_new_result[1] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result[0] = -1;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk2;
+ fixed_crypto_pk_new_result[1] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk3;
+ fixed_crypto_pk_new_result[1] = pk4;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
+ fixed_crypto_pk_generate_key_with_bits_result[1] = -1;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ MOCK(tor_tls_create_certificate, fixed_tor_tls_create_certificate);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk5;
+ fixed_crypto_pk_new_result[1] = pk6;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_crypto_pk_generate_key_with_bits_result[1] = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = NULL;
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk7;
+ fixed_crypto_pk_new_result[1] = pk8;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = NULL;
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk9;
+ fixed_crypto_pk_new_result[1] = pk10;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = NULL;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ MOCK(tor_x509_cert_new, fixed_tor_x509_cert_new);
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk11;
+ fixed_crypto_pk_new_result[1] = pk12;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_x509_cert_new_result_index = 0;
+ fixed_tor_x509_cert_new_result[0] = NULL;
+ fixed_tor_x509_cert_new_result[1] = NULL;
+ fixed_tor_x509_cert_new_result[2] = NULL;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk13;
+ fixed_crypto_pk_new_result[1] = pk14;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_x509_cert_new_result_index = 0;
+ fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ fixed_tor_x509_cert_new_result[1] = NULL;
+ fixed_tor_x509_cert_new_result[2] = NULL;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk15;
+ fixed_crypto_pk_new_result[1] = pk16;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_x509_cert_new_result_index = 0;
+ fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ fixed_tor_x509_cert_new_result[2] = NULL;
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = pk17;
+ fixed_crypto_pk_new_result[1] = pk18;
+ fixed_crypto_pk_new_result[2] = NULL;
+ fixed_crypto_pk_generate_key_with_bits_result_index = 0;
+ fixed_tor_tls_create_certificate_result_index = 0;
+ fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
+ fixed_tor_x509_cert_new_result_index = 0;
+ fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ fixed_tor_x509_cert_new_result[2] = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ ret = tor_tls_context_new(NULL, 0, 0, 0);
+ tt_assert(!ret);
+
+ done:
+ UNMOCK(tor_x509_cert_new);
+ UNMOCK(tor_tls_create_certificate);
+ UNMOCK(crypto_pk_generate_key_with_bits);
+ UNMOCK(crypto_pk_new);
+}
+#endif
+
+static int fixed_crypto_pk_get_evp_pkey_result_index = 0;
+static EVP_PKEY *fixed_crypto_pk_get_evp_pkey_result[5];
+
+static EVP_PKEY *
+fixed_crypto_pk_get_evp_pkey_(crypto_pk_t *env, int private)
+{
+ (void) env;
+ (void) private;
+ return fixed_crypto_pk_get_evp_pkey_result[
+ fixed_crypto_pk_get_evp_pkey_result_index++];
+}
+
+static void
+test_tortls_create_certificate(void *ignored)
+{
+ (void)ignored;
+ X509 *ret;
+ crypto_pk_t *pk1, *pk2;
+
+ pk1 = crypto_pk_new();
+ pk2 = crypto_pk_new();
+
+ MOCK(crypto_pk_get_evp_pkey_, fixed_crypto_pk_get_evp_pkey_);
+ fixed_crypto_pk_get_evp_pkey_result_index = 0;
+ fixed_crypto_pk_get_evp_pkey_result[0] = NULL;
+ ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_get_evp_pkey_result_index = 0;
+ fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
+ fixed_crypto_pk_get_evp_pkey_result[1] = NULL;
+ ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
+ tt_assert(!ret);
+
+ fixed_crypto_pk_get_evp_pkey_result_index = 0;
+ fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
+ fixed_crypto_pk_get_evp_pkey_result[1] = EVP_PKEY_new();
+ ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
+ tt_assert(!ret);
+
+ done:
+ UNMOCK(crypto_pk_get_evp_pkey_);
+ crypto_pk_free(pk1);
+ crypto_pk_free(pk2);
+}
+
+static void
+test_tortls_cert_new(void *ignored)
+{
+ (void)ignored;
+ tor_x509_cert_t *ret;
+ X509 *cert = read_cert_from(validCertString);
+
+ ret = tor_x509_cert_new(NULL);
+ tt_assert(!ret);
+
+ ret = tor_x509_cert_new(cert);
+ tt_assert(ret);
+ tor_x509_cert_free(ret);
+ ret = NULL;
+
+#if 0
+ cert = read_cert_from(validCertString);
+ /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
+ X509_get_pubkey(cert)->type = EVP_PKEY_DSA;
+ ret = tor_x509_cert_new(cert);
+ tt_assert(ret);
+#endif
+
+#ifndef OPENSSL_OPAQUE
+ cert = read_cert_from(validCertString);
+ X509_CINF_free(cert->cert_info);
+ cert->cert_info = NULL;
+ ret = tor_x509_cert_new(cert);
+ tt_assert(ret);
+#endif
+
+ done:
+ tor_x509_cert_free(ret);
+}
+
+static void
+test_tortls_cert_is_valid(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_x509_cert_t *cert = NULL, *scert = NULL;
+
+ scert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 0);
+ tor_free(scert);
+ tor_free(cert);
+
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 1);
+
+#ifndef OPENSSL_OPAQUE
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ ASN1_TIME_free(cert->cert->cert_info->validity->notAfter);
+ cert->cert->cert_info->validity->notAfter =
+ ASN1_TIME_set(NULL, time(NULL)-1000000);
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 0);
+
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ X509_PUBKEY_free(cert->cert->cert_info->key);
+ cert->cert->cert_info->key = NULL;
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
+ tt_int_op(ret, OP_EQ, 0);
+#endif
+
+#if 0
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ /* This doesn't actually change the key in the cert. XXXXXX */
+ BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert->cert))->n);
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
+ tt_int_op(ret, OP_EQ, 0);
+
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ /* This doesn't actually change the key in the cert. XXXXXX */
+ X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
+ tt_int_op(ret, OP_EQ, 0);
+
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ /* This doesn't actually change the key in the cert. XXXXXX */
+ X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 1);
+
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+ cert = tor_x509_cert_new(read_cert_from(validCertString));
+ scert = tor_x509_cert_new(read_cert_from(caCertString));
+ /* This doesn't actually change the key in the cert. XXXXXX */
+ X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
+ X509_get_pubkey(cert->cert)->ameth = NULL;
+ ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
+ tt_int_op(ret, OP_EQ, 0);
+#endif
+
+ done:
+ tor_x509_cert_free(cert);
+ tor_x509_cert_free(scert);
+}
+
+static void
+test_tortls_context_init_one(void *ignored)
+{
+ (void)ignored;
+ int ret;
+ tor_tls_context_t *old = NULL;
+
+ MOCK(crypto_pk_new, fixed_crypto_pk_new);
+
+ fixed_crypto_pk_new_result_index = 0;
+ fixed_crypto_pk_new_result[0] = NULL;
+ ret = tor_tls_context_init_one(&old, NULL, 0, 0, 0);
+ tt_int_op(ret, OP_EQ, -1);
+
+ done:
+ UNMOCK(crypto_pk_new);
+}
+
+#define LOCAL_TEST_CASE(name, flags) \
+ { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
+
+#ifdef OPENSSL_OPAQUE
+#define INTRUSIVE_TEST_CASE(name, flags) \
+ { #name, NULL, TT_SKIP, NULL, NULL }
+#else
+#define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
+#endif
+
+struct testcase_t tortls_tests[] = {
+ LOCAL_TEST_CASE(errno_to_tls_error, 0),
+ LOCAL_TEST_CASE(err_to_string, 0),
+ LOCAL_TEST_CASE(tor_tls_new, TT_FORK),
+ LOCAL_TEST_CASE(tor_tls_get_error, 0),
+ LOCAL_TEST_CASE(get_state_description, TT_FORK),
+ LOCAL_TEST_CASE(get_by_ssl, TT_FORK),
+ LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index, TT_FORK),
+ LOCAL_TEST_CASE(log_one_error, TT_FORK),
+ INTRUSIVE_TEST_CASE(get_error, TT_FORK),
+ LOCAL_TEST_CASE(always_accept_verify_cb, 0),
+ INTRUSIVE_TEST_CASE(x509_cert_free, 0),
+ LOCAL_TEST_CASE(x509_cert_get_id_digests, 0),
+ INTRUSIVE_TEST_CASE(cert_matches_key, 0),
+ INTRUSIVE_TEST_CASE(cert_get_key, 0),
+ LOCAL_TEST_CASE(get_my_client_auth_key, TT_FORK),
+ LOCAL_TEST_CASE(get_my_certs, TT_FORK),
+ INTRUSIVE_TEST_CASE(get_ciphersuite_name, 0),
+ INTRUSIVE_TEST_CASE(classify_client_ciphers, 0),
+ LOCAL_TEST_CASE(client_is_using_v2_ciphers, 0),
+ INTRUSIVE_TEST_CASE(verify, 0),
+ INTRUSIVE_TEST_CASE(check_lifetime, 0),
+ INTRUSIVE_TEST_CASE(get_pending_bytes, 0),
+ LOCAL_TEST_CASE(get_forced_write_size, 0),
+ LOCAL_TEST_CASE(get_write_overhead_ratio, TT_FORK),
+ LOCAL_TEST_CASE(used_v1_handshake, TT_FORK),
+ LOCAL_TEST_CASE(get_num_server_handshakes, 0),
+ LOCAL_TEST_CASE(server_got_renegotiate, 0),
+ INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key, 0),
+ INTRUSIVE_TEST_CASE(get_tlssecrets, 0),
+ INTRUSIVE_TEST_CASE(get_buffer_sizes, 0),
+ LOCAL_TEST_CASE(evaluate_ecgroup_for_tls, 0),
+ INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls, 0),
+ INTRUSIVE_TEST_CASE(get_peer_cert, 0),
+ INTRUSIVE_TEST_CASE(peer_has_cert, 0),
+ INTRUSIVE_TEST_CASE(shutdown, 0),
+ INTRUSIVE_TEST_CASE(finish_handshake, 0),
+ INTRUSIVE_TEST_CASE(handshake, 0),
+ INTRUSIVE_TEST_CASE(write, 0),
+ INTRUSIVE_TEST_CASE(read, 0),
+ INTRUSIVE_TEST_CASE(server_info_callback, 0),
+ LOCAL_TEST_CASE(is_server, 0),
+ INTRUSIVE_TEST_CASE(assert_renegotiation_unblocked, 0),
+ INTRUSIVE_TEST_CASE(block_renegotiation, 0),
+ INTRUSIVE_TEST_CASE(unblock_renegotiation, 0),
+ INTRUSIVE_TEST_CASE(set_renegotiate_callback, 0),
+ LOCAL_TEST_CASE(set_logged_address, 0),
+ INTRUSIVE_TEST_CASE(find_cipher_by_id, 0),
+ INTRUSIVE_TEST_CASE(session_secret_cb, 0),
+ INTRUSIVE_TEST_CASE(debug_state_callback, 0),
+ INTRUSIVE_TEST_CASE(context_new, TT_FORK /* redundant */),
+ LOCAL_TEST_CASE(create_certificate, 0),
+ LOCAL_TEST_CASE(cert_new, 0),
+ LOCAL_TEST_CASE(cert_is_valid, 0),
+ LOCAL_TEST_CASE(context_init_one, 0),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 0a5783e9f5..fcda564569 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1,10 +1,11 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#define COMPAT_PRIVATE
+#define COMPAT_TIME_PRIVATE
#define CONTROL_PRIVATE
#define UTIL_PRIVATE
#include "or.h"
@@ -13,12 +14,26 @@
#include "test.h"
#include "memarea.h"
#include "util_process.h"
+#include "log_test_helpers.h"
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+#ifdef HAVE_SYS_UTIME_H
+#include <sys/utime.h>
+#endif
+#ifdef HAVE_UTIME_H
+#include <utime.h>
+#endif
#ifdef _WIN32
#include <tchar.h>
#endif
#include <math.h>
#include <ctype.h>
+#include <float.h>
+
+#define INFINITY_DBL ((double)INFINITY)
+#define NAN_DBL ((double)NAN)
/* XXXX this is a minimal wrapper to make the unit tests compile with the
* changed tor_timegm interface. */
@@ -248,7 +263,7 @@ test_util_time(void *arg)
int i;
struct timeval tv;
- /* Test tv_udiff */
+ /* Test tv_udiff and tv_mdiff */
(void)arg;
start.tv_sec = 5;
@@ -258,22 +273,312 @@ test_util_time(void *arg)
end.tv_usec = 5000;
tt_int_op(0L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(0L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(0L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(0L,OP_EQ, tv_mdiff(&end, &start));
end.tv_usec = 7000;
tt_int_op(2000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(2L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-2000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-2L,OP_EQ, tv_mdiff(&end, &start));
end.tv_sec = 6;
tt_int_op(1002000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(1002L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-1002000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-1002L,OP_EQ, tv_mdiff(&end, &start));
end.tv_usec = 0;
tt_int_op(995000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(995L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-995000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-995L,OP_EQ, tv_mdiff(&end, &start));
end.tv_sec = 4;
tt_int_op(-1005000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(-1005L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(1005000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(1005L,OP_EQ, tv_mdiff(&end, &start));
+
+ /* Negative tv_sec values, these will break on platforms where tv_sec is
+ * unsigned */
+
+ end.tv_sec = -10;
+
+ tt_int_op(-15005000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(-15005L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(15005000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(15005L,OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = -100;
+
+ tt_int_op(89995000L,OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(89995L,OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-89995000L,OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-89995L,OP_EQ, tv_mdiff(&end, &start));
+
+ /* Test that tv_usec values round away from zero when converted to msec */
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = 10;
+ end.tv_usec = 499;
+
+ tt_int_op(10000499L, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(10000L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-10000499L, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-10000L, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = 10;
+ end.tv_usec = 500;
+
+ tt_int_op(10000500L, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(10001L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-10000500L, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-10000L, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = 10;
+ end.tv_usec = 501;
+
+ tt_int_op(10000501L, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(10001L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-10000501L, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-10001L, OP_EQ, tv_mdiff(&end, &start));
+
+ /* Overflow conditions */
+
+#ifdef _WIN32
+ /* Would you believe that tv_sec is a long on windows? Of course you would.*/
+#define TV_SEC_MAX LONG_MAX
+#define TV_SEC_MIN LONG_MIN
+#else
+ /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
+ * Which means TIME_MAX is not actually the maximum value of tv_sec.
+ * But that's ok for the moment, because the code correctly performs 64-bit
+ * calculations internally, then catches the overflow. */
+#define TV_SEC_MAX TIME_MAX
+#define TV_SEC_MIN TIME_MIN
+#endif
+
+/* Assume tv_usec is an unsigned integer until proven otherwise */
+#define TV_USEC_MAX UINT_MAX
+#define TOR_USEC_PER_SEC 1000000
+
+ /* Overflows in the result type */
+
+ /* All comparisons work */
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000 - 2;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000000 - 1;
+ end.tv_usec = 0;
+
+ tt_int_op(end.tv_sec*1000000L, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(-end.tv_sec*1000000L, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
+
+ /* No comparisons work */
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000 + 1;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000000 + 1;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+ end.tv_sec = LONG_MAX/1000000;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op((end.tv_sec + 1)*1000L, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(-(end.tv_sec + 1)*1000L, OP_EQ, tv_mdiff(&end, &start));
+
+ /* Overflows on comparison to zero */
+
+ start.tv_sec = 0;
+ start.tv_usec = 0;
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = 0;
+ end.tv_usec = TV_USEC_MAX;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = TV_USEC_MAX;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = 0;
+ end.tv_usec = 0;
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = TV_USEC_MAX;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ /* overflows on comparison to maxima / minima */
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = 0;
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = 0;
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ /* overflows on comparison to maxima / minima with extra usec */
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = TOR_USEC_PER_SEC;
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ end.tv_sec = TV_SEC_MAX;
+ end.tv_usec = TOR_USEC_PER_SEC;
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = 0;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
+
+ start.tv_sec = TV_SEC_MIN;
+ start.tv_usec = TOR_USEC_PER_SEC;
+
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
+ tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
+ tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
/* Test tor_timegm & tor_gmtime_r */
@@ -318,33 +623,78 @@ test_util_time(void *arg)
tor_gmtime_r(&t_res, &b_time);
TM_EQUAL(a_time, b_time);
+ /* This value is in range with 32 bit and 64 bit time_t */
+ a_time.tm_year = 2037-1900;
+ t_res = 2115180895UL;
+ tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
+ tor_gmtime_r(&t_res, &b_time);
+ TM_EQUAL(a_time, b_time);
+
+ /* This value is out of range with 32 bit time_t, but in range for 64 bit
+ * time_t */
+ a_time.tm_year = 2039-1900;
+#if SIZEOF_TIME_T == 4
+ tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+#elif SIZEOF_TIME_T == 8
+ t_res = 2178252895UL;
+ tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
+ tor_gmtime_r(&t_res, &b_time);
+ TM_EQUAL(a_time, b_time);
+#endif
+
/* Test tor_timegm out of range */
+ /* The below tests will all cause a BUG message, so we capture, suppress,
+ * and detect. */
+#define CAPTURE() do { \
+ setup_full_capture_of_logs(LOG_WARN); \
+ } while (0)
+#define CHECK_TIMEGM_WARNING(msg) do { \
+ expect_log_msg_containing(msg); \
+ tt_int_op(1, OP_EQ, smartlist_len(mock_saved_logs())); \
+ teardown_capture_of_logs(); \
+ } while (0)
+
+#define CHECK_TIMEGM_ARG_OUT_OF_RANGE(msg) \
+ CHECK_TIMEGM_WARNING("Out-of-range argument to tor_timegm")
+
/* year */
/* Wrong year < 1970 */
a_time.tm_year = 1969-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = -1-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
#if SIZEOF_INT == 4 || SIZEOF_INT == 8
a_time.tm_year = -1*(1 << 16);
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* one of the smallest tm_year values my 64 bit system supports:
* t_res = -9223372036854775LL without clamping */
a_time.tm_year = -292275055-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = INT32_MIN;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
#endif
#if SIZEOF_INT == 8
a_time.tm_year = -1*(1 << 48);
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* while unlikely, the system's gmtime(_r) could return
* a "correct" retrospective gregorian negative year value,
@@ -352,25 +702,35 @@ test_util_time(void *arg)
* -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
* 730485 is the number of days in two millenia, including leap days */
a_time.tm_year = -292277022657-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = INT64_MIN;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
#endif
/* Wrong year >= INT32_MAX - 1900 */
#if SIZEOF_INT == 4 || SIZEOF_INT == 8
a_time.tm_year = INT32_MAX-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = INT32_MAX;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
#endif
#if SIZEOF_INT == 8
/* one of the largest tm_year values my 64 bit system supports */
a_time.tm_year = 292278994-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* while unlikely, the system's gmtime(_r) could return
* a "correct" proleptic gregorian year value,
@@ -378,72 +738,104 @@ test_util_time(void *arg)
* (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
* 730485 is the number of days in two millenia, including leap days */
a_time.tm_year = 292277026596-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = INT64_MAX-1900;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = INT64_MAX;
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
#endif
/* month */
a_time.tm_year = 2007-1900; /* restore valid year */
a_time.tm_mon = 12; /* Wrong month, it's 0-based */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_mon = -1; /* Wrong month */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* day */
a_time.tm_mon = 6; /* Try July */
a_time.tm_mday = 32; /* Wrong day */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_mon = 5; /* Try June */
a_time.tm_mday = 31; /* Wrong day */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = 2008-1900; /* Try a leap year */
a_time.tm_mon = 1; /* in feb. */
a_time.tm_mday = 30; /* Wrong day */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_year = 2011-1900; /* Try a non-leap year */
a_time.tm_mon = 1; /* in feb. */
a_time.tm_mday = 29; /* Wrong day */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_mday = 0; /* Wrong day, it's 1-based (to be different) */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* hour */
a_time.tm_mday = 3; /* restore valid month day */
a_time.tm_hour = 24; /* Wrong hour, it's 0-based */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_hour = -1; /* Wrong hour */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* minute */
a_time.tm_hour = 22; /* restore valid hour */
a_time.tm_min = 60; /* Wrong minute, it's 0-based */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_min = -1; /* Wrong minute */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* second */
a_time.tm_min = 37; /* restore valid minute */
a_time.tm_sec = 61; /* Wrong second: 0-based with leap seconds */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
a_time.tm_sec = -1; /* Wrong second */
+ CAPTURE();
tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
+ CHECK_TIMEGM_ARG_OUT_OF_RANGE();
/* Test tor_gmtime_r out of range */
@@ -483,9 +875,17 @@ test_util_time(void *arg)
* 730485 is the number of days in two millenia, including leap days
* (int64_t)b_time.tm_year == (-292277022657LL-1900LL) without clamping */
t_res = INT64_MIN;
+ CAPTURE();
tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1-1900));
+ if (! (b_time.tm_year == (1970-1900) ||
+ b_time.tm_year == (1-1900))) {
+ tt_int_op(b_time.tm_year, OP_EQ, 1970-1900);
+ }
+ if (b_time.tm_year != 1970-1900) {
+ CHECK_TIMEGM_WARNING("Rounding up to ");
+ } else {
+ teardown_capture_of_logs();
+ }
}
#endif
@@ -521,7 +921,10 @@ test_util_time(void *arg)
* 730485 is the number of days in two millenia, including leap days
* (int64_t)b_time.tm_year == (292277026596L-1900L) without clamping */
t_res = INT64_MAX;
+ CAPTURE();
tor_gmtime_r(&t_res, &b_time);
+ CHECK_TIMEGM_WARNING("Rounding down to ");
+
tt_assert(b_time.tm_year == (2037-1900) ||
b_time.tm_year == (9999-1900));
}
@@ -538,6 +941,40 @@ test_util_time(void *arg)
i = parse_rfc1123_time(timestr, &t_res);
tt_int_op(0,OP_EQ, i);
tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
+
+ /* This value is in range with 32 bit and 64 bit time_t */
+ format_rfc1123_time(timestr, (time_t)2080000000UL);
+ tt_str_op("Fri, 30 Nov 2035 01:46:40 GMT",OP_EQ, timestr);
+
+ t_res = 0;
+ i = parse_rfc1123_time(timestr, &t_res);
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(t_res,OP_EQ, (time_t)2080000000UL);
+
+ /* This value is out of range with 32 bit time_t, but in range for 64 bit
+ * time_t */
+ format_rfc1123_time(timestr, (time_t)2150000000UL);
+#if SIZEOF_TIME_T == 4
+#if 0
+ /* Wrapping around will have made it this. */
+ /* On windows, at least, this is clipped to 1 Jan 1970. ??? */
+ tt_str_op("Sat, 11 Jan 1902 23:45:04 GMT",OP_EQ, timestr);
+#endif
+ /* Make sure that the right date doesn't parse. */
+ strlcpy(timestr, "Wed, 17 Feb 2038 06:13:20 GMT", sizeof(timestr));
+
+ t_res = 0;
+ i = parse_rfc1123_time(timestr, &t_res);
+ tt_int_op(-1,OP_EQ, i);
+#elif SIZEOF_TIME_T == 8
+ tt_str_op("Wed, 17 Feb 2038 06:13:20 GMT",OP_EQ, timestr);
+
+ t_res = 0;
+ i = parse_rfc1123_time(timestr, &t_res);
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(t_res,OP_EQ, (time_t)2150000000UL);
+#endif
+
/* The timezone doesn't matter */
t_res = 0;
tt_int_op(0,OP_EQ,
@@ -559,9 +996,16 @@ test_util_time(void *arg)
parse_rfc1123_time("Wed, 30 Ene 2011 23:59:59 GMT", &t_res));
tt_int_op(-1,OP_EQ,
parse_rfc1123_time("Wed, 30 Mar 2011 23:59:59 GM", &t_res));
+ tt_int_op(-1,OP_EQ,
+ parse_rfc1123_time("Wed, 30 Mar 1900 23:59:59 GMT", &t_res));
+ /* Leap year. */
tt_int_op(-1,OP_EQ,
parse_rfc1123_time("Wed, 29 Feb 2011 16:00:00 GMT", &t_res));
+ tt_int_op(0,OP_EQ,
+ parse_rfc1123_time("Wed, 29 Feb 2012 16:00:00 GMT", &t_res));
+
+ /* Leap second plus one */
tt_int_op(-1,OP_EQ,
parse_rfc1123_time("Wed, 30 Mar 2011 23:59:61 GMT", &t_res));
@@ -585,6 +1029,24 @@ test_util_time(void *arg)
i = parse_iso_time("2004-8-4 0:48:22", &t_res);
tt_int_op(0,OP_EQ, i);
tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
+
+ /* This value is in range with 32 bit and 64 bit time_t */
+ t_res = 0;
+ i = parse_iso_time("2035-11-30 01:46:40", &t_res);
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(t_res,OP_EQ, (time_t)2080000000UL);
+
+ /* This value is out of range with 32 bit time_t, but in range for 64 bit
+ * time_t */
+ t_res = 0;
+ i = parse_iso_time("2038-02-17 06:13:20", &t_res);
+#if SIZEOF_TIME_T == 4
+ tt_int_op(-1,OP_EQ, i);
+#elif SIZEOF_TIME_T == 8
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(t_res,OP_EQ, (time_t)2150000000UL);
+#endif
+
tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-zz 99-99x99", &t_res));
tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-32 00:00:00", &t_res));
tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 24:00:00", &t_res));
@@ -612,7 +1074,7 @@ test_util_time(void *arg)
/* Test format_iso_time */
- tv.tv_sec = (time_t)1326296338;
+ tv.tv_sec = (time_t)1326296338UL;
tv.tv_usec = 3060;
format_iso_time(timestr, (time_t)tv.tv_sec);
tt_str_op("2012-01-11 15:38:58",OP_EQ, timestr);
@@ -629,8 +1091,33 @@ test_util_time(void *arg)
tt_str_op("2012-01-11T15:38:58.003060",OP_EQ, timestr);
tt_int_op(strlen(timestr),OP_EQ, ISO_TIME_USEC_LEN);
+ tv.tv_usec = 0;
+ /* This value is in range with 32 bit and 64 bit time_t */
+ tv.tv_sec = (time_t)2080000000UL;
+ format_iso_time(timestr, (time_t)tv.tv_sec);
+ tt_str_op("2035-11-30 01:46:40",OP_EQ, timestr);
+
+ /* This value is out of range with 32 bit time_t, but in range for 64 bit
+ * time_t */
+ tv.tv_sec = (time_t)2150000000UL;
+ format_iso_time(timestr, (time_t)tv.tv_sec);
+#if SIZEOF_TIME_T == 4
+ /* format_iso_time should indicate failure on overflow, but it doesn't yet.
+ * Hopefully #18480 will improve the failure semantics in this case.
+ tt_str_op("2038-02-17 06:13:20",OP_EQ, timestr);
+ */
+#elif SIZEOF_TIME_T == 8
+#ifndef _WIN32
+ /* This SHOULD work on windows too; see bug #18665 */
+ tt_str_op("2038-02-17 06:13:20",OP_EQ, timestr);
+#endif
+#endif
+
+#undef CAPTURE
+#undef CHECK_TIMEGM_ARG_OUT_OF_RANGE
+
done:
- ;
+ teardown_capture_of_logs();
}
static void
@@ -702,6 +1189,25 @@ test_util_parse_http_time(void *arg)
tt_int_op(0,OP_EQ,parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
tt_int_op((time_t)1356912000UL,OP_EQ, tor_timegm(&a_time));
T("2012-12-31 00:00:00");
+
+ /* This value is in range with 32 bit and 64 bit time_t */
+ tt_int_op(0,OP_EQ,parse_http_time("Fri, 30 Nov 2035 01:46:40 GMT", &a_time));
+ tt_int_op((time_t)2080000000UL,OP_EQ, tor_timegm(&a_time));
+ T("2035-11-30 01:46:40");
+
+ /* This value is out of range with 32 bit time_t, but in range for 64 bit
+ * time_t */
+#if SIZEOF_TIME_T == 4
+ /* parse_http_time should indicate failure on overflow, but it doesn't yet.
+ * Hopefully #18480 will improve the failure semantics in this case. */
+ tt_int_op(0,OP_EQ,parse_http_time("Wed, 17 Feb 2038 06:13:20 GMT", &a_time));
+ tt_int_op((time_t)-1,OP_EQ, tor_timegm(&a_time));
+#elif SIZEOF_TIME_T == 8
+ tt_int_op(0,OP_EQ,parse_http_time("Wed, 17 Feb 2038 06:13:20 GMT", &a_time));
+ tt_int_op((time_t)2150000000UL,OP_EQ, tor_timegm(&a_time));
+ T("2038-02-17 06:13:20");
+#endif
+
tt_int_op(-1,OP_EQ, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
tt_int_op(-1,OP_EQ, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
@@ -743,106 +1249,106 @@ test_util_config_line(void *arg)
, sizeof(buf));
str = buf;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k");
tt_str_op(v,OP_EQ, "v");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "key value with"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "key");
tt_str_op(v,OP_EQ, "value with spaces");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "keykey"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "keykey");
tt_str_op(v,OP_EQ, "val");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "k2\n"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k2");
tt_str_op(v,OP_EQ, "");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "k3 \n"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k3");
tt_str_op(v,OP_EQ, "");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "#comment"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k4");
tt_str_op(v,OP_EQ, "");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "k5#abc"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k5");
tt_str_op(v,OP_EQ, "");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "k6"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k6");
tt_str_op(v,OP_EQ, "val");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "kseven"));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "kseven");
tt_str_op(v,OP_EQ, "a quoted \'string");
tor_free(k); tor_free(v);
tt_assert(!strcmpstart(str, "k8 "));
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k8");
tt_str_op(v,OP_EQ, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k9");
tt_str_op(v,OP_EQ, "a line that spans two lines.");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k10");
tt_str_op(v,OP_EQ, "more than one continuation");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k11");
tt_str_op(v,OP_EQ, "continuation at the start");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k12");
tt_str_op(v,OP_EQ, "line with a embedded");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k13");
tt_str_op(v,OP_EQ, "continuation at the very start");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k14");
tt_str_op(v,OP_EQ, "a line that has a comment and" );
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k15");
tt_str_op(v,OP_EQ, "this should be the next new line");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k16");
tt_str_op(v,OP_EQ, "a line that has a comment and" );
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k17");
tt_str_op(v,OP_EQ, "this should be the next new line");
tor_free(k); tor_free(v);
@@ -877,32 +1383,36 @@ test_util_config_line_quotes(void *arg)
, sizeof(buf4));
str = buf1;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "kTrailingSpace");
tt_str_op(v,OP_EQ, "quoted value");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
str = buf2;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
str = buf3;
- str = parse_config_line_from_str(str, &k, &v);
+ const char *err = NULL;
+ str = parse_config_line_from_str_verbose(str, &k, &v, &err);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
+ tt_str_op(err, OP_EQ, "Invalid escape sequence in quoted string");
str = buf4;
- str = parse_config_line_from_str(str, &k, &v);
+ err = NULL;
+ str = parse_config_line_from_str_verbose(str, &k, &v, &err);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
+ tt_str_op(err, OP_EQ, "Invalid escape sequence in quoted string");
done:
tor_free(k);
@@ -924,12 +1434,12 @@ test_util_config_line_comment_character(void *arg)
, sizeof(buf));
str = buf;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k1");
tt_str_op(v,OP_EQ, "# in quotes");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "k2");
tt_str_op(v,OP_EQ, "some value");
tor_free(k); tor_free(v);
@@ -937,7 +1447,7 @@ test_util_config_line_comment_character(void *arg)
tt_str_op(str,OP_EQ, "k3 /home/user/myTorNetwork#2\n");
#if 0
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
test_streq(k, "k3");
test_streq(v, "/home/user/myTorNetwork#2");
tor_free(k); tor_free(v);
@@ -994,57 +1504,57 @@ test_util_config_line_escaped_content(void *arg)
str = buf1;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "HexadecimalLower");
tt_str_op(v,OP_EQ, "*");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "HexadecimalUpper");
tt_str_op(v,OP_EQ, "*");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "HexadecimalUpperX");
tt_str_op(v,OP_EQ, "*");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "Octal");
tt_str_op(v,OP_EQ, "*");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "Newline");
tt_str_op(v,OP_EQ, "\n");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "Tab");
tt_str_op(v,OP_EQ, "\t");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "CarriageReturn");
tt_str_op(v,OP_EQ, "\r");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "DoubleQuote");
tt_str_op(v,OP_EQ, "\"");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "SimpleQuote");
tt_str_op(v,OP_EQ, "'");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "Backslash");
tt_str_op(v,OP_EQ, "\\");
tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_str_op(k,OP_EQ, "Mix");
tt_str_op(v,OP_EQ, "This is a \"star\":\t'*'\nAnd second line");
tor_free(k); tor_free(v);
@@ -1052,41 +1562,115 @@ test_util_config_line_escaped_content(void *arg)
str = buf2;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
str = buf3;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
str = buf4;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
#if 0
str = buf5;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str, OP_EQ, NULL);
tor_free(k); tor_free(v);
#endif
str = buf6;
- str = parse_config_line_from_str(str, &k, &v);
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+
+ /* more things to try. */
+ /* Bad hex: */
+ strlcpy(buf1, "Foo \"\\x9g\"\n", sizeof(buf1));
+ strlcpy(buf2, "Foo \"\\xg0\"\n", sizeof(buf2));
+ strlcpy(buf3, "Foo \"\\xf\"\n", sizeof(buf3));
+ /* bad escape */
+ strlcpy(buf4, "Foo \"\\q\"\n", sizeof(buf4));
+ /* missing endquote */
+ strlcpy(buf5, "Foo \"hello\n", sizeof(buf5));
+ /* extra stuff */
+ strlcpy(buf6, "Foo \"hello\" world\n", sizeof(buf6));
+
+ str=buf1;
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+
+ str=buf2;
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+
+ str=buf3;
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+
+ str=buf4;
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
tt_ptr_op(str,OP_EQ, NULL);
tor_free(k); tor_free(v);
+ str=buf5;
+
+ str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+
+ str=buf6;
+ const char *err = NULL;
+ str = parse_config_line_from_str_verbose(str, &k, &v, &err);
+ tt_ptr_op(str,OP_EQ, NULL);
+ tor_free(k); tor_free(v);
+ tt_str_op(err,OP_EQ, "Excess data after quoted string");
+
done:
tor_free(k);
tor_free(v);
}
+static void
+test_util_config_line_crlf(void *arg)
+{
+ char *k=NULL, *v=NULL;
+ const char *err = NULL;
+ (void)arg;
+ const char *str =
+ "Hello world\r\n"
+ "Hello \"nice big world\"\r\n";
+
+ str = parse_config_line_from_str_verbose(str, &k, &v, &err);
+ tt_assert(str);
+ tt_str_op(k,OP_EQ,"Hello");
+ tt_str_op(v,OP_EQ,"world");
+ tt_assert(!err);
+ tor_free(k); tor_free(v);
+
+ str = parse_config_line_from_str_verbose(str, &k, &v, &err);
+ tt_assert(str);
+ tt_str_op(k,OP_EQ,"Hello");
+ tt_str_op(v,OP_EQ,"nice big world");
+ tt_assert(!err);
+ tor_free(k); tor_free(v);
+ tt_str_op(str,OP_EQ, "");
+
+ done:
+ tor_free(k); tor_free(v);
+}
+
#ifndef _WIN32
static void
test_util_expand_filename(void *arg)
@@ -1250,8 +1834,7 @@ static void
test_util_strmisc(void *arg)
{
char buf[1024];
- int i;
- char *cp, *cp_tmp = NULL;
+ char *cp_tmp = NULL;
/* Test strl operations */
(void)arg;
@@ -1276,117 +1859,6 @@ test_util_strmisc(void *arg)
tor_strstrip(buf, "!? ");
tt_str_op(buf,OP_EQ, "Testing123");
- /* Test parse_long */
- /* Empty/zero input */
- tt_int_op(0L,OP_EQ, tor_parse_long("",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("0",10,0,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- /* Normal cases */
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,10,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,10,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,0,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-50,0,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- /* Extra garbage */
- tt_int_op(0L,OP_EQ, tor_parse_long("10m",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10m",10,0,100,&i,&cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, "m");
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, " plus garbage");
- /* Out of bounds */
- tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- /* Base different than 10 */
- tt_int_op(2L,OP_EQ, tor_parse_long("10",2,0,100,NULL,NULL));
- tt_int_op(0L,OP_EQ, tor_parse_long("2",2,0,100,NULL,NULL));
- tt_int_op(0L,OP_EQ, tor_parse_long("10",-2,0,100,NULL,NULL));
- tt_int_op(68284L,OP_EQ, tor_parse_long("10abc",16,0,70000,NULL,NULL));
- tt_int_op(68284L,OP_EQ, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
- tt_int_op(0,OP_EQ, tor_parse_long("10ABC",-1,0,70000,&i,NULL));
- tt_int_op(i,OP_EQ, 0);
-
- /* Test parse_ulong */
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("",10,0,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("0",10,0,100,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("10",10,50,100,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,10,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,10,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("8",8,0,100,NULL,NULL));
- tt_int_op(50UL,OP_EQ, tor_parse_ulong("50",10,50,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,-100,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("50",-1,50,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
-
- /* Test parse_uint64 */
- tt_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, " x");
- tt_assert(U64_LITERAL(12345678901) ==
- tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, "");
- tt_assert(U64_LITERAL(0) ==
- tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
- tt_int_op(0,OP_EQ, i);
- tt_assert(U64_LITERAL(0) ==
- tor_parse_uint64("123",-1,0,INT32_MAX, &i, &cp));
- tt_int_op(0,OP_EQ, i);
-
- {
- /* Test parse_double */
- double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 10);
- d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 0);
- d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
- tt_int_op(0,OP_EQ, i);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
- tt_int_op(0,OP_EQ, i);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
- tt_int_op(1,OP_EQ, i);
- d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 0);
- d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-10.0,OP_EQ, d);
- }
-
- {
- /* Test tor_parse_* where we overflow/underflow the underlying type. */
- /* This string should overflow 64-bit ints. */
-#define TOOBIG "100000000000000000000000000"
- tt_int_op(0L, OP_EQ,
- tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_int_op(0L,OP_EQ,
- tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_int_op(0UL,OP_EQ, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_u64_op(U64_LITERAL(0), OP_EQ, tor_parse_uint64(TOOBIG, 10,
- 0, UINT64_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- }
-
/* Test snprintf */
/* Returning -1 when there's not enough room in the output buffer */
tt_int_op(-1,OP_EQ, tor_snprintf(buf, 0, "Foo"));
@@ -1461,6 +1933,17 @@ test_util_strmisc(void *arg)
tt_str_op("\"z\\001abc\\277d\"",OP_EQ, escaped("z\001abc\277d"));
tt_str_op("\"z\\336\\255 ;foo\"",OP_EQ, escaped("z\xde\xad\x20;foo"));
+ /* Other cases of esc_for_log{,_len} */
+ cp_tmp = esc_for_log(NULL);
+ tt_str_op(cp_tmp, OP_EQ, "(null)");
+ tor_free(cp_tmp);
+ cp_tmp = esc_for_log_len("abcdefg", 3);
+ tt_str_op(cp_tmp, OP_EQ, "\"abc\"");
+ tor_free(cp_tmp);
+ cp_tmp = esc_for_log_len("abcdefg", 100);
+ tt_str_op(cp_tmp, OP_EQ, "\"abcdefg\"");
+ tor_free(cp_tmp);
+
/* Test strndup and memdup */
{
const char *s = "abcdefghijklmnopqrstuvwxyz";
@@ -1520,9 +2003,9 @@ test_util_strmisc(void *arg)
/* Test hex_str */
{
char binary_data[68];
- size_t i;
- for (i = 0; i < sizeof(binary_data); ++i)
- binary_data[i] = i;
+ size_t idx;
+ for (idx = 0; idx < sizeof(binary_data); ++idx)
+ binary_data[idx] = idx;
tt_str_op(hex_str(binary_data, 0),OP_EQ, "");
tt_str_op(hex_str(binary_data, 1),OP_EQ, "00");
tt_str_op(hex_str(binary_data, 17),OP_EQ,
@@ -1565,6 +2048,144 @@ test_util_strmisc(void *arg)
}
static void
+test_util_parse_integer(void *arg)
+{
+ (void)arg;
+ int i;
+ char *cp;
+
+ /* Test parse_long */
+ /* Empty/zero input */
+ tt_int_op(0L,OP_EQ, tor_parse_long("",10,0,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(0L,OP_EQ, tor_parse_long("0",10,0,100,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ /* Normal cases */
+ tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,100,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,10,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ tt_int_op(10L,OP_EQ, tor_parse_long("10",10,10,100,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,100,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,0,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-50,0,&i,NULL));
+ tt_int_op(1,OP_EQ, i);
+ /* Extra garbage */
+ tt_int_op(0L,OP_EQ, tor_parse_long("10m",10,0,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(0L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(10L,OP_EQ, tor_parse_long("10m",10,0,100,&i,&cp));
+ tt_int_op(1,OP_EQ, i);
+ tt_str_op(cp,OP_EQ, "m");
+ tt_int_op(-50L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
+ tt_int_op(1,OP_EQ, i);
+ tt_str_op(cp,OP_EQ, " plus garbage");
+ /* Illogical min max */
+ tor_capture_bugs_(1);
+ tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,4,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(1, OP_EQ, smartlist_len(tor_get_captured_bug_log_()));
+ tt_str_op("!(max < min)", OP_EQ,
+ smartlist_get(tor_get_captured_bug_log_(), 0));
+ tor_end_capture_bugs_();
+ tor_capture_bugs_(1);
+ tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,100,-100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(1, OP_EQ, smartlist_len(tor_get_captured_bug_log_()));
+ tt_str_op("!(max < min)", OP_EQ,
+ smartlist_get(tor_get_captured_bug_log_(), 0));
+ tor_end_capture_bugs_();
+ /* Out of bounds */
+ tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,0,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ /* Base different than 10 */
+ tt_int_op(2L,OP_EQ, tor_parse_long("10",2,0,100,NULL,NULL));
+ tt_int_op(0L,OP_EQ, tor_parse_long("2",2,0,100,NULL,NULL));
+ tt_int_op(0L,OP_EQ, tor_parse_long("10",-2,0,100,NULL,NULL));
+ tt_int_op(68284L,OP_EQ, tor_parse_long("10abc",16,0,70000,NULL,NULL));
+ tt_int_op(68284L,OP_EQ, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
+ tt_int_op(0,OP_EQ, tor_parse_long("10ABC",-1,0,70000,&i,NULL));
+ tt_int_op(i,OP_EQ, 0);
+
+ /* Test parse_ulong */
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("",10,0,100,NULL,NULL));
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("0",10,0,100,NULL,NULL));
+ tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,100,NULL,NULL));
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("10",10,50,100,NULL,NULL));
+ tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,10,NULL,NULL));
+ tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,10,100,NULL,NULL));
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("8",8,0,100,NULL,NULL));
+ tt_int_op(50UL,OP_EQ, tor_parse_ulong("50",10,50,100,NULL,NULL));
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,0,100,NULL,NULL));
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("50",-1,50,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,0,100,&i,NULL));
+ tt_int_op(0,OP_EQ, i);
+
+ /* Test parse_uint64 */
+ tt_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
+ tt_int_op(1,OP_EQ, i);
+ tt_str_op(cp,OP_EQ, " x");
+ tt_assert(U64_LITERAL(12345678901) ==
+ tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
+ tt_int_op(1,OP_EQ, i);
+ tt_str_op(cp,OP_EQ, "");
+ tt_assert(U64_LITERAL(0) ==
+ tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
+ tt_int_op(0,OP_EQ, i);
+ tt_assert(U64_LITERAL(0) ==
+ tor_parse_uint64("123",-1,0,INT32_MAX, &i, &cp));
+ tt_int_op(0,OP_EQ, i);
+
+ {
+ /* Test parse_double */
+ double d = tor_parse_double("10", 0, (double)UINT64_MAX,&i,NULL);
+ tt_int_op(1,OP_EQ, i);
+ tt_assert(DBL_TO_U64(d) == 10);
+ d = tor_parse_double("0", 0, (double)UINT64_MAX,&i,NULL);
+ tt_int_op(1,OP_EQ, i);
+ tt_assert(DBL_TO_U64(d) == 0);
+ d = tor_parse_double(" ", 0, (double)UINT64_MAX,&i,NULL);
+ tt_int_op(0,OP_EQ, i);
+ d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,NULL);
+ tt_int_op(0,OP_EQ, i);
+ d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,&cp);
+ tt_int_op(1,OP_EQ, i);
+ d = tor_parse_double("-.0", 0, (double)UINT64_MAX,&i,NULL);
+ tt_int_op(1,OP_EQ, i);
+ tt_assert(DBL_TO_U64(d) == 0);
+ d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
+ tt_int_op(1,OP_EQ, i);
+ tt_double_op(fabs(d - -10.0),OP_LT, 1E-12);
+ }
+
+ {
+ /* Test tor_parse_* where we overflow/underflow the underlying type. */
+ /* This string should overflow 64-bit ints. */
+#define TOOBIG "100000000000000000000000000"
+ tt_int_op(0L, OP_EQ,
+ tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
+ tt_int_op(i,OP_EQ, 0);
+ tt_int_op(0L,OP_EQ,
+ tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
+ tt_int_op(i,OP_EQ, 0);
+ tt_int_op(0UL,OP_EQ, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
+ tt_int_op(i,OP_EQ, 0);
+ tt_u64_op(U64_LITERAL(0), OP_EQ, tor_parse_uint64(TOOBIG, 10,
+ 0, UINT64_MAX, &i, NULL));
+ tt_int_op(i,OP_EQ, 0);
+ }
+ done:
+ tor_end_capture_bugs_();
+}
+
+static void
test_util_pow2(void *arg)
{
/* Test tor_log2(). */
@@ -1615,22 +2236,21 @@ test_util_gzip(void *arg)
(void)arg;
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
- if (is_gzip_supported()) {
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- GZIP_METHOD));
- tt_assert(buf2);
- tt_assert(len1 < strlen(buf1));
- tt_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
-
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- GZIP_METHOD, 1, LOG_INFO));
- tt_assert(buf3);
- tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
- tt_str_op(buf1,OP_EQ, buf3);
-
- tor_free(buf2);
- tor_free(buf3);
- }
+
+ tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ GZIP_METHOD));
+ tt_assert(buf2);
+ tt_assert(len1 < strlen(buf1));
+ tt_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
+
+ tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
+ GZIP_METHOD, 1, LOG_INFO));
+ tt_assert(buf3);
+ tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
+ tt_str_op(buf1,OP_EQ, buf3);
+
+ tor_free(buf2);
+ tor_free(buf3);
tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
ZLIB_METHOD));
@@ -1714,6 +2334,58 @@ test_util_gzip(void *arg)
tor_free(buf1);
}
+static void
+test_util_gzip_compression_bomb(void *arg)
+{
+ /* A 'compression bomb' is a very small object that uncompresses to a huge
+ * one. Most compression formats support them, but they can be a DOS vector.
+ * In Tor we try not to generate them, and we don't accept them.
+ */
+ (void) arg;
+ size_t one_million = 1<<20;
+ char *one_mb = tor_malloc_zero(one_million);
+ char *result = NULL;
+ size_t result_len = 0;
+ tor_zlib_state_t *state = NULL;
+
+ /* Make sure we can't produce a compression bomb */
+ setup_full_capture_of_logs(LOG_WARN);
+ tt_int_op(-1, OP_EQ, tor_gzip_compress(&result, &result_len,
+ one_mb, one_million,
+ ZLIB_METHOD));
+ expect_single_log_msg_containing(
+ "We compressed something and got an insanely high "
+ "compression factor; other Tors would think this "
+ "was a zlib bomb.");
+ teardown_capture_of_logs();
+
+ /* Here's a compression bomb that we made manually. */
+ const char compression_bomb[1039] =
+ { 0x78, 0xDA, 0xED, 0xC1, 0x31, 0x01, 0x00, 0x00, 0x00, 0xC2,
+ 0xA0, 0xF5, 0x4F, 0x6D, 0x08, 0x5F, 0xA0 /* .... */ };
+ tt_int_op(-1, OP_EQ, tor_gzip_uncompress(&result, &result_len,
+ compression_bomb, 1039,
+ ZLIB_METHOD, 0, LOG_WARN));
+
+ /* Now try streaming that. */
+ state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
+ tor_zlib_output_t r;
+ const char *inp = compression_bomb;
+ size_t inlen = 1039;
+ do {
+ char *outp = one_mb;
+ size_t outleft = 4096; /* small on purpose */
+ r = tor_zlib_process(state, &outp, &outleft, &inp, &inlen, 0);
+ tt_int_op(inlen, OP_NE, 0);
+ } while (r == TOR_ZLIB_BUF_FULL);
+
+ tt_int_op(r, OP_EQ, TOR_ZLIB_ERR);
+
+ done:
+ tor_free(one_mb);
+ tor_zlib_free(state);
+}
+
/** Run unit tests for mmap() wrapper functionality. */
static void
test_util_mmap(void *arg)
@@ -2175,7 +2847,8 @@ test_util_sscanf(void *arg)
}
#define tt_char_op(a,op,b) tt_assert_op_type(a,op,b,char,"%c")
-#define tt_ci_char_op(a,op,b) tt_char_op(tolower(a),op,tolower(b))
+#define tt_ci_char_op(a,op,b) \
+ tt_char_op(TOR_TOLOWER((int)a),op,TOR_TOLOWER((int)b))
#ifndef HAVE_STRNLEN
static size_t
@@ -2719,19 +3392,40 @@ test_util_memarea(void *arg)
p1 = memarea_alloc(area, 1);
tt_ptr_op(p1,OP_EQ, p1_orig);
memarea_clear(area);
+ size_t total = 0, initial_allocation, allocation2, dummy;
+ memarea_get_stats(area, &initial_allocation, &dummy);
/* Check for running over an area's size. */
- for (i = 0; i < 512; ++i) {
- p1 = memarea_alloc(area, crypto_rand_int(5)+1);
+ for (i = 0; i < 4096; ++i) {
+ size_t n = crypto_rand_int(6);
+ p1 = memarea_alloc(area, n);
+ total += n;
tt_assert(memarea_owns_ptr(area, p1));
}
memarea_assert_ok(area);
+ memarea_get_stats(area, &allocation2, &dummy);
/* Make sure we can allocate a too-big object. */
p1 = memarea_alloc_zero(area, 9000);
p2 = memarea_alloc_zero(area, 16);
+ total += 9000;
+ total += 16;
tt_assert(memarea_owns_ptr(area, p1));
tt_assert(memarea_owns_ptr(area, p2));
+ /* Now test stats... */
+ size_t allocated = 0, used = 0;
+ memarea_get_stats(area, &allocated, &used);
+ tt_int_op(used, OP_LE, allocated);
+ tt_int_op(used, OP_GE, total); /* not EQ, because of alignment and headers*/
+ tt_int_op(allocated, OP_GT, allocation2);
+
+ tt_int_op(allocation2, OP_GT, initial_allocation);
+
+ memarea_clear(area);
+ memarea_get_stats(area, &allocated, &used);
+ tt_int_op(used, OP_LT, 128); /* Not 0, because of header */
+ tt_int_op(allocated, OP_EQ, initial_allocation);
+
done:
memarea_drop_all(area);
tor_free(malloced_ptr);
@@ -3121,6 +3815,21 @@ test_util_ftruncate(void *ptr)
tor_free(buf);
}
+static void
+test_util_num_cpus(void *arg)
+{
+ (void)arg;
+ int num = compute_num_cpus();
+ if (num < 0)
+ tt_skip();
+
+ tt_int_op(num, OP_GE, 1);
+ tt_int_op(num, OP_LE, 16);
+
+ done:
+ ;
+}
+
#ifdef _WIN32
static void
test_util_load_win_lib(void *ptr)
@@ -4086,6 +4795,31 @@ test_util_mathlog(void *arg)
}
static void
+test_util_fraction(void *arg)
+{
+ uint64_t a,b;
+ (void)arg;
+
+ a = 99; b = 30;
+ simplify_fraction64(&a,&b);
+ tt_u64_op(a, OP_EQ, 33);
+ tt_u64_op(b, OP_EQ, 10);
+
+ a = 3000000; b = 10000000;
+ simplify_fraction64(&a,&b);
+ tt_u64_op(a, OP_EQ, 3);
+ tt_u64_op(b, OP_EQ, 10);
+
+ a = 0; b = 15;
+ simplify_fraction64(&a,&b);
+ tt_u64_op(a, OP_EQ, 0);
+ tt_u64_op(b, OP_EQ, 1);
+
+ done:
+ ;
+}
+
+static void
test_util_round_to_next_multiple_of(void *arg)
{
(void)arg;
@@ -4097,20 +4831,28 @@ test_util_round_to_next_multiple_of(void *arg)
tt_u64_op(round_uint64_to_next_multiple_of(99,7), ==, 105);
tt_u64_op(round_uint64_to_next_multiple_of(99,9), ==, 99);
- tt_i64_op(round_int64_to_next_multiple_of(0,1), ==, 0);
- tt_i64_op(round_int64_to_next_multiple_of(0,7), ==, 0);
+ tt_u64_op(round_uint64_to_next_multiple_of(UINT64_MAX,2), ==,
+ UINT64_MAX);
+
+ tt_int_op(round_uint32_to_next_multiple_of(0,1), ==, 0);
+ tt_int_op(round_uint32_to_next_multiple_of(0,7), ==, 0);
+
+ tt_int_op(round_uint32_to_next_multiple_of(99,1), ==, 99);
+ tt_int_op(round_uint32_to_next_multiple_of(99,7), ==, 105);
+ tt_int_op(round_uint32_to_next_multiple_of(99,9), ==, 99);
+
+ tt_int_op(round_uint32_to_next_multiple_of(UINT32_MAX,2), ==,
+ UINT32_MAX);
- tt_i64_op(round_int64_to_next_multiple_of(99,1), ==, 99);
- tt_i64_op(round_int64_to_next_multiple_of(99,7), ==, 105);
- tt_i64_op(round_int64_to_next_multiple_of(99,9), ==, 99);
+ tt_uint_op(round_to_next_multiple_of(0,1), ==, 0);
+ tt_uint_op(round_to_next_multiple_of(0,7), ==, 0);
- tt_i64_op(round_int64_to_next_multiple_of(-99,1), ==, -99);
- tt_i64_op(round_int64_to_next_multiple_of(-99,7), ==, -98);
- tt_i64_op(round_int64_to_next_multiple_of(-99,9), ==, -99);
+ tt_uint_op(round_to_next_multiple_of(99,1), ==, 99);
+ tt_uint_op(round_to_next_multiple_of(99,7), ==, 105);
+ tt_uint_op(round_to_next_multiple_of(99,9), ==, 99);
- tt_i64_op(round_int64_to_next_multiple_of(INT64_MIN,2), ==, INT64_MIN);
- tt_i64_op(round_int64_to_next_multiple_of(INT64_MAX,2), ==,
- INT64_MAX-INT64_MAX%2);
+ tt_uint_op(round_to_next_multiple_of(UINT_MAX,2), ==,
+ UINT_MAX);
done:
;
}
@@ -4143,6 +4885,7 @@ test_util_laplace(void *arg)
*/
tt_i64_op(INT64_MIN + 20, ==,
add_laplace_noise(20, 0.0, delta_f, epsilon));
+
tt_i64_op(-60, ==, add_laplace_noise(20, 0.1, delta_f, epsilon));
tt_i64_op(-14, ==, add_laplace_noise(20, 0.25, delta_f, epsilon));
tt_i64_op(20, ==, add_laplace_noise(20, 0.5, delta_f, epsilon));
@@ -4150,15 +4893,146 @@ test_util_laplace(void *arg)
tt_i64_op(100, ==, add_laplace_noise(20, 0.9, delta_f, epsilon));
tt_i64_op(215, ==, add_laplace_noise(20, 0.99, delta_f, epsilon));
+ /* Test extreme values of signal with maximally negative values of noise
+ * 1.0000000000000002 is the smallest number > 1
+ * 0.0000000000000002 is the double epsilon (error when calculating near 1)
+ * this is approximately 1/(2^52)
+ * per https://en.wikipedia.org/wiki/Double_precision
+ * (let's not descend into the world of subnormals)
+ * >>> laplace.ppf([0, 0.0000000000000002], loc = 0, scale = 1)
+ * array([ -inf, -35.45506713])
+ */
+ const double noscale_df = 1.0, noscale_eps = 1.0;
+
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(0, 0.0, noscale_df, noscale_eps));
+
+ /* is it clipped to INT64_MIN? */
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(-1, 0.0, noscale_df, noscale_eps));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(INT64_MIN, 0.0,
+ noscale_df, noscale_eps));
+ /* ... even when scaled? */
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(0, 0.0, delta_f, epsilon));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(0, 0.0,
+ DBL_MAX, 1));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(INT64_MIN, 0.0,
+ DBL_MAX, 1));
+
+ /* does it play nice with INT64_MAX? */
+ tt_i64_op((INT64_MIN + INT64_MAX), ==,
+ add_laplace_noise(INT64_MAX, 0.0,
+ noscale_df, noscale_eps));
+
+ /* do near-zero fractional values work? */
+ const double min_dbl_error = 0.0000000000000002;
+
+ tt_i64_op(-35, ==,
+ add_laplace_noise(0, min_dbl_error,
+ noscale_df, noscale_eps));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(INT64_MIN, min_dbl_error,
+ noscale_df, noscale_eps));
+ tt_i64_op((-35 + INT64_MAX), ==,
+ add_laplace_noise(INT64_MAX, min_dbl_error,
+ noscale_df, noscale_eps));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(0, min_dbl_error,
+ DBL_MAX, 1));
+ tt_i64_op((INT64_MAX + INT64_MIN), ==,
+ add_laplace_noise(INT64_MAX, min_dbl_error,
+ DBL_MAX, 1));
+ tt_i64_op(INT64_MIN, ==,
+ add_laplace_noise(INT64_MIN, min_dbl_error,
+ DBL_MAX, 1));
+
+ /* does it play nice with INT64_MAX? */
+ tt_i64_op((INT64_MAX - 35), ==,
+ add_laplace_noise(INT64_MAX, min_dbl_error,
+ noscale_df, noscale_eps));
+
+ /* Test extreme values of signal with maximally positive values of noise
+ * 1.0000000000000002 is the smallest number > 1
+ * 0.9999999999999998 is the greatest number < 1 by calculation
+ * per https://en.wikipedia.org/wiki/Double_precision
+ * >>> laplace.ppf([1.0, 0.9999999999999998], loc = 0, scale = 1)
+ * array([inf, 35.35050621])
+ * but the function rejects p == 1.0, so we just use max_dbl_lt_one
+ */
+ const double max_dbl_lt_one = 0.9999999999999998;
+
+ /* do near-one fractional values work? */
+ tt_i64_op(35, ==,
+ add_laplace_noise(0, max_dbl_lt_one, noscale_df, noscale_eps));
+
+ /* is it clipped to INT64_MAX? */
+ tt_i64_op(INT64_MAX, ==,
+ add_laplace_noise(INT64_MAX - 35, max_dbl_lt_one,
+ noscale_df, noscale_eps));
+ tt_i64_op(INT64_MAX, ==,
+ add_laplace_noise(INT64_MAX - 34, max_dbl_lt_one,
+ noscale_df, noscale_eps));
+ tt_i64_op(INT64_MAX, ==,
+ add_laplace_noise(INT64_MAX, max_dbl_lt_one,
+ noscale_df, noscale_eps));
+ /* ... even when scaled? */
+ tt_i64_op(INT64_MAX, ==,
+ add_laplace_noise(INT64_MAX, max_dbl_lt_one,
+ delta_f, epsilon));
+ tt_i64_op((INT64_MIN + INT64_MAX), ==,
+ add_laplace_noise(INT64_MIN, max_dbl_lt_one,
+ DBL_MAX, 1));
+ tt_i64_op(INT64_MAX, ==,
+ add_laplace_noise(INT64_MAX, max_dbl_lt_one,
+ DBL_MAX, 1));
+ /* does it play nice with INT64_MIN? */
+ tt_i64_op((INT64_MIN + 35), ==,
+ add_laplace_noise(INT64_MIN, max_dbl_lt_one,
+ noscale_df, noscale_eps));
+
done:
;
}
-#define UTIL_LEGACY(name) \
- { #name, test_util_ ## name , 0, NULL, NULL }
+static void
+test_util_clamp_double_to_int64(void *arg)
+{
+ (void)arg;
-#define UTIL_TEST(name, flags) \
- { #name, test_util_ ## name, flags, NULL, NULL }
+ tt_i64_op(INT64_MIN, ==, clamp_double_to_int64(-INFINITY_DBL));
+ tt_i64_op(INT64_MIN, ==,
+ clamp_double_to_int64(-1.0 * pow(2.0, 64.0) - 1.0));
+ tt_i64_op(INT64_MIN, ==,
+ clamp_double_to_int64(-1.0 * pow(2.0, 63.0) - 1.0));
+ tt_i64_op(((uint64_t) -1) << 53, ==,
+ clamp_double_to_int64(-1.0 * pow(2.0, 53.0)));
+ tt_i64_op((((uint64_t) -1) << 53) + 1, ==,
+ clamp_double_to_int64(-1.0 * pow(2.0, 53.0) + 1.0));
+ tt_i64_op(-1, ==, clamp_double_to_int64(-1.0));
+ tt_i64_op(0, ==, clamp_double_to_int64(-0.9));
+ tt_i64_op(0, ==, clamp_double_to_int64(-0.1));
+ tt_i64_op(0, ==, clamp_double_to_int64(0.0));
+ tt_i64_op(0, ==, clamp_double_to_int64(NAN_DBL));
+ tt_i64_op(0, ==, clamp_double_to_int64(0.1));
+ tt_i64_op(0, ==, clamp_double_to_int64(0.9));
+ tt_i64_op(1, ==, clamp_double_to_int64(1.0));
+ tt_i64_op((((int64_t) 1) << 53) - 1, ==,
+ clamp_double_to_int64(pow(2.0, 53.0) - 1.0));
+ tt_i64_op(((int64_t) 1) << 53, ==,
+ clamp_double_to_int64(pow(2.0, 53.0)));
+ tt_i64_op(INT64_MAX, ==,
+ clamp_double_to_int64(pow(2.0, 63.0)));
+ tt_i64_op(INT64_MAX, ==,
+ clamp_double_to_int64(pow(2.0, 64.0)));
+ tt_i64_op(INT64_MAX, ==, clamp_double_to_int64(INFINITY_DBL));
+
+ done:
+ ;
+}
#ifdef FD_CLOEXEC
#define CAN_CHECK_CLOEXEC
@@ -4180,9 +5054,14 @@ fd_is_nonblocking(tor_socket_t fd)
}
#endif
+#define ERRNO_IS_EPROTO(e) (e == SOCK_ERRNO(EPROTONOSUPPORT))
+#define SOCK_ERR_IS_EPROTO(s) ERRNO_IS_EPROTO(tor_socket_errno(s))
+
+/* Test for tor_open_socket*, using IPv4 or IPv6 depending on arg. */
static void
test_util_socket(void *arg)
{
+ const int domain = !strcmp(arg, "4") ? AF_INET : AF_INET6;
tor_socket_t fd1 = TOR_INVALID_SOCKET;
tor_socket_t fd2 = TOR_INVALID_SOCKET;
tor_socket_t fd3 = TOR_INVALID_SOCKET;
@@ -4193,15 +5072,20 @@ test_util_socket(void *arg)
(void)arg;
- fd1 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 0);
- fd2 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 1);
+ fd1 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 0, 0);
+ int err = tor_socket_errno(fd1);
+ if (fd1 < 0 && err == SOCK_ERRNO(EPROTONOSUPPORT)) {
+ /* Assume we're on an IPv4-only or IPv6-only system, and give up now. */
+ goto done;
+ }
+ fd2 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 0, 1);
tt_assert(SOCKET_OK(fd1));
tt_assert(SOCKET_OK(fd2));
tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
- //fd3 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 0);
- //fd4 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 1);
- fd3 = tor_open_socket(AF_INET, SOCK_STREAM, 0);
- fd4 = tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, 0);
+ //fd3 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 1, 0);
+ //fd4 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 1, 1);
+ fd3 = tor_open_socket(domain, SOCK_STREAM, 0);
+ fd4 = tor_open_socket_nonblocking(domain, SOCK_STREAM, 0);
tt_assert(SOCKET_OK(fd3));
tt_assert(SOCKET_OK(fd4));
tt_int_op(get_n_open_sockets(), OP_EQ, n + 4);
@@ -4219,26 +5103,62 @@ test_util_socket(void *arg)
tt_int_op(fd_is_nonblocking(fd4), OP_EQ, 1);
#endif
- tor_close_socket(fd1);
- tor_close_socket(fd2);
+ tor_assert(tor_close_socket == tor_close_socket__real);
+
+ /* we use close_socket__real here so that coverity can tell that we are
+ * really closing these sockets. */
+ tor_close_socket__real(fd1);
+ tor_close_socket__real(fd2);
fd1 = fd2 = TOR_INVALID_SOCKET;
tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
- tor_close_socket(fd3);
- tor_close_socket(fd4);
+ tor_close_socket__real(fd3);
+ tor_close_socket__real(fd4);
fd3 = fd4 = TOR_INVALID_SOCKET;
tt_int_op(get_n_open_sockets(), OP_EQ, n);
done:
if (SOCKET_OK(fd1))
- tor_close_socket(fd1);
+ tor_close_socket__real(fd1);
if (SOCKET_OK(fd2))
- tor_close_socket(fd2);
+ tor_close_socket__real(fd2);
if (SOCKET_OK(fd3))
- tor_close_socket(fd3);
+ tor_close_socket__real(fd3);
if (SOCKET_OK(fd4))
- tor_close_socket(fd4);
+ tor_close_socket__real(fd4);
}
+#if 0
+static int
+is_there_a_localhost(int family)
+{
+ tor_socket_t s;
+ s = tor_open_socket(family, SOCK_STREAM, IPPROTO_TCP);
+ tor_assert(SOCKET_OK(s));
+
+ int result = 0;
+ if (family == AF_INET) {
+ struct sockaddr_in s_in;
+ memset(&s_in, 0, sizeof(s_in));
+ s_in.sin_family = AF_INET;
+ s_in.sin_addr.s_addr = htonl(0x7f000001);
+ s_in.sin_port = 0;
+
+ if (bind(s, (void*)&s_in, sizeof(s_in)) == 0) {
+ result = 1;
+ }
+ } else if (family == AF_INET6) {
+ struct sockaddr_in6 sin6;
+ memset(&sin6, 0, sizeof(sin6));
+ sin6.sin6_family = AF_INET6;
+ sin6.sin6_addr.s6_addr[15] = 1;
+ sin6.sin6_port = 0;
+ }
+ tor_close_socket(s);
+
+ return result;
+}
+#endif
+
/* Test for socketpair and ersatz_socketpair(). We test them both, since
* the latter is a tolerably good way to exersize tor_accept_socket(). */
static void
@@ -4250,8 +5170,23 @@ test_util_socketpair(void *arg)
int n = get_n_open_sockets();
tor_socket_t fds[2] = {TOR_INVALID_SOCKET, TOR_INVALID_SOCKET};
const int family = AF_UNIX;
+ int socketpair_result = 0;
+
+ socketpair_result = tor_socketpair_fn(family, SOCK_STREAM, 0, fds);
+
+#ifdef __FreeBSD__
+ /* If there is no 127.0.0.1, tor_ersatz_socketpair will and must fail.
+ * Otherwise, we risk exposing a socketpair on a routable IP address. (Some
+ * BSD jails use a routable address for localhost. Fortunately, they have
+ * the real AF_UNIX socketpair.) */
+ if (ersatz && socketpair_result < 0) {
+ /* In my testing, an IPv6-only FreeBSD jail without ::1 returned EINVAL.
+ * Assume we're on a machine without 127.0.0.1 or ::1 and give up now. */
+ tt_skip();
+ }
+#endif
+ tt_int_op(0, OP_EQ, socketpair_result);
- tt_int_op(0, OP_EQ, tor_socketpair_fn(family, SOCK_STREAM, 0, fds));
tt_assert(SOCKET_OK(fds[0]));
tt_assert(SOCKET_OK(fds[1]));
tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
@@ -4271,6 +5206,8 @@ test_util_socketpair(void *arg)
tor_close_socket(fds[1]);
}
+#undef SOCKET_EPROTO
+
static void
test_util_max_mem(void *arg)
{
@@ -4377,7 +5314,7 @@ test_util_writepid(void *arg)
contents = read_file_to_str(fname, 0, NULL);
tt_assert(contents);
- int n = sscanf(contents, "%lu\n%c", &pid, &c);
+ int n = tor_sscanf(contents, "%lu\n%c", &pid, &c);
tt_int_op(n, OP_EQ, 1);
#ifdef _WIN32
@@ -4414,6 +5351,283 @@ test_util_get_avail_disk_space(void *arg)
;
}
+static void
+test_util_touch_file(void *arg)
+{
+ (void) arg;
+ const char *fname = get_fname("touch");
+
+ const time_t now = time(NULL);
+ struct stat st;
+ write_bytes_to_file(fname, "abc", 3, 1);
+ tt_int_op(0, OP_EQ, stat(fname, &st));
+ /* A subtle point: the filesystem time is not necessarily equal to the
+ * system clock time, since one can be using a monotonic clock, or coarse
+ * monotonic clock, or whatever. So we might wind up with an mtime a few
+ * microseconds ago. Let's just give it a lot of wiggle room. */
+ tt_i64_op(st.st_mtime, OP_GE, now - 1);
+
+ const time_t five_sec_ago = now - 5;
+ struct utimbuf u = { five_sec_ago, five_sec_ago };
+ tt_int_op(0, OP_EQ, utime(fname, &u));
+ tt_int_op(0, OP_EQ, stat(fname, &st));
+ /* Let's hope that utime/stat give the same second as a round-trip? */
+ tt_i64_op(st.st_mtime, OP_EQ, five_sec_ago);
+
+ /* Finally we can touch the file */
+ tt_int_op(0, OP_EQ, touch_file(fname));
+ tt_int_op(0, OP_EQ, stat(fname, &st));
+ tt_i64_op(st.st_mtime, OP_GE, now-1);
+
+ done:
+ ;
+}
+
+#ifndef _WIN32
+static void
+test_util_pwdb(void *arg)
+{
+ (void) arg;
+ const struct passwd *me = NULL, *me2, *me3;
+ char *name = NULL;
+ char *dir = NULL;
+
+ /* Uncached case. */
+ /* Let's assume that we exist. */
+ me = tor_getpwuid(getuid());
+ tt_assert(me != NULL);
+ name = tor_strdup(me->pw_name);
+
+ /* Uncached case */
+ me2 = tor_getpwnam(name);
+ tt_assert(me2 != NULL);
+ tt_int_op(me2->pw_uid, OP_EQ, getuid());
+
+ /* Cached case */
+ me3 = tor_getpwuid(getuid());
+ tt_assert(me3 != NULL);
+ tt_str_op(me3->pw_name, OP_EQ, name);
+
+ me3 = tor_getpwnam(name);
+ tt_assert(me3 != NULL);
+ tt_int_op(me3->pw_uid, OP_EQ, getuid());
+
+ dir = get_user_homedir(name);
+ tt_assert(dir != NULL);
+
+ /* Try failing cases. First find a user that doesn't exist by name */
+ char randbytes[4];
+ char badname[9];
+ int i, found=0;
+ for (i = 0; i < 100; ++i) {
+ crypto_rand(randbytes, sizeof(randbytes));
+ base16_encode(badname, sizeof(badname), randbytes, sizeof(randbytes));
+ if (tor_getpwnam(badname) == NULL) {
+ found = 1;
+ break;
+ }
+ }
+ tt_assert(found);
+ tor_free(dir);
+
+ /* We should do a LOG_ERR */
+ setup_full_capture_of_logs(LOG_ERR);
+ dir = get_user_homedir(badname);
+ tt_assert(dir == NULL);
+ expect_log_msg_containing("not found");
+ tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
+ teardown_capture_of_logs();
+
+ /* Now try to find a user that doesn't exist by ID. */
+ found = 0;
+ for (i = 0; i < 1000; ++i) {
+ uid_t u;
+ crypto_rand((char*)&u, sizeof(u));
+ if (tor_getpwuid(u) == NULL) {
+ found = 1;
+ break;
+ }
+ }
+ tt_assert(found);
+
+ done:
+ tor_free(name);
+ tor_free(dir);
+ teardown_capture_of_logs();
+}
+#endif
+
+static void
+test_util_calloc_check(void *arg)
+{
+ (void) arg;
+ /* Easy cases that are good. */
+ tt_assert(size_mul_check__(0,0));
+ tt_assert(size_mul_check__(0,100));
+ tt_assert(size_mul_check__(100,0));
+ tt_assert(size_mul_check__(100,100));
+
+ /* Harder cases that are still good. */
+ tt_assert(size_mul_check__(SIZE_MAX, 1));
+ tt_assert(size_mul_check__(1, SIZE_MAX));
+ tt_assert(size_mul_check__(SIZE_MAX / 10, 9));
+ tt_assert(size_mul_check__(11, SIZE_MAX / 12));
+ const size_t sqrt_size_max_p1 = ((size_t)1) << (sizeof(size_t) * 4);
+ tt_assert(size_mul_check__(sqrt_size_max_p1, sqrt_size_max_p1 - 1));
+
+ /* Cases that overflow */
+ tt_assert(! size_mul_check__(SIZE_MAX, 2));
+ tt_assert(! size_mul_check__(2, SIZE_MAX));
+ tt_assert(! size_mul_check__(SIZE_MAX / 10, 11));
+ tt_assert(! size_mul_check__(11, SIZE_MAX / 10));
+ tt_assert(! size_mul_check__(SIZE_MAX / 8, 9));
+ tt_assert(! size_mul_check__(sqrt_size_max_p1, sqrt_size_max_p1));
+
+ done:
+ ;
+}
+
+static void
+test_util_monotonic_time(void *arg)
+{
+ (void)arg;
+
+ monotime_t mt1, mt2;
+ monotime_coarse_t mtc1, mtc2;
+ uint64_t nsec1, nsec2, usec1, msec1;
+ uint64_t nsecc1, nsecc2, usecc1, msecc1;
+
+ monotime_init();
+
+ monotime_get(&mt1);
+ monotime_coarse_get(&mtc1);
+ nsec1 = monotime_absolute_nsec();
+ usec1 = monotime_absolute_usec();
+ msec1 = monotime_absolute_msec();
+ nsecc1 = monotime_coarse_absolute_nsec();
+ usecc1 = monotime_coarse_absolute_usec();
+ msecc1 = monotime_coarse_absolute_msec();
+
+ tor_sleep_msec(200);
+
+ monotime_get(&mt2);
+ monotime_coarse_get(&mtc2);
+ nsec2 = monotime_absolute_nsec();
+ nsecc2 = monotime_coarse_absolute_nsec();
+
+ /* We need to be a little careful here since we don't know the system load.
+ */
+ tt_i64_op(monotime_diff_msec(&mt1, &mt2), OP_GE, 175);
+ tt_i64_op(monotime_diff_msec(&mt1, &mt2), OP_LT, 1000);
+ tt_i64_op(monotime_coarse_diff_msec(&mtc1, &mtc2), OP_GE, 125);
+ tt_i64_op(monotime_coarse_diff_msec(&mtc1, &mtc2), OP_LT, 1000);
+ tt_u64_op(nsec2-nsec1, OP_GE, 175000000);
+ tt_u64_op(nsec2-nsec1, OP_LT, 1000000000);
+ tt_u64_op(nsecc2-nsecc1, OP_GE, 125000000);
+ tt_u64_op(nsecc2-nsecc1, OP_LT, 1000000000);
+
+ tt_u64_op(msec1, OP_GE, nsec1 / 1000000);
+ tt_u64_op(usec1, OP_GE, nsec1 / 1000);
+ tt_u64_op(msecc1, OP_GE, nsecc1 / 1000000);
+ tt_u64_op(usecc1, OP_GE, nsecc1 / 1000);
+ tt_u64_op(msec1, OP_LE, nsec1 / 1000000 + 1);
+ tt_u64_op(usec1, OP_LE, nsec1 / 1000 + 1000);
+ tt_u64_op(msecc1, OP_LE, nsecc1 / 1000000 + 1);
+ tt_u64_op(usecc1, OP_LE, nsecc1 / 1000 + 1000);
+
+ done:
+ ;
+}
+
+static void
+test_util_monotonic_time_ratchet(void *arg)
+{
+ (void)arg;
+ monotime_init();
+ monotime_reset_ratchets_for_testing();
+
+ /* win32, performance counter ratchet. */
+ tt_i64_op(100, OP_EQ, ratchet_performance_counter(100));
+ tt_i64_op(101, OP_EQ, ratchet_performance_counter(101));
+ tt_i64_op(2000, OP_EQ, ratchet_performance_counter(2000));
+ tt_i64_op(2000, OP_EQ, ratchet_performance_counter(100));
+ tt_i64_op(2005, OP_EQ, ratchet_performance_counter(105));
+ tt_i64_op(3005, OP_EQ, ratchet_performance_counter(1105));
+ tt_i64_op(3005, OP_EQ, ratchet_performance_counter(1000));
+ tt_i64_op(3010, OP_EQ, ratchet_performance_counter(1005));
+
+ /* win32, GetTickCounts32 ratchet-and-rollover-detector. */
+ const int64_t R = ((int64_t)1) << 32;
+ tt_i64_op(5, OP_EQ, ratchet_coarse_performance_counter(5));
+ tt_i64_op(1000, OP_EQ, ratchet_coarse_performance_counter(1000));
+ tt_i64_op(5+R, OP_EQ, ratchet_coarse_performance_counter(5));
+ tt_i64_op(10+R, OP_EQ, ratchet_coarse_performance_counter(10));
+ tt_i64_op(4+R*2, OP_EQ, ratchet_coarse_performance_counter(4));
+
+ /* gettimeofday regular ratchet. */
+ struct timeval tv_in = {0,0}, tv_out;
+ tv_in.tv_usec = 9000;
+
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 9000);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 0);
+
+ tv_in.tv_sec = 1337;
+ tv_in.tv_usec = 0;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 0);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
+
+ tv_in.tv_sec = 1336;
+ tv_in.tv_usec = 500000;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 0);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
+
+ tv_in.tv_sec = 1337;
+ tv_in.tv_usec = 0;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 500000);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
+
+ tv_in.tv_sec = 1337;
+ tv_in.tv_usec = 600000;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 100000);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 1338);
+
+ tv_in.tv_sec = 1000;
+ tv_in.tv_usec = 1000;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 100000);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 1338);
+
+ tv_in.tv_sec = 2000;
+ tv_in.tv_usec = 2000;
+ ratchet_timeval(&tv_in, &tv_out);
+ tt_int_op(tv_out.tv_usec, OP_EQ, 101000);
+ tt_i64_op(tv_out.tv_sec, OP_EQ, 2338);
+
+ done:
+ ;
+}
+
+#define UTIL_LEGACY(name) \
+ { #name, test_util_ ## name , 0, NULL, NULL }
+
+#define UTIL_TEST(name, flags) \
+ { #name, test_util_ ## name, flags, NULL, NULL }
+
+#ifdef _WIN32
+#define UTIL_TEST_NO_WIN(n, f) { #n, NULL, TT_SKIP, NULL, NULL }
+#define UTIL_TEST_WIN_ONLY(n, f) UTIL_TEST(n, (f))
+#define UTIL_LEGACY_NO_WIN(n) UTIL_TEST_NO_WIN(n, 0)
+#else
+#define UTIL_TEST_NO_WIN(n, f) UTIL_TEST(n, (f))
+#define UTIL_TEST_WIN_ONLY(n, f) { #n, NULL, TT_SKIP, NULL, NULL }
+#define UTIL_LEGACY_NO_WIN(n) UTIL_LEGACY(n)
+#endif
+
struct testcase_t util_tests[] = {
UTIL_LEGACY(time),
UTIL_TEST(parse_http_time, 0),
@@ -4421,19 +5635,20 @@ struct testcase_t util_tests[] = {
UTIL_LEGACY(config_line_quotes),
UTIL_LEGACY(config_line_comment_character),
UTIL_LEGACY(config_line_escaped_content),
-#ifndef _WIN32
- UTIL_LEGACY(expand_filename),
-#endif
+ UTIL_LEGACY(config_line_crlf),
+ UTIL_LEGACY_NO_WIN(expand_filename),
UTIL_LEGACY(escape_string_socks),
UTIL_LEGACY(string_is_key_value),
UTIL_LEGACY(strmisc),
+ UTIL_TEST(parse_integer, 0),
UTIL_LEGACY(pow2),
UTIL_LEGACY(gzip),
+ UTIL_TEST(gzip_compression_bomb, TT_FORK),
UTIL_LEGACY(datadir),
UTIL_LEGACY(memarea),
UTIL_LEGACY(control_formats),
UTIL_LEGACY(mmap),
- UTIL_LEGACY(sscanf),
+ UTIL_TEST(sscanf, TT_FORK),
UTIL_LEGACY(format_time_interval),
UTIL_LEGACY(path_is_relative),
UTIL_LEGACY(strtok),
@@ -4441,19 +5656,17 @@ struct testcase_t util_tests[] = {
UTIL_TEST(di_map, 0),
UTIL_TEST(round_to_next_multiple_of, 0),
UTIL_TEST(laplace, 0),
+ UTIL_TEST(clamp_double_to_int64, 0),
UTIL_TEST(find_str_at_start_of_line, 0),
UTIL_TEST(string_is_C_identifier, 0),
UTIL_TEST(asprintf, 0),
UTIL_TEST(listdir, 0),
UTIL_TEST(parent_dir, 0),
UTIL_TEST(ftruncate, 0),
-#ifdef _WIN32
- UTIL_TEST(load_win_lib, 0),
-#endif
-#ifndef _WIN32
- UTIL_TEST(exit_status, 0),
- UTIL_TEST(fgets_eagain, 0),
-#endif
+ UTIL_TEST(num_cpus, 0),
+ UTIL_TEST_WIN_ONLY(load_win_lib, 0),
+ UTIL_TEST_NO_WIN(exit_status, 0),
+ UTIL_TEST_NO_WIN(fgets_eagain, 0),
UTIL_TEST(format_hex_number, 0),
UTIL_TEST(format_dec_number, 0),
UTIL_TEST(join_win_cmdline, 0),
@@ -4472,8 +5685,12 @@ struct testcase_t util_tests[] = {
UTIL_TEST(read_file_eof_zero_bytes, 0),
UTIL_TEST(write_chunks_to_file, 0),
UTIL_TEST(mathlog, 0),
+ UTIL_TEST(fraction, 0),
UTIL_TEST(weak_random, 0),
- UTIL_TEST(socket, TT_FORK),
+ { "socket_ipv4", test_util_socket, TT_FORK, &passthrough_setup,
+ (void*)"4" },
+ { "socket_ipv6", test_util_socket, TT_FORK,
+ &passthrough_setup, (void*)"6" },
{ "socketpair", test_util_socketpair, TT_FORK, &passthrough_setup,
(void*)"0" },
{ "socketpair_ersatz", test_util_socketpair, TT_FORK,
@@ -4483,6 +5700,11 @@ struct testcase_t util_tests[] = {
UTIL_TEST(ipv4_validation, 0),
UTIL_TEST(writepid, 0),
UTIL_TEST(get_avail_disk_space, 0),
+ UTIL_TEST(touch_file, 0),
+ UTIL_TEST_NO_WIN(pwdb, TT_FORK),
+ UTIL_TEST(calloc_check, 0),
+ UTIL_TEST(monotonic_time, 0),
+ UTIL_TEST(monotonic_time_ratchet, TT_FORK),
END_OF_TESTCASES
};
diff --git a/src/test/test_util_format.c b/src/test/test_util_format.c
new file mode 100644
index 0000000000..63a668238c
--- /dev/null
+++ b/src/test/test_util_format.c
@@ -0,0 +1,395 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "or.h"
+
+#include "test.h"
+
+#define UTIL_FORMAT_PRIVATE
+#include "util_format.h"
+
+#define NS_MODULE util_format
+
+#if !defined(HAVE_HTONLL) && !defined(htonll)
+#ifdef WORDS_BIGENDIAN
+#define htonll(x) (x)
+#else
+static uint64_t
+htonll(uint64_t a)
+{
+ return htonl((uint32_t)(a>>32)) | (((uint64_t)htonl((uint32_t)a))<<32);
+}
+#endif
+#endif
+
+static void
+test_util_format_unaligned_accessors(void *ignored)
+{
+ (void)ignored;
+ char buf[9] = "onionsoup"; // 6f6e696f6e736f7570
+
+ tt_u64_op(get_uint64(buf+1), OP_EQ, htonll(U64_LITERAL(0x6e696f6e736f7570)));
+ tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
+ tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
+ tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);
+
+ set_uint8(buf+7, 0x61);
+ tt_mem_op(buf, OP_EQ, "onionsoap", 9);
+
+ set_uint16(buf+6, htons(0x746f));
+ tt_mem_op(buf, OP_EQ, "onionstop", 9);
+
+ set_uint32(buf+1, htonl(0x78696465));
+ tt_mem_op(buf, OP_EQ, "oxidestop", 9);
+
+ set_uint64(buf+1, htonll(U64_LITERAL(0x6266757363617465)));
+ tt_mem_op(buf, OP_EQ, "obfuscate", 9);
+ done:
+ ;
+}
+
+static void
+test_util_format_base64_encode(void *ignored)
+{
+ (void)ignored;
+ int res;
+ int i;
+ char *src;
+ char *dst;
+
+ src = tor_malloc_zero(256);
+ dst = tor_malloc_zero(1000);
+
+ for (i=0;i<256;i++) {
+ src[i] = (char)i;
+ }
+
+ res = base64_encode(NULL, 1, src, 1, 0);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_encode(dst, 1, NULL, 1, 0);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_encode(dst, 1, src, 10, 0);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_encode(dst, SSIZE_MAX-1, src, 1, 0);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_encode(dst, SSIZE_MAX-1, src, 10, 0);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_encode(dst, 1000, src, 256, 0);
+ tt_int_op(res, OP_EQ, 344);
+ tt_str_op(dst, OP_EQ, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh"
+ "8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
+ "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3"
+ "BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY"
+ "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wM"
+ "HCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp"
+ "6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
+
+ res = base64_encode(dst, 1000, src, 256, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 350);
+ tt_str_op(dst, OP_EQ,
+ "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
+ "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
+ "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
+ "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
+ "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
+ "8PHy8/T19vf4+fr7/P3+/w==\n");
+
+ res = base64_encode(dst, 1000, src+1, 255, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 346);
+
+ for (i = 0;i<50;i++) {
+ src[i] = 0;
+ }
+ src[50] = (char)255;
+ src[51] = (char)255;
+ src[52] = (char)255;
+ src[53] = (char)255;
+
+ res = base64_encode(dst, 1000, src, 54, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 74);
+
+ res = base64_encode(dst, 1000, src+1, 53, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 74);
+
+ res = base64_encode(dst, 1000, src+2, 52, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 74);
+
+ res = base64_encode(dst, 1000, src+3, 51, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 70);
+
+ res = base64_encode(dst, 1000, src+4, 50, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 70);
+
+ res = base64_encode(dst, 1000, src+5, 49, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 70);
+
+ res = base64_encode(dst, 1000, src+6, 48, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 65);
+
+ res = base64_encode(dst, 1000, src+7, 47, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 65);
+
+ res = base64_encode(dst, 1000, src+8, 46, BASE64_ENCODE_MULTILINE);
+ tt_int_op(res, OP_EQ, 65);
+
+ done:
+ tor_free(src);
+ tor_free(dst);
+}
+
+static void
+test_util_format_base64_decode_nopad(void *ignored)
+{
+ (void)ignored;
+ int res;
+ int i;
+ char *src;
+ uint8_t *dst, *real_dst;
+ uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
+ char real_src[] = "ZXhhbXBsZQ";
+
+ src = tor_malloc_zero(256);
+ dst = tor_malloc_zero(1000);
+ real_dst = tor_malloc_zero(10);
+
+ for (i=0;i<256;i++) {
+ src[i] = (char)i;
+ }
+
+ res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_decode_nopad(dst, 1, src, 5);
+ tt_int_op(res, OP_EQ, -1);
+
+ const char *s = "SGVsbG8gd29ybGQ";
+ res = base64_decode_nopad(dst, 1000, s, strlen(s));
+ tt_int_op(res, OP_EQ, 11);
+ tt_mem_op(dst, OP_EQ, "Hello world", 11);
+
+ s = "T3BhIG11bmRv";
+ res = base64_decode_nopad(dst, 9, s, strlen(s));
+ tt_int_op(res, OP_EQ, 9);
+ tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
+
+ res = base64_decode_nopad(real_dst, 10, real_src, 10);
+ tt_int_op(res, OP_EQ, 7);
+ tt_mem_op(real_dst, OP_EQ, expected, 7);
+
+ done:
+ tor_free(src);
+ tor_free(dst);
+ tor_free(real_dst);
+}
+
+static void
+test_util_format_base64_decode(void *ignored)
+{
+ (void)ignored;
+ int res;
+ int i;
+ char *src;
+ char *dst, *real_dst;
+ uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
+ char real_src[] = "ZXhhbXBsZQ==";
+
+ src = tor_malloc_zero(256);
+ dst = tor_malloc_zero(1000);
+ real_dst = tor_malloc_zero(10);
+
+ for (i=0;i<256;i++) {
+ src[i] = (char)i;
+ }
+
+ res = base64_decode(dst, 1, src, SIZE_T_CEILING);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
+ tt_int_op(res, OP_EQ, -1);
+
+ const char *s = "T3BhIG11bmRv";
+ res = base64_decode(dst, 9, s, strlen(s));
+ tt_int_op(res, OP_EQ, 9);
+ tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
+
+ memset(dst, 0, 1000);
+ res = base64_decode(dst, 100, s, strlen(s));
+ tt_int_op(res, OP_EQ, 9);
+ tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
+
+ s = "SGVsbG8gd29ybGQ=";
+ res = base64_decode(dst, 100, s, strlen(s));
+ tt_int_op(res, OP_EQ, 11);
+ tt_mem_op(dst, OP_EQ, "Hello world", 11);
+
+ res = base64_decode(real_dst, 10, real_src, 10);
+ tt_int_op(res, OP_EQ, 7);
+ tt_mem_op(real_dst, OP_EQ, expected, 7);
+
+ done:
+ tor_free(src);
+ tor_free(dst);
+ tor_free(real_dst);
+}
+
+static void
+test_util_format_base16_decode(void *ignored)
+{
+ (void)ignored;
+ int res;
+ int i;
+ char *src;
+ char *dst, *real_dst;
+ char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
+ char real_src[] = "6578616D706C65";
+
+ src = tor_malloc_zero(256);
+ dst = tor_malloc_zero(1000);
+ real_dst = tor_malloc_zero(10);
+
+ for (i=0;i<256;i++) {
+ src[i] = (char)i;
+ }
+
+ res = base16_decode(dst, 3, src, 3);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base16_decode(dst, 1, src, 10);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base16_decode(dst, ((size_t)INT_MAX)+1, src, 10);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base16_decode(dst, 1000, "", 0);
+ tt_int_op(res, OP_EQ, 0);
+
+ res = base16_decode(dst, 1000, "aabc", 4);
+ tt_int_op(res, OP_EQ, 2);
+ tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2);
+
+ res = base16_decode(dst, 1000, "aabcd", 6);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base16_decode(dst, 1000, "axxx", 4);
+ tt_int_op(res, OP_EQ, -1);
+
+ res = base16_decode(real_dst, 10, real_src, 14);
+ tt_int_op(res, OP_EQ, 7);
+ tt_mem_op(real_dst, OP_EQ, expected, 7);
+
+ done:
+ tor_free(src);
+ tor_free(dst);
+ tor_free(real_dst);
+}
+
+static void
+test_util_format_base32_encode(void *arg)
+{
+ (void) arg;
+ size_t real_dstlen = 32;
+ char *dst = tor_malloc_zero(real_dstlen);
+
+ /* Basic use case that doesn't require a source length correction. */
+ {
+ /* Length of 10 bytes. */
+ const char *src = "blahbleh12";
+ size_t srclen = strlen(src);
+ /* Expected result encoded base32. This was created using python as
+ * such (and same goes for all test case.):
+ *
+ * b = bytes("blahbleh12", 'utf-8')
+ * base64.b32encode(b)
+ * (result in lower case)
+ */
+ const char *expected = "mjwgc2dcnrswqmjs";
+
+ base32_encode(dst, base32_encoded_size(srclen), src, srclen);
+ tt_mem_op(expected, OP_EQ, dst, strlen(expected));
+ /* Encode but to a larger size destination. */
+ memset(dst, 0, real_dstlen);
+ base32_encode(dst, real_dstlen, src, srclen);
+ tt_mem_op(expected, OP_EQ, dst, strlen(expected));
+ }
+
+ /* Non multiple of 5 for the source buffer length. */
+ {
+ /* Length of 8 bytes. */
+ const char *expected = "mjwgc2dcnrswq";
+ const char *src = "blahbleh";
+ size_t srclen = strlen(src);
+
+ memset(dst, 0, real_dstlen);
+ base32_encode(dst, base32_encoded_size(srclen), src, srclen);
+ tt_mem_op(expected, OP_EQ, dst, strlen(expected));
+ }
+
+ done:
+ tor_free(dst);
+}
+
+static void
+test_util_format_base32_decode(void *arg)
+{
+ (void) arg;
+ int ret;
+ size_t real_dstlen = 32;
+ char *dst = tor_malloc_zero(real_dstlen);
+
+ /* Basic use case. */
+ {
+ /* Length of 10 bytes. */
+ const char *expected = "blahbleh12";
+ /* Expected result encoded base32. */
+ const char *src = "mjwgc2dcnrswqmjs";
+
+ ret = base32_decode(dst, strlen(expected), src, strlen(src));
+ tt_int_op(ret, ==, 0);
+ tt_str_op(expected, OP_EQ, dst);
+ }
+
+ /* Non multiple of 5 for the source buffer length. */
+ {
+ /* Length of 8 bytes. */
+ const char *expected = "blahbleh";
+ const char *src = "mjwgc2dcnrswq";
+
+ ret = base32_decode(dst, strlen(expected), src, strlen(src));
+ tt_int_op(ret, ==, 0);
+ tt_mem_op(expected, OP_EQ, dst, strlen(expected));
+ }
+
+ /* Invalid values. */
+ {
+ /* Invalid character '#'. */
+ ret = base32_decode(dst, real_dstlen, "#abcde", 6);
+ tt_int_op(ret, ==, -1);
+ /* Make sure the destination buffer has been zeroed even on error. */
+ tt_int_op(tor_mem_is_zero(dst, real_dstlen), ==, 1);
+ }
+
+ done:
+ tor_free(dst);
+}
+
+struct testcase_t util_format_tests[] = {
+ { "unaligned_accessors", test_util_format_unaligned_accessors, 0,
+ NULL, NULL },
+ { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
+ { "base64_decode_nopad", test_util_format_base64_decode_nopad, 0,
+ NULL, NULL },
+ { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
+ { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
+ { "base32_encode", test_util_format_base32_encode, 0,
+ NULL, NULL },
+ { "base32_decode", test_util_format_base32_decode, 0,
+ NULL, NULL },
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_util_process.c b/src/test/test_util_process.c
new file mode 100644
index 0000000000..4e75b97f3d
--- /dev/null
+++ b/src/test/test_util_process.c
@@ -0,0 +1,83 @@
+/* Copyright (c) 2010-2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define UTIL_PROCESS_PRIVATE
+#include "orconfig.h"
+#include "or.h"
+
+#include "test.h"
+
+#include "util_process.h"
+
+#include "log_test_helpers.h"
+
+#ifndef _WIN32
+#define NS_MODULE util_process
+
+static void
+temp_callback(int r, void *s)
+{
+ (void)r;
+ (void)s;
+}
+
+static void
+test_util_process_set_waitpid_callback(void *ignored)
+{
+ (void)ignored;
+ waitpid_callback_t *res1 = NULL, *res2 = NULL;
+ setup_full_capture_of_logs(LOG_WARN);
+ pid_t pid = (pid_t)42;
+
+ res1 = set_waitpid_callback(pid, temp_callback, NULL);
+ tt_assert(res1);
+
+ res2 = set_waitpid_callback(pid, temp_callback, NULL);
+ tt_assert(res2);
+ expect_single_log_msg(
+ "Replaced a waitpid monitor on pid 42. That should be "
+ "impossible.\n");
+
+ done:
+ teardown_capture_of_logs();
+ clear_waitpid_callback(res1);
+ clear_waitpid_callback(res2);
+}
+
+static void
+test_util_process_clear_waitpid_callback(void *ignored)
+{
+ (void)ignored;
+ waitpid_callback_t *res;
+ setup_capture_of_logs(LOG_WARN);
+ pid_t pid = (pid_t)43;
+
+ clear_waitpid_callback(NULL);
+
+ res = set_waitpid_callback(pid, temp_callback, NULL);
+ clear_waitpid_callback(res);
+ expect_no_log_entry();
+
+#if 0
+ /* No. This is use-after-free. We don't _do_ that. XXXX */
+ clear_waitpid_callback(res);
+ expect_log_msg("Couldn't remove waitpid monitor for pid 43.\n");
+#endif
+
+ done:
+ teardown_capture_of_logs();
+}
+#endif /* _WIN32 */
+
+#ifndef _WIN32
+#define TEST(name) { #name, test_util_process_##name, 0, NULL, NULL }
+#else
+#define TEST(name) { #name, NULL, TT_SKIP, NULL, NULL }
+#endif
+
+struct testcase_t util_process_tests[] = {
+ TEST(set_waitpid_callback),
+ TEST(clear_waitpid_callback),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_util_slow.c b/src/test/test_util_slow.c
index dcd0c9af36..1e7160598c 100644
--- a/src/test/test_util_slow.c
+++ b/src/test/test_util_slow.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
diff --git a/src/test/test_workqueue.c b/src/test/test_workqueue.c
index 0d79733cf0..ccb8d0c8ca 100644
--- a/src/test/test_workqueue.c
+++ b/src/test/test_workqueue.c
@@ -1,6 +1,6 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
@@ -12,11 +12,7 @@
#include "compat_libevent.h"
#include <stdio.h>
-#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
-#else
-#include <event.h>
-#endif
#define MAX_INFLIGHT (1<<16)
@@ -390,10 +386,19 @@ main(int argc, char **argv)
init_logging(1);
network_init();
- crypto_global_init(1, NULL, NULL);
- crypto_seed_rng();
+ if (crypto_global_init(1, NULL, NULL) < 0) {
+ printf("Couldn't initialize crypto subsystem; exiting.\n");
+ return 1;
+ }
+ if (crypto_seed_rng() < 0) {
+ printf("Couldn't seed RNG; exiting.\n");
+ return 1;
+ }
rq = replyqueue_new(as_flags);
+ if (as_flags && rq == NULL)
+ return 77; // 77 means "skipped".
+
tor_assert(rq);
tp = threadpool_new(opt_n_threads,
rq, new_state, free_state, NULL);
diff --git a/src/test/test_workqueue_cancel.sh b/src/test/test_workqueue_cancel.sh
new file mode 100755
index 0000000000..f7c663171e
--- /dev/null
+++ b/src/test/test_workqueue_cancel.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue -C 1
+
diff --git a/src/test/test_workqueue_efd.sh b/src/test/test_workqueue_efd.sh
new file mode 100755
index 0000000000..4d89396819
--- /dev/null
+++ b/src/test/test_workqueue_efd.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue \
+ --no-eventfd2 --no-pipe2 --no-pipe --no-socketpair
diff --git a/src/test/test_workqueue_efd2.sh b/src/test/test_workqueue_efd2.sh
new file mode 100755
index 0000000000..7cfff45ff3
--- /dev/null
+++ b/src/test/test_workqueue_efd2.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue \
+ --no-eventfd --no-pipe2 --no-pipe --no-socketpair
diff --git a/src/test/test_workqueue_pipe.sh b/src/test/test_workqueue_pipe.sh
new file mode 100755
index 0000000000..afcef87853
--- /dev/null
+++ b/src/test/test_workqueue_pipe.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue \
+ --no-eventfd2 --no-eventfd --no-pipe2 --no-socketpair
diff --git a/src/test/test_workqueue_pipe2.sh b/src/test/test_workqueue_pipe2.sh
new file mode 100755
index 0000000000..a20a1427e0
--- /dev/null
+++ b/src/test/test_workqueue_pipe2.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue \
+ --no-eventfd2 --no-eventfd --no-pipe --no-socketpair
diff --git a/src/test/test_workqueue_socketpair.sh b/src/test/test_workqueue_socketpair.sh
new file mode 100755
index 0000000000..76af79746d
--- /dev/null
+++ b/src/test/test_workqueue_socketpair.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+${builddir:-.}/src/test/test_workqueue \
+ --no-eventfd2 --no-eventfd --no-pipe2 --no-pipe
diff --git a/src/test/testing_common.c b/src/test/testing_common.c
index 441024bd7d..9c6580f788 100644
--- a/src/test/testing_common.c
+++ b/src/test/testing_common.c
@@ -1,8 +1,10 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
+ * Copyright (c) 2007-2016, The Tor Project, Inc. */
/* See LICENSE for licensing information */
+extern const char tor_git_revision[];
+
/* Ordinarily defined in tor_main.c; this bit is just here to provide one
* since we're not linking to tor_main.c */
const char tor_git_revision[] = "";
@@ -100,18 +102,41 @@ setup_directory(void)
temp_dir_setup_in_pid = getpid();
}
-/** Return a filename relative to our testing temporary directory */
-const char *
-get_fname(const char *name)
+/** 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)
{
static char buf[1024];
setup_directory();
if (!name)
return temp_dir;
- tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
+ tor_snprintf(buf,sizeof(buf),"%s/%s%s%s",temp_dir,name,suffix ? "_" : "",
+ suffix ? suffix : "");
return buf;
}
+/** 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);
+}
+
+/** 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);
+}
+
/* Remove a directory and all of its subdirectories */
static void
rm_rf(const char *dir)
@@ -154,37 +179,51 @@ remove_directory(void)
}
/** Define this if unit tests spend too much time generating public keys*/
-#undef CACHE_GENERATED_KEYS
+#define CACHE_GENERATED_KEYS
-static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
-#define N_PREGEN_KEYS ARRAY_LENGTH(pregen_keys)
+#define N_PREGEN_KEYS 11
+static crypto_pk_t *pregen_keys[N_PREGEN_KEYS];
+static int next_key_idx;
/** Generate and return a new keypair for use in unit tests. If we're using
- * the key cache optimization, we might reuse keys: we only guarantee that
- * keys made with distinct values for <b>idx</b> are different. The value of
- * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
+ * the key cache optimization, we might reuse keys. "idx" is ignored.
+ * Our only guarantee is that we won't reuse a key till this function has been
+ * called several times. The order in which keys are returned is slightly
+ * randomized, so that tests that depend on a particular order will not be
+ * reliable. */
crypto_pk_t *
pk_generate(int idx)
{
- int res;
+ (void) idx;
#ifdef CACHE_GENERATED_KEYS
- tor_assert(idx < N_PREGEN_KEYS);
- if (! pregen_keys[idx]) {
- pregen_keys[idx] = crypto_pk_new();
- res = crypto_pk_generate_key(pregen_keys[idx]);
- tor_assert(!res);
- }
- return crypto_pk_dup_key(pregen_keys[idx]);
+ /* Either skip 1 or 2 keys. */
+ next_key_idx += crypto_rand_int_range(1,3);
+ next_key_idx %= N_PREGEN_KEYS;
+ return crypto_pk_dup_key(pregen_keys[next_key_idx]);
#else
crypto_pk_t *result;
- (void) idx;
+ int res;
result = crypto_pk_new();
- res = crypto_pk_generate_key(result);
+ res = crypto_pk_generate_key__real(result);
tor_assert(!res);
return result;
#endif
}
+#ifdef CACHE_GENERATED_KEYS
+static int
+crypto_pk_generate_key_with_bits__get_cached(crypto_pk_t *env, int bits)
+{
+ if (bits != 1024)
+ return crypto_pk_generate_key_with_bits__real(env, bits);
+
+ crypto_pk_t *newkey = pk_generate(0);
+ crypto_pk_assign_(env, newkey);
+ crypto_pk_free(newkey);
+ return 0;
+}
+#endif
+
/** Free all storage used for the cached key optimization. */
static void
free_pregenerated_keys(void)
@@ -201,6 +240,9 @@ free_pregenerated_keys(void)
static void *
passthrough_test_setup(const struct testcase_t *testcase)
{
+ /* Make sure the passthrough doesn't unintentionally fail or skip tests */
+ tor_assert(testcase->setup_data);
+ tor_assert(testcase->setup_data != (void*)TT_SKIP);
return testcase->setup_data;
}
static int
@@ -211,11 +253,33 @@ passthrough_test_cleanup(const struct testcase_t *testcase, void *ptr)
return 1;
}
+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
+};
+
const struct testcase_setup_t passthrough_setup = {
passthrough_test_setup, passthrough_test_cleanup
};
-extern struct testgroup_t testgroups[];
+static void
+an_assertion_failed(void)
+{
+ tinytest_set_test_failed_();
+}
/** Main entry point for unit test code: parse the command line, and run
* some unit tests. */
@@ -228,6 +292,9 @@ main(int c, const char **v)
int loglevel = LOG_ERR;
int accel_crypto = 0;
+ /* We must initialise logs before we call tor_assert() */
+ init_logging(1);
+
#ifdef USE_DMALLOC
{
int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
@@ -238,8 +305,16 @@ main(int c, const char **v)
update_approx_time(time(NULL));
options = options_new();
tor_threads_init();
+
+ network_init();
+
+ monotime_init();
+
+ struct tor_libevent_cfg cfg;
+ memset(&cfg, 0, sizeof(cfg));
+ tor_libevent_initialize(&cfg);
+
control_initialize_event_queue();
- init_logging(1);
configure_backtrace_handler(get_version());
for (i_out = i = 1; i < c; ++i) {
@@ -263,6 +338,8 @@ main(int c, const char **v)
log_severity_list_t s;
memset(&s, 0, sizeof(s));
set_log_severity_config(loglevel, LOG_ERR, &s);
+ /* ALWAYS log bug warnings. */
+ s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
add_stream_log(&s, "", fileno(stdout));
}
@@ -272,9 +349,11 @@ main(int c, const char **v)
return 1;
}
crypto_set_tls_dh_prime();
- crypto_seed_rng();
+ if (crypto_seed_rng() < 0) {
+ printf("Couldn't seed RNG; exiting.\n");
+ return 1;
+ }
rep_hist_init();
- network_init();
setup_directory();
options_init(options);
options->DataDirectory = tor_strdup(temp_dir);
@@ -284,6 +363,17 @@ main(int c, const char **v)
tor_free(errmsg);
return 1;
}
+ tor_set_failed_assertion_callback(an_assertion_failed);
+
+#ifdef CACHE_GENERATED_KEYS
+ for (i = 0; i < N_PREGEN_KEYS; ++i) {
+ pregen_keys[i] = crypto_pk_new();
+ int r = crypto_pk_generate_key(pregen_keys[i]);
+ tor_assert(r == 0);
+ }
+ MOCK(crypto_pk_generate_key_with_bits,
+ crypto_pk_generate_key_with_bits__get_cached);
+#endif
atexit(remove_directory);
@@ -294,6 +384,7 @@ main(int c, const char **v)
tor_free_all(0);
dmalloc_log_unfreed();
#endif
+ crypto_global_cleanup();
if (have_failed)
return 1;
diff --git a/src/test/vote_descriptors.inc b/src/test/vote_descriptors.inc
new file mode 100644
index 0000000000..895dc6c65c
--- /dev/null
+++ b/src/test/vote_descriptors.inc
@@ -0,0 +1,94 @@
+static const char* VOTE_BODY_V3 =
+"network-status-version 3\n"
+"vote-status vote\n"
+"consensus-methods 13 14 15 16 17 18 19 20 21\n"
+"published 2015-09-02 19:34:15\n"
+"valid-after 2015-09-02 19:50:55\n"
+"fresh-until 2015-09-02 20:07:38\n"
+"valid-until 2015-09-02 20:24:15\n"
+"voting-delay 100 250\n"
+"client-versions 0.1.2.14,0.1.2.17\n"
+"server-versions 0.1.2.10,0.1.2.15,0.1.2.16\n"
+"known-flags Authority Exit Fast Guard MadeOfCheese MadeOfTin Running Stable V2Dir Valid\n"
+"flag-thresholds stable-uptime=0 stable-mtbf=0 fast-speed=0 guard-wfu=0.000% guard-tk=0 guard-bw-inc-exits=0 guard-bw-exc-exits=0 enough-mtbf=0 ignoring-advertised-bws=0\n"
+"params circuitwindow=80 foo=660\n"
+"dir-source Voter3 D867ACF56A9D229B35C25F0090BC9867E906BE69 3.4.5.6 3.4.5.6 80 9000\n"
+"contact voter@example.com\n"
+"legacy-dir-key 4141414141414141414141414141414141414141\n"
+"dir-key-certificate-version 3\n"
+"fingerprint D867ACF56A9D229B35C25F0090BC9867E906BE69\n"
+"dir-key-published 2008-12-12 18:07:24\n"
+"dir-key-expires 2009-12-12 18:07:24\n"
+"dir-identity-key\n"
+"-----BEGIN RSA PUBLIC KEY-----\n"
+"MIIBigKCAYEAveMpKlw8oD1YqFqpJchuwSR82BDhutbqgHiez3QO9FmzOctJpV+Y\n"
+"mpTYIJLS/qC+4GBKFF1VK0C4SoBrS3zri0qdXdE+vBGcyrxrjMklpxoqSKRY2011\n"
+"4eqYPghKlo5RzuqteBclGCHyNxWjUJeRKDWgvh+U/gr2uYM6fRm5q0fCzg4aECE7\n"
+"VP6fDGZrMbQI8jHpiMSoC9gkUASNEa6chLInlnP8/H5qUEW4TB9CN/q095pefuwL\n"
+"P+F+1Nz5hnM7fa5XmeMB8iM4RriUmOQlLBZgpQBMpEfWMIPcR9F1Gh3MxERqqUcH\n"
+"tmij+IZdeXg9OkCXykcabaYIhZD3meErn9Tax4oA/THduLfgli9zM0ExwzH1OooN\n"
+"L8rIcJ+2eBo3bQiQUbdYW71sl9w7nSPtircbJUa1mUvWYLPWQxFliPiQSetgJLMj\n"
+"VQqtPmV2hvN2Xk3lLfJO50qMTK7w7Gsaw8UtV4YDM1Hcjp/hQaIB1xfwhXgl+eUU\n"
+"btUa4c+cUTjHAgMBAAE=\n"
+"-----END RSA PUBLIC KEY-----\n"
+"dir-signing-key\n"
+"-----BEGIN RSA PUBLIC KEY-----\n"
+"MIGJAoGBALPSUInyuEu6NV3NjozplaniIEBzQXEjv1x9/+mqnwZABpYVmuy9A8nx\n"
+"eoyY3sZFsnYwNW/IZjAgG23pEmevu3F+L4myMjjaa6ORl3MgRYQ4gmuFqpefrGdm\n"
+"ywRCleh2JerkQ4VxOuq10dn/abITzLyaZzMw30KXWp5pxKXOLtxFAgMBAAE=\n"
+"-----END RSA PUBLIC KEY-----\n"
+"dir-key-crosscert\n"
+"-----BEGIN ID SIGNATURE-----\n"
+"FTBJNR/Hlt4T53yUMp1r/QCSMCpkHJCbYBT0R0pvYqhqFfYN5qHRSICRXaFFImIF\n"
+"0DGWmwRza6DxPKNzkm5/b7I0de9zJW1jNNdQAQK5xppAtQcAafRdu8cBonnmh9KX\n"
+"k1NrAK/X00FYywju3yl/SxCn1GddVNkHYexEudmJMPM=\n"
+"-----END ID SIGNATURE-----\n"
+"dir-key-certification\n"
+"-----BEGIN SIGNATURE-----\n"
+"pjWguLFBfELZDc6DywL6Do21SCl7LcutfpM92MEn4WYeSNcTXNR6lRX7reOEJk4e\n"
+"NwEaMt+Hl7slgeR5wjnW3OmMmRPZK9bquNWbfD+sAOV9bRFZTpXIdleAQFPlwvMF\n"
+"z/Gzwspzn4i2Yh6hySShrctMmW8YL3OM8LsBXzBhp/rG2uHlsxmIsc13DA6HWt61\n"
+"ffY72uNE6KckDGsQ4wPGP9q69y6g+X+TNio1KPbsILbePv6EjbO+rS8FiS4njPlg\n"
+"SPYry1RaUvxzxTkswIzdE1tjJrUiqpbWlTGxrH9N4OszoLm45Pc784KLULrjKIoi\n"
+"Q+vRsGrcMBAa+kDowWU6H1ryKR7KOhzRTcf2uqLE/W3ezaRwmOG+ETmoVFwbhk2X\n"
+"OlbXEM9fWP+INvFkr6Z93VYL2jGkCjV7e3xXmre/Lb92fUcYi6t5dwzfV8gJnIoG\n"
+"eCHd0K8NrQK0ipVk/7zcPDKOPeo9Y5aj/f6X/pDHtb+Dd5sT+l82G/Tqy4DIYUYR\n"
+"-----END SIGNATURE-----\n"
+"r router2 AwMDAwMDAwMDAwMDAwMDAwMDAwM Tk5OTk5OTk5OTk5OTk5OTk5OTk4 2015-09-02 19:09:15 153.0.136.1 443 8000\n"
+"s Running V2Dir\n"
+"v 0.1.2.14\n"
+"w Bandwidth=30 Measured=30\n"
+"p reject 1-65535\n"
+"id ed25519 none\n"
+"m 9,10,11,12,13,14,15,16,17 sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa0\n"
+"r router1 BQUFBQUFBQUFBQUFBQUFBQUFBQU TU1NTU1NTU1NTU1NTU1NTU1NTU0 2015-09-02 19:17:35 153.0.153.1 443 0\n"
+"a [1:2:3::4]:4711\n"
+"s Exit Fast Guard Running Stable Valid\n"
+"v 0.2.0.5\n"
+"w Bandwidth=120 Measured=120\n"
+"p reject 1-65535\n"
+"id ed25519 none\n"
+"m 9,10,11,12,13,14,15,16,17 sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa1\n"
+"r router3 MzMzMzMzMzMzMzMzMzMzMzMzMzM T09PT09PT09PT09PT09PT09PT08 2015-09-02 19:17:35 170.0.153.1 400 9999\n"
+"s Authority Exit Fast Guard Running Stable V2Dir Valid\n"
+"v 0.1.0.3\n"
+"w Bandwidth=120\n"
+"p reject 1-65535\n"
+"id ed25519 none\n"
+"m 9,10,11,12,13,14,15,16,17 "
+"sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa2\n"
+"r router4 NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ Ly8vLy8vLy8vLy8vLy8vLy8vLy8 2015-09-02 19:17:35 192.0.2.3 500 1999\n"
+"s Running V2Dir\n"
+"v 0.1.6.3\n"
+"w Bandwidth=30\n"
+"p reject 1-65535\n"
+"id ed25519 none\n"
+"m 9,10,11,12,13,14,15,16,17 sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa3\n"
+"directory-footer\n"
+"directory-signature D867ACF56A9D229B35C25F0090BC9867E906BE69 CBF56A83368A5150F1A9AAADAFB4D77F8C4170E2\n"
+"-----BEGIN SIGNATURE-----\n"
+"AHiWcHe+T3XbnlQqvqSAk6RY3XmEy1+hM2u9Xk6BNi7BpQkEQM1f0vzRpgn5Dnf2\n"
+"TXQWGUq9Z7jdSVnzWT3xqPA4zjw6eZkj+DKUtwq+oEDZGlf8eHTFmr0NAWfwZbk9\n"
+"NAjbMTUXUP37N2XAZwkoCWwFCrrfMwXrL7OhZbj7ifo=\n"
+"-----END SIGNATURE-----\n";
+