2015-09-10 05:57:14 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2010, 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 "settings.hpp"
|
|
|
|
#include "setup_swarm.hpp"
|
|
|
|
#include "simulator/simulator.hpp"
|
|
|
|
#include "simulator/http_server.hpp"
|
2016-03-27 03:02:49 +02:00
|
|
|
#include "simulator/utils.hpp"
|
2015-09-10 05:57:14 +02:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2015-11-25 05:38:19 +01:00
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2015-12-10 07:56:33 +01:00
|
|
|
#include "libtorrent/session.hpp"
|
2016-04-07 06:06:04 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
|
|
|
#include "libtorrent/file_storage.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace sim;
|
|
|
|
namespace lt = libtorrent;
|
|
|
|
|
2015-10-27 05:21:07 +01:00
|
|
|
using chrono::duration_cast;
|
|
|
|
|
2015-09-10 05:57:14 +02:00
|
|
|
// seconds
|
|
|
|
const int duration = 10000;
|
|
|
|
|
|
|
|
void test_interval(int interval)
|
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim::default_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
lt::time_point start = lt::clock_type::now();
|
|
|
|
|
|
|
|
sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
// listen on port 8080
|
|
|
|
sim::http_server http(web_server, 8080);
|
|
|
|
|
|
|
|
// the timestamps (in seconds) of all announces
|
|
|
|
std::vector<int> announces;
|
|
|
|
|
|
|
|
http.register_handler("/announce"
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&announces,interval,start](std::string /* method */
|
|
|
|
, std::string /* req */
|
|
|
|
, std::map<std::string, std::string>&)
|
2015-09-10 05:57:14 +02:00
|
|
|
{
|
2016-06-19 01:24:27 +02:00
|
|
|
std::uint32_t const seconds = std::uint32_t(chrono::duration_cast<lt::seconds>(
|
|
|
|
lt::clock_type::now() - start).count());
|
2015-09-10 05:57:14 +02:00
|
|
|
announces.push_back(seconds);
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
|
2015-09-10 05:57:14 +02:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
});
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
std::vector<int> announce_alerts;
|
|
|
|
|
|
|
|
lt::settings_pack default_settings = settings();
|
|
|
|
lt::add_torrent_params default_add_torrent;
|
|
|
|
|
|
|
|
setup_swarm(1, swarm_test::upload, sim, default_settings, default_add_torrent
|
|
|
|
// add session
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](lt::settings_pack&) {}
|
2015-12-10 07:56:33 +01:00
|
|
|
// add torrent
|
|
|
|
, [](lt::add_torrent_params& params) {
|
|
|
|
params.trackers.push_back("http://2.2.2.2:8080/announce");
|
|
|
|
}
|
|
|
|
// on alert
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&](lt::alert const* a, lt::session&) {
|
2015-12-10 07:56:33 +01:00
|
|
|
|
|
|
|
if (lt::alert_cast<lt::tracker_announce_alert>(a))
|
|
|
|
{
|
2016-06-19 01:24:27 +02:00
|
|
|
std::uint32_t const seconds = std::uint32_t(chrono::duration_cast<lt::seconds>(
|
|
|
|
a->timestamp() - start).count());
|
2015-12-10 07:56:33 +01:00
|
|
|
|
|
|
|
announce_alerts.push_back(seconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// terminate
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](int const ticks, lt::session&) -> bool { return ticks > duration; });
|
2015-12-10 07:56:33 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(announce_alerts.size(), announces.size());
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
for (int i = 0; i < int(announces.size()); ++i)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(announces[i], counter);
|
2015-12-10 07:56:33 +01:00
|
|
|
TEST_EQUAL(announce_alerts[i], counter);
|
2015-09-10 05:57:14 +02:00
|
|
|
counter += interval;
|
|
|
|
if (counter > duration + 1) counter = duration + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:38:19 +01:00
|
|
|
TORRENT_TEST(event_completed)
|
2015-09-10 05:57:14 +02:00
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim::default_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
// listen on port 8080
|
|
|
|
sim::http_server http(web_server, 8080);
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
// the request strings of all announces
|
|
|
|
std::vector<std::pair<int, std::string>> announces;
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
const int interval = 500;
|
2015-12-10 07:56:33 +01:00
|
|
|
lt::time_point start = lt::clock_type::now();
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
http.register_handler("/announce"
|
2015-12-10 07:56:33 +01:00
|
|
|
, [&](std::string method, std::string req
|
2015-11-01 18:26:22 +01:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-09-10 05:57:14 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
2016-06-19 01:24:27 +02:00
|
|
|
int const timestamp = int(chrono::duration_cast<lt::seconds>(
|
|
|
|
lt::clock_type::now() - start).count());
|
2015-12-10 07:56:33 +01:00
|
|
|
announces.push_back({timestamp, req});
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
|
2015-09-10 05:57:14 +02:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
});
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
lt::settings_pack default_settings = settings();
|
|
|
|
lt::add_torrent_params default_add_torrent;
|
|
|
|
|
|
|
|
int completion = -1;
|
|
|
|
|
|
|
|
setup_swarm(2, swarm_test::download, sim, default_settings, default_add_torrent
|
|
|
|
// add session
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](lt::settings_pack&) { }
|
2015-12-10 07:56:33 +01:00
|
|
|
// add torrent
|
|
|
|
, [](lt::add_torrent_params& params) {
|
|
|
|
params.trackers.push_back("http://2.2.2.2:8080/announce");
|
|
|
|
}
|
|
|
|
// on alert
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&](lt::alert const*, lt::session&) {}
|
2015-12-10 07:56:33 +01:00
|
|
|
// terminate
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&](int const ticks, lt::session& ses) -> bool
|
2015-12-10 07:56:33 +01:00
|
|
|
{
|
|
|
|
if (completion == -1 && is_seed(ses))
|
|
|
|
{
|
2016-06-19 01:24:27 +02:00
|
|
|
completion = int(chrono::duration_cast<lt::seconds>(
|
|
|
|
lt::clock_type::now() - start).count());
|
2015-12-10 07:56:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ticks > duration;
|
|
|
|
});
|
2015-09-10 05:57:14 +02:00
|
|
|
|
|
|
|
// the first announce should be event=started, the second should be
|
|
|
|
// event=completed, then all but the last should have no event and the last
|
|
|
|
// be event=stopped.
|
|
|
|
for (int i = 0; i < int(announces.size()); ++i)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
std::string const& str = announces[i].second;
|
|
|
|
int timestamp = announces[i].first;
|
|
|
|
|
2015-09-10 05:57:14 +02:00
|
|
|
const bool has_start = str.find("&event=started")
|
|
|
|
!= std::string::npos;
|
|
|
|
const bool has_completed = str.find("&event=completed")
|
|
|
|
!= std::string::npos;
|
|
|
|
const bool has_stopped = str.find("&event=stopped")
|
|
|
|
!= std::string::npos;
|
|
|
|
|
|
|
|
// we there can only be one event
|
|
|
|
const bool has_event = str.find("&event=") != std::string::npos;
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("- %s\n", str.c_str());
|
2015-09-10 05:57:14 +02:00
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
// there is exactly 0 or 1 events.
|
2015-09-10 05:57:14 +02:00
|
|
|
TEST_EQUAL(int(has_start) + int(has_completed) + int(has_stopped)
|
|
|
|
, int(has_event));
|
|
|
|
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
TEST_CHECK(has_start);
|
|
|
|
break;
|
|
|
|
case 1:
|
2015-12-10 07:56:33 +01:00
|
|
|
{
|
|
|
|
// the announce should have come approximately the same time we
|
|
|
|
// completed
|
|
|
|
TEST_CHECK(abs(completion - timestamp) <= 1);
|
2015-09-10 05:57:14 +02:00
|
|
|
TEST_CHECK(has_completed);
|
|
|
|
break;
|
2015-12-10 07:56:33 +01:00
|
|
|
}
|
2015-09-10 05:57:14 +02:00
|
|
|
default:
|
|
|
|
if (i == int(announces.size()) - 1)
|
|
|
|
{
|
|
|
|
TEST_CHECK(has_stopped);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_CHECK(!has_event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(announce_interval_440)
|
|
|
|
{
|
|
|
|
test_interval(440);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(announce_interval_1800)
|
|
|
|
{
|
|
|
|
test_interval(1800);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(announce_interval_1200)
|
|
|
|
{
|
|
|
|
test_interval(3600);
|
|
|
|
}
|
|
|
|
|
2015-10-27 05:21:07 +01:00
|
|
|
struct sim_config : sim::default_config
|
|
|
|
{
|
|
|
|
chrono::high_resolution_clock::duration hostname_lookup(
|
|
|
|
asio::ip::address const& requestor
|
|
|
|
, std::string hostname
|
|
|
|
, std::vector<asio::ip::address>& result
|
2016-07-10 02:10:38 +02:00
|
|
|
, boost::system::error_code& ec) override
|
2015-10-27 05:21:07 +01:00
|
|
|
{
|
|
|
|
if (hostname == "tracker.com")
|
|
|
|
{
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.2"));
|
|
|
|
result.push_back(address_v6::from_string("ff::dead:beef"));
|
|
|
|
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_config::hostname_lookup(requestor, hostname, result, ec);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void on_alert_notify(lt::session* ses)
|
|
|
|
{
|
2015-11-29 08:06:36 +01:00
|
|
|
ses->get_io_service().post([ses] {
|
|
|
|
std::vector<lt::alert*> alerts;
|
|
|
|
ses->pop_alerts(&alerts);
|
2015-10-27 05:21:07 +01:00
|
|
|
|
2015-11-29 08:06:36 +01:00
|
|
|
for (lt::alert* a : alerts)
|
|
|
|
{
|
|
|
|
lt::time_duration d = a->timestamp().time_since_epoch();
|
2016-06-19 01:24:27 +02:00
|
|
|
std::uint32_t const millis = std::uint32_t(
|
|
|
|
lt::duration_cast<lt::milliseconds>(d).count());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%4d.%03d: %s\n", millis / 1000, millis % 1000,
|
2015-11-29 08:06:36 +01:00
|
|
|
a->message().c_str());
|
|
|
|
}
|
|
|
|
});
|
2015-10-27 05:21:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// this test makes sure that a tracker whose host name resolves to both IPv6 and
|
|
|
|
// IPv4 addresses will be announced to twice, once for each address family
|
|
|
|
TORRENT_TEST(ipv6_support)
|
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
sim::asio::io_service web_server_v4(sim, address_v4::from_string("10.0.0.2"));
|
|
|
|
sim::asio::io_service web_server_v6(sim, address_v6::from_string("ff::dead:beef"));
|
|
|
|
|
|
|
|
// listen on port 8080
|
|
|
|
sim::http_server http_v4(web_server_v4, 8080);
|
|
|
|
sim::http_server http_v6(web_server_v6, 8080);
|
|
|
|
|
|
|
|
int v4_announces = 0;
|
|
|
|
int v6_announces = 0;
|
|
|
|
|
|
|
|
http_v4.register_handler("/announce"
|
2015-11-07 20:57:25 +01:00
|
|
|
, [&v4_announces](std::string method, std::string req
|
2015-11-01 18:26:22 +01:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-10-27 05:21:07 +01:00
|
|
|
{
|
|
|
|
++v4_announces;
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
|
2015-10-27 05:21:07 +01:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
});
|
|
|
|
|
|
|
|
http_v6.register_handler("/announce"
|
2015-11-07 20:57:25 +01:00
|
|
|
, [&v6_announces](std::string method, std::string req
|
2015-11-01 18:26:22 +01:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-10-27 05:21:07 +01:00
|
|
|
{
|
|
|
|
++v6_announces;
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
|
2015-10-27 05:21:07 +01:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
lt::session_proxy zombie;
|
|
|
|
|
|
|
|
asio::io_service ios(sim, { address_v4::from_string("10.0.0.3")
|
|
|
|
, address_v6::from_string("ffff::1337") });
|
|
|
|
lt::settings_pack sett = settings();
|
|
|
|
std::unique_ptr<lt::session> ses(new lt::session(sett, ios));
|
|
|
|
|
|
|
|
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
|
|
|
|
|
|
|
|
lt::add_torrent_params p;
|
|
|
|
p.name = "test-torrent";
|
|
|
|
p.save_path = ".";
|
|
|
|
p.info_hash.assign("abababababababababab");
|
|
|
|
|
|
|
|
//TODO: parameterize http vs. udp here
|
|
|
|
p.trackers.push_back("http://tracker.com:8080/announce");
|
|
|
|
ses->async_add_torrent(p);
|
|
|
|
|
|
|
|
// stop the torrent 5 seconds in
|
2016-03-27 03:02:49 +02:00
|
|
|
sim::timer t1(sim, lt::seconds(5)
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&ses](boost::system::error_code const&)
|
2015-10-27 05:21:07 +01:00
|
|
|
{
|
|
|
|
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
|
|
|
|
for (auto const& t : torrents)
|
|
|
|
{
|
|
|
|
t.pause();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// then shut down 10 seconds in
|
2016-03-27 03:02:49 +02:00
|
|
|
sim::timer t2(sim, lt::seconds(10)
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&ses,&zombie](boost::system::error_code const&)
|
2015-10-27 05:21:07 +01:00
|
|
|
{
|
|
|
|
zombie = ses->abort();
|
|
|
|
ses.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 because there's one announce on startup and one when shutting down
|
|
|
|
TEST_EQUAL(v4_announces, 2);
|
|
|
|
TEST_EQUAL(v6_announces, 2);
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:38:45 +01:00
|
|
|
// this runs a simulation of a torrent with tracker(s), making sure the request
|
|
|
|
// received by the tracker matches the expectation.
|
|
|
|
// The Setup function is run first, giving the test an opportunity to add
|
|
|
|
// trackers to the torrent. It's expected to return the number of seconds to
|
|
|
|
// wait until test2 is called.
|
|
|
|
// The Announce function is called on http requests. Test1 is run on the session
|
|
|
|
// 5 seconds after startup. The tracker is running at 10.0.0.2 (or tracker.com)
|
|
|
|
// port 8080.
|
|
|
|
template <typename Setup, typename Announce, typename Test1, typename Test2>
|
|
|
|
void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
|
|
|
|
, char const* url_path = "/announce")
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
sim::asio::io_service tracker_ios(sim, address_v4::from_string("10.0.0.2"));
|
|
|
|
|
|
|
|
// listen on port 8080
|
|
|
|
sim::http_server http(tracker_ios, 8080);
|
|
|
|
|
2016-01-25 06:51:20 +01:00
|
|
|
http.register_handler(url_path, a);
|
2015-11-25 05:38:19 +01:00
|
|
|
|
|
|
|
lt::session_proxy zombie;
|
|
|
|
|
|
|
|
asio::io_service ios(sim, { address_v4::from_string("10.0.0.3")
|
|
|
|
, address_v6::from_string("ffff::1337") });
|
|
|
|
lt::settings_pack sett = settings();
|
|
|
|
std::unique_ptr<lt::session> ses(new lt::session(sett, ios));
|
|
|
|
|
|
|
|
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
|
|
|
|
|
|
|
|
lt::add_torrent_params p;
|
|
|
|
p.name = "test-torrent";
|
|
|
|
p.save_path = ".";
|
|
|
|
p.info_hash.assign("abababababababababab");
|
2016-04-07 06:06:04 +02:00
|
|
|
int const delay = setup(p, *ses);
|
2015-11-25 05:38:19 +01:00
|
|
|
ses->async_add_torrent(p);
|
|
|
|
|
2016-01-25 06:51:20 +01:00
|
|
|
// run the test 5 seconds in
|
2016-03-27 03:02:49 +02:00
|
|
|
sim::timer t1(sim, lt::seconds(5)
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&ses,&test1](boost::system::error_code const&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
|
|
|
|
TEST_EQUAL(torrents.size(), 1);
|
|
|
|
torrent_handle h = torrents.front();
|
2016-01-25 06:51:20 +01:00
|
|
|
test1(h);
|
|
|
|
});
|
2015-11-25 05:38:19 +01:00
|
|
|
|
2016-03-27 03:02:49 +02:00
|
|
|
sim::timer t2(sim, lt::seconds(5 + delay)
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&ses,&test2](boost::system::error_code const&)
|
2016-01-25 06:51:20 +01:00
|
|
|
{
|
|
|
|
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
|
|
|
|
TEST_EQUAL(torrents.size(), 1);
|
|
|
|
torrent_handle h = torrents.front();
|
|
|
|
test2(h);
|
2015-11-25 05:38:19 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// then shut down 10 seconds in
|
2016-03-27 03:02:49 +02:00
|
|
|
sim::timer t3(sim, lt::seconds(10 + delay)
|
2016-06-19 01:24:27 +02:00
|
|
|
, [&ses,&zombie](boost::system::error_code const&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
zombie = ses->abort();
|
|
|
|
ses.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:38:45 +01:00
|
|
|
template <typename Announce, typename Test1, typename Test2>
|
|
|
|
void tracker_test(Announce a, Test1 test1, Test2 test2, char const* url_path = "/announce")
|
|
|
|
{
|
2016-04-07 06:06:04 +02:00
|
|
|
tracker_test([](lt::add_torrent_params& p, lt::session&) {
|
2016-03-18 19:38:45 +01:00
|
|
|
p.trackers.push_back("http://tracker.com:8080/announce");
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
a, test1, test2, url_path);
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:51:20 +01:00
|
|
|
template <typename Announce, typename Test>
|
|
|
|
void announce_entry_test(Announce a, Test t, char const* url_path = "/announce")
|
|
|
|
{
|
|
|
|
tracker_test(a
|
|
|
|
, [&t] (torrent_handle h) {
|
|
|
|
std::vector<announce_entry> tr = h.trackers();
|
|
|
|
|
|
|
|
TEST_EQUAL(tr.size(), 1);
|
|
|
|
announce_entry const& ae = tr[0];
|
|
|
|
t(ae);
|
|
|
|
}
|
|
|
|
, [](torrent_handle){}
|
|
|
|
, url_path);
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:38:19 +01:00
|
|
|
TORRENT_TEST(test_error)
|
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d14:failure reason4:teste");
|
2015-11-25 05:38:19 +01:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), false);
|
|
|
|
TEST_EQUAL(ae.message, "test");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
2016-10-02 21:27:50 +02:00
|
|
|
TEST_EQUAL(ae.last_error, error_code(errors::tracker_failure));
|
2015-11-25 05:38:19 +01:00
|
|
|
TEST_EQUAL(ae.fails, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_warning)
|
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d5:peers6:aaaaaa15:warning message5:test2e");
|
2015-11-25 05:38:19 +01:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), true);
|
|
|
|
TEST_EQUAL(ae.message, "test2");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
|
|
|
TEST_EQUAL(ae.last_error, error_code());
|
|
|
|
TEST_EQUAL(ae.fails, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:51:20 +01:00
|
|
|
TORRENT_TEST(test_scrape_data_in_announce)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response),
|
2015-11-25 05:38:19 +01:00
|
|
|
"d5:peers6:aaaaaa8:completei1e10:incompletei2e10:downloadedi3e11:downloadersi4ee");
|
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), true);
|
|
|
|
TEST_EQUAL(ae.message, "");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
|
|
|
TEST_EQUAL(ae.last_error, error_code());
|
|
|
|
TEST_EQUAL(ae.fails, 0);
|
|
|
|
TEST_EQUAL(ae.scrape_complete, 1);
|
|
|
|
TEST_EQUAL(ae.scrape_incomplete, 2);
|
|
|
|
TEST_EQUAL(ae.scrape_downloaded, 3);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:51:20 +01:00
|
|
|
TORRENT_TEST(test_scrape)
|
|
|
|
{
|
|
|
|
tracker_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2016-01-25 06:51:20 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response),
|
2016-01-25 06:51:20 +01:00
|
|
|
"d5:filesd20:ababababababababababd8:completei1e10:downloadedi3e10:incompletei2eeee");
|
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](torrent_handle h)
|
|
|
|
{
|
|
|
|
h.scrape_tracker();
|
|
|
|
}
|
|
|
|
, [](torrent_handle h)
|
|
|
|
{
|
|
|
|
std::vector<announce_entry> tr = h.trackers();
|
|
|
|
|
|
|
|
TEST_EQUAL(tr.size(), 1);
|
|
|
|
announce_entry const& ae = tr[0];
|
|
|
|
TEST_EQUAL(ae.scrape_incomplete, 2);
|
|
|
|
TEST_EQUAL(ae.scrape_complete, 1);
|
|
|
|
TEST_EQUAL(ae.scrape_downloaded, 3);
|
|
|
|
}
|
|
|
|
, "/scrape");
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:38:19 +01:00
|
|
|
TORRENT_TEST(test_http_status)
|
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
return sim::send_response(410, "Not A Tracker", 0);
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), false);
|
|
|
|
TEST_EQUAL(ae.message, "Not A Tracker");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
2016-10-02 21:27:50 +02:00
|
|
|
TEST_EQUAL(ae.last_error, error_code(410, http_category()));
|
2015-11-25 05:38:19 +01:00
|
|
|
TEST_EQUAL(ae.fails, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_interval)
|
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response)
|
2015-11-25 05:38:19 +01:00
|
|
|
, "d10:tracker id8:testteste");
|
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), true);
|
|
|
|
TEST_EQUAL(ae.message, "");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
|
|
|
TEST_EQUAL(ae.last_error, error_code());
|
|
|
|
TEST_EQUAL(ae.fails, 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(ae.trackerid, "testtest");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_invalid_bencoding)
|
|
|
|
{
|
|
|
|
announce_entry_test(
|
|
|
|
[](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-25 05:38:19 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
char response[500];
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response)
|
2015-11-25 05:38:19 +01:00
|
|
|
, "d10:tracer idteste");
|
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](announce_entry const& ae)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ae.is_working(), false);
|
|
|
|
TEST_EQUAL(ae.message, "");
|
|
|
|
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
|
|
|
|
TEST_EQUAL(ae.last_error, error_code(bdecode_errors::expected_value
|
2016-10-02 21:27:50 +02:00
|
|
|
, bdecode_category()));
|
2015-11-25 05:38:19 +01:00
|
|
|
TEST_EQUAL(ae.fails, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:38:45 +01:00
|
|
|
TORRENT_TEST(try_next)
|
|
|
|
{
|
|
|
|
// test that we move on to try the next tier if the first one fails
|
|
|
|
|
|
|
|
bool got_announce = false;
|
|
|
|
tracker_test(
|
2016-04-07 06:06:04 +02:00
|
|
|
[](lt::add_torrent_params& p, lt::session&)
|
2016-03-18 19:38:45 +01:00
|
|
|
{
|
|
|
|
// TODO: 3 use tracker_tiers here to put the trackers in different tiers
|
|
|
|
p.trackers.push_back("udp://failing-tracker.com/announce");
|
|
|
|
p.trackers.push_back("http://failing-tracker.com/announce");
|
|
|
|
|
|
|
|
// this is the working tracker
|
|
|
|
p.trackers.push_back("http://tracker.com:8080/announce");
|
|
|
|
return 60;
|
|
|
|
},
|
|
|
|
[&](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2016-03-18 19:38:45 +01:00
|
|
|
{
|
|
|
|
got_announce = true;
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char response[500];
|
|
|
|
// respond with an empty peer list
|
2016-06-19 01:24:27 +02:00
|
|
|
int const size = std::snprintf(response, sizeof(response), "d5:peers0:e");
|
2016-03-18 19:38:45 +01:00
|
|
|
return sim::send_response(200, "OK", size) + response;
|
|
|
|
}
|
|
|
|
, [](torrent_handle h) {}
|
|
|
|
, [](torrent_handle h)
|
|
|
|
{
|
|
|
|
torrent_status st = h.status();
|
|
|
|
TEST_EQUAL(st.current_tracker, "http://tracker.com:8080/announce");
|
|
|
|
|
|
|
|
std::vector<announce_entry> tr = h.trackers();
|
|
|
|
|
|
|
|
TEST_EQUAL(tr.size(), 3);
|
|
|
|
|
2016-04-17 07:56:05 +02:00
|
|
|
for (int i = 0; i < int(tr.size()); ++i)
|
2016-03-18 19:38:45 +01:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("tracker \"%s\"\n", tr[i].url.c_str());
|
2016-03-18 19:38:45 +01:00
|
|
|
if (tr[i].url == "http://tracker.com:8080/announce")
|
|
|
|
{
|
|
|
|
TEST_EQUAL(tr[i].fails, 0);
|
|
|
|
TEST_EQUAL(tr[i].verified, true);
|
|
|
|
}
|
|
|
|
else if (tr[i].url == "http://failing-tracker.com/announce")
|
|
|
|
{
|
|
|
|
TEST_CHECK(tr[i].fails >= 1);
|
|
|
|
TEST_EQUAL(tr[i].verified, false);
|
|
|
|
TEST_EQUAL(tr[i].last_error
|
|
|
|
, error_code(boost::asio::error::host_not_found));
|
|
|
|
}
|
|
|
|
else if (tr[i].url == "udp://failing-tracker.com/announce")
|
|
|
|
{
|
|
|
|
TEST_CHECK(tr[i].fails >= 1);
|
|
|
|
TEST_EQUAL(tr[i].verified, false);
|
|
|
|
TEST_EQUAL(tr[i].last_error
|
|
|
|
, error_code(boost::asio::error::host_not_found));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ERROR(("unexpected tracker URL: " + tr[i].url).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
TEST_EQUAL(got_announce, true);
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> make_torrent(bool priv)
|
2016-04-07 06:06:04 +02:00
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("foobar", 13241);
|
|
|
|
create_torrent ct(fs);
|
|
|
|
|
|
|
|
ct.add_tracker("http://tracker.com:8080/announce");
|
|
|
|
|
|
|
|
for (int i = 0; i < ct.num_pieces(); ++i)
|
2016-07-09 22:26:26 +02:00
|
|
|
ct.set_hash(i, sha1_hash(nullptr));
|
2016-04-07 06:06:04 +02:00
|
|
|
|
|
|
|
ct.set_priv(priv);
|
|
|
|
|
|
|
|
entry e = ct.generate();
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), e);
|
|
|
|
error_code ec;
|
2016-08-17 23:26:35 +02:00
|
|
|
return std::make_shared<torrent_info>(buf.data(), int(buf.size()), ec);
|
2016-04-07 06:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we _do_ send our IPv6 address to trackers for private torrents
|
|
|
|
TORRENT_TEST(tracker_ipv6_argument)
|
|
|
|
{
|
|
|
|
bool got_announce = false;
|
|
|
|
bool got_ipv6 = false;
|
|
|
|
tracker_test(
|
|
|
|
[](lt::add_torrent_params& p, lt::session& ses)
|
|
|
|
{
|
|
|
|
settings_pack pack;
|
|
|
|
pack.set_bool(settings_pack::anonymous_mode, false);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
p.ti = make_torrent(true);
|
|
|
|
return 60;
|
|
|
|
},
|
|
|
|
[&](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2016-04-07 06:06:04 +02:00
|
|
|
{
|
|
|
|
got_announce = true;
|
2016-04-08 14:41:19 +02:00
|
|
|
bool const stop_event = req.find("&event=stopped") != std::string::npos;
|
|
|
|
// stop events don't need to advertise the IPv6 address
|
2016-04-17 07:56:05 +02:00
|
|
|
std::string::size_type pos = req.find("&ipv6=");
|
2016-04-08 14:41:19 +02:00
|
|
|
TEST_CHECK(pos != std::string::npos || stop_event);
|
|
|
|
got_ipv6 |= pos != std::string::npos;
|
2016-04-07 06:06:04 +02:00
|
|
|
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
|
|
|
|
}
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](torrent_handle) {}
|
|
|
|
, [](torrent_handle) {});
|
2016-04-07 06:06:04 +02:00
|
|
|
TEST_EQUAL(got_announce, true);
|
|
|
|
TEST_EQUAL(got_ipv6, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we do _not_ send our IPv6 address to trackers for non-private
|
|
|
|
// torrents
|
|
|
|
TORRENT_TEST(tracker_ipv6_argument_non_private)
|
|
|
|
{
|
|
|
|
bool got_announce = false;
|
|
|
|
bool got_ipv6 = false;
|
|
|
|
tracker_test(
|
|
|
|
[](lt::add_torrent_params& p, lt::session& ses)
|
|
|
|
{
|
|
|
|
settings_pack pack;
|
|
|
|
pack.set_bool(settings_pack::anonymous_mode, false);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
p.ti = make_torrent(false);
|
|
|
|
return 60;
|
|
|
|
},
|
|
|
|
[&](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2016-04-07 06:06:04 +02:00
|
|
|
{
|
|
|
|
got_announce = true;
|
2016-04-17 07:56:05 +02:00
|
|
|
std::string::size_type pos = req.find("&ipv6=");
|
2016-04-07 06:06:04 +02:00
|
|
|
TEST_CHECK(pos == std::string::npos);
|
2016-04-08 14:41:19 +02:00
|
|
|
got_ipv6 |= pos != std::string::npos;
|
2016-04-07 06:06:04 +02:00
|
|
|
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
|
|
|
|
}
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](torrent_handle) {}
|
|
|
|
, [](torrent_handle) {});
|
2016-04-07 06:06:04 +02:00
|
|
|
TEST_EQUAL(got_announce, true);
|
|
|
|
TEST_EQUAL(got_ipv6, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(tracker_ipv6_argument_privacy_mode)
|
|
|
|
{
|
|
|
|
bool got_announce = false;
|
|
|
|
bool got_ipv6 = false;
|
|
|
|
tracker_test(
|
|
|
|
[](lt::add_torrent_params& p, lt::session& ses)
|
|
|
|
{
|
|
|
|
settings_pack pack;
|
|
|
|
pack.set_bool(settings_pack::anonymous_mode, true);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
p.ti = make_torrent(true);
|
|
|
|
return 60;
|
|
|
|
},
|
|
|
|
[&](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2016-04-07 06:06:04 +02:00
|
|
|
{
|
|
|
|
got_announce = true;
|
2016-04-17 07:56:05 +02:00
|
|
|
std::string::size_type pos = req.find("&ipv6=");
|
2016-04-07 06:06:04 +02:00
|
|
|
TEST_CHECK(pos == std::string::npos);
|
2016-04-08 14:41:19 +02:00
|
|
|
got_ipv6 |= pos != std::string::npos;
|
2016-04-07 06:06:04 +02:00
|
|
|
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
|
|
|
|
}
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](torrent_handle) {}
|
|
|
|
, [](torrent_handle) {});
|
2016-04-07 06:06:04 +02:00
|
|
|
TEST_EQUAL(got_announce, true);
|
|
|
|
TEST_EQUAL(got_ipv6, false);
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:38:19 +01:00
|
|
|
// TODO: test external IP
|
2015-09-10 05:57:14 +02:00
|
|
|
// TODO: test with different queuing settings
|
|
|
|
// TODO: test when a torrent transitions from downloading to finished and
|
|
|
|
// finished to seeding
|
|
|
|
// TODO: test that left, downloaded and uploaded are reported correctly
|
|
|
|
|
|
|
|
// TODO: test scrape
|
|
|
|
|