2005-09-11 11:48:05 +02:00
|
|
|
#include "libtorrent/storage.hpp"
|
2006-11-19 16:25:02 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2005-09-11 11:48:05 +02:00
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
#include <boost/utility.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/convenience.hpp>
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
|
|
|
|
#include "test.hpp"
|
2007-06-10 22:46:09 +02:00
|
|
|
#include "setup_transfer.hpp"
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2007-02-25 22:11:29 +01:00
|
|
|
const int piece_size = 16;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void on_read_piece(int ret, disk_io_job const& j, char const* data, int size)
|
|
|
|
{
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << "on_read_piece piece: " << j.piece << std::endl;
|
2007-06-10 22:46:09 +02:00
|
|
|
TEST_CHECK(ret == size);
|
|
|
|
TEST_CHECK(std::equal(j.buffer, j.buffer + ret, data));
|
|
|
|
}
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
void run_storage_tests(boost::intrusive_ptr<torrent_info> info
|
|
|
|
, path const& test_path
|
|
|
|
, libtorrent::storage_mode_t storage_mode)
|
2005-09-11 11:48:05 +02:00
|
|
|
{
|
|
|
|
const int half = piece_size / 2;
|
|
|
|
|
|
|
|
char piece0[piece_size] =
|
|
|
|
{ 6, 6, 6, 6, 6, 6, 6, 6
|
|
|
|
, 9, 9, 9, 9, 9, 9, 9, 9};
|
|
|
|
|
|
|
|
char piece1[piece_size] =
|
|
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
, 1, 1, 1, 1, 1, 1, 1, 1};
|
|
|
|
|
|
|
|
char piece2[piece_size] =
|
|
|
|
{ 0, 0, 1, 0, 0, 0, 0, 0
|
|
|
|
, 1, 1, 1, 1, 1, 1, 1, 1};
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
info->set_hash(0, hasher(piece0, piece_size).final());
|
|
|
|
info->set_hash(1, hasher(piece1, piece_size).final());
|
|
|
|
info->set_hash(2, hasher(piece2, piece_size).final());
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
info->create_torrent();
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
create_directory(test_path / "temp_storage");
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2006-08-10 21:18:11 +02:00
|
|
|
int num_pieces = (1 + 612 + 17 + piece_size - 1) / piece_size;
|
2007-09-01 05:00:31 +02:00
|
|
|
TEST_CHECK(info->num_pieces() == num_pieces);
|
2005-09-28 23:46:35 +02:00
|
|
|
|
|
|
|
char piece[piece_size];
|
|
|
|
|
|
|
|
{ // avoid having two storages use the same files
|
2006-11-19 16:25:02 +01:00
|
|
|
file_pool fp;
|
2007-04-02 07:46:19 +02:00
|
|
|
boost::scoped_ptr<storage_interface> s(
|
2007-10-08 22:01:36 +02:00
|
|
|
default_storage_constructor(info, test_path, fp));
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
// write piece 1 (in slot 0)
|
2007-04-02 07:46:19 +02:00
|
|
|
s->write(piece1, 0, 0, half);
|
|
|
|
s->write(piece1 + half, 0, half, half);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
// verify piece 1
|
2007-04-02 07:46:19 +02:00
|
|
|
TEST_CHECK(s->read(piece, 0, 0, piece_size) == piece_size);
|
2005-09-11 11:48:05 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece1));
|
|
|
|
|
|
|
|
// do the same with piece 0 and 2 (in slot 1 and 2)
|
2007-04-02 07:46:19 +02:00
|
|
|
s->write(piece0, 1, 0, piece_size);
|
|
|
|
s->write(piece2, 2, 0, piece_size);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
// verify piece 0 and 2
|
2007-04-02 07:46:19 +02:00
|
|
|
TEST_CHECK(s->read(piece, 1, 0, piece_size) == piece_size);
|
2005-09-11 11:48:05 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece0));
|
|
|
|
|
2007-04-02 07:46:19 +02:00
|
|
|
s->read(piece, 2, 0, piece_size);
|
2005-09-11 11:48:05 +02:00
|
|
|
TEST_CHECK(std::equal(piece, piece + piece_size, piece2));
|
|
|
|
|
2007-04-02 07:46:19 +02:00
|
|
|
s->release_files();
|
2005-09-28 23:46:35 +02:00
|
|
|
}
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
// make sure the piece_manager can identify the pieces
|
2007-02-25 22:11:29 +01:00
|
|
|
{
|
2006-11-19 16:25:02 +01:00
|
|
|
file_pool fp;
|
2007-06-10 22:46:09 +02:00
|
|
|
disk_io_thread io;
|
|
|
|
boost::shared_ptr<int> dummy(new int);
|
|
|
|
boost::intrusive_ptr<piece_manager> pm = new piece_manager(dummy, info
|
2007-10-08 22:01:36 +02:00
|
|
|
, test_path, fp, io, default_storage_constructor);
|
2005-09-11 11:48:05 +02:00
|
|
|
boost::mutex lock;
|
2006-10-11 16:02:21 +02:00
|
|
|
libtorrent::aux::piece_checker_data d;
|
2005-09-11 11:48:05 +02:00
|
|
|
|
|
|
|
std::vector<bool> pieces;
|
2006-04-25 23:04:48 +02:00
|
|
|
num_pieces = 0;
|
2007-10-09 09:23:39 +02:00
|
|
|
std::string error_msg;
|
2007-06-10 22:46:09 +02:00
|
|
|
TEST_CHECK(pm->check_fastresume(d, pieces, num_pieces
|
2007-10-09 09:23:39 +02:00
|
|
|
, storage_mode, error_msg) == false);
|
2005-10-13 09:59:05 +02:00
|
|
|
bool finished = false;
|
|
|
|
float progress;
|
2006-04-25 23:04:48 +02:00
|
|
|
num_pieces = 0;
|
2007-03-18 03:19:23 +01:00
|
|
|
boost::recursive_mutex mutex;
|
2005-10-13 09:59:05 +02:00
|
|
|
while (!finished)
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::tie(finished, progress) = pm->check_files(pieces, num_pieces, mutex);
|
2005-09-11 11:48:05 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
TEST_CHECK(num_pieces == std::count(pieces.begin(), pieces.end()
|
|
|
|
, true));
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-06-11 23:24:14 +02:00
|
|
|
|
|
|
|
boost::function<void(int, disk_io_job const&)> none;
|
2007-10-08 22:01:36 +02:00
|
|
|
TEST_CHECK(exists(test_path / "temp_storage"));
|
|
|
|
pm->async_move_storage(test_path / "temp_storage2", none);
|
2007-06-10 22:46:09 +02:00
|
|
|
test_sleep(2000);
|
2007-10-08 22:01:36 +02:00
|
|
|
TEST_CHECK(!exists(test_path / "temp_storage"));
|
|
|
|
TEST_CHECK(exists(test_path / "temp_storage2/temp_storage"));
|
|
|
|
pm->async_move_storage(test_path , none);
|
2007-06-10 22:46:09 +02:00
|
|
|
test_sleep(2000);
|
2007-10-08 22:01:36 +02:00
|
|
|
TEST_CHECK(!exists(test_path / "temp_storage2/temp_storage"));
|
|
|
|
remove_all(test_path / "temp_storage2");
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
peer_request r;
|
|
|
|
r.piece = 0;
|
|
|
|
r.start = 0;
|
|
|
|
r.length = piece_size;
|
|
|
|
pm->async_read(r, bind(&on_read_piece, _1, _2, piece0, piece_size));
|
|
|
|
r.piece = 1;
|
|
|
|
pm->async_read(r, bind(&on_read_piece, _1, _2, piece1, piece_size));
|
|
|
|
r.piece = 2;
|
|
|
|
pm->async_read(r, bind(&on_read_piece, _1, _2, piece2, piece_size));
|
2007-06-11 23:24:14 +02:00
|
|
|
pm->async_release_files(none);
|
2006-06-25 16:37:30 +02:00
|
|
|
|
2007-02-25 22:11:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-21 02:19:37 +02:00
|
|
|
void test_remove(path const& test_path)
|
|
|
|
{
|
|
|
|
boost::intrusive_ptr<torrent_info> info(new torrent_info());
|
|
|
|
info->set_piece_size(4);
|
|
|
|
info->add_file("temp_storage/test1.tmp", 8);
|
|
|
|
info->add_file("temp_storage/folder1/test2.tmp", 8);
|
|
|
|
info->add_file("temp_storage/folder2/test3.tmp", 0);
|
|
|
|
info->add_file("temp_storage/_folder3/test4.tmp", 0);
|
|
|
|
info->add_file("temp_storage/_folder3/subfolder/test5.tmp", 8);
|
|
|
|
|
|
|
|
char buf[4] = {0, 0, 0, 0};
|
|
|
|
sha1_hash h = hasher(buf, 4).final();
|
|
|
|
for (int i = 0; i < 6; ++i) info->set_hash(i, h);
|
|
|
|
|
|
|
|
info->create_torrent();
|
|
|
|
|
|
|
|
file_pool fp;
|
|
|
|
boost::scoped_ptr<storage_interface> s(
|
|
|
|
default_storage_constructor(info, test_path, fp));
|
|
|
|
|
|
|
|
// allocate the files and create the directories
|
|
|
|
s->initialize(true);
|
|
|
|
|
|
|
|
TEST_CHECK(exists(test_path / "temp_storage/_folder3/subfolder/test5.tmp"));
|
|
|
|
TEST_CHECK(exists(test_path / "temp_storage/folder2/test3.tmp"));
|
|
|
|
|
|
|
|
s->delete_files();
|
|
|
|
|
|
|
|
TEST_CHECK(!exists(test_path / "temp_storage"));
|
|
|
|
}
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
void run_test(path const& test_path)
|
2007-02-25 22:11:29 +01:00
|
|
|
{
|
2007-10-13 20:29:02 +02:00
|
|
|
std::cerr << "\n=== " << test_path.string() << " ===\n" << std::endl;
|
2007-10-08 22:01:36 +02:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> info(new torrent_info());
|
|
|
|
info->set_piece_size(piece_size);
|
|
|
|
info->add_file("temp_storage/test1.tmp", 17);
|
|
|
|
info->add_file("temp_storage/test2.tmp", 612);
|
|
|
|
info->add_file("temp_storage/test3.tmp", 0);
|
|
|
|
info->add_file("temp_storage/test4.tmp", 0);
|
|
|
|
info->add_file("temp_storage/test5.tmp", 1);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << "=== test 1 ===" << std::endl;
|
|
|
|
|
|
|
|
run_storage_tests(info, test_path, storage_mode_compact);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
|
|
|
// make sure the files have the correct size
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
|
|
|
|
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 17);
|
|
|
|
std::cerr << file_size(test_path / "temp_storage" / "test2.tmp") << std::endl;
|
|
|
|
TEST_CHECK(file_size(test_path / "temp_storage" / "test2.tmp") == 31);
|
|
|
|
TEST_CHECK(exists(test_path / "temp_storage/test3.tmp"));
|
|
|
|
TEST_CHECK(exists(test_path / "temp_storage/test4.tmp"));
|
|
|
|
remove_all(test_path / "temp_storage");
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-08-25 20:26:43 +02:00
|
|
|
// ==============================================
|
|
|
|
|
|
|
|
// make sure remap_files works
|
2007-11-25 11:47:05 +01:00
|
|
|
std::vector<file_entry> map;
|
|
|
|
file_entry fe;
|
|
|
|
fe.path = "temp_storage/test.tmp";
|
|
|
|
fe.size = 17;
|
|
|
|
fe.file_base = 612 + 1;
|
|
|
|
map.push_back(fe);
|
|
|
|
fe.path = "temp_storage/test.tmp";
|
|
|
|
fe.size = 612 + 1;
|
|
|
|
fe.file_base = 0;
|
|
|
|
map.push_back(fe);
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
bool ret = info->remap_files(map);
|
2007-08-25 20:26:43 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << "=== test 2 ===" << std::endl;
|
|
|
|
|
|
|
|
run_storage_tests(info, test_path, storage_mode_compact);
|
2007-08-25 20:26:43 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << file_size(test_path / "temp_storage" / "test.tmp") << std::endl;
|
2007-11-25 11:47:05 +01:00
|
|
|
TEST_CHECK(file_size(test_path / "temp_storage" / "test.tmp") == 17 + 612 + 1);
|
2007-08-25 20:26:43 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
remove_all(test_path / "temp_storage");
|
2007-08-25 20:26:43 +02:00
|
|
|
|
|
|
|
// ==============================================
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
info = new torrent_info();
|
|
|
|
info->set_piece_size(piece_size);
|
|
|
|
info->add_file("temp_storage/test1.tmp", 17 + 612 + 1);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << "=== test 3 ===" << std::endl;
|
|
|
|
|
|
|
|
run_storage_tests(info, test_path, storage_mode_compact);
|
2007-02-25 22:11:29 +01:00
|
|
|
|
|
|
|
// 48 = piece_size * 3
|
2007-10-08 22:01:36 +02:00
|
|
|
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 48);
|
|
|
|
remove_all(test_path / "temp_storage");
|
2007-02-25 22:11:29 +01:00
|
|
|
|
2007-08-25 20:26:43 +02:00
|
|
|
// ==============================================
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::cerr << "=== test 4 ===" << std::endl;
|
|
|
|
|
|
|
|
run_storage_tests(info, test_path, storage_mode_allocate);
|
|
|
|
|
|
|
|
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
|
|
|
|
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 17 + 612 + 1);
|
2007-06-01 01:35:48 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
remove_all(test_path / "temp_storage");
|
2007-10-21 02:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
// ==============================================
|
|
|
|
|
|
|
|
std::cerr << "=== test 5 ===" << std::endl;
|
|
|
|
test_remove(test_path);
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
}
|
2007-06-01 01:35:48 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
int test_main()
|
|
|
|
{
|
|
|
|
std::vector<path> test_paths;
|
|
|
|
char* env = std::getenv("TORRENT_TEST_PATHS");
|
|
|
|
if (env == 0)
|
|
|
|
{
|
|
|
|
test_paths.push_back(initial_path());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* p = std::strtok(env, ";");
|
|
|
|
while (p != 0)
|
|
|
|
{
|
|
|
|
test_paths.push_back(complete(p));
|
|
|
|
p = std::strtok(0, ";");
|
|
|
|
}
|
|
|
|
}
|
2007-06-01 01:35:48 +02:00
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
std::for_each(test_paths.begin(), test_paths.end(), bind(&run_test, _1));
|
2007-10-21 02:19:37 +02:00
|
|
|
|
2005-09-11 11:48:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|