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
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <algorithm>
|
2021-12-26 14:30:24 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstdlib> // mktemp()
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <numeric>
|
2021-12-26 14:30:24 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2021-12-26 14:30:24 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-03 18:23:00 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-12-26 14:30:24 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/crypto-utils.h>
|
|
|
|
#include <libtransmission/file.h>
|
|
|
|
#include <libtransmission/makemeta.h>
|
|
|
|
#include <libtransmission/session.h> // TR_NAME
|
|
|
|
#include <libtransmission/torrent-metainfo.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;
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class MakemetaTest : public SandboxedTest
|
|
|
|
{
|
|
|
|
protected:
|
2022-08-02 19:46:08 +00:00
|
|
|
static auto constexpr DefaultMaxFileCount = size_t{ 16 };
|
|
|
|
static auto constexpr DefaultMaxFileSize = size_t{ 1024 };
|
|
|
|
|
|
|
|
auto makeRandomFiles(
|
|
|
|
std::string_view top,
|
2022-12-14 18:21:56 +00:00
|
|
|
size_t n_files = std::max(size_t{ 1U }, static_cast<size_t>(tr_rand_int(DefaultMaxFileCount))),
|
2022-08-02 19:46:08 +00:00
|
|
|
size_t max_size = DefaultMaxFileSize)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto files = std::vector<std::pair<std::string, std::vector<std::byte>>>{};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
for (size_t i = 0; i < n_files; ++i)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto payload = std::vector<std::byte>{};
|
|
|
|
// TODO(5.0.0): zero-sized files are disabled in these test
|
|
|
|
// because tr_torrent_metainfo discards them, throwing off the
|
|
|
|
// builder-to-metainfo comparisons here. tr_torrent_metainfo
|
|
|
|
// will behave when BEP52 support is added in Transmission 5.
|
|
|
|
static auto constexpr MinFileSize = size_t{ 1U };
|
2022-12-14 18:21:56 +00:00
|
|
|
payload.resize(std::max(MinFileSize, static_cast<size_t>(tr_rand_int(max_size))));
|
2022-08-02 19:46:08 +00:00
|
|
|
tr_rand_buffer(std::data(payload), std::size(payload));
|
|
|
|
|
|
|
|
auto filename = tr_pathbuf{ top, '/', "test.XXXXXX" };
|
|
|
|
createTmpfileWithContents(std::data(filename), std::data(payload), std::size(payload));
|
|
|
|
tr_sys_path_native_separators(std::data(filename));
|
|
|
|
|
|
|
|
files.emplace_back(std::string{ filename.sv() }, payload);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
return files;
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
static auto testBuilder(tr_metainfo_builder& builder)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
tr_error* error = builder.make_checksums().get();
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(error, nullptr) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(metainfo.parse_benc(builder.benc()));
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(builder.file_count(), metainfo.file_count());
|
|
|
|
EXPECT_EQ(builder.piece_size(), metainfo.piece_size());
|
|
|
|
EXPECT_EQ(builder.total_size(), metainfo.total_size());
|
|
|
|
EXPECT_EQ(builder.total_size(), metainfo.total_size());
|
|
|
|
for (size_t i = 0, n = std::min(builder.file_count(), metainfo.file_count()); i < n; ++i)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(builder.file_size(i), metainfo.files().fileSize(i));
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(builder.path(i), metainfo.files().path(i));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(builder.name(), metainfo.name());
|
|
|
|
EXPECT_EQ(builder.comment(), metainfo.comment());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(builder.is_private(), metainfo.is_private());
|
|
|
|
EXPECT_EQ(builder.announce_list().to_string(), metainfo.announce_list().to_string());
|
2022-08-02 19:46:08 +00:00
|
|
|
return metainfo;
|
|
|
|
}
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, comment)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
static auto constexpr Comment = "This is the comment"sv;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_comment(Comment);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(Comment, testBuilder(builder).comment());
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, source)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
static auto constexpr Source = "This is the source"sv;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_source(Source);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(Source, testBuilder(builder).source());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, isPrivate)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
|
|
|
|
for (bool const is_private : { true, false })
|
|
|
|
{
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_private(is_private);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(is_private, testBuilder(builder).is_private());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, pieceSize)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
for (uint32_t const piece_size : { 16384, 32768 })
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_piece_size(piece_size);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(piece_size, testBuilder(builder).piece_size());
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, webseeds)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
static auto constexpr Webseed = "https://www.example.com/linux.iso"sv;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_webseeds(std::vector<std::string>{ std::string{ Webseed } });
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const metainfo = testBuilder(builder);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(1U, metainfo.webseed_count());
|
2022-08-02 19:46:08 +00:00
|
|
|
EXPECT_EQ(Webseed, metainfo.webseed(0));
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, nameIsRootSingleFile)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
EXPECT_EQ(tr_sys_path_basename(filename), testBuilder(builder).name());
|
2022-02-12 18:50:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, anonymizeTrue)
|
2022-02-12 18:50:47 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_anonymize(true);
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const metainfo = testBuilder(builder);
|
|
|
|
EXPECT_EQ(""sv, metainfo.creator());
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(time_t{}, metainfo.date_created());
|
2021-10-18 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, anonymizeFalse)
|
2021-10-18 23:05:39 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_anonymize(false);
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const metainfo = testBuilder(builder);
|
|
|
|
EXPECT_TRUE(tr_strvContains(metainfo.creator(), TR_NAME)) << metainfo.creator();
|
|
|
|
auto const now = time(nullptr);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_LE(metainfo.date_created(), now);
|
|
|
|
EXPECT_LE(now - 60, metainfo.date_created());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
TEST_F(MakemetaTest, nameIsRootMultifile)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 10);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
EXPECT_EQ(tr_sys_path_basename(filename), testBuilder(builder).name());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, singleFile)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
trackers.add("udp://tracker.openbittorrent.com:80"sv, trackers.nextTier());
|
|
|
|
trackers.add("udp://tracker.publicbt.com:80"sv, trackers.nextTier());
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_announce_list(std::move(trackers));
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
static auto constexpr Comment = "This is the comment"sv;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_comment(Comment);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
static auto constexpr IsPrivate = false;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_private(IsPrivate);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
static auto constexpr Anonymize = false;
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_anonymize(Anonymize);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
testBuilder(builder);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 19:10:20 +00:00
|
|
|
TEST_F(MakemetaTest, announceSingleTracker)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
|
|
|
|
// add a tracker
|
|
|
|
static auto constexpr SingleAnnounce = "udp://tracker.openbittorrent.com:80"sv;
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
trackers.add(SingleAnnounce, trackers.nextTier());
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_announce_list(std::move(trackers));
|
2023-03-01 19:10:20 +00:00
|
|
|
|
|
|
|
// generate the torrent and parse it as a variant
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(nullptr, builder.make_checksums().get());
|
2023-03-01 19:10:20 +00:00
|
|
|
auto top = tr_variant{};
|
|
|
|
auto const benc = builder.benc();
|
|
|
|
EXPECT_TRUE(tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc));
|
|
|
|
|
|
|
|
// confirm there's an "announce" entry
|
|
|
|
auto single_announce = std::string_view{};
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&top, TR_KEY_announce, &single_announce));
|
|
|
|
EXPECT_EQ(SingleAnnounce, single_announce);
|
|
|
|
|
|
|
|
// confirm there's not an "announce-list" entry
|
|
|
|
EXPECT_EQ(nullptr, tr_variantDictFind(&top, TR_KEY_announce_list));
|
|
|
|
|
|
|
|
tr_variantClear(&top);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MakemetaTest, announceMultiTracker)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
|
|
|
|
// add the trackers
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
for (auto const& url : { "udp://tracker.openbittorrent.com:80"sv, "udp://tracker.publicbt.com:80"sv })
|
|
|
|
{
|
|
|
|
trackers.add(url, trackers.nextTier());
|
|
|
|
}
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_announce_list(std::move(trackers));
|
2023-03-01 19:10:20 +00:00
|
|
|
|
|
|
|
// generate the torrent and parse it as a variant
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(nullptr, builder.make_checksums().get());
|
2023-03-01 19:10:20 +00:00
|
|
|
auto top = tr_variant{};
|
|
|
|
auto const benc = builder.benc();
|
|
|
|
EXPECT_TRUE(tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc));
|
|
|
|
|
|
|
|
// confirm there's an "announce" entry
|
|
|
|
auto single_announce = std::string_view{};
|
|
|
|
EXPECT_TRUE(tr_variantDictFindStrView(&top, TR_KEY_announce, &single_announce));
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(builder.announce_list().at(0).announce.sv(), single_announce);
|
2023-03-01 19:10:20 +00:00
|
|
|
|
|
|
|
// confirm there's an "announce-list" entry
|
|
|
|
tr_variant* announce_list_variant = nullptr;
|
|
|
|
EXPECT_TRUE(tr_variantDictFindList(&top, TR_KEY_announce_list, &announce_list_variant));
|
|
|
|
EXPECT_NE(nullptr, announce_list_variant);
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_EQ(std::size(builder.announce_list()), tr_variantListSize(announce_list_variant));
|
2023-03-01 19:10:20 +00:00
|
|
|
|
|
|
|
tr_variantClear(&top);
|
|
|
|
}
|
|
|
|
|
2023-02-11 02:33:24 +00:00
|
|
|
TEST_F(MakemetaTest, privateAndSourceHasDifferentInfoHash)
|
|
|
|
{
|
|
|
|
auto const files = makeRandomFiles(sandboxDir(), 1);
|
|
|
|
auto const [filename, payload] = files.front();
|
|
|
|
auto builder = tr_metainfo_builder{ filename };
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
trackers.add("udp://tracker.openbittorrent.com:80"sv, trackers.nextTier());
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_announce_list(std::move(trackers));
|
2023-03-01 19:10:20 +00:00
|
|
|
auto base_metainfo = testBuilder(builder);
|
2023-02-11 02:33:24 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_private(true);
|
2023-03-01 19:10:20 +00:00
|
|
|
auto private_metainfo = testBuilder(builder);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_NE(base_metainfo.info_hash(), private_metainfo.info_hash());
|
2023-02-11 02:33:24 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
builder.set_source("FOO");
|
2023-03-01 19:10:20 +00:00
|
|
|
auto private_source_metainfo = testBuilder(builder);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_NE(base_metainfo.info_hash(), private_source_metainfo.info_hash());
|
|
|
|
EXPECT_NE(private_metainfo.info_hash(), private_source_metainfo.info_hash());
|
2023-02-11 02:33:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|