2021-12-25 21:21:13 +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 <array>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "metainfo.h"
|
|
|
|
|
#include "torrent-metainfo.h"
|
|
|
|
|
#include "torrent.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
#include "test-fixtures.h"
|
2021-12-25 21:21:13 +00:00
|
|
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
namespace libtransmission
|
|
|
|
|
{
|
|
|
|
|
namespace test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using TorrentMetainfoTest = SessionTest;
|
|
|
|
|
|
|
|
|
|
TEST_F(TorrentMetainfoTest, magnetLink)
|
2021-12-25 21:21:13 +00:00
|
|
|
|
{
|
|
|
|
|
// background info @ http://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding
|
|
|
|
|
char const constexpr* const MagnetLink =
|
|
|
|
|
"magnet:?"
|
|
|
|
|
"xt=urn:btih:14ffe5dd23188fd5cb53a1d47f1289db70abf31e"
|
|
|
|
|
"&dn=ubuntu_12_04_1_desktop_32_bit"
|
|
|
|
|
"&tr=http%3A%2F%2Ftracker.publicbt.com%2Fannounce"
|
|
|
|
|
"&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80"
|
|
|
|
|
"&ws=http%3A%2F%2Ftransmissionbt.com";
|
|
|
|
|
|
|
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
|
EXPECT_TRUE(metainfo.parseMagnet(MagnetLink));
|
|
|
|
|
EXPECT_EQ(0, std::size(metainfo.files())); // because it's a magnet link
|
|
|
|
|
EXPECT_EQ(2, std::size(metainfo.announceList()));
|
|
|
|
|
EXPECT_EQ(MagnetLink, metainfo.magnet());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define BEFORE_PATH \
|
|
|
|
|
"d10:created by25:Transmission/2.82 (14160)13:creation datei1402280218e8:encoding5:UTF-84:infod5:filesld6:lengthi2e4:pathl"
|
|
|
|
|
#define AFTER_PATH \
|
|
|
|
|
"eed6:lengthi2e4:pathl5:b.txteee4:name3:foo12:piece lengthi32768e6:pieces20:ÞÉ`âMs¡Å;˺¬.åÂà7:privatei0eee"
|
|
|
|
|
|
|
|
|
|
// FIXME: split these into parameterized tests?
|
2021-12-26 16:25:07 +00:00
|
|
|
|
TEST_F(TorrentMetainfoTest, bucket)
|
2021-12-25 21:21:13 +00:00
|
|
|
|
{
|
|
|
|
|
struct LocalTest
|
|
|
|
|
{
|
|
|
|
|
std::string_view benc;
|
|
|
|
|
bool expected_parse_result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto const tests = std::array<LocalTest, 9>{ {
|
|
|
|
|
{ BEFORE_PATH "5:a.txt" AFTER_PATH, true },
|
|
|
|
|
// allow empty components, but not =all= empty components, see bug #5517
|
|
|
|
|
{ BEFORE_PATH "0:5:a.txt" AFTER_PATH, true },
|
|
|
|
|
{ BEFORE_PATH "0:0:" AFTER_PATH, false },
|
|
|
|
|
// allow path separators in a filename (replaced with '_')
|
|
|
|
|
{ BEFORE_PATH "7:a/a.txt" AFTER_PATH, true },
|
|
|
|
|
// allow "." components (skipped)
|
|
|
|
|
{ BEFORE_PATH "1:.5:a.txt" AFTER_PATH, true },
|
|
|
|
|
{ BEFORE_PATH "5:a.txt1:." AFTER_PATH, true },
|
|
|
|
|
// allow ".." components (replaced with "__")
|
|
|
|
|
{ BEFORE_PATH "2:..5:a.txt" AFTER_PATH, true },
|
|
|
|
|
{ BEFORE_PATH "5:a.txt2:.." AFTER_PATH, true },
|
|
|
|
|
// fail on empty string
|
|
|
|
|
{ "", false },
|
|
|
|
|
} };
|
|
|
|
|
|
|
|
|
|
tr_logSetLevel(TR_LOG_SILENT);
|
|
|
|
|
|
|
|
|
|
for (auto const& test : tests)
|
|
|
|
|
{
|
|
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
|
EXPECT_EQ(test.expected_parse_result, metainfo.parseBenc(test.benc));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
TEST_F(TorrentMetainfoTest, sanitize)
|
2021-12-25 21:21:13 +00:00
|
|
|
|
{
|
|
|
|
|
struct LocalTest
|
|
|
|
|
{
|
|
|
|
|
std::string_view input;
|
|
|
|
|
std::string_view expected_output;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto const tests = std::array<LocalTest, 29>{
|
|
|
|
|
// skipped
|
2021-12-26 22:04:20 +00:00
|
|
|
|
LocalTest{ ""sv, ""sv },
|
|
|
|
|
{ "."sv, ""sv },
|
|
|
|
|
{ ".."sv, ""sv },
|
|
|
|
|
{ "....."sv, ""sv },
|
|
|
|
|
{ " "sv, ""sv },
|
|
|
|
|
{ " . "sv, ""sv },
|
|
|
|
|
{ ". . ."sv, ""sv },
|
2021-12-25 21:21:13 +00:00
|
|
|
|
// replaced with '_'
|
2021-12-26 22:04:20 +00:00
|
|
|
|
{ "/"sv, "_"sv },
|
|
|
|
|
{ "////"sv, "____"sv },
|
|
|
|
|
{ "\\\\"sv, "__"sv },
|
|
|
|
|
{ "/../"sv, "_.._"sv },
|
|
|
|
|
{ "foo<bar:baz/boo"sv, "foo_bar_baz_boo"sv },
|
|
|
|
|
{ "t\0e\x01s\tt\ri\nn\fg"sv, "t_e_s_t_i_n_g"sv },
|
2021-12-25 21:21:13 +00:00
|
|
|
|
// appended with '_'
|
2021-12-26 22:04:20 +00:00
|
|
|
|
{ "con"sv, "con_"sv },
|
|
|
|
|
{ "cOm4"sv, "cOm4_"sv },
|
|
|
|
|
{ "LPt9.txt"sv, "LPt9_.txt"sv },
|
|
|
|
|
{ "NUL.tar.gz"sv, "NUL_.tar.gz"sv },
|
2021-12-25 21:21:13 +00:00
|
|
|
|
// trimmed
|
2021-12-26 22:04:20 +00:00
|
|
|
|
{ " foo"sv, "foo"sv },
|
|
|
|
|
{ "foo "sv, "foo"sv },
|
|
|
|
|
{ " foo "sv, "foo"sv },
|
|
|
|
|
{ "foo."sv, "foo"sv },
|
|
|
|
|
{ "foo..."sv, "foo"sv },
|
|
|
|
|
{ " foo... "sv, "foo"sv },
|
2021-12-25 21:21:13 +00:00
|
|
|
|
// unmodified
|
2021-12-26 22:04:20 +00:00
|
|
|
|
{ "foo"sv, "foo"sv },
|
|
|
|
|
{ ".foo"sv, ".foo"sv },
|
|
|
|
|
{ "..foo"sv, "..foo"sv },
|
|
|
|
|
{ "foo.bar.baz"sv, "foo.bar.baz"sv },
|
|
|
|
|
{ "null"sv, "null"sv },
|
|
|
|
|
{ "compass"sv, "compass"sv },
|
2021-12-25 21:21:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto out = std::string{};
|
|
|
|
|
for (auto const& test : tests)
|
|
|
|
|
{
|
|
|
|
|
out.clear();
|
2021-12-26 22:04:20 +00:00
|
|
|
|
auto const success = tr_metainfoAppendSanitizedPathComponent(out, test.input);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
EXPECT_EQ(!std::empty(out), success);
|
|
|
|
|
EXPECT_EQ(test.expected_output, out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
TEST_F(TorrentMetainfoTest, AndroidTorrent)
|
2021-12-25 21:21:13 +00:00
|
|
|
|
{
|
|
|
|
|
auto const filename = tr_strvJoin(LIBTRANSMISSION_TEST_ASSETS_DIR, "/Android-x86 8.1 r6 iso.torrent"sv);
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
auto* ctor = tr_ctorNew(session_);
|
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
|
EXPECT_TRUE(tr_ctorSetMetainfoFromFile(ctor, filename.c_str(), &error));
|
|
|
|
|
EXPECT_EQ(nullptr, error);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
|
TEST_F(TorrentMetainfoTest, ctorSaveContents)
|
2021-12-25 21:21:13 +00:00
|
|
|
|
{
|
|
|
|
|
auto const src_filename = tr_strvJoin(LIBTRANSMISSION_TEST_ASSETS_DIR, "/Android-x86 8.1 r6 iso.torrent"sv);
|
|
|
|
|
auto const tgt_filename = tr_strvJoin(::testing::TempDir(), "save-contents-test.torrent");
|
|
|
|
|
|
|
|
|
|
// try saving without passing any metainfo.
|
2021-12-26 16:25:07 +00:00
|
|
|
|
auto* ctor = tr_ctorNew(session_);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
|
EXPECT_FALSE(tr_ctorSaveContents(ctor, tgt_filename.c_str(), &error));
|
|
|
|
|
EXPECT_NE(nullptr, error);
|
|
|
|
|
if (error != nullptr)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_EQ(EINVAL, error->code);
|
|
|
|
|
tr_error_clear(&error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// now try saving _with_ metainfo
|
2021-12-26 16:25:07 +00:00
|
|
|
|
EXPECT_TRUE(tr_ctorSetMetainfoFromFile(ctor, src_filename.c_str(), &error));
|
|
|
|
|
EXPECT_EQ(nullptr, error);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
EXPECT_TRUE(tr_ctorSaveContents(ctor, tgt_filename.c_str(), &error));
|
|
|
|
|
EXPECT_EQ(nullptr, error);
|
|
|
|
|
|
|
|
|
|
// the saved contents should match the source file's contents
|
|
|
|
|
auto src_contents = std::vector<char>{};
|
|
|
|
|
EXPECT_TRUE(tr_loadFile(src_contents, src_filename.c_str(), &error));
|
|
|
|
|
auto tgt_contents = std::vector<char>{};
|
|
|
|
|
EXPECT_TRUE(tr_loadFile(tgt_contents, tgt_filename.c_str(), &error));
|
|
|
|
|
EXPECT_EQ(src_contents, tgt_contents);
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
EXPECT_TRUE(tr_sys_path_remove(tgt_filename.c_str(), &error));
|
|
|
|
|
EXPECT_EQ(nullptr, error);
|
|
|
|
|
tr_error_clear(&error);
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
|
}
|
2021-12-26 16:25:07 +00:00
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace libtransmission
|