2022-03-01 23:06:29 +00:00
|
|
|
// This file Copyright (C) 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-03-01 23:06:29 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
#include <set>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2022-11-14 20:16:29 +00:00
|
|
|
#include <vector>
|
2022-03-28 22:13:32 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-03-01 23:06:29 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/torrent.h>
|
|
|
|
#include <libtransmission/torrents.h>
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2023-06-05 18:15:32 +00:00
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
using TorrentsTest = ::testing::Test;
|
|
|
|
|
|
|
|
TEST_F(TorrentsTest, simpleTests)
|
|
|
|
{
|
|
|
|
auto constexpr* const TorrentFile = LIBTRANSMISSION_TEST_ASSETS_DIR "/Android-x86 8.1 r6 iso.torrent";
|
2022-11-14 20:16:29 +00:00
|
|
|
|
|
|
|
auto owned = std::vector<std::unique_ptr<tr_torrent>>{};
|
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
auto tm = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(tm.parse_torrent_file(TorrentFile));
|
2022-11-14 20:16:29 +00:00
|
|
|
owned.emplace_back(std::make_unique<tr_torrent>(std::move(tm)));
|
|
|
|
auto* const tor = owned.back().get();
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
auto torrents = tr_torrents{};
|
|
|
|
EXPECT_TRUE(std::empty(torrents));
|
|
|
|
EXPECT_EQ(0U, std::size(torrents));
|
|
|
|
|
|
|
|
auto const id = torrents.add(tor);
|
|
|
|
EXPECT_GT(id, 0);
|
2022-06-17 15:43:04 +00:00
|
|
|
tor->unique_id_ = id;
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(std::empty(torrents.removedSince(0)));
|
|
|
|
EXPECT_FALSE(std::empty(torrents));
|
|
|
|
EXPECT_EQ(1U, std::size(torrents));
|
|
|
|
|
|
|
|
EXPECT_EQ(tor, torrents.get(id));
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(tor, torrents.get(tor->info_hash()));
|
2022-03-01 23:06:29 +00:00
|
|
|
EXPECT_EQ(tor, torrents.get(tor->magnet()));
|
|
|
|
|
|
|
|
tm = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(tm.parse_torrent_file(TorrentFile));
|
2022-03-01 23:06:29 +00:00
|
|
|
EXPECT_EQ(tor, torrents.get(tm));
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
torrents.remove(tor, time(nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TorrentsTest, rangedLoop)
|
|
|
|
{
|
|
|
|
auto constexpr Filenames = std::array<std::string_view, 4>{ "Android-x86 8.1 r6 iso.torrent"sv,
|
|
|
|
"debian-11.2.0-amd64-DVD-1.iso.torrent"sv,
|
|
|
|
"ubuntu-18.04.6-desktop-amd64.iso.torrent"sv,
|
|
|
|
"ubuntu-20.04.4-desktop-amd64.iso.torrent"sv };
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto owned = std::vector<std::unique_ptr<tr_torrent>>{};
|
2022-03-01 23:06:29 +00:00
|
|
|
auto torrents = tr_torrents{};
|
|
|
|
auto torrents_set = std::set<tr_torrent const*>{};
|
|
|
|
|
|
|
|
for (auto const& name : Filenames)
|
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const path = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, '/', name };
|
2022-03-01 23:06:29 +00:00
|
|
|
auto tm = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(tm.parse_torrent_file(path));
|
2022-11-14 20:16:29 +00:00
|
|
|
owned.emplace_back(std::make_unique<tr_torrent>(std::move(tm)));
|
|
|
|
|
|
|
|
auto* const tor = owned.back().get();
|
2022-06-17 15:43:04 +00:00
|
|
|
tor->unique_id_ = torrents.add(tor);
|
|
|
|
EXPECT_EQ(tor, torrents.get(tor->id()));
|
2022-03-01 23:06:29 +00:00
|
|
|
torrents_set.insert(tor);
|
|
|
|
}
|
|
|
|
|
2022-03-31 17:12:54 +00:00
|
|
|
for (auto* const tor : torrents)
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(1U, torrents_set.erase(tor));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0U, std::size(torrents_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TorrentsTest, removedSince)
|
|
|
|
{
|
|
|
|
auto constexpr Filenames = std::array<std::string_view, 4>{ "Android-x86 8.1 r6 iso.torrent"sv,
|
|
|
|
"debian-11.2.0-amd64-DVD-1.iso.torrent"sv,
|
|
|
|
"ubuntu-18.04.6-desktop-amd64.iso.torrent"sv,
|
|
|
|
"ubuntu-20.04.4-desktop-amd64.iso.torrent"sv };
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto owned = std::vector<std::unique_ptr<tr_torrent>>{};
|
2022-03-01 23:06:29 +00:00
|
|
|
auto torrents = tr_torrents{};
|
|
|
|
auto torrents_v = std::vector<tr_torrent const*>{};
|
|
|
|
torrents_v.reserve(std::size(Filenames));
|
|
|
|
|
|
|
|
// setup: add the torrents
|
|
|
|
for (auto const& name : Filenames)
|
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const path = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, '/', name };
|
2022-03-01 23:06:29 +00:00
|
|
|
auto tm = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(tm.parse_torrent_file(path));
|
2022-11-14 20:16:29 +00:00
|
|
|
owned.emplace_back(std::make_unique<tr_torrent>(std::move(tm)));
|
|
|
|
|
|
|
|
auto* const tor = owned.back().get();
|
2022-06-17 15:43:04 +00:00
|
|
|
tor->unique_id_ = torrents.add(tor);
|
2022-03-01 23:06:29 +00:00
|
|
|
torrents_v.push_back(tor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup: remove them at the given timestamp
|
|
|
|
auto constexpr TimeRemoved = std::array<time_t, 4>{ 100, 200, 200, 300 };
|
|
|
|
for (size_t i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
auto* const tor = torrents_v[i];
|
2022-06-17 15:43:04 +00:00
|
|
|
EXPECT_EQ(tor, torrents.get(tor->id()));
|
2022-03-01 23:06:29 +00:00
|
|
|
torrents.remove(torrents_v[i], TimeRemoved[i]);
|
2022-06-17 15:43:04 +00:00
|
|
|
EXPECT_EQ(nullptr, torrents.get(tor->id()));
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
auto remove = std::vector<tr_torrent_id_t>{};
|
|
|
|
remove = { torrents_v[3]->id() };
|
2022-03-01 23:06:29 +00:00
|
|
|
EXPECT_EQ(remove, torrents.removedSince(300));
|
|
|
|
EXPECT_EQ(remove, torrents.removedSince(201));
|
2022-06-17 15:43:04 +00:00
|
|
|
remove = { torrents_v[1]->id(), torrents_v[2]->id(), torrents_v[3]->id() };
|
2022-03-01 23:06:29 +00:00
|
|
|
EXPECT_EQ(remove, torrents.removedSince(200));
|
2022-06-17 15:43:04 +00:00
|
|
|
remove = { torrents_v[0]->id(), torrents_v[1]->id(), torrents_v[2]->id(), torrents_v[3]->id() };
|
2022-03-01 23:06:29 +00:00
|
|
|
EXPECT_EQ(remove, torrents.removedSince(50));
|
|
|
|
}
|
2023-06-05 18:15:32 +00:00
|
|
|
|
|
|
|
using TorrentsPieceSpanTest = libtransmission::test::SessionTest;
|
|
|
|
|
|
|
|
TEST_F(TorrentsPieceSpanTest, exposesFilePieceSpan)
|
|
|
|
{
|
|
|
|
auto tor = zeroTorrentInit(ZeroTorrentState::Complete);
|
|
|
|
auto file_view = tr_torrentFile(tor, 0);
|
|
|
|
EXPECT_EQ(file_view.beginPiece, 0);
|
|
|
|
EXPECT_EQ(file_view.endPiece, 32);
|
|
|
|
}
|