more torrent parsing unit tests

This commit is contained in:
Arvid Norberg 2013-02-26 05:57:29 +00:00
parent c40a35e90b
commit ba6770ccd8
15 changed files with 99 additions and 13 deletions

View File

@ -238,8 +238,10 @@ namespace libtorrent
return url; return url;
char msg[TORRENT_MAX_PATH*4]; char msg[TORRENT_MAX_PATH*4];
snprintf(msg, sizeof(msg), "%s://%s%s%s:%d%s", protocol.c_str(), auth.c_str() snprintf(msg, sizeof(msg), "%s://%s%s%s%s%s%s", protocol.c_str(), auth.c_str()
, auth.empty()?"":"@", host.c_str(), port , auth.empty()?"":"@", host.c_str()
, port == -1 ? "" : ":"
, port == -1 ? "" : to_string(port).elems
, escape_path(path.c_str(), path.size()).c_str()); , escape_path(path.c_str(), path.size()).c_str());
return msg; return msg;
} }

View File

@ -121,6 +121,7 @@ void http_connection::get(std::string const& url, time_duration timeout, int pri
= parse_url_components(url, ec); = parse_url_components(url, ec);
int default_port = protocol == "https" ? 443 : 80; int default_port = protocol == "https" ? 443 : 80;
if (port == -1) port = default_port;
// keep ourselves alive even if the callback function // keep ourselves alive even if the callback function
// deletes this object // deletes this object

View File

@ -43,7 +43,7 @@ namespace libtorrent
std::string hostname; // hostname only std::string hostname; // hostname only
std::string auth; // user:pass std::string auth; // user:pass
std::string protocol; // http or https for instance std::string protocol; // http or https for instance
int port = 80; int port = -1;
std::string::iterator at; std::string::iterator at;
std::string::iterator colon; std::string::iterator colon;
@ -58,8 +58,6 @@ namespace libtorrent
= std::find(url.begin(), url.end(), ':'); = std::find(url.begin(), url.end(), ':');
protocol.assign(start, end); protocol.assign(start, end);
if (protocol == "https") port = 443;
if (end == url.end()) if (end == url.end())
{ {
ec = errors::unsupported_url_protocol; ec = errors::unsupported_url_protocol;

View File

@ -4554,6 +4554,10 @@ namespace libtorrent
error_code ec; error_code ec;
boost::tie(protocol, auth, hostname, port, path) boost::tie(protocol, auth, hostname, port, path)
= parse_url_components(web->url, ec); = parse_url_components(web->url, ec);
if (port == -1)
{
port = protocol == "http" ? 80 : 443;
}
if (ec) if (ec)
{ {
@ -4732,8 +4736,10 @@ namespace libtorrent
std::string hostname; std::string hostname;
int port; int port;
error_code ec; error_code ec;
boost::tie(ignore, ignore, hostname, port, ignore) std::string protocol;
boost::tie(protocol, ignore, hostname, port, ignore)
= parse_url_components(web->url, ec); = parse_url_components(web->url, ec);
if (port == -1) port = protocol == "http" ? 80 : 443;
if (ec) if (ec)
{ {

View File

@ -84,12 +84,14 @@ namespace libtorrent
void udp_tracker_connection::start() void udp_tracker_connection::start()
{ {
std::string hostname; std::string hostname;
std::string protocol;
int port; int port;
error_code ec; error_code ec;
using boost::tuples::ignore; using boost::tuples::ignore;
boost::tie(ignore, ignore, hostname, port, ignore) boost::tie(protocol, ignore, hostname, port, ignore)
= parse_url_components(tracker_req().url, ec); = parse_url_components(tracker_req().url, ec);
if (port == -1) port = protocol == "http" ? 80 : 443;
if (ec) if (ec)
{ {

View File

@ -475,6 +475,7 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
// we don't have this device in our list. Add it // we don't have this device in our list. Add it
boost::tie(protocol, auth, d.hostname, d.port, d.path) boost::tie(protocol, auth, d.hostname, d.port, d.path)
= parse_url_components(d.url, ec); = parse_url_components(d.url, ec);
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
if (ec) if (ec)
{ {
@ -933,6 +934,7 @@ void upnp::on_upnp_xml(error_code const& e
{ {
boost::tie(protocol, auth, d.hostname, d.port, d.path) boost::tie(protocol, auth, d.hostname, d.port, d.path)
= parse_url_components(d.url, ec); = parse_url_components(d.url, ec);
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
d.control_url = protocol + "://" + d.hostname + ":" d.control_url = protocol + "://" + d.hostname + ":"
+ to_string(d.port).elems + s.control_url; + to_string(d.port).elems + s.control_url;
} }
@ -946,6 +948,7 @@ void upnp::on_upnp_xml(error_code const& e
boost::tie(protocol, auth, d.hostname, d.port, d.path) boost::tie(protocol, auth, d.hostname, d.port, d.path)
= parse_url_components(d.control_url, ec); = parse_url_components(d.control_url, ec);
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
if (ec) if (ec)
{ {

View File

@ -87,8 +87,15 @@ namespace libtorrent
= parse_url_components(url, ec); = parse_url_components(url, ec);
TORRENT_ASSERT(!ec); TORRENT_ASSERT(!ec);
if (m_port == -1 && protocol == "http")
m_port = 80;
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
if (protocol == "https") m_ssl = true; if (protocol == "https")
{
m_ssl = true;
if (m_port == -1) m_port = 443;
}
#endif #endif
if (!m_basic_auth.empty()) if (!m_basic_auth.empty())

View File

@ -69,7 +69,12 @@ EXTRA_DIST = Jamfile \
test_torrents/missing_path_list.torrent \ test_torrents/missing_path_list.torrent \
test_torrents/invalid_pieces.torrent \ test_torrents/invalid_pieces.torrent \
test_torrents/unaligned_pieces.torrent \ test_torrents/unaligned_pieces.torrent \
test_torrents/creation_date.torrent test_torrents/creation_date.torrent \
test_torrents/no_creation_date.torrent \
test_torrents/url_seed.torrent \
test_torrents/url_seed_multi.torrent \
test_torrents/url_seed_multi_space.torrent \
test_torrents/url_seed_multi_space_nolist.torrent
EXTRA_PROGRAMS = $(test_programs) EXTRA_PROGRAMS = $(test_programs)

View File

@ -1045,9 +1045,10 @@ int test_main()
TEST_CHECK(strcmp(msg, "too long ") == 0); TEST_CHECK(strcmp(msg, "too long ") == 0);
// test maybe_url_encode // test maybe_url_encode
TEST_EQUAL(maybe_url_encode("http://test:test@abc.com/abc<>abc"), "http://test:test@abc.com/abc%3c%3eabc");
TEST_EQUAL(maybe_url_encode("http://test:test@abc.com/abc<>abc"), "http://test:test@abc.com:80/abc%3c%3eabc"); TEST_EQUAL(maybe_url_encode("http://abc.com/foo bar"), "http://abc.com/foo%20bar");
TEST_EQUAL(maybe_url_encode("http://abc.com/foo bar"), "http://abc.com:80/foo%20bar"); TEST_EQUAL(maybe_url_encode("http://abc.com:80/foo bar"), "http://abc.com:80/foo%20bar");
TEST_EQUAL(maybe_url_encode("http://abc.com:8080/foo bar"), "http://abc.com:8080/foo%20bar");
TEST_EQUAL(maybe_url_encode("abc"), "abc"); TEST_EQUAL(maybe_url_encode("abc"), "abc");
TEST_EQUAL(maybe_url_encode("http://abc.com/abc"), "http://abc.com/abc"); TEST_EQUAL(maybe_url_encode("http://abc.com/abc"), "http://abc.com/abc");

View File

@ -34,6 +34,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/file.hpp" #include "libtorrent/file.hpp"
#include "libtorrent/torrent_info.hpp" #include "libtorrent/torrent_info.hpp"
#if TORRENT_USE_IOSTREAM
#include <sstream>
#endif
struct test_torrent_t struct test_torrent_t
{ {
char const* file; char const* file;
@ -60,6 +64,11 @@ test_torrent_t test_torrents[] =
{ "duplicate_files.torrent" }, { "duplicate_files.torrent" },
{ "pad_file.torrent" }, { "pad_file.torrent" },
{ "creation_date.torrent" }, { "creation_date.torrent" },
{ "no_creation_date.torrent" },
{ "url_seed.torrent" },
{ "url_seed_multi.torrent" },
{ "url_seed_multi_space.torrent" },
{ "url_seed_multi_space_nolist.torrent" },
}; };
struct test_failing_torrent_t struct test_failing_torrent_t
@ -96,7 +105,6 @@ test_failing_torrent_t test_error_torrents[] =
// TODO: torrent with 'l' (symlink) attribute // TODO: torrent with 'l' (symlink) attribute
// TODO: creating a merkle torrent (torrent_info::build_merkle_list) // 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) // 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)
// TODO: torrent with web seed. make sure we append '/' for multifile torrents
int test_main() int test_main()
{ {
@ -130,6 +138,41 @@ int test_main()
{ {
TEST_CHECK(*ti->creation_date() == 1234567); TEST_CHECK(*ti->creation_date() == 1234567);
} }
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
}
int index = 0; int index = 0;
for (torrent_info::file_iterator i = ti->begin_files(); for (torrent_info::file_iterator i = ti->begin_files();
@ -151,6 +194,19 @@ int test_main()
, i->symlink_attribute && i->symlink_index != -1 ? ti->files().symlink(*i).c_str() : ""); , i->symlink_attribute && i->symlink_index != -1 ? ti->files().symlink(*i).c_str() : "");
} }
// 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
} }
for (int i = 0; i < sizeof(test_error_torrents)/sizeof(test_error_torrents[0]); ++i) for (int i = 0; i < sizeof(test_error_torrents)/sizeof(test_error_torrents[0]); ++i)

View File

@ -0,0 +1 @@
d10:created by10:libtorrent4:infod5:filesld6:lengthi425e4:pathl3:foo7:bar.txteed6:lengthi425e4:pathl3:foo7:var.txteee4:name4:temp12:piece lengthi16384e6:pieces20:01234567890123456789ee

View File

@ -0,0 +1 @@
d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW}ÜA4u,·¼‡e8:url-listl20:http://test.com/fileee

View File

@ -0,0 +1 @@
d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl3:foo7:bar.txteed6:lengthi425e4:pathl3:foo7:var.txteee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW}ÜA4u,·¼‡e8:url-listl20:http://test.com/fileee

View File

@ -0,0 +1 @@
d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl3:foo7:bar.txteed6:lengthi425e4:pathl3:foo7:var.txteee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW}ÜA4u,·¼‡e8:url-listl33:http://test.com/test file/foo baree

View File

@ -0,0 +1 @@
d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl3:foo7:bar.txteed6:lengthi425e4:pathl3:foo7:var.txteee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW}ÜA4u,·¼‡e8:url-listl33:http://test.com/test file/foo baree