test: Add test to get the digest of a bw file

This commit is contained in:
juga0 2018-11-06 14:56:05 +00:00
parent fc3e90a7b6
commit 28490fa23e
2 changed files with 62 additions and 1 deletions

View File

@ -254,7 +254,7 @@ dirserv_read_measured_bandwidths(const char *from_file,
goto continue_digest;
}
now = time(NULL);
now = approx_time();
if ((now - file_time) > MAX_MEASUREMENT_AGE) {
log_warn(LD_DIRSERV, "Bandwidth measurement file stale. Age: %u",
(unsigned)(time(NULL) - file_time));

View File

@ -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
@ -2027,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);
@ -3860,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,
@ -6441,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),