2013-11-18 01:56:02 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2012, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "libtorrent/file_storage.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2013-11-18 01:56:02 +01:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/aux_/escape_string.hpp" // for convert_path_to_posix
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "libtorrent/hex.hpp" // to_hex
|
2016-05-22 19:46:37 +02:00
|
|
|
|
2016-05-23 14:15:39 +02:00
|
|
|
#include <iostream>
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
#ifndef TORRENT_DISABLE_MUTABLE_TORRENTS
|
|
|
|
TORRENT_TEST(mutable_torrents)
|
2015-03-21 01:12:40 +01:00
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
|
|
|
|
fs.add_file("test/temporary.txt", 0x4000);
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 0x4000);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
sha1_hash ph;
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.piece_range())
|
2015-03-21 01:12:40 +01:00
|
|
|
t.set_hash(i, ph);
|
|
|
|
|
|
|
|
t.add_collection("collection1");
|
|
|
|
t.add_collection("collection2");
|
|
|
|
|
|
|
|
t.add_similar_torrent(sha1_hash("abababababababababab"));
|
|
|
|
t.add_similar_torrent(sha1_hash("babababababababababa"));
|
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
|
|
|
entry tor = t.generate();
|
|
|
|
bencode(out, tor);
|
|
|
|
|
2017-07-20 09:27:47 +02:00
|
|
|
torrent_info ti(tmp, from_span);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
|
|
|
std::vector<sha1_hash> similar;
|
|
|
|
similar.push_back(sha1_hash("abababababababababab"));
|
|
|
|
similar.push_back(sha1_hash("babababababababababa"));
|
|
|
|
|
|
|
|
std::vector<std::string> collections;
|
|
|
|
collections.push_back("collection1");
|
|
|
|
collections.push_back("collection2");
|
|
|
|
|
|
|
|
TEST_CHECK(similar == ti.similar_torrents());
|
|
|
|
TEST_CHECK(collections == ti.collections());
|
|
|
|
}
|
2015-05-31 14:21:16 +02:00
|
|
|
#endif
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2018-01-19 22:40:39 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-02-15 02:04:50 +01:00
|
|
|
struct test_torrent_t
|
|
|
|
{
|
|
|
|
char const* file;
|
|
|
|
};
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2013-11-18 01:56:02 +01:00
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
static test_torrent_t test_torrents[] =
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
{ "base.torrent" },
|
|
|
|
{ "empty_path.torrent" },
|
|
|
|
{ "parent_path.torrent" },
|
|
|
|
{ "hidden_parent_path.torrent" },
|
|
|
|
{ "single_multi_file.torrent" },
|
|
|
|
{ "slash_path.torrent" },
|
|
|
|
{ "slash_path2.torrent" },
|
|
|
|
{ "slash_path3.torrent" },
|
|
|
|
{ "backslash_path.torrent" },
|
|
|
|
{ "url_list.torrent" },
|
|
|
|
{ "url_list2.torrent" },
|
|
|
|
{ "url_list3.torrent" },
|
|
|
|
{ "httpseed.torrent" },
|
|
|
|
{ "empty_httpseed.torrent" },
|
|
|
|
{ "long_name.torrent" },
|
|
|
|
{ "whitespace_url.torrent" },
|
|
|
|
{ "duplicate_files.torrent" },
|
|
|
|
{ "pad_file.torrent" },
|
|
|
|
{ "creation_date.torrent" },
|
|
|
|
{ "no_creation_date.torrent" },
|
|
|
|
{ "url_seed.torrent" },
|
|
|
|
{ "url_seed_multi.torrent" },
|
2018-12-10 14:15:29 +01:00
|
|
|
{ "url_seed_multi_single_file.torrent" },
|
2015-02-15 02:04:50 +01:00
|
|
|
{ "url_seed_multi_space.torrent" },
|
|
|
|
{ "url_seed_multi_space_nolist.torrent" },
|
|
|
|
{ "root_hash.torrent" },
|
|
|
|
{ "empty_path_multi.torrent" },
|
|
|
|
{ "duplicate_web_seeds.torrent" },
|
|
|
|
{ "invalid_name2.torrent" },
|
|
|
|
{ "invalid_name3.torrent" },
|
|
|
|
{ "symlink1.torrent" },
|
2019-03-14 10:02:41 +01:00
|
|
|
{ "symlink2.torrent" },
|
2016-07-27 06:15:26 +02:00
|
|
|
{ "unordered.torrent" },
|
2016-08-08 04:37:10 +02:00
|
|
|
{ "symlink_zero_size.torrent" },
|
|
|
|
{ "pad_file_no_path.torrent" },
|
2017-10-01 11:39:06 +02:00
|
|
|
{ "large.torrent" },
|
2017-10-01 04:30:32 +02:00
|
|
|
{ "absolute_filename.torrent" },
|
2017-10-08 02:04:56 +02:00
|
|
|
{ "invalid_filename.torrent" },
|
|
|
|
{ "invalid_filename2.torrent" },
|
2019-07-22 00:50:57 +02:00
|
|
|
{ "overlapping_symlinks.torrent" },
|
2015-02-15 02:04:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct test_failing_torrent_t
|
|
|
|
{
|
|
|
|
char const* file;
|
|
|
|
error_code error; // the expected error
|
|
|
|
};
|
|
|
|
|
|
|
|
test_failing_torrent_t test_error_torrents[] =
|
|
|
|
{
|
|
|
|
{ "missing_piece_len.torrent", errors::torrent_missing_piece_length },
|
|
|
|
{ "invalid_piece_len.torrent", errors::torrent_missing_piece_length },
|
|
|
|
{ "negative_piece_len.torrent", errors::torrent_missing_piece_length },
|
|
|
|
{ "no_name.torrent", errors::torrent_missing_name },
|
2017-09-30 21:10:35 +02:00
|
|
|
{ "bad_name.torrent", errors::torrent_missing_name },
|
2015-02-15 02:04:50 +01:00
|
|
|
{ "invalid_name.torrent", errors::torrent_missing_name },
|
|
|
|
{ "invalid_info.torrent", errors::torrent_missing_info },
|
|
|
|
{ "string.torrent", errors::torrent_is_no_dict },
|
|
|
|
{ "negative_size.torrent", errors::torrent_invalid_length },
|
|
|
|
{ "negative_file_size.torrent", errors::torrent_invalid_length },
|
2016-08-08 04:37:10 +02:00
|
|
|
{ "invalid_path_list.torrent", errors::torrent_invalid_name},
|
2015-02-15 02:04:50 +01:00
|
|
|
{ "missing_path_list.torrent", errors::torrent_missing_name },
|
|
|
|
{ "invalid_pieces.torrent", errors::torrent_missing_pieces },
|
|
|
|
{ "unaligned_pieces.torrent", errors::torrent_invalid_hashes },
|
|
|
|
{ "invalid_root_hash.torrent", errors::torrent_invalid_hashes },
|
|
|
|
{ "invalid_root_hash2.torrent", errors::torrent_missing_pieces },
|
2018-01-13 14:36:13 +01:00
|
|
|
{ "invalid_merkle.torrent", errors::no_files_in_torrent},
|
2015-02-15 02:04:50 +01:00
|
|
|
{ "invalid_file_size.torrent", errors::torrent_invalid_length },
|
2016-08-08 04:37:10 +02:00
|
|
|
{ "invalid_symlink.torrent", errors::torrent_invalid_name },
|
2017-09-28 21:20:44 +02:00
|
|
|
{ "many_pieces.torrent", errors::too_many_pieces_in_torrent },
|
2017-10-01 05:15:27 +02:00
|
|
|
{ "no_files.torrent", errors::no_files_in_torrent},
|
2015-02-15 02:04:50 +01:00
|
|
|
};
|
|
|
|
|
2018-01-19 22:40:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-02-15 02:04:50 +01:00
|
|
|
// TODO: test remap_files
|
|
|
|
// TODO: merkle torrents. specifically torrent_info::add_merkle_nodes and torrent with "root hash"
|
|
|
|
// TODO: torrent with 'p' (padfile) attribute
|
|
|
|
// TODO: torrent with 'h' (hidden) attribute
|
|
|
|
// TODO: torrent with 'x' (executable) attribute
|
|
|
|
// TODO: torrent with 'l' (symlink) attribute
|
|
|
|
// TODO: creating a merkle torrent (torrent_info::build_merkle_list)
|
2016-11-12 19:50:49 +01:00
|
|
|
// TODO: torrent with multiple trackers in multiple tiers, making sure we
|
|
|
|
// shuffle them (how do you test shuffling?, load it multiple times and make
|
|
|
|
// sure it's in different order at least once)
|
2015-02-16 22:53:23 +01:00
|
|
|
// TODO: torrents with a zero-length name
|
|
|
|
// TODO: torrents with a merkle tree and add_merkle_nodes
|
|
|
|
// TODO: torrent with a non-dictionary info-section
|
|
|
|
// TODO: torrents with DHT nodes
|
|
|
|
// TODO: torrent with url-list as a single string
|
|
|
|
// TODO: torrent with http seed as a single string
|
|
|
|
// TODO: torrent with a comment
|
|
|
|
// TODO: torrent with an SSL cert
|
|
|
|
// TODO: torrent with attributes (executable and hidden)
|
|
|
|
// TODO: torrent_info::add_tracker
|
|
|
|
// TODO: torrent_info constructor that takes an invalid bencoded buffer
|
|
|
|
// TODO: verify_encoding with a string that triggers character replacement
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2017-05-04 02:18:30 +02:00
|
|
|
TORRENT_TEST(url_list_and_httpseeds)
|
|
|
|
{
|
|
|
|
entry info;
|
|
|
|
info["pieces"] = "aaaaaaaaaaaaaaaaaaaa";
|
|
|
|
info["name.utf-8"] = "test1";
|
|
|
|
info["name"] = "test__";
|
|
|
|
info["piece length"] = 16 * 1024;
|
|
|
|
info["length"] = 3245;
|
|
|
|
entry::list_type l;
|
|
|
|
l.push_back(entry("http://foo.com/bar1"));
|
|
|
|
l.push_back(entry("http://foo.com/bar1"));
|
|
|
|
l.push_back(entry("http://foo.com/bar2"));
|
|
|
|
entry const e(l);
|
|
|
|
entry torrent;
|
|
|
|
torrent["url-list"] = e;
|
|
|
|
torrent["httpseeds"] = e;
|
|
|
|
torrent["info"] = info;
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
2017-07-20 09:27:47 +02:00
|
|
|
torrent_info ti(buf, from_span);
|
2017-05-04 02:18:30 +02:00
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 4);
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:36:15 +01:00
|
|
|
TORRENT_TEST(add_url_seed)
|
|
|
|
{
|
|
|
|
torrent_info ti(sha1_hash(" "));
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 0);
|
|
|
|
|
|
|
|
ti.add_url_seed("http://test.com");
|
|
|
|
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 1);
|
|
|
|
web_seed_entry we = ti.web_seeds()[0];
|
|
|
|
TEST_EQUAL(we.type, web_seed_entry::url_seed);
|
|
|
|
TEST_EQUAL(we.url, "http://test.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(add_http_seed)
|
|
|
|
{
|
|
|
|
torrent_info ti(sha1_hash(" "));
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 0);
|
|
|
|
|
|
|
|
ti.add_http_seed("http://test.com");
|
|
|
|
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 1);
|
|
|
|
web_seed_entry we = ti.web_seeds()[0];
|
|
|
|
TEST_EQUAL(we.type, web_seed_entry::http_seed);
|
|
|
|
TEST_EQUAL(we.url, "http://test.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(set_web_seeds)
|
|
|
|
{
|
|
|
|
torrent_info ti(sha1_hash(" "));
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 0);
|
|
|
|
|
|
|
|
std::vector<web_seed_entry> seeds;
|
|
|
|
web_seed_entry e1("http://test1.com", web_seed_entry::url_seed);
|
|
|
|
seeds.push_back(e1);
|
|
|
|
web_seed_entry e2("http://test2com", web_seed_entry::http_seed);
|
|
|
|
seeds.push_back(e2);
|
|
|
|
|
|
|
|
ti.set_web_seeds(seeds);
|
|
|
|
|
|
|
|
TEST_EQUAL(ti.web_seeds().size(), 2);
|
|
|
|
TEST_CHECK(ti.web_seeds() == seeds);
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#define SEPARATOR "\\"
|
|
|
|
#else
|
|
|
|
#define SEPARATOR "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_TEST(sanitize_long_path)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
// test sanitize_append_path_element
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
sanitize_append_path_element(path,
|
2016-11-12 19:50:49 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
2016-08-13 13:04:53 +02:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_");
|
2015-02-15 02:04:50 +01:00
|
|
|
sanitize_append_path_element(path,
|
2016-11-12 19:50:49 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
2016-08-13 13:04:53 +02:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcde.test");
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_EQUAL(path,
|
2016-11-12 19:50:49 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
2016-01-18 19:07:04 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_" SEPARATOR
|
2016-11-12 19:50:49 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_abcdefghi_"
|
2015-02-15 02:04:50 +01:00
|
|
|
"abcdefghi_abcdefghi_abcdefghi_abcdefghi_.test");
|
2016-01-18 19:07:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(sanitize_path_trailing_dots)
|
|
|
|
{
|
|
|
|
std::string path;
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a");
|
|
|
|
sanitize_append_path_element(path, "abc...");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "abc" SEPARATOR "c");
|
2015-02-15 02:04:50 +01:00
|
|
|
#else
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "abc..." SEPARATOR "c");
|
2015-02-15 02:04:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc...");
|
2015-02-15 02:04:50 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "abc");
|
2015-02-15 02:04:50 +01:00
|
|
|
#else
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "abc...");
|
2015-02-15 02:04:50 +01:00
|
|
|
#endif
|
|
|
|
|
2015-05-27 23:27:41 +02:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc.");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "abc");
|
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "abc.");
|
|
|
|
#endif
|
|
|
|
|
2015-05-27 23:27:41 +02:00
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a. . .");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-05-27 23:27:41 +02:00
|
|
|
TEST_EQUAL(path, "a");
|
2016-01-18 19:07:04 +01:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "a. . .");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(sanitize_path_trailing_spaces)
|
|
|
|
{
|
|
|
|
std::string path;
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a");
|
|
|
|
sanitize_append_path_element(path, "abc ");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "abc" SEPARATOR "c");
|
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "abc " SEPARATOR "c");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc ");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "abc");
|
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "abc ");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc ");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "abc");
|
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "abc ");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(sanitize_path)
|
|
|
|
{
|
|
|
|
std::string path;
|
2017-08-16 22:46:47 +02:00
|
|
|
sanitize_append_path_element(path, "\0\0\xed\0\x80");
|
2017-08-15 13:37:12 +02:00
|
|
|
TEST_EQUAL(path, "_");
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "/a/");
|
|
|
|
sanitize_append_path_element(path, "b");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "b" SEPARATOR "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a...b");
|
2015-05-27 23:27:41 +02:00
|
|
|
TEST_EQUAL(path, "a...b");
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a");
|
|
|
|
sanitize_append_path_element(path, "..");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "a" SEPARATOR "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "a");
|
|
|
|
sanitize_append_path_element(path, "..");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "a");
|
|
|
|
|
2015-05-27 23:27:41 +02:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "/..");
|
|
|
|
sanitize_append_path_element(path, ".");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
TEST_EQUAL(path, "c");
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "dev:");
|
2015-05-27 23:27:41 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-11-21 01:05:50 +01:00
|
|
|
TEST_EQUAL(path, "dev_");
|
2015-05-27 23:27:41 +02:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "dev:");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "c:");
|
|
|
|
sanitize_append_path_element(path, "b");
|
2015-05-27 23:27:41 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-11-21 01:05:50 +01:00
|
|
|
TEST_EQUAL(path, "c_" SEPARATOR "b");
|
2015-05-27 23:27:41 +02:00
|
|
|
#else
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "c:" SEPARATOR "b");
|
2015-05-27 23:27:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "c:");
|
|
|
|
sanitize_append_path_element(path, ".");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-11-21 01:05:50 +01:00
|
|
|
TEST_EQUAL(path, "c_" SEPARATOR "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
#else
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "c:" SEPARATOR "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "\\c");
|
|
|
|
sanitize_append_path_element(path, ".");
|
|
|
|
sanitize_append_path_element(path, "c");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "c" SEPARATOR "c");
|
2015-05-27 23:27:41 +02:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "\b");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "_");
|
2015-05-27 23:27:41 +02:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "\b");
|
|
|
|
sanitize_append_path_element(path, "filename");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "_" SEPARATOR "filename");
|
2015-05-27 23:27:41 +02:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename");
|
|
|
|
sanitize_append_path_element(path, "\b");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "filename" SEPARATOR "_");
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc");
|
|
|
|
sanitize_append_path_element(path, "");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "abc" SEPARATOR "_");
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "abc");
|
|
|
|
sanitize_append_path_element(path, " ");
|
2016-01-18 19:07:04 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "abc");
|
2016-01-18 19:07:04 +01:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "abc" SEPARATOR " ");
|
|
|
|
#endif
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "");
|
|
|
|
sanitize_append_path_element(path, "abc");
|
2016-01-18 19:07:04 +01:00
|
|
|
TEST_EQUAL(path, "_" SEPARATOR "abc");
|
2015-12-20 00:28:12 +01:00
|
|
|
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "\b?filename=4");
|
2015-02-15 02:04:50 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "__filename=4");
|
2015-02-15 02:04:50 +01:00
|
|
|
#else
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "_?filename=4");
|
2015-02-15 02:04:50 +01:00
|
|
|
#endif
|
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename=4");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename=4");
|
|
|
|
|
|
|
|
// valid 2-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xc2\xa1");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename\xc2\xa1");
|
|
|
|
|
|
|
|
// truncated 2-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xc2");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
|
|
|
|
|
|
|
// valid 3-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xe2\x9f\xb9");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename\xe2\x9f\xb9");
|
|
|
|
|
|
|
|
// truncated 3-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xe2\x9f");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
|
|
|
|
|
|
|
// truncated 3-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xe2");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
2015-02-15 02:04:50 +01:00
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
// valid 4-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xf0\x9f\x92\x88");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename\xf0\x9f\x92\x88");
|
|
|
|
|
|
|
|
// truncated 4-byte sequence
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xf0\x9f\x92");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
|
|
|
|
|
|
|
// 5-byte utf-8 sequence (not allowed)
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xf8\x9f\x9f\x9f\x9f" "foobar");
|
2017-05-15 02:58:51 +02:00
|
|
|
TEST_EQUAL(path, "filename_foobar");
|
2015-12-20 00:28:12 +01:00
|
|
|
|
|
|
|
// redundant (overlong) 2-byte sequence
|
|
|
|
// ascii code 0x2e encoded with a leading 0
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xc0\xae");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
|
|
|
|
|
|
|
// redundant (overlong) 3-byte sequence
|
|
|
|
// ascii code 0x2e encoded with two leading 0s
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xe0\x80\xae");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
|
|
|
|
|
|
|
// redundant (overlong) 4-byte sequence
|
|
|
|
// ascii code 0x2e encoded with three leading 0s
|
|
|
|
path.clear();
|
2016-08-13 13:04:53 +02:00
|
|
|
sanitize_append_path_element(path, "filename\xf0\x80\x80\xae");
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(path, "filename_");
|
2017-05-15 02:58:51 +02:00
|
|
|
|
|
|
|
// a filename where every character is filtered is not replaced by an understcore
|
|
|
|
path.clear();
|
2017-05-15 15:08:57 +02:00
|
|
|
sanitize_append_path_element(path, "//\\");
|
2017-05-15 02:58:51 +02:00
|
|
|
TEST_EQUAL(path, "");
|
|
|
|
|
|
|
|
// make sure suspicious unicode characters are filtered out
|
|
|
|
path.clear();
|
|
|
|
// that's utf-8 for U+200e LEFT-TO-RIGHT MARK
|
2017-05-15 15:08:57 +02:00
|
|
|
sanitize_append_path_element(path, "foo\xe2\x80\x8e" "bar");
|
2017-05-15 02:58:51 +02:00
|
|
|
TEST_EQUAL(path, "foobar");
|
|
|
|
|
|
|
|
// make sure suspicious unicode characters are filtered out
|
|
|
|
path.clear();
|
|
|
|
// that's utf-8 for U+202b RIGHT-TO-LEFT EMBEDDING
|
2017-05-15 15:08:57 +02:00
|
|
|
sanitize_append_path_element(path, "foo\xe2\x80\xab" "bar");
|
2017-05-15 02:58:51 +02:00
|
|
|
TEST_EQUAL(path, "foobar");
|
2015-12-20 00:28:12 +01:00
|
|
|
}
|
|
|
|
|
2017-08-15 13:37:12 +02:00
|
|
|
TORRENT_TEST(sanitize_path_zeroes)
|
|
|
|
{
|
|
|
|
std::string path;
|
2017-08-16 22:46:47 +02:00
|
|
|
sanitize_append_path_element(path, "\0foo");
|
|
|
|
TEST_EQUAL(path, "_");
|
2017-08-15 13:37:12 +02:00
|
|
|
|
|
|
|
path.clear();
|
2017-08-16 22:46:47 +02:00
|
|
|
sanitize_append_path_element(path, "\0\0\0\0");
|
|
|
|
TEST_EQUAL(path, "_");
|
2017-08-15 13:37:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 01:05:50 +01:00
|
|
|
TORRENT_TEST(sanitize_path_colon)
|
|
|
|
{
|
|
|
|
std::string path;
|
2017-11-23 01:17:43 +01:00
|
|
|
sanitize_append_path_element(path, "foo:bar");
|
2017-11-21 01:05:50 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(path, "foo_bar");
|
|
|
|
#else
|
|
|
|
TEST_EQUAL(path, "foo:bar");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-12-20 00:28:12 +01:00
|
|
|
TORRENT_TEST(verify_encoding)
|
|
|
|
{
|
2015-02-15 02:04:50 +01:00
|
|
|
// verify_encoding
|
|
|
|
std::string test = "\b?filename=4";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(verify_encoding(test));
|
|
|
|
TEST_CHECK(test == "\b?filename=4");
|
2015-02-15 02:04:50 +01:00
|
|
|
|
|
|
|
test = "filename=4";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(verify_encoding(test));
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename=4");
|
|
|
|
|
|
|
|
// valid 2-byte sequence
|
|
|
|
test = "filename\xc2\xa1";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename\xc2\xa1");
|
|
|
|
|
|
|
|
// truncated 2-byte sequence
|
|
|
|
test = "filename\xc2";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename_");
|
|
|
|
|
|
|
|
// valid 3-byte sequence
|
|
|
|
test = "filename\xe2\x9f\xb9";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename\xe2\x9f\xb9");
|
|
|
|
|
|
|
|
// truncated 3-byte sequence
|
|
|
|
test = "filename\xe2\x9f";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename_");
|
|
|
|
|
|
|
|
// truncated 3-byte sequence
|
|
|
|
test = "filename\xe2";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename_");
|
|
|
|
|
|
|
|
// valid 4-byte sequence
|
|
|
|
test = "filename\xf0\x9f\x92\x88";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename\xf0\x9f\x92\x88");
|
|
|
|
|
|
|
|
// truncated 4-byte sequence
|
|
|
|
test = "filename\xf0\x9f\x92";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename_");
|
|
|
|
|
|
|
|
// 5-byte utf-8 sequence (not allowed)
|
|
|
|
test = "filename\xf8\x9f\x9f\x9f\x9f""foobar";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename_____foobar");
|
|
|
|
|
|
|
|
// redundant (overlong) 2-byte sequence
|
|
|
|
// ascii code 0x2e encoded with a leading 0
|
|
|
|
test = "filename\xc0\xae";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename__");
|
|
|
|
|
|
|
|
// redundant (overlong) 3-byte sequence
|
|
|
|
// ascii code 0x2e encoded with two leading 0s
|
|
|
|
test = "filename\xe0\x80\xae";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename___");
|
|
|
|
|
|
|
|
// redundant (overlong) 4-byte sequence
|
|
|
|
// ascii code 0x2e encoded with three leading 0s
|
|
|
|
test = "filename\xf0\x80\x80\xae";
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_CHECK(!verify_encoding(test));
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", test.c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(test == "filename____");
|
2017-08-15 13:37:12 +02:00
|
|
|
|
|
|
|
// missing byte header
|
|
|
|
test = "filename\xed\0\x80";
|
|
|
|
TEST_CHECK(!verify_encoding(test));
|
|
|
|
fprintf(stdout, "%s\n", test.c_str());
|
|
|
|
TEST_CHECK(test == "filename_");
|
2015-12-20 00:28:12 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:07:04 +01:00
|
|
|
TORRENT_TEST(parse_torrents)
|
2015-12-20 00:28:12 +01:00
|
|
|
{
|
|
|
|
// test torrent parsing
|
|
|
|
|
|
|
|
entry info;
|
|
|
|
info["pieces"] = "aaaaaaaaaaaaaaaaaaaa";
|
|
|
|
info["name.utf-8"] = "test1";
|
|
|
|
info["name"] = "test__";
|
|
|
|
info["piece length"] = 16 * 1024;
|
|
|
|
info["length"] = 3245;
|
|
|
|
entry torrent;
|
|
|
|
torrent["info"] = info;
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
2018-01-19 22:40:39 +01:00
|
|
|
torrent_info ti1(buf, from_span);
|
|
|
|
std::cout << ti1.name() << std::endl;
|
|
|
|
TEST_CHECK(ti1.name() == "test1");
|
2015-12-20 00:28:12 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
info["name.utf-8"] = "c:/test1/test2/test3";
|
|
|
|
#else
|
|
|
|
info["name.utf-8"] = "/test1/test2/test3";
|
|
|
|
#endif
|
|
|
|
torrent["info"] = info;
|
|
|
|
buf.clear();
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
2017-07-20 09:27:47 +02:00
|
|
|
torrent_info ti2(buf, from_span);
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << ti2.name() << std::endl;
|
2015-12-20 00:28:12 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-11-21 01:05:50 +01:00
|
|
|
TEST_EQUAL(ti2.name(), "c_test1test2test3");
|
2015-12-20 00:28:12 +01:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(ti2.name(), "test1test2test3");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
info["name.utf-8"] = "test2/../test3/.././../../test4";
|
|
|
|
torrent["info"] = info;
|
|
|
|
buf.clear();
|
|
|
|
bencode(std::back_inserter(buf), torrent);
|
2017-07-20 09:27:47 +02:00
|
|
|
torrent_info ti3(buf, from_span);
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << ti3.name() << std::endl;
|
2015-12-20 00:28:12 +01:00
|
|
|
TEST_EQUAL(ti3.name(), "test2..test3.......test4");
|
2015-02-15 02:04:50 +01:00
|
|
|
|
|
|
|
std::string root_dir = parent_path(current_working_directory());
|
2018-12-10 14:15:29 +01:00
|
|
|
for (auto const t : test_torrents)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
2018-12-10 14:15:29 +01:00
|
|
|
std::printf("loading %s\n", t.file);
|
2015-02-15 02:04:50 +01:00
|
|
|
std::string filename = combine_path(combine_path(root_dir, "test_torrents")
|
2018-12-10 14:15:29 +01:00
|
|
|
, t.file);
|
2017-07-20 09:27:47 +02:00
|
|
|
error_code ec;
|
2016-08-22 17:02:51 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>(filename, ec);
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(!ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf(" loading(\"%s\") -> failed %s\n", filename.c_str()
|
2015-02-15 02:04:50 +01:00
|
|
|
, ec.message().c_str());
|
|
|
|
|
2018-12-10 14:15:29 +01:00
|
|
|
if (t.file == "whitespace_url.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
// make sure we trimmed the url
|
|
|
|
TEST_CHECK(ti->trackers().size() > 0);
|
|
|
|
if (ti->trackers().size() > 0)
|
|
|
|
TEST_CHECK(ti->trackers()[0].url == "udp://test.com/announce");
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "duplicate_files.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
// make sure we disambiguated the files
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(ti->files().file_path(file_index_t{0}) == combine_path(combine_path("temp", "foo"), "bar.txt"));
|
|
|
|
TEST_CHECK(ti->files().file_path(file_index_t{1}) == combine_path(combine_path("temp", "foo"), "bar.1.txt"));
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "pad_file.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
2017-08-03 11:31:03 +02:00
|
|
|
TEST_EQUAL(bool(ti->files().file_flags(file_index_t{0}) & file_storage::flag_pad_file), false);
|
|
|
|
TEST_EQUAL(bool(ti->files().file_flags(file_index_t{1}) & file_storage::flag_pad_file), true);
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "creation_date.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
2016-08-22 17:02:51 +02:00
|
|
|
TEST_EQUAL(ti->creation_date(), 1234567);
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "duplicate_web_seeds.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->web_seeds().size(), 3);
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "no_creation_date.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_CHECK(!ti->creation_date());
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "url_seed.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->web_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/file");
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_EQUAL(ti->http_seeds().size(), 0);
|
|
|
|
TEST_EQUAL(ti->url_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->url_seeds()[0], "http://test.com/file");
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "url_seed_multi.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->web_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/file/");
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_EQUAL(ti->http_seeds().size(), 0);
|
|
|
|
TEST_EQUAL(ti->url_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->url_seeds()[0], "http://test.com/file/");
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "url_seed_multi_single_file.torrent"_sv)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->web_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/file/temp/foo/bar.txt");
|
|
|
|
}
|
|
|
|
else if (t.file == "url_seed_multi_space.torrent"_sv
|
|
|
|
|| t.file == "url_seed_multi_space_nolist.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->web_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/test%20file/foo%20bar/");
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_EQUAL(ti->http_seeds().size(), 0);
|
|
|
|
TEST_EQUAL(ti->url_seeds().size(), 1);
|
|
|
|
TEST_EQUAL(ti->url_seeds()[0], "http://test.com/test%20file/foo%20bar/");
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "invalid_name2.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
// if, after all invalid characters are removed from the name, it ends up
|
|
|
|
// being empty, it's set to the info-hash. Some torrents also have an empty name
|
|
|
|
// in which case it's also set to the info-hash
|
|
|
|
TEST_EQUAL(ti->name(), "b61560c2918f463768cd122b6d2fdd47b77bdb35");
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "invalid_name3.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
2016-01-18 19:07:04 +01:00
|
|
|
// windows does not allow trailing spaces in filenames
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_EQUAL(ti->name(), "foobar");
|
2016-01-18 19:07:04 +01:00
|
|
|
#else
|
|
|
|
TEST_EQUAL(ti->name(), "foobar ");
|
|
|
|
#endif
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2019-03-14 10:02:41 +01:00
|
|
|
else if (t.file == "symlink1.torrent"_sv)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{1}), "temp" SEPARATOR "a" SEPARATOR "b" SEPARATOR "bar");
|
|
|
|
}
|
|
|
|
else if (t.file == "symlink2.torrent"_sv)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 5);
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{0}), "Some.framework" SEPARATOR "Versions" SEPARATOR "A" SEPARATOR "SDL2");
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{4}), "Some.framework" SEPARATOR "Versions" SEPARATOR "A");
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "slash_path.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{0}), "temp" SEPARATOR "bar");
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "slash_path2.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{0}), "temp" SEPARATOR "abc....def" SEPARATOR "bar");
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "slash_path3.torrent"_sv)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{0}), "temp....abc");
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "symlink_zero_size.torrent"_sv)
|
2016-08-08 04:37:10 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
2019-03-14 10:02:41 +01:00
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t(1)), "temp" SEPARATOR "a" SEPARATOR "b" SEPARATOR "bar");
|
2016-08-08 04:37:10 +02:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "pad_file_no_path.torrent"_sv)
|
2016-08-08 04:37:10 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{1}), combine_path(".pad", "0"));
|
2016-08-08 04:37:10 +02:00
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "absolute_filename.torrent"_sv)
|
2017-10-01 04:30:32 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{0}), combine_path("temp", "abcde"));
|
|
|
|
TEST_EQUAL(ti->files().file_path(file_index_t{1}), combine_path("temp", "foobar"));
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "invalid_filename.torrent"_sv)
|
2017-10-08 02:04:56 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 2);
|
|
|
|
}
|
2018-12-10 14:15:29 +01:00
|
|
|
else if (t.file == "invalid_filename2.torrent"_sv)
|
2017-10-08 02:04:56 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(ti->num_files(), 3);
|
|
|
|
}
|
2019-07-22 00:50:57 +02:00
|
|
|
else if (t.file == "overlapping_symlinks.torrent"_sv)
|
|
|
|
{
|
|
|
|
TEST_CHECK(ti->num_files() > 3);
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{0}), "SDL2.framework" SEPARATOR "Versions" SEPARATOR "Current" SEPARATOR "Headers");
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{1}), "SDL2.framework" SEPARATOR "Versions" SEPARATOR "Current" SEPARATOR "Resources");
|
|
|
|
TEST_EQUAL(ti->files().symlink(file_index_t{2}), "SDL2.framework" SEPARATOR "Versions" SEPARATOR "Current" SEPARATOR "SDL2");
|
|
|
|
}
|
2015-02-15 02:04:50 +01:00
|
|
|
|
|
|
|
file_storage const& fs = ti->files();
|
2018-01-23 19:23:57 +01:00
|
|
|
for (file_index_t idx{0}; idx != file_index_t(fs.num_files()); ++idx)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
2018-01-23 19:23:57 +01:00
|
|
|
piece_index_t const first = ti->map_file(idx, 0, 0).piece;
|
|
|
|
piece_index_t const last = ti->map_file(idx, std::max(fs.file_size(idx)-1, std::int64_t(0)), 0).piece;
|
|
|
|
file_flags_t const flags = fs.file_flags(idx);
|
|
|
|
sha1_hash const ih = fs.hash(idx);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" %11" PRId64 " %c%c%c%c [ %4d, %4d ] %7u %s %s %s%s\n"
|
2018-01-23 19:23:57 +01:00
|
|
|
, fs.file_size(idx)
|
2015-02-15 02:04:50 +01:00
|
|
|
, (flags & file_storage::flag_pad_file)?'p':'-'
|
|
|
|
, (flags & file_storage::flag_executable)?'x':'-'
|
|
|
|
, (flags & file_storage::flag_hidden)?'h':'-'
|
|
|
|
, (flags & file_storage::flag_symlink)?'l':'-'
|
2016-12-22 16:42:33 +01:00
|
|
|
, static_cast<int>(first), static_cast<int>(last)
|
2018-01-23 19:23:57 +01:00
|
|
|
, std::uint32_t(fs.mtime(idx))
|
2016-07-29 08:36:15 +02:00
|
|
|
, ih != sha1_hash(nullptr) ? aux::to_hex(ih).c_str() : ""
|
2018-01-23 19:23:57 +01:00
|
|
|
, fs.file_path(idx).c_str()
|
2015-02-15 02:04:50 +01:00
|
|
|
, flags & file_storage::flag_symlink ? "-> ": ""
|
2018-01-23 19:23:57 +01:00
|
|
|
, flags & file_storage::flag_symlink ? fs.symlink(idx).c_str() : "");
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(sizeof(test_error_torrents)/sizeof(test_error_torrents[0])); ++i)
|
2015-02-15 02:04:50 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("loading %s\n", test_error_torrents[i].file);
|
2016-08-22 17:02:51 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>(combine_path(
|
2016-05-14 19:24:29 +02:00
|
|
|
combine_path(root_dir, "test_torrents"), test_error_torrents[i].file), ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("E: \"%s\"\nexpected: \"%s\"\n", ec.message().c_str()
|
2015-05-16 20:35:47 +02:00
|
|
|
, test_error_torrents[i].error.message().c_str());
|
2015-02-15 02:04:50 +01:00
|
|
|
TEST_CHECK(ec.message() == test_error_torrents[i].error.message());
|
2017-01-19 02:13:46 +01:00
|
|
|
TEST_EQUAL(ti->is_valid(), false);
|
2015-02-15 02:04:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
namespace {
|
|
|
|
|
2019-02-28 17:23:11 +01:00
|
|
|
struct file_t
|
2013-11-18 01:56:02 +01:00
|
|
|
{
|
2019-02-28 17:23:11 +01:00
|
|
|
std::string filename;
|
|
|
|
int size;
|
|
|
|
file_flags_t flags;
|
|
|
|
string_view expected_filename;
|
|
|
|
};
|
2013-11-18 01:56:02 +01:00
|
|
|
|
2019-02-28 17:23:11 +01:00
|
|
|
std::vector<lt::aux::vector<file_t, lt::file_index_t>> const test_cases
|
|
|
|
{
|
2015-03-21 17:55:23 +01:00
|
|
|
{
|
2019-02-28 17:23:11 +01:00
|
|
|
{"test/temporary.txt", 0x4000, {}, "test/temporary.txt"},
|
|
|
|
{"test/Temporary.txt", 0x4000, {}, "test/Temporary.1.txt"},
|
|
|
|
{"test/TeMPorArY.txT", 0x4000, {}, "test/TeMPorArY.2.txT"},
|
|
|
|
// a file with the same name in a seprate directory is fine
|
|
|
|
{"test/test/TEMPORARY.TXT", 0x4000, {}, "test/test/TEMPORARY.TXT"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"test/b.exe", 0x4000, {}, "test/b.exe"},
|
|
|
|
// duplicate of b.exe
|
|
|
|
{"test/B.ExE", 0x4000, {}, "test/B.1.ExE"},
|
|
|
|
// duplicate of b.exe
|
|
|
|
{"test/B.exe", 0x4000, {}, "test/B.2.exe"},
|
|
|
|
{"test/filler", 0x4000, {}, "test/filler"},
|
|
|
|
},
|
2019-04-05 01:27:46 +02:00
|
|
|
{
|
|
|
|
{"test/a/b/c/d/e/f/g/h/i/j/k/l/m", 0x4000, {}, "test/a/b/c/d/e/f/g/h/i/j/k/l/m"},
|
|
|
|
{"test/a", 0x4000, {}, "test/a.1"},
|
|
|
|
{"test/a/b", 0x4000, {}, "test/a/b.1"},
|
|
|
|
{"test/a/b/c", 0x4000, {}, "test/a/b/c.1"},
|
|
|
|
{"test/a/b/c/d", 0x4000, {}, "test/a/b/c/d.1"},
|
|
|
|
{"test/a/b/c/d/e", 0x4000, {}, "test/a/b/c/d/e.1"},
|
|
|
|
{"test/a/b/c/d/e/f", 0x4000, {}, "test/a/b/c/d/e/f.1"},
|
|
|
|
{"test/a/b/c/d/e/f/g", 0x4000, {}, "test/a/b/c/d/e/f/g.1"},
|
|
|
|
{"test/a/b/c/d/e/f/g/h", 0x4000, {}, "test/a/b/c/d/e/f/g/h.1"},
|
|
|
|
{"test/a/b/c/d/e/f/g/h/i", 0x4000, {}, "test/a/b/c/d/e/f/g/h/i.1"},
|
|
|
|
{"test/a/b/c/d/e/f/g/h/i/j", 0x4000, {}, "test/a/b/c/d/e/f/g/h/i/j.1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// it doesn't matter whether the file comes before the directory,
|
|
|
|
// directories take precedence
|
|
|
|
{"test/a", 0x4000, {}, "test/a.1"},
|
|
|
|
{"test/a/b", 0x4000, {}, "test/a/b"},
|
|
|
|
},
|
2019-02-28 17:23:11 +01:00
|
|
|
{
|
|
|
|
{"test/A/tmp", 0x4000, {}, "test/A/tmp"},
|
|
|
|
// a file may not have the same name as a directory
|
|
|
|
{"test/a", 0x4000, {}, "test/a.1"},
|
|
|
|
// duplicate of directory a
|
|
|
|
{"test/A", 0x4000, {}, "test/A.2"},
|
|
|
|
{"test/filler", 0x4000, {}, "test/filler"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// a subset of this path collides with the next filename
|
|
|
|
{"test/long/path/name/that/collides", 0x4000, {}, "test/long/path/name/that/collides"},
|
|
|
|
// so this file needs to be renamed, to not collide with the path name
|
|
|
|
{"test/long/path", 0x4000, {}, "test/long/path.1"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/filler-2", 0x4000, {}, "test/filler-2"},
|
|
|
|
},
|
2019-02-28 18:18:50 +01:00
|
|
|
{
|
|
|
|
// pad files are allowed to collide, as long as they have the same size
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234"},
|
|
|
|
{"test/filler-2", 0x4000, {}, "test/filler-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// pad files of different sizes are NOT allowed to collide
|
|
|
|
{"test/.pad/1234", 0x8000, file_storage::flag_pad_file, "test/.pad/1234"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234.1"},
|
|
|
|
{"test/filler-2", 0x4000, {}, "test/filler-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// pad files are NOT allowed to collide with normal files
|
|
|
|
{"test/.pad/1234", 0x4000, {}, "test/.pad/1234"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234.1"},
|
|
|
|
{"test/filler-2", 0x4000, {}, "test/filler-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// normal files are NOT allowed to collide with pad files
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/.pad/1234", 0x4000, {}, "test/.pad/1234.1"},
|
|
|
|
{"test/filler-2", 0x4000, {}, "test/filler-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// pad files are NOT allowed to collide with directories
|
|
|
|
{"test/.pad/1234", 0x4000, file_storage::flag_pad_file, "test/.pad/1234.1"},
|
|
|
|
{"test/filler-1", 0x4000, {}, "test/filler-1"},
|
|
|
|
{"test/.pad/1234/filler-2", 0x4000, {}, "test/.pad/1234/filler-2"},
|
|
|
|
},
|
2019-02-28 17:23:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void test_resolve_duplicates(aux::vector<file_t, file_index_t> const& test)
|
|
|
|
{
|
|
|
|
file_storage fs;
|
|
|
|
for (auto const& f : test) fs.add_file(f.filename, f.size, f.flags);
|
2013-11-18 01:56:02 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::create_torrent t(fs, 0x4000);
|
2013-11-18 01:56:02 +01:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
sha1_hash ph;
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.piece_range())
|
2013-11-18 01:56:02 +01:00
|
|
|
t.set_hash(i, ph);
|
|
|
|
|
|
|
|
std::vector<char> tmp;
|
2016-10-25 23:27:48 +02:00
|
|
|
std::back_insert_iterator<std::vector<char>> out(tmp);
|
2013-11-18 01:56:02 +01:00
|
|
|
|
|
|
|
entry tor = t.generate();
|
|
|
|
bencode(out, tor);
|
|
|
|
|
2017-07-20 09:27:47 +02:00
|
|
|
torrent_info ti(tmp, from_span);
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.file_range())
|
2013-11-18 01:56:02 +01:00
|
|
|
{
|
2015-01-21 16:46:12 +01:00
|
|
|
std::string p = ti.files().file_path(i);
|
2013-11-18 01:56:02 +01:00
|
|
|
convert_path_to_posix(p);
|
2019-02-28 17:23:11 +01:00
|
|
|
std::printf("%s == %s\n", p.c_str(), test[i].expected_filename.to_string().c_str());
|
2015-03-15 00:10:20 +01:00
|
|
|
|
2019-02-28 17:23:11 +01:00
|
|
|
TEST_EQUAL(p, test[i].expected_filename);
|
2013-11-18 01:56:02 +01:00
|
|
|
}
|
2014-12-17 06:33:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:56:03 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(resolve_duplicates)
|
|
|
|
{
|
2019-02-28 17:23:11 +01:00
|
|
|
for (auto const& t : test_cases)
|
|
|
|
test_resolve_duplicates(t);
|
2015-05-31 14:21:16 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 19:24:29 +02:00
|
|
|
TORRENT_TEST(empty_file)
|
|
|
|
{
|
|
|
|
error_code ec;
|
2017-07-20 09:27:47 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>("", ec, from_span);
|
2016-05-14 19:24:29 +02:00
|
|
|
TEST_CHECK(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(empty_file2)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-07-20 09:27:47 +02:00
|
|
|
auto ti = std::make_shared<torrent_info>("", from_span);
|
2016-05-14 19:24:29 +02:00
|
|
|
TEST_ERROR("expected exception thrown");
|
|
|
|
}
|
2016-09-25 15:50:48 +02:00
|
|
|
catch (system_error const& e)
|
2016-05-14 19:24:29 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("Expected error: %s\n", e.code().message().c_str());
|
2016-05-14 19:24:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(copy)
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-01-15 04:42:08 +01:00
|
|
|
|
2016-08-22 17:02:51 +02:00
|
|
|
std::shared_ptr<torrent_info> a = std::make_shared<torrent_info>(
|
2015-01-15 04:42:08 +01:00
|
|
|
combine_path(parent_path(current_working_directory())
|
2016-08-22 17:02:51 +02:00
|
|
|
, combine_path("test_torrents", "sample.torrent")));
|
2014-12-17 06:33:16 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<char const*, file_index_t> expected_files =
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
|
|
|
"sample/text_file2.txt",
|
|
|
|
"sample/.____padding_file/0",
|
|
|
|
"sample/text_file.txt",
|
|
|
|
};
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<sha1_hash, file_index_t> file_hashes =
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
sha1_hash(nullptr),
|
|
|
|
sha1_hash(nullptr),
|
2014-12-17 06:33:16 +01:00
|
|
|
sha1_hash("abababababababababab")
|
|
|
|
};
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
file_storage const& fs = a->files();
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs.file_range())
|
2015-01-21 16:46:12 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
std::string p = fs.file_path(i);
|
2015-01-21 16:46:12 +01:00
|
|
|
convert_path_to_posix(p);
|
|
|
|
TEST_EQUAL(p, expected_files[i]);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", p.c_str());
|
2015-01-21 16:46:12 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(a->files().hash(i), file_hashes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the torrent_info object
|
2016-08-22 17:02:51 +02:00
|
|
|
std::shared_ptr<torrent_info> b = std::make_shared<torrent_info>(*a);
|
2015-01-21 16:46:12 +01:00
|
|
|
|
|
|
|
// clear out the buffer for a, just to make sure b doesn't have any
|
|
|
|
// references into it by mistake
|
|
|
|
int s = a->metadata_size();
|
2018-01-23 19:23:57 +01:00
|
|
|
std::memset(a->metadata().get(), 0, std::size_t(s));
|
2015-01-21 16:46:12 +01:00
|
|
|
|
|
|
|
a.reset();
|
|
|
|
|
|
|
|
TEST_EQUAL(b->num_files(), 3);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
file_storage const& fs2 = b->files();
|
2018-06-02 19:29:40 +02:00
|
|
|
for (auto const i : fs2.file_range())
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
std::string p = fs2.file_path(i);
|
2014-12-17 06:33:16 +01:00
|
|
|
convert_path_to_posix(p);
|
|
|
|
TEST_EQUAL(p, expected_files[i]);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", p.c_str());
|
2014-12-17 06:33:16 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(fs2.hash(i), file_hashes[i]);
|
2014-12-17 06:33:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-06 23:34:40 +02:00
|
|
|
|
|
|
|
struct A
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
};
|
|
|
|
|
|
|
|
TORRENT_TEST(copy_ptr)
|
|
|
|
{
|
|
|
|
copy_ptr<A> a(new A{4});
|
|
|
|
copy_ptr<A> b(a);
|
|
|
|
|
|
|
|
TEST_EQUAL(a->val, b->val);
|
|
|
|
TEST_CHECK(&*a != &*b);
|
|
|
|
a->val = 5;
|
|
|
|
TEST_EQUAL(b->val, 4);
|
|
|
|
}
|