remove some randomness from the unit tests

This commit is contained in:
arvidn 2015-08-13 02:06:30 -04:00
parent 411be1c017
commit 24b20a59cd
5 changed files with 88 additions and 52 deletions

View File

@ -79,6 +79,12 @@ namespace lt = libtorrent;
#endif #endif
boost::uint32_t g_addr = 0x92343023; boost::uint32_t g_addr = 0x92343023;
void init_rand_address()
{
g_addr = 0x92343023;
}
address rand_v4() address rand_v4()
{ {
do { do {

View File

@ -51,6 +51,7 @@ EXPORT void save_file(char const* filename, char const* data, int size);
EXPORT void report_failure(char const* err, char const* file, int line); EXPORT void report_failure(char const* err, char const* file, int line);
EXPORT void init_rand_address();
EXPORT libtorrent::address rand_v4(); EXPORT libtorrent::address rand_v4();
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
EXPORT libtorrent::address rand_v6(); EXPORT libtorrent::address rand_v6();

View File

@ -567,6 +567,8 @@ TORRENT_TEST(dht)
fprintf(stderr, " invalid announce response: %s\n", error_string); fprintf(stderr, " invalid announce response: %s\n", error_string);
} }
init_rand_address();
// announce from 100 random IPs and make sure scrape works // announce from 100 random IPs and make sure scrape works
// 50 downloaders and 50 seeds // 50 downloaders and 50 seeds
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
@ -753,9 +755,11 @@ TORRENT_TEST(dht)
// ====== put ====== // ====== put ======
init_rand_address();
udp::endpoint eps[1000]; udp::endpoint eps[1000];
for (int i = 0; i < 1000; ++i) for (int i = 0; i < 1000; ++i)
eps[i] = udp::endpoint(rand_v4(), (rand() % 16534) + 1); eps[i] = udp::endpoint(rand_v4(), (rand() % 16534) + 1);
announce_item items[] = announce_item items[] =
@ -1329,6 +1333,8 @@ TORRENT_TEST(dht)
s.restrict_routing_ips = false; s.restrict_routing_ips = false;
init_rand_address();
add_and_replace(tmp, diff); add_and_replace(tmp, diff);
table.node_seen(id, udp::endpoint(rand_v4(), rand()), 10); table.node_seen(id, udp::endpoint(rand_v4(), rand()), 10);

View File

@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp" #include "libtorrent/socket.hpp"
#include "libtorrent/random.hpp" #include "libtorrent/random.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/broadcast_socket.hpp" // for supports_ipv6()
#include "setup_transfer.hpp" // for rand_v4 #include "setup_transfer.hpp" // for rand_v4
using namespace libtorrent; using namespace libtorrent;
@ -58,6 +60,8 @@ bool cast_vote(ip_voter& ipv, address ext_ip, address voter)
// we don't flap // we don't flap
TORRENT_TEST(test_random) TORRENT_TEST(test_random)
{ {
init_rand_address();
ip_voter ipv; ip_voter ipv;
address_v4 addr1(address_v4::from_string("51.41.61.132")); address_v4 addr1(address_v4::from_string("51.41.61.132"));
@ -75,6 +79,8 @@ TORRENT_TEST(test_random)
TORRENT_TEST(two_ips) TORRENT_TEST(two_ips)
{ {
init_rand_address();
ip_voter ipv; ip_voter ipv;
address_v4 addr1(address_v4::from_string("51.1.1.1")); address_v4 addr1(address_v4::from_string("51.1.1.1"));
@ -100,6 +106,8 @@ TORRENT_TEST(two_ips)
TORRENT_TEST(one_ip) TORRENT_TEST(one_ip)
{ {
init_rand_address();
ip_voter ipv; ip_voter ipv;
address_v4 start_addr(address_v4::from_string("93.12.63.174")); address_v4 start_addr(address_v4::from_string("93.12.63.174"));
@ -140,3 +148,69 @@ TORRENT_TEST(one_ip)
TEST_CHECK(ipv.external_address() == addr1); TEST_CHECK(ipv.external_address() == addr1);
} }
TORRENT_TEST(externa_ip_1)
{
init_rand_address();
// test external ip voting
external_ip ipv1;
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 malicious responses from the same peer
error_code ec;
address real_external = address_v4::from_string("5.5.5.5", ec);
TEST_CHECK(!ec);
address malicious = address_v4::from_string("4.4.4.4", ec);
TEST_CHECK(!ec);
for (int i = 0; i < 50; ++i)
{
ipv1.cast_vote(real_external, aux::session_impl::source_dht, rand_v4());
ipv1.cast_vote(rand_v4(), aux::session_impl::source_dht, malicious);
}
TEST_CHECK(ipv1.external_address(rand_v4()) == real_external);
}
TORRENT_TEST(externa_ip_2)
{
init_rand_address();
external_ip ipv2;
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 consistent malicious responses from the same peer
error_code ec;
address malicious = address_v4::from_string("4.4.4.4", ec);
TEST_CHECK(!ec);
address real_external1 = address_v4::from_string("5.5.5.5", ec);
TEST_CHECK(!ec);
address real_external2;
#if TORRENT_USE_IPV6
if (supports_ipv6())
{
real_external2 = address_v6::from_string("2f80::", ec);
TEST_CHECK(!ec);
}
#endif
malicious = address_v4::from_string("4.4.4.4", ec);
TEST_CHECK(!ec);
address malicious_external = address_v4::from_string("3.3.3.3", ec);
TEST_CHECK(!ec);
for (int i = 0; i < 50; ++i)
{
ipv2.cast_vote(real_external1, aux::session_impl::source_dht, rand_v4());
#if TORRENT_USE_IPV6
if (supports_ipv6())
ipv2.cast_vote(real_external2, aux::session_impl::source_dht, rand_v6());
#endif
ipv2.cast_vote(malicious_external, aux::session_impl::source_dht, malicious);
}
TEST_CHECK(ipv2.external_address(rand_v4()) == real_external1);
#if TORRENT_USE_IPV6
if (supports_ipv6())
TEST_CHECK(ipv2.external_address(rand_v6()) == real_external2);
#endif
}

View File

@ -112,57 +112,6 @@ TORRENT_TEST(primitives)
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
// test external ip voting
external_ip ipv1;
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 malicious responses from the same peer
address real_external = address_v4::from_string("5.5.5.5", ec);
TEST_CHECK(!ec);
address malicious = address_v4::from_string("4.4.4.4", ec);
TEST_CHECK(!ec);
for (int i = 0; i < 50; ++i)
{
ipv1.cast_vote(real_external, aux::session_impl::source_dht, rand_v4());
ipv1.cast_vote(rand_v4(), aux::session_impl::source_dht, malicious);
}
TEST_CHECK(ipv1.external_address(rand_v4()) == real_external);
external_ip ipv2;
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 consistent malicious responses from the same peer
address real_external1 = address_v4::from_string("5.5.5.5", ec);
TEST_CHECK(!ec);
address real_external2;
#if TORRENT_USE_IPV6
if (supports_ipv6())
{
real_external2 = address_v6::from_string("2f80::", ec);
TEST_CHECK(!ec);
}
#endif
malicious = address_v4::from_string("4.4.4.4", ec);
TEST_CHECK(!ec);
address malicious_external = address_v4::from_string("3.3.3.3", ec);
TEST_CHECK(!ec);
for (int i = 0; i < 50; ++i)
{
ipv2.cast_vote(real_external1, aux::session_impl::source_dht, rand_v4());
#if TORRENT_USE_IPV6
if (supports_ipv6())
ipv2.cast_vote(real_external2, aux::session_impl::source_dht, rand_v6());
#endif
ipv2.cast_vote(malicious_external, aux::session_impl::source_dht, malicious);
}
TEST_CHECK(ipv2.external_address(rand_v4()) == real_external1);
#if TORRENT_USE_IPV6
if (supports_ipv6())
TEST_CHECK(ipv2.external_address(rand_v6()) == real_external2);
#endif
// test error codes // test error codes
TEST_CHECK(error_code(errors::http_error).message() == "HTTP error"); TEST_CHECK(error_code(errors::http_error).message() == "HTTP error");
TEST_CHECK(error_code(errors::missing_file_sizes).message() == "missing or invalid 'file sizes' entry"); TEST_CHECK(error_code(errors::missing_file_sizes).message() == "missing or invalid 'file sizes' entry");