2015-05-08 06:28:24 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "libtorrent/ip_voter.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/random.hpp"
|
2015-05-30 20:28:09 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2017-10-09 01:21:19 +02:00
|
|
|
#include "libtorrent/aux_/session_interface.hpp"
|
2015-08-13 08:06:30 +02:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for supports_ipv6()
|
2015-05-08 06:28:24 +02:00
|
|
|
#include "setup_transfer.hpp" // for rand_v4
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-05-08 06:28:24 +02:00
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-05-30 20:28:09 +02:00
|
|
|
bool cast_vote(ip_voter& ipv, address ext_ip, address voter)
|
|
|
|
{
|
2017-10-10 15:50:49 +02:00
|
|
|
bool new_ip = ipv.cast_vote(ext_ip, aux::session_interface::source_dht, voter);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%15s -> %-15s\n"
|
2015-05-30 20:28:09 +02:00
|
|
|
, print_address(voter).c_str()
|
|
|
|
, print_address(ext_ip).c_str());
|
|
|
|
if (new_ip)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" \x1b[1mnew external IP: %s\x1b[0m\n"
|
2015-05-30 20:28:09 +02:00
|
|
|
, print_address(ipv.external_address()).c_str());
|
|
|
|
}
|
|
|
|
return new_ip;
|
|
|
|
}
|
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-08 06:28:24 +02:00
|
|
|
// test the case where every time we get a new IP. Make sure
|
|
|
|
// we don't flap
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(test_random)
|
2015-05-08 06:28:24 +02:00
|
|
|
{
|
2015-08-13 08:06:30 +02:00
|
|
|
init_rand_address();
|
|
|
|
|
2015-05-08 06:28:24 +02:00
|
|
|
ip_voter ipv;
|
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
address_v4 addr1(address_v4::from_string("51.41.61.132"));
|
|
|
|
|
|
|
|
bool new_ip = cast_vote(ipv, addr1, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(new_ip);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_CHECK(ipv.external_address() == addr1);
|
2015-05-08 06:28:24 +02:00
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
{
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, rand_v4(), rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
|
|
|
}
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_CHECK(ipv.external_address() == addr1);
|
2015-05-08 06:28:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(two_ips)
|
2015-05-08 06:28:24 +02:00
|
|
|
{
|
2015-08-13 08:06:30 +02:00
|
|
|
init_rand_address();
|
|
|
|
|
2015-05-08 06:28:24 +02:00
|
|
|
ip_voter ipv;
|
|
|
|
|
|
|
|
address_v4 addr1(address_v4::from_string("51.1.1.1"));
|
|
|
|
address_v4 addr2(address_v4::from_string("53.3.3.3"));
|
|
|
|
|
|
|
|
// addr1 is the first address we see, which is the one we pick. Even though
|
|
|
|
// we'll have as many votes for addr2, we shouldn't flap, since addr2 never
|
|
|
|
// gets an overwhelming majority.
|
2015-05-30 20:28:09 +02:00
|
|
|
bool new_ip = cast_vote(ipv, addr1, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(new_ip);
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
{
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, addr2, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, rand_v4(), rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, addr1, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
|
|
|
|
|
|
|
TEST_CHECK(ipv.external_address() == addr1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(one_ip)
|
2015-05-08 06:28:24 +02:00
|
|
|
{
|
2015-08-13 08:06:30 +02:00
|
|
|
init_rand_address();
|
|
|
|
|
2015-05-08 06:28:24 +02:00
|
|
|
ip_voter ipv;
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
address_v4 start_addr(address_v4::from_string("93.12.63.174"));
|
2015-05-08 06:28:24 +02:00
|
|
|
address_v4 addr1(address_v4::from_string("51.1.1.1"));
|
|
|
|
address_v4 addr2(address_v4::from_string("53.3.3.3"));
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
bool new_ip = cast_vote(ipv, start_addr, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(new_ip);
|
2015-05-30 20:28:09 +02:00
|
|
|
TEST_CHECK(ipv.external_address() != addr1);
|
2015-05-31 14:21:16 +02:00
|
|
|
TEST_CHECK(ipv.external_address() == start_addr);
|
2015-05-30 20:28:09 +02:00
|
|
|
for (int i = 0; i < 30; ++i)
|
|
|
|
{
|
|
|
|
new_ip = cast_vote(ipv, addr2, rand_v4());
|
|
|
|
if (new_ip) break;
|
|
|
|
new_ip = cast_vote(ipv, rand_v4(), rand_v4());
|
|
|
|
if (new_ip) break;
|
|
|
|
new_ip = cast_vote(ipv, addr1, rand_v4());
|
|
|
|
if (new_ip) break;
|
|
|
|
new_ip = cast_vote(ipv, addr1, rand_v4());
|
|
|
|
if (new_ip) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CHECK(ipv.external_address() == addr1);
|
|
|
|
|
|
|
|
for (int i = 0; i < 500; ++i)
|
2015-05-08 06:28:24 +02:00
|
|
|
{
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, addr2, rand_v4());
|
|
|
|
TEST_CHECK(!new_ip);
|
|
|
|
new_ip = cast_vote(ipv, rand_v4(), rand_v4());
|
|
|
|
TEST_CHECK(!new_ip);
|
|
|
|
new_ip = cast_vote(ipv, addr1, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
2015-05-30 20:28:09 +02:00
|
|
|
new_ip = cast_vote(ipv, addr1, rand_v4());
|
2015-05-08 06:28:24 +02:00
|
|
|
TEST_CHECK(!new_ip);
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:28:09 +02:00
|
|
|
TEST_CHECK(ipv.external_address() == addr1);
|
2015-05-08 06:28:24 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:58:51 +01:00
|
|
|
TORRENT_TEST(ip_voter_1)
|
2015-08-13 08:06:30 +02:00
|
|
|
{
|
|
|
|
init_rand_address();
|
|
|
|
|
|
|
|
// test external ip voting
|
2016-12-04 21:58:51 +01:00
|
|
|
ip_voter ipv1;
|
2015-08-13 08:06:30 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2017-10-09 01:21:19 +02:00
|
|
|
ipv1.cast_vote(real_external, aux::session_interface::source_dht, rand_v4());
|
|
|
|
ipv1.cast_vote(rand_v4(), aux::session_interface::source_dht, malicious);
|
2015-08-13 08:06:30 +02:00
|
|
|
}
|
2016-12-04 21:58:51 +01:00
|
|
|
TEST_CHECK(ipv1.external_address() == real_external);
|
2015-08-13 08:06:30 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:58:51 +01:00
|
|
|
TORRENT_TEST(ip_voter_2)
|
2015-08-13 08:06:30 +02:00
|
|
|
{
|
|
|
|
init_rand_address();
|
|
|
|
|
2016-12-04 21:58:51 +01:00
|
|
|
ip_voter ipv2,ipv6;
|
2015-08-13 08:06:30 +02:00
|
|
|
|
|
|
|
// 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);
|
2016-12-04 21:58:51 +01:00
|
|
|
address malicious_external = address_v4::from_string("3.3.3.3", ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
address malicious2;
|
2015-08-13 08:06:30 +02:00
|
|
|
address real_external2;
|
2016-12-04 21:58:51 +01:00
|
|
|
address malicious_external2;
|
2015-08-13 08:06:30 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (supports_ipv6())
|
|
|
|
{
|
2016-12-04 21:58:51 +01:00
|
|
|
malicious2 = address_v6::from_string("2f90::", ec);
|
|
|
|
TEST_CHECK(!ec);
|
2015-08-13 08:06:30 +02:00
|
|
|
real_external2 = address_v6::from_string("2f80::", ec);
|
|
|
|
TEST_CHECK(!ec);
|
2016-12-04 21:58:51 +01:00
|
|
|
malicious_external2 = address_v6::from_string("2f70::", ec);
|
|
|
|
TEST_CHECK(!ec);
|
2015-08-13 08:06:30 +02:00
|
|
|
}
|
|
|
|
#endif
|
2016-12-04 21:58:51 +01:00
|
|
|
|
2015-08-13 08:06:30 +02:00
|
|
|
for (int i = 0; i < 50; ++i)
|
|
|
|
{
|
2017-10-09 01:21:19 +02:00
|
|
|
ipv2.cast_vote(real_external1, aux::session_interface::source_dht, rand_v4());
|
2017-10-10 14:25:12 +02:00
|
|
|
ipv2.cast_vote(malicious_external, aux::session_interface::source_dht, malicious);
|
2015-08-13 08:06:30 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (supports_ipv6())
|
2016-12-04 21:58:51 +01:00
|
|
|
{
|
2017-10-10 14:25:12 +02:00
|
|
|
ipv6.cast_vote(real_external2, aux::session_interface::source_dht, rand_v6());
|
|
|
|
ipv6.cast_vote(malicious_external2, aux::session_interface::source_dht, malicious2);
|
2016-12-04 21:58:51 +01:00
|
|
|
}
|
2015-08-13 08:06:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
2016-12-04 21:58:51 +01:00
|
|
|
TEST_CHECK(ipv2.external_address() == real_external1);
|
2015-08-13 08:06:30 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (supports_ipv6())
|
2016-12-04 21:58:51 +01:00
|
|
|
TEST_CHECK(ipv6.external_address() == real_external2);
|
2015-08-13 08:06:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|