Add a test for router_get_advertised_or_port_by_af().

This commit is contained in:
Nick Mathewson 2020-03-19 09:36:47 -04:00 committed by teor
parent 6ffe073db7
commit 1ba79d4567
No known key found for this signature in database
GPG Key ID: 10FEAA0E7075672A

View File

@ -7,9 +7,12 @@
* \brief Unittests for code in router.c
**/
#define CONFIG_PRIVATE
#define CONNECTION_PRIVATE
#include "core/or/or.h"
#include "app/config/config.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/connection.h"
#include "feature/hibernate/hibernate.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist.h"
@ -17,6 +20,9 @@
#include "feature/stats/rephist.h"
#include "lib/crypt_ops/crypto_curve25519.h"
#include "lib/crypt_ops/crypto_ed25519.h"
#include "lib/encoding/confline.h"
#include "core/or/listener_connection_st.h"
/* Test suite stuff */
#include "test/test.h"
@ -231,11 +237,59 @@ test_router_check_descriptor_bandwidth_changed(void *arg)
UNMOCK(we_are_hibernating);
}
static smartlist_t *fake_connection_array = NULL;
static smartlist_t *
mock_get_connection_array(void)
{
return fake_connection_array;
}
static void
test_router_get_advertised_or_port(void *arg)
{
(void)arg;
// uint16_t port;
int r, w=0, n=0;
char *msg=NULL;
or_options_t *opts = options_new();
listener_connection_t *listener = NULL;
// Set up a couple of configured ports.
config_line_append(&opts->ORPort_lines, "ORPort", "[1234::5678]:9999");
config_line_append(&opts->ORPort_lines, "ORPort", "5.6.7.8:auto");
r = parse_ports(opts, 0, &msg, &n, &w);
tt_assert(r == 0);
// There are no listeners, so the "auto" case will turn up no results.
tt_int_op(0, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET));
// This will return the matching value from the configured port.
tt_int_op(9999, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET6));
// Now set up a dummy listener.
MOCK(get_connection_array, mock_get_connection_array);
fake_connection_array = smartlist_new();
listener = listener_connection_new(CONN_TYPE_OR_LISTENER, AF_INET);
TO_CONN(listener)->port = 54321;
smartlist_add(fake_connection_array, TO_CONN(listener));
// We should get a port this time.
tt_int_op(54321, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET));
done:
or_options_free(opts);
config_free_all();
smartlist_free(fake_connection_array);
connection_free_minimal(TO_CONN(listener));
UNMOCK(get_connection_array);
}
#define ROUTER_TEST(name, flags) \
{ #name, test_router_ ## name, flags, NULL, NULL }
struct testcase_t router_tests[] = {
ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
ROUTER_TEST(get_advertised_or_port, TT_FORK),
END_OF_TESTCASES
};