test: Add unittest for descriptors with BridgeDistribution option.

This commit is contained in:
Isis Lovecruft 2017-10-23 19:44:06 +00:00 committed by Nick Mathewson
parent b0e10f23ba
commit 02cde0d939
6 changed files with 176 additions and 2 deletions

View File

@ -654,7 +654,6 @@ static int parse_ports(or_options_t *options, int validate_only,
static int check_server_ports(const smartlist_t *ports,
const or_options_t *options,
int *num_low_ports_out);
static int check_bridge_distribution_setting(const char *bd);
static int validate_data_directory(or_options_t *options);
static int write_configuration_file(const char *fname,
const or_options_t *options);
@ -6370,7 +6369,7 @@ warn_client_dns_cache(const char *option, int disabling)
* returned. If the option string contains an invalid character, -1 is
* returned.
**/
static int
STATIC int
check_bridge_distribution_setting(const char *bd)
{
if (bd == NULL)

View File

@ -199,6 +199,7 @@ STATIC int parse_port_config(smartlist_t *out,
const char *defaultaddr,
int defaultport,
const unsigned flags);
STATIC int check_bridge_distribution_setting(const char *bd);
#endif
#endif

View File

@ -115,6 +115,7 @@ src_test_test_SOURCES = \
src/test/test_relaycell.c \
src/test/test_rendcache.c \
src/test/test_replay.c \
src/test/test_router.c \
src/test/test_routerkeys.c \
src/test/test_routerlist.c \
src/test/test_routerset.c \

View File

@ -1220,6 +1220,7 @@ struct testgroup_t testgroups[] = {
{ "relaycell/", relaycell_tests },
{ "rend_cache/", rend_cache_tests },
{ "replaycache/", replaycache_tests },
{ "router/", router_tests },
{ "routerkeys/", routerkeys_tests },
{ "routerlist/", routerlist_tests },
{ "routerset/" , routerset_tests },

View File

@ -4890,6 +4890,65 @@ test_config_parse_port_config__ports__server_options(void *data)
config_free_lines(config_port_valid); config_port_valid = NULL;
}
/* If we're not configured to be a bridge, but we set
* BridgeDistribution, then options_validate () should return -1. */
static void
test_config_check_bridge_distribution_setting_not_a_bridge(void *arg) {
or_options_t* options = get_options_mutable();
or_options_t* old_options = options;
or_options_t* default_options = options;
char* message = (char*)("");
int ret;
(void)arg;
options->BridgeRelay = 0;
options->BridgeDistribution = (char*)("https");
ret = options_validate(old_options, options, default_options, 0, &message);
tt_int_op(ret, OP_EQ, -1);
done:
return;
}
/* If the BridgeDistribution setting was valid, 0 should be returned. */
static void
test_config_check_bridge_distribution_setting_valid(void *arg) {
int ret = check_bridge_distribution_setting("https");
(void)arg;
tt_int_op(ret, OP_EQ, 0);
done:
return;
}
/* If the BridgeDistribution setting was invalid, -1 should be returned. */
static void
test_config_check_bridge_distribution_setting_invalid(void *arg) {
int ret = check_bridge_distribution_setting("hyphens-are-not-allowed");
(void)arg;
tt_int_op(ret, OP_EQ, -1);
done:
return;
}
/* If the BridgeDistribution setting was unrecognised, a warning should be
* logged and 0 should be returned. */
static void
test_config_check_bridge_distribution_setting_unrecognised(void *arg) {
int ret = check_bridge_distribution_setting("unicorn");
(void)arg;
tt_int_op(ret, OP_EQ, 0);
done:
return;
}
#define CONFIG_TEST(name, flags) \
{ #name, test_config_ ## name, flags, NULL, NULL }
@ -4916,6 +4975,10 @@ struct testcase_t config_tests[] = {
CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
CONFIG_TEST(parse_port_config__ports__server_options, 0),
CONFIG_TEST(parse_port_config__ports__ports_given, 0),
CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
END_OF_TESTCASES
};

109
src/test/test_router.c Normal file
View File

@ -0,0 +1,109 @@
/* Copyright (c) 2017, The Tor Project, Inc. */
/* Copyright (c) 2017, isis agora lovecruft */
/* See LICENSE for licensing information */
/**
* \file test_router.c
* \brief Unittests for code in src/or/router.c
**/
#include "or.h"
#include "config.h"
#include "crypto_curve25519.h"
#include "crypto_ed25519.h"
#include "router.h"
#include "routerlist.h"
/* Test suite stuff */
#include "test.h"
NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
static routerinfo_t* mock_routerinfo;
static const routerinfo_t*
NS(router_get_my_routerinfo)(void) {
crypto_pk_t* ident_key;
crypto_pk_t* tap_key;
time_t now;
if (!mock_routerinfo) {
/* Mock the published timestamp, otherwise router_dump_router_to_string()
* will poop its pants. */
time(&now);
/* We'll need keys, or router_dump_router_to_string() would return NULL. */
ident_key = pk_generate(0);
tap_key = pk_generate(0);
tor_assert(ident_key != NULL);
tor_assert(tap_key != NULL);
mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
mock_routerinfo->nickname = tor_strdup("ConlonNancarrow");
mock_routerinfo->addr = 123456789;
mock_routerinfo->or_port = 443;
mock_routerinfo->platform = tor_strdup("unittest");
mock_routerinfo->cache_info.published_on = now;
mock_routerinfo->identity_pkey = crypto_pk_dup_key(ident_key);
mock_routerinfo->onion_pkey = crypto_pk_dup_key(tap_key);
mock_routerinfo->bandwidthrate = 9001;
mock_routerinfo->bandwidthburst = 9002;
}
return mock_routerinfo;
}
/* If no distribution option was set, then check_bridge_distribution_setting()
* should have set it to "any". */
static void
test_router_dump_router_to_string_no_bridge_distribution_method(void *arg) {
const char* needle = "bridge-distribution-request any";
or_options_t* options = get_options_mutable();
routerinfo_t* router = NULL;
curve25519_keypair_t ntor_keypair;
ed25519_keypair_t signing_keypair;
char* desc = NULL;
char* found = NULL;
(void)arg;
NS_MOCK(router_get_my_routerinfo);
options->ORPort_set = 1;
options->BridgeRelay = 1;
/* Generate keys which router_dump_router_to_string() expects to exist. */
tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
/* Set up part of our routerinfo_t so that we don't trigger any other
* assertions in router_dump_router_to_string(). */
router = (routerinfo_t*)router_get_my_routerinfo();
tt_ptr_op(router, !=, NULL);
router->onion_curve25519_pkey = &ntor_keypair.pubkey;
/* Generate our server descriptor and ensure that the substring
* "bridge-distribution-request any" occurs somewhere within it. */
desc = router_dump_router_to_string(router,
router->identity_pkey,
router->onion_pkey,
&ntor_keypair,
&signing_keypair);
tt_ptr_op(desc, !=, NULL);
found = strstr(desc, needle);
tt_ptr_op(found, !=, NULL);
done:
NS_UNMOCK(router_get_my_routerinfo);
tor_free(desc);
}
#define ROUTER_TEST(name, flags) \
{ #name, test_router_ ## name, flags, NULL, NULL }
struct testcase_t router_tests[] = {
ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
END_OF_TESTCASES
};