mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-28 06:13:31 +01:00
Add unit test for dirserv measured bandwidth cache
This commit is contained in:
parent
302d1dae6c
commit
6e978ab829
@ -85,18 +85,8 @@ static const signed_descriptor_t *get_signed_descriptor_by_fp(
|
||||
time_t publish_cutoff);
|
||||
static was_router_added_t dirserv_add_extrainfo(extrainfo_t *ei,
|
||||
const char **msg);
|
||||
static void dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
|
||||
time_t as_of);
|
||||
static void dirserv_clear_measured_bw_cache(void);
|
||||
static void dirserv_expire_measured_bw_cache(time_t now);
|
||||
static int dirserv_get_measured_bw_cache_size(void);
|
||||
static int dirserv_query_measured_bw_cache(const char *node_id, long *bw_out,
|
||||
time_t *as_of_out);
|
||||
static uint32_t dirserv_get_bandwidth_for_router(const routerinfo_t *ri);
|
||||
|
||||
/************** Measured Bandwidth parsing code ******/
|
||||
#define MAX_MEASUREMENT_AGE (3*24*60*60) /* 3 days */
|
||||
|
||||
/************** Fingerprint handling code ************/
|
||||
|
||||
#define FP_NAMED 1 /**< Listed in fingerprint file. */
|
||||
@ -2066,7 +2056,7 @@ static digestmap_t *mbw_cache = NULL;
|
||||
|
||||
/** Store a measured bandwidth cache entry when reading the measured
|
||||
* bandwidths file. */
|
||||
static void
|
||||
void
|
||||
dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
|
||||
time_t as_of)
|
||||
{
|
||||
@ -2096,7 +2086,7 @@ dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
|
||||
}
|
||||
|
||||
/** Clear and free the measured bandwidth cache */
|
||||
static void
|
||||
void
|
||||
dirserv_clear_measured_bw_cache(void)
|
||||
{
|
||||
if (mbw_cache) {
|
||||
@ -2107,7 +2097,7 @@ dirserv_clear_measured_bw_cache(void)
|
||||
}
|
||||
|
||||
/** Scan the measured bandwidth cache and remove expired entries */
|
||||
static void
|
||||
void
|
||||
dirserv_expire_measured_bw_cache(time_t now)
|
||||
{
|
||||
digestmap_iter_t *itr = NULL;
|
||||
@ -2152,7 +2142,7 @@ dirserv_expire_measured_bw_cache(time_t now)
|
||||
}
|
||||
|
||||
/** Get the current size of the measured bandwidth cache */
|
||||
static int
|
||||
int
|
||||
dirserv_get_measured_bw_cache_size(void)
|
||||
{
|
||||
if (mbw_cache) return digestmap_size(mbw_cache);
|
||||
@ -2161,7 +2151,7 @@ dirserv_get_measured_bw_cache_size(void)
|
||||
|
||||
/** Query the cache by identity digest, return value indicates whether
|
||||
* we found it. */
|
||||
static int
|
||||
int
|
||||
dirserv_query_measured_bw_cache(const char *node_id, long *bw_out,
|
||||
time_t *as_of_out)
|
||||
{
|
||||
|
@ -138,10 +138,22 @@ void cached_dir_decref(cached_dir_t *d);
|
||||
cached_dir_t *new_cached_dir(char *s, time_t published);
|
||||
|
||||
#ifdef DIRSERV_PRIVATE
|
||||
|
||||
/* Put the MAX_MEASUREMENT_AGE #define here so unit tests can see it */
|
||||
#define MAX_MEASUREMENT_AGE (3*24*60*60) /* 3 days */
|
||||
|
||||
int measured_bw_line_parse(measured_bw_line_t *out, const char *line);
|
||||
|
||||
int measured_bw_line_apply(measured_bw_line_t *parsed_line,
|
||||
smartlist_t *routerstatuses);
|
||||
|
||||
void dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
|
||||
time_t as_of);
|
||||
void dirserv_clear_measured_bw_cache(void);
|
||||
void dirserv_expire_measured_bw_cache(time_t now);
|
||||
int dirserv_get_measured_bw_cache_size(void);
|
||||
int dirserv_query_measured_bw_cache(const char *node_id, long *bw_out,
|
||||
time_t *as_of_out);
|
||||
#endif
|
||||
|
||||
int dirserv_read_measured_bandwidths(const char *from_file,
|
||||
|
@ -575,6 +575,83 @@ test_dir_measured_bw(void)
|
||||
return;
|
||||
}
|
||||
|
||||
#define MBWC_INIT_TIME 1000
|
||||
|
||||
/** Do the measured bandwidth cache unit test */
|
||||
static void
|
||||
test_dir_measured_bw_cache(void)
|
||||
{
|
||||
/* Initial fake time_t for testing */
|
||||
time_t curr = MBWC_INIT_TIME;
|
||||
/* Some measured_bw_line_ts */
|
||||
measured_bw_line_t mbwl[3];
|
||||
/* For receiving output on cache queries */
|
||||
long bw;
|
||||
time_t as_of;
|
||||
|
||||
/* First, clear the cache and assert that it's empty */
|
||||
dirserv_clear_measured_bw_cache();
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 0);
|
||||
/*
|
||||
* Set up test mbwls; none of the dirserv_cache_*() functions care about
|
||||
* the node_hex field.
|
||||
*/
|
||||
memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
|
||||
mbwl[0].bw = 20;
|
||||
memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
|
||||
mbwl[1].bw = 40;
|
||||
memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
|
||||
mbwl[2].bw = 80;
|
||||
/* Try caching something */
|
||||
dirserv_cache_measured_bw(&(mbwl[0]), curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 1);
|
||||
/* Okay, let's see if we can retrieve it */
|
||||
test_assert(dirserv_query_measured_bw_cache(mbwl[0].node_id, &bw, &as_of));
|
||||
test_eq(bw, 20);
|
||||
test_eq(as_of, MBWC_INIT_TIME);
|
||||
/* Try retrieving it without some outputs */
|
||||
test_assert(dirserv_query_measured_bw_cache(mbwl[0].node_id, NULL, NULL));
|
||||
test_assert(dirserv_query_measured_bw_cache(mbwl[0].node_id, &bw, NULL));
|
||||
test_eq(bw, 20);
|
||||
test_assert(dirserv_query_measured_bw_cache(mbwl[0].node_id, NULL, &as_of));
|
||||
test_eq(as_of, MBWC_INIT_TIME);
|
||||
/* Now expire it */
|
||||
curr += MAX_MEASUREMENT_AGE + 1;
|
||||
dirserv_expire_measured_bw_cache(curr);
|
||||
/* Check that the cache is empty */
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 0);
|
||||
/* Check that we can't retrieve it */
|
||||
test_assert(!dirserv_query_measured_bw_cache(mbwl[0].node_id, NULL, NULL));
|
||||
/* Try caching a few things now */
|
||||
dirserv_cache_measured_bw(&(mbwl[0]), curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 1);
|
||||
curr += MAX_MEASUREMENT_AGE / 4;
|
||||
dirserv_cache_measured_bw(&(mbwl[1]), curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 2);
|
||||
curr += MAX_MEASUREMENT_AGE / 4;
|
||||
dirserv_cache_measured_bw(&(mbwl[2]), curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 3);
|
||||
curr += MAX_MEASUREMENT_AGE / 4 + 1;
|
||||
/* Do an expire that's too soon to get any of them */
|
||||
dirserv_expire_measured_bw_cache(curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 3);
|
||||
/* Push the oldest one off the cliff */
|
||||
curr += MAX_MEASUREMENT_AGE / 4;
|
||||
dirserv_expire_measured_bw_cache(curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 2);
|
||||
/* And another... */
|
||||
curr += MAX_MEASUREMENT_AGE / 4;
|
||||
dirserv_expire_measured_bw_cache(curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 1);
|
||||
/* This should empty it out again */
|
||||
curr += MAX_MEASUREMENT_AGE / 4;
|
||||
dirserv_expire_measured_bw_cache(curr);
|
||||
test_eq(dirserv_get_measured_bw_cache_size(), 0);
|
||||
|
||||
done:
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
test_dir_param_voting(void)
|
||||
{
|
||||
@ -2141,6 +2218,7 @@ struct testcase_t dir_tests[] = {
|
||||
DIR(scale_bw),
|
||||
DIR_LEGACY(clip_unmeasured_bw),
|
||||
DIR_LEGACY(clip_unmeasured_bw_alt),
|
||||
DIR_LEGACY(measured_bw_cache),
|
||||
END_OF_TESTCASES
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user