2013-11-24 02:32:51 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2013, 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 "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "libtorrent/create_torrent.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "libtorrent/hex.hpp" // to_hex
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2013-11-24 02:32:51 +01:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
namespace {
|
|
|
|
|
2013-11-24 02:32:51 +01:00
|
|
|
enum flags_t
|
|
|
|
{
|
|
|
|
seed_mode = 1,
|
|
|
|
time_critical = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
void test_read_piece(int flags)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2013-11-24 02:32:51 +01:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("==== TEST READ PIECE =====\n");
|
2013-11-24 02:32:51 +01:00
|
|
|
|
|
|
|
// in case the previous run was terminated
|
|
|
|
error_code ec;
|
|
|
|
remove_all("tmp1_read_piece", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("ERROR: removing tmp1_read_piece: (%d) %s\n"
|
2013-11-24 02:32:51 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
|
|
|
|
create_directory("tmp1_read_piece", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("ERROR: creating directory tmp1_read_piece: (%d) %s\n"
|
2013-11-24 02:32:51 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
|
|
|
|
create_directory(combine_path("tmp1_read_piece", "test_torrent"), ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("ERROR: creating directory test_torrent: (%d) %s\n"
|
2013-11-24 02:32:51 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
2015-06-05 08:31:52 +02:00
|
|
|
|
2013-11-24 02:32:51 +01:00
|
|
|
file_storage fs;
|
|
|
|
std::srand(10);
|
|
|
|
int piece_size = 0x4000;
|
|
|
|
|
2018-06-10 13:31:26 +02:00
|
|
|
static std::array<const int, 2> const file_sizes{{ 100000, 10000 }};
|
2013-11-24 02:32:51 +01:00
|
|
|
|
2018-06-10 13:31:26 +02:00
|
|
|
create_random_files(combine_path("tmp1_read_piece", "test_torrent"), file_sizes);
|
2013-11-24 02:32:51 +01:00
|
|
|
|
|
|
|
add_files(fs, combine_path("tmp1_read_piece", "test_torrent"));
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, piece_size, 0x4000);
|
2013-11-24 02:32:51 +01:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
set_piece_hashes(t, "tmp1_read_piece", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("ERROR: set_piece_hashes: (%d) %s\n"
|
2013-11-24 02:32:51 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
|
|
|
|
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, ec, from_span);
|
2013-11-24 02:32:51 +01:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("generated torrent: %s tmp1_read_piece/test_torrent\n"
|
2016-07-29 08:36:15 +02:00
|
|
|
, aux::to_hex(ti->info_hash()).c_str());
|
2013-11-24 02:32:51 +01:00
|
|
|
|
2018-03-30 12:50:31 +02:00
|
|
|
auto const mask = ~(
|
|
|
|
alert::performance_warning
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2018-03-30 12:50:31 +02:00
|
|
|
| alert::progress_notification
|
|
|
|
#endif
|
2015-06-14 21:48:11 +02:00
|
|
|
| alert::stats_notification);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
settings_pack sett;
|
2015-06-14 21:48:11 +02:00
|
|
|
sett.set_bool(settings_pack::enable_lsd, false);
|
|
|
|
sett.set_bool(settings_pack::enable_natpmp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_upnp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_dht, false);
|
|
|
|
sett.set_int(settings_pack::alert_mask, mask);
|
2015-06-05 08:31:52 +02:00
|
|
|
sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:48000");
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_int(settings_pack::alert_mask, alert::all_categories);
|
2015-06-05 08:31:52 +02:00
|
|
|
lt::session ses(sett);
|
2013-11-24 02:32:51 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2013-11-24 02:32:51 +01:00
|
|
|
p.save_path = "tmp1_read_piece";
|
|
|
|
p.ti = ti;
|
2017-07-09 16:32:01 +02:00
|
|
|
if (flags & flags_t::seed_mode)
|
|
|
|
p.flags |= torrent_flags::seed_mode;
|
2013-11-24 02:32:51 +01:00
|
|
|
torrent_handle tor1 = ses.add_torrent(p, ec);
|
2016-12-21 07:00:49 +01:00
|
|
|
if (ec) std::printf("ERROR: add_torrent: (%d) %s\n"
|
|
|
|
, ec.value(), ec.message().c_str());
|
2013-11-24 02:32:51 +01:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
TEST_CHECK(tor1.is_valid());
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert const* a = wait_for_alert(ses, torrent_finished_alert::alert_type, "ses");
|
|
|
|
TEST_CHECK(a);
|
2013-11-24 02:32:51 +01:00
|
|
|
|
|
|
|
TEST_CHECK(tor1.status().is_seeding);
|
|
|
|
|
|
|
|
if (flags & time_critical)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
tor1.set_piece_deadline(piece_index_t(1), 0, torrent_handle::alert_when_available);
|
2013-11-24 02:32:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
tor1.read_piece(piece_index_t(1));
|
2013-11-24 02:32:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a = wait_for_alert(ses, read_piece_alert::alert_type, "ses");
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
TEST_CHECK(a);
|
|
|
|
if (a)
|
2013-11-24 02:32:51 +01:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
read_piece_alert const* rp = alert_cast<read_piece_alert>(a);
|
2013-11-24 02:32:51 +01:00
|
|
|
TEST_CHECK(rp);
|
|
|
|
if (rp)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(rp->piece, piece_index_t(1));
|
2013-11-24 02:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_all("tmp1_read_piece", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("ERROR: removing tmp1_read_piece: (%d) %s\n"
|
2013-11-24 02:32:51 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(read_piece)
|
2013-11-24 02:32:51 +01:00
|
|
|
{
|
|
|
|
test_read_piece(0);
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(seed_mode)
|
|
|
|
{
|
2013-11-24 02:32:51 +01:00
|
|
|
test_read_piece(seed_mode);
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(time_critical)
|
|
|
|
{
|
2013-11-24 02:32:51 +01:00
|
|
|
test_read_piece(time_critical);
|
|
|
|
}
|