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
|
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2022-01-18 05:14:00 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
#include "file.h"
|
|
|
|
|
#include "resume.h"
|
|
|
|
|
#include "torrent.h" // tr_isTorrent()
|
|
|
|
|
#include "tr-assert.h"
|
2022-04-22 16:35:56 +00:00
|
|
|
|
#include "tr-strbuf.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
#include "variant.h"
|
|
|
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <cstdio> // fopen()
|
|
|
|
|
#include <string>
|
2022-04-08 01:50:26 +00:00
|
|
|
|
#include <string_view>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
namespace libtransmission
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class RenameTest : public SessionTest
|
|
|
|
|
{
|
|
|
|
|
static auto constexpr MaxWaitMsec = 3000;
|
|
|
|
|
|
|
|
|
|
protected:
|
2022-08-24 21:03:30 +00:00
|
|
|
|
void torrentRemoveAndWait(tr_torrent* tor, size_t expected_torrent_count)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
tr_torrentRemove(tor, false, nullptr);
|
|
|
|
|
auto const test = [this, expected_torrent_count]()
|
2021-08-15 09:41:48 +00:00
|
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
|
return std::size(session_->torrents()) == expected_torrent_count;
|
2021-08-15 09:41:48 +00:00
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_TRUE(waitFor(test, MaxWaitMsec));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
|
void createSingleFileTorrentContents(std::string_view top)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
2022-04-22 16:35:56 +00:00
|
|
|
|
auto const path = tr_pathbuf{ top, "/hello-world.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
createFileWithContents(path, "hello, world!\n");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
|
void createMultifileTorrentContents(std::string_view top)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
2022-04-22 16:35:56 +00:00
|
|
|
|
auto path = tr_pathbuf{ top, "/Felidae/Felinae/Acinonyx/Cheetah/Chester"sv };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
createFileWithContents(path, "It ain't easy bein' cheesy.\n");
|
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
|
path.assign(top, "/Felidae/Pantherinae/Panthera/Tiger/Tony"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
createFileWithContents(path, "They’re Grrrrreat!\n");
|
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
|
path.assign(top, "/Felidae/Felinae/Felis/catus/Kyphi"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
createFileWithContents(path, "Inquisitive\n");
|
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
|
path.assign(top, "/Felidae/Felinae/Felis/catus/Saffron"sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
createFileWithContents(path, "Tough\n");
|
|
|
|
|
|
|
|
|
|
sync();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
|
tr_torrent* createTorrentFromBase64Metainfo(tr_ctor* ctor, char const* benc_base64)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
// create the torrent ctor
|
2022-01-08 12:46:25 +00:00
|
|
|
|
auto const benc = tr_base64_decode(benc_base64);
|
2022-04-02 14:06:02 +00:00
|
|
|
|
EXPECT_LT(0U, std::size(benc));
|
2021-12-26 16:25:07 +00:00
|
|
|
|
tr_error* error = nullptr;
|
2022-01-08 12:46:25 +00:00
|
|
|
|
EXPECT_TRUE(tr_ctorSetMetainfo(ctor, std::data(benc), std::size(benc), &error));
|
2022-03-21 20:22:50 +00:00
|
|
|
|
EXPECT_EQ(nullptr, error) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, true);
|
|
|
|
|
|
|
|
|
|
// create the torrent
|
2022-09-06 04:43:59 +00:00
|
|
|
|
auto* const tor = createTorrentAndWaitForVerifyDone(ctor);
|
2021-12-24 22:05:17 +00:00
|
|
|
|
EXPECT_NE(nullptr, tor);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
return tor;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 19:22:32 +00:00
|
|
|
|
static bool testFileExistsAndConsistsOfThisString(tr_torrent const* tor, tr_file_index_t file_index, std::string_view str)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
2022-07-28 19:22:32 +00:00
|
|
|
|
if (auto const found = tor->findFile(file_index); found)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
2022-07-28 19:22:32 +00:00
|
|
|
|
EXPECT_TRUE(tr_sys_path_exists(found->filename()));
|
|
|
|
|
auto contents = std::vector<char>{};
|
|
|
|
|
return tr_loadFile(found->filename(), contents) &&
|
|
|
|
|
std::string_view{ std::data(contents), std::size(contents) } == str;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 19:22:32 +00:00
|
|
|
|
return false;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
|
static void expectHaveNone(tr_torrent* tor, uint64_t total_size)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
auto const* tst = tr_torrentStat(tor);
|
|
|
|
|
EXPECT_EQ(TR_STATUS_STOPPED, tst->activity);
|
|
|
|
|
EXPECT_EQ(TR_STAT_OK, tst->error);
|
|
|
|
|
EXPECT_EQ(total_size, tst->sizeWhenDone);
|
|
|
|
|
EXPECT_EQ(total_size, tst->leftUntilDone);
|
2021-12-15 15:53:20 +00:00
|
|
|
|
EXPECT_EQ(total_size, tor->totalSize());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_EQ(0, tst->haveValid);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
|
static int torrentRenameAndWait(tr_torrent* tor, char const* oldpath, char const* newname)
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
|
auto const on_rename_done =
|
|
|
|
|
[](tr_torrent* /*tor*/, char const* /*oldpath*/, char const* /*newname*/, int error, void* user_data) noexcept
|
2020-08-11 18:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
*static_cast<int*>(user_data) = error;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int error = -1;
|
|
|
|
|
tr_torrentRenamePath(tor, oldpath, newname, on_rename_done, &error);
|
2021-08-15 09:41:48 +00:00
|
|
|
|
auto test = [&error]()
|
|
|
|
|
{
|
|
|
|
|
return error != -1;
|
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_TRUE(waitFor(test, MaxWaitMsec));
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(RenameTest, singleFilenameTorrent)
|
|
|
|
|
{
|
|
|
|
|
static auto constexpr TotalSize = size_t{ 14 };
|
|
|
|
|
|
|
|
|
|
// this is a single-file torrent whose file is hello-world.txt, holding the string "hello, world!"
|
|
|
|
|
auto* ctor = tr_ctorNew(session_);
|
2021-08-15 09:41:48 +00:00
|
|
|
|
auto* tor = createTorrentFromBase64Metainfo(
|
|
|
|
|
ctor,
|
2020-08-11 18:11:55 +00:00
|
|
|
|
"ZDEwOmNyZWF0ZWQgYnkyNTpUcmFuc21pc3Npb24vMi42MSAoMTM0MDcpMTM6Y3JlYXRpb24gZGF0"
|
|
|
|
|
"ZWkxMzU4NTQ5MDk4ZTg6ZW5jb2Rpbmc1OlVURi04NDppbmZvZDY6bGVuZ3RoaTE0ZTQ6bmFtZTE1"
|
|
|
|
|
"OmhlbGxvLXdvcmxkLnR4dDEyOnBpZWNlIGxlbmd0aGkzMjc2OGU2OnBpZWNlczIwOukboJcrkFUY"
|
|
|
|
|
"f6LvqLXBVvSHqCk6Nzpwcml2YXRlaTBlZWU=");
|
|
|
|
|
EXPECT_TRUE(tr_isTorrent(tor));
|
|
|
|
|
|
|
|
|
|
// sanity check the info
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_EQ(tr_file_index_t{ 1 }, tor->fileCount());
|
2021-12-24 21:12:33 +00:00
|
|
|
|
EXPECT_STREQ("hello-world.txt", tr_torrentFile(tor, 0).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// sanity check the (empty) stats
|
|
|
|
|
blockingTorrentVerify(tor);
|
|
|
|
|
expectHaveNone(tor, TotalSize);
|
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
|
createSingleFileTorrentContents(tor->currentDir().sv());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// sanity check the stats again, now that we've added the file
|
|
|
|
|
blockingTorrentVerify(tor);
|
|
|
|
|
auto const* st = tr_torrentStat(tor);
|
|
|
|
|
EXPECT_EQ(TR_STATUS_STOPPED, st->activity);
|
|
|
|
|
EXPECT_EQ(TR_STAT_OK, st->error);
|
|
|
|
|
EXPECT_EQ(0, st->leftUntilDone);
|
|
|
|
|
EXPECT_EQ(0, st->haveUnchecked);
|
|
|
|
|
EXPECT_EQ(0, st->desiredAvailable);
|
|
|
|
|
EXPECT_EQ(TotalSize, st->sizeWhenDone);
|
|
|
|
|
EXPECT_EQ(TotalSize, st->haveValid);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*** okay! we've finally put together all the scaffolding to test
|
|
|
|
|
*** renaming a single-file torrent
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
// confirm that bad inputs get caught
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "hello-world.txt", nullptr));
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "hello-world.txt", ""));
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "hello-world.txt", "."));
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "hello-world.txt", ".."));
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "hello-world.txt", "hello-world.txt"));
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "hello-world.txt", "hello/world.txt"));
|
2021-12-24 21:12:33 +00:00
|
|
|
|
EXPECT_STREQ("hello-world.txt", tr_torrentFile(tor, 0).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
**** Now try a rename that should succeed
|
|
|
|
|
***/
|
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
|
auto tmpstr = tr_pathbuf{ tor->currentDir(), "/hello-world.txt" };
|
|
|
|
|
EXPECT_TRUE(tr_sys_path_exists(tmpstr));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_STREQ("hello-world.txt", tr_torrentName(tor));
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, tr_torrentName(tor), "foobar"));
|
2022-04-22 16:35:56 +00:00
|
|
|
|
EXPECT_FALSE(tr_sys_path_exists(tmpstr)); // confirm the old filename can't be found
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_STREQ("foobar", tr_torrentName(tor)); // confirm the torrent's name is now 'foobar'
|
2021-12-24 21:12:33 +00:00
|
|
|
|
EXPECT_STREQ("foobar", tr_torrentFile(tor, 0).name); // confirm the file's name is now 'foobar'
|
2022-08-17 00:28:57 +00:00
|
|
|
|
auto const torrent_filename = tr_torrentFilename(tor);
|
|
|
|
|
EXPECT_EQ(std::string::npos, torrent_filename.find("foobar")); // confirm torrent file hasn't changed
|
2022-04-22 16:35:56 +00:00
|
|
|
|
tmpstr.assign(tor->currentDir(), "/foobar");
|
|
|
|
|
EXPECT_TRUE(tr_sys_path_exists(tmpstr)); // confirm the file's name is now 'foobar' on the disk
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 0, "hello, world!\n")); // confirm the contents are right
|
|
|
|
|
|
|
|
|
|
// (while it's renamed: confirm that the .resume file remembers the changes)
|
2022-02-09 02:25:19 +00:00
|
|
|
|
tr_resume::save(tor);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
sync();
|
2022-02-09 02:25:19 +00:00
|
|
|
|
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor, nullptr);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_STREQ("foobar", tr_torrentName(tor));
|
2022-02-09 02:25:19 +00:00
|
|
|
|
EXPECT_NE(decltype(loaded){ 0 }, (loaded & tr_resume::Name));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
**** ...and rename it back again
|
|
|
|
|
***/
|
|
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
|
tmpstr.assign(tor->currentDir(), "/foobar");
|
|
|
|
|
EXPECT_TRUE(tr_sys_path_exists(tmpstr));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "foobar", "hello-world.txt"));
|
2022-04-22 16:35:56 +00:00
|
|
|
|
EXPECT_FALSE(tr_sys_path_exists(tmpstr));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_STREQ("hello-world.txt", tr_torrentName(tor));
|
2021-12-24 21:12:33 +00:00
|
|
|
|
EXPECT_STREQ("hello-world.txt", tr_torrentFile(tor, 0).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 0, "hello, world!\n"));
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
|
torrentRemoveAndWait(tor, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
****
|
|
|
|
|
****
|
|
|
|
|
****
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
TEST_F(RenameTest, multifileTorrent)
|
|
|
|
|
{
|
|
|
|
|
auto constexpr TotalSize = size_t{ 67 };
|
2022-08-16 22:47:02 +00:00
|
|
|
|
auto constexpr ExpectedFiles = std::array<std::string_view, 4>{
|
|
|
|
|
"Felidae/Felinae/Acinonyx/Cheetah/Chester"sv,
|
|
|
|
|
"Felidae/Felinae/Felis/catus/Kyphi"sv,
|
|
|
|
|
"Felidae/Felinae/Felis/catus/Saffron"sv,
|
|
|
|
|
"Felidae/Pantherinae/Panthera/Tiger/Tony"sv,
|
2020-08-11 18:11:55 +00:00
|
|
|
|
};
|
2022-08-16 22:47:02 +00:00
|
|
|
|
auto constexpr ExpectedContents = std::array<std::string_view, 4>{
|
|
|
|
|
"It ain't easy bein' cheesy.\n"sv,
|
|
|
|
|
"Inquisitive\n"sv,
|
|
|
|
|
"Tough\n"sv,
|
|
|
|
|
"They’re Grrrrreat!\n"sv,
|
2020-08-11 18:11:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto* ctor = tr_ctorNew(session_);
|
2021-08-15 09:41:48 +00:00
|
|
|
|
auto* tor = createTorrentFromBase64Metainfo(
|
|
|
|
|
ctor,
|
2020-08-11 18:11:55 +00:00
|
|
|
|
"ZDEwOmNyZWF0ZWQgYnkyNTpUcmFuc21pc3Npb24vMi42MSAoMTM0MDcpMTM6Y3JlYXRpb24gZGF0"
|
|
|
|
|
"ZWkxMzU4NTU1NDIwZTg6ZW5jb2Rpbmc1OlVURi04NDppbmZvZDU6ZmlsZXNsZDY6bGVuZ3RoaTI4"
|
|
|
|
|
"ZTQ6cGF0aGw3OkZlbGluYWU4OkFjaW5vbnl4NzpDaGVldGFoNzpDaGVzdGVyZWVkNjpsZW5ndGhp"
|
|
|
|
|
"MTJlNDpwYXRobDc6RmVsaW5hZTU6RmVsaXM1OmNhdHVzNTpLeXBoaWVlZDY6bGVuZ3RoaTZlNDpw"
|
|
|
|
|
"YXRobDc6RmVsaW5hZTU6RmVsaXM1OmNhdHVzNzpTYWZmcm9uZWVkNjpsZW5ndGhpMjFlNDpwYXRo"
|
|
|
|
|
"bDExOlBhbnRoZXJpbmFlODpQYW50aGVyYTU6VGlnZXI0OlRvbnllZWU0Om5hbWU3OkZlbGlkYWUx"
|
|
|
|
|
"MjpwaWVjZSBsZW5ndGhpMzI3NjhlNjpwaWVjZXMyMDp27buFkmy8ICfNX4nsJmt0Ckm2Ljc6cHJp"
|
|
|
|
|
"dmF0ZWkwZWVl");
|
|
|
|
|
EXPECT_TRUE(tr_isTorrent(tor));
|
|
|
|
|
|
|
|
|
|
// sanity check the info
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae", tr_torrentName(tor));
|
2021-12-15 15:53:20 +00:00
|
|
|
|
EXPECT_EQ(TotalSize, tor->totalSize());
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_EQ(tr_file_index_t{ 4 }, tor->fileCount());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[i], tr_torrentFile(tor, i).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sanity check the (empty) stats
|
|
|
|
|
blockingTorrentVerify(tor);
|
|
|
|
|
expectHaveNone(tor, TotalSize);
|
|
|
|
|
|
|
|
|
|
// build the local data
|
2021-12-23 17:16:05 +00:00
|
|
|
|
createMultifileTorrentContents(tor->currentDir().sv());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// sanity check the (full) stats
|
|
|
|
|
blockingTorrentVerify(tor);
|
|
|
|
|
auto const* st = tr_torrentStat(tor);
|
|
|
|
|
EXPECT_EQ(TR_STATUS_STOPPED, st->activity);
|
|
|
|
|
EXPECT_EQ(TR_STAT_OK, st->error);
|
|
|
|
|
EXPECT_EQ(0, st->leftUntilDone);
|
|
|
|
|
EXPECT_EQ(0, st->haveUnchecked);
|
|
|
|
|
EXPECT_EQ(0, st->desiredAvailable);
|
|
|
|
|
EXPECT_EQ(TotalSize, st->sizeWhenDone);
|
|
|
|
|
EXPECT_EQ(TotalSize, st->haveValid);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*** okay! let's test renaming.
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
// rename a leaf...
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/catus/Kyphi", "placeholder"));
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/catus/placeholder", tr_torrentFile(tor, 1).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 1, "Inquisitive\n"));
|
|
|
|
|
|
|
|
|
|
// ...and back again
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/catus/placeholder", "Kyphi"));
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/catus/Kyphi", tr_torrentFile(tor, 1).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
testFileExistsAndConsistsOfThisString(tor, 1, "Inquisitive\n");
|
|
|
|
|
|
|
|
|
|
// rename a branch...
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/catus", "placeholder"));
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[0], tr_torrentFile(tor, 0).name);
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/placeholder/Kyphi", tr_torrentFile(tor, 1).name);
|
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/placeholder/Saffron", tr_torrentFile(tor, 2).name);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[3], tr_torrentFile(tor, 3).name);
|
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 1, ExpectedContents[1]));
|
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 2, ExpectedContents[2]));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// (while the branch is renamed: confirm that the .resume file remembers the changes)
|
2022-02-09 02:25:19 +00:00
|
|
|
|
tr_resume::save(tor);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
// this is a bit dodgy code-wise, but let's make sure the .resume file got the name
|
2022-01-08 23:41:05 +00:00
|
|
|
|
tor->setFileSubpath(1, "gabba gabba hey"sv);
|
2022-02-09 02:25:19 +00:00
|
|
|
|
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor, nullptr);
|
|
|
|
|
EXPECT_NE(decltype(loaded){ 0 }, (loaded & tr_resume::Filenames));
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[0], tr_torrentFile(tor, 0).name);
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/placeholder/Kyphi", tr_torrentFile(tor, 1).name);
|
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/placeholder/Saffron", tr_torrentFile(tor, 2).name);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[3], tr_torrentFile(tor, 3).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// ...and back again
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/placeholder", "catus"));
|
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[i], tr_torrentFile(tor, i).name);
|
|
|
|
|
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, i, ExpectedContents[i]));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
**** Test it an incomplete torrent...
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
// remove the directory Felidae/Felinae/Felis/catus
|
2022-08-17 00:28:57 +00:00
|
|
|
|
auto str = tr_torrentFindFile(tor, 1);
|
|
|
|
|
EXPECT_NE(""sv, str);
|
2022-03-27 17:37:29 +00:00
|
|
|
|
tr_sys_path_remove(str);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
str = tr_torrentFindFile(tor, 2);
|
2022-08-17 00:28:57 +00:00
|
|
|
|
EXPECT_NE(""sv, str);
|
2022-03-27 17:37:29 +00:00
|
|
|
|
tr_sys_path_remove(str);
|
2022-05-22 01:17:00 +00:00
|
|
|
|
tr_sys_path_remove(std::string{ tr_sys_path_dirname(str) });
|
2020-08-11 18:11:55 +00:00
|
|
|
|
sync();
|
|
|
|
|
blockingTorrentVerify(tor);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
testFileExistsAndConsistsOfThisString(tor, 0, ExpectedContents[0]);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 1; i <= 2; ++i)
|
|
|
|
|
{
|
|
|
|
|
str = tr_torrentFindFile(tor, i);
|
2022-08-17 00:28:57 +00:00
|
|
|
|
EXPECT_EQ(""sv, str);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-16 22:47:02 +00:00
|
|
|
|
testFileExistsAndConsistsOfThisString(tor, 3, ExpectedContents[3]);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// rename a branch...
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/catus", "foo"));
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[0], tr_torrentFile(tor, 0).name);
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/foo/Kyphi", tr_torrentFile(tor, 1).name);
|
|
|
|
|
EXPECT_STREQ("Felidae/Felinae/Felis/foo/Saffron", tr_torrentFile(tor, 2).name);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[3], tr_torrentFile(tor, 3).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// ...and back again
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/foo", "catus"));
|
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2022-08-16 22:47:02 +00:00
|
|
|
|
EXPECT_EQ(ExpectedFiles[i], tr_torrentFile(tor, i).name);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae", "gabba"));
|
|
|
|
|
auto strings = std::array<char const*, 4>{};
|
|
|
|
|
strings[0] = "gabba/Felinae/Acinonyx/Cheetah/Chester";
|
|
|
|
|
strings[1] = "gabba/Felinae/Felis/catus/Kyphi";
|
|
|
|
|
strings[2] = "gabba/Felinae/Felis/catus/Saffron";
|
|
|
|
|
strings[3] = "gabba/Pantherinae/Panthera/Tiger/Tony";
|
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ(strings[i], tr_torrentFile(tor, i).name);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
testFileExistsAndConsistsOfThisString(tor, i, ExpectedContents[i]);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rename the root, then a branch, and then a leaf...
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "gabba", "Felidae"));
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Pantherinae/Panthera/Tiger", "Snow Leopard"));
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "Felidae/Pantherinae/Panthera/Snow Leopard/Tony", "10.6"));
|
|
|
|
|
strings[0] = "Felidae/Felinae/Acinonyx/Cheetah/Chester";
|
|
|
|
|
strings[1] = "Felidae/Felinae/Felis/catus/Kyphi";
|
|
|
|
|
strings[2] = "Felidae/Felinae/Felis/catus/Saffron";
|
|
|
|
|
strings[3] = "Felidae/Pantherinae/Panthera/Snow Leopard/10.6";
|
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ(strings[i], tr_torrentFile(tor, i).name);
|
2022-08-16 22:47:02 +00:00
|
|
|
|
testFileExistsAndConsistsOfThisString(tor, i, ExpectedContents[i]);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
|
torrentRemoveAndWait(tor, 0);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*** Test renaming prefixes (shouldn't work)
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
ctor = tr_ctorNew(session_);
|
2021-08-15 09:41:48 +00:00
|
|
|
|
tor = createTorrentFromBase64Metainfo(
|
|
|
|
|
ctor,
|
2020-08-11 18:11:55 +00:00
|
|
|
|
"ZDEwOmNyZWF0ZWQgYnkyNTpUcmFuc21pc3Npb24vMi42MSAoMTM0MDcpMTM6Y3JlYXRpb24gZGF0"
|
|
|
|
|
"ZWkxMzU4NTU1NDIwZTg6ZW5jb2Rpbmc1OlVURi04NDppbmZvZDU6ZmlsZXNsZDY6bGVuZ3RoaTI4"
|
|
|
|
|
"ZTQ6cGF0aGw3OkZlbGluYWU4OkFjaW5vbnl4NzpDaGVldGFoNzpDaGVzdGVyZWVkNjpsZW5ndGhp"
|
|
|
|
|
"MTJlNDpwYXRobDc6RmVsaW5hZTU6RmVsaXM1OmNhdHVzNTpLeXBoaWVlZDY6bGVuZ3RoaTZlNDpw"
|
|
|
|
|
"YXRobDc6RmVsaW5hZTU6RmVsaXM1OmNhdHVzNzpTYWZmcm9uZWVkNjpsZW5ndGhpMjFlNDpwYXRo"
|
|
|
|
|
"bDExOlBhbnRoZXJpbmFlODpQYW50aGVyYTU6VGlnZXI0OlRvbnllZWU0Om5hbWU3OkZlbGlkYWUx"
|
|
|
|
|
"MjpwaWVjZSBsZW5ndGhpMzI3NjhlNjpwaWVjZXMyMDp27buFkmy8ICfNX4nsJmt0Ckm2Ljc6cHJp"
|
|
|
|
|
"dmF0ZWkwZWVl");
|
|
|
|
|
EXPECT_TRUE(tr_isTorrent(tor));
|
|
|
|
|
|
|
|
|
|
// rename prefix of top
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "Feli", "FelidaeX"));
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae", tr_torrentName(tor));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
// rename false path
|
|
|
|
|
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "Felidae/FelinaeX", "Genus Felinae"));
|
2021-12-16 05:16:40 +00:00
|
|
|
|
EXPECT_STREQ("Felidae", tr_torrentName(tor));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
****
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
|
torrentRemoveAndWait(tor, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
****
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
TEST_F(RenameTest, partialFile)
|
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
|
auto constexpr PieceCount = uint32_t{ 33 };
|
|
|
|
|
auto constexpr PieceSize = uint32_t{ 32768 };
|
2021-10-21 18:31:03 +00:00
|
|
|
|
auto constexpr Length = std::array<uint32_t, 3>{ 1048576, 4096, 512 };
|
|
|
|
|
auto constexpr TotalSize = uint64_t(Length[0]) + Length[1] + Length[2];
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
**** create our test torrent with an incomplete .part file
|
|
|
|
|
***/
|
|
|
|
|
|
2022-03-30 06:30:39 +00:00
|
|
|
|
auto* tor = zeroTorrentInit(ZeroTorrentState::Partial);
|
2021-12-15 15:53:20 +00:00
|
|
|
|
EXPECT_EQ(TotalSize, tor->totalSize());
|
|
|
|
|
EXPECT_EQ(PieceSize, tor->pieceSize());
|
|
|
|
|
EXPECT_EQ(PieceCount, tor->pieceCount());
|
2022-01-08 18:53:35 +00:00
|
|
|
|
EXPECT_EQ("files-filled-with-zeroes/1048576"sv, tor->fileSubpath(0));
|
|
|
|
|
EXPECT_EQ("files-filled-with-zeroes/4096"sv, tor->fileSubpath(1));
|
|
|
|
|
EXPECT_EQ("files-filled-with-zeroes/512"sv, tor->fileSubpath(2));
|
2022-03-30 06:30:39 +00:00
|
|
|
|
EXPECT_NE(0, tr_torrentFile(tor, 0).have);
|
2021-11-28 03:17:47 +00:00
|
|
|
|
EXPECT_EQ(Length[0], tr_torrentFile(tor, 0).have + PieceSize);
|
|
|
|
|
EXPECT_EQ(Length[1], tr_torrentFile(tor, 1).have);
|
|
|
|
|
EXPECT_EQ(Length[2], tr_torrentFile(tor, 2).have);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
auto const* st = tr_torrentStat(tor);
|
2021-10-21 18:31:03 +00:00
|
|
|
|
EXPECT_EQ(TotalSize, st->sizeWhenDone);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_EQ(PieceSize, st->leftUntilDone);
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
****
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "files-filled-with-zeroes", "foo"));
|
|
|
|
|
EXPECT_EQ(0, torrentRenameAndWait(tor, "foo/1048576", "bar"));
|
2022-01-08 18:53:35 +00:00
|
|
|
|
auto strings = std::array<std::string_view, 3>{};
|
|
|
|
|
strings[0] = "foo/bar"sv;
|
|
|
|
|
strings[1] = "foo/4096"sv;
|
|
|
|
|
strings[2] = "foo/512"sv;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 3; ++i)
|
|
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
|
EXPECT_EQ(strings[i], tor->fileSubpath(i));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strings[0] = "foo/bar.part";
|
|
|
|
|
|
|
|
|
|
for (tr_file_index_t i = 0; i < 3; ++i)
|
|
|
|
|
{
|
2022-04-22 16:35:56 +00:00
|
|
|
|
auto const expected = tr_pathbuf{ tor->currentDir(), '/', strings[i] };
|
2022-08-17 00:28:57 +00:00
|
|
|
|
auto const actual = tr_torrentFindFile(tor, i);
|
|
|
|
|
EXPECT_EQ(expected, actual);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrentRemoveAndWait(tor, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
|
|
} // namespace libtransmission
|