Files
transmission/tests/libtransmission/copy-test.cc
Yat Ho a89ca4f2c9 build(cmake): support building with (almost) all system 3rd-party libraries (#7554)
* build: overhaul tr_add_external_auto_library

* build: use package-provided CMake config file libevent

* build: use package-provided CMake config file miniupnpc

* build: update libutp find module

* build: make LIBNAME an optional parameter

* build: use package-provided CMake config file libdeflate

* build: update libb64 find module

* build: update libnatpmp find module

* build: update libpsl find module

* build: support system fast_float library

* chore: reformat long brew commands

* build: support system fmt library

* build: support system rapidjson library

* build: support system small library

* build: support system library utf8cpp

* build: support system library WideInteger

* build: support system library gtest

* fix: incorrectly labeled test suites

* build: remove unused parameters from tr_add_external_auto_library

* build: update crc32c cmake script

* fix: dht system library

* fix: add libutp interface target

* code review: move TrGtest.cmake

* code review: move tr_get_fmt_version into Findfmt.cmake

* code review: use option() for gtest

* code review: move find_package(PkgConfig) out of loop

* build: delete FindCrc32c.cmake

Impossible to parse package version from distributed source files.

* code review: Finddht.cmake

* build: delete FindFastFloat.cmake

Impossible to parse package version from distributed source files.

* code review: Findfmt.cmake

* code review: Findlibb64.cmake

* code review: Findlibdeflate.cmake

* code review: Findlibevent.cmake

* code review: Findlibnatpmp.cmake

* code review: Findlibpsl.cmake

* code review: Findlibutp.cmake

* code review: Findlibminiupnpc.cmake

* code review: FindRapidJSON.cmake

* build: delete FindSmall.cmake

Impossible to parse package version from distributed source files.

* build: only accept cmake config package for utf8cpp

Impossible to parse package version from distributed source files.

* build: delete FindWideInteger.cmake

Impossible to parse package version from distributed source files.

* build: add `USE_SYSTEM_DEFAULT`

* ci: drop Fedora 40 and adopt Fedora 43

* ci: try to silence system header warnings

* ci: use `cmake --install`

* Revert "build: only accept cmake config package for utf8cpp"

This reverts commit 2158d631fd.

* build: harden utf8cpp find module

* chore: bump wide-integer

Pick up bf9398f9da and bcc726a30f

* refactor: gtest should be included with angled brackets

Now that gtest is built as a system library, it should be included with angled brackets instead of quotes.

* code review: fixup libutp variables before `find_package_handle_standard_args`

* code review: define `WIDE_INTEGER_HAS_LIMB_TYPE_UINT64` only for targets depending on WideInteger

* chore: bump wide-integer

Pickup 4b2258acac so that wide-integer tests won't run in Transmission project.
2026-01-12 09:04:47 -06:00

92 lines
2.7 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 <cstdint> // uint64_t
#include <cstring>
#include <string_view>
#include <vector>
#include <gtest/gtest.h>
#include <libtransmission/crypto-utils.h>
#include <libtransmission/error.h>
#include <libtransmission/file.h>
#include <libtransmission/tr-strbuf.h>
#include <libtransmission/utils.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 };
/* Copy it. */
auto error = tr_error{};
EXPECT_TRUE(tr_sys_path_copy(path1, path2, &error));
EXPECT_FALSE(error) << error;
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