2009-02-11 02:50:17 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-05-27 10:07:59 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/session_settings.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/time.hpp"
|
2008-05-27 10:07:59 +02:00
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2008-12-14 20:47:02 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2014-11-08 19:36:54 +01:00
|
|
|
#include "libtorrent/torrent.hpp"
|
2016-05-27 18:12:32 +02:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2016-06-23 19:19:35 +02:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp" // for combine_path, current_working_directory
|
2017-03-29 08:16:07 +02:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2016-08-21 18:15:19 +02:00
|
|
|
#include "settings.hpp"
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <tuple>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2008-05-27 10:07:59 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
2017-10-29 00:44:40 +02:00
|
|
|
#include "test_utils.hpp"
|
2008-05-27 10:07:59 +02:00
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-07-04 17:49:22 +02:00
|
|
|
bool wait_priority(torrent_handle const& h, aux::vector<download_priority_t, file_index_t> const& prio)
|
2018-07-04 08:16:33 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
{
|
2018-07-04 17:49:22 +02:00
|
|
|
if (h.get_file_priorities() == prio) return true;
|
2018-07-04 08:16:33 +02:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
2018-07-04 17:49:22 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2018-07-04 08:16:33 +02:00
|
|
|
#else
|
2018-07-04 17:49:22 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(300));
|
2018-07-04 08:16:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:49:22 +02:00
|
|
|
return h.get_file_priorities() == prio;
|
2018-07-04 08:16:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:49:22 +02:00
|
|
|
bool prioritize_files(torrent_handle const& h, aux::vector<download_priority_t, file_index_t> const& prio)
|
2018-07-04 08:16:33 +02:00
|
|
|
{
|
|
|
|
h.prioritize_files(prio);
|
|
|
|
return wait_priority(h, prio);
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
void test_running_torrent(std::shared_ptr<torrent_info> info, std::int64_t file_size)
|
2008-08-17 13:42:57 +02:00
|
|
|
{
|
2016-08-21 18:15:19 +02:00
|
|
|
settings_pack pack = settings();
|
2018-09-22 02:25:56 +02:00
|
|
|
pack.set_int(settings_pack::alert_mask, alert::piece_progress_notification | alert::storage_notification);
|
2014-07-06 21:18:00 +02:00
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48130");
|
|
|
|
pack.set_int(settings_pack::max_retry_port_bind, 10);
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses(pack);
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2017-10-29 00:44:40 +02:00
|
|
|
aux::vector<download_priority_t, file_index_t> zeroes;
|
|
|
|
zeroes.resize(1000, 0_pri);
|
2008-05-27 10:07:59 +02:00
|
|
|
add_torrent_params p;
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2008-05-27 10:07:59 +02:00
|
|
|
p.ti = info;
|
|
|
|
p.save_path = ".";
|
2013-01-28 05:57:35 +01:00
|
|
|
|
|
|
|
// make sure we correctly handle the case where we pass in
|
|
|
|
// more values than there are files
|
2013-07-17 22:21:48 +02:00
|
|
|
p.file_priorities = zeroes;
|
2013-01-28 05:57:35 +01:00
|
|
|
|
2009-12-15 14:11:07 +01:00
|
|
|
error_code ec;
|
|
|
|
torrent_handle h = ses.add_torrent(p, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("add_torrent: %s\n", ec.message().c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
aux::vector<download_priority_t, file_index_t> ones(std::size_t(info->num_files()), 1_pri);
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_CHECK(prioritize_files(h, ones));
|
2013-01-28 05:57:35 +01:00
|
|
|
|
2008-05-27 10:07:59 +02:00
|
|
|
torrent_status st = h.status();
|
|
|
|
|
2016-04-26 06:37:47 +02:00
|
|
|
TEST_EQUAL(st.total_wanted, file_size); // we want the single file
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_EQUAL(st.total_wanted_done, 0);
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
aux::vector<download_priority_t, file_index_t> prio(std::size_t(info->num_files()), 1_pri);
|
2017-10-29 00:44:40 +02:00
|
|
|
prio[file_index_t(0)] = 0_pri;
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_CHECK(prioritize_files(h, prio));
|
2008-05-27 10:07:59 +02:00
|
|
|
st = h.status();
|
|
|
|
|
2018-07-04 08:16:33 +02:00
|
|
|
st = h.status();
|
2016-04-26 06:37:47 +02:00
|
|
|
TEST_EQUAL(st.total_wanted, 0); // we don't want anything
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_EQUAL(st.total_wanted_done, 0);
|
2017-11-05 16:27:34 +01:00
|
|
|
TEST_EQUAL(int(h.get_file_priorities().size()), info->num_files());
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (info->num_files() > 1)
|
|
|
|
{
|
2018-07-04 17:49:22 +02:00
|
|
|
prio[file_index_t{1}] = 0_pri;
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_CHECK(prioritize_files(h, prio));
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2018-07-04 08:16:33 +02:00
|
|
|
st = h.status();
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_EQUAL(st.total_wanted, file_size);
|
|
|
|
TEST_EQUAL(st.total_wanted_done, 0);
|
|
|
|
}
|
2008-12-07 22:04:19 +01:00
|
|
|
|
|
|
|
if (info->num_pieces() > 0)
|
|
|
|
{
|
2017-10-29 00:44:40 +02:00
|
|
|
h.piece_priority(piece_index_t(0), 1_pri);
|
2008-12-07 22:04:19 +01:00
|
|
|
st = h.status();
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(st.pieces.size() > 0 && st.pieces[piece_index_t(0)] == false);
|
2018-01-25 15:34:39 +01:00
|
|
|
std::vector<char> piece(std::size_t(info->piece_length()));
|
2008-12-07 22:04:19 +01:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2018-01-25 15:34:39 +01:00
|
|
|
piece[std::size_t(i)] = (i % 26) + 'A';
|
2016-12-22 16:42:33 +01:00
|
|
|
h.add_piece(piece_index_t(0), &piece[0], torrent_handle::overwrite_existing);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// wait until the piece is done writing and hashing
|
2016-04-26 06:37:47 +02:00
|
|
|
wait_for_alert(ses, piece_finished_alert::alert_type, "piece_finished_alert");
|
2008-12-07 22:04:19 +01:00
|
|
|
st = h.status();
|
2016-04-26 06:37:47 +02:00
|
|
|
TEST_CHECK(st.pieces.size() > 0);
|
2008-12-14 20:47:02 +01:00
|
|
|
|
|
|
|
std::cout << "reading piece 0" << std::endl;
|
2016-12-22 16:42:33 +01:00
|
|
|
h.read_piece(piece_index_t(0));
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* a = wait_for_alert(ses, read_piece_alert::alert_type, "read_piece");
|
|
|
|
TEST_CHECK(a);
|
|
|
|
read_piece_alert const* rpa = alert_cast<read_piece_alert>(a);
|
|
|
|
TEST_CHECK(rpa);
|
|
|
|
if (rpa)
|
2008-12-14 20:47:02 +01:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
std::cout << "SUCCEEDED!" << std::endl;
|
2018-01-25 15:34:39 +01:00
|
|
|
TEST_CHECK(std::memcmp(&piece[0], rpa->buffer.get()
|
|
|
|
, std::size_t(info->piece_size(piece_index_t(0)))) == 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(rpa->size == info->piece_size(piece_index_t(0)));
|
|
|
|
TEST_CHECK(rpa->piece == piece_index_t(0));
|
|
|
|
TEST_CHECK(hasher(piece).final() == info->hash_for_piece(piece_index_t(0)));
|
2008-12-14 20:47:02 +01:00
|
|
|
}
|
2008-12-07 22:04:19 +01:00
|
|
|
}
|
2018-07-04 08:16:33 +02:00
|
|
|
|
2018-07-04 17:49:22 +02:00
|
|
|
TEST_CHECK(h.get_file_priorities() == prio);
|
2008-08-17 13:42:57 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 15:08:29 +01:00
|
|
|
void test_large_piece_size(int const size)
|
|
|
|
{
|
|
|
|
entry torrent;
|
|
|
|
entry& info = torrent["info"];
|
|
|
|
info["pieces"] = "aaaaaaaaaaaaaaaaaaaa";
|
|
|
|
info["name"] = "test";
|
|
|
|
info["piece length"] = size;
|
|
|
|
info["length"] = size;
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
|
|
|
add_torrent_params atp;
|
|
|
|
atp.ti = std::make_shared<torrent_info>(buf, from_span);
|
|
|
|
atp.save_path = ".";
|
|
|
|
|
|
|
|
lt::session ses;
|
|
|
|
auto h = ses.add_torrent(atp);
|
|
|
|
TEST_CHECK(h.status().errc == error_code(lt::errors::invalid_piece_size));
|
|
|
|
h.clear_error();
|
|
|
|
TEST_CHECK(h.status().errc == error_code(lt::errors::invalid_piece_size));
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-08-14 05:06:59 +02:00
|
|
|
TORRENT_TEST(long_names)
|
|
|
|
{
|
2019-11-24 15:08:29 +01:00
|
|
|
entry torrent;
|
|
|
|
entry& info = torrent["info"];
|
2015-08-14 05:06:59 +02:00
|
|
|
info["pieces"] = "aaaaaaaaaaaaaaaaaaaa";
|
2016-11-12 19:50:49 +01:00
|
|
|
info["name"] = "slightly shorter name, it's kind of sad that people started "
|
|
|
|
"the trend of incorrectly encoding the regular name field and then adding "
|
|
|
|
"another one with correct encoding";
|
|
|
|
info["name.utf-8"] = "this is a long ass name in order to try to make "
|
|
|
|
"make_magnet_uri overflow and hopefully crash. Although, by the time you "
|
|
|
|
"read this that particular bug should have been fixed";
|
2015-08-14 05:06:59 +02:00
|
|
|
info["piece length"] = 16 * 1024;
|
|
|
|
info["length"] = 3245;
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
2019-11-24 15:08:29 +01:00
|
|
|
auto ti = std::make_shared<torrent_info>(buf, from_span);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(large_piece_size)
|
|
|
|
{
|
|
|
|
test_large_piece_size(32768 * 16 * 1024);
|
|
|
|
test_large_piece_size(65536 * 16 * 1024);
|
|
|
|
test_large_piece_size(65537 * 16 * 1024);
|
2015-08-14 05:06:59 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 21:18:21 +02:00
|
|
|
TORRENT_TEST(total_wanted)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
|
|
|
fs.add_file("test_torrent_dir4/tmp1", 1024);
|
|
|
|
fs.add_file("test_torrent_dir4/tmp2", 1024);
|
|
|
|
fs.add_file("test_torrent_dir4/tmp3", 1024);
|
|
|
|
fs.add_file("test_torrent_dir4/tmp4", 1024);
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 1024);
|
2015-08-02 21:18:21 +02:00
|
|
|
std::vector<char> tmp;
|
|
|
|
bencode(std::back_inserter(tmp), t.generate());
|
2019-11-24 15:08:29 +01:00
|
|
|
auto info = std::make_shared<torrent_info>(tmp, from_span);
|
2015-08-02 21:18:21 +02:00
|
|
|
|
2016-08-21 18:15:19 +02:00
|
|
|
settings_pack pack = settings();
|
2015-08-02 21:18:21 +02:00
|
|
|
pack.set_int(settings_pack::alert_mask, alert::storage_notification);
|
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48130");
|
|
|
|
pack.set_int(settings_pack::max_retry_port_bind, 10);
|
|
|
|
lt::session ses(pack);
|
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
p.ti = info;
|
|
|
|
p.save_path = ".";
|
|
|
|
|
|
|
|
// we just want 1 out of 4 files, 1024 out of 4096 bytes
|
2017-10-29 00:44:40 +02:00
|
|
|
p.file_priorities.resize(4, 0_pri);
|
|
|
|
p.file_priorities[1] = 1_pri;
|
2015-08-02 21:18:21 +02:00
|
|
|
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p));
|
2015-08-02 21:18:21 +02:00
|
|
|
|
|
|
|
torrent_status st = h.status();
|
|
|
|
TEST_EQUAL(st.total_wanted, 1024);
|
|
|
|
TEST_EQUAL(st.total_wanted_done, 0);
|
2018-07-04 08:16:33 +02:00
|
|
|
|
2018-07-29 10:33:09 +02:00
|
|
|
// make sure that selecting and unseleting a file quickly still end up with
|
|
|
|
// the last set priority
|
2018-07-04 17:49:22 +02:00
|
|
|
h.file_priority(file_index_t{1}, default_priority);
|
|
|
|
h.file_priority(file_index_t{1}, dont_download);
|
|
|
|
TEST_CHECK(wait_priority(h, aux::vector<download_priority_t, file_index_t>(static_cast<std::size_t>(fs.num_files()))));
|
|
|
|
TEST_EQUAL(h.status({}).total_wanted, 0);
|
2015-08-02 21:18:21 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 18:12:32 +02:00
|
|
|
TORRENT_TEST(added_peers)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
|
|
|
fs.add_file("test_torrent_dir4/tmp1", 1024);
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 1024);
|
2016-05-27 18:12:32 +02:00
|
|
|
std::vector<char> tmp;
|
|
|
|
bencode(std::back_inserter(tmp), t.generate());
|
2019-11-24 15:08:29 +01:00
|
|
|
auto info = std::make_shared<torrent_info>(tmp, from_span);
|
2016-05-27 18:12:32 +02:00
|
|
|
|
2016-08-21 18:15:19 +02:00
|
|
|
settings_pack pack = settings();
|
2016-05-27 18:12:32 +02:00
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48130");
|
|
|
|
pack.set_int(settings_pack::max_retry_port_bind, 10);
|
|
|
|
lt::session ses(pack);
|
|
|
|
|
2017-10-14 16:40:48 +02:00
|
|
|
add_torrent_params p = parse_magnet_uri(
|
2019-11-24 15:08:29 +01:00
|
|
|
"magnet:?xt=urn:btih:abababababababababababababababababababab&x.pe=127.0.0.1:48081&x.pe=127.0.0.2:48082");
|
2016-05-27 18:12:32 +02:00
|
|
|
p.ti = info;
|
|
|
|
p.save_path = ".";
|
|
|
|
|
2017-03-26 20:54:16 +02:00
|
|
|
torrent_handle h = ses.add_torrent(std::move(p));
|
2016-05-27 18:12:32 +02:00
|
|
|
|
2017-07-16 21:25:07 +02:00
|
|
|
h.save_resume_data();
|
|
|
|
alert const* a = wait_for_alert(ses, save_resume_data_alert::alert_type);
|
|
|
|
|
|
|
|
TEST_CHECK(a);
|
|
|
|
save_resume_data_alert const* ra = alert_cast<save_resume_data_alert>(a);
|
|
|
|
TEST_CHECK(ra);
|
|
|
|
if (ra) TEST_EQUAL(ra->params.peers.size(), 2);
|
2016-05-27 18:12:32 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(torrent)
|
2008-08-17 13:42:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
/* {
|
2008-12-07 22:04:19 +01:00
|
|
|
remove("test_torrent_dir2/tmp1");
|
|
|
|
remove("test_torrent_dir2/tmp2");
|
|
|
|
remove("test_torrent_dir2/tmp3");
|
2008-08-17 13:42:57 +02:00
|
|
|
file_storage fs;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_size = 256 * 1024;
|
2008-11-30 07:10:37 +01:00
|
|
|
fs.add_file("test_torrent_dir2/tmp1", file_size);
|
|
|
|
fs.add_file("test_torrent_dir2/tmp2", file_size);
|
|
|
|
fs.add_file("test_torrent_dir2/tmp3", file_size);
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 128 * 1024);
|
2008-08-17 13:42:57 +02:00
|
|
|
t.add_tracker("http://non-existing.com/announce");
|
|
|
|
|
2013-10-20 10:06:18 +02:00
|
|
|
std::vector<char> piece(128 * 1024);
|
2008-08-17 13:42:57 +02:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
|
|
|
piece[i] = (i % 26) + 'A';
|
2015-05-18 07:04:55 +02:00
|
|
|
|
2008-08-17 13:42:57 +02:00
|
|
|
// calculate the hash for all pieces
|
2016-07-22 16:29:39 +02:00
|
|
|
sha1_hash ph = hasher(piece).final();
|
2008-08-17 13:42:57 +02:00
|
|
|
int num = t.num_pieces();
|
2008-12-07 22:04:19 +01:00
|
|
|
TEST_CHECK(t.num_pieces() > 0);
|
2008-08-17 13:42:57 +02:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
t.set_hash(i, ph);
|
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2008-08-17 13:42:57 +02:00
|
|
|
bencode(out, t.generate());
|
2009-12-15 14:11:07 +01:00
|
|
|
error_code ec;
|
2017-07-20 09:27:47 +02:00
|
|
|
auto info = std::make_shared<torrent_info>(tmp, std::ref(ec), from_span);
|
2008-12-07 22:04:19 +01:00
|
|
|
TEST_CHECK(info->num_pieces() > 0);
|
2008-08-17 13:42:57 +02:00
|
|
|
|
|
|
|
test_running_torrent(info, file_size);
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
*/
|
2008-08-17 13:42:57 +02:00
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
2016-04-26 06:37:47 +02:00
|
|
|
fs.add_file("test_torrent_dir2/tmp1", 1024);
|
2018-07-04 17:49:22 +02:00
|
|
|
lt::create_torrent t(fs, 1024, 6);
|
2008-08-17 13:42:57 +02:00
|
|
|
|
2018-07-04 08:16:33 +02:00
|
|
|
std::vector<char> piece(1024);
|
2016-04-26 06:37:47 +02:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2018-01-25 15:34:39 +01:00
|
|
|
piece[std::size_t(i)] = (i % 26) + 'A';
|
2016-04-26 06:37:47 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2018-07-04 17:49:22 +02:00
|
|
|
sha1_hash const ph = hasher(piece).final();
|
2016-04-26 06:37:47 +02:00
|
|
|
TEST_CHECK(t.num_pieces() > 0);
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.piece_range())
|
2016-04-26 06:37:47 +02:00
|
|
|
t.set_hash(i, ph);
|
|
|
|
|
2008-08-17 13:42:57 +02:00
|
|
|
std::vector<char> tmp;
|
2016-08-17 23:26:35 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2008-08-17 13:42:57 +02:00
|
|
|
bencode(out, t.generate());
|
2019-11-24 15:08:29 +01:00
|
|
|
auto info = std::make_shared<torrent_info>(tmp, from_span);
|
2016-04-26 06:37:47 +02:00
|
|
|
test_running_torrent(info, 1024);
|
2008-08-17 13:42:57 +02:00
|
|
|
}
|
2016-04-22 19:51:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 19:19:35 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2017-04-12 20:05:53 +02:00
|
|
|
struct test_plugin : lt::torrent_plugin {};
|
2016-06-23 19:19:35 +02:00
|
|
|
|
|
|
|
struct plugin_creator
|
|
|
|
{
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit plugin_creator(int& c) : m_called(c) {}
|
2016-06-23 19:19:35 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
std::shared_ptr<lt::torrent_plugin>
|
2016-06-23 19:19:35 +02:00
|
|
|
operator()(torrent_handle const&, void*)
|
|
|
|
{
|
|
|
|
++m_called;
|
2016-08-17 20:30:24 +02:00
|
|
|
return std::make_shared<test_plugin>();
|
2016-06-23 19:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int& m_called;
|
|
|
|
};
|
|
|
|
|
|
|
|
TORRENT_TEST(duplicate_is_not_error)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
|
|
|
fs.add_file("test_torrent_dir2/tmp1", 1024);
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 128 * 1024, 6);
|
2016-06-23 19:19:35 +02:00
|
|
|
|
|
|
|
std::vector<char> piece(128 * 1024);
|
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2018-01-25 15:34:39 +01:00
|
|
|
piece[std::size_t(i)] = (i % 26) + 'A';
|
2016-06-23 19:19:35 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2016-07-22 16:29:39 +02:00
|
|
|
sha1_hash ph = hasher(piece).final();
|
2016-06-23 19:19:35 +02:00
|
|
|
TEST_CHECK(t.num_pieces() > 0);
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.piece_range())
|
2016-06-23 19:19:35 +02:00
|
|
|
t.set_hash(i, ph);
|
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2016-06-23 19:19:35 +02:00
|
|
|
bencode(out, t.generate());
|
|
|
|
|
|
|
|
int called = 0;
|
|
|
|
plugin_creator creator(called);
|
|
|
|
|
|
|
|
add_torrent_params p;
|
2019-11-24 15:08:29 +01:00
|
|
|
p.ti = std::make_shared<torrent_info>(tmp, from_span);
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
|
|
|
p.flags &= ~torrent_flags::duplicate_is_error;
|
2016-06-23 19:19:35 +02:00
|
|
|
p.save_path = ".";
|
|
|
|
p.extensions.push_back(creator);
|
|
|
|
|
2016-08-21 18:15:19 +02:00
|
|
|
lt::session ses(settings());
|
2016-06-23 19:19:35 +02:00
|
|
|
ses.async_add_torrent(p);
|
2017-03-26 20:54:16 +02:00
|
|
|
ses.async_add_torrent(std::move(p));
|
2016-06-23 19:19:35 +02:00
|
|
|
|
|
|
|
wait_for_downloading(ses, "ses");
|
|
|
|
|
|
|
|
// we should only have added the plugin once
|
|
|
|
TEST_EQUAL(called, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-22 19:51:06 +02:00
|
|
|
TORRENT_TEST(torrent_total_size_zero)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
|
|
|
fs.add_file("test_torrent_dir2/tmp1", 0);
|
2016-04-26 06:37:47 +02:00
|
|
|
TEST_CHECK(fs.num_files() == 1);
|
|
|
|
TEST_CHECK(fs.total_size() == 0);
|
2008-08-17 13:42:57 +02:00
|
|
|
|
2019-11-24 15:08:29 +01:00
|
|
|
error_code ec;
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t1(fs);
|
2016-04-26 06:37:47 +02:00
|
|
|
set_piece_hashes(t1, ".", ec);
|
|
|
|
TEST_CHECK(ec);
|
|
|
|
|
|
|
|
fs.add_file("test_torrent_dir2/tmp2", 0);
|
|
|
|
TEST_CHECK(fs.num_files() == 2);
|
|
|
|
TEST_CHECK(fs.total_size() == 0);
|
|
|
|
|
|
|
|
ec.clear();
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t2(fs);
|
2016-04-26 06:37:47 +02:00
|
|
|
set_piece_hashes(t2, ".", ec);
|
2016-04-22 19:51:06 +02:00
|
|
|
TEST_CHECK(ec);
|
2008-05-27 10:07:59 +02:00
|
|
|
}
|
|
|
|
|
2016-02-27 16:20:23 +01:00
|
|
|
TORRENT_TEST(rename_file)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
2016-04-30 05:57:26 +02:00
|
|
|
fs.add_file("test3/tmp1", 20);
|
|
|
|
fs.add_file("test3/tmp2", 20);
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 128 * 1024, 6);
|
2016-02-27 16:20:23 +01:00
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-08-17 23:26:35 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2016-02-27 16:20:23 +01:00
|
|
|
bencode(out, t.generate());
|
2019-11-24 15:08:29 +01:00
|
|
|
auto info = std::make_shared<torrent_info>(tmp, from_span);
|
2016-02-27 16:20:23 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(info->files().file_path(file_index_t(0)), combine_path("test3","tmp1"));
|
2016-02-27 16:20:23 +01:00
|
|
|
|
|
|
|
// move "test3/tmp1" -> "tmp1"
|
2016-12-22 16:42:33 +01:00
|
|
|
info->rename_file(file_index_t(0), "tmp1");
|
2016-02-27 16:20:23 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(info->files().file_path(file_index_t(0)), "tmp1");
|
2016-02-27 16:20:23 +01:00
|
|
|
}
|
2016-11-02 06:01:04 +01:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-03-23 23:19:40 +01:00
|
|
|
TORRENT_TEST(async_load_deprecated)
|
2016-11-02 06:01:04 +01:00
|
|
|
{
|
|
|
|
settings_pack pack = settings();
|
|
|
|
lt::session ses(pack);
|
|
|
|
|
|
|
|
add_torrent_params p;
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2016-11-02 06:01:04 +01:00
|
|
|
std::string dir = parent_path(current_working_directory());
|
|
|
|
|
|
|
|
p.url = "file://" + combine_path(combine_path(dir, "test_torrents"), "base.torrent");
|
|
|
|
p.save_path = ".";
|
2017-03-26 20:54:16 +02:00
|
|
|
ses.async_add_torrent(std::move(p));
|
2016-11-02 06:01:04 +01:00
|
|
|
|
|
|
|
alert const* a = wait_for_alert(ses, add_torrent_alert::alert_type);
|
|
|
|
TEST_CHECK(a);
|
|
|
|
if (a == nullptr) return;
|
|
|
|
auto const* ta = alert_cast<add_torrent_alert const>(a);
|
|
|
|
TEST_CHECK(ta);
|
|
|
|
if (ta == nullptr) return;
|
|
|
|
TEST_CHECK(!ta->error);
|
|
|
|
TEST_CHECK(ta->params.ti->name() == "temp");
|
|
|
|
}
|
2017-03-23 23:19:40 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_TEST(torrent_status)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(static_cast<int>(torrent_status::error_file_none), -1);
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(static_cast<int>(torrent_status::error_file_url), -2);
|
|
|
|
TEST_EQUAL(static_cast<int>(torrent_status::error_file_metadata), -4);
|
2018-03-13 01:15:08 +01:00
|
|
|
#endif
|
|
|
|
TEST_EQUAL(static_cast<int>(torrent_status::error_file_ssl_ctx), -3);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(static_cast<int>(torrent_status::error_file_exception), -5);
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void test_queue(add_torrent_params)
|
2017-01-31 03:31:31 +01:00
|
|
|
{
|
|
|
|
lt::settings_pack pack = settings();
|
|
|
|
// we're not testing the hash check, just accept the data we write
|
|
|
|
pack.set_bool(settings_pack::disable_hash_checks, true);
|
|
|
|
lt::session ses(pack);
|
|
|
|
|
|
|
|
std::vector<torrent_handle> torrents;
|
|
|
|
for(int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
std::stringstream file_path;
|
|
|
|
file_path << "test_torrent_dir4/queue" << i;
|
|
|
|
fs.add_file(file_path.str(), 1024);
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 128 * 1024, 6);
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2017-07-20 09:27:47 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>(buf, from_span);
|
2017-01-31 03:31:31 +01:00
|
|
|
add_torrent_params p;
|
|
|
|
p.ti = ti;
|
|
|
|
p.save_path = ".";
|
2017-03-26 20:54:16 +02:00
|
|
|
torrents.push_back(ses.add_torrent(std::move(p)));
|
2017-01-31 03:31:31 +01:00
|
|
|
}
|
|
|
|
|
2018-03-13 20:13:56 +01:00
|
|
|
print_alerts(ses, "ses");
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
std::vector<download_priority_t> pieces(
|
|
|
|
std::size_t(torrents[5].torrent_file()->num_pieces()), 0_pri);
|
2017-02-03 04:58:33 +01:00
|
|
|
torrents[5].prioritize_pieces(pieces);
|
2017-01-31 03:31:31 +01:00
|
|
|
torrent_handle finished = torrents[5];
|
|
|
|
|
2018-03-13 20:13:56 +01:00
|
|
|
wait_for_alert(ses, torrent_finished_alert::alert_type, "ses");
|
|
|
|
|
2017-01-31 03:31:31 +01:00
|
|
|
// add_torrent should be ordered
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
// test top and bottom
|
|
|
|
torrents[2].queue_position_top();
|
|
|
|
torrents[1].queue_position_bottom();
|
|
|
|
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
// test set pos
|
2017-10-25 02:39:22 +02:00
|
|
|
torrents[0].queue_position_set(queue_position_t{0});
|
|
|
|
torrents[1].queue_position_set(queue_position_t{1});
|
2017-01-31 03:31:31 +01:00
|
|
|
// torrent 2 should be get moved down by 0 and 1 to pos 2
|
|
|
|
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
//test strange up and down commands
|
|
|
|
torrents[0].queue_position_up();
|
|
|
|
torrents[4].queue_position_down();
|
|
|
|
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
torrents[1].queue_position_up();
|
|
|
|
torrents[3].queue_position_down();
|
|
|
|
finished.queue_position_up();
|
|
|
|
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
torrents[1].queue_position_down();
|
|
|
|
torrents[3].queue_position_up();
|
|
|
|
finished.queue_position_down();
|
|
|
|
|
|
|
|
|
2017-10-25 02:39:22 +02:00
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
|
|
|
|
// test set pos on not existing pos
|
2017-10-25 02:39:22 +02:00
|
|
|
torrents[3].queue_position_set(queue_position_t{10});
|
|
|
|
finished.queue_position_set(queue_position_t{10});
|
|
|
|
|
|
|
|
TEST_EQUAL(finished.queue_position(), no_pos);
|
|
|
|
TEST_EQUAL(torrents[0].queue_position(), queue_position_t{0});
|
|
|
|
TEST_EQUAL(torrents[1].queue_position(), queue_position_t{1});
|
|
|
|
TEST_EQUAL(torrents[2].queue_position(), queue_position_t{2});
|
|
|
|
TEST_EQUAL(torrents[4].queue_position(), queue_position_t{3});
|
|
|
|
TEST_EQUAL(torrents[3].queue_position(), queue_position_t{4});
|
2017-01-31 03:31:31 +01:00
|
|
|
}
|
2008-05-27 10:07:59 +02:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-06-25 17:56:19 +02:00
|
|
|
TORRENT_TEST(queue)
|
|
|
|
{
|
|
|
|
test_queue(add_torrent_params());
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(queue_paused)
|
|
|
|
{
|
|
|
|
add_torrent_params p;
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags |= torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2017-06-25 17:56:19 +02:00
|
|
|
test_queue(p);
|
|
|
|
}
|
|
|
|
|
2017-06-15 03:34:31 +02:00
|
|
|
TORRENT_TEST(test_move_storage_no_metadata)
|
|
|
|
{
|
|
|
|
lt::session ses(settings());
|
2019-11-24 15:08:29 +01:00
|
|
|
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababababababababababababababababababab");
|
2017-10-14 16:40:48 +02:00
|
|
|
p.save_path = "save_path";
|
2017-06-15 03:34:31 +02:00
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
|
|
|
TEST_EQUAL(h.status().save_path, complete("save_path"));
|
|
|
|
|
|
|
|
h.move_storage("save_path_1");
|
|
|
|
|
|
|
|
TEST_EQUAL(h.status().save_path, complete("save_path_1"));
|
|
|
|
}
|
2018-07-23 08:43:56 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(test_have_piece_no_metadata)
|
|
|
|
{
|
|
|
|
lt::session ses(settings());
|
2019-11-24 15:08:29 +01:00
|
|
|
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababababababababababababababababababab");
|
2018-07-23 08:43:56 +02:00
|
|
|
p.save_path = "save_path";
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{-1}), false);
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{0}), false);
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{100}), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_have_piece_out_of_range)
|
|
|
|
{
|
|
|
|
lt::session ses(settings());
|
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
static std::array<const int, 2> const file_sizes{{100000, 100000}};
|
|
|
|
int const piece_size = 0x8000;
|
|
|
|
p.ti = make_torrent(file_sizes, piece_size);
|
|
|
|
p.save_path = "save_path";
|
|
|
|
p.flags |= torrent_flags::seed_mode;
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{-1}), false);
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{0}), true);
|
|
|
|
TEST_EQUAL(h.have_piece(piece_index_t{100}), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_read_piece_no_metadata)
|
|
|
|
{
|
|
|
|
lt::session ses(settings());
|
2019-11-24 15:08:29 +01:00
|
|
|
add_torrent_params p = parse_magnet_uri("magnet:?xt=urn:btih:abababababababababababababababababababab");
|
2018-07-23 08:43:56 +02:00
|
|
|
p.save_path = "save_path";
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
|
|
|
h.read_piece(piece_index_t{-1});
|
|
|
|
|
|
|
|
alert const* a = wait_for_alert(ses, read_piece_alert::alert_type, "read_piece_alert");
|
|
|
|
TEST_CHECK(a);
|
|
|
|
if (auto* rp = alert_cast<read_piece_alert>(a))
|
|
|
|
{
|
|
|
|
TEST_CHECK(rp->error == error_code(lt::errors::no_metadata, lt::libtorrent_category()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_read_piece_out_of_range)
|
|
|
|
{
|
|
|
|
lt::session ses(settings());
|
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
static std::array<const int, 2> const file_sizes{{100000, 100000}};
|
|
|
|
int const piece_size = 0x8000;
|
|
|
|
p.ti = make_torrent(file_sizes, piece_size);
|
|
|
|
p.save_path = "save_path";
|
|
|
|
p.flags |= torrent_flags::seed_mode;
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
|
|
|
h.read_piece(piece_index_t{-1});
|
|
|
|
|
|
|
|
alert const* a = wait_for_alert(ses, read_piece_alert::alert_type, "read_piece_alert");
|
|
|
|
TEST_CHECK(a);
|
|
|
|
if (auto* rp = alert_cast<read_piece_alert>(a))
|
|
|
|
{
|
|
|
|
TEST_CHECK(rp->error == error_code(lt::errors::invalid_piece_index
|
|
|
|
, lt::libtorrent_category()));
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 22:23:13 +02:00
|
|
|
|
|
|
|
namespace {
|
2018-09-07 04:09:09 +02:00
|
|
|
int const piece_size = 0x4000 * 128;
|
2018-08-07 22:23:13 +02:00
|
|
|
|
|
|
|
file_storage test_fs()
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
fs.set_piece_length(piece_size);
|
2018-09-07 04:09:09 +02:00
|
|
|
fs.add_file("temp", 99999999999);
|
2018-08-07 22:23:13 +02:00
|
|
|
fs.set_num_pieces(int((fs.total_size() + piece_size - 1) / piece_size));
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_pieces)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{2, 0, false}), 2 * piece_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_pieces_last)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{2, 0, true}), piece_size + fs.total_size() % piece_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_no_pieces)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{0, 0, false}), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_all_pieces)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{fs.num_pieces(), 0, true}), fs.total_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_all_pieces_one_pad)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{fs.num_pieces(), 1, true}), fs.total_size() - 0x4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(test_calc_bytes_all_pieces_two_pad)
|
|
|
|
{
|
|
|
|
auto const fs = test_fs();
|
|
|
|
TEST_EQUAL(calc_bytes(fs, piece_count{fs.num_pieces(), 2, true}), fs.total_size() - 2 * 0x4000);
|
|
|
|
}
|
2019-02-26 14:44:16 +01:00
|
|
|
|
|
|
|
#if TORRENT_HAS_SYMLINK
|
|
|
|
TORRENT_TEST(symlinks_restore)
|
|
|
|
{
|
|
|
|
// downloading test torrent with symlinks
|
|
|
|
std::string const work_dir = current_working_directory();
|
|
|
|
lt::add_torrent_params p;
|
|
|
|
p.ti = std::make_shared<lt::torrent_info>(combine_path(
|
|
|
|
combine_path(parent_path(work_dir), "test_torrents"), "symlink2.torrent"));
|
|
|
|
p.flags &= ~lt::torrent_flags::paused;
|
|
|
|
p.save_path = work_dir;
|
|
|
|
settings_pack pack = settings();
|
|
|
|
pack.set_int(libtorrent::settings_pack::alert_mask, libtorrent::alert::status_notification | libtorrent::alert::error_notification);
|
|
|
|
lt::session ses(std::move(pack));
|
|
|
|
ses.add_torrent(p);
|
|
|
|
|
|
|
|
wait_for_alert(ses, torrent_checked_alert::alert_type, "torrent_checked_alert");
|
|
|
|
|
|
|
|
std::string const f = combine_path(combine_path(work_dir, "Some.framework"), "SDL2");
|
2019-03-10 21:56:08 +01:00
|
|
|
TEST_CHECK(aux::get_file_attributes(f) & file_storage::flag_symlink);
|
|
|
|
TEST_CHECK(aux::get_symlink_path(f) == "Versions/A/SDL2");
|
2019-02-26 14:44:16 +01:00
|
|
|
}
|
|
|
|
#endif
|