1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-03 05:25:52 +00:00
transmission/tests/libtransmission/copy-test.cc
Charles Kerr 8183d7fddf
refactor: utils naming (#5696)
* chore: rename tr_strvContains to tr_strv_contains

* chore: rename tr_strvStartsWith to tr_strv_starts_with

* chore: rename tr_strvEndsWith to tr_strv_ends_with

* chore: rename tr_strvSep to tr_strv_sep

* chore: rename tr_strvStrip to tr_strv_strip

* chore: rename tr_strvToBuf to tr_strv_to_buf

* refactor: rename tr_saveFile() to tr_file_save()

rename tr_loadFile() to tr_file_read()

rename tr_moveFile() to tr_file_move()

* refactor: rename tr_parseNum() to tr_num_parse()

refactor: rename tr_parseNumRange() to tr_num_parse_range()

* chore: group related functions together in header
2023-06-30 09:49:58 -05:00

86 lines
2.6 KiB
C++

// This file Copyright (C) 2013-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <cstring>
#include <vector>
#include <libtransmission/transmission.h>
#include <libtransmission/error.h>
#include <libtransmission/file.h>
#include "test-fixtures.h"
namespace libtransmission::test
{
class CopyTest : public SandboxedTest
{
protected:
void testImpl(char const* filename1, char const* filename2, size_t const file_length)
{
auto const path1 = tr_pathbuf{ sandboxDir(), '/', filename1 };
/* Create a file. */
auto contents = std::vector<char>{};
contents.resize(file_length);
tr_rand_buffer(std::data(contents), std::size(contents));
createFileWithContents(path1, std::data(contents), std::size(contents));
auto const path2 = tr_pathbuf{ sandboxDir(), '/', filename2 };
tr_error* err = nullptr;
/* Copy it. */
EXPECT_TRUE(tr_sys_path_copy(path1, path2, &err));
EXPECT_EQ(nullptr, err) << ' ' << *err;
tr_error_clear(&err);
EXPECT_TRUE(filesAreIdentical(path1, path2));
/* Dispose of those files that we created. */
tr_sys_path_remove(path1);
tr_sys_path_remove(path2);
}
private:
static uint64_t fillBufferFromFd(tr_sys_file_t fd, uint64_t bytes_remaining, char* buf, size_t buf_len)
{
memset(buf, 0, buf_len);
size_t buf_pos = 0;
while (buf_pos < buf_len && bytes_remaining > 0)
{
uint64_t const chunk_size = std::min(uint64_t{ buf_len - buf_pos }, bytes_remaining);
uint64_t bytes_read = 0;
tr_sys_file_read(fd, buf + buf_pos, chunk_size, &bytes_read);
EXPECT_LE(buf_pos + bytes_read, buf_len);
EXPECT_LE(bytes_read, bytes_remaining);
buf_pos += bytes_read;
bytes_remaining -= bytes_read;
}
return bytes_remaining;
}
static bool filesAreIdentical(std::string_view filename1, std::string_view filename2)
{
auto contents1 = std::vector<char>{};
auto contents2 = std::vector<char>{};
return tr_file_read(filename1, contents1) && tr_file_read(filename2, contents2) && contents1 == contents2;
}
};
TEST_F(CopyTest, copy)
{
char const* filename1 = "orig-blob.txt";
char const* filename2 = "copy-blob.txt";
auto const random_file_length = 1024 * 1024 * 10;
testImpl(filename1, filename2, random_file_length);
}
} // namespace libtransmission::test