2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-08-16 01:45:50 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <chrono>
|
2022-11-10 05:56:08 +00:00
|
|
|
#include <condition_variable>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstdlib> // getenv()
|
|
|
|
#include <cstring> // strlen()
|
2022-03-21 20:22:50 +00:00
|
|
|
#include <iostream>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex> // std::once_flag()
|
|
|
|
#include <string>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/crypto-utils.h> // tr_base64_decode()
|
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/file.h> // tr_sys_file_*()
|
|
|
|
#include <libtransmission/platform.h> // TR_PATH_DELIMITER
|
|
|
|
#include <libtransmission/quark.h>
|
2023-11-26 21:43:37 +00:00
|
|
|
#include <libtransmission/torrent-ctor.h>
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/torrent.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
#include <libtransmission/variant.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2022-07-22 18:07:23 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, tr_error const& err)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
os << err.message() << ' ' << err.code();
|
2022-03-21 20:22:50 +00:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
using file_func_t = std::function<void(char const* filename)>;
|
|
|
|
|
|
|
|
static void depthFirstWalk(char const* path, file_func_t func)
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
if (auto const info = tr_sys_path_get_info(path); info && info->isFolder())
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
if (auto const odir = tr_sys_dir_open(path); odir != TR_BAD_SYS_DIR)
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
for (;;)
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
char const* const name = tr_sys_dir_read_name(odir);
|
|
|
|
if (name == nullptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("."sv != name && ".."sv != name)
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
2022-07-22 18:07:23 +00:00
|
|
|
auto const child = fmt::format("{:s}/{:s}"sv, path, name);
|
|
|
|
depthFirstWalk(child.c_str(), func);
|
2022-04-15 23:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_dir_close(odir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(path);
|
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
inline bool waitFor(std::function<bool()> const& test, std::chrono::milliseconds msec)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
auto const deadline = std::chrono::steady_clock::now() + msec;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (test())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (std::chrono::steady_clock::now() > deadline)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
inline bool waitFor(std::function<bool()> const& test, int msec)
|
|
|
|
{
|
|
|
|
return waitFor(test, std::chrono::milliseconds{ msec });
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
inline bool waitFor(
|
|
|
|
struct event_base* evb,
|
|
|
|
std::function<bool()> const& test,
|
|
|
|
std::chrono::milliseconds msec = std::chrono::seconds{ 5 })
|
|
|
|
{
|
|
|
|
auto const deadline = std::chrono::steady_clock::now() + msec;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (test())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::chrono::steady_clock::now() > deadline)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:20:21 +00:00
|
|
|
event_base_loop(evb, EVLOOP_ONCE | EVLOOP_NONBLOCK);
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
class Sandbox
|
|
|
|
{
|
|
|
|
public:
|
2021-08-15 09:41:48 +00:00
|
|
|
Sandbox()
|
|
|
|
: parent_dir_{ get_default_parent_dir() }
|
|
|
|
, sandbox_dir_{ create_sandbox(parent_dir_, "transmission-test-XXXXXX") }
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Sandbox()
|
|
|
|
{
|
|
|
|
rimraf(sandbox_dir_);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& path() const
|
|
|
|
{
|
|
|
|
return sandbox_dir_;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static std::string get_default_parent_dir()
|
|
|
|
{
|
2022-04-02 22:42:51 +00:00
|
|
|
if (auto* const path = getenv("TMPDIR"); path != nullptr)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
|
|
|
return tr_sys_dir_get_current(&error);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string create_sandbox(std::string const& parent_dir, std::string const& tmpl)
|
|
|
|
{
|
2022-10-08 19:18:39 +00:00
|
|
|
auto path = fmt::format(FMT_STRING("{:s}/{:s}"sv), tr_sys_path_resolve(parent_dir), tmpl);
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_create_temp(std::data(path));
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_sys_path_native_separators(std::data(path));
|
2020-08-11 18:11:55 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rimraf(std::string const& path, bool verbose = false)
|
|
|
|
{
|
2022-04-01 19:16:33 +00:00
|
|
|
auto remove = [verbose](char const* filename)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-04-01 19:16:33 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::cerr << "cleanup: removing '" << filename << "'" << std::endl;
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
tr_sys_path_remove(filename);
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
depthFirstWalk(path.c_str(), remove);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string const parent_dir_;
|
|
|
|
std::string const sandbox_dir_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SandboxedTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
2021-08-15 09:41:48 +00:00
|
|
|
std::string sandboxDir() const
|
|
|
|
{
|
|
|
|
return sandbox_.path();
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
auto currentTestName() const
|
|
|
|
{
|
|
|
|
auto const* i = ::testing::UnitTest::GetInstance()->current_test_info();
|
|
|
|
auto child = std::string(i->test_suite_name());
|
|
|
|
child += '_';
|
|
|
|
child += i->name();
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
void buildParentDir(std::string_view path) const
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto const tmperr = errno;
|
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
auto dir = tr_pathbuf{ path };
|
|
|
|
dir.popdir();
|
2022-09-05 00:40:34 +00:00
|
|
|
if (auto const info = tr_sys_path_get_info(path); !info)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-09-05 00:40:34 +00:00
|
|
|
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &error);
|
2023-11-04 16:39:41 +00:00
|
|
|
EXPECT_FALSE(error) << "path[" << path << "] dir[" << dir << "] " << error;
|
2022-09-05 00:40:34 +00:00
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
static void blockingFileWrite(tr_sys_file_t fd, void const* data, size_t data_len, tr_error* error = nullptr)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
error = &local_error;
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
uint64_t n_left = data_len;
|
|
|
|
auto const* left = static_cast<uint8_t const*>(data);
|
|
|
|
while (n_left > 0)
|
|
|
|
{
|
|
|
|
uint64_t n = {};
|
2023-11-04 16:39:41 +00:00
|
|
|
if (!tr_sys_file_write(fd, left, n_left, &n, error))
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::print(stderr, "Error writing file: '{:s}'\n", error->message());
|
2020-08-11 18:11:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
left += n;
|
|
|
|
n_left -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
void createTmpfileWithContents(char* tmpl, void const* payload, size_t n) const
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto const tmperr = errno;
|
|
|
|
|
|
|
|
buildParentDir(tmpl);
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const fd = tr_sys_file_open_temp(tmpl, &error);
|
|
|
|
blockingFileWrite(fd, payload, n, &error);
|
|
|
|
tr_sys_file_flush(fd, &error);
|
|
|
|
tr_sys_file_flush(fd, &error);
|
|
|
|
tr_sys_file_close(fd, &error);
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
fmt::print(
|
|
|
|
"Couldn't create '{path}': {error} ({error_code})\n",
|
|
|
|
fmt::arg("path", tmpl),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code()));
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
sync();
|
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
void createFileWithContents(std::string_view path, void const* payload, size_t n) const
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto const tmperr = errno;
|
|
|
|
|
|
|
|
buildParentDir(path);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const fd = tr_sys_file_open(
|
2022-04-15 23:39:04 +00:00
|
|
|
tr_pathbuf{ path },
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
|
2021-08-15 09:41:48 +00:00
|
|
|
0600,
|
|
|
|
nullptr);
|
2020-08-11 18:11:55 +00:00
|
|
|
blockingFileWrite(fd, payload, n);
|
2022-08-10 13:34:51 +00:00
|
|
|
tr_sys_file_flush(fd);
|
|
|
|
tr_sys_file_flush(fd);
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
sync();
|
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
}
|
|
|
|
|
2022-04-29 02:35:47 +00:00
|
|
|
void createFileWithContents(std::string_view path, std::string_view payload) const
|
|
|
|
{
|
|
|
|
createFileWithContents(path, std::data(payload), std::size(payload));
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
void createFileWithContents(std::string_view path, void const* payload) const
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
createFileWithContents(path, payload, strlen(static_cast<char const*>(payload)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
void sync() const
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
::sync();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Sandbox sandbox_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SessionTest : public SandboxedTest
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<tr_variant> settings_;
|
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
tr_session* sessionInit(tr_variant& settings)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-10-02 21:18:35 +00:00
|
|
|
auto* const settings_map = settings.get_if<tr_variant::Map>();
|
|
|
|
EXPECT_NE(settings_map, nullptr);
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
// download dir
|
2023-10-02 21:18:35 +00:00
|
|
|
auto key = TR_KEY_download_dir;
|
|
|
|
auto val = settings_map->value_if<std::string_view>(key).value_or("Downloads"sv);
|
|
|
|
auto const download_dir = tr_pathbuf{ sandboxDir(), '/', val };
|
2022-05-22 01:17:00 +00:00
|
|
|
tr_sys_dir_create(download_dir, TR_SYS_DIR_CREATE_PARENTS, 0700);
|
2023-10-02 21:18:35 +00:00
|
|
|
(*settings_map)[key] = download_dir.sv();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// incomplete dir
|
2023-10-02 21:18:35 +00:00
|
|
|
key = TR_KEY_incomplete_dir;
|
|
|
|
val = settings_map->value_if<std::string_view>(key).value_or("Incomplete"sv);
|
|
|
|
auto const incomplete_dir = tr_pathbuf{ sandboxDir(), '/', val };
|
|
|
|
(*settings_map)[key] = incomplete_dir.sv();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// blocklists
|
2022-04-22 16:35:56 +00:00
|
|
|
tr_sys_dir_create(tr_pathbuf{ sandboxDir(), "/blocklists" }, TR_SYS_DIR_CREATE_PARENTS, 0700);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// fill in any missing settings
|
2023-10-02 21:18:35 +00:00
|
|
|
settings_map->try_emplace(TR_KEY_port_forwarding_enabled, false);
|
|
|
|
settings_map->try_emplace(TR_KEY_dht_enabled, false);
|
|
|
|
settings_map->try_emplace(TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
return tr_sessionInit(sandboxDir().data(), !verbose, settings);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sessionClose(tr_session* session)
|
|
|
|
{
|
|
|
|
tr_sessionClose(session);
|
|
|
|
tr_logFreeQueue(tr_logGetQueue());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-03-30 06:30:39 +00:00
|
|
|
enum class ZeroTorrentState
|
|
|
|
{
|
|
|
|
NoFiles,
|
|
|
|
Partial,
|
|
|
|
Complete
|
|
|
|
};
|
|
|
|
|
2022-11-10 05:56:08 +00:00
|
|
|
[[nodiscard]] tr_torrent* createTorrentAndWaitForVerifyDone(tr_ctor* ctor)
|
2022-09-06 04:43:59 +00:00
|
|
|
{
|
2022-11-10 05:56:08 +00:00
|
|
|
auto verified_lock = std::unique_lock(verified_mutex_);
|
2022-09-06 04:43:59 +00:00
|
|
|
auto const n_previously_verified = std::size(verified_);
|
2022-11-10 05:56:08 +00:00
|
|
|
|
2023-11-26 21:43:37 +00:00
|
|
|
ctor->set_verify_done_callback(
|
2023-10-19 13:39:34 +00:00
|
|
|
[this](tr_torrent* const tor)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto lambda_verified_lock = std::lock_guard{ verified_mutex_ };
|
2023-10-19 13:39:34 +00:00
|
|
|
verified_.emplace_back(tor);
|
|
|
|
verified_cv_.notify_one();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto* const tor = tr_torrentNew(ctor, nullptr);
|
2022-11-10 05:56:08 +00:00
|
|
|
auto const stop_waiting = [this, tor, n_previously_verified]()
|
|
|
|
{
|
|
|
|
return std::size(verified_) > n_previously_verified && verified_.back() == tor;
|
|
|
|
};
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
EXPECT_NE(nullptr, tor);
|
2022-11-10 05:56:08 +00:00
|
|
|
verified_cv_.wait_for(verified_lock, 20s, stop_waiting);
|
2022-09-06 04:43:59 +00:00
|
|
|
return tor;
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:56:08 +00:00
|
|
|
[[nodiscard]] tr_torrent* zeroTorrentInit(ZeroTorrentState state)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
// 1048576 files-filled-with-zeroes/1048576
|
|
|
|
// 4096 files-filled-with-zeroes/4096
|
|
|
|
// 512 files-filled-with-zeroes/512
|
2022-03-30 06:30:39 +00:00
|
|
|
char const* benc_base64 =
|
2020-08-11 18:11:55 +00:00
|
|
|
"ZDg6YW5ub3VuY2UzMTpodHRwOi8vd3d3LmV4YW1wbGUuY29tL2Fubm91bmNlMTA6Y3JlYXRlZCBi"
|
|
|
|
"eTI1OlRyYW5zbWlzc2lvbi8yLjYxICgxMzQwNykxMzpjcmVhdGlvbiBkYXRlaTEzNTg3MDQwNzVl"
|
|
|
|
"ODplbmNvZGluZzU6VVRGLTg0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpMTA0ODU3NmU0OnBhdGhs"
|
|
|
|
"NzoxMDQ4NTc2ZWVkNjpsZW5ndGhpNDA5NmU0OnBhdGhsNDo0MDk2ZWVkNjpsZW5ndGhpNTEyZTQ6"
|
|
|
|
"cGF0aGwzOjUxMmVlZTQ6bmFtZTI0OmZpbGVzLWZpbGxlZC13aXRoLXplcm9lczEyOnBpZWNlIGxl"
|
|
|
|
"bmd0aGkzMjc2OGU2OnBpZWNlczY2MDpRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj"
|
|
|
|
"/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv17"
|
|
|
|
"26aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGEx"
|
|
|
|
"Uv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJ"
|
|
|
|
"tGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GI"
|
|
|
|
"QxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZC"
|
|
|
|
"S1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8K"
|
|
|
|
"T9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9um"
|
|
|
|
"o/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9"
|
|
|
|
"e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRh"
|
|
|
|
"MVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMY"
|
|
|
|
"SbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLOlf5A+Tz30nMBVuNM2hpV3wg/103"
|
|
|
|
"OnByaXZhdGVpMGVlZQ==";
|
|
|
|
|
|
|
|
// create the torrent ctor
|
2022-03-30 06:30:39 +00:00
|
|
|
auto const benc = tr_base64_decode(benc_base64);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_LT(0U, std::size(benc));
|
2020-08-11 18:11:55 +00:00
|
|
|
auto* ctor = tr_ctorNew(session_);
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-03-30 06:30:39 +00:00
|
|
|
EXPECT_TRUE(tr_ctorSetMetainfo(ctor, std::data(benc), std::size(benc), &error));
|
2023-11-04 16:39:41 +00:00
|
|
|
EXPECT_FALSE(error) << error;
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, true);
|
|
|
|
|
2022-03-30 06:30:39 +00:00
|
|
|
// maybe create the files
|
|
|
|
if (state != ZeroTorrentState::NoFiles)
|
|
|
|
{
|
|
|
|
auto const* const metainfo = tr_ctorGetMetainfo(ctor);
|
2023-04-23 01:25:55 +00:00
|
|
|
for (tr_file_index_t i = 0, n = metainfo->file_count(); i < n; ++i)
|
2022-03-30 06:30:39 +00:00
|
|
|
{
|
|
|
|
auto const base = state == ZeroTorrentState::Partial && tr_sessionIsIncompleteDirEnabled(session_) ?
|
|
|
|
tr_sessionGetIncompleteDir(session_) :
|
|
|
|
tr_sessionGetDownloadDir(session_);
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const& subpath = metainfo->file_subpath(i);
|
2022-03-30 06:30:39 +00:00
|
|
|
auto const partial = state == ZeroTorrentState::Partial && i == 0;
|
|
|
|
auto const suffix = std::string_view{ partial ? ".part" : "" };
|
|
|
|
auto const filename = tr_pathbuf{ base, '/', subpath, suffix };
|
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
auto dirname = tr_pathbuf{ filename.sv() };
|
|
|
|
dirname.popdir();
|
|
|
|
tr_sys_dir_create(dirname, TR_SYS_DIR_CREATE_PARENTS, 0700);
|
2022-03-30 06:30:39 +00:00
|
|
|
|
|
|
|
auto fd = tr_sys_file_open(filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600);
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const file_size = metainfo->file_size(i);
|
2022-03-30 06:30:39 +00:00
|
|
|
for (uint64_t j = 0; j < file_size; ++j)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const ch = partial && j < metainfo->piece_size() ? '\1' : '\0';
|
2022-03-30 06:30:39 +00:00
|
|
|
tr_sys_file_write(fd, &ch, 1, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_file_flush(fd);
|
|
|
|
tr_sys_file_close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
auto* const tor = createTorrentAndWaitForVerifyDone(ctor);
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_ctorFree(ctor);
|
|
|
|
return tor;
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:56:08 +00:00
|
|
|
void blockingTorrentVerify(tr_torrent* tor)
|
2022-03-30 06:30:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, tor->session);
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_FALSE(tor->session->am_in_session_thread());
|
2022-11-10 05:56:08 +00:00
|
|
|
|
|
|
|
auto verified_lock = std::unique_lock(verified_mutex_);
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
auto const n_previously_verified = std::size(verified_);
|
2022-11-10 05:56:08 +00:00
|
|
|
auto const stop_waiting = [this, tor, n_previously_verified]()
|
|
|
|
{
|
|
|
|
return std::size(verified_) > n_previously_verified && verified_.back() == tor;
|
|
|
|
};
|
2022-03-30 06:30:39 +00:00
|
|
|
tr_torrentVerify(tor);
|
2022-11-10 05:56:08 +00:00
|
|
|
verified_cv_.wait_for(verified_lock, 20s, stop_waiting);
|
2022-03-30 06:30:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_session* session_ = nullptr;
|
|
|
|
|
|
|
|
tr_variant* settings()
|
|
|
|
{
|
|
|
|
if (!settings_)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto* settings = new tr_variant{};
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variantInitDict(settings, 10);
|
2023-08-21 04:15:23 +00:00
|
|
|
settings_.reset(settings);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return settings_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetUp() override
|
|
|
|
{
|
|
|
|
SandboxedTest::SetUp();
|
2021-08-16 01:45:50 +00:00
|
|
|
|
2023-07-06 03:05:03 +00:00
|
|
|
init_mgr_ = tr_lib_init();
|
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
session_ = sessionInit(*settings());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() override
|
|
|
|
{
|
|
|
|
sessionClose(session_);
|
|
|
|
session_ = nullptr;
|
|
|
|
settings_.reset();
|
|
|
|
|
|
|
|
SandboxedTest::TearDown();
|
|
|
|
}
|
2022-09-06 04:43:59 +00:00
|
|
|
|
|
|
|
private:
|
2022-11-10 05:56:08 +00:00
|
|
|
std::mutex verified_mutex_;
|
|
|
|
std::condition_variable verified_cv_;
|
2022-09-06 04:43:59 +00:00
|
|
|
std::vector<tr_torrent*> verified_;
|
2023-07-06 03:05:03 +00:00
|
|
|
|
|
|
|
std::unique_ptr<tr_net_init_mgr> init_mgr_;
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
} // namespace libtransmission
|