From ba6770ccd84a9c902b7826137b079c2b1094d4d4 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 26 Feb 2013 05:57:29 +0000 Subject: [PATCH] more torrent parsing unit tests --- src/escape_string.cpp | 6 +- src/http_connection.cpp | 1 + src/parse_url.cpp | 4 +- src/torrent.cpp | 8 ++- src/udp_tracker_connection.cpp | 4 +- src/upnp.cpp | 3 + src/web_connection_base.cpp | 9 ++- test/Makefile.am | 7 ++- test/test_primitives.cpp | 7 ++- test/test_torrent_parse.cpp | 58 ++++++++++++++++++- test/test_torrents/no_creation_date.torrent | 1 + test/test_torrents/url_seed.torrent | 1 + test/test_torrents/url_seed_multi.torrent | 1 + .../url_seed_multi_space.torrent | 1 + .../url_seed_multi_space_nolist.torrent | 1 + 15 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 test/test_torrents/no_creation_date.torrent create mode 100644 test/test_torrents/url_seed.torrent create mode 100644 test/test_torrents/url_seed_multi.torrent create mode 100644 test/test_torrents/url_seed_multi_space.torrent create mode 100644 test/test_torrents/url_seed_multi_space_nolist.torrent diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 236f11c63..8b09403c8 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -238,8 +238,10 @@ namespace libtorrent return url; char msg[TORRENT_MAX_PATH*4]; - snprintf(msg, sizeof(msg), "%s://%s%s%s:%d%s", protocol.c_str(), auth.c_str() - , auth.empty()?"":"@", host.c_str(), port + snprintf(msg, sizeof(msg), "%s://%s%s%s%s%s%s", protocol.c_str(), auth.c_str() + , auth.empty()?"":"@", host.c_str() + , port == -1 ? "" : ":" + , port == -1 ? "" : to_string(port).elems , escape_path(path.c_str(), path.size()).c_str()); return msg; } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index f68f1b22a..d420074ac 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -121,6 +121,7 @@ void http_connection::get(std::string const& url, time_duration timeout, int pri = parse_url_components(url, ec); int default_port = protocol == "https" ? 443 : 80; + if (port == -1) port = default_port; // keep ourselves alive even if the callback function // deletes this object diff --git a/src/parse_url.cpp b/src/parse_url.cpp index 09980ac6c..d0db46b92 100644 --- a/src/parse_url.cpp +++ b/src/parse_url.cpp @@ -43,7 +43,7 @@ namespace libtorrent std::string hostname; // hostname only std::string auth; // user:pass std::string protocol; // http or https for instance - int port = 80; + int port = -1; std::string::iterator at; std::string::iterator colon; @@ -58,8 +58,6 @@ namespace libtorrent = std::find(url.begin(), url.end(), ':'); protocol.assign(start, end); - if (protocol == "https") port = 443; - if (end == url.end()) { ec = errors::unsupported_url_protocol; diff --git a/src/torrent.cpp b/src/torrent.cpp index 64091d418..7bebd15d6 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -4554,6 +4554,10 @@ namespace libtorrent error_code ec; boost::tie(protocol, auth, hostname, port, path) = parse_url_components(web->url, ec); + if (port == -1) + { + port = protocol == "http" ? 80 : 443; + } if (ec) { @@ -4732,8 +4736,10 @@ namespace libtorrent std::string hostname; int port; 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); + if (port == -1) port = protocol == "http" ? 80 : 443; if (ec) { diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 36364963c..aee2c44f0 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -84,12 +84,14 @@ namespace libtorrent void udp_tracker_connection::start() { std::string hostname; + std::string protocol; int port; error_code ec; 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); + if (port == -1) port = protocol == "http" ? 80 : 443; if (ec) { diff --git a/src/upnp.cpp b/src/upnp.cpp index 241557309..678e0ed08 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -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 boost::tie(protocol, auth, d.hostname, d.port, d.path) = parse_url_components(d.url, ec); + if (d.port == -1) d.port = protocol == "http" ? 80 : 443; 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) = parse_url_components(d.url, ec); + if (d.port == -1) d.port = protocol == "http" ? 80 : 443; d.control_url = protocol + "://" + d.hostname + ":" + 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) = parse_url_components(d.control_url, ec); + if (d.port == -1) d.port = protocol == "http" ? 80 : 443; if (ec) { diff --git a/src/web_connection_base.cpp b/src/web_connection_base.cpp index 69b22a2dd..a554034b6 100644 --- a/src/web_connection_base.cpp +++ b/src/web_connection_base.cpp @@ -87,8 +87,15 @@ namespace libtorrent = parse_url_components(url, ec); TORRENT_ASSERT(!ec); + if (m_port == -1 && protocol == "http") + m_port = 80; + #ifdef TORRENT_USE_OPENSSL - if (protocol == "https") m_ssl = true; + if (protocol == "https") + { + m_ssl = true; + if (m_port == -1) m_port = 443; + } #endif if (!m_basic_auth.empty()) diff --git a/test/Makefile.am b/test/Makefile.am index 9e52ceaa8..65adddb2b 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -69,7 +69,12 @@ EXTRA_DIST = Jamfile \ test_torrents/missing_path_list.torrent \ test_torrents/invalid_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) diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index c314ffc72..bcc2aa1bd 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -1045,9 +1045,10 @@ int test_main() TEST_CHECK(strcmp(msg, "too long ") == 0); // test maybe_url_encode - - 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:80/foo%20bar"); + 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://abc.com/foo bar"), "http://abc.com/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("http://abc.com/abc"), "http://abc.com/abc"); diff --git a/test/test_torrent_parse.cpp b/test/test_torrent_parse.cpp index 5f45e4d62..39695983f 100644 --- a/test/test_torrent_parse.cpp +++ b/test/test_torrent_parse.cpp @@ -34,6 +34,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/torrent_info.hpp" +#if TORRENT_USE_IOSTREAM +#include +#endif + struct test_torrent_t { char const* file; @@ -60,6 +64,11 @@ test_torrent_t test_torrents[] = { "duplicate_files.torrent" }, { "pad_file.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 @@ -96,7 +105,6 @@ test_failing_torrent_t test_error_torrents[] = // 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) -// TODO: torrent with web seed. make sure we append '/' for multifile torrents int test_main() { @@ -130,6 +138,41 @@ int test_main() { 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; 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() : ""); } + // 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) diff --git a/test/test_torrents/no_creation_date.torrent b/test/test_torrents/no_creation_date.torrent new file mode 100644 index 000000000..21de7b337 --- /dev/null +++ b/test/test_torrents/no_creation_date.torrent @@ -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 diff --git a/test/test_torrents/url_seed.torrent b/test/test_torrents/url_seed.torrent new file mode 100644 index 000000000..66d02dc4c --- /dev/null +++ b/test/test_torrents/url_seed.torrent @@ -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 diff --git a/test/test_torrents/url_seed_multi.torrent b/test/test_torrents/url_seed_multi.torrent new file mode 100644 index 000000000..4a5e4db4c --- /dev/null +++ b/test/test_torrents/url_seed_multi.torrent @@ -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 diff --git a/test/test_torrents/url_seed_multi_space.torrent b/test/test_torrents/url_seed_multi_space.torrent new file mode 100644 index 000000000..fca95364c --- /dev/null +++ b/test/test_torrents/url_seed_multi_space.torrent @@ -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 diff --git a/test/test_torrents/url_seed_multi_space_nolist.torrent b/test/test_torrents/url_seed_multi_space_nolist.torrent new file mode 100644 index 000000000..fca95364c --- /dev/null +++ b/test/test_torrents/url_seed_multi_space_nolist.torrent @@ -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