merged test from RC_0_16
This commit is contained in:
parent
6bc908fd49
commit
971b979dbc
|
@ -32,6 +32,7 @@ project
|
|||
<conditional>@libtorrent_dependency
|
||||
<source>main.cpp
|
||||
<source>setup_transfer.cpp
|
||||
<source>dht_server.cpp
|
||||
: default-build
|
||||
<threading>multi
|
||||
<invariant-checks>full
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
|
||||
Copyright (c) 2013, 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 "libtorrent/thread.hpp"
|
||||
#include "libtorrent/bencode.hpp"
|
||||
#include "libtorrent/entry.hpp"
|
||||
#include "libtorrent/address.hpp"
|
||||
#include "libtorrent/io_service.hpp"
|
||||
#include "libtorrent/error_code.hpp"
|
||||
#include "libtorrent/socket.hpp"
|
||||
|
||||
#include <boost/detail/atomic_count.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
struct dht_server
|
||||
{
|
||||
|
||||
boost::asio::io_service m_ios;
|
||||
boost::detail::atomic_count m_dht_requests;
|
||||
udp::socket m_socket;
|
||||
int m_port;
|
||||
|
||||
boost::shared_ptr<libtorrent::thread> m_thread;
|
||||
|
||||
dht_server()
|
||||
: m_dht_requests(0)
|
||||
, m_socket(m_ios)
|
||||
{
|
||||
error_code ec;
|
||||
m_socket.open(udp::v4(), ec);
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error opening listen DHT socket: %s\n", ec.message().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
m_socket.bind(udp::endpoint(address_v4::any(), 0), ec);
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error binding DHT socket to port 0: %s\n", ec.message().c_str());
|
||||
return;
|
||||
}
|
||||
m_port = m_socket.local_endpoint(ec).port();
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error getting local endpoint of DHT socket: %s\n", ec.message().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "DHT initialized on port %d\n", m_port);
|
||||
|
||||
m_thread.reset(new thread(boost::bind(&dht_server::thread_fun, this)));
|
||||
}
|
||||
|
||||
~dht_server()
|
||||
{
|
||||
m_socket.close();
|
||||
if (m_thread) m_thread->join();
|
||||
}
|
||||
|
||||
int port() const { return m_port; }
|
||||
|
||||
int num_hits() const { return m_dht_requests; }
|
||||
|
||||
void thread_fun()
|
||||
{
|
||||
char buffer[2000];
|
||||
|
||||
for (;;)
|
||||
{
|
||||
error_code ec;
|
||||
udp::endpoint from;
|
||||
int bytes_transferred = m_socket.receive_from(
|
||||
asio::buffer(buffer, sizeof(buffer)), from, 0, ec);
|
||||
if (ec == boost::asio::error::operation_aborted) return;
|
||||
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Error receiving on DHT socket: %s\n", ec.message().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
entry msg = bdecode(buffer, buffer + bytes_transferred);
|
||||
|
||||
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
|
||||
std::cerr << msg << std::endl;
|
||||
#endif
|
||||
++m_dht_requests;
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
fprintf(stderr, "failed to decode DHT message: %s\n", e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
boost::shared_ptr<dht_server> g_dht;
|
||||
|
||||
int start_dht()
|
||||
{
|
||||
g_dht.reset(new dht_server);
|
||||
return g_dht->port();
|
||||
}
|
||||
|
||||
// the number of DHT messages received
|
||||
int num_dht_hits()
|
||||
{
|
||||
if (g_dht) return g_dht->num_hits();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stop_dht()
|
||||
{
|
||||
g_dht.reset();
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
|
||||
Copyright (c) 2013, 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.
|
||||
|
||||
*/
|
||||
|
||||
// returns the port the DHT is running on
|
||||
int start_dht();
|
||||
|
||||
// the number of DHT messages received
|
||||
int num_dht_hits();
|
||||
|
||||
void stop_dht();
|
||||
|
|
@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#include "test.hpp"
|
||||
#include "setup_transfer.hpp"
|
||||
#include "dht_server.hpp"
|
||||
#include "libtorrent/alert.hpp"
|
||||
#include "libtorrent/alert_types.hpp"
|
||||
|
||||
|
@ -69,6 +70,7 @@ enum flags_t
|
|||
expect_udp_connection = 4,
|
||||
expect_http_reject = 8,
|
||||
expect_udp_reject = 16,
|
||||
expect_dht_msg = 32,
|
||||
};
|
||||
|
||||
void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
||||
|
@ -76,6 +78,7 @@ void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
|||
fprintf(stderr, "\n=== TEST == proxy: %s anonymous-mode: %s\n\n", proxy_name[proxy_type], (flags & anonymous_mode) ? "yes" : "no");
|
||||
int http_port = start_web_server();
|
||||
int udp_port = start_tracker();
|
||||
int dht_port = start_dht();
|
||||
|
||||
int prev_udp_announces = g_udp_tracker_requests;
|
||||
int prev_http_announces = g_http_tracker_requests;
|
||||
|
@ -100,6 +103,8 @@ void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
|||
ps.type = proxy_type;
|
||||
s->set_proxy(ps);
|
||||
|
||||
s->start_dht();
|
||||
|
||||
error_code ec;
|
||||
create_directory("tmp1_privacy", ec);
|
||||
std::ofstream file(combine_path("tmp1_privacy", "temporary").c_str());
|
||||
|
@ -119,9 +124,10 @@ void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
|||
addp.flags &= ~add_torrent_params::flag_auto_managed;
|
||||
addp.ti = t;
|
||||
addp.save_path = "tmp1_privacy";
|
||||
addp.dht_nodes.push_back(std::pair<std::string, int>("127.0.0.1", dht_port));
|
||||
torrent_handle h = s->add_torrent(addp);
|
||||
|
||||
//TODO: also add a peer and a DHT node to make sure we don't contact those as well
|
||||
//TODO: also add a peer to make sure we don't contact that as well
|
||||
|
||||
rejected_trackers.clear();
|
||||
for (int i = 0; i < 10; ++i)
|
||||
|
@ -133,6 +139,14 @@ void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
|||
// we should have announced to the tracker by now
|
||||
TEST_EQUAL(g_udp_tracker_requests, prev_udp_announces + bool(flags & expect_udp_connection));
|
||||
TEST_EQUAL(g_http_tracker_requests, prev_http_announces + bool(flags & expect_http_connection));
|
||||
if (flags & expect_dht_msg)
|
||||
{
|
||||
TEST_CHECK(num_dht_hits() > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
TEST_EQUAL(num_dht_hits(), 0);
|
||||
}
|
||||
|
||||
if (flags & expect_udp_reject)
|
||||
TEST_CHECK(std::find(rejected_trackers.begin(), rejected_trackers.end(), udp_tracker_url) != rejected_trackers.end());
|
||||
|
@ -143,6 +157,10 @@ void test_proxy(proxy_settings::proxy_type proxy_type, int flags)
|
|||
fprintf(stderr, "destructing session\n");
|
||||
delete s;
|
||||
fprintf(stderr, "done\n");
|
||||
|
||||
stop_dht();
|
||||
stop_tracker();
|
||||
stop_web_server();
|
||||
}
|
||||
|
||||
int test_main()
|
||||
|
@ -150,13 +168,13 @@ int test_main()
|
|||
// not using anonymous mode
|
||||
// UDP fails open if we can't connect to the proxy
|
||||
// or if the proxy doesn't support UDP
|
||||
test_proxy(proxy_settings::none, expect_udp_connection | expect_http_connection);
|
||||
test_proxy(proxy_settings::socks4, expect_udp_connection);
|
||||
test_proxy(proxy_settings::socks5, expect_udp_connection);
|
||||
test_proxy(proxy_settings::socks5_pw, expect_udp_connection);
|
||||
test_proxy(proxy_settings::http, expect_udp_connection);
|
||||
test_proxy(proxy_settings::http_pw, expect_udp_connection);
|
||||
test_proxy(proxy_settings::i2p_proxy, expect_udp_connection);
|
||||
test_proxy(proxy_settings::none, expect_udp_connection | expect_http_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::socks4, expect_udp_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::socks5, expect_udp_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::socks5_pw, expect_udp_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::http, expect_udp_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::http_pw, expect_udp_connection | expect_dht_msg);
|
||||
test_proxy(proxy_settings::i2p_proxy, expect_udp_connection | expect_dht_msg);
|
||||
|
||||
// using anonymous mode
|
||||
// TODO: also expect whether or not to get a anonymous_alert indicating the tracker isn't anonymous
|
||||
|
|
Loading…
Reference in New Issue