diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 8e67db981..4779171ed 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -79,6 +79,12 @@ namespace lt = libtorrent; #endif boost::uint32_t g_addr = 0x92343023; + +void init_rand_address() +{ + g_addr = 0x92343023; +} + address rand_v4() { do { diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index e8005091f..7bb850663 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -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 init_rand_address(); EXPORT libtorrent::address rand_v4(); #if TORRENT_USE_IPV6 EXPORT libtorrent::address rand_v6(); diff --git a/test/test_dht.cpp b/test/test_dht.cpp index cca52086d..912ff3243 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -567,6 +567,8 @@ TORRENT_TEST(dht) fprintf(stderr, " invalid announce response: %s\n", error_string); } + init_rand_address(); + // announce from 100 random IPs and make sure scrape works // 50 downloaders and 50 seeds for (int i = 0; i < 100; ++i) @@ -753,9 +755,11 @@ TORRENT_TEST(dht) // ====== put ====== + init_rand_address(); + 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); announce_item items[] = @@ -1329,6 +1333,8 @@ TORRENT_TEST(dht) s.restrict_routing_ips = false; + init_rand_address(); + add_and_replace(tmp, diff); table.node_seen(id, udp::endpoint(rand_v4(), rand()), 10); diff --git a/test/test_ip_voter.cpp b/test/test_ip_voter.cpp index b5f562e8c..a41efefbe 100644 --- a/test/test_ip_voter.cpp +++ b/test/test_ip_voter.cpp @@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/random.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 using namespace libtorrent; @@ -58,6 +60,8 @@ bool cast_vote(ip_voter& ipv, address ext_ip, address voter) // we don't flap TORRENT_TEST(test_random) { + init_rand_address(); + ip_voter ipv; address_v4 addr1(address_v4::from_string("51.41.61.132")); @@ -75,6 +79,8 @@ TORRENT_TEST(test_random) TORRENT_TEST(two_ips) { + init_rand_address(); + ip_voter ipv; address_v4 addr1(address_v4::from_string("51.1.1.1")); @@ -100,6 +106,8 @@ TORRENT_TEST(two_ips) TORRENT_TEST(one_ip) { + init_rand_address(); + ip_voter ipv; 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); } +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 + +} + diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index ea63602f3..0907926fe 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -112,57 +112,6 @@ TORRENT_TEST(primitives) } 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_CHECK(error_code(errors::http_error).message() == "HTTP error"); TEST_CHECK(error_code(errors::missing_file_sizes).message() == "missing or invalid 'file sizes' entry");