2014-03-05 10:37:49 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 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 "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2014-03-05 10:37:49 +01:00
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "test.hpp"
|
2016-07-15 20:35:57 +02:00
|
|
|
#include "settings.hpp"
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-05-29 18:58:16 +02:00
|
|
|
#include <iostream>
|
2014-03-05 10:37:49 +01:00
|
|
|
#include <fstream>
|
2016-05-30 20:33:24 +02:00
|
|
|
#include <iostream>
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <tuple>
|
2014-03-05 10:37:49 +01:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace lt = libtorrent;
|
2016-06-20 17:32:06 +02:00
|
|
|
using std::ignore;
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
bool all_of(std::vector<bool> const& v)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
return std::all_of(v.begin(), v.end(), [](bool v){ return v; });
|
2014-03-05 10:37:49 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
void test_remap_files(storage_mode_t storage_mode = storage_mode_sparse)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
using namespace libtorrent;
|
2015-07-16 07:38:48 +02:00
|
|
|
|
2014-03-05 10:37:49 +01:00
|
|
|
// in case the previous run was terminated
|
|
|
|
error_code ec;
|
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// create a torrent with 2 files, remap them into 3 files and make sure
|
|
|
|
// the file priorities don't break things
|
|
|
|
static const int file_sizes[] = {100000, 100000};
|
|
|
|
const int num_files = 2;
|
|
|
|
const int piece_size = 0x8000;
|
|
|
|
auto t = make_torrent(file_sizes, num_files, piece_size);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
static const int remap_file_sizes[] = {10000, 10000, int(t->total_size() - 20000)};
|
|
|
|
int const num_new_files = 3;
|
2015-01-05 13:15:16 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
file_storage fs = make_file_storage(remap_file_sizes, num_new_files
|
|
|
|
, piece_size, "multifile-");
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
t->remap_files(fs);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
|
|
|
int const alert_mask = alert::all_categories
|
|
|
|
& ~alert::stats_notification;
|
|
|
|
|
2015-01-05 13:15:16 +01:00
|
|
|
session_proxy p1;
|
2015-06-05 08:31:52 +02:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
settings_pack sett = settings();
|
2015-06-05 08:31:52 +02:00
|
|
|
sett.set_int(settings_pack::alert_mask, alert_mask);
|
2016-07-15 20:35:57 +02:00
|
|
|
lt::session ses(sett);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
|
|
|
add_torrent_params params;
|
2016-07-15 20:35:57 +02:00
|
|
|
params.save_path = ".";
|
2014-03-05 10:37:49 +01:00
|
|
|
params.storage_mode = storage_mode;
|
|
|
|
params.flags &= ~add_torrent_params::flag_paused;
|
|
|
|
params.flags &= ~add_torrent_params::flag_auto_managed;
|
2016-07-15 20:35:57 +02:00
|
|
|
params.ti = t;
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
torrent_handle tor1 = ses.add_torrent(params);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// write pieces
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < fs.end_piece(); ++i)
|
2016-07-15 20:35:57 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
std::vector<char> const piece = generate_piece(i, fs.piece_size(i));
|
2016-07-15 20:35:57 +02:00
|
|
|
tor1.add_piece(i, piece.data());
|
|
|
|
}
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// read pieces
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < fs.end_piece(); ++i)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
tor1.read_piece(i);
|
|
|
|
}
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// wait for all alerts to come back and verify the data against the expected
|
|
|
|
// piece adata
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<bool, piece_index_t> pieces(fs.num_pieces(), false);
|
|
|
|
aux::vector<bool, piece_index_t> passed(fs.num_pieces(), false);
|
|
|
|
aux::vector<bool, file_index_t> files(fs.num_files(), false);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
while (!all_of(pieces) || !all_of(passed) || !all_of(files))
|
|
|
|
{
|
|
|
|
alert* a = ses.wait_for_alert(lt::seconds(5));
|
|
|
|
if (a == nullptr) break;
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
std::vector<alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
for (alert* i : alerts)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
printf("%s\n", i->message().c_str());
|
|
|
|
|
|
|
|
read_piece_alert* rp = alert_cast<read_piece_alert>(i);
|
|
|
|
if (rp)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
auto const idx = rp->piece;
|
2016-07-15 20:35:57 +02:00
|
|
|
TEST_EQUAL(t->piece_size(idx), rp->size);
|
|
|
|
|
|
|
|
std::vector<char> const piece = generate_piece(idx, t->piece_size(idx));
|
|
|
|
TEST_CHECK(memcmp(rp->buffer.get(), piece.data(), rp->size) == 0);
|
|
|
|
TEST_CHECK(pieces[idx] == false);
|
|
|
|
pieces[idx] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_completed_alert* fc = alert_cast<file_completed_alert>(i);
|
|
|
|
if (fc)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
auto const idx = fc->index;
|
2016-07-15 20:35:57 +02:00
|
|
|
TEST_CHECK(files[idx] == false);
|
|
|
|
files[idx] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
piece_finished_alert* pf = alert_cast<piece_finished_alert>(i);
|
|
|
|
if (pf)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
auto const idx = pf->piece_index;
|
2016-07-15 20:35:57 +02:00
|
|
|
TEST_CHECK(passed[idx] == false);
|
|
|
|
passed[idx] = true;
|
|
|
|
}
|
2014-03-05 10:37:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
TEST_CHECK(all_of(pieces));
|
|
|
|
TEST_CHECK(all_of(files));
|
|
|
|
TEST_CHECK(all_of(passed));
|
2014-03-05 10:37:49 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// just because we can read them back throught libtorrent, doesn't mean the
|
|
|
|
// files have hit disk yet (because of the cache). Retry a few times to try
|
|
|
|
// to pick up the files
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < file_index_t(num_new_files); ++i)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
std::string name = fs.file_path(i);
|
|
|
|
for (int j = 0; j < 10 && !exists(name); ++j)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
|
|
|
print_alerts(ses, "ses");
|
2014-03-05 10:37:49 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", name.c_str());
|
2016-07-15 20:35:57 +02:00
|
|
|
TEST_CHECK(exists(name));
|
2014-03-05 10:37:49 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
print_alerts(ses, "ses");
|
2014-03-10 22:32:48 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
torrent_status st = tor1.status();
|
|
|
|
TEST_EQUAL(st.is_seeding, true);
|
2014-03-10 22:32:48 +01:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("\ntesting force recheck\n\n");
|
2014-03-13 09:00:53 +01:00
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
// test force rechecking a seeding torrent with remapped files
|
|
|
|
tor1.force_recheck();
|
2014-03-10 22:32:48 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 50; ++i)
|
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
torrent_status st = tor1.status();
|
|
|
|
if (st.is_seeding) break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2016-07-15 20:35:57 +02:00
|
|
|
print_alerts(ses, "ses");
|
2014-03-10 22:32:48 +01:00
|
|
|
}
|
|
|
|
|
2016-07-15 20:35:57 +02:00
|
|
|
print_alerts(ses, "ses");
|
|
|
|
st = tor1.status();
|
|
|
|
TEST_CHECK(st.is_seeding);
|
2014-03-10 22:32:48 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(remap_files)
|
2014-03-05 10:37:49 +01:00
|
|
|
{
|
2016-07-15 20:35:57 +02:00
|
|
|
test_remap_files();
|
2014-03-05 10:37:49 +01:00
|
|
|
}
|
|
|
|
|