2022-03-01 23:06:29 +00:00
|
|
|
// This file Copyright (C) 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.
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <set>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2022-03-28 22:13:32 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrents.h"
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
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";
|
|
|
|
auto tm = tr_torrent_metainfo{};
|
|
|
|
EXPECT_TRUE(tm.parseTorrentFile(TorrentFile));
|
|
|
|
auto* tor = new tr_torrent(std::move(tm));
|
|
|
|
EXPECT_NE(nullptr, tor);
|
|
|
|
|
|
|
|
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));
|
|
|
|
EXPECT_EQ(tor, torrents.get(tor->infoHash()));
|
|
|
|
EXPECT_EQ(tor, torrents.get(tor->magnet()));
|
|
|
|
|
|
|
|
tm = tr_torrent_metainfo{};
|
|
|
|
EXPECT_TRUE(tm.parseTorrentFile(TorrentFile));
|
|
|
|
EXPECT_EQ(tor, torrents.get(tm));
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
torrents.remove(tor, time(nullptr));
|
|
|
|
delete tor;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
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{};
|
|
|
|
EXPECT_TRUE(tm.parseTorrentFile(path));
|
2022-03-31 17:12:54 +00:00
|
|
|
auto* const tor = new tr_torrent{ std::move(tm) };
|
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));
|
2022-03-31 17:12:54 +00:00
|
|
|
delete tor;
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(0U, std::size(torrents_set));
|
|
|
|
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 };
|
|
|
|
|
|
|
|
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{};
|
2022-03-31 17:12:54 +00:00
|
|
|
auto* const tor = new tr_torrent{ std::move(tm) };
|
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));
|
2022-03-31 17:12:54 +00:00
|
|
|
|
|
|
|
std::for_each(std::begin(torrents_v), std::end(torrents_v), [](auto* tor) { delete tor; });
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|