2014-01-19 01:09:44 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2012-10-14 18:02:47 +00:00
|
|
|
|
#include "transmission.h"
|
2019-06-23 13:23:22 +00:00
|
|
|
|
#include "metainfo.h"
|
2017-05-13 22:38:31 +00:00
|
|
|
|
#include "utils.h"
|
2012-10-14 18:02:47 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
#include "gtest/gtest.h"
|
2012-10-14 18:02:47 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
#include <array>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <cstring>
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
TEST(Metainfo, magnetLink)
|
|
|
|
|
{
|
|
|
|
|
// background info @ http://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding
|
|
|
|
|
char const constexpr* const MagnetLink =
|
2017-04-19 12:04:45 +00:00
|
|
|
|
"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://transmissionbt.com ";
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
|
|
auto* ctor = tr_ctorNew(nullptr);
|
|
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, MagnetLink);
|
|
|
|
|
tr_info inf;
|
|
|
|
|
auto const parse_result = tr_torrentParse(ctor, &inf);
|
|
|
|
|
EXPECT_EQ(TR_PARSE_OK, parse_result);
|
|
|
|
|
EXPECT_EQ(0, inf.fileCount); // because it's a magnet link
|
|
|
|
|
EXPECT_EQ(2, inf.trackerCount);
|
|
|
|
|
EXPECT_STREQ("http://tracker.publicbt.com/announce", inf.trackers[0].announce);
|
|
|
|
|
EXPECT_STREQ("udp://tracker.publicbt.com:80", inf.trackers[1].announce);
|
|
|
|
|
EXPECT_EQ(1, inf.webseedCount);
|
|
|
|
|
EXPECT_STREQ("http://transmissionbt.com", inf.webseeds[0]);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
|
tr_metainfoFree(&inf);
|
|
|
|
|
tr_ctorFree(ctor);
|
2014-06-09 02:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
#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"
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
// FIXME: split these into parameterized tests?
|
|
|
|
|
TEST(Metainfo, bucket)
|
2014-06-09 02:53:04 +00:00
|
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
|
struct Test
|
2017-04-19 12:04:45 +00:00
|
|
|
|
{
|
|
|
|
|
int expected_benc_err;
|
|
|
|
|
int expected_parse_result;
|
2019-02-16 07:50:37 +00:00
|
|
|
|
void const* benc;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto constexpr Tests = std::array<Test, 9>{
|
|
|
|
|
Test{ 0, TR_PARSE_OK, BEFORE_PATH "5:a.txt" AFTER_PATH },
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
/* allow empty components, but not =all= empty components, see bug #5517 */
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "0:5:a.txt" AFTER_PATH },
|
|
|
|
|
{ 0, TR_PARSE_ERR, BEFORE_PATH "0:0:" AFTER_PATH },
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2019-06-23 13:23:22 +00:00
|
|
|
|
/* allow path separators in a filename (replaced with '_') */
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "7:a/a.txt" AFTER_PATH },
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2019-06-23 13:23:22 +00:00
|
|
|
|
/* allow "." components (skipped) */
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "1:.5:a.txt" AFTER_PATH },
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "5:a.txt1:." AFTER_PATH },
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2019-06-23 13:23:22 +00:00
|
|
|
|
/* allow ".." components (replaced with "__") */
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "2:..5:a.txt" AFTER_PATH },
|
|
|
|
|
{ 0, TR_PARSE_OK, BEFORE_PATH "5:a.txt2:.." AFTER_PATH },
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
|
/* fail on empty string */
|
|
|
|
|
{ EILSEQ, TR_PARSE_ERR, "" }
|
|
|
|
|
};
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
tr_logSetLevel(TR_LOG_SILENT);
|
2014-06-09 02:53:04 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
for (auto const& test : Tests)
|
2014-06-09 02:53:04 +00:00
|
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
|
auto* ctor = tr_ctorNew(nullptr);
|
|
|
|
|
int const err = tr_ctorSetMetainfo(ctor, test.benc, strlen(static_cast<char const*>(test.benc)));
|
|
|
|
|
EXPECT_EQ(test.expected_benc_err, err);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
|
if (err == 0)
|
2014-06-09 02:53:04 +00:00
|
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
|
tr_parse_result const parse_result = tr_torrentParse(ctor, nullptr);
|
|
|
|
|
EXPECT_EQ(test.expected_parse_result, parse_result);
|
2014-06-09 02:53:04 +00:00
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
|
|
tr_ctorFree(ctor);
|
2014-06-09 02:53:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-02 11:15:31 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
TEST(Metainfo, sanitize)
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
|
struct Test
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{
|
|
|
|
|
char const* str;
|
|
|
|
|
size_t len;
|
|
|
|
|
char const* expected_result;
|
|
|
|
|
bool expected_is_adjusted;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto constexpr Tests = std::array<Test, 29>{
|
|
|
|
|
// skipped
|
|
|
|
|
Test{ "", 0, nullptr, false },
|
|
|
|
|
{ ".", 1, nullptr, false },
|
|
|
|
|
{ "..", 2, nullptr, true },
|
|
|
|
|
{ ".....", 5, nullptr, false },
|
|
|
|
|
{ " ", 2, nullptr, false },
|
|
|
|
|
{ " . ", 3, nullptr, false },
|
|
|
|
|
{ ". . .", 5, nullptr, false },
|
|
|
|
|
// replaced with '_'
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{ "/", 1, "_", true },
|
|
|
|
|
{ "////", 4, "____", true },
|
|
|
|
|
{ "\\\\", 2, "__", true },
|
|
|
|
|
{ "/../", 4, "_.._", true },
|
|
|
|
|
{ "foo<bar:baz/boo", 15, "foo_bar_baz_boo", true },
|
|
|
|
|
{ "t\0e\x01s\tt\ri\nn\fg", 13, "t_e_s_t_i_n_g", true },
|
2020-08-11 18:11:55 +00:00
|
|
|
|
// appended with '_'
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{ "con", 3, "con_", true },
|
|
|
|
|
{ "cOm4", 4, "cOm4_", true },
|
|
|
|
|
{ "LPt9.txt", 8, "LPt9_.txt", true },
|
|
|
|
|
{ "NUL.tar.gz", 10, "NUL_.tar.gz", true },
|
2020-08-11 18:11:55 +00:00
|
|
|
|
// trimmed
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{ " foo", 4, "foo", true },
|
|
|
|
|
{ "foo ", 4, "foo", true },
|
|
|
|
|
{ " foo ", 5, "foo", true },
|
|
|
|
|
{ "foo.", 4, "foo", true },
|
|
|
|
|
{ "foo...", 6, "foo", true },
|
|
|
|
|
{ " foo... ", 8, "foo", true },
|
2020-08-11 18:11:55 +00:00
|
|
|
|
// unmodified
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{ "foo", 3, "foo", false },
|
|
|
|
|
{ ".foo", 4, ".foo", false },
|
|
|
|
|
{ "..foo", 5, "..foo", false },
|
2019-06-24 03:32:38 +00:00
|
|
|
|
{ "foo.bar.baz", 11, "foo.bar.baz", false },
|
|
|
|
|
{ "null", 4, "null", false },
|
|
|
|
|
{ "compass", 7, "compass", false }
|
2019-06-23 13:23:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
for (auto const& test : Tests)
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{
|
|
|
|
|
bool is_adjusted;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
char* const result = tr_metainfo_sanitize_path_component(test.str, test.len, &is_adjusted);
|
2019-06-23 13:23:22 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_STREQ(test.expected_result, result);
|
2019-06-23 13:23:22 +00:00
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
|
if (test.expected_result != nullptr)
|
2019-06-23 13:23:22 +00:00
|
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
|
EXPECT_EQ(test.expected_is_adjusted, is_adjusted);
|
2019-06-23 13:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tr_free(result);
|
|
|
|
|
}
|
2012-10-14 18:02:47 +00:00
|
|
|
|
}
|