Introduce full coverage tests for module routerset.c.

This is using the paradigm introduced for test_status.c.
This commit is contained in:
dana koch 2014-08-27 20:41:25 +10:00 committed by Nick Mathewson
parent cc3b04a8c1
commit c887e20e6a
15 changed files with 2213 additions and 78 deletions

View File

@ -28,8 +28,8 @@
/** Allocate and return an empty smartlist.
*/
smartlist_t *
smartlist_new(void)
MOCK_IMPL(smartlist_t *,
smartlist_new,(void))
{
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
sl->num_used = 0;
@ -41,8 +41,8 @@ smartlist_new(void)
/** Deallocate a smartlist. Does not release storage associated with the
* list's elements.
*/
void
smartlist_free(smartlist_t *sl)
MOCK_IMPL(void,
smartlist_free,(smartlist_t *sl))
{
if (!sl)
return;
@ -1062,8 +1062,8 @@ HT_GENERATE(digestmap_impl, digestmap_entry_t, node, digestmap_entry_hash,
/** Constructor to create a new empty map from strings to void*'s.
*/
strmap_t *
strmap_new(void)
MOCK_IMPL(strmap_t *,
strmap_new,(void))
{
strmap_t *result;
result = tor_malloc(sizeof(strmap_t));
@ -1073,8 +1073,8 @@ strmap_new(void)
/** Constructor to create a new empty map from digests to void*'s.
*/
digestmap_t *
digestmap_new(void)
MOCK_IMPL(digestmap_t *,
digestmap_new,(void))
{
digestmap_t *result;
result = tor_malloc(sizeof(digestmap_t));
@ -1427,8 +1427,8 @@ digestmap_iter_done(digestmap_iter_t *iter)
* entries. If free_val is provided, it is invoked on every value in
* <b>map</b>.
*/
void
strmap_free(strmap_t *map, void (*free_val)(void*))
MOCK_IMPL(void,
strmap_free,(strmap_t *map, void (*free_val)(void*)))
{
strmap_entry_t **ent, **next, *this;
if (!map)
@ -1451,8 +1451,8 @@ strmap_free(strmap_t *map, void (*free_val)(void*))
* entries. If free_val is provided, it is invoked on every value in
* <b>map</b>.
*/
void
digestmap_free(digestmap_t *map, void (*free_val)(void*))
MOCK_IMPL(void,
digestmap_free, (digestmap_t *map, void (*free_val)(void*)))
{
digestmap_entry_t **ent, **next, *this;
if (!map)

View File

@ -27,8 +27,8 @@ typedef struct smartlist_t {
/** @} */
} smartlist_t;
smartlist_t *smartlist_new(void);
void smartlist_free(smartlist_t *sl);
MOCK_DECL(smartlist_t *, smartlist_new, (void));
MOCK_DECL(void, smartlist_free, (smartlist_t *sl));
void smartlist_clear(smartlist_t *sl);
void smartlist_add(smartlist_t *sl, void *element);
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
@ -328,11 +328,11 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join,
#define DECLARE_MAP_FNS(maptype, keytype, prefix) \
typedef struct maptype maptype; \
typedef struct prefix##entry_t *prefix##iter_t; \
maptype* prefix##new(void); \
MOCK_DECL(maptype*, prefix##new, (void)); \
void* prefix##set(maptype *map, keytype key, void *val); \
void* prefix##get(const maptype *map, keytype key); \
void* prefix##remove(maptype *map, keytype key); \
void prefix##free(maptype *map, void (*free_val)(void*)); \
MOCK_DECL(void, prefix##free, (maptype *map, void (*free_val)(void*))); \
int prefix##isempty(const maptype *map); \
int prefix##size(const maptype *map); \
prefix##iter_t *prefix##iter_init(maptype *map); \

View File

@ -58,8 +58,8 @@ static char geoip6_digest[DIGEST_LEN];
/** Return the index of the <b>country</b>'s entry in the GeoIP
* country list if it is a valid 2-letter country code, otherwise
* return -1. */
country_t
geoip_get_country(const char *country)
MOCK_IMPL(country_t,
geoip_get_country,(const char *country))
{
void *idxplus1_;
intptr_t idx;
@ -396,8 +396,8 @@ geoip_get_country_by_ipv6(const struct in6_addr *addr)
* the 'unknown country'. The return value will always be less than
* geoip_get_n_countries(). To decode it, call geoip_get_country_name().
*/
int
geoip_get_country_by_addr(const tor_addr_t *addr)
MOCK_IMPL(int,
geoip_get_country_by_addr,(const tor_addr_t *addr))
{
if (tor_addr_family(addr) == AF_INET) {
return geoip_get_country_by_ipv4(tor_addr_to_ipv4h(addr));
@ -409,8 +409,8 @@ geoip_get_country_by_addr(const tor_addr_t *addr)
}
/** Return the number of countries recognized by the GeoIP country list. */
int
geoip_get_n_countries(void)
MOCK_IMPL(int,
geoip_get_n_countries,(void))
{
if (!geoip_countries)
init_geoip_countries();
@ -430,8 +430,8 @@ geoip_get_country_name(country_t num)
}
/** Return true iff we have loaded a GeoIP database.*/
int
geoip_is_loaded(sa_family_t family)
MOCK_IMPL(int,
geoip_is_loaded,(sa_family_t family))
{
tor_assert(family == AF_INET || family == AF_INET6);
if (geoip_countries == NULL)

View File

@ -21,12 +21,12 @@ STATIC int geoip_get_country_by_ipv6(const struct in6_addr *addr);
#endif
int should_record_bridge_info(const or_options_t *options);
int geoip_load_file(sa_family_t family, const char *filename);
int geoip_get_country_by_addr(const tor_addr_t *addr);
int geoip_get_n_countries(void);
MOCK_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
MOCK_DECL(int, geoip_get_n_countries, (void));
const char *geoip_get_country_name(country_t num);
int geoip_is_loaded(sa_family_t family);
MOCK_DECL(int, geoip_is_loaded, (sa_family_t family));
const char *geoip_db_digest(sa_family_t family);
country_t geoip_get_country(const char *countrycode);
MOCK_DECL(country_t, geoip_get_country, (const char *countrycode));
void geoip_note_client_seen(geoip_client_action_t action,
const tor_addr_t *addr, const char *transport_name,

View File

@ -474,8 +474,8 @@ nodelist_assert_ok(void)
/** Return a list of a node_t * for every node we know about. The caller
* MUST NOT modify the list. (You can set and clear flags in the nodes if
* you must, but you must not add or remove nodes.) */
smartlist_t *
nodelist_get_list(void)
MOCK_IMPL(smartlist_t *,
nodelist_get_list,(void))
{
init_nodelist();
return the_nodelist->nodes;
@ -517,8 +517,8 @@ node_get_by_hex_id(const char *hex_id)
* the corresponding node_t, or NULL if none exists. Warn the user if
* <b>warn_if_unnamed</b> is set, and they have specified a router by
* nickname, but the Named flag isn't set for that router. */
const node_t *
node_get_by_nickname(const char *nickname, int warn_if_unnamed)
MOCK_IMPL(const node_t *,
node_get_by_nickname,(const char *nickname, int warn_if_unnamed))
{
const node_t *node;
if (!the_nodelist)

View File

@ -31,7 +31,8 @@ smartlist_t *nodelist_find_nodes_with_microdesc(const microdesc_t *md);
void nodelist_free_all(void);
void nodelist_assert_ok(void);
const node_t *node_get_by_nickname(const char *nickname, int warn_if_unnamed);
MOCK_DECL(const node_t *, node_get_by_nickname,
(const char *nickname, int warn_if_unnamed));
void node_get_verbose_nickname(const node_t *node,
char *verbose_name_out);
void node_get_verbose_nickname_by_id(const char *id_digest,
@ -60,7 +61,7 @@ void node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out);
void node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out);
int node_has_curve25519_onion_key(const node_t *node);
smartlist_t *nodelist_get_list(void);
MOCK_DECL(smartlist_t *, nodelist_get_list, (void));
/* Temporary during transition to multiple addresses. */
void node_get_addr(const node_t *node, tor_addr_t *addr_out);

View File

@ -769,9 +769,9 @@ compare_unknown_tor_addr_to_addr_policy(uint16_t port,
* We could do better by assuming that some ranges never match typical
* addresses (127.0.0.1, and so on). But we'll try this for now.
*/
addr_policy_result_t
compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
const smartlist_t *policy)
MOCK_IMPL(addr_policy_result_t,
compare_tor_addr_to_addr_policy,(const tor_addr_t *addr, uint16_t port,
const smartlist_t *policy))
{
if (!policy) {
/* no policy? accept all. */

View File

@ -37,8 +37,8 @@ int policies_parse_from_options(const or_options_t *options);
addr_policy_t *addr_policy_get_canonical_entry(addr_policy_t *ent);
int cmp_addr_policies(smartlist_t *a, smartlist_t *b);
addr_policy_result_t compare_tor_addr_to_addr_policy(const tor_addr_t *addr,
uint16_t port, const smartlist_t *policy);
MOCK_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
(const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr,
uint16_t port, const node_t *node);

View File

@ -3243,8 +3243,8 @@ networkstatus_parse_detached_signatures(const char *s, const char *eos)
* AF_UNSPEC for '*'. Use policy_expand_unspec() to turn this into a pair
* of AF_INET and AF_INET6 items.
*/
addr_policy_t *
router_parse_addr_policy_item_from_string(const char *s, int assume_action)
MOCK_IMPL(addr_policy_t *,
router_parse_addr_policy_item_from_string,(const char *s, int assume_action))
{
directory_token_t *tok = NULL;
const char *cp, *eos;

View File

@ -37,8 +37,8 @@ routerinfo_t *router_parse_entry_from_string(const char *s, const char *end,
const char *prepend_annotations);
extrainfo_t *extrainfo_parse_entry_from_string(const char *s, const char *end,
int cache_copy, struct digest_ri_map_t *routermap);
addr_policy_t *router_parse_addr_policy_item_from_string(const char *s,
int assume_action);
MOCK_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
(const char *s, int assume_action));
version_status_t tor_version_is_obsolete(const char *myversion,
const char *versionlist);
int tor_version_supports_microdescriptors(const char *platform);

View File

@ -4,6 +4,8 @@
* Copyright (c) 2007-2013, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define ROUTERSET_PRIVATE
#include "or.h"
#include "geoip.h"
#include "nodelist.h"
@ -12,39 +14,6 @@
#include "routerparse.h"
#include "routerset.h"
/** A routerset specifies constraints on a set of possible routerinfos, based
* on their names, identities, or addresses. It is optimized for determining
* whether a router is a member or not, in O(1+P) time, where P is the number
* of address policy constraints. */
struct routerset_t {
/** A list of strings for the elements of the policy. Each string is either
* a nickname, a hexadecimal identity fingerprint, or an address policy. A
* router belongs to the set if its nickname OR its identity OR its address
* matches an entry here. */
smartlist_t *list;
/** A map from lowercase nicknames of routers in the set to (void*)1 */
strmap_t *names;
/** A map from identity digests routers in the set to (void*)1 */
digestmap_t *digests;
/** An address policy for routers in the set. For implementation reasons,
* a router belongs to the set if it is _rejected_ by this policy. */
smartlist_t *policies;
/** A human-readable description of what this routerset is for. Used in
* log messages. */
char *description;
/** A list of the country codes in this set. */
smartlist_t *country_names;
/** Total number of countries we knew about when we built <b>countries</b>.*/
int n_countries;
/** Bit array mapping the return value of geoip_get_country() to 1 iff the
* country is a member of this routerset. Note that we MUST call
* routerset_refresh_countries() whenever the geoip country list is
* reloaded. */
bitarray_t *countries;
};
/** Return a new empty routerset. */
routerset_t *
routerset_new(void)
@ -60,7 +29,7 @@ routerset_new(void)
/** If <b>c</b> is a country code in the form {cc}, return a newly allocated
* string holding the "cc" part. Else, return NULL. */
static char *
STATIC char *
routerset_get_countryname(const char *c)
{
char *country;
@ -200,7 +169,7 @@ routerset_is_empty(const routerset_t *set)
*
* (If country is -1, then we take the country
* from addr.) */
static int
STATIC int
routerset_contains(const routerset_t *set, const tor_addr_t *addr,
uint16_t orport,
const char *nickname, const char *id_digest,

View File

@ -39,5 +39,45 @@ char *routerset_to_string(const routerset_t *routerset);
int routerset_equal(const routerset_t *old, const routerset_t *new);
void routerset_free(routerset_t *routerset);
#ifdef ROUTERSET_PRIVATE
STATIC char * routerset_get_countryname(const char *c);
STATIC int routerset_contains(const routerset_t *set, const tor_addr_t *addr,
uint16_t orport,
const char *nickname, const char *id_digest,
country_t country);
/** A routerset specifies constraints on a set of possible routerinfos, based
* on their names, identities, or addresses. It is optimized for determining
* whether a router is a member or not, in O(1+P) time, where P is the number
* of address policy constraints. */
struct routerset_t {
/** A list of strings for the elements of the policy. Each string is either
* a nickname, a hexadecimal identity fingerprint, or an address policy. A
* router belongs to the set if its nickname OR its identity OR its address
* matches an entry here. */
smartlist_t *list;
/** A map from lowercase nicknames of routers in the set to (void*)1 */
strmap_t *names;
/** A map from identity digests routers in the set to (void*)1 */
digestmap_t *digests;
/** An address policy for routers in the set. For implementation reasons,
* a router belongs to the set if it is _rejected_ by this policy. */
smartlist_t *policies;
/** A human-readable description of what this routerset is for. Used in
* log messages. */
char *description;
/** A list of the country codes in this set. */
smartlist_t *country_names;
/** Total number of countries we knew about when we built <b>countries</b>.*/
int n_countries;
/** Bit array mapping the return value of geoip_get_country() to 1 iff the
* country is a member of this routerset. Note that we MUST call
* routerset_refresh_countries() whenever the geoip country list is
* reloaded. */
bitarray_t *countries;
};
#endif
#endif

View File

@ -46,6 +46,7 @@ src_test_test_SOURCES = \
src/test/test_nodelist.c \
src/test/test_policy.c \
src/test/test_status.c \
src/test/test_routerset.c \
src/ext/tinytest.c
src_test_test_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)

View File

@ -1324,6 +1324,7 @@ extern struct testcase_t routerkeys_tests[];
extern struct testcase_t oom_tests[];
extern struct testcase_t policy_tests[];
extern struct testcase_t status_tests[];
extern struct testcase_t routerset_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@ -1355,6 +1356,7 @@ static struct testgroup_t testgroups[] = {
{ "oom/", oom_tests },
{ "policy/" , policy_tests },
{ "status/" , status_tests },
{ "routerset/" , routerset_tests },
END_OF_GROUPS
};

2122
src/test/test_routerset.c Normal file

File diff suppressed because it is too large Load Diff