2016-07-07 08:22:15 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2016, 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/session.hpp"
|
|
|
|
#include "libtorrent/settings_pack.hpp"
|
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2016-10-03 08:08:03 +02:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2016-07-07 08:22:15 +02:00
|
|
|
#include "simulator/http_server.hpp"
|
2017-02-23 04:46:33 +01:00
|
|
|
#include "simulator/http_proxy.hpp"
|
2016-07-07 08:22:15 +02:00
|
|
|
#include "settings.hpp"
|
|
|
|
#include "libtorrent/create_torrent.hpp"
|
|
|
|
#include "simulator/simulator.hpp"
|
|
|
|
#include "setup_swarm.hpp"
|
|
|
|
#include "utils.hpp"
|
2017-02-23 04:46:33 +01:00
|
|
|
#include "make_proxy_settings.hpp"
|
2016-07-07 08:22:15 +02:00
|
|
|
#include "simulator/utils.hpp"
|
|
|
|
#include <iostream>
|
2017-03-18 20:53:07 +01:00
|
|
|
#include <numeric>
|
2016-07-07 08:22:15 +02:00
|
|
|
|
|
|
|
using namespace sim;
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-07-07 08:22:15 +02:00
|
|
|
|
|
|
|
|
2017-02-23 04:46:33 +01:00
|
|
|
int const piece_size = 0x4000;
|
|
|
|
|
2016-10-03 08:08:03 +02:00
|
|
|
add_torrent_params create_torrent(file_storage& fs, bool const pad_files = false)
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, piece_size
|
2016-10-03 08:08:03 +02:00
|
|
|
, pad_files ? piece_size : -1
|
2017-08-03 12:02:03 +02:00
|
|
|
, pad_files ? create_torrent::optimize_alignment : create_flags_t{});
|
2016-07-07 08:22:15 +02:00
|
|
|
|
2017-02-10 21:31:20 +01:00
|
|
|
std::vector<char> piece;
|
|
|
|
piece.reserve(fs.piece_length());
|
2016-12-26 00:20:17 +01:00
|
|
|
piece_index_t const num = fs.end_piece();
|
2017-02-10 21:31:20 +01:00
|
|
|
for (piece_index_t i(0); i < num; ++i)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
2017-02-10 21:31:20 +01:00
|
|
|
int k = 0;
|
|
|
|
std::vector<file_slice> files = fs.map_block(i, 0, fs.piece_size(i));
|
|
|
|
for (auto& f : files)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
2017-02-10 21:31:20 +01:00
|
|
|
if (fs.pad_file_at(f.file_index))
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
2017-02-10 21:31:20 +01:00
|
|
|
for (int j = 0; j < f.size; ++j, ++k)
|
|
|
|
piece.push_back('\0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int j = 0; j < f.size; ++j, ++k)
|
|
|
|
piece.push_back((k % 26) + 'A');
|
2016-10-03 08:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:31:20 +01:00
|
|
|
t.set_hash(i, hasher(piece).final());
|
|
|
|
piece.clear();
|
2016-10-03 08:08:03 +02:00
|
|
|
}
|
2016-07-07 08:22:15 +02:00
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2016-07-07 08:22:15 +02:00
|
|
|
|
|
|
|
entry tor = t.generate();
|
|
|
|
|
|
|
|
bencode(out, tor);
|
2016-10-03 08:08:03 +02:00
|
|
|
add_torrent_params ret;
|
2017-07-20 09:27:47 +02:00
|
|
|
ret.ti = std::make_shared<torrent_info>(tmp, from_span);
|
2017-07-26 23:56:17 +02:00
|
|
|
ret.flags &= ~lt::torrent_flags::auto_managed;
|
|
|
|
ret.flags &= ~lt::torrent_flags::paused;
|
2016-10-03 08:08:03 +02:00
|
|
|
ret.save_path = ".";
|
|
|
|
return ret;
|
2016-07-07 08:22:15 +02:00
|
|
|
}
|
|
|
|
// this is the general template for these tests. create the session with custom
|
|
|
|
// settings (Settings), set up the test, by adding torrents with certain
|
|
|
|
// arguments (Setup), run the test and verify the end state (Test)
|
|
|
|
template <typename Setup, typename HandleAlerts, typename Test>
|
|
|
|
void run_test(Setup const& setup
|
|
|
|
, HandleAlerts const& on_alert
|
2017-03-18 20:53:07 +01:00
|
|
|
, Test const& test
|
|
|
|
, lt::seconds const timeout = lt::seconds{100})
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
|
|
|
// setup the simulation
|
|
|
|
sim::default_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
std::unique_ptr<sim::asio::io_service> ios = make_io_service(sim, 0);
|
|
|
|
lt::session_proxy zombie;
|
|
|
|
|
|
|
|
lt::settings_pack pack = settings();
|
|
|
|
// create session
|
|
|
|
std::shared_ptr<lt::session> ses = std::make_shared<lt::session>(pack, *ios);
|
|
|
|
|
|
|
|
// set up test, like adding torrents (customization point)
|
|
|
|
setup(*ses);
|
|
|
|
|
|
|
|
// only monitor alerts for session 0 (the downloader)
|
|
|
|
print_alerts(*ses, [=](lt::session& ses, lt::alert const* a) {
|
|
|
|
on_alert(ses, a);
|
|
|
|
});
|
|
|
|
|
|
|
|
// set up a timer to fire later, to verify everything we expected to happen
|
|
|
|
// happened
|
2018-11-16 16:19:12 +01:00
|
|
|
sim::timer t(sim, timeout, [&](boost::system::error_code const&)
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("shutting down\n");
|
2016-07-07 08:22:15 +02:00
|
|
|
// shut down
|
|
|
|
zombie = ses->abort();
|
|
|
|
ses.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
test(sim, *ses);
|
|
|
|
}
|
|
|
|
|
2016-10-03 08:08:03 +02:00
|
|
|
TORRENT_TEST(single_file)
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("abc'abc", 0x8000); // this filename will have to be escaped
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
2016-07-07 08:22:15 +02:00
|
|
|
bool expected = false;
|
|
|
|
run_test(
|
2016-10-03 08:08:03 +02:00
|
|
|
[¶ms](lt::session& ses)
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[](lt::session&, lt::alert const*) {},
|
|
|
|
[&expected](sim::simulation& sim, lt::session&)
|
2016-07-07 08:22:15 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
// make sure the requested file is correctly escaped
|
|
|
|
http.register_handler("/abc%27abc"
|
|
|
|
, [&expected](std::string method, std::string req
|
|
|
|
, std::map<std::string, std::string>&)
|
|
|
|
{
|
|
|
|
expected = true;
|
|
|
|
return sim::send_response(404, "Not Found", 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_CHECK(expected);
|
|
|
|
}
|
|
|
|
|
2016-10-03 08:08:03 +02:00
|
|
|
TORRENT_TEST(multi_file)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("foo", "abc'abc"), 0x8000); // this filename will have to be escaped
|
|
|
|
fs.add_file(combine_path("foo", "bar"), 0x3000);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
std::array<bool, 2> expected{{ false, false }};
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[](lt::session&, lt::alert const*) {},
|
|
|
|
[&expected](sim::simulation& sim, lt::session&)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
// make sure the requested file is correctly escaped
|
|
|
|
http.register_handler("/foo/abc%27abc"
|
|
|
|
, [&expected](std::string, std::string, std::map<std::string, std::string>&)
|
|
|
|
{
|
|
|
|
expected[0] = true;
|
|
|
|
return sim::send_response(404, "not found", 0);
|
|
|
|
});
|
|
|
|
http.register_handler("/foo/bar"
|
|
|
|
, [&expected](std::string, std::string, std::map<std::string, std::string>&)
|
|
|
|
{
|
|
|
|
expected[1] = true;
|
|
|
|
return sim::send_response(404, "not found", 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_CHECK(expected[0]);
|
|
|
|
TEST_CHECK(expected[1]);
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:20:17 +01:00
|
|
|
std::string generate_content(lt::file_storage const& fs, file_index_t file
|
2016-10-03 08:08:03 +02:00
|
|
|
, std::int64_t offset, std::int64_t len)
|
|
|
|
{
|
|
|
|
std::string ret;
|
2017-02-23 04:46:33 +01:00
|
|
|
ret.reserve(lt::aux::numeric_cast<std::size_t>(len));
|
2016-10-03 08:08:03 +02:00
|
|
|
std::int64_t const file_offset = fs.file_offset(file);
|
2018-11-16 16:19:12 +01:00
|
|
|
int const piece_sz = fs.piece_length();
|
2017-02-10 21:31:20 +01:00
|
|
|
for (std::int64_t i = offset + file_offset; i < offset + file_offset + len; ++i)
|
2018-11-16 16:19:12 +01:00
|
|
|
ret.push_back(((i % piece_sz) % 26) + 'A');
|
2016-10-03 08:08:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void serve_content_for(sim::http_server& http, std::string const& path
|
2016-12-26 00:20:17 +01:00
|
|
|
, lt::file_storage const& fs, file_index_t const file)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
2016-12-26 00:20:17 +01:00
|
|
|
http.register_content(path, fs.file_size(file_index_t(file))
|
2016-10-03 08:08:03 +02:00
|
|
|
, [&fs,file](std::int64_t offset, std::int64_t len)
|
|
|
|
{ return generate_content(fs, file, offset, len); });
|
|
|
|
}
|
|
|
|
|
|
|
|
// test redirecting *unaligned* files to the same server still working. i.e. the
|
|
|
|
// second redirect is added to the same web-seed entry as the first one
|
|
|
|
TORRENT_TEST(unaligned_file_redirect)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("foo", "1"), 0xc030);
|
|
|
|
fs.add_file(combine_path("foo", "2"), 0xc030);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
bool seeding = false;
|
|
|
|
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&](lt::session&, lt::alert const* alert) {
|
2016-10-03 08:08:03 +02:00
|
|
|
if (lt::alert_cast<lt::torrent_finished_alert>(alert))
|
|
|
|
seeding = true;
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&fs](sim::simulation& sim, lt::session&)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
// http1 is the root web server that will just redirect requests to
|
|
|
|
// other servers
|
|
|
|
sim::asio::io_service web_server1(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
sim::http_server http1(web_server1, 8080);
|
|
|
|
// redirect file 1 and file 2 to the same servers
|
|
|
|
http1.register_redirect("/foo/1", "http://3.3.3.3:4444/bla/file1");
|
|
|
|
http1.register_redirect("/foo/2", "http://3.3.3.3:4444/bar/file2");
|
|
|
|
|
|
|
|
// server for serving the content
|
|
|
|
sim::asio::io_service web_server2(sim, address_v4::from_string("3.3.3.3"));
|
|
|
|
sim::http_server http2(web_server2, 4444);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http2, "/bla/file1", fs, file_index_t(0));
|
|
|
|
serve_content_for(http2, "/bar/file2", fs, file_index_t(1));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_EQUAL(seeding, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test redirecting *unaligned* but padded files to separate servers
|
|
|
|
TORRENT_TEST(multi_file_redirect_pad_files)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
file_storage fs_;
|
|
|
|
fs_.add_file(combine_path("foo", "1"), 0xc030);
|
|
|
|
fs_.add_file(combine_path("foo", "2"), 0xc030);
|
|
|
|
// true means use padfiles
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs_, true);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
// since the final torrent is different than what we built (because of pad
|
|
|
|
// files), ask about it.
|
|
|
|
file_storage const& fs = params.ti->files();
|
|
|
|
|
|
|
|
bool seeding = false;
|
|
|
|
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&](lt::session&, lt::alert const* alert) {
|
2016-10-03 08:08:03 +02:00
|
|
|
if (lt::alert_cast<lt::torrent_finished_alert>(alert))
|
|
|
|
seeding = true;
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&fs](sim::simulation& sim, lt::session&)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
// http1 is the root web server that will just redirect requests to
|
|
|
|
// other servers
|
|
|
|
sim::asio::io_service web_server1(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
sim::http_server http1(web_server1, 8080);
|
|
|
|
// redirect file 1 and file 2 to different servers
|
|
|
|
http1.register_redirect("/foo/1", "http://3.3.3.3:4444/bla/file1");
|
|
|
|
http1.register_redirect("/foo/2", "http://4.4.4.4:9999/bar/file2");
|
|
|
|
|
|
|
|
// server for file 1
|
|
|
|
sim::asio::io_service web_server2(sim, address_v4::from_string("3.3.3.3"));
|
|
|
|
sim::http_server http2(web_server2, 4444);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http2, "/bla/file1", fs, file_index_t(0));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
// server for file 2
|
|
|
|
sim::asio::io_service web_server3(sim, address_v4::from_string("4.4.4.4"));
|
|
|
|
sim::http_server http3(web_server3, 9999);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http3, "/bar/file2", fs, file_index_t(2));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_EQUAL(seeding, true);
|
|
|
|
}
|
|
|
|
// test that a web seed can redirect files to separate web servers (as long as
|
|
|
|
// they are piece aligned)
|
|
|
|
TORRENT_TEST(multi_file_redirect)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("foo", "1"), 0xc000);
|
|
|
|
fs.add_file(combine_path("foo", "2"), 0xc030);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
bool seeding = false;
|
|
|
|
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&](lt::session&, lt::alert const* alert) {
|
2016-10-03 08:08:03 +02:00
|
|
|
if (lt::alert_cast<lt::torrent_finished_alert>(alert))
|
|
|
|
seeding = true;
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&fs](sim::simulation& sim, lt::session&)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
// http1 is the root web server that will just redirect requests to
|
|
|
|
// other servers
|
|
|
|
sim::asio::io_service web_server1(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
sim::http_server http1(web_server1, 8080);
|
|
|
|
// redirect file 1 and file 2 to different servers
|
|
|
|
http1.register_redirect("/foo/1", "http://3.3.3.3:4444/bla/file1");
|
|
|
|
http1.register_redirect("/foo/2", "http://4.4.4.4:9999/bar/file2");
|
|
|
|
|
|
|
|
// server for file 1
|
|
|
|
sim::asio::io_service web_server2(sim, address_v4::from_string("3.3.3.3"));
|
|
|
|
sim::http_server http2(web_server2, 4444);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http2, "/bla/file1", fs, file_index_t(0));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
// server for file 2
|
|
|
|
sim::asio::io_service web_server3(sim, address_v4::from_string("4.4.4.4"));
|
2017-02-23 04:46:33 +01:00
|
|
|
sim::http_server http3(web_server3, 9999);
|
|
|
|
serve_content_for(http3, "/bar/file2", fs, file_index_t(1));
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_EQUAL(seeding, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test web_seed redirect through proxy
|
|
|
|
TORRENT_TEST(multi_file_redirect_through_proxy)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2017-02-23 04:46:33 +01:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("foo", "1"), 0xc000);
|
|
|
|
fs.add_file(combine_path("foo", "2"), 0xc030);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
bool seeding = false;
|
|
|
|
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
settings_pack pack;
|
|
|
|
|
|
|
|
pack.set_int(settings_pack::proxy_type, settings_pack::http);
|
|
|
|
pack.set_str(settings_pack::proxy_hostname, "50.50.50.50");
|
|
|
|
pack.set_str(settings_pack::proxy_username, "testuser");
|
|
|
|
pack.set_str(settings_pack::proxy_password, "testpass");
|
|
|
|
pack.set_int(settings_pack::proxy_port, 4445);
|
|
|
|
pack.set_bool(settings_pack::proxy_hostnames, true);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&](lt::session&, lt::alert const* alert) {
|
2017-02-23 04:46:33 +01:00
|
|
|
if (lt::alert_cast<lt::torrent_finished_alert>(alert)) {
|
|
|
|
seeding = true;
|
|
|
|
}
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&fs](sim::simulation& sim, lt::session&)
|
2017-02-23 04:46:33 +01:00
|
|
|
{
|
|
|
|
sim::asio::io_service proxy_ios(sim, address_v4::from_string("50.50.50.50"));
|
|
|
|
sim::http_proxy http_p(proxy_ios, 4445);
|
|
|
|
|
|
|
|
// http1 is the root web server that will just redirect requests to
|
|
|
|
// other servers
|
|
|
|
sim::asio::io_service web_server1(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
sim::http_server http1(web_server1, 8080);
|
|
|
|
// redirect file 1 and file 2 to different servers
|
|
|
|
http1.register_redirect("/foo/1", "http://3.3.3.3:4444/bla/file1");
|
|
|
|
http1.register_redirect("/foo/2", "http://4.4.4.4:9999/bar/file2");
|
|
|
|
|
|
|
|
// server for file 1
|
|
|
|
sim::asio::io_service web_server2(sim, address_v4::from_string("3.3.3.3"));
|
|
|
|
sim::http_server http2(web_server2, 4444);
|
|
|
|
serve_content_for(http2, "/bla/file1", fs, file_index_t(0));
|
|
|
|
|
|
|
|
// server for file 2
|
|
|
|
sim::asio::io_service web_server3(sim, address_v4::from_string("4.4.4.4"));
|
2016-10-03 08:08:03 +02:00
|
|
|
sim::http_server http3(web_server3, 9999);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http3, "/bar/file2", fs, file_index_t(1));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_EQUAL(seeding, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is expected to fail, since the files are not aligned and redirected to
|
|
|
|
// separate servers, without pad files
|
|
|
|
TORRENT_TEST(multi_file_unaligned_redirect)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-10-03 08:08:03 +02:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file(combine_path("foo", "1"), 0xc030);
|
|
|
|
fs.add_file(combine_path("foo", "2"), 0xc030);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&](lt::session&, lt::alert const* alert) {
|
2016-10-03 08:08:03 +02:00
|
|
|
// We don't expect to get this aslert
|
|
|
|
TEST_CHECK(lt::alert_cast<lt::torrent_finished_alert>(alert) == nullptr);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&fs](sim::simulation& sim, lt::session&)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
// http1 is the root web server that will just redirect requests to
|
|
|
|
// other servers
|
|
|
|
sim::asio::io_service web_server1(sim, address_v4::from_string("2.2.2.2"));
|
|
|
|
sim::http_server http1(web_server1, 8080);
|
|
|
|
// redirect file 1 and file 2 to different servers
|
|
|
|
http1.register_redirect("/foo/1", "http://3.3.3.3:4444/bla/file1");
|
|
|
|
http1.register_redirect("/foo/2", "http://4.4.4.4:9999/bar/file2");
|
|
|
|
|
|
|
|
// server for file 1
|
|
|
|
sim::asio::io_service web_server2(sim, address_v4::from_string("3.3.3.3"));
|
|
|
|
sim::http_server http2(web_server2, 4444);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http2, "/bla/file1", fs, file_index_t(0));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
// server for file 2
|
|
|
|
sim::asio::io_service web_server3(sim, address_v4::from_string("4.4.4.4"));
|
|
|
|
sim::http_server http3(web_server3, 9999);
|
2016-12-26 00:20:17 +01:00
|
|
|
serve_content_for(http3, "/bar/file2", fs, file_index_t(1));
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-11-20 16:32:20 +01:00
|
|
|
TORRENT_TEST(urlseed_timeout)
|
|
|
|
{
|
|
|
|
bool timeout = false;
|
|
|
|
run_test(
|
|
|
|
[](lt::session& ses)
|
2016-11-20 17:18:16 +01:00
|
|
|
{
|
2016-11-20 16:32:20 +01:00
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("timeout_test", 0x8000);
|
2016-11-20 17:18:16 +01:00
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
2016-11-20 16:32:20 +01:00
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
2017-07-26 23:56:17 +02:00
|
|
|
params.flags &= ~lt::torrent_flags::auto_managed;
|
|
|
|
params.flags &= ~lt::torrent_flags::paused;
|
2016-11-20 16:32:20 +01:00
|
|
|
params.save_path = ".";
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[&timeout](lt::session&, lt::alert const* alert) {
|
2016-11-20 16:32:20 +01:00
|
|
|
const lt::peer_disconnected_alert *pda = lt::alert_cast<lt::peer_disconnected_alert>(alert);
|
|
|
|
if (pda && pda->error == errors::timed_out_inactivity){
|
|
|
|
timeout = true;
|
|
|
|
}
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[](sim::simulation& sim, lt::session&)
|
2016-11-20 16:32:20 +01:00
|
|
|
{
|
|
|
|
sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2"));
|
2016-11-20 17:18:16 +01:00
|
|
|
|
2016-11-20 16:32:20 +01:00
|
|
|
// listen on port 8080
|
|
|
|
sim::http_server http(web_server, 8080);
|
|
|
|
http.register_stall_handler("/timeout_test");
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
TEST_EQUAL(timeout, true);
|
2016-11-20 17:18:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 13:58:04 +01:00
|
|
|
// check for correct handle of unexpected http status response.
|
|
|
|
// with disabled "close_redundant_connections" alive web server connection
|
|
|
|
// may be closed in such manner.
|
|
|
|
TORRENT_TEST(no_close_redudant_webseed)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2017-02-08 13:58:04 +01:00
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("file1", 1);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
|
|
|
|
bool expected = false;
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
lt::settings_pack pack;
|
|
|
|
pack.set_bool(settings_pack::close_redundant_connections, false);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[](lt::session&, lt::alert const*) {},
|
|
|
|
[&expected](sim::simulation& sim, lt::session&)
|
2017-02-08 13:58:04 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
http.register_handler("/file1"
|
|
|
|
, [&expected](std::string method, std::string req
|
|
|
|
, std::map<std::string, std::string>&)
|
|
|
|
{
|
|
|
|
expected = true;
|
|
|
|
char const* extra_headers[4] = { "Content-Range: bytes 0-0/1\r\n", "", "", ""};
|
|
|
|
|
|
|
|
return sim::send_response(206, "Partial Content", 1, extra_headers).
|
|
|
|
append("A").
|
|
|
|
append(sim::send_response(408, "REQUEST TIMEOUT", 0));
|
|
|
|
});
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TEST_CHECK(expected);
|
|
|
|
}
|
2017-03-18 20:53:07 +01:00
|
|
|
|
|
|
|
// make sure the max_web_seed_connections limit is honored
|
|
|
|
TORRENT_TEST(web_seed_connection_limit)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2017-03-18 20:53:07 +01:00
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
fs.add_file("file1", 1);
|
|
|
|
lt::add_torrent_params params = ::create_torrent(fs);
|
|
|
|
params.url_seeds.push_back("http://2.2.2.1:8080/");
|
|
|
|
params.url_seeds.push_back("http://2.2.2.2:8080/");
|
|
|
|
params.url_seeds.push_back("http://2.2.2.3:8080/");
|
|
|
|
params.url_seeds.push_back("http://2.2.2.4:8080/");
|
|
|
|
|
|
|
|
std::array<int, 4> expected = {};
|
|
|
|
run_test(
|
|
|
|
[¶ms](lt::session& ses)
|
|
|
|
{
|
|
|
|
lt::settings_pack pack;
|
|
|
|
pack.set_int(settings_pack::max_web_seed_connections, 2);
|
|
|
|
ses.apply_settings(pack);
|
|
|
|
ses.async_add_torrent(params);
|
|
|
|
},
|
2018-11-16 16:19:12 +01:00
|
|
|
[](lt::session&, lt::alert const*) {},
|
|
|
|
[&expected](sim::simulation& sim, lt::session&)
|
2017-03-18 20:53:07 +01:00
|
|
|
{
|
|
|
|
using ios = sim::asio::io_service;
|
|
|
|
ios web_server1{sim, address_v4::from_string("2.2.2.1")};
|
|
|
|
ios web_server2{sim, address_v4::from_string("2.2.2.2")};
|
|
|
|
ios web_server3{sim, address_v4::from_string("2.2.2.3")};
|
|
|
|
ios web_server4{sim, address_v4::from_string("2.2.2.4")};
|
|
|
|
|
|
|
|
// listen on port 8080
|
|
|
|
using ws = sim::http_server;
|
|
|
|
ws http1{web_server1, 8080};
|
|
|
|
ws http2{web_server2, 8080};
|
|
|
|
ws http3{web_server3, 8080};
|
|
|
|
ws http4{web_server4, 8080};
|
|
|
|
|
|
|
|
auto const handler = [&expected](std::string method, std::string req
|
|
|
|
, std::map<std::string, std::string>&, int idx)
|
|
|
|
{
|
|
|
|
++expected[idx];
|
|
|
|
// deliberately avoid sending the content, to cause a hang
|
|
|
|
return sim::send_response(206, "Partial Content", 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace std::placeholders;
|
|
|
|
http1.register_handler("/file1", std::bind(handler, _1, _2, _3, 0));
|
|
|
|
http2.register_handler("/file1", std::bind(handler, _1, _2, _3, 1));
|
|
|
|
http3.register_handler("/file1", std::bind(handler, _1, _2, _3, 2));
|
|
|
|
http4.register_handler("/file1", std::bind(handler, _1, _2, _3, 3));
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
},
|
|
|
|
lt::seconds(15)
|
|
|
|
);
|
|
|
|
|
|
|
|
// make sure we only connected to 2 of the web seeds, since that's the limit
|
|
|
|
TEST_CHECK(std::accumulate(expected.begin(), expected.end(), 0) == 2);
|
|
|
|
}
|
|
|
|
|