2013-06-12 09:57:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
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 "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
2013-06-12 19:03:56 +02:00
|
|
|
#include "dht_server.hpp"
|
2013-06-13 08:32:13 +02:00
|
|
|
#include "peer_server.hpp"
|
2014-02-23 02:32:55 +01:00
|
|
|
#include "udp_tracker.hpp"
|
2013-06-12 09:57:13 +02:00
|
|
|
#include "libtorrent/alert.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace lt = libtorrent;
|
2013-06-12 09:57:13 +02:00
|
|
|
|
|
|
|
char const* proxy_name[] = {
|
|
|
|
"none",
|
|
|
|
"socks4",
|
|
|
|
"socks5",
|
|
|
|
"socks5_pw",
|
|
|
|
"http",
|
|
|
|
"http_pw",
|
|
|
|
"i2p_proxy"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> rejected_trackers;
|
|
|
|
|
|
|
|
bool alert_predicate(libtorrent::alert* a)
|
|
|
|
{
|
|
|
|
anonymous_mode_alert* am = alert_cast<anonymous_mode_alert>(a);
|
|
|
|
if (am == NULL) return false;
|
|
|
|
|
|
|
|
if (am->kind == anonymous_mode_alert::tracker_not_anonymous)
|
|
|
|
rejected_trackers.push_back(am->str);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum flags_t
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
force_proxy_mode = 1,
|
2013-06-12 09:57:13 +02:00
|
|
|
expect_http_connection = 2,
|
|
|
|
expect_udp_connection = 4,
|
|
|
|
expect_http_reject = 8,
|
|
|
|
expect_udp_reject = 16,
|
2013-06-12 19:03:56 +02:00
|
|
|
expect_dht_msg = 32,
|
2013-06-13 08:32:13 +02:00
|
|
|
expect_peer_connection = 64,
|
2013-06-12 09:57:13 +02:00
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags)
|
2013-06-12 09:57:13 +02:00
|
|
|
{
|
2013-09-01 02:11:59 +02:00
|
|
|
#ifdef TORRENT_DISABLE_DHT
|
|
|
|
// if DHT is disabled, we won't get any requests to it
|
|
|
|
flags &= ~expect_dht_msg;
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
fprintf(stderr, "\n=== TEST == proxy: %s anonymous-mode: %s\n\n", proxy_name[proxy_type], (flags & force_proxy_mode) ? "yes" : "no");
|
2013-06-12 09:57:13 +02:00
|
|
|
int http_port = start_web_server();
|
2014-02-23 02:32:55 +01:00
|
|
|
int udp_port = start_udp_tracker();
|
2013-06-12 19:03:56 +02:00
|
|
|
int dht_port = start_dht();
|
2013-06-13 08:32:13 +02:00
|
|
|
int peer_port = start_peer();
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2014-02-23 02:32:55 +01:00
|
|
|
int prev_udp_announces = num_udp_announces();
|
2013-06-12 09:57:13 +02:00
|
|
|
|
|
|
|
int const alert_mask = alert::all_categories
|
|
|
|
& ~alert::progress_notification
|
|
|
|
& ~alert::stats_notification;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
settings_pack sett;
|
|
|
|
sett.set_int(settings_pack::stop_tracker_timeout, 2);
|
|
|
|
sett.set_int(settings_pack::tracker_completion_timeout, 2);
|
|
|
|
sett.set_int(settings_pack::tracker_receive_timeout, 2);
|
2014-10-03 22:56:57 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_int(settings_pack::half_open_limit, 2);
|
2014-10-03 22:56:57 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_bool(settings_pack::announce_to_all_trackers, true);
|
|
|
|
sett.set_bool(settings_pack::announce_to_all_tiers, true);
|
|
|
|
sett.set_bool(settings_pack::force_proxy, flags & force_proxy_mode);
|
|
|
|
sett.set_int(settings_pack::alert_mask, alert_mask);
|
2014-08-01 07:44:48 +02:00
|
|
|
sett.set_bool(settings_pack::enable_upnp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_natpmp, false);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// since multiple sessions may exist simultaneously (because of the
|
|
|
|
// pipelining of the tests) they actually need to use different ports
|
|
|
|
static int listen_port = 48875;
|
|
|
|
char iface[200];
|
|
|
|
snprintf(iface, sizeof(iface), "127.0.0.1:%d", listen_port++);
|
|
|
|
sett.set_str(settings_pack::listen_interfaces, iface);
|
|
|
|
sett.set_bool(settings_pack::enable_dht, true);
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
// if we don't do this, the peer connection test
|
|
|
|
// will be delayed by several seconds, by first
|
|
|
|
// trying uTP
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_bool(settings_pack::enable_outgoing_utp, false);
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2013-10-20 23:08:59 +02:00
|
|
|
// in non-anonymous mode we circumvent/ignore the proxy if it fails
|
|
|
|
// wheras in anonymous mode, we just fail
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_str(settings_pack::proxy_hostname, "non-existing.com");
|
|
|
|
sett.set_int(settings_pack::proxy_type, (settings_pack::proxy_type_t)proxy_type);
|
|
|
|
sett.set_int(settings_pack::proxy_port, 4444);
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
lt::session* s = new lt::session(sett, fingerprint("LT", 0, 1, 0, 0));
|
2013-06-12 19:03:56 +02:00
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
error_code ec;
|
2014-03-11 08:12:49 +01:00
|
|
|
remove_all("tmp1_privacy", ec);
|
2013-06-12 09:57:13 +02:00
|
|
|
create_directory("tmp1_privacy", ec);
|
|
|
|
std::ofstream file(combine_path("tmp1_privacy", "temporary").c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::shared_ptr<torrent_info> t = ::create_torrent(&file, 16 * 1024, 13, false);
|
2013-06-12 09:57:13 +02:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
char http_tracker_url[200];
|
|
|
|
snprintf(http_tracker_url, sizeof(http_tracker_url), "http://127.0.0.1:%d/announce", http_port);
|
|
|
|
t->add_tracker(http_tracker_url, 0);
|
|
|
|
|
|
|
|
char udp_tracker_url[200];
|
|
|
|
snprintf(udp_tracker_url, sizeof(udp_tracker_url), "udp://127.0.0.1:%d/announce", udp_port);
|
|
|
|
t->add_tracker(udp_tracker_url, 1);
|
|
|
|
|
|
|
|
add_torrent_params addp;
|
|
|
|
addp.flags &= ~add_torrent_params::flag_paused;
|
|
|
|
addp.flags &= ~add_torrent_params::flag_auto_managed;
|
2014-03-11 08:12:49 +01:00
|
|
|
|
|
|
|
// we don't want to waste time checking the torrent, just go straight into
|
|
|
|
// seeding it, announcing to trackers and connecting to peers
|
|
|
|
addp.flags |= add_torrent_params::flag_seed_mode;
|
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
addp.ti = t;
|
|
|
|
addp.save_path = "tmp1_privacy";
|
2013-06-12 19:03:56 +02:00
|
|
|
addp.dht_nodes.push_back(std::pair<std::string, int>("127.0.0.1", dht_port));
|
2013-06-12 09:57:13 +02:00
|
|
|
torrent_handle h = s->add_torrent(addp);
|
|
|
|
|
2013-06-19 21:14:46 +02:00
|
|
|
printf("connect_peer: 127.0.0.1:%d\n", peer_port);
|
2013-06-13 08:32:13 +02:00
|
|
|
h.connect_peer(tcp::endpoint(address_v4::from_string("127.0.0.1"), peer_port));
|
2013-06-12 09:57:13 +02:00
|
|
|
|
|
|
|
rejected_trackers.clear();
|
2013-10-14 10:15:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2013-10-20 23:08:59 +02:00
|
|
|
const int timeout = 100;
|
2013-10-14 10:15:08 +02:00
|
|
|
#else
|
2013-10-20 23:08:59 +02:00
|
|
|
const int timeout = 20;
|
2013-10-14 10:15:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i = 0; i < timeout; ++i)
|
2013-06-12 09:57:13 +02:00
|
|
|
{
|
|
|
|
print_alerts(*s, "s", false, false, false, &alert_predicate);
|
|
|
|
test_sleep(100);
|
2013-09-01 02:04:40 +02:00
|
|
|
|
2014-02-23 02:32:55 +01:00
|
|
|
if (num_udp_announces() >= prev_udp_announces + 1
|
2013-09-01 02:04:40 +02:00
|
|
|
&& num_peer_hits() > 0)
|
|
|
|
break;
|
2013-06-12 09:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// we should have announced to the tracker by now
|
2014-02-23 02:32:55 +01:00
|
|
|
TEST_EQUAL(num_udp_announces(), prev_udp_announces + bool(flags & expect_udp_connection));
|
2013-06-12 19:03:56 +02:00
|
|
|
if (flags & expect_dht_msg)
|
|
|
|
{
|
|
|
|
TEST_CHECK(num_dht_hits() > 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL(num_dht_hits(), 0);
|
|
|
|
}
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2013-06-13 08:32:13 +02:00
|
|
|
if (flags & expect_peer_connection)
|
|
|
|
{
|
|
|
|
TEST_CHECK(num_peer_hits() > 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL(num_peer_hits(), 0);
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
if (flags & expect_udp_reject)
|
|
|
|
TEST_CHECK(std::find(rejected_trackers.begin(), rejected_trackers.end(), udp_tracker_url) != rejected_trackers.end());
|
|
|
|
|
|
|
|
if (flags & expect_http_reject)
|
|
|
|
TEST_CHECK(std::find(rejected_trackers.begin(), rejected_trackers.end(), http_tracker_url) != rejected_trackers.end());
|
|
|
|
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: ~session\n", time_now_string());
|
2013-10-20 23:08:59 +02:00
|
|
|
session_proxy pr = s->abort();
|
2013-06-12 09:57:13 +02:00
|
|
|
delete s;
|
2013-06-12 19:03:56 +02:00
|
|
|
|
2013-06-13 08:32:13 +02:00
|
|
|
stop_peer();
|
2013-06-12 19:03:56 +02:00
|
|
|
stop_dht();
|
2014-02-23 02:32:55 +01:00
|
|
|
stop_udp_tracker();
|
2013-06-12 19:03:56 +02:00
|
|
|
stop_web_server();
|
2013-10-20 23:08:59 +02:00
|
|
|
return pr;
|
2013-06-12 09:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int test_main()
|
|
|
|
{
|
2013-10-20 23:08:59 +02:00
|
|
|
session_proxy pr[20];
|
2013-06-12 09:57:13 +02:00
|
|
|
// not using anonymous mode
|
|
|
|
// UDP fails open if we can't connect to the proxy
|
|
|
|
// or if the proxy doesn't support UDP
|
2014-07-06 21:18:00 +02:00
|
|
|
pr[0] = test_proxy(settings_pack::none, expect_udp_connection | expect_http_connection | expect_dht_msg | expect_peer_connection);
|
|
|
|
pr[1] = test_proxy(settings_pack::socks4, expect_udp_connection | expect_dht_msg);
|
|
|
|
pr[2] = test_proxy(settings_pack::socks5, expect_udp_connection | expect_dht_msg);
|
|
|
|
pr[3] = test_proxy(settings_pack::socks5_pw, expect_udp_connection | expect_dht_msg);
|
|
|
|
pr[4] = test_proxy(settings_pack::http, expect_udp_connection | expect_dht_msg);
|
|
|
|
pr[5] = test_proxy(settings_pack::http_pw, expect_udp_connection | expect_dht_msg);
|
|
|
|
pr[6] = test_proxy(settings_pack::i2p_proxy, expect_udp_connection | expect_dht_msg);
|
2013-06-12 09:57:13 +02:00
|
|
|
|
|
|
|
// using anonymous mode
|
2013-06-13 08:32:13 +02:00
|
|
|
|
|
|
|
// anonymous mode doesn't require a proxy when one isn't configured. It could be
|
|
|
|
// used with a VPN for instance. This will all changed in 1.0, where anonymous
|
|
|
|
// mode is separated from force_proxy
|
2014-07-06 21:18:00 +02:00
|
|
|
pr[7] = test_proxy(settings_pack::none, force_proxy_mode | expect_peer_connection);
|
|
|
|
pr[8] = test_proxy(settings_pack::socks4, force_proxy_mode | expect_udp_reject);
|
|
|
|
pr[9] = test_proxy(settings_pack::socks5, force_proxy_mode);
|
|
|
|
pr[10] = test_proxy(settings_pack::socks5_pw, force_proxy_mode);
|
|
|
|
pr[11] = test_proxy(settings_pack::http, force_proxy_mode | expect_udp_reject);
|
|
|
|
pr[12] = test_proxy(settings_pack::http_pw, force_proxy_mode | expect_udp_reject);
|
|
|
|
pr[13] = test_proxy(settings_pack::i2p_proxy, force_proxy_mode);
|
2013-06-12 09:57:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|