|
@@ -91,6 +91,9 @@
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
#include <sys/stat.h>
|
|
|
#endif
|
|
|
+#ifdef HAVE_UNISTD_H
|
|
|
+#include <unistd.h>
|
|
|
+#endif
|
|
|
|
|
|
#define NS_MODULE dir
|
|
|
|
|
@@ -1765,7 +1768,8 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, "", 0);
|
|
|
setup_capture_of_logs(LOG_WARN);
|
|
|
tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
expect_log_msg("Empty bandwidth file\n");
|
|
|
teardown_capture_of_logs();
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
@@ -1781,7 +1785,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(-1, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op("", OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1792,7 +1798,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, header_lines_v100, 0);
|
|
|
bw_file_headers = smartlist_new();
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1805,7 +1813,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1816,7 +1826,8 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
tor_asprintf(&content, "%s%s", header_lines_v100, relay_lines_v100);
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
- tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL, NULL));
|
|
|
+ tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL, NULL,
|
|
|
+ NULL));
|
|
|
|
|
|
/* Test bandwidth file including v1.1.0 bandwidth headers and
|
|
|
* v1.0.0 relay lines. bw_file_headers will contain the v1.1.0 headers. */
|
|
@@ -1826,7 +1837,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1842,7 +1855,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1859,7 +1874,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v100, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1870,7 +1887,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
bw_file_headers = smartlist_new();
|
|
|
write_str_to_file(fname, header_lines_v110_no_terminator, 0);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1881,7 +1900,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
bw_file_headers = smartlist_new();
|
|
|
write_str_to_file(fname, header_lines_v110, 0);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1896,7 +1917,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1911,7 +1934,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1927,7 +1952,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_v110, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1944,7 +1971,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
|
tt_str_op(bw_file_headers_str_bad, OP_EQ, bw_file_headers_str);
|
|
|
SMARTLIST_FOREACH(bw_file_headers, char *, c, tor_free(c));
|
|
@@ -1962,7 +1991,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_EQ,
|
|
|
smartlist_len(bw_file_headers));
|
|
|
bw_file_headers_str = smartlist_join_strings(bw_file_headers, " ", 0, NULL);
|
|
@@ -1983,7 +2014,9 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
write_str_to_file(fname, content, 0);
|
|
|
tor_free(content);
|
|
|
tt_int_op(0, OP_EQ, dirserv_read_measured_bandwidths(fname, NULL,
|
|
|
- bw_file_headers));
|
|
|
+ bw_file_headers,
|
|
|
+ NULL));
|
|
|
+
|
|
|
tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_EQ,
|
|
|
smartlist_len(bw_file_headers));
|
|
|
/* force bw_file_headers to be bigger than
|
|
@@ -1997,6 +2030,7 @@ test_dir_dirserv_read_measured_bandwidths(void *arg)
|
|
|
tor_free(bw_file_headers_str);
|
|
|
|
|
|
done:
|
|
|
+ unlink(fname);
|
|
|
tor_free(fname);
|
|
|
tor_free(header_lines_v100);
|
|
|
tor_free(header_lines_v110_no_terminator);
|
|
@@ -3830,6 +3864,62 @@ mock_get_options(void)
|
|
|
return mock_options;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Test dirauth_get_b64_digest_bw_file.
|
|
|
+ * This function should be near the other bwauth functions, but it needs
|
|
|
+ * mock_get_options, that is only defined here.
|
|
|
+ */
|
|
|
+
|
|
|
+static void
|
|
|
+test_dir_bwauth_bw_file_digest256(void *arg)
|
|
|
+{
|
|
|
+ (void)arg;
|
|
|
+ const char *content =
|
|
|
+ "1541171221\n"
|
|
|
+ "node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 "
|
|
|
+ "master_key_ed25519=YaqV4vbvPYKucElk297eVdNArDz9HtIwUoIeo0+cVIpQ "
|
|
|
+ "bw=760 nick=Test time=2018-05-08T16:13:26\n";
|
|
|
+
|
|
|
+ char *fname = tor_strdup(get_fname("V3BandwidthsFile"));
|
|
|
+ /* Initialize to a wrong digest. */
|
|
|
+ uint8_t digest[DIGEST256_LEN] = "01234567890123456789abcdefghijkl";
|
|
|
+
|
|
|
+ /* Digest of an empty string. Initialize to a wrong digest. */
|
|
|
+ char digest_empty_str[DIGEST256_LEN] = "01234567890123456789abcdefghijkl";
|
|
|
+ crypto_digest256(digest_empty_str, "", 0, DIGEST_SHA256);
|
|
|
+
|
|
|
+ /* Digest of the content. Initialize to a wrong digest. */
|
|
|
+ char digest_expected[DIGEST256_LEN] = "01234567890123456789abcdefghijkl";
|
|
|
+ crypto_digest256(digest_expected, content, strlen(content), DIGEST_SHA256);
|
|
|
+
|
|
|
+ /* When the bandwidth file can not be found. */
|
|
|
+ tt_int_op(-1, OP_EQ,
|
|
|
+ dirserv_read_measured_bandwidths(fname,
|
|
|
+ NULL, NULL, digest));
|
|
|
+ tt_mem_op(digest, OP_EQ, digest_empty_str, DIGEST256_LEN);
|
|
|
+
|
|
|
+ /* When there is a timestamp but it is too old. */
|
|
|
+ write_str_to_file(fname, content, 0);
|
|
|
+ tt_int_op(-1, OP_EQ,
|
|
|
+ dirserv_read_measured_bandwidths(fname,
|
|
|
+ NULL, NULL, digest));
|
|
|
+ /* The digest will be correct. */
|
|
|
+ tt_mem_op(digest, OP_EQ, digest_expected, DIGEST256_LEN);
|
|
|
+
|
|
|
+ update_approx_time(1541171221);
|
|
|
+
|
|
|
+ /* When there is a bandwidth file and it can be read. */
|
|
|
+ tt_int_op(0, OP_EQ,
|
|
|
+ dirserv_read_measured_bandwidths(fname,
|
|
|
+ NULL, NULL, digest));
|
|
|
+ tt_mem_op(digest, OP_EQ, digest_expected, DIGEST256_LEN);
|
|
|
+
|
|
|
+ done:
|
|
|
+ unlink(fname);
|
|
|
+ tor_free(fname);
|
|
|
+ update_approx_time(time(NULL));
|
|
|
+}
|
|
|
+
|
|
|
static void
|
|
|
reset_routerstatus(routerstatus_t *rs,
|
|
|
const char *hex_identity_digest,
|
|
@@ -6411,6 +6501,7 @@ struct testcase_t dir_tests[] = {
|
|
|
DIR_LEGACY(measured_bw_kb_line_is_after_headers),
|
|
|
DIR_LEGACY(measured_bw_kb_cache),
|
|
|
DIR_LEGACY(dirserv_read_measured_bandwidths),
|
|
|
+ DIR(bwauth_bw_file_digest256, 0),
|
|
|
DIR_LEGACY(param_voting),
|
|
|
DIR(param_voting_lookup, 0),
|
|
|
DIR_LEGACY(v3_networkstatus),
|