2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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-12-26 14:30:24 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstdlib> // mktemp()
|
|
|
|
#include <cstring> // strlen()
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "transmission.h"
|
2021-12-26 14:30:24 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "makemeta.h"
|
2021-12-26 14:30:24 +00:00
|
|
|
#include "torrent-metainfo.h"
|
|
|
|
#include "utils.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
class MakemetaTest : public SandboxedTest
|
|
|
|
{
|
|
|
|
protected:
|
2021-08-15 09:41:48 +00:00
|
|
|
void testSingleFileImpl(
|
2021-12-26 14:30:24 +00:00
|
|
|
tr_torrent_metainfo& metainfo,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const trackerCount,
|
2022-02-12 18:50:47 +00:00
|
|
|
char const** webseeds,
|
|
|
|
int const webseedCount,
|
2021-08-15 09:41:48 +00:00
|
|
|
void const* payload,
|
|
|
|
size_t const payloadSize,
|
|
|
|
char const* comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
bool isPrivate,
|
2021-12-26 14:30:24 +00:00
|
|
|
std::string_view source)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// create a single input file
|
2022-04-03 18:23:00 +00:00
|
|
|
auto input_file = tr_pathbuf{ sandboxDir(), '/', "test.XXXXXX" };
|
|
|
|
createTmpfileWithContents(std::data(input_file), payload, payloadSize);
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_sys_path_native_separators(std::data(input_file));
|
2022-04-03 18:23:00 +00:00
|
|
|
auto* builder = tr_metaInfoBuilderCreate(input_file);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(tr_file_index_t{ 1 }, builder->fileCount);
|
2021-11-13 00:10:04 +00:00
|
|
|
EXPECT_EQ(input_file, builder->top);
|
|
|
|
EXPECT_EQ(input_file, builder->files[0].filename);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(payloadSize, builder->files[0].size);
|
|
|
|
EXPECT_EQ(payloadSize, builder->totalSize);
|
|
|
|
EXPECT_FALSE(builder->isFolder);
|
|
|
|
EXPECT_FALSE(builder->abortFlag);
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// have tr_makeMetaInfo() build the torrent file
|
2022-04-03 18:23:00 +00:00
|
|
|
auto const torrent_file = tr_pathbuf{ input_file, ".torrent"sv };
|
2022-02-12 18:50:47 +00:00
|
|
|
tr_makeMetaInfo(
|
|
|
|
builder,
|
2022-04-03 18:23:00 +00:00
|
|
|
torrent_file,
|
2022-02-12 18:50:47 +00:00
|
|
|
trackers,
|
|
|
|
trackerCount,
|
|
|
|
webseeds,
|
|
|
|
webseedCount,
|
|
|
|
comment,
|
|
|
|
isPrivate,
|
|
|
|
std::string(source).c_str());
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(isPrivate, builder->isPrivate);
|
2021-11-13 00:10:04 +00:00
|
|
|
EXPECT_EQ(torrent_file, builder->outputFile);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_STREQ(comment, builder->comment);
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(source, builder->source);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(trackerCount, builder->trackerCount);
|
|
|
|
|
|
|
|
while (!builder->isDone)
|
|
|
|
{
|
|
|
|
tr_wait_msec(100);
|
|
|
|
}
|
2021-12-26 14:30:24 +00:00
|
|
|
sync();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// now let's check our work: parse the torrent file
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_TRUE(metainfo.parseTorrentFile(torrent_file));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// quick check of some of the parsed metainfo
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(payloadSize, metainfo.totalSize());
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(tr_sys_path_basename(input_file), metainfo.name());
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(comment, metainfo.comment());
|
|
|
|
EXPECT_EQ(isPrivate, metainfo.isPrivate());
|
|
|
|
EXPECT_EQ(size_t(trackerCount), std::size(metainfo.announceList()));
|
2022-02-12 18:50:47 +00:00
|
|
|
EXPECT_EQ(size_t(webseedCount), metainfo.webseedCount());
|
2022-01-29 21:37:42 +00:00
|
|
|
EXPECT_EQ(tr_file_index_t{ 1 }, metainfo.fileCount());
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(tr_sys_path_basename(input_file), metainfo.fileSubpath(0));
|
2022-01-29 21:37:42 +00:00
|
|
|
EXPECT_EQ(payloadSize, metainfo.fileSize(0));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_metaInfoBuilderFree(builder);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void testSingleDirectoryImpl(
|
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
char const** webseeds,
|
|
|
|
int const webseed_count,
|
2021-08-15 09:41:48 +00:00
|
|
|
void const** payloads,
|
|
|
|
size_t const* payload_sizes,
|
|
|
|
size_t const payload_count,
|
|
|
|
char const* comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
bool const is_private,
|
|
|
|
char const* source)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
// create the top temp directory
|
2022-04-03 18:23:00 +00:00
|
|
|
auto top = tr_pathbuf{ sandboxDir(), '/', "folder.XXXXXX"sv };
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_sys_path_native_separators(std::data(top));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_create_temp(std::data(top));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// build the payload files that go into the top temp directory
|
|
|
|
auto files = std::vector<std::string>{};
|
|
|
|
files.reserve(payload_count);
|
|
|
|
size_t total_size = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < payload_count; i++)
|
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
auto path = fmt::format(FMT_STRING("{:s}/file.{:04}XXXXXX"), top.sv(), i);
|
|
|
|
createTmpfileWithContents(std::data(path), payloads[i], payload_sizes[i]);
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_sys_path_native_separators(std::data(path));
|
2020-08-11 18:11:55 +00:00
|
|
|
files.push_back(path);
|
|
|
|
total_size += payload_sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
sync();
|
|
|
|
|
|
|
|
// init the builder
|
2022-04-03 18:23:00 +00:00
|
|
|
auto* builder = tr_metaInfoBuilderCreate(top);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(builder->abortFlag);
|
2021-11-13 00:10:04 +00:00
|
|
|
EXPECT_EQ(top, builder->top);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(payload_count, builder->fileCount);
|
|
|
|
EXPECT_EQ(total_size, builder->totalSize);
|
|
|
|
EXPECT_TRUE(builder->isFolder);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < builder->fileCount; ++i)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(files[i], builder->files[i].filename);
|
|
|
|
EXPECT_EQ(payload_sizes[i], builder->files[i].size);
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// build the torrent file
|
2022-04-03 18:23:00 +00:00
|
|
|
auto const torrent_file = tr_pathbuf{ top, ".torrent"sv };
|
2022-02-12 18:50:47 +00:00
|
|
|
tr_makeMetaInfo(
|
|
|
|
builder,
|
|
|
|
torrent_file.c_str(),
|
|
|
|
trackers,
|
|
|
|
tracker_count,
|
|
|
|
webseeds,
|
|
|
|
webseed_count,
|
|
|
|
comment,
|
|
|
|
is_private,
|
|
|
|
source);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(is_private, builder->isPrivate);
|
2021-11-13 00:10:04 +00:00
|
|
|
EXPECT_EQ(torrent_file, builder->outputFile);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_STREQ(comment, builder->comment);
|
2021-10-18 23:05:39 +00:00
|
|
|
EXPECT_STREQ(source, builder->source);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(tracker_count, builder->trackerCount);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto test = [&builder]()
|
|
|
|
{
|
|
|
|
return builder->isDone;
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(waitFor(test, 5000));
|
|
|
|
sync();
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// now let's check our work: parse the torrent file
|
2021-12-26 14:30:24 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
EXPECT_TRUE(metainfo.parseTorrentFile(torrent_file));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// quick check of some of the parsed metainfo
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(total_size, metainfo.totalSize());
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(tr_sys_path_basename(top), metainfo.name());
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(comment, metainfo.comment());
|
|
|
|
EXPECT_EQ(source, metainfo.source());
|
2022-01-08 18:53:35 +00:00
|
|
|
EXPECT_EQ(payload_count, metainfo.fileCount());
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_EQ(is_private, metainfo.isPrivate());
|
|
|
|
EXPECT_EQ(size_t(tracker_count), std::size(metainfo.announceList()));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_metaInfoBuilderFree(builder);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void testSingleDirectoryRandomPayloadImpl(
|
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
char const** webseeds,
|
|
|
|
int const webseed_count,
|
2021-08-15 09:41:48 +00:00
|
|
|
size_t const max_file_count,
|
|
|
|
size_t const max_file_size,
|
|
|
|
char const* comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
bool const is_private,
|
|
|
|
char const* source)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
// build random payloads
|
|
|
|
size_t payload_count = 1 + tr_rand_int_weak(max_file_count);
|
2022-04-02 22:42:51 +00:00
|
|
|
auto** payloads = tr_new0(void*, payload_count);
|
|
|
|
auto* payload_sizes = tr_new0(size_t, payload_count);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < payload_count; i++)
|
|
|
|
{
|
|
|
|
size_t const n = 1 + tr_rand_int_weak(max_file_size);
|
|
|
|
payloads[i] = tr_new(char, n);
|
|
|
|
tr_rand_buffer(payloads[i], n);
|
|
|
|
payload_sizes[i] = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the test
|
|
|
|
testSingleDirectoryImpl(
|
2021-08-15 09:41:48 +00:00
|
|
|
trackers,
|
|
|
|
tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
webseeds,
|
|
|
|
webseed_count,
|
2020-08-11 18:11:55 +00:00
|
|
|
const_cast<void const**>(payloads),
|
|
|
|
payload_sizes,
|
|
|
|
payload_count,
|
2021-08-15 09:41:48 +00:00
|
|
|
comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
is_private,
|
|
|
|
source);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
for (size_t i = 0; i < payload_count; i++)
|
|
|
|
{
|
|
|
|
tr_free(payloads[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(payloads);
|
|
|
|
tr_free(payload_sizes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, singleFile)
|
|
|
|
{
|
|
|
|
auto trackers = std::array<tr_tracker_info, 16>{};
|
|
|
|
auto tracker_count = int{};
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.openbittorrent.com:80");
|
|
|
|
++tracker_count;
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.publicbt.com:80");
|
|
|
|
++tracker_count;
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const payload = std::string{ "Hello, World!\n" };
|
2020-08-11 18:11:55 +00:00
|
|
|
char const* const comment = "This is the comment";
|
|
|
|
bool const is_private = false;
|
2021-12-26 14:30:24 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
testSingleFileImpl(
|
|
|
|
metainfo,
|
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
|
|
|
"TESTME"sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, webseed)
|
|
|
|
{
|
|
|
|
auto trackers = std::vector<tr_tracker_info>{};
|
|
|
|
auto webseeds = std::vector<char const*>{};
|
|
|
|
|
|
|
|
webseeds.emplace_back("https://www.example.com/linux.iso");
|
|
|
|
|
|
|
|
auto const payload = std::string{ "Hello, World!\n" };
|
|
|
|
char const* const comment = "This is the comment";
|
|
|
|
bool const is_private = false;
|
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
testSingleFileImpl(
|
|
|
|
metainfo,
|
|
|
|
std::data(trackers),
|
|
|
|
std::size(trackers),
|
|
|
|
std::data(webseeds),
|
|
|
|
std::size(webseeds),
|
2021-12-26 14:30:24 +00:00
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
|
|
|
"TESTME"sv);
|
2021-10-18 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, singleFileDifferentSourceFlags)
|
|
|
|
{
|
|
|
|
auto trackers = std::array<tr_tracker_info, 16>{};
|
|
|
|
auto tracker_count = int{};
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.openbittorrent.com:80");
|
|
|
|
++tracker_count;
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.publicbt.com:80");
|
|
|
|
++tracker_count;
|
|
|
|
auto const payload = std::string{ "Hello, World!\n" };
|
|
|
|
char const* const comment = "This is the comment";
|
|
|
|
bool const is_private = false;
|
|
|
|
|
2021-12-26 14:30:24 +00:00
|
|
|
auto metainfo_foobar = tr_torrent_metainfo{};
|
2021-10-18 23:05:39 +00:00
|
|
|
testSingleFileImpl(
|
2021-12-26 14:30:24 +00:00
|
|
|
metainfo_foobar,
|
2021-10-18 23:05:39 +00:00
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
nullptr,
|
|
|
|
0,
|
2021-10-18 23:05:39 +00:00
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
2021-12-26 14:30:24 +00:00
|
|
|
"FOOBAR"sv);
|
2021-10-18 23:05:39 +00:00
|
|
|
|
2021-12-26 14:30:24 +00:00
|
|
|
auto metainfo_testme = tr_torrent_metainfo{};
|
2021-10-18 23:05:39 +00:00
|
|
|
testSingleFileImpl(
|
2021-12-26 14:30:24 +00:00
|
|
|
metainfo_testme,
|
2021-10-18 23:05:39 +00:00
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
nullptr,
|
|
|
|
0,
|
2021-10-18 23:05:39 +00:00
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
2021-12-26 14:30:24 +00:00
|
|
|
"TESTME"sv);
|
2021-10-18 23:05:39 +00:00
|
|
|
|
2021-12-26 14:30:24 +00:00
|
|
|
EXPECT_NE(metainfo_foobar.infoHash(), metainfo_testme.infoHash());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, singleDirectoryRandomPayload)
|
|
|
|
{
|
|
|
|
auto constexpr DefaultMaxFileCount = size_t{ 16 };
|
|
|
|
auto constexpr DefaultMaxFileSize = size_t{ 1024 };
|
|
|
|
|
|
|
|
auto trackers = std::array<tr_tracker_info, 16>{};
|
|
|
|
auto tracker_count = int{};
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.openbittorrent.com:80");
|
|
|
|
++tracker_count;
|
|
|
|
trackers[tracker_count].tier = tracker_count;
|
|
|
|
trackers[tracker_count].announce = const_cast<char*>("udp://tracker.publicbt.com:80");
|
|
|
|
++tracker_count;
|
|
|
|
char const* const comment = "This is the comment";
|
|
|
|
bool const is_private = false;
|
2021-10-18 23:05:39 +00:00
|
|
|
char const* const source = "TESTME";
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
testSingleDirectoryRandomPayloadImpl(
|
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
2022-02-12 18:50:47 +00:00
|
|
|
nullptr,
|
|
|
|
0,
|
2020-08-11 18:11:55 +00:00
|
|
|
DefaultMaxFileCount,
|
|
|
|
DefaultMaxFileSize,
|
2021-08-15 09:41:48 +00:00
|
|
|
comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
is_private,
|
|
|
|
source);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
} // namespace libtransmission
|