premiere-libtorrent/test/test_torrent_parse.cpp

229 lines
8.4 KiB
C++
Raw Normal View History

2013-01-31 04:39:14 +01:00
/*
Copyright (c) 2013, 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.hpp"
#include "libtorrent/torrent_info.hpp"
2013-02-26 06:57:29 +01:00
#if TORRENT_USE_IOSTREAM
#include <sstream>
#endif
2013-01-31 04:39:14 +01:00
struct test_torrent_t
{
char const* file;
};
using namespace libtorrent;
test_torrent_t test_torrents[] =
{
{ "base.torrent" },
{ "empty_path.torrent" },
{ "parent_path.torrent" },
{ "hidden_parent_path.torrent" },
{ "single_multi_file.torrent" },
{ "slash_path.torrent" },
{ "backslash_path.torrent" },
{ "url_list.torrent" },
{ "url_list2.torrent" },
{ "url_list3.torrent" },
{ "httpseed.torrent" },
{ "empty_httpseed.torrent" },
2013-02-06 19:51:42 +01:00
{ "long_name.torrent" },
2013-02-24 11:02:26 +01:00
{ "whitespace_url.torrent" },
{ "duplicate_files.torrent" },
{ "pad_file.torrent" },
{ "creation_date.torrent" },
2013-02-26 06:57:29 +01:00
{ "no_creation_date.torrent" },
{ "url_seed.torrent" },
{ "url_seed_multi.torrent" },
{ "url_seed_multi_space.torrent" },
{ "url_seed_multi_space_nolist.torrent" },
{ "root_hash.torrent" },
};
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 },
{ "invalid_name.torrent", errors::torrent_missing_name },
{ "invalid_name2.torrent", errors::torrent_invalid_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_file_parse_failed },
{ "invalid_path_list.torrent", errors::torrent_file_parse_failed },
{ "missing_path_list.torrent", errors::torrent_file_parse_failed },
{ "invalid_pieces.torrent", errors::torrent_missing_pieces },
{ "unaligned_pieces.torrent", errors::torrent_invalid_hashes },
{ "invalid_root_hash.torrent", errors::torrent_invalid_hashes },
2013-03-05 01:32:57 +01:00
{ "invalid_root_hash2.torrent", errors::torrent_missing_pieces},
{ "invalid_file_size.torrent", errors::torrent_file_parse_failed },
2013-01-31 04:39:14 +01:00
};
// TODO: test remap_files
2013-02-24 11:02:26 +01:00
// 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)
// 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)
2013-01-31 04:39:14 +01:00
int test_main()
{
2013-06-17 17:56:58 +02:00
std::string root_dir = parent_path(current_working_directory());
2013-01-31 04:39:14 +01:00
for (int i = 0; i < sizeof(test_torrents)/sizeof(test_torrents[0]); ++i)
{
error_code ec;
fprintf(stderr, "loading %s\n", test_torrents[i].file);
2013-06-17 17:56:58 +02:00
boost::intrusive_ptr<torrent_info> ti(new torrent_info(combine_path(combine_path(root_dir, "test_torrents"), test_torrents[i].file), ec));
2013-01-31 04:39:14 +01:00
TEST_CHECK(!ec);
if (ec) fprintf(stderr, " -> failed %s\n", ec.message().c_str());
2013-02-24 11:02:26 +01:00
if (std::string(test_torrents[i].file) == "whitespace_url.torrent")
{
// make sure we trimmed the url
2013-06-17 09:47:47 +02:00
TEST_CHECK(ti->trackers().size() > 0);
if (ti->trackers().size() > 0)
TEST_CHECK(ti->trackers()[0].url == "udp://test.com/announce");
2013-02-24 11:02:26 +01:00
}
else if (std::string(test_torrents[i].file) == "duplicate_files.torrent")
{
// make sure we disambiguated the files
TEST_EQUAL(ti->num_files(), 2);
2013-05-12 00:00:54 +02:00
TEST_CHECK(ti->file_at(0).path == combine_path(combine_path("temp", "foo"), "bar.txt"));
TEST_CHECK(ti->file_at(1).path == combine_path(combine_path("temp", "foo"), "bar.1.txt"));
2013-02-24 11:02:26 +01:00
}
else if (std::string(test_torrents[i].file) == "pad_file.torrent")
{
TEST_EQUAL(ti->num_files(), 2);
TEST_CHECK(ti->file_at(0).pad_file == false);
TEST_CHECK(ti->file_at(1).pad_file == true);
}
else if (std::string(test_torrents[i].file) == "creation_date.torrent")
{
TEST_CHECK(*ti->creation_date() == 1234567);
}
2013-02-26 06:57:29 +01:00
else if (std::string(test_torrents[i].file) == "no_creation_date.torrent")
{
TEST_CHECK(!ti->creation_date());
}
else if (std::string(test_torrents[i].file) == "url_seed.torrent")
{
TEST_EQUAL(ti->web_seeds().size(), 1);
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/file");
#ifndef TORRENT_NO_DEPRECATE
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
}
else if (std::string(test_torrents[i].file) == "url_seed_multi.torrent")
{
TEST_EQUAL(ti->web_seeds().size(), 1);
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/file/");
#ifndef TORRENT_NO_DEPRECATE
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
}
else if (std::string(test_torrents[i].file) == "url_seed_multi_space.torrent"
|| std::string(test_torrents[i].file) == "url_seed_multi_space_nolist.torrent")
{
TEST_EQUAL(ti->web_seeds().size(), 1);
TEST_EQUAL(ti->web_seeds()[0].url, "http://test.com/test%20file/foo%20bar/");
#ifndef TORRENT_NO_DEPRECATE
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
}
2013-02-24 11:02:26 +01:00
2013-01-31 04:39:14 +01:00
int index = 0;
for (torrent_info::file_iterator i = ti->begin_files();
i != ti->end_files(); ++i, ++index)
{
int first = ti->map_file(index, 0, 0).piece;
int last = ti->map_file(index, (std::max)(size_type(i->size)-1, size_type(0)), 0).piece;
fprintf(stderr, " %11"PRId64" %c%c%c%c [ %4d, %4d ] %7u %s %s %s%s\n"
, i->size
, (i->pad_file?'p':'-')
, (i->executable_attribute?'x':'-')
, (i->hidden_attribute?'h':'-')
, (i->symlink_attribute?'l':'-')
, first, last
, boost::uint32_t(ti->files().mtime(*i))
, ti->files().hash(*i) != sha1_hash(0) ? to_hex(ti->files().hash(*i).to_string()).c_str() : ""
, ti->files().file_path(*i).c_str()
, i->symlink_attribute ? "-> ": ""
, i->symlink_attribute && i->symlink_index != -1 ? ti->files().symlink(*i).c_str() : "");
}
2013-02-24 11:02:26 +01:00
2013-02-26 06:57:29 +01:00
// test swap
#if !defined TORRENT_NO_DEPRECATE && TORRENT_USE_IOSTREAM
std::stringstream str1;
ti->print(str1);
torrent_info temp("temp", ec);
temp.swap(*ti);
std::stringstream str2;
temp.print(str2);
TEST_EQUAL(str1.str(), str2.str());
#endif
2013-01-31 04:39:14 +01:00
}
for (int i = 0; i < sizeof(test_error_torrents)/sizeof(test_error_torrents[0]); ++i)
{
error_code ec;
fprintf(stderr, "loading %s\n", test_error_torrents[i].file);
2013-06-17 17:56:58 +02:00
boost::intrusive_ptr<torrent_info> ti(new torrent_info(combine_path(combine_path(root_dir, "test_torrents"), test_error_torrents[i].file), ec));
fprintf(stderr, "E: %s\nexpected: %s\n", ec.message().c_str(), test_error_torrents[i].error.message().c_str());
TEST_EQUAL(ec, test_error_torrents[i].error);
}
2013-02-09 23:02:05 +01:00
return 0;
2013-01-31 04:39:14 +01:00
}