Merge branch 'networkstatus_mmap_merge'

This commit is contained in:
Nick Mathewson 2018-10-31 10:06:30 -04:00
commit 672e26cad8
30 changed files with 467 additions and 247 deletions

5
changes/feature27244 Normal file
View File

@ -0,0 +1,5 @@
o Minor features (memory usage):
- Tor clients no longer need to keep the full text of a consensus in
memory in order to parse it, or apply a diff to it. Instead, they
use mmap() to read the consensus files from disk. Closes ticket
27244.

View File

@ -2352,7 +2352,11 @@ getinfo_helper_dir(control_connection_t *control_conn,
*answer = tor_strdup(consensus->dir); *answer = tor_strdup(consensus->dir);
} }
if (!*answer) { /* try loading it from disk */ if (!*answer) { /* try loading it from disk */
*answer = networkstatus_read_cached_consensus("ns"); tor_mmap_t *mapped = networkstatus_map_cached_consensus("ns");
if (mapped) {
*answer = tor_memdup_nulterm(mapped->data, mapped->size);
tor_munmap_file(mapped);
}
if (!*answer) { /* generate an error */ if (!*answer) { /* generate an error */
*errmsg = "Could not open cached consensus. " *errmsg = "Could not open cached consensus. "
"Make sure FetchUselessDescriptors is set to 1."; "Make sure FetchUselessDescriptors is set to 1.";

View File

@ -413,7 +413,8 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
{ {
networkstatus_t *v; networkstatus_t *v;
if (!(v = networkstatus_parse_vote_from_string(status, NULL, if (!(v = networkstatus_parse_vote_from_string(status, strlen(status),
NULL,
v3_ns->type))) { v3_ns->type))) {
log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: " log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
"<<%s>>", "<<%s>>",
@ -2410,7 +2411,8 @@ networkstatus_compute_consensus(smartlist_t *votes,
{ {
networkstatus_t *c; networkstatus_t *c;
if (!(c = networkstatus_parse_vote_from_string(result, NULL, if (!(c = networkstatus_parse_vote_from_string(result, strlen(result),
NULL,
NS_TYPE_CONSENSUS))) { NS_TYPE_CONSENSUS))) {
log_err(LD_BUG, "Generated a networkstatus consensus we couldn't " log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
"parse."); "parse.");
@ -3133,7 +3135,8 @@ dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
*msg_out = NULL; *msg_out = NULL;
again: again:
vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote, vote = networkstatus_parse_vote_from_string(vote_body, strlen(vote_body),
&end_of_vote,
NS_TYPE_VOTE); NS_TYPE_VOTE);
if (!end_of_vote) if (!end_of_vote)
end_of_vote = vote_body + strlen(vote_body); end_of_vote = vote_body + strlen(vote_body);
@ -3391,7 +3394,9 @@ dirvote_compute_consensuses(void)
flavor_name); flavor_name);
continue; continue;
} }
consensus = networkstatus_parse_vote_from_string(consensus_body, NULL, consensus = networkstatus_parse_vote_from_string(consensus_body,
strlen(consensus_body),
NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
if (!consensus) { if (!consensus) {
log_warn(LD_DIR, "Couldn't parse %s consensus we generated!", log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
@ -3530,7 +3535,7 @@ dirvote_add_signatures_to_pending_consensus(
* just in case we break detached signature processing at some point. */ * just in case we break detached signature processing at some point. */
{ {
networkstatus_t *v = networkstatus_parse_vote_from_string( networkstatus_t *v = networkstatus_parse_vote_from_string(
pc->body, NULL, pc->body, strlen(pc->body), NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
tor_assert(v); tor_assert(v);
networkstatus_vote_free(v); networkstatus_vote_free(v);
@ -3655,7 +3660,9 @@ dirvote_publish_consensus(void)
continue; continue;
} }
if (networkstatus_set_current_consensus(pending->body, name, 0, NULL)) if (networkstatus_set_current_consensus(pending->body,
strlen(pending->body),
name, 0, NULL))
log_warn(LD_DIR, "Error publishing %s consensus", name); log_warn(LD_DIR, "Error publishing %s consensus", name);
else else
log_notice(LD_DIR, "Published %s consensus", name); log_notice(LD_DIR, "Published %s consensus", name);

View File

@ -189,6 +189,7 @@ static consdiff_cfg_t consdiff_cfg = {
static int consdiffmgr_ensure_space_for_files(int n); static int consdiffmgr_ensure_space_for_files(int n);
static int consensus_queue_compression_work(const char *consensus, static int consensus_queue_compression_work(const char *consensus,
size_t consensus_len,
const networkstatus_t *as_parsed); const networkstatus_t *as_parsed);
static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from, static int consensus_diff_queue_diff_work(consensus_cache_entry_t *diff_from,
consensus_cache_entry_t *diff_to); consensus_cache_entry_t *diff_to);
@ -509,8 +510,25 @@ get_max_age_to_cache(void)
MAX_MAX_AGE_TO_CACHE); MAX_MAX_AGE_TO_CACHE);
} }
#ifdef TOR_UNIT_TESTS
/** As consdiffmgr_add_consensus, but requires a nul-terminated input. For
* testing. */
int
consdiffmgr_add_consensus_nulterm(const char *consensus,
const networkstatus_t *as_parsed)
{
size_t len = strlen(consensus);
/* make a non-nul-terminated copy so that we can have a better chance
* of catching errors. */
char *ctmp = tor_memdup(consensus, len);
int r = consdiffmgr_add_consensus(ctmp, len, as_parsed);
tor_free(ctmp);
return r;
}
#endif
/** /**
* Given a string containing a networkstatus consensus, and the results of * Given a buffer containing a networkstatus consensus, and the results of
* having parsed that consensus, add that consensus to the cache if it is not * having parsed that consensus, add that consensus to the cache if it is not
* already present and not too old. Create new consensus diffs from or to * already present and not too old. Create new consensus diffs from or to
* that consensus as appropriate. * that consensus as appropriate.
@ -519,6 +537,7 @@ get_max_age_to_cache(void)
*/ */
int int
consdiffmgr_add_consensus(const char *consensus, consdiffmgr_add_consensus(const char *consensus,
size_t consensus_len,
const networkstatus_t *as_parsed) const networkstatus_t *as_parsed)
{ {
if (BUG(consensus == NULL) || BUG(as_parsed == NULL)) if (BUG(consensus == NULL) || BUG(as_parsed == NULL))
@ -544,7 +563,7 @@ consdiffmgr_add_consensus(const char *consensus,
} }
/* We don't have it. Add it to the cache. */ /* We don't have it. Add it to the cache. */
return consensus_queue_compression_work(consensus, as_parsed); return consensus_queue_compression_work(consensus, consensus_len, as_parsed);
} }
/** /**
@ -1387,19 +1406,21 @@ typedef struct consensus_diff_worker_job_t {
} consensus_diff_worker_job_t; } consensus_diff_worker_job_t;
/** Given a consensus_cache_entry_t, check whether it has a label claiming /** Given a consensus_cache_entry_t, check whether it has a label claiming
* that it was compressed. If so, uncompress its contents into <b>out</b> and * that it was compressed. If so, uncompress its contents into *<b>out</b> and
* set <b>outlen</b> to hold their size. If not, just copy the body into * set <b>outlen</b> to hold their size, and set *<b>owned_out</b> to a pointer
* <b>out</b> and set <b>outlen</b> to its length. Return 0 on success, * that the caller will need to free. If not, just set *<b>out</b> and
* -1 on failure. * <b>outlen</b> to its extent in memory. Return 0 on success, -1 on failure.
* **/
* In all cases, the output is nul-terminated. */
STATIC int STATIC int
uncompress_or_copy(char **out, size_t *outlen, uncompress_or_set_ptr(const char **out, size_t *outlen,
consensus_cache_entry_t *ent) char **owned_out,
consensus_cache_entry_t *ent)
{ {
const uint8_t *body; const uint8_t *body;
size_t bodylen; size_t bodylen;
*owned_out = NULL;
if (consensus_cache_entry_get_body(ent, &body, &bodylen) < 0) if (consensus_cache_entry_get_body(ent, &body, &bodylen) < 0)
return -1; return -1;
@ -1410,8 +1431,17 @@ uncompress_or_copy(char **out, size_t *outlen,
if (lv_compression) if (lv_compression)
method = compression_method_get_by_name(lv_compression); method = compression_method_get_by_name(lv_compression);
return tor_uncompress(out, outlen, (const char *)body, bodylen, int rv;
if (method == NO_METHOD) {
*out = (const char *)body;
*outlen = bodylen;
rv = 0;
} else {
rv = tor_uncompress(owned_out, outlen, (const char *)body, bodylen,
method, 1, LOG_WARN); method, 1, LOG_WARN);
*out = *owned_out;
}
return rv;
} }
/** /**
@ -1478,16 +1508,17 @@ consensus_diff_worker_threadfn(void *state_, void *work_)
char *consensus_diff; char *consensus_diff;
{ {
char *diff_from_nt = NULL, *diff_to_nt = NULL; const char *diff_from_nt = NULL, *diff_to_nt = NULL;
char *owned1 = NULL, *owned2 = NULL;
size_t diff_from_nt_len, diff_to_nt_len; size_t diff_from_nt_len, diff_to_nt_len;
if (uncompress_or_copy(&diff_from_nt, &diff_from_nt_len, if (uncompress_or_set_ptr(&diff_from_nt, &diff_from_nt_len, &owned1,
job->diff_from) < 0) { job->diff_from) < 0) {
return WQ_RPL_REPLY; return WQ_RPL_REPLY;
} }
if (uncompress_or_copy(&diff_to_nt, &diff_to_nt_len, if (uncompress_or_set_ptr(&diff_to_nt, &diff_to_nt_len, &owned2,
job->diff_to) < 0) { job->diff_to) < 0) {
tor_free(diff_from_nt); tor_free(owned1);
return WQ_RPL_REPLY; return WQ_RPL_REPLY;
} }
tor_assert(diff_from_nt); tor_assert(diff_from_nt);
@ -1496,9 +1527,12 @@ consensus_diff_worker_threadfn(void *state_, void *work_)
// XXXX ugh; this is going to calculate the SHA3 of both its // XXXX ugh; this is going to calculate the SHA3 of both its
// XXXX inputs again, even though we already have that. Maybe it's time // XXXX inputs again, even though we already have that. Maybe it's time
// XXXX to change the API here? // XXXX to change the API here?
consensus_diff = consensus_diff_generate(diff_from_nt, diff_to_nt); consensus_diff = consensus_diff_generate(diff_from_nt,
tor_free(diff_from_nt); diff_from_nt_len,
tor_free(diff_to_nt); diff_to_nt,
diff_to_nt_len);
tor_free(owned1);
tor_free(owned2);
} }
if (!consensus_diff) { if (!consensus_diff) {
/* Couldn't generate consensus; we'll leave the reply blank. */ /* Couldn't generate consensus; we'll leave the reply blank. */
@ -1746,8 +1780,8 @@ consensus_compress_worker_threadfn(void *state_, void *work_)
(const uint8_t *)consensus, bodylen); (const uint8_t *)consensus, bodylen);
{ {
const char *start, *end; const char *start, *end;
if (router_get_networkstatus_v3_signed_boundaries(consensus, if (router_get_networkstatus_v3_signed_boundaries(consensus, bodylen,
&start, &end) < 0) { &start, &end) < 0) {
start = consensus; start = consensus;
end = consensus+bodylen; end = consensus+bodylen;
} }
@ -1811,14 +1845,15 @@ static int background_compression = 0;
*/ */
static int static int
consensus_queue_compression_work(const char *consensus, consensus_queue_compression_work(const char *consensus,
size_t consensus_len,
const networkstatus_t *as_parsed) const networkstatus_t *as_parsed)
{ {
tor_assert(consensus); tor_assert(consensus);
tor_assert(as_parsed); tor_assert(as_parsed);
consensus_compress_worker_job_t *job = tor_malloc_zero(sizeof(*job)); consensus_compress_worker_job_t *job = tor_malloc_zero(sizeof(*job));
job->consensus = tor_strdup(consensus); job->consensus = tor_memdup_nulterm(consensus, consensus_len);
job->consensus_len = strlen(consensus); job->consensus_len = strlen(job->consensus);
job->flavor = as_parsed->flavor; job->flavor = as_parsed->flavor;
char va_str[ISO_TIME_LEN+1]; char va_str[ISO_TIME_LEN+1];

View File

@ -22,6 +22,7 @@ typedef struct consdiff_cfg_t {
struct consensus_cache_entry_t; // from conscache.h struct consensus_cache_entry_t; // from conscache.h
int consdiffmgr_add_consensus(const char *consensus, int consdiffmgr_add_consensus(const char *consensus,
size_t consensus_len,
const networkstatus_t *as_parsed); const networkstatus_t *as_parsed);
consdiff_status_t consdiffmgr_find_consensus( consdiff_status_t consdiffmgr_find_consensus(
@ -68,8 +69,14 @@ STATIC consensus_cache_entry_t *cdm_cache_lookup_consensus(
STATIC int cdm_entry_get_sha3_value(uint8_t *digest_out, STATIC int cdm_entry_get_sha3_value(uint8_t *digest_out,
consensus_cache_entry_t *ent, consensus_cache_entry_t *ent,
const char *label); const char *label);
STATIC int uncompress_or_copy(char **out, size_t *outlen, STATIC int uncompress_or_set_ptr(const char **out, size_t *outlen,
consensus_cache_entry_t *ent); char **owned_out,
consensus_cache_entry_t *ent);
#endif /* defined(CONSDIFFMGR_PRIVATE) */ #endif /* defined(CONSDIFFMGR_PRIVATE) */
#ifdef TOR_UNIT_TESTS
int consdiffmgr_add_consensus_nulterm(const char *consensus,
const networkstatus_t *as_parsed);
#endif
#endif /* !defined(TOR_CONSDIFFMGR_H) */ #endif /* !defined(TOR_CONSDIFFMGR_H) */

View File

@ -234,6 +234,7 @@ free_cached_dir_(void *_d)
* validation is performed. */ * validation is performed. */
void void
dirserv_set_cached_consensus_networkstatus(const char *networkstatus, dirserv_set_cached_consensus_networkstatus(const char *networkstatus,
size_t networkstatus_len,
const char *flavor_name, const char *flavor_name,
const common_digests_t *digests, const common_digests_t *digests,
const uint8_t *sha3_as_signed, const uint8_t *sha3_as_signed,
@ -244,7 +245,9 @@ dirserv_set_cached_consensus_networkstatus(const char *networkstatus,
if (!cached_consensuses) if (!cached_consensuses)
cached_consensuses = strmap_new(); cached_consensuses = strmap_new();
new_networkstatus = new_cached_dir(tor_strdup(networkstatus), published); new_networkstatus =
new_cached_dir(tor_memdup_nulterm(networkstatus, networkstatus_len),
published);
memcpy(&new_networkstatus->digests, digests, sizeof(common_digests_t)); memcpy(&new_networkstatus->digests, digests, sizeof(common_digests_t));
memcpy(&new_networkstatus->digest_sha3_as_signed, sha3_as_signed, memcpy(&new_networkstatus->digest_sha3_as_signed, sha3_as_signed,
DIGEST256_LEN); DIGEST256_LEN);

View File

@ -84,6 +84,7 @@ int directory_too_idle_to_fetch_descriptors(const or_options_t *options,
cached_dir_t *dirserv_get_consensus(const char *flavor_name); cached_dir_t *dirserv_get_consensus(const char *flavor_name);
void dirserv_set_cached_consensus_networkstatus(const char *consensus, void dirserv_set_cached_consensus_networkstatus(const char *consensus,
size_t consensus_len,
const char *flavor_name, const char *flavor_name,
const common_digests_t *digests, const common_digests_t *digests,
const uint8_t *sha3_as_signed, const uint8_t *sha3_as_signed,

View File

@ -2205,13 +2205,18 @@ handle_response_fetch_consensus(dir_connection_t *conn,
if (looks_like_a_consensus_diff(body, body_len)) { if (looks_like_a_consensus_diff(body, body_len)) {
/* First find our previous consensus. Maybe it's in ram, maybe not. */ /* First find our previous consensus. Maybe it's in ram, maybe not. */
cached_dir_t *cd = dirserv_get_consensus(flavname); cached_dir_t *cd = dirserv_get_consensus(flavname);
const char *consensus_body; const char *consensus_body = NULL;
char *owned_consensus = NULL; size_t consensus_body_len;
tor_mmap_t *mapped_consensus = NULL;
if (cd) { if (cd) {
consensus_body = cd->dir; consensus_body = cd->dir;
consensus_body_len = cd->dir_len;
} else { } else {
owned_consensus = networkstatus_read_cached_consensus(flavname); mapped_consensus = networkstatus_map_cached_consensus(flavname);
consensus_body = owned_consensus; if (mapped_consensus) {
consensus_body = mapped_consensus->data;
consensus_body_len = mapped_consensus->size;
}
} }
if (!consensus_body) { if (!consensus_body) {
log_warn(LD_DIR, "Received a consensus diff, but we can't find " log_warn(LD_DIR, "Received a consensus diff, but we can't find "
@ -2221,8 +2226,9 @@ handle_response_fetch_consensus(dir_connection_t *conn,
return -1; return -1;
} }
new_consensus = consensus_diff_apply(consensus_body, body); new_consensus = consensus_diff_apply(consensus_body, consensus_body_len,
tor_free(owned_consensus); body, body_len);
tor_munmap_file(mapped_consensus);
if (new_consensus == NULL) { if (new_consensus == NULL) {
log_warn(LD_DIR, "Could not apply consensus diff received from server " log_warn(LD_DIR, "Could not apply consensus diff received from server "
"'%s:%d'", conn->base_.address, conn->base_.port); "'%s:%d'", conn->base_.address, conn->base_.port);
@ -2244,7 +2250,9 @@ handle_response_fetch_consensus(dir_connection_t *conn,
sourcename = "downloaded"; sourcename = "downloaded";
} }
if ((r=networkstatus_set_current_consensus(consensus, flavname, 0, if ((r=networkstatus_set_current_consensus(consensus,
strlen(consensus),
flavname, 0,
conn->identity_digest))<0) { conn->identity_digest))<0) {
log_fn(r<-1?LOG_WARN:LOG_INFO, LD_DIR, log_fn(r<-1?LOG_WARN:LOG_INFO, LD_DIR,
"Unable to load %s consensus directory %s from " "Unable to load %s consensus directory %s from "

View File

@ -101,11 +101,11 @@ smartlist_add_linecpy(smartlist_t *lst, memarea_t *area, const char *s)
/* This is a separate, mockable function so that we can override it when /* This is a separate, mockable function so that we can override it when
* fuzzing. */ * fuzzing. */
MOCK_IMPL(STATIC int, MOCK_IMPL(STATIC int,
consensus_compute_digest,(const char *cons, consensus_compute_digest,(const char *cons, size_t len,
consensus_digest_t *digest_out)) consensus_digest_t *digest_out))
{ {
int r = crypto_digest256((char*)digest_out->sha3_256, int r = crypto_digest256((char*)digest_out->sha3_256,
cons, strlen(cons), DIGEST_SHA3_256); cons, len, DIGEST_SHA3_256);
return r; return r;
} }
@ -114,11 +114,11 @@ consensus_compute_digest,(const char *cons,
/* This is a separate, mockable function so that we can override it when /* This is a separate, mockable function so that we can override it when
* fuzzing. */ * fuzzing. */
MOCK_IMPL(STATIC int, MOCK_IMPL(STATIC int,
consensus_compute_digest_as_signed,(const char *cons, consensus_compute_digest_as_signed,(const char *cons, size_t len,
consensus_digest_t *digest_out)) consensus_digest_t *digest_out))
{ {
return router_get_networkstatus_v3_sha3_as_signed(digest_out->sha3_256, return router_get_networkstatus_v3_sha3_as_signed(digest_out->sha3_256,
cons); cons, len);
} }
/** Return true iff <b>d1</b> and <b>d2</b> contain the same digest */ /** Return true iff <b>d1</b> and <b>d2</b> contain the same digest */
@ -1229,7 +1229,8 @@ consdiff_apply_diff(const smartlist_t *cons1,
cons2_str = consensus_join_lines(cons2); cons2_str = consensus_join_lines(cons2);
consensus_digest_t cons2_digests; consensus_digest_t cons2_digests;
if (consensus_compute_digest(cons2_str, &cons2_digests) < 0) { if (consensus_compute_digest(cons2_str, strlen(cons2_str),
&cons2_digests) < 0) {
/* LCOV_EXCL_START -- digest can't fail */ /* LCOV_EXCL_START -- digest can't fail */
log_warn(LD_CONSDIFF, "Could not compute digests of the consensus " log_warn(LD_CONSDIFF, "Could not compute digests of the consensus "
"resulting from applying a consensus diff."); "resulting from applying a consensus diff.");
@ -1283,12 +1284,13 @@ consdiff_apply_diff(const smartlist_t *cons1,
* generated cdlines will become invalid. * generated cdlines will become invalid.
*/ */
STATIC int STATIC int
consensus_split_lines(smartlist_t *out, const char *s, memarea_t *area) consensus_split_lines(smartlist_t *out,
const char *s, size_t len,
memarea_t *area)
{ {
const char *end_of_str = s + strlen(s); const char *end_of_str = s + len;
tor_assert(*end_of_str == '\0');
while (*s) { while (s < end_of_str) {
const char *eol = memchr(s, '\n', end_of_str - s); const char *eol = memchr(s, '\n', end_of_str - s);
if (!eol) { if (!eol) {
/* File doesn't end with newline. */ /* File doesn't end with newline. */
@ -1334,25 +1336,25 @@ consensus_join_lines(const smartlist_t *inp)
* success, retun a newly allocated string containing that diff. On failure, * success, retun a newly allocated string containing that diff. On failure,
* return NULL. */ * return NULL. */
char * char *
consensus_diff_generate(const char *cons1, consensus_diff_generate(const char *cons1, size_t cons1len,
const char *cons2) const char *cons2, size_t cons2len)
{ {
consensus_digest_t d1, d2; consensus_digest_t d1, d2;
smartlist_t *lines1 = NULL, *lines2 = NULL, *result_lines = NULL; smartlist_t *lines1 = NULL, *lines2 = NULL, *result_lines = NULL;
int r1, r2; int r1, r2;
char *result = NULL; char *result = NULL;
r1 = consensus_compute_digest_as_signed(cons1, &d1); r1 = consensus_compute_digest_as_signed(cons1, cons1len, &d1);
r2 = consensus_compute_digest(cons2, &d2); r2 = consensus_compute_digest(cons2, cons2len, &d2);
if (BUG(r1 < 0 || r2 < 0)) if (BUG(r1 < 0 || r2 < 0))
return NULL; // LCOV_EXCL_LINE return NULL; // LCOV_EXCL_LINE
memarea_t *area = memarea_new(); memarea_t *area = memarea_new();
lines1 = smartlist_new(); lines1 = smartlist_new();
lines2 = smartlist_new(); lines2 = smartlist_new();
if (consensus_split_lines(lines1, cons1, area) < 0) if (consensus_split_lines(lines1, cons1, cons1len, area) < 0)
goto done; goto done;
if (consensus_split_lines(lines2, cons2, area) < 0) if (consensus_split_lines(lines2, cons2, cons2len, area) < 0)
goto done; goto done;
result_lines = consdiff_gen_diff(lines1, lines2, &d1, &d2, area); result_lines = consdiff_gen_diff(lines1, lines2, &d1, &d2, area);
@ -1375,7 +1377,9 @@ consensus_diff_generate(const char *cons1,
* consensus. On failure, return NULL. */ * consensus. On failure, return NULL. */
char * char *
consensus_diff_apply(const char *consensus, consensus_diff_apply(const char *consensus,
const char *diff) size_t consensus_len,
const char *diff,
size_t diff_len)
{ {
consensus_digest_t d1; consensus_digest_t d1;
smartlist_t *lines1 = NULL, *lines2 = NULL; smartlist_t *lines1 = NULL, *lines2 = NULL;
@ -1383,15 +1387,15 @@ consensus_diff_apply(const char *consensus,
char *result = NULL; char *result = NULL;
memarea_t *area = memarea_new(); memarea_t *area = memarea_new();
r1 = consensus_compute_digest_as_signed(consensus, &d1); r1 = consensus_compute_digest_as_signed(consensus, consensus_len, &d1);
if (BUG(r1 < 0)) if (BUG(r1 < 0))
return NULL; // LCOV_EXCL_LINE return NULL; // LCOV_EXCL_LINE
lines1 = smartlist_new(); lines1 = smartlist_new();
lines2 = smartlist_new(); lines2 = smartlist_new();
if (consensus_split_lines(lines1, consensus, area) < 0) if (consensus_split_lines(lines1, consensus, consensus_len, area) < 0)
goto done; goto done;
if (consensus_split_lines(lines2, diff, area) < 0) if (consensus_split_lines(lines2, diff, diff_len, area) < 0)
goto done; goto done;
result = consdiff_apply_diff(lines1, lines2, &d1); result = consdiff_apply_diff(lines1, lines2, &d1);

View File

@ -7,10 +7,10 @@
#include "core/or/or.h" #include "core/or/or.h"
char *consensus_diff_generate(const char *cons1, char *consensus_diff_generate(const char *cons1, size_t cons1len,
const char *cons2); const char *cons2, size_t cons2len);
char *consensus_diff_apply(const char *consensus, char *consensus_diff_apply(const char *consensus, size_t consensus_len,
const char *diff); const char *diff, size_t diff_len);
int looks_like_a_consensus_diff(const char *document, size_t len); int looks_like_a_consensus_diff(const char *document, size_t len);
@ -78,7 +78,8 @@ STATIC int smartlist_slice_string_pos(const smartlist_slice_t *slice,
STATIC void set_changed(bitarray_t *changed1, bitarray_t *changed2, STATIC void set_changed(bitarray_t *changed1, bitarray_t *changed2,
const smartlist_slice_t *slice1, const smartlist_slice_t *slice1,
const smartlist_slice_t *slice2); const smartlist_slice_t *slice2);
STATIC int consensus_split_lines(smartlist_t *out, const char *s, STATIC int consensus_split_lines(smartlist_t *out,
const char *s, size_t len,
struct memarea_t *area); struct memarea_t *area);
STATIC void smartlist_add_linecpy(smartlist_t *lst, struct memarea_t *area, STATIC void smartlist_add_linecpy(smartlist_t *lst, struct memarea_t *area,
const char *s); const char *s);
@ -86,10 +87,10 @@ STATIC int lines_eq(const cdline_t *a, const cdline_t *b);
STATIC int line_str_eq(const cdline_t *a, const char *b); STATIC int line_str_eq(const cdline_t *a, const char *b);
MOCK_DECL(STATIC int, MOCK_DECL(STATIC int,
consensus_compute_digest,(const char *cons, consensus_compute_digest,(const char *cons, size_t len,
consensus_digest_t *digest_out)); consensus_digest_t *digest_out));
MOCK_DECL(STATIC int, MOCK_DECL(STATIC int,
consensus_compute_digest_as_signed,(const char *cons, consensus_compute_digest_as_signed,(const char *cons, size_t len,
consensus_digest_t *digest_out)); consensus_digest_t *digest_out));
MOCK_DECL(STATIC int, MOCK_DECL(STATIC int,
consensus_digest_eq,(const uint8_t *d1, consensus_digest_eq,(const uint8_t *d1,

View File

@ -24,7 +24,8 @@ static token_rule_t dir_key_certificate_table[] = {
/** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to /** Parse a key certificate from <b>s</b>; point <b>end-of-string</b> to
* the first character after the certificate. */ * the first character after the certificate. */
authority_cert_t * authority_cert_t *
authority_cert_parse_from_string(const char *s, const char **end_of_string) authority_cert_parse_from_string(const char *s, size_t maxlen,
const char **end_of_string)
{ {
/** Reject any certificate at least this big; it is probably an overflow, an /** Reject any certificate at least this big; it is probably an overflow, an
* attack, a bug, or some other nonsense. */ * attack, a bug, or some other nonsense. */
@ -35,24 +36,25 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
char digest[DIGEST_LEN]; char digest[DIGEST_LEN];
directory_token_t *tok; directory_token_t *tok;
char fp_declared[DIGEST_LEN]; char fp_declared[DIGEST_LEN];
char *eos; const char *eos;
size_t len; size_t len;
int found; int found;
memarea_t *area = NULL; memarea_t *area = NULL;
const char *end_of_s = s + maxlen;
const char *s_dup = s; const char *s_dup = s;
s = eat_whitespace(s); s = eat_whitespace_eos(s, end_of_s);
eos = strstr(s, "\ndir-key-certification"); eos = tor_memstr(s, end_of_s - s, "\ndir-key-certification");
if (! eos) { if (! eos) {
log_warn(LD_DIR, "No signature found on key certificate"); log_warn(LD_DIR, "No signature found on key certificate");
return NULL; return NULL;
} }
eos = strstr(eos, "\n-----END SIGNATURE-----\n"); eos = tor_memstr(eos, end_of_s - eos, "\n-----END SIGNATURE-----\n");
if (! eos) { if (! eos) {
log_warn(LD_DIR, "No end-of-signature found on key certificate"); log_warn(LD_DIR, "No end-of-signature found on key certificate");
return NULL; return NULL;
} }
eos = strchr(eos+2, '\n'); eos = memchr(eos+2, '\n', end_of_s - (eos+2));
tor_assert(eos); tor_assert(eos);
++eos; ++eos;
len = eos - s; len = eos - s;
@ -69,7 +71,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
log_warn(LD_DIR, "Error tokenizing key certificate"); log_warn(LD_DIR, "Error tokenizing key certificate");
goto err; goto err;
} }
if (router_get_hash_impl(s, strlen(s), digest, "dir-key-certificate-version", if (router_get_hash_impl(s, eos - s, digest, "dir-key-certificate-version",
"\ndir-key-certification", '\n', DIGEST_SHA1) < 0) "\ndir-key-certification", '\n', DIGEST_SHA1) < 0)
goto err; goto err;
tok = smartlist_get(tokens, 0); tok = smartlist_get(tokens, 0);

View File

@ -13,6 +13,7 @@
#define TOR_AUTHCERT_PARSE_H #define TOR_AUTHCERT_PARSE_H
authority_cert_t *authority_cert_parse_from_string(const char *s, authority_cert_t *authority_cert_parse_from_string(const char *s,
size_t maxlen,
const char **end_of_string); const char **end_of_string);
#endif /* !defined(TOR_AUTHCERT_PARSE_H) */ #endif /* !defined(TOR_AUTHCERT_PARSE_H) */

View File

@ -151,10 +151,11 @@ static token_rule_t networkstatus_vote_footer_token_table[] = {
* -1. */ * -1. */
int int
router_get_networkstatus_v3_signed_boundaries(const char *s, router_get_networkstatus_v3_signed_boundaries(const char *s,
size_t len,
const char **start_out, const char **start_out,
const char **end_out) const char **end_out)
{ {
return router_get_hash_impl_helper(s, strlen(s), return router_get_hash_impl_helper(s, len,
"network-status-version", "network-status-version",
"\ndirectory-signature", "\ndirectory-signature",
' ', LOG_INFO, ' ', LOG_INFO,
@ -166,12 +167,13 @@ router_get_networkstatus_v3_signed_boundaries(const char *s,
* signed portion can be identified. Return 0 on success, -1 on failure. */ * signed portion can be identified. Return 0 on success, -1 on failure. */
int int
router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out, router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
const char *s) const char *s, size_t len)
{ {
const char *start, *end; const char *start, *end;
if (router_get_networkstatus_v3_signed_boundaries(s, &start, &end) < 0) { if (router_get_networkstatus_v3_signed_boundaries(s, len,
&start, &end) < 0) {
start = s; start = s;
end = s + strlen(s); end = s + len;
} }
tor_assert(start); tor_assert(start);
tor_assert(end); tor_assert(end);
@ -182,9 +184,10 @@ router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
/** Set <b>digests</b> to all the digests of the consensus document in /** Set <b>digests</b> to all the digests of the consensus document in
* <b>s</b> */ * <b>s</b> */
int int
router_get_networkstatus_v3_hashes(const char *s, common_digests_t *digests) router_get_networkstatus_v3_hashes(const char *s, size_t len,
common_digests_t *digests)
{ {
return router_get_hashes_impl(s,strlen(s),digests, return router_get_hashes_impl(s, len, digests,
"network-status-version", "network-status-version",
"\ndirectory-signature", "\ndirectory-signature",
' '); ' ');
@ -195,13 +198,13 @@ router_get_networkstatus_v3_hashes(const char *s, common_digests_t *digests)
* return the start of the directory footer, or the next directory signature. * return the start of the directory footer, or the next directory signature.
* If none is found, return the end of the string. */ * If none is found, return the end of the string. */
static inline const char * static inline const char *
find_start_of_next_routerstatus(const char *s) find_start_of_next_routerstatus(const char *s, const char *s_eos)
{ {
const char *eos, *footer, *sig; const char *eos, *footer, *sig;
if ((eos = strstr(s, "\nr "))) if ((eos = tor_memstr(s, s_eos - s, "\nr ")))
++eos; ++eos;
else else
eos = s + strlen(s); eos = s_eos;
footer = tor_memstr(s, eos-s, "\ndirectory-footer"); footer = tor_memstr(s, eos-s, "\ndirectory-footer");
sig = tor_memstr(s, eos-s, "\ndirectory-signature"); sig = tor_memstr(s, eos-s, "\ndirectory-signature");
@ -289,7 +292,8 @@ routerstatus_parse_guardfraction(const char *guardfraction_str,
**/ **/
STATIC routerstatus_t * STATIC routerstatus_t *
routerstatus_parse_entry_from_string(memarea_t *area, routerstatus_parse_entry_from_string(memarea_t *area,
const char **s, smartlist_t *tokens, const char **s, const char *s_eos,
smartlist_t *tokens,
networkstatus_t *vote, networkstatus_t *vote,
vote_routerstatus_t *vote_rs, vote_routerstatus_t *vote_rs,
int consensus_method, int consensus_method,
@ -308,7 +312,7 @@ routerstatus_parse_entry_from_string(memarea_t *area,
flav = FLAV_NS; flav = FLAV_NS;
tor_assert(flav == FLAV_NS || flav == FLAV_MICRODESC); tor_assert(flav == FLAV_NS || flav == FLAV_MICRODESC);
eos = find_start_of_next_routerstatus(*s); eos = find_start_of_next_routerstatus(*s, s_eos);
if (tokenize_string(area,*s, eos, tokens, rtrstatus_token_table,0)) { if (tokenize_string(area,*s, eos, tokens, rtrstatus_token_table,0)) {
log_warn(LD_DIR, "Error tokenizing router status"); log_warn(LD_DIR, "Error tokenizing router status");
@ -1051,7 +1055,9 @@ extract_shared_random_srvs(networkstatus_t *ns, smartlist_t *tokens)
/** Parse a v3 networkstatus vote, opinion, or consensus (depending on /** Parse a v3 networkstatus vote, opinion, or consensus (depending on
* ns_type), from <b>s</b>, and return the result. Return NULL on failure. */ * ns_type), from <b>s</b>, and return the result. Return NULL on failure. */
networkstatus_t * networkstatus_t *
networkstatus_parse_vote_from_string(const char *s, const char **eos_out, networkstatus_parse_vote_from_string(const char *s,
size_t s_len,
const char **eos_out,
networkstatus_type_t ns_type) networkstatus_type_t ns_type)
{ {
smartlist_t *tokens = smartlist_new(); smartlist_t *tokens = smartlist_new();
@ -1067,20 +1073,22 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
memarea_t *area = NULL, *rs_area = NULL; memarea_t *area = NULL, *rs_area = NULL;
consensus_flavor_t flav = FLAV_NS; consensus_flavor_t flav = FLAV_NS;
char *last_kwd=NULL; char *last_kwd=NULL;
const char *eos = s + s_len;
tor_assert(s); tor_assert(s);
if (eos_out) if (eos_out)
*eos_out = NULL; *eos_out = NULL;
if (router_get_networkstatus_v3_hashes(s, &ns_digests) || if (router_get_networkstatus_v3_hashes(s, s_len, &ns_digests) ||
router_get_networkstatus_v3_sha3_as_signed(sha3_as_signed, s)<0) { router_get_networkstatus_v3_sha3_as_signed(sha3_as_signed,
s, s_len)<0) {
log_warn(LD_DIR, "Unable to compute digest of network-status"); log_warn(LD_DIR, "Unable to compute digest of network-status");
goto err; goto err;
} }
area = memarea_new(); area = memarea_new();
end_of_header = find_start_of_next_routerstatus(s); end_of_header = find_start_of_next_routerstatus(s, eos);
if (tokenize_string(area, s, end_of_header, tokens, if (tokenize_string(area, s, end_of_header, tokens,
(ns_type == NS_TYPE_CONSENSUS) ? (ns_type == NS_TYPE_CONSENSUS) ?
networkstatus_consensus_token_table : networkstatus_consensus_token_table :
@ -1111,10 +1119,12 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
if (ns_type != NS_TYPE_CONSENSUS) { if (ns_type != NS_TYPE_CONSENSUS) {
const char *end_of_cert = NULL; const char *end_of_cert = NULL;
if (!(cert = strstr(s, "\ndir-key-certificate-version"))) if (!(cert = tor_memstr(s, end_of_header - s,
"\ndir-key-certificate-version")))
goto err; goto err;
++cert; ++cert;
ns->cert = authority_cert_parse_from_string(cert, &end_of_cert); ns->cert = authority_cert_parse_from_string(cert, end_of_header - cert,
&end_of_cert);
if (!ns->cert || !end_of_cert || end_of_cert > end_of_header) if (!ns->cert || !end_of_cert || end_of_cert > end_of_header)
goto err; goto err;
} }
@ -1424,10 +1434,10 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
s = end_of_header; s = end_of_header;
ns->routerstatus_list = smartlist_new(); ns->routerstatus_list = smartlist_new();
while (!strcmpstart(s, "r ")) { while (eos - s >= 2 && fast_memeq(s, "r ", 2)) {
if (ns->type != NS_TYPE_CONSENSUS) { if (ns->type != NS_TYPE_CONSENSUS) {
vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t)); vote_routerstatus_t *rs = tor_malloc_zero(sizeof(vote_routerstatus_t));
if (routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens, ns, if (routerstatus_parse_entry_from_string(rs_area, &s, eos, rs_tokens, ns,
rs, 0, 0)) { rs, 0, 0)) {
smartlist_add(ns->routerstatus_list, rs); smartlist_add(ns->routerstatus_list, rs);
} else { } else {
@ -1435,7 +1445,8 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
} }
} else { } else {
routerstatus_t *rs; routerstatus_t *rs;
if ((rs = routerstatus_parse_entry_from_string(rs_area, &s, rs_tokens, if ((rs = routerstatus_parse_entry_from_string(rs_area, &s, eos,
rs_tokens,
NULL, NULL, NULL, NULL,
ns->consensus_method, ns->consensus_method,
flav))) { flav))) {
@ -1480,10 +1491,10 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
/* Parse footer; check signature. */ /* Parse footer; check signature. */
footer_tokens = smartlist_new(); footer_tokens = smartlist_new();
if ((end_of_footer = strstr(s, "\nnetwork-status-version "))) if ((end_of_footer = tor_memstr(s, eos-s, "\nnetwork-status-version ")))
++end_of_footer; ++end_of_footer;
else else
end_of_footer = s + strlen(s); end_of_footer = eos;
if (tokenize_string(area,s, end_of_footer, footer_tokens, if (tokenize_string(area,s, end_of_footer, footer_tokens,
networkstatus_vote_footer_token_table, 0)) { networkstatus_vote_footer_token_table, 0)) {
log_warn(LD_DIR, "Error tokenizing network-status vote footer."); log_warn(LD_DIR, "Error tokenizing network-status vote footer.");

View File

@ -12,18 +12,19 @@
#ifndef TOR_NS_PARSE_H #ifndef TOR_NS_PARSE_H
#define TOR_NS_PARSE_H #define TOR_NS_PARSE_H
int router_get_networkstatus_v3_hashes(const char *s, int router_get_networkstatus_v3_hashes(const char *s, size_t len,
common_digests_t *digests); common_digests_t *digests);
int router_get_networkstatus_v3_signed_boundaries(const char *s, int router_get_networkstatus_v3_signed_boundaries(const char *s, size_t len,
const char **start_out, const char **start_out,
const char **end_out); const char **end_out);
int router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out, int router_get_networkstatus_v3_sha3_as_signed(uint8_t *digest_out,
const char *s); const char *s, size_t len);
int compare_vote_routerstatus_entries(const void **_a, const void **_b); int compare_vote_routerstatus_entries(const void **_a, const void **_b);
int networkstatus_verify_bw_weights(networkstatus_t *ns, int); int networkstatus_verify_bw_weights(networkstatus_t *ns, int);
enum networkstatus_type_t; enum networkstatus_type_t;
networkstatus_t *networkstatus_parse_vote_from_string(const char *s, networkstatus_t *networkstatus_parse_vote_from_string(const char *s,
size_t len,
const char **eos_out, const char **eos_out,
enum networkstatus_type_t ns_type); enum networkstatus_type_t ns_type);
@ -35,7 +36,8 @@ STATIC int routerstatus_parse_guardfraction(const char *guardfraction_str,
struct memarea_t; struct memarea_t;
STATIC routerstatus_t *routerstatus_parse_entry_from_string( STATIC routerstatus_t *routerstatus_parse_entry_from_string(
struct memarea_t *area, struct memarea_t *area,
const char **s, smartlist_t *tokens, const char **s, const char *eos,
smartlist_t *tokens,
networkstatus_t *vote, networkstatus_t *vote,
vote_routerstatus_t *vote_rs, vote_routerstatus_t *vote_rs,
int consensus_method, int consensus_method,

View File

@ -380,7 +380,8 @@ trusted_dirs_load_certs_from_string(const char *contents, int source,
int added_trusted_cert = 0; int added_trusted_cert = 0;
for (s = contents; *s; s = eos) { for (s = contents; *s; s = eos) {
authority_cert_t *cert = authority_cert_parse_from_string(s, &eos); authority_cert_t *cert = authority_cert_parse_from_string(s, strlen(s),
&eos);
cert_list_t *cl; cert_list_t *cl;
if (!cert) { if (!cert) {
failure_code = -1; failure_code = -1;

View File

@ -116,8 +116,6 @@ STATIC networkstatus_t *current_md_consensus = NULL;
typedef struct consensus_waiting_for_certs_t { typedef struct consensus_waiting_for_certs_t {
/** The consensus itself. */ /** The consensus itself. */
networkstatus_t *consensus; networkstatus_t *consensus;
/** The encoded version of the consensus, nul-terminated. */
char *body;
/** When did we set the current value of consensus_waiting_for_certs? If /** When did we set the current value of consensus_waiting_for_certs? If
* this is too recent, we shouldn't try to fetch a new consensus for a * this is too recent, we shouldn't try to fetch a new consensus for a
* little while, to give ourselves time to get certificates for this one. */ * little while, to give ourselves time to get certificates for this one. */
@ -210,14 +208,11 @@ networkstatus_reset_download_failures(void)
download_status_reset(&consensus_bootstrap_dl_status[i]); download_status_reset(&consensus_bootstrap_dl_status[i]);
} }
/** /** Return the filename used to cache the consensus of a given flavor */
* Read and and return the cached consensus of type <b>flavorname</b>. If
* <b>unverified</b> is true, get the one we haven't verified. Return NULL if
* the file isn't there. */
static char * static char *
networkstatus_read_cached_consensus_impl(int flav, networkstatus_get_cache_fname(int flav,
const char *flavorname, const char *flavorname,
int unverified_consensus) int unverified_consensus)
{ {
char buf[128]; char buf[128];
const char *prefix; const char *prefix;
@ -232,21 +227,35 @@ networkstatus_read_cached_consensus_impl(int flav,
tor_snprintf(buf, sizeof(buf), "%s-%s-consensus", prefix, flavorname); tor_snprintf(buf, sizeof(buf), "%s-%s-consensus", prefix, flavorname);
} }
char *filename = get_cachedir_fname(buf); return get_cachedir_fname(buf);
char *result = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL); }
/**
* Read and and return the cached consensus of type <b>flavorname</b>. If
* <b>unverified</b> is false, get the one we haven't verified. Return NULL if
* the file isn't there. */
static tor_mmap_t *
networkstatus_map_cached_consensus_impl(int flav,
const char *flavorname,
int unverified_consensus)
{
char *filename = networkstatus_get_cache_fname(flav,
flavorname,
unverified_consensus);
tor_mmap_t *result = tor_mmap_file(filename);
tor_free(filename); tor_free(filename);
return result; return result;
} }
/** Return a new string containing the current cached consensus of flavor /** Map the file containing the current cached consensus of flavor
* <b>flavorname</b>. */ * <b>flavorname</b> */
char * tor_mmap_t *
networkstatus_read_cached_consensus(const char *flavorname) networkstatus_map_cached_consensus(const char *flavorname)
{ {
int flav = networkstatus_parse_flavor_name(flavorname); int flav = networkstatus_parse_flavor_name(flavorname);
if (flav < 0) if (flav < 0)
return NULL; return NULL;
return networkstatus_read_cached_consensus_impl(flav, flavorname, 0); return networkstatus_map_cached_consensus_impl(flav, flavorname, 0);
} }
/** Read every cached v3 consensus networkstatus from the disk. */ /** Read every cached v3 consensus networkstatus from the disk. */
@ -259,24 +268,26 @@ router_reload_consensus_networkstatus(void)
/* FFFF Suppress warnings if cached consensus is bad? */ /* FFFF Suppress warnings if cached consensus is bad? */
for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) { for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
const char *flavor = networkstatus_get_flavor_name(flav); const char *flavor = networkstatus_get_flavor_name(flav);
char *s = networkstatus_read_cached_consensus_impl(flav, flavor, 0); tor_mmap_t *m = networkstatus_map_cached_consensus_impl(flav, flavor, 0);
if (s) { if (m) {
if (networkstatus_set_current_consensus(s, flavor, flags, NULL) < -1) { if (networkstatus_set_current_consensus(m->data, m->size,
flavor, flags, NULL) < -1) {
log_warn(LD_FS, "Couldn't load consensus %s networkstatus from cache", log_warn(LD_FS, "Couldn't load consensus %s networkstatus from cache",
flavor); flavor);
} }
tor_free(s); tor_munmap_file(m);
} }
s = networkstatus_read_cached_consensus_impl(flav, flavor, 1); m = networkstatus_map_cached_consensus_impl(flav, flavor, 1);
if (s) { if (m) {
if (networkstatus_set_current_consensus(s, flavor, if (networkstatus_set_current_consensus(m->data, m->size,
flavor,
flags | NSSET_WAS_WAITING_FOR_CERTS, flags | NSSET_WAS_WAITING_FOR_CERTS,
NULL)) { NULL)) {
log_info(LD_FS, "Couldn't load unverified consensus %s networkstatus " log_info(LD_FS, "Couldn't load unverified consensus %s networkstatus "
"from cache", flavor); "from cache", flavor);
} }
tor_free(s); tor_munmap_file(m);
} }
} }
@ -1844,6 +1855,7 @@ warn_early_consensus(const networkstatus_t *c, const char *flavor,
*/ */
int int
networkstatus_set_current_consensus(const char *consensus, networkstatus_set_current_consensus(const char *consensus,
size_t consensus_len,
const char *flavor, const char *flavor,
unsigned flags, unsigned flags,
const char *source_dir) const char *source_dir)
@ -1872,7 +1884,9 @@ networkstatus_set_current_consensus(const char *consensus,
} }
/* Make sure it's parseable. */ /* Make sure it's parseable. */
c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS); c = networkstatus_parse_vote_from_string(consensus,
consensus_len,
NULL, NS_TYPE_CONSENSUS);
if (!c) { if (!c) {
log_warn(LD_DIR, "Unable to parse networkstatus consensus"); log_warn(LD_DIR, "Unable to parse networkstatus consensus");
result = -2; result = -2;
@ -1960,14 +1974,12 @@ networkstatus_set_current_consensus(const char *consensus,
c->valid_after > current_valid_after) { c->valid_after > current_valid_after) {
waiting = &consensus_waiting_for_certs[flav]; waiting = &consensus_waiting_for_certs[flav];
networkstatus_vote_free(waiting->consensus); networkstatus_vote_free(waiting->consensus);
tor_free(waiting->body);
waiting->consensus = c; waiting->consensus = c;
free_consensus = 0; free_consensus = 0;
waiting->body = tor_strdup(consensus);
waiting->set_at = now; waiting->set_at = now;
waiting->dl_failed = 0; waiting->dl_failed = 0;
if (!from_cache) { if (!from_cache) {
write_str_to_file(unverified_fname, consensus, 0); write_bytes_to_file(unverified_fname, consensus, consensus_len, 0);
} }
if (dl_certs) if (dl_certs)
authority_certs_fetch_missing(c, now, source_dir); authority_certs_fetch_missing(c, now, source_dir);
@ -2058,10 +2070,6 @@ networkstatus_set_current_consensus(const char *consensus,
waiting->consensus->valid_after <= c->valid_after) { waiting->consensus->valid_after <= c->valid_after) {
networkstatus_vote_free(waiting->consensus); networkstatus_vote_free(waiting->consensus);
waiting->consensus = NULL; waiting->consensus = NULL;
if (consensus != waiting->body)
tor_free(waiting->body);
else
waiting->body = NULL;
waiting->set_at = 0; waiting->set_at = 0;
waiting->dl_failed = 0; waiting->dl_failed = 0;
if (unlink(unverified_fname) != 0) { if (unlink(unverified_fname) != 0) {
@ -2111,17 +2119,18 @@ networkstatus_set_current_consensus(const char *consensus,
if (we_want_to_fetch_flavor(options, flav)) { if (we_want_to_fetch_flavor(options, flav)) {
if (dir_server_mode(get_options())) { if (dir_server_mode(get_options())) {
dirserv_set_cached_consensus_networkstatus(consensus, dirserv_set_cached_consensus_networkstatus(consensus,
consensus_len,
flavor, flavor,
&c->digests, &c->digests,
c->digest_sha3_as_signed, c->digest_sha3_as_signed,
c->valid_after); c->valid_after);
consdiffmgr_add_consensus(consensus, c); consdiffmgr_add_consensus(consensus, consensus_len, c);
} }
} }
if (!from_cache) { if (!from_cache) {
write_str_to_file(consensus_fname, consensus, 0); write_bytes_to_file(consensus_fname, consensus, consensus_len, 0);
} }
warn_early_consensus(c, flavor, now); warn_early_consensus(c, flavor, now);
@ -2157,14 +2166,16 @@ networkstatus_note_certs_arrived(const char *source_dir)
if (!waiting->consensus) if (!waiting->consensus)
continue; continue;
if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) { if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
char *waiting_body = waiting->body; tor_mmap_t *mapping = networkstatus_map_cached_consensus_impl(
if (!networkstatus_set_current_consensus( i, flavor_name, 1);
waiting_body, if (mapping) {
flavor_name, networkstatus_set_current_consensus(mapping->data,
NSSET_WAS_WAITING_FOR_CERTS, mapping->size,
source_dir)) { flavor_name,
tor_free(waiting_body); NSSET_WAS_WAITING_FOR_CERTS,
source_dir);
} }
tor_munmap_file(mapping);
} }
} }
} }
@ -2721,6 +2732,5 @@ networkstatus_free_all(void)
networkstatus_vote_free(waiting->consensus); networkstatus_vote_free(waiting->consensus);
waiting->consensus = NULL; waiting->consensus = NULL;
} }
tor_free(waiting->body);
} }
} }

View File

@ -16,7 +16,7 @@
void networkstatus_reset_warnings(void); void networkstatus_reset_warnings(void);
void networkstatus_reset_download_failures(void); void networkstatus_reset_download_failures(void);
char *networkstatus_read_cached_consensus(const char *flavorname); tor_mmap_t *networkstatus_map_cached_consensus(const char *flavorname);
int router_reload_consensus_networkstatus(void); int router_reload_consensus_networkstatus(void);
void routerstatus_free_(routerstatus_t *rs); void routerstatus_free_(routerstatus_t *rs);
#define routerstatus_free(rs) \ #define routerstatus_free(rs) \
@ -105,6 +105,7 @@ int networkstatus_consensus_has_ipv6(const or_options_t* options);
#define NSSET_ACCEPT_OBSOLETE 8 #define NSSET_ACCEPT_OBSOLETE 8
#define NSSET_REQUIRE_FLAVOR 16 #define NSSET_REQUIRE_FLAVOR 16
int networkstatus_set_current_consensus(const char *consensus, int networkstatus_set_current_consensus(const char *consensus,
size_t consensus_len,
const char *flavor, const char *flavor,
unsigned flags, unsigned flags,
const char *source_dir); const char *source_dir);
@ -156,4 +157,3 @@ extern networkstatus_t *current_md_consensus;
#endif /* defined(NETWORKSTATUS_PRIVATE) */ #endif /* defined(NETWORKSTATUS_PRIVATE) */
#endif /* !defined(TOR_NETWORKSTATUS_H) */ #endif /* !defined(TOR_NETWORKSTATUS_H) */

View File

@ -636,7 +636,7 @@ load_authority_keyset(int legacy, crypto_pk_t **key_out,
fname); fname);
goto done; goto done;
} }
parsed = authority_cert_parse_from_string(cert, &eos); parsed = authority_cert_parse_from_string(cert, strlen(cert), &eos);
if (!parsed) { if (!parsed) {
log_warn(LD_DIR, "Unable to parse certificate in %s", fname); log_warn(LD_DIR, "Unable to parse certificate in %s", fname);
goto done; goto done;

View File

@ -702,11 +702,13 @@ main(int argc, const char **argv)
perror("X"); perror("X");
return 1; return 1;
} }
size_t f1len = strlen(f1);
size_t f2len = strlen(f2);
for (i = 0; i < N; ++i) { for (i = 0; i < N; ++i) {
char *diff = consensus_diff_generate(f1, f2); char *diff = consensus_diff_generate(f1, f1len, f2, f2len);
tor_free(diff); tor_free(diff);
} }
char *diff = consensus_diff_generate(f1, f2); char *diff = consensus_diff_generate(f1, f1len, f2, f2len);
printf("%s", diff); printf("%s", diff);
tor_free(f1); tor_free(f1);
tor_free(f2); tor_free(f2);

View File

@ -61,13 +61,13 @@ int
fuzz_main(const uint8_t *data, size_t sz) fuzz_main(const uint8_t *data, size_t sz)
{ {
networkstatus_t *ns; networkstatus_t *ns;
char *str = tor_memdup_nulterm(data, sz);
const char *eos = NULL; const char *eos = NULL;
networkstatus_type_t tp = NS_TYPE_CONSENSUS; networkstatus_type_t tp = NS_TYPE_CONSENSUS;
if (tor_memstr(data, MIN(sz, 1024), "tus vote")) if (tor_memstr(data, MIN(sz, 1024), "tus vote"))
tp = NS_TYPE_VOTE; tp = NS_TYPE_VOTE;
const char *what = (tp == NS_TYPE_CONSENSUS) ? "consensus" : "vote"; const char *what = (tp == NS_TYPE_CONSENSUS) ? "consensus" : "vote";
ns = networkstatus_parse_vote_from_string(str, ns = networkstatus_parse_vote_from_string((const char *)data,
sz,
&eos, &eos,
tp); tp);
if (ns) { if (ns) {
@ -76,6 +76,6 @@ fuzz_main(const uint8_t *data, size_t sz)
} else { } else {
log_debug(LD_GENERAL, "Parsing as %s failed", what); log_debug(LD_GENERAL, "Parsing as %s failed", what);
} }
tor_free(str);
return 0; return 0;
} }

View File

@ -10,9 +10,11 @@
#include "test/fuzz/fuzzing.h" #include "test/fuzz/fuzzing.h"
static int static int
mock_consensus_compute_digest_(const char *c, consensus_digest_t *d) mock_consensus_compute_digest_(const char *c, size_t len,
consensus_digest_t *d)
{ {
(void)c; (void)c;
(void)len;
memset(d->sha3_256, 3, sizeof(d->sha3_256)); memset(d->sha3_256, 3, sizeof(d->sha3_256));
return 0; return 0;
} }
@ -42,28 +44,34 @@ fuzz_main(const uint8_t *stdin_buf, size_t data_size)
if (! separator) if (! separator)
return 0; return 0;
size_t c1_len = separator - stdin_buf; size_t c1_len = separator - stdin_buf;
char *c1 = tor_memdup_nulterm(stdin_buf, c1_len); const char *c1 = (const char *)stdin_buf;
size_t c2_len = data_size - c1_len - SEPLEN; size_t c2_len = data_size - c1_len - SEPLEN;
char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len); const char *c2 = (const char *)separator + SEPLEN;
char *c3 = consensus_diff_generate(c1, c2); const char *cp = memchr(c1, 0, c1_len);
if (cp)
c1_len = cp - c1;
cp = memchr(c2, 0, c2_len);
if (cp)
c2_len = cp - c2;
char *c3 = consensus_diff_generate(c1, c1_len, c2, c2_len);
if (c3) { if (c3) {
char *c4 = consensus_diff_apply(c1, c3); char *c4 = consensus_diff_apply(c1, c1_len, c3, strlen(c3));
tor_assert(c4); tor_assert(c4);
if (strcmp(c2, c4)) { int equal = (c2_len == strlen(c4)) && fast_memeq(c2, c4, c2_len);
printf("%s\n", escaped(c1)); if (! equal) {
printf("%s\n", escaped(c2)); //printf("%s\n", escaped(c1));
//printf("%s\n", escaped(c2));
printf("%s\n", escaped(c3)); printf("%s\n", escaped(c3));
printf("%s\n", escaped(c4)); printf("%s\n", escaped(c4));
} }
tor_assert(! strcmp(c2, c4)); tor_assert(equal);
tor_free(c3); tor_free(c3);
tor_free(c4); tor_free(c4);
} }
tor_free(c1);
tor_free(c2);
return 0; return 0;
} }

View File

@ -10,9 +10,11 @@
#include "test/fuzz/fuzzing.h" #include "test/fuzz/fuzzing.h"
static int static int
mock_consensus_compute_digest_(const char *c, consensus_digest_t *d) mock_consensus_compute_digest_(const char *c, size_t len,
consensus_digest_t *d)
{ {
(void)c; (void)c;
(void)len;
memset(d->sha3_256, 3, sizeof(d->sha3_256)); memset(d->sha3_256, 3, sizeof(d->sha3_256));
return 0; return 0;
} }
@ -50,16 +52,13 @@ fuzz_main(const uint8_t *stdin_buf, size_t data_size)
if (! separator) if (! separator)
return 0; return 0;
size_t c1_len = separator - stdin_buf; size_t c1_len = separator - stdin_buf;
char *c1 = tor_memdup_nulterm(stdin_buf, c1_len); const char *c1 = (const char *)stdin_buf;
size_t c2_len = data_size - c1_len - SEPLEN; size_t c2_len = data_size - c1_len - SEPLEN;
char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len); const char *c2 = (const char *)separator + SEPLEN;
char *c3 = consensus_diff_apply(c1, c2); char *c3 = consensus_diff_apply(c1, c1_len, c2, c2_len);
tor_free(c1);
tor_free(c2);
tor_free(c3); tor_free(c3);
return 0; return 0;
} }

View File

@ -53,24 +53,24 @@ fuzz_cleanup(void)
int int
fuzz_main(const uint8_t *data, size_t sz) fuzz_main(const uint8_t *data, size_t sz)
{ {
char *str = tor_memdup_nulterm(data, sz);
const char *s; const char *s;
routerstatus_t *rs_ns = NULL, *rs_md = NULL, *rs_vote = NULL; routerstatus_t *rs_ns = NULL, *rs_md = NULL, *rs_vote = NULL;
vote_routerstatus_t *vrs = tor_malloc_zero(sizeof(*vrs)); vote_routerstatus_t *vrs = tor_malloc_zero(sizeof(*vrs));
smartlist_t *tokens = smartlist_new(); smartlist_t *tokens = smartlist_new();
const char *eos = (const char *)data + sz;
s = str; s = (const char *)data;
rs_ns = routerstatus_parse_entry_from_string(area, &s, tokens, rs_ns = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
NULL, NULL, 26, FLAV_NS); NULL, NULL, 26, FLAV_NS);
tor_assert(smartlist_len(tokens) == 0); tor_assert(smartlist_len(tokens) == 0);
s = str; s = (const char *)data;
rs_md = routerstatus_parse_entry_from_string(area, &s, tokens, rs_md = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
NULL, NULL, 26, FLAV_MICRODESC); NULL, NULL, 26, FLAV_MICRODESC);
tor_assert(smartlist_len(tokens) == 0); tor_assert(smartlist_len(tokens) == 0);
s = str; s = (const char *)data;
rs_vote = routerstatus_parse_entry_from_string(area, &s, tokens, rs_vote = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
dummy_vote, vrs, 26, FLAV_NS); dummy_vote, vrs, 26, FLAV_NS);
tor_assert(smartlist_len(tokens) == 0); tor_assert(smartlist_len(tokens) == 0);
@ -82,6 +82,6 @@ fuzz_main(const uint8_t *data, size_t sz)
vote_routerstatus_free(vrs); vote_routerstatus_free(vrs);
memarea_clear(area); memarea_clear(area);
smartlist_free(tokens); smartlist_free(tokens);
tor_free(str);
return 0; return 0;
} }

View File

@ -14,6 +14,39 @@
#define tt_str_eq_line(a,b) \ #define tt_str_eq_line(a,b) \
tt_assert(line_str_eq((b),(a))) tt_assert(line_str_eq((b),(a)))
static int
consensus_split_lines_(smartlist_t *out, const char *s, memarea_t *area)
{
size_t len = strlen(s);
return consensus_split_lines(out, s, len, area);
}
static int
consensus_compute_digest_(const char *cons,
consensus_digest_t *digest_out)
{
size_t len = strlen(cons);
char *tmp = tor_memdup(cons, len);
// We use memdup here to ensure that the input is NOT nul-terminated.
// This makes it likelier for us to spot bugs.
int r = consensus_compute_digest(tmp, len, digest_out);
tor_free(tmp);
return r;
}
static int
consensus_compute_digest_as_signed_(const char *cons,
consensus_digest_t *digest_out)
{
size_t len = strlen(cons);
char *tmp = tor_memdup(cons, len);
// We use memdup here to ensure that the input is NOT nul-terminated.
// This makes it likelier for us to spot bugs.
int r = consensus_compute_digest_as_signed(tmp, len, digest_out);
tor_free(tmp);
return r;
}
static void static void
test_consdiff_smartlist_slice(void *arg) test_consdiff_smartlist_slice(void *arg)
{ {
@ -58,7 +91,7 @@ test_consdiff_smartlist_slice_string_pos(void *arg)
/* Create a regular smartlist. */ /* Create a regular smartlist. */
(void)arg; (void)arg;
consensus_split_lines(sl, "a\nd\nc\na\nb\n", area); consensus_split_lines_(sl, "a\nd\nc\na\nb\n", area);
/* See that smartlist_slice_string_pos respects the bounds of the slice. */ /* See that smartlist_slice_string_pos respects the bounds of the slice. */
sls = smartlist_slice(sl, 2, 5); sls = smartlist_slice(sl, 2, 5);
@ -87,8 +120,8 @@ test_consdiff_lcs_lengths(void *arg)
int e_lengths2[] = { 0, 1, 1, 2, 3, 4 }; int e_lengths2[] = { 0, 1, 1, 2, 3, 4 };
(void)arg; (void)arg;
consensus_split_lines(sl1, "a\nb\nc\nd\ne\n", area); consensus_split_lines_(sl1, "a\nb\nc\nd\ne\n", area);
consensus_split_lines(sl2, "a\nc\nd\ni\ne\n", area); consensus_split_lines_(sl2, "a\nc\nd\ni\ne\n", area);
sls1 = smartlist_slice(sl1, 0, -1); sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1); sls2 = smartlist_slice(sl2, 0, -1);
@ -119,10 +152,10 @@ test_consdiff_trim_slices(void *arg)
memarea_t *area = memarea_new(); memarea_t *area = memarea_new();
(void)arg; (void)arg;
consensus_split_lines(sl1, "a\nb\nb\nb\nd\n", area); consensus_split_lines_(sl1, "a\nb\nb\nb\nd\n", area);
consensus_split_lines(sl2, "a\nc\nc\nc\nd\n", area); consensus_split_lines_(sl2, "a\nc\nc\nc\nd\n", area);
consensus_split_lines(sl3, "a\nb\nb\nb\na\n", area); consensus_split_lines_(sl3, "a\nb\nb\nb\na\n", area);
consensus_split_lines(sl4, "c\nb\nb\nb\nc\n", area); consensus_split_lines_(sl4, "c\nb\nb\nb\nc\n", area);
sls1 = smartlist_slice(sl1, 0, -1); sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1); sls2 = smartlist_slice(sl2, 0, -1);
sls3 = smartlist_slice(sl3, 0, -1); sls3 = smartlist_slice(sl3, 0, -1);
@ -165,8 +198,8 @@ test_consdiff_set_changed(void *arg)
memarea_t *area = memarea_new(); memarea_t *area = memarea_new();
(void)arg; (void)arg;
consensus_split_lines(sl1, "a\nb\na\na\n", area); consensus_split_lines_(sl1, "a\nb\na\na\n", area);
consensus_split_lines(sl2, "a\na\na\na\n", area); consensus_split_lines_(sl2, "a\na\na\na\n", area);
/* Length of sls1 is 0. */ /* Length of sls1 is 0. */
sls1 = smartlist_slice(sl1, 0, 0); sls1 = smartlist_slice(sl1, 0, 0);
@ -240,8 +273,8 @@ test_consdiff_calc_changes(void *arg)
memarea_t *area = memarea_new(); memarea_t *area = memarea_new();
(void)arg; (void)arg;
consensus_split_lines(sl1, "a\na\na\na\n", area); consensus_split_lines_(sl1, "a\na\na\na\n", area);
consensus_split_lines(sl2, "a\na\na\na\n", area); consensus_split_lines_(sl2, "a\na\na\na\n", area);
sls1 = smartlist_slice(sl1, 0, -1); sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1); sls2 = smartlist_slice(sl2, 0, -1);
@ -259,7 +292,7 @@ test_consdiff_calc_changes(void *arg)
tt_assert(!bitarray_is_set(changed2, 3)); tt_assert(!bitarray_is_set(changed2, 3));
smartlist_clear(sl2); smartlist_clear(sl2);
consensus_split_lines(sl2, "a\nb\na\nb\n", area); consensus_split_lines_(sl2, "a\nb\na\nb\n", area);
tor_free(sls1); tor_free(sls1);
tor_free(sls2); tor_free(sls2);
sls1 = smartlist_slice(sl1, 0, -1); sls1 = smartlist_slice(sl1, 0, -1);
@ -282,7 +315,7 @@ test_consdiff_calc_changes(void *arg)
bitarray_clear(changed1, 3); bitarray_clear(changed1, 3);
smartlist_clear(sl2); smartlist_clear(sl2);
consensus_split_lines(sl2, "b\nb\nb\nb\n", area); consensus_split_lines_(sl2, "b\nb\nb\nb\n", area);
tor_free(sls1); tor_free(sls1);
tor_free(sls2); tor_free(sls2);
sls1 = smartlist_slice(sl1, 0, -1); sls1 = smartlist_slice(sl1, 0, -1);
@ -610,8 +643,8 @@ test_consdiff_gen_ed_diff(void *arg)
/* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */ /* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */
smartlist_clear(cons1); smartlist_clear(cons1);
smartlist_clear(cons2); smartlist_clear(cons2);
consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area); consensus_split_lines_(cons1, "A\nB\nC\nD\nE\n", area);
consensus_split_lines(cons2, "A\nC\nO\nE\nU\n", area); consensus_split_lines_(cons2, "A\nC\nO\nE\nU\n", area);
diff = gen_ed_diff(cons1, cons2, area); diff = gen_ed_diff(cons1, cons2, area);
tt_ptr_op(NULL, OP_NE, diff); tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(7, OP_EQ, smartlist_len(diff)); tt_int_op(7, OP_EQ, smartlist_len(diff));
@ -627,8 +660,8 @@ test_consdiff_gen_ed_diff(void *arg)
smartlist_clear(cons1); smartlist_clear(cons1);
smartlist_clear(cons2); smartlist_clear(cons2);
consensus_split_lines(cons1, "B\n", area); consensus_split_lines_(cons1, "B\n", area);
consensus_split_lines(cons2, "A\nB\n", area); consensus_split_lines_(cons2, "A\nB\n", area);
diff = gen_ed_diff(cons1, cons2, area); diff = gen_ed_diff(cons1, cons2, area);
tt_ptr_op(NULL, OP_NE, diff); tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(3, OP_EQ, smartlist_len(diff)); tt_int_op(3, OP_EQ, smartlist_len(diff));
@ -656,7 +689,7 @@ test_consdiff_apply_ed_diff(void *arg)
diff = smartlist_new(); diff = smartlist_new();
setup_capture_of_logs(LOG_WARN); setup_capture_of_logs(LOG_WARN);
consensus_split_lines(cons1, "A\nB\nC\nD\nE\n", area); consensus_split_lines_(cons1, "A\nB\nC\nD\nE\n", area);
/* Command without range. */ /* Command without range. */
smartlist_add_linecpy(diff, area, "a"); smartlist_add_linecpy(diff, area, "a");
@ -829,7 +862,7 @@ test_consdiff_apply_ed_diff(void *arg)
smartlist_clear(diff); smartlist_clear(diff);
/* Test appending text, 'a'. */ /* Test appending text, 'a'. */
consensus_split_lines(diff, "3a\nU\nO\n.\n0a\nV\n.\n", area); consensus_split_lines_(diff, "3a\nU\nO\n.\n0a\nV\n.\n", area);
cons2 = apply_ed_diff(cons1, diff, 0); cons2 = apply_ed_diff(cons1, diff, 0);
tt_ptr_op(NULL, OP_NE, cons2); tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(8, OP_EQ, smartlist_len(cons2)); tt_int_op(8, OP_EQ, smartlist_len(cons2));
@ -846,7 +879,7 @@ test_consdiff_apply_ed_diff(void *arg)
smartlist_free(cons2); smartlist_free(cons2);
/* Test deleting text, 'd'. */ /* Test deleting text, 'd'. */
consensus_split_lines(diff, "4d\n1,2d\n", area); consensus_split_lines_(diff, "4d\n1,2d\n", area);
cons2 = apply_ed_diff(cons1, diff, 0); cons2 = apply_ed_diff(cons1, diff, 0);
tt_ptr_op(NULL, OP_NE, cons2); tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(2, OP_EQ, smartlist_len(cons2)); tt_int_op(2, OP_EQ, smartlist_len(cons2));
@ -857,7 +890,7 @@ test_consdiff_apply_ed_diff(void *arg)
smartlist_free(cons2); smartlist_free(cons2);
/* Test changing text, 'c'. */ /* Test changing text, 'c'. */
consensus_split_lines(diff, "4c\nT\nX\n.\n1,2c\nM\n.\n", area); consensus_split_lines_(diff, "4c\nT\nX\n.\n1,2c\nM\n.\n", area);
cons2 = apply_ed_diff(cons1, diff, 0); cons2 = apply_ed_diff(cons1, diff, 0);
tt_ptr_op(NULL, OP_NE, cons2); tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(5, OP_EQ, smartlist_len(cons2)); tt_int_op(5, OP_EQ, smartlist_len(cons2));
@ -871,7 +904,7 @@ test_consdiff_apply_ed_diff(void *arg)
smartlist_free(cons2); smartlist_free(cons2);
/* Test 'a', 'd' and 'c' together. */ /* Test 'a', 'd' and 'c' together. */
consensus_split_lines(diff, "4c\nT\nX\n.\n2d\n0a\nM\n.\n", area); consensus_split_lines_(diff, "4c\nT\nX\n.\n2d\n0a\nM\n.\n", area);
cons2 = apply_ed_diff(cons1, diff, 0); cons2 = apply_ed_diff(cons1, diff, 0);
tt_ptr_op(NULL, OP_NE, cons2); tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(6, OP_EQ, smartlist_len(cons2)); tt_int_op(6, OP_EQ, smartlist_len(cons2));
@ -918,12 +951,12 @@ test_consdiff_gen_diff(void *arg)
); );
tt_int_op(0, OP_EQ, tt_int_op(0, OP_EQ,
consensus_compute_digest_as_signed(cons1_str, &digests1)); consensus_compute_digest_as_signed_(cons1_str, &digests1));
tt_int_op(0, OP_EQ, tt_int_op(0, OP_EQ,
consensus_compute_digest(cons2_str, &digests2)); consensus_compute_digest_(cons2_str, &digests2));
consensus_split_lines(cons1, cons1_str, area); consensus_split_lines_(cons1, cons1_str, area);
consensus_split_lines(cons2, cons2_str, area); consensus_split_lines_(cons2, cons2_str, area);
diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area); diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
tt_ptr_op(NULL, OP_EQ, diff); tt_ptr_op(NULL, OP_EQ, diff);
@ -937,9 +970,9 @@ test_consdiff_gen_diff(void *arg)
"directory-signature foo bar\nbar\n" "directory-signature foo bar\nbar\n"
); );
tt_int_op(0, OP_EQ, tt_int_op(0, OP_EQ,
consensus_compute_digest_as_signed(cons1_str, &digests1)); consensus_compute_digest_as_signed_(cons1_str, &digests1));
smartlist_clear(cons1); smartlist_clear(cons1);
consensus_split_lines(cons1, cons1_str, area); consensus_split_lines_(cons1, cons1_str, area);
diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area); diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2, area);
tt_ptr_op(NULL, OP_NE, diff); tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(11, OP_EQ, smartlist_len(diff)); tt_int_op(11, OP_EQ, smartlist_len(diff));
@ -991,8 +1024,8 @@ test_consdiff_apply_diff(void *arg)
"directory-signature foo bar\nbar\n" "directory-signature foo bar\nbar\n"
); );
tt_int_op(0, OP_EQ, tt_int_op(0, OP_EQ,
consensus_compute_digest(cons1_str, &digests1)); consensus_compute_digest_(cons1_str, &digests1));
consensus_split_lines(cons1, cons1_str, area); consensus_split_lines_(cons1, cons1_str, area);
/* diff doesn't have enough lines. */ /* diff doesn't have enough lines. */
cons2 = consdiff_apply_diff(cons1, diff, &digests1); cons2 = consdiff_apply_diff(cons1, diff, &digests1);
@ -1182,4 +1215,3 @@ struct testcase_t consdiff_tests[] = {
CONSDIFF_LEGACY(apply_diff), CONSDIFF_LEGACY(apply_diff),
END_OF_TESTCASES END_OF_TESTCASES
}; };

View File

@ -21,6 +21,23 @@
#include "test/test.h" #include "test/test.h"
#include "test/log_test_helpers.h" #include "test/log_test_helpers.h"
#define consdiffmgr_add_consensus consdiffmgr_add_consensus_nulterm
static char *
consensus_diff_apply_(const char *c, const char *d)
{
size_t c_len = strlen(c);
size_t d_len = strlen(d);
// We use memdup here to ensure that the input is NOT nul-terminated.
// This makes it likelier for us to spot bugs.
char *c_tmp = tor_memdup(c, c_len);
char *d_tmp = tor_memdup(d, d_len);
char *result = consensus_diff_apply(c_tmp, c_len, d_tmp, d_len);
tor_free(c_tmp);
tor_free(d_tmp);
return result;
}
// ============================== Setup/teardown the consdiffmgr // ============================== Setup/teardown the consdiffmgr
// These functions get run before/after each test in this module // These functions get run before/after each test in this module
@ -153,7 +170,8 @@ lookup_diff_from(consensus_cache_entry_t **out,
const char *str1) const char *str1)
{ {
uint8_t digest[DIGEST256_LEN]; uint8_t digest[DIGEST256_LEN];
if (router_get_networkstatus_v3_sha3_as_signed(digest, str1)<0) { if (router_get_networkstatus_v3_sha3_as_signed(digest,
str1, strlen(str1))<0) {
TT_FAIL(("Unable to compute sha3-as-signed")); TT_FAIL(("Unable to compute sha3-as-signed"));
return CONSDIFF_NOT_FOUND; return CONSDIFF_NOT_FOUND;
} }
@ -175,14 +193,15 @@ lookup_apply_and_verify_diff(consensus_flavor_t flav,
consensus_cache_entry_incref(ent); consensus_cache_entry_incref(ent);
size_t size; size_t size;
char *diff_string = NULL; const char *diff_string = NULL;
int r = uncompress_or_copy(&diff_string, &size, ent); char *diff_owned = NULL;
int r = uncompress_or_set_ptr(&diff_string, &size, &diff_owned, ent);
consensus_cache_entry_decref(ent); consensus_cache_entry_decref(ent);
if (diff_string == NULL || r < 0) if (diff_string == NULL || r < 0)
return -1; return -1;
char *applied = consensus_diff_apply(str1, diff_string); char *applied = consensus_diff_apply(str1, strlen(str1), diff_string, size);
tor_free(diff_string); tor_free(diff_owned);
if (applied == NULL) if (applied == NULL)
return -1; return -1;
@ -282,7 +301,8 @@ test_consdiffmgr_add(void *arg)
(void) arg; (void) arg;
time_t now = approx_time(); time_t now = approx_time();
char *body = NULL; const char *body = NULL;
char *body_owned = NULL;
consensus_cache_entry_t *ent = NULL; consensus_cache_entry_t *ent = NULL;
networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now); networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now);
@ -324,7 +344,7 @@ test_consdiffmgr_add(void *arg)
tt_assert(ent); tt_assert(ent);
consensus_cache_entry_incref(ent); consensus_cache_entry_incref(ent);
size_t s; size_t s;
r = uncompress_or_copy(&body, &s, ent); r = uncompress_or_set_ptr(&body, &s, &body_owned, ent);
tt_int_op(r, OP_EQ, 0); tt_int_op(r, OP_EQ, 0);
tt_int_op(s, OP_EQ, 4); tt_int_op(s, OP_EQ, 4);
tt_mem_op(body, OP_EQ, "quux", 4); tt_mem_op(body, OP_EQ, "quux", 4);
@ -337,7 +357,7 @@ test_consdiffmgr_add(void *arg)
networkstatus_vote_free(ns_tmp); networkstatus_vote_free(ns_tmp);
teardown_capture_of_logs(); teardown_capture_of_logs();
consensus_cache_entry_decref(ent); consensus_cache_entry_decref(ent);
tor_free(body); tor_free(body_owned);
} }
static void static void
@ -370,7 +390,8 @@ test_consdiffmgr_make_diffs(void *arg)
ns = fake_ns_new(FLAV_MICRODESC, now-3600); ns = fake_ns_new(FLAV_MICRODESC, now-3600);
md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600); md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600);
r = consdiffmgr_add_consensus(md_ns_body, ns); r = consdiffmgr_add_consensus(md_ns_body, ns);
router_get_networkstatus_v3_sha3_as_signed(md_ns_sha3, md_ns_body); router_get_networkstatus_v3_sha3_as_signed(md_ns_sha3, md_ns_body,
strlen(md_ns_body));
networkstatus_vote_free(ns); networkstatus_vote_free(ns);
tt_int_op(r, OP_EQ, 0); tt_int_op(r, OP_EQ, 0);
@ -414,7 +435,7 @@ test_consdiffmgr_make_diffs(void *arg)
r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size); r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
tt_int_op(r, OP_EQ, 0); tt_int_op(r, OP_EQ, 0);
diff_text = tor_memdup_nulterm(diff_body, diff_size); diff_text = tor_memdup_nulterm(diff_body, diff_size);
applied = consensus_diff_apply(md_ns_body, diff_text); applied = consensus_diff_apply_(md_ns_body, diff_text);
tt_assert(applied); tt_assert(applied);
tt_str_op(applied, OP_EQ, md_ns_body_2); tt_str_op(applied, OP_EQ, md_ns_body_2);

View File

@ -94,6 +94,23 @@
#define NS_MODULE dir #define NS_MODULE dir
static networkstatus_t *
networkstatus_parse_vote_from_string_(const char *s,
const char **eos_out,
enum networkstatus_type_t ns_type)
{
size_t len = strlen(s);
// memdup so that it won't be nul-terminated.
char *tmp = tor_memdup(s, len);
networkstatus_t *result =
networkstatus_parse_vote_from_string(tmp, len, eos_out, ns_type);
if (eos_out && *eos_out) {
*eos_out = s + (*eos_out - tmp);
}
tor_free(tmp);
return result;
}
static void static void
test_dir_nicknames(void *arg) test_dir_nicknames(void *arg)
{ {
@ -2806,11 +2823,17 @@ test_a_networkstatus(
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
/* Parse certificates and keys. */ /* Parse certificates and keys. */
cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); cert1 = mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
tt_assert(cert1); tt_assert(cert1);
cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL); cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2,
strlen(AUTHORITY_CERT_2),
NULL);
tt_assert(cert2); tt_assert(cert2);
cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL); cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3,
strlen(AUTHORITY_CERT_3),
NULL);
tt_assert(cert3); tt_assert(cert3);
sign_skey_1 = crypto_pk_new(); sign_skey_1 = crypto_pk_new();
sign_skey_2 = crypto_pk_new(); sign_skey_2 = crypto_pk_new();
@ -2912,7 +2935,7 @@ test_a_networkstatus(
sign_skey_leg1, sign_skey_leg1,
FLAV_NS); FLAV_NS);
tt_assert(consensus_text); tt_assert(consensus_text);
con = networkstatus_parse_vote_from_string(consensus_text, NULL, con = networkstatus_parse_vote_from_string_(consensus_text, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
tt_assert(con); tt_assert(con);
//log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n", //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
@ -2924,7 +2947,7 @@ test_a_networkstatus(
sign_skey_leg1, sign_skey_leg1,
FLAV_MICRODESC); FLAV_MICRODESC);
tt_assert(consensus_text_md); tt_assert(consensus_text_md);
con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL, con_md = networkstatus_parse_vote_from_string_(consensus_text_md, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
tt_assert(con_md); tt_assert(con_md);
tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC); tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
@ -3023,13 +3046,13 @@ test_a_networkstatus(
tt_assert(consensus_text3); tt_assert(consensus_text3);
tt_assert(consensus_text_md2); tt_assert(consensus_text_md2);
tt_assert(consensus_text_md3); tt_assert(consensus_text_md3);
con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL, con2 = networkstatus_parse_vote_from_string_(consensus_text2, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL, con3 = networkstatus_parse_vote_from_string_(consensus_text3, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL, con_md2 = networkstatus_parse_vote_from_string_(consensus_text_md2, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL, con_md3 = networkstatus_parse_vote_from_string_(consensus_text_md3, NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
tt_assert(con2); tt_assert(con2);
tt_assert(con3); tt_assert(con3);
@ -6044,9 +6067,10 @@ test_dir_assumed_flags(void *arg)
"192.168.0.1 9001 0\n" "192.168.0.1 9001 0\n"
"m thisoneislongerbecauseitisa256bitmddigest33\n" "m thisoneislongerbecauseitisa256bitmddigest33\n"
"s Fast Guard Stable\n"; "s Fast Guard Stable\n";
const char *eos = str1 + strlen(str1);
const char *cp = str1; const char *cp = str1;
rs = routerstatus_parse_entry_from_string(area, &cp, tokens, NULL, NULL, rs = routerstatus_parse_entry_from_string(area, &cp, eos, tokens, NULL, NULL,
24, FLAV_MICRODESC); 24, FLAV_MICRODESC);
tt_assert(rs); tt_assert(rs);
tt_assert(rs->is_flagged_running); tt_assert(rs->is_flagged_running);

View File

@ -42,14 +42,20 @@ dir_common_authority_pk_init(authority_cert_t **cert1,
{ {
/* Parse certificates and keys. */ /* Parse certificates and keys. */
authority_cert_t *cert; authority_cert_t *cert;
cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
tt_assert(cert); tt_assert(cert);
tt_assert(cert->identity_key); tt_assert(cert->identity_key);
*cert1 = cert; *cert1 = cert;
tt_assert(*cert1); tt_assert(*cert1);
*cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL); *cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2,
strlen(AUTHORITY_CERT_2),
NULL);
tt_assert(*cert2); tt_assert(*cert2);
*cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL); *cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3,
strlen(AUTHORITY_CERT_3),
NULL);
tt_assert(*cert3); tt_assert(*cert3);
*sign_skey_1 = crypto_pk_new(); *sign_skey_1 = crypto_pk_new();
*sign_skey_2 = crypto_pk_new(); *sign_skey_2 = crypto_pk_new();
@ -266,7 +272,9 @@ dir_common_add_rs_and_parse(networkstatus_t *vote, networkstatus_t **vote_out,
/* dump the vote and try to parse it. */ /* dump the vote and try to parse it. */
v_text = format_networkstatus_vote(sign_skey, vote); v_text = format_networkstatus_vote(sign_skey, vote);
tt_assert(v_text); tt_assert(v_text);
*vote_out = networkstatus_parse_vote_from_string(v_text, NULL, NS_TYPE_VOTE); *vote_out = networkstatus_parse_vote_from_string(v_text,
strlen(v_text),
NULL, NS_TYPE_VOTE);
done: done:
if (v_text) if (v_text)
@ -424,4 +432,3 @@ dir_common_construct_vote_3(networkstatus_t **vote, authority_cert_t *cert,
return 0; return 0;
} }

View File

@ -72,6 +72,8 @@ ENABLE_GCC_WARNING(overlength-strings)
#define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \ #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
"Consensus not signed by sufficient number of requested authorities\r\n\r\n" "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
#define consdiffmgr_add_consensus consdiffmgr_add_consensus_nulterm
static dir_connection_t * static dir_connection_t *
new_dir_conn(void) new_dir_conn(void)
{ {
@ -1275,7 +1277,9 @@ test_dir_handle_get_server_keys_authority(void* data)
size_t body_used = 0; size_t body_used = 0;
(void) data; (void) data;
mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL); mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
strlen(TEST_CERTIFICATE),
NULL);
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock); MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
@ -1425,7 +1429,9 @@ test_dir_handle_get_server_keys_sk(void* data)
size_t body_used = 0; size_t body_used = 0;
(void) data; (void) data;
mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL); mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
strlen(TEST_CERTIFICATE),
NULL);
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock); MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
@ -2393,7 +2399,9 @@ test_dir_handle_get_status_vote_next_authority(void* data)
routerlist_free_all(); routerlist_free_all();
dirvote_free_all(); dirvote_free_all();
mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL); mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
strlen(TEST_CERTIFICATE),
NULL);
/* create a trusted ds */ /* create a trusted ds */
ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest, ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
@ -2471,7 +2479,9 @@ test_dir_handle_get_status_vote_current_authority(void* data)
routerlist_free_all(); routerlist_free_all();
dirvote_free_all(); dirvote_free_all();
mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL); mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
strlen(TEST_CERTIFICATE),
NULL);
/* create a trusted ds */ /* create a trusted ds */
ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest, ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,

View File

@ -265,7 +265,9 @@ test_router_pick_directory_server_impl(void *arg)
/* Init SR subsystem. */ /* Init SR subsystem. */
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
sr_init(0); sr_init(0);
UNMOCK(get_my_v3_authority_cert); UNMOCK(get_my_v3_authority_cert);
@ -275,7 +277,9 @@ test_router_pick_directory_server_impl(void *arg)
construct_consensus(&consensus_text_md, now); construct_consensus(&consensus_text_md, now);
tt_assert(consensus_text_md); tt_assert(consensus_text_md);
con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL, con_md = networkstatus_parse_vote_from_string(consensus_text_md,
strlen(consensus_text_md),
NULL,
NS_TYPE_CONSENSUS); NS_TYPE_CONSENSUS);
tt_assert(con_md); tt_assert(con_md);
tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC); tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
@ -475,7 +479,9 @@ test_directory_guard_fetch_with_no_dirinfo(void *arg)
/* Initialize the SRV subsystem */ /* Initialize the SRV subsystem */
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
sr_init(0); sr_init(0);
UNMOCK(get_my_v3_authority_cert); UNMOCK(get_my_v3_authority_cert);
@ -648,7 +654,9 @@ test_skew_common(void *arg, time_t now, unsigned long *offset)
/* Initialize the SRV subsystem */ /* Initialize the SRV subsystem */
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
sr_init(0); sr_init(0);
UNMOCK(get_my_v3_authority_cert); UNMOCK(get_my_v3_authority_cert);
@ -662,7 +670,8 @@ test_skew_common(void *arg, time_t now, unsigned long *offset)
MOCK(clock_skew_warning, mock_clock_skew_warning); MOCK(clock_skew_warning, mock_clock_skew_warning);
/* Caller will call teardown_capture_of_logs() */ /* Caller will call teardown_capture_of_logs() */
setup_capture_of_logs(LOG_WARN); setup_capture_of_logs(LOG_WARN);
retval = networkstatus_set_current_consensus(consensus, "microdesc", 0, retval = networkstatus_set_current_consensus(consensus, strlen(consensus),
"microdesc", 0,
NULL); NULL);
done: done:

View File

@ -65,7 +65,9 @@ init_authority_state(void)
MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m); MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
or_options_t *options = get_options_mutable(); or_options_t *options = get_options_mutable();
mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
tt_assert(mock_cert); tt_assert(mock_cert);
options->AuthoritativeDir = 1; options->AuthoritativeDir = 1;
tt_int_op(load_ed_keys(options, time(NULL)), OP_GE, 0); tt_int_op(load_ed_keys(options, time(NULL)), OP_GE, 0);
@ -421,7 +423,9 @@ test_sr_commit(void *arg)
{ /* Setup a minimal dirauth environment for this test */ { /* Setup a minimal dirauth environment for this test */
or_options_t *options = get_options_mutable(); or_options_t *options = get_options_mutable();
auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
tt_assert(auth_cert); tt_assert(auth_cert);
options->AuthoritativeDir = 1; options->AuthoritativeDir = 1;
@ -824,7 +828,9 @@ test_sr_setup_commits(void)
{ /* Setup a minimal dirauth environment for this test */ { /* Setup a minimal dirauth environment for this test */
or_options_t *options = get_options_mutable(); or_options_t *options = get_options_mutable();
auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL); auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1,
strlen(AUTHORITY_CERT_1),
NULL);
tt_assert(auth_cert); tt_assert(auth_cert);
options->AuthoritativeDir = 1; options->AuthoritativeDir = 1;