premiere-libtorrent/test/test_checking.cpp

354 lines
11 KiB
C++
Raw Normal View History

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 <sys/stat.h> // for chmod
2013-01-06 05:53:26 +01:00
#include "libtorrent/session.hpp"
#include "test.hpp"
#include "settings.hpp"
2013-01-06 05:53:26 +01:00
#include "setup_transfer.hpp"
2018-03-26 11:25:18 +02:00
#include "test_utils.hpp"
2013-01-06 05:53:26 +01:00
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/torrent_status.hpp"
#include "libtorrent/hex.hpp" // to_hex
#include "libtorrent/aux_/path.hpp"
namespace {
namespace
{
bool is_checking(int const state)
{
return state == lt::torrent_status::checking_files
#if TORRENT_ABI_VERSION == 1
|| state == lt::torrent_status::queued_for_checking
#endif
|| state == lt::torrent_status::checking_resume_data;
2018-03-26 11:25:18 +02:00
}
}
2013-01-07 01:55:15 +01: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,
// make the files not be there when starting up, move the files in place and
// force-recheck. Make sure the stat cache is cleared and let us pick up the
// new files
force_recheck = 8,
};
void test_checking(int flags)
2013-01-06 05:53:26 +01:00
{
2017-04-12 20:05:53 +02:00
using namespace lt;
2013-01-06 05:53:26 +01:00
2017-08-10 16:44:57 +02:00
std::printf("\n==== TEST CHECKING %s%s%s%s=====\n\n"
, (flags & read_only_files) ? "read-only-files ":""
2014-10-22 09:05:10 +02:00
, (flags & corrupt_files) ? "corrupt ":""
, (flags & incomplete_files) ? "incomplete ":""
, (flags & force_recheck) ? "force_recheck ":"");
2013-01-07 01:55:15 +01:00
2013-01-06 05:53:26 +01:00
error_code ec;
create_directory("test_torrent_dir", ec);
if (ec) fprintf(stdout, "ERROR: creating directory test_torrent_dir: (%d) %s\n"
2013-01-06 05:53:26 +01:00
, ec.value(), ec.message().c_str());
2013-01-06 05:53:26 +01:00
file_storage fs;
std::srand(10);
int piece_size = 0x4000;
static std::array<const int, 46> const file_sizes
{{ 0, 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 }};
2018-03-26 11:25:18 +02:00
create_random_files("test_torrent_dir", file_sizes, &fs);
2013-01-07 01:55:15 +01:00
2017-04-12 20:05:53 +02:00
lt::create_torrent t(fs, piece_size, 0x4000
, lt::create_torrent::optimize_alignment);
2013-01-06 05:53:26 +01:00
// calculate the hash for all pieces
set_piece_hashes(t, ".", ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("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());
auto ti = std::make_shared<torrent_info>(buf, ec, from_span);
2013-01-06 05:53:26 +01:00
2017-08-10 16:44:57 +02:00
std::printf("generated torrent: %s test_torrent_dir\n"
, 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 (std::size_t i = 0; i < file_sizes.size(); ++i)
2014-10-22 09:05:10 +02:00
{
char name[1024];
std::snprintf(name, sizeof(name), "test%d", int(i));
2014-10-22 09:05:10 +02:00
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
std::string path = combine_path("test_torrent_dir", dirname);
2014-10-22 09:05:10 +02:00
path = combine_path(path, name);
file f(path, open_mode::read_write, ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("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-10-10 02:23:45 +02:00
if (ec) std::printf("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
if (flags & corrupt_files)
2013-01-06 05:53:26 +01:00
{
2016-10-10 02:23:45 +02:00
std::printf("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 std::array<const int, 46> const file_sizes2
{{ 0, 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}};
create_random_files("test_torrent_dir", file_sizes2);
2013-01-06 05:53:26 +01:00
}
// make the files read only
if (flags & read_only_files)
2013-01-06 05:53:26 +01:00
{
2016-10-10 02:23:45 +02:00
std::printf("making files read-only\n");
for (std::size_t i = 0; i < file_sizes.size(); ++i)
2013-01-06 05:53:26 +01:00
{
2013-01-07 01:55:15 +01:00
char name[1024];
std::snprintf(name, sizeof(name), "test%d", int(i));
2014-03-07 09:39:55 +01:00
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
2014-03-07 09:39:55 +01:00
std::string path = combine_path("test_torrent_dir", dirname);
2013-01-07 01:55:15 +01:00
path = combine_path(path, name);
2016-10-10 02:23:45 +02:00
std::printf(" %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
}
}
if (flags & force_recheck)
{
remove_all("test_torrent_dir_tmp", ec);
2017-08-10 16:44:57 +02:00
if (ec) std::printf("ERROR: removing \"test_torrent_dir_tmp\": (%d) %s\n"
, ec.value(), ec.message().c_str());
rename("test_torrent_dir", "test_torrent_dir_tmp", ec);
2017-08-10 16:44:57 +02:00
if (ec) std::printf("ERROR: renaming dir \"test_torrent_dir\": (%d) %s\n"
, ec.value(), ec.message().c_str());
}
lt::session ses1(settings());
2013-01-06 05:53:26 +01:00
add_torrent_params p;
p.save_path = ".";
2013-01-06 05:53:26 +01:00
p.ti = ti;
torrent_handle tor1 = ses1.add_torrent(p, ec);
TEST_CHECK(!ec);
if (flags & force_recheck)
{
// first make sure the session tries to check for the file and can't find
// them
libtorrent::alert const* a = wait_for_alert(
ses1, torrent_checked_alert::alert_type, "checking");
TEST_CHECK(a);
// now, move back the files and force-recheck. make sure we pick up the
// files this time
remove_all("test_torrent_dir", ec);
if (ec) fprintf(stdout, "ERROR: removing \"test_torrent_dir\": (%d) %s\n"
, ec.value(), ec.message().c_str());
rename("test_torrent_dir_tmp", "test_torrent_dir", ec);
if (ec) fprintf(stdout, "ERROR: renaming dir \"test_torrent_dir_tmp\": (%d) %s\n"
, ec.value(), ec.message().c_str());
tor1.force_recheck();
}
2013-01-06 05:53:26 +01:00
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();
2018-01-29 15:58:22 +01:00
std::printf("%d %f %s\n", st.state, st.progress_ppm / 10000.0, st.errc.message().c_str());
2013-01-06 05:53:26 +01:00
if (!is_checking(st.state) || st.errc) break;
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);
std::this_thread::sleep_for(lt::milliseconds(500));
2014-10-22 09:05:10 +02:00
st = tor1.status();
TEST_CHECK(!st.is_seeding);
}
if (flags & corrupt_files)
2013-01-06 05:53:26 +01:00
{
TEST_CHECK(!st.is_seeding);
TEST_CHECK(!st.errc);
if (st.errc)
2018-02-10 19:40:55 +01:00
std::printf("error: %s\n", st.errc.message().c_str());
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);
if (st.errc)
2016-10-10 02:23:45 +02:00
std::printf("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
if (flags & read_only_files)
2013-01-07 01:55:15 +01:00
{
for (std::size_t i = 0; i < file_sizes.size(); ++i)
2013-01-07 01:55:15 +01:00
{
char name[1024];
std::snprintf(name, sizeof(name), "test%d", int(i));
2014-03-07 09:39:55 +01:00
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
2014-03-07 09:39:55 +01:00
std::string path = combine_path("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
}
}
remove_all("test_torrent_dir", ec);
2017-08-10 16:44:57 +02:00
if (ec) std::printf("ERROR: removing test_torrent_dir: (%d) %s\n"
2013-01-06 05:53:26 +01:00
, ec.value(), ec.message().c_str());
}
} // anonymous namespace
TORRENT_TEST(checking)
2013-01-06 05:53:26 +01:00
{
test_checking(0);
2015-06-14 21:48:11 +02:00
}
TORRENT_TEST(read_only_corrupt)
{
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
}
TORRENT_TEST(force_recheck)
{
test_checking(force_recheck);
}
TORRENT_TEST(discrete_checking)
{
using namespace lt;
printf("\n==== TEST CHECKING discrete =====\n\n");
error_code ec;
create_directory("test_torrent_dir", ec);
if (ec) printf("ERROR: creating directory test_torrent_dir: (%d) %s\n", ec.value(), ec.message().c_str());
int const megabyte = 0x100000;
int const piece_size = 2 * megabyte;
static std::array<int const, 2> const file_sizes{{ 9 * megabyte, 3 * megabyte }};
file_storage fs;
create_random_files("test_torrent_dir", file_sizes, &fs);
2018-06-10 13:13:33 +02:00
TEST_EQUAL(fs.num_files(), 2);
2018-03-26 11:25:18 +02:00
lt::create_torrent t(fs, piece_size, 1, lt::create_torrent::optimize_alignment);
set_piece_hashes(t, ".", ec);
if (ec) printf("ERROR: set_piece_hashes: (%d) %s\n", ec.value(), ec.message().c_str());
std::vector<char> buf;
bencode(std::back_inserter(buf), t.generate());
2018-03-26 11:25:18 +02:00
auto ti = std::make_shared<torrent_info>(buf, ec, from_span);
printf("generated torrent: %s test_torrent_dir\n", aux::to_hex(ti->info_hash().to_string()).c_str());
2018-06-10 13:13:33 +02:00
// we have two files, but there's a padfile now too
TEST_EQUAL(ti->num_files(), 3);
2018-06-10 13:13:33 +02:00
{
session ses1(settings());
add_torrent_params p;
2018-03-26 11:25:18 +02:00
p.file_priorities.resize(std::size_t(ti->num_files()));
p.file_priorities[0] = 1_pri;
p.save_path = ".";
p.ti = ti;
torrent_handle tor1 = ses1.add_torrent(p, ec);
// change the priority of a file while checking and make sure it doesn't interrupt the checking.
2018-03-26 11:25:18 +02:00
std::vector<download_priority_t> prio(std::size_t(ti->num_files()), 0_pri);
prio[2] = 1_pri;
tor1.prioritize_files(prio);
2018-03-26 11:25:18 +02:00
TEST_CHECK(wait_for_alert(ses1, torrent_checked_alert::alert_type
, "torrent checked", pop_alerts::pop_all, seconds(50)));
TEST_CHECK(tor1.status({}).is_seeding);
}
remove_all("test_torrent_dir", ec);
if (ec) fprintf(stdout, "ERROR: removing test_torrent_dir: (%d) %s\n", ec.value(), ec.message().c_str());
}