2014-07-09 22:53:39 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008-2014, 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"
|
|
|
|
#include "web_seed_suite.hpp"
|
2016-08-21 18:15:19 +02:00
|
|
|
#include "settings.hpp"
|
2014-07-09 22:53:39 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2014-07-09 22:53:39 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2014-07-09 22:53:39 +02:00
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(web_seed_redirect)
|
2014-07-09 22:53:39 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2014-07-09 22:53:39 +02:00
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
file_storage fs;
|
|
|
|
int piece_size = 0x4000;
|
|
|
|
|
|
|
|
char random_data[16000];
|
|
|
|
std::generate(random_data, random_data + sizeof(random_data), random_byte);
|
|
|
|
file f("test_file", file::write_only, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("failed to create file \"test_file\": (%d) %s\n"
|
2014-07-09 22:53:39 +02:00
|
|
|
, ec.value(), ec.message().c_str());
|
2015-05-30 23:46:59 +02:00
|
|
|
TEST_ERROR("failed to create file");
|
|
|
|
return;
|
2014-07-09 22:53:39 +02:00
|
|
|
}
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t b = { random_data, size_t(16000)};
|
2016-10-27 02:40:56 +02:00
|
|
|
f.writev(0, b, ec);
|
2014-07-09 22:53:39 +02:00
|
|
|
fs.add_file("test_file", 16000);
|
|
|
|
|
|
|
|
int port = start_web_server();
|
|
|
|
|
|
|
|
// generate a torrent with pad files to make sure they
|
|
|
|
// are not requested web seeds
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, piece_size, 0x4000);
|
2014-07-09 22:53:39 +02:00
|
|
|
|
|
|
|
char tmp[512];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(tmp, sizeof(tmp), "http://127.0.0.1:%d/redirect", port);
|
2014-07-09 22:53:39 +02:00
|
|
|
t.add_url_seed(tmp);
|
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
set_piece_hashes(t, ".", ec);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("error creating hashes for test torrent: %s\n"
|
2014-07-09 22:53:39 +02:00
|
|
|
, ec.message().c_str());
|
2015-05-30 23:46:59 +02:00
|
|
|
TEST_ERROR("failed to create hashes");
|
|
|
|
return;
|
2014-07-09 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2016-08-17 23:26:35 +02:00
|
|
|
auto torrent_file = std::make_shared<torrent_info>(&buf[0]
|
|
|
|
, int(buf.size()), ec);
|
2014-07-09 22:53:39 +02:00
|
|
|
|
|
|
|
{
|
2016-08-21 18:15:19 +02:00
|
|
|
settings_pack p = settings();
|
|
|
|
p.set_int(settings_pack::max_queued_disk_bytes, 256 * 1024);
|
|
|
|
p.set_int(settings_pack::alert_mask, ~(alert::progress_notification | alert::stats_notification));
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::session ses(p);
|
2015-05-30 23:46:59 +02:00
|
|
|
|
2014-07-09 22:53:39 +02:00
|
|
|
// disable keep-alive because otherwise the test will choke on seeing
|
|
|
|
// the disconnect (from the redirect)
|
2016-07-09 22:26:26 +02:00
|
|
|
test_transfer(ses, torrent_file, 0, nullptr, "http", true, false, false, false);
|
2014-07-09 22:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_web_server();
|
|
|
|
}
|
|
|
|
|
|
|
|
|