2017-04-15 00:51:11 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2017, 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/torrent_handle.hpp"
|
|
|
|
#include "libtorrent/torrent_status.hpp"
|
|
|
|
#include "libtorrent/session_settings.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2017-04-21 07:21:31 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "settings.hpp"
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2017-04-21 07:21:31 +02:00
|
|
|
#include <cstdint>
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
namespace lt = libtorrent;
|
2017-04-21 07:21:31 +02:00
|
|
|
using std::ignore;
|
2017-04-15 00:51:11 +02:00
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2017-04-15 00:51:11 +02:00
|
|
|
enum test_case {
|
|
|
|
complete_download,
|
|
|
|
partial_download,
|
|
|
|
mid_download
|
|
|
|
};
|
|
|
|
|
2017-07-27 22:26:12 +02:00
|
|
|
void test_remove_torrent(remove_flags_t const remove_options
|
2017-04-15 00:51:11 +02:00
|
|
|
, test_case const test = complete_download)
|
|
|
|
{
|
|
|
|
// this allows shutting down the sessions in parallel
|
|
|
|
std::vector<session_proxy> sp;
|
|
|
|
settings_pack pack = settings();
|
|
|
|
|
|
|
|
// we do this to force pieces to be evicted into the ghost lists
|
|
|
|
pack.set_int(settings_pack::cache_size, 10);
|
|
|
|
|
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48075");
|
|
|
|
lt::session ses1(pack);
|
|
|
|
|
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:49075");
|
|
|
|
lt::session ses2(pack);
|
|
|
|
|
|
|
|
torrent_handle tor1;
|
|
|
|
torrent_handle tor2;
|
|
|
|
|
|
|
|
int const num_pieces = (test == mid_download) ? 500 : 100;
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
remove_all("tmp1_remove", ec);
|
|
|
|
remove_all("tmp2_remove", ec);
|
|
|
|
create_directory("tmp1_remove", ec);
|
|
|
|
std::ofstream file("tmp1_remove/temporary");
|
2017-04-21 07:21:31 +02:00
|
|
|
std::shared_ptr<torrent_info> t = ::create_torrent(&file, "temporary"
|
2017-04-15 00:51:11 +02:00
|
|
|
, 16 * 1024, num_pieces, false);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
wait_for_listen(ses1, "ses1");
|
2018-02-18 23:28:41 +01:00
|
|
|
wait_for_listen(ses2, "ses2");
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
// test using piece sizes smaller than 16kB
|
2018-03-22 17:01:38 +01:00
|
|
|
std::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, nullptr
|
|
|
|
, true, false, true, "_remove", 8 * 1024, &t, false, nullptr);
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
if (test == partial_download)
|
|
|
|
{
|
2018-01-19 22:40:39 +01:00
|
|
|
std::vector<download_priority_t> priorities(std::size_t(num_pieces), low_priority);
|
2017-04-15 00:51:11 +02:00
|
|
|
// set half of the pieces to priority 0
|
2017-10-29 00:44:40 +02:00
|
|
|
std::fill(priorities.begin(), priorities.begin() + (num_pieces / 2), dont_download);
|
2017-04-15 00:51:11 +02:00
|
|
|
tor2.prioritize_pieces(priorities);
|
|
|
|
}
|
2018-02-18 23:28:41 +01:00
|
|
|
else if (test == mid_download)
|
|
|
|
{
|
|
|
|
tor1.set_upload_limit(static_cast<int>(t->total_size()));
|
|
|
|
tor2.set_download_limit(static_cast<int>(t->total_size()));
|
|
|
|
}
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
torrent_status st1;
|
|
|
|
torrent_status st2;
|
|
|
|
|
|
|
|
for (int i = 0; i < 200; ++i)
|
|
|
|
{
|
2017-04-21 07:21:31 +02:00
|
|
|
print_alerts(ses1, "ses1", true, true);
|
|
|
|
print_alerts(ses2, "ses2", true, true);
|
2017-04-15 00:51:11 +02:00
|
|
|
|
|
|
|
st1 = tor1.status();
|
|
|
|
st2 = tor2.status();
|
|
|
|
|
|
|
|
if (test == mid_download && st2.num_pieces > num_pieces / 2)
|
|
|
|
{
|
|
|
|
TEST_CHECK(st2.is_finished == false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (st2.is_finished) break;
|
|
|
|
|
|
|
|
TEST_CHECK(st1.state == torrent_status::seeding
|
2017-11-28 22:59:46 +01:00
|
|
|
|| st1.state == torrent_status::checking_resume_data
|
2017-04-15 00:51:11 +02:00
|
|
|
|| st1.state == torrent_status::checking_files);
|
|
|
|
TEST_CHECK(st2.state == torrent_status::downloading
|
|
|
|
|| st2.state == torrent_status::checking_resume_data);
|
|
|
|
|
2017-11-28 22:59:46 +01:00
|
|
|
// if nothing is being transferred after 3 seconds, we're failing the test
|
2018-09-03 09:54:03 +02:00
|
|
|
if (st1.total_payload_upload == 0 && i > 30)
|
2017-04-15 00:51:11 +02:00
|
|
|
{
|
|
|
|
TEST_ERROR("no transfer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:21:31 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2017-04-15 00:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CHECK(st1.num_pieces > 0);
|
|
|
|
TEST_CHECK(st2.num_pieces > 0);
|
|
|
|
|
|
|
|
ses2.remove_torrent(tor2, remove_options);
|
|
|
|
ses1.remove_torrent(tor1, remove_options);
|
|
|
|
|
|
|
|
std::cerr << "removed" << std::endl;
|
|
|
|
|
|
|
|
for (int i = 0; tor2.is_valid() || tor1.is_valid(); ++i)
|
|
|
|
{
|
2017-04-21 07:21:31 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2020-03-01 00:01:20 +01:00
|
|
|
if (++i > 400)
|
2017-04-15 00:51:11 +02:00
|
|
|
{
|
|
|
|
std::cerr << "torrent handle(s) still valid: "
|
|
|
|
<< (tor1.is_valid() ? "tor1 " : "")
|
|
|
|
<< (tor2.is_valid() ? "tor2 " : "")
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
TEST_ERROR("handle did not become invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_options & session::delete_files)
|
|
|
|
{
|
|
|
|
TEST_CHECK(!exists("tmp1_remove/temporary"));
|
|
|
|
TEST_CHECK(!exists("tmp2_remove/temporary"));
|
|
|
|
}
|
|
|
|
|
|
|
|
sp.push_back(ses1.abort());
|
|
|
|
sp.push_back(ses2.abort());
|
|
|
|
}
|
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-04-15 00:51:11 +02:00
|
|
|
TORRENT_TEST(remove_torrent)
|
|
|
|
{
|
2017-07-27 22:26:12 +02:00
|
|
|
test_remove_torrent({});
|
2017-04-15 00:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(remove_torrent_and_files)
|
|
|
|
{
|
|
|
|
test_remove_torrent(session::delete_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(remove_torrent_partial)
|
|
|
|
{
|
2017-07-27 22:26:12 +02:00
|
|
|
test_remove_torrent({}, partial_download);
|
2017-04-15 00:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(remove_torrent_and_files_partial)
|
|
|
|
{
|
|
|
|
test_remove_torrent(session::delete_files, partial_download);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(remove_torrent_mid_download)
|
|
|
|
{
|
2017-07-27 22:26:12 +02:00
|
|
|
test_remove_torrent({}, mid_download);
|
2017-04-15 00:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(remove_torrent_and_files_mid_download)
|
|
|
|
{
|
|
|
|
test_remove_torrent(session::delete_files, mid_download);
|
|
|
|
}
|
|
|
|
|
|
|
|
|