2020-08-11 18:11:55 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "makemeta.h"
|
|
|
|
#include "utils.h" // tr_free()
|
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdlib> // mktemp()
|
|
|
|
#include <cstring> // strlen()
|
|
|
|
#include <string>
|
|
|
|
|
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-10-18 23:05:39 +00:00
|
|
|
tr_info& inf,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const trackerCount,
|
|
|
|
void const* payload,
|
|
|
|
size_t const payloadSize,
|
|
|
|
char const* comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
bool isPrivate,
|
|
|
|
char const* source)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// create a single input file
|
2021-11-13 00:10:04 +00:00
|
|
|
auto input_file = tr_strvPath(sandboxDir().data(), "test.XXXXXX");
|
2020-08-11 18:11:55 +00:00
|
|
|
createTmpfileWithContents(input_file, payload, payloadSize);
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_sys_path_native_separators(std::data(input_file));
|
2020-08-11 18:11:55 +00:00
|
|
|
auto* builder = tr_metaInfoBuilderCreate(input_file.c_str());
|
|
|
|
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);
|
|
|
|
|
|
|
|
// have tr_makeMetaInfo() build the .torrent file
|
2021-11-13 00:10:04 +00:00
|
|
|
auto const torrent_file = tr_strvJoin(input_file, ".torrent");
|
|
|
|
tr_makeMetaInfo(builder, torrent_file.c_str(), trackers, trackerCount, comment, isPrivate, source);
|
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-10-18 23:05:39 +00:00
|
|
|
EXPECT_STREQ(source, builder->source);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(trackerCount, builder->trackerCount);
|
|
|
|
|
|
|
|
while (!builder->isDone)
|
|
|
|
{
|
|
|
|
tr_wait_msec(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now let's check our work: parse the .torrent file
|
|
|
|
auto* ctor = tr_ctorNew(nullptr);
|
|
|
|
sync();
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_ctorSetMetainfoFromFile(ctor, torrent_file.c_str());
|
2020-08-11 18:11:55 +00:00
|
|
|
auto const parse_result = tr_torrentParse(ctor, &inf);
|
|
|
|
EXPECT_EQ(TR_PARSE_OK, parse_result);
|
|
|
|
|
|
|
|
// quick check of some of the parsed metainfo
|
|
|
|
EXPECT_EQ(payloadSize, inf.totalSize);
|
|
|
|
EXPECT_EQ(makeString(tr_sys_path_basename(input_file.data(), nullptr)), inf.name);
|
|
|
|
EXPECT_STREQ(comment, inf.comment);
|
|
|
|
EXPECT_EQ(tr_file_index_t{ 1 }, inf.fileCount);
|
|
|
|
EXPECT_EQ(isPrivate, inf.isPrivate);
|
|
|
|
EXPECT_FALSE(inf.isFolder);
|
|
|
|
EXPECT_EQ(trackerCount, inf.trackerCount);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
tr_metaInfoBuilderFree(builder);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void testSingleDirectoryImpl(
|
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const tracker_count,
|
|
|
|
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
|
2021-11-13 00:10:04 +00:00
|
|
|
auto top = tr_strvPath(sandboxDir(), "folder.XXXXXX");
|
|
|
|
tr_sys_path_native_separators(std::data(top));
|
|
|
|
tr_sys_dir_create_temp(std::data(top), nullptr);
|
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++)
|
|
|
|
{
|
|
|
|
auto tmpl = std::array<char, 16>{};
|
|
|
|
tr_snprintf(tmpl.data(), tmpl.size(), "file.%04zu%s", i, "XXXXXX");
|
2021-11-13 00:10:04 +00:00
|
|
|
auto path = tr_strvPath(top, std::data(tmpl));
|
2020-08-11 18:11:55 +00:00
|
|
|
createTmpfileWithContents(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
|
2021-11-13 00:10:04 +00:00
|
|
|
auto* builder = tr_metaInfoBuilderCreate(top.c_str());
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the .torrent file
|
2021-11-13 00:10:04 +00:00
|
|
|
auto torrent_file = tr_strvJoin(top, ".torrent"sv);
|
|
|
|
tr_makeMetaInfo(builder, torrent_file.c_str(), trackers, tracker_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();
|
|
|
|
|
|
|
|
// now let's check our work: parse the .torrent file
|
|
|
|
auto* ctor = tr_ctorNew(nullptr);
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_ctorSetMetainfoFromFile(ctor, torrent_file.c_str());
|
2021-10-06 14:26:07 +00:00
|
|
|
auto inf = tr_info{};
|
2020-08-11 18:11:55 +00:00
|
|
|
auto parse_result = tr_torrentParse(ctor, &inf);
|
|
|
|
EXPECT_EQ(TR_PARSE_OK, parse_result);
|
|
|
|
|
|
|
|
// quick check of some of the parsed metainfo
|
|
|
|
EXPECT_EQ(total_size, inf.totalSize);
|
2021-11-13 00:10:04 +00:00
|
|
|
auto* tmpstr = tr_sys_path_basename(top.c_str(), nullptr);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_STREQ(tmpstr, inf.name);
|
|
|
|
tr_free(tmpstr);
|
|
|
|
EXPECT_STREQ(comment, inf.comment);
|
2021-10-18 23:05:39 +00:00
|
|
|
EXPECT_STREQ(source, inf.source);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(payload_count, inf.fileCount);
|
|
|
|
EXPECT_EQ(is_private, inf.isPrivate);
|
|
|
|
EXPECT_EQ(builder->isFolder, inf.isFolder);
|
|
|
|
EXPECT_EQ(tracker_count, inf.trackerCount);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
tr_metainfoFree(&inf);
|
|
|
|
tr_metaInfoBuilderFree(builder);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void testSingleDirectoryRandomPayloadImpl(
|
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int const tracker_count,
|
|
|
|
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);
|
|
|
|
void** payloads = tr_new0(void*, payload_count);
|
|
|
|
size_t* payload_sizes = tr_new0(size_t, payload_count);
|
|
|
|
|
|
|
|
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,
|
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-10-18 23:05:39 +00:00
|
|
|
char const* const source = "TESTME";
|
|
|
|
tr_info inf{};
|
|
|
|
testSingleFileImpl(inf, trackers.data(), tracker_count, payload.data(), payload.size(), comment, is_private, source);
|
|
|
|
tr_metainfoFree(&inf);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
tr_info inf_foobar{};
|
|
|
|
testSingleFileImpl(
|
|
|
|
inf_foobar,
|
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
|
|
|
"FOOBAR");
|
|
|
|
|
|
|
|
tr_info inf_testme{};
|
|
|
|
testSingleFileImpl(
|
|
|
|
inf_testme,
|
|
|
|
trackers.data(),
|
|
|
|
tracker_count,
|
|
|
|
payload.data(),
|
|
|
|
payload.size(),
|
|
|
|
comment,
|
|
|
|
is_private,
|
|
|
|
"TESTME");
|
|
|
|
|
|
|
|
EXPECT_TRUE(std::memcmp(inf_foobar.hash, inf_testme.hash, sizeof(inf_foobar.hash)) != 0);
|
|
|
|
|
|
|
|
tr_metainfoFree(&inf_foobar);
|
|
|
|
tr_metainfoFree(&inf_testme);
|
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,
|
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
|