2013-01-06 05:53:26 +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"
|
2013-09-09 09:06:12 +02:00
|
|
|
#include <sys/stat.h> // for chmod
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/torrent_status.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "libtorrent/hex.hpp" // to_hex
|
2015-09-18 06:23:45 +02:00
|
|
|
|
2013-01-07 01:55:15 +01:00
|
|
|
static const int file_sizes[] =
|
|
|
|
{ 5, 16 - 5, 16000, 17, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
|
|
|
|
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4};
|
|
|
|
const int num_files = sizeof(file_sizes)/sizeof(file_sizes[0]);
|
|
|
|
|
2014-10-14 04:06:20 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// make sure we don't accidentally require files to be writable just to
|
|
|
|
// check their hashes
|
|
|
|
read_only_files = 1,
|
|
|
|
|
|
|
|
// make sure we detect corrupt files and mark the appropriate pieces
|
|
|
|
// as not had
|
|
|
|
corrupt_files = 2,
|
2014-10-22 09:05:10 +02:00
|
|
|
|
|
|
|
incomplete_files = 4,
|
2014-10-14 04:06:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void test_checking(int flags = read_only_files)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace lt = libtorrent;
|
2013-01-06 05:53:26 +01:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "\n==== TEST CHECKING %s%s%s=====\n\n"
|
2014-10-14 04:06:20 +02:00
|
|
|
, (flags & read_only_files) ? "read-only-files ":""
|
2014-10-22 09:05:10 +02:00
|
|
|
, (flags & corrupt_files) ? "corrupt ":""
|
|
|
|
, (flags & incomplete_files) ? "incomplete ":"");
|
2013-01-06 05:53:26 +01:00
|
|
|
|
2013-01-07 01:55:15 +01:00
|
|
|
// make the files writable again
|
|
|
|
for (int i = 0; i < num_files; ++i)
|
|
|
|
{
|
|
|
|
char name[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(name, sizeof(name), "test%d", i);
|
2014-10-22 09:05:10 +02:00
|
|
|
char dirname[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
|
2014-10-22 09:05:10 +02:00
|
|
|
std::string path = combine_path(combine_path("tmp1_checking", "test_torrent_dir"), dirname);
|
2013-01-07 01:55:15 +01:00
|
|
|
path = combine_path(path, name);
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
SetFileAttributesA(path.c_str(), FILE_ATTRIBUTE_NORMAL);
|
|
|
|
#else
|
2013-01-07 05:13:45 +01:00
|
|
|
chmod(path.c_str(), S_IRUSR | S_IWUSR);
|
2013-01-07 01:55:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
// in case the previous run was terminated
|
|
|
|
error_code ec;
|
|
|
|
remove_all("tmp1_checking", ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: removing tmp1_checking: (%d) %s\n"
|
2013-01-06 05:53:26 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
|
|
|
|
create_directory("tmp1_checking", ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: creating directory tmp1_checking: (%d) %s\n"
|
2013-01-06 05:53:26 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
2013-01-07 01:55:15 +01:00
|
|
|
create_directory(combine_path("tmp1_checking", "test_torrent_dir"), ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: creating directory test_torrent_dir: (%d) %s\n"
|
2013-01-06 05:53:26 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
2016-03-07 03:42:18 +01:00
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
file_storage fs;
|
|
|
|
std::srand(10);
|
|
|
|
int piece_size = 0x4000;
|
|
|
|
|
2014-10-22 09:05:10 +02:00
|
|
|
create_random_files(combine_path("tmp1_checking", "test_torrent_dir")
|
|
|
|
, file_sizes, num_files);
|
2013-01-07 01:55:15 +01:00
|
|
|
|
|
|
|
add_files(fs, combine_path("tmp1_checking", "test_torrent_dir"));
|
2016-03-19 06:44:21 +01:00
|
|
|
libtorrent::create_torrent t(fs, piece_size, 0x4000
|
|
|
|
, libtorrent::create_torrent::optimize_alignment);
|
2013-01-06 05:53:26 +01:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
set_piece_hashes(t, "tmp1_checking", ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: set_piece_hashes: (%d) %s\n"
|
2013-01-06 05:53:26 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), t.generate());
|
2016-08-17 23:26:35 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>(&buf[0], int(buf.size()), ec);
|
2013-01-06 05:53:26 +01:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "generated torrent: %s tmp1_checking/test_torrent_dir\n"
|
2016-07-29 08:36:15 +02:00
|
|
|
, aux::to_hex(ti->info_hash()).c_str());
|
2013-01-06 05:53:26 +01:00
|
|
|
|
2014-10-22 09:05:10 +02:00
|
|
|
// truncate every file in half
|
|
|
|
if (flags & incomplete_files)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < num_files; ++i)
|
|
|
|
{
|
|
|
|
char name[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(name, sizeof(name), "test%d", i);
|
2014-10-22 09:05:10 +02:00
|
|
|
char dirname[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
|
2014-10-22 09:05:10 +02:00
|
|
|
std::string path = combine_path(combine_path("tmp1_checking", "test_torrent_dir"), dirname);
|
|
|
|
path = combine_path(path, name);
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
file f(path, file::read_write, ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: opening file \"%s\": (%d) %s\n"
|
2014-10-22 09:05:10 +02:00
|
|
|
, path.c_str(), ec.value(), ec.message().c_str());
|
|
|
|
f.set_size(file_sizes[i] / 2, ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: truncating file \"%s\": (%d) %s\n"
|
2014-10-22 09:05:10 +02:00
|
|
|
, path.c_str(), ec.value(), ec.message().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
// overwrite the files with new random data
|
2014-10-14 04:06:20 +02:00
|
|
|
if (flags & corrupt_files)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "corrupt file test. overwriting files\n");
|
2013-01-06 05:53:26 +01:00
|
|
|
// increase the size of some files. When they're read only that forces
|
|
|
|
// the checker to open them in write-mode to truncate them
|
|
|
|
static const int file_sizes2[] =
|
|
|
|
{ 5, 16 - 5, 16001, 30, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
|
|
|
|
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4};
|
2013-01-07 01:55:15 +01:00
|
|
|
create_random_files(combine_path("tmp1_checking", "test_torrent_dir"), file_sizes2, num_files);
|
2013-01-06 05:53:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// make the files read only
|
2014-10-14 04:06:20 +02:00
|
|
|
if (flags & read_only_files)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "making files read-only\n");
|
2013-01-06 05:53:26 +01:00
|
|
|
for (int i = 0; i < num_files; ++i)
|
|
|
|
{
|
2013-01-07 01:55:15 +01:00
|
|
|
char name[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(name, sizeof(name), "test%d", i);
|
2014-03-07 09:39:55 +01:00
|
|
|
char dirname[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
|
2014-03-07 09:39:55 +01:00
|
|
|
|
|
|
|
std::string path = combine_path(combine_path("tmp1_checking", "test_torrent_dir"), dirname);
|
2013-01-07 01:55:15 +01:00
|
|
|
path = combine_path(path, name);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, " %s\n", path.c_str());
|
2014-03-07 09:39:55 +01:00
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2013-01-07 01:55:15 +01:00
|
|
|
SetFileAttributesA(path.c_str(), FILE_ATTRIBUTE_READONLY);
|
2013-01-06 05:53:26 +01:00
|
|
|
#else
|
2013-01-07 01:55:15 +01:00
|
|
|
chmod(path.c_str(), S_IRUSR);
|
2013-01-06 05:53:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 21:48:11 +02:00
|
|
|
int mask = alert::all_categories
|
|
|
|
& ~(alert::progress_notification
|
|
|
|
| alert::performance_warning
|
|
|
|
| alert::stats_notification);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
settings_pack pack;
|
2015-06-14 21:48:11 +02:00
|
|
|
pack.set_bool(settings_pack::enable_lsd, false);
|
|
|
|
pack.set_bool(settings_pack::enable_natpmp, false);
|
|
|
|
pack.set_bool(settings_pack::enable_upnp, false);
|
|
|
|
pack.set_bool(settings_pack::enable_dht, false);
|
|
|
|
pack.set_int(settings_pack::alert_mask, mask);
|
2014-07-06 21:18:00 +02:00
|
|
|
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48000");
|
|
|
|
pack.set_int(settings_pack::max_retry_port_bind, 1000);
|
2014-12-31 16:51:45 +01:00
|
|
|
lt::session ses1(pack);
|
2013-01-06 05:53:26 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
p.save_path = "tmp1_checking";
|
|
|
|
p.ti = ti;
|
|
|
|
torrent_handle tor1 = ses1.add_torrent(p, ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
torrent_status st;
|
2015-06-14 21:48:11 +02:00
|
|
|
for (int i = 0; i < 20; ++i)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
|
|
|
print_alerts(ses1, "ses1");
|
|
|
|
|
|
|
|
st = tor1.status();
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%d %f %s\n", st.state, st.progress_ppm / 10000.f, st.errc.message().c_str());
|
2013-01-06 05:53:26 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (
|
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
st.state != torrent_status::queued_for_checking &&
|
|
|
|
#endif
|
|
|
|
st.state != torrent_status::checking_files
|
2013-01-06 05:53:26 +01:00
|
|
|
&& st.state != torrent_status::checking_resume_data)
|
|
|
|
break;
|
|
|
|
|
2015-11-07 07:01:07 +01:00
|
|
|
if (st.errc) break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2013-01-06 05:53:26 +01:00
|
|
|
}
|
2014-10-22 09:05:10 +02:00
|
|
|
if (flags & incomplete_files)
|
|
|
|
{
|
|
|
|
TEST_CHECK(!st.is_seeding);
|
|
|
|
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2014-10-22 09:05:10 +02:00
|
|
|
st = tor1.status();
|
|
|
|
TEST_CHECK(!st.is_seeding);
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:06:20 +02:00
|
|
|
if (flags & corrupt_files)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
|
|
|
TEST_CHECK(!st.is_seeding);
|
|
|
|
|
2014-10-20 18:52:28 +02:00
|
|
|
if (flags & read_only_files)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
// we expect our checking of the files to trigger
|
|
|
|
// attempts to truncate them, since the files are
|
|
|
|
// read-only here, we expect the checking to fail.
|
2015-11-03 06:12:30 +01:00
|
|
|
TEST_CHECK(st.errc);
|
|
|
|
if (st.errc)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "error: %s\n", st.errc.message().c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// wait a while to make sure libtorrent survived the error
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(1000));
|
2015-06-14 21:48:11 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
st = tor1.status();
|
|
|
|
TEST_CHECK(!st.is_seeding);
|
2015-11-07 07:01:07 +01:00
|
|
|
TEST_CHECK(st.errc);
|
2015-11-03 06:12:30 +01:00
|
|
|
if (st.errc)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "error: %s\n", st.errc.message().c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-03 06:12:30 +01:00
|
|
|
TEST_CHECK(!st.errc);
|
|
|
|
if (st.errc)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "error: %s\n", st.errc.message().c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2013-01-06 05:53:26 +01:00
|
|
|
}
|
2014-10-22 09:05:10 +02:00
|
|
|
|
|
|
|
if ((flags & (incomplete_files | corrupt_files)) == 0)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
|
|
|
TEST_CHECK(st.is_seeding);
|
2015-11-03 06:12:30 +01:00
|
|
|
if (st.errc)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "error: %s\n", st.errc.message().c_str());
|
2013-01-06 05:53:26 +01:00
|
|
|
}
|
|
|
|
|
2013-01-07 01:55:15 +01:00
|
|
|
// make the files writable again
|
2014-10-14 04:06:20 +02:00
|
|
|
if (flags & read_only_files)
|
2013-01-07 01:55:15 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num_files; ++i)
|
|
|
|
{
|
|
|
|
char name[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(name, sizeof(name), "test%d", i);
|
2014-03-07 09:39:55 +01:00
|
|
|
char dirname[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
|
2014-03-07 09:39:55 +01:00
|
|
|
|
|
|
|
std::string path = combine_path(combine_path("tmp1_checking", "test_torrent_dir"), dirname);
|
2013-01-07 01:55:15 +01:00
|
|
|
path = combine_path(path, name);
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
SetFileAttributesA(path.c_str(), FILE_ATTRIBUTE_NORMAL);
|
|
|
|
#else
|
2013-01-07 05:13:45 +01:00
|
|
|
chmod(path.c_str(), S_IRUSR | S_IWUSR);
|
2013-01-07 01:55:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
remove_all("tmp1_checking", ec);
|
2016-05-17 15:24:06 +02:00
|
|
|
if (ec) std::fprintf(stderr, "ERROR: removing tmp1_checking: (%d) %s\n"
|
2013-01-06 05:53:26 +01:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
}
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(checking)
|
2013-01-06 05:53:26 +01:00
|
|
|
{
|
2014-10-14 04:06:20 +02:00
|
|
|
test_checking();
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(read_only_corrupt)
|
|
|
|
{
|
2014-10-14 04:06:20 +02:00
|
|
|
test_checking(read_only_files | corrupt_files);
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(read_only)
|
|
|
|
{
|
2014-10-22 09:05:10 +02:00
|
|
|
test_checking(read_only_files);
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(incomplete)
|
|
|
|
{
|
2014-10-22 09:05:10 +02:00
|
|
|
test_checking(incomplete_files);
|
2015-06-14 21:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(corrupt)
|
|
|
|
{
|
2014-10-20 05:44:18 +02:00
|
|
|
test_checking(corrupt_files);
|
2013-01-06 05:53:26 +01:00
|
|
|
}
|
|
|
|
|