premiere-libtorrent/test/test_part_file.cpp

141 lines
5.0 KiB
C++
Raw Normal View History

2014-07-06 21:18:00 +02:00
/*
Copyright (c) 2012, 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 <cstring>
#include <array>
2014-07-06 21:18:00 +02:00
#include "test.hpp"
#include "libtorrent/part_file.hpp"
#include "libtorrent/aux_/path.hpp"
2014-07-06 21:18:00 +02:00
#include "libtorrent/error_code.hpp"
2017-04-12 20:05:53 +02:00
using namespace lt;
2014-07-06 21:18:00 +02:00
TORRENT_TEST(part_file)
2014-07-06 21:18:00 +02:00
{
error_code ec;
std::string cwd = complete(".");
remove_all(combine_path(cwd, "partfile_test_dir"), ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
remove_all(combine_path(cwd, "partfile_test_dir2"), ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
int piece_size = 16 * 0x4000;
std::array<char, 1024> buf;
2014-07-06 21:18:00 +02:00
{
create_directory(combine_path(cwd, "partfile_test_dir"), ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("create_directory: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
create_directory(combine_path(cwd, "partfile_test_dir2"), ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("create_directory: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
part_file pf(combine_path(cwd, "partfile_test_dir"), "partfile.parts", 100, piece_size);
pf.flush_metadata(ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
// since we don't have anything in the part file, it will have
// not have been created yet
TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
// write something to the metadata file
2018-01-29 15:58:22 +01:00
for (int i = 0; i < 1024; ++i) buf[std::size_t(i)] = char(i & 0xff);
2014-07-06 21:18:00 +02:00
iovec_t v = buf;
pf.writev(v, piece_index_t(10), 0, ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("part_file::writev: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
pf.flush_metadata(ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
// now wwe should have created the partfile
TEST_CHECK(exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
pf.move_partfile(combine_path(cwd, "partfile_test_dir2"), ec);
TEST_CHECK(!ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("move_partfile: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
TEST_CHECK(exists(combine_path(combine_path(cwd, "partfile_test_dir2"), "partfile.parts")));
buf.fill(0);
2014-07-06 21:18:00 +02:00
pf.readv(v, piece_index_t(10), 0, ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("part_file::readv: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
for (int i = 0; i < 1024; ++i)
2018-01-29 15:58:22 +01:00
TEST_CHECK(buf[std::size_t(i)] == char(i));
2014-07-06 21:18:00 +02:00
}
{
// load the part file back in
part_file pf(combine_path(cwd, "partfile_test_dir2"), "partfile.parts", 100, piece_size);
buf.fill(0);
2014-07-06 21:18:00 +02:00
iovec_t v = buf;
pf.readv(v, piece_index_t(10), 0, ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("part_file::readv: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
for (int i = 0; i < 1024; ++i)
2018-01-29 15:58:22 +01:00
TEST_CHECK(buf[std::size_t(i)] == static_cast<char>(i));
2014-07-06 21:18:00 +02:00
// test exporting the piece to a file
std::string output_filename = combine_path(combine_path(cwd, "partfile_test_dir")
, "part_file_test_export");
2018-01-23 20:56:03 +01:00
pf.export_file([](std::int64_t file_offset, span<char> buf_data)
{
2018-01-23 20:56:03 +01:00
for (char i : buf_data)
{
// make sure we got the bytes we expected
TEST_CHECK(i == static_cast<char>(file_offset));
++file_offset;
}
}, 10 * piece_size, 1024, ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("export_file: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
pf.free_piece(piece_index_t(10));
2014-07-06 21:18:00 +02:00
pf.flush_metadata(ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
// we just removed the last piece. The partfile does not
// contain anything anymore, it should have deleted itself
2015-01-15 04:35:13 +01:00
TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir2"), "partfile.parts"), ec));
TEST_CHECK(!ec);
2016-10-10 02:23:45 +02:00
if (ec) std::printf("exists: %s\n", ec.message().c_str());
2014-07-06 21:18:00 +02:00
}
}