diff --git a/ChangeLog b/ChangeLog index a0e46a113..cfec0290b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,7 @@ * fix uTP edge case where udp socket buffer fills up * fix nagle implementation in uTP + * make torrent file parser reject invalid path elements earlier * fixed piece picker bug when using pad-files * fix read-piece response for cancelled deadline-pieces * fixed file priority vector-overrun diff --git a/Makefile.am b/Makefile.am index 98eec0bed..eb7ca623e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -114,6 +114,18 @@ EXTRA_DIST = \ libtorrent-rasterbar-cmake.pc \ parse_dht_log.py \ parse_session_stats.py \ + test/test_torrents/url_list2.torrent \ + test/test_torrents/httpseed.torrent \ + test/test_torrents/empty_httpseed.torrent \ + test/test_torrents/hidden_parent_path.torrent \ + test/test_torrents/url_list3.torrent \ + test/test_torrents/parent_path.torrent \ + test/test_torrents/slash_path.torrent \ + test/test_torrents/backslash_path.torrent \ + test/test_torrents/base.torrent \ + test/test_torrents/empty_path.torrent \ + test/test_torrents/single_multi_file.torrent \ + test/test_torrents/url_list.torrent \ $(DOCS_PAGES) \ $(DOCS_IMAGES) diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index e40a01fd9..55dae34e6 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -281,6 +281,7 @@ namespace libtorrent if (p->list_at(i)->type() != lazy_entry::string_t) return false; std::string path_element = p->list_at(i)->string_value(); + if (!valid_path_element(path_element)) continue; if (i == end - 1) *filename = p->list_at(i); trim_path_element(path_element); path = combine_path(path, path_element); diff --git a/test/Jamfile b/test/Jamfile index ec22c03da..983d54cda 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -53,6 +53,7 @@ test-suite libtorrent : [ run test_hasher.cpp ] [ run test_dht.cpp ] [ run test_storage.cpp ] + [ run test_torrent_parse.cpp ] [ run test_upnp.cpp ] [ run test_tracker.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index d5a3d7b32..0b85a6ed9 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -24,6 +24,7 @@ test_programs = \ test_swarm \ test_threads \ test_torrent \ + test_torrent_parse \ test_trackers_extension \ test_transfer \ test_upnp \ diff --git a/test/test_torrent_parse.cpp b/test/test_torrent_parse.cpp new file mode 100644 index 000000000..653e3401a --- /dev/null +++ b/test/test_torrent_parse.cpp @@ -0,0 +1,93 @@ +/* + +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" + +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" }, + { "slash_path.torrent" }, + { "url_list.torrent" }, + { "url_list2.torrent" }, + { "url_list3.torrent" }, + { "httpseed.torrent" }, + { "empty_httpseed.torrent" }, +// { "" }, +}; + +int test_main() +{ + 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); + boost::intrusive_ptr ti(new torrent_info(combine_path("test_torrents", test_torrents[i].file), ec)); + TEST_CHECK(!ec); + if (ec) fprintf(stderr, " -> failed %s\n", ec.message().c_str()); + + 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() : ""); + } + } +} + diff --git a/test/test_torrents/backslash_path.torrent b/test/test_torrents/backslash_path.torrent new file mode 100644 index 000000000..c3d61a588 --- /dev/null +++ b/test/test_torrents/backslash_path.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl1:\1:\3:bareee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/base.torrent b/test/test_torrents/base.torrent new file mode 100644 index 000000000..d2c26dbad --- /dev/null +++ b/test/test_torrents/base.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee \ No newline at end of file diff --git a/test/test_torrents/empty_httpseed.torrent b/test/test_torrents/empty_httpseed.torrent new file mode 100644 index 000000000..5dea41455 --- /dev/null +++ b/test/test_torrents/empty_httpseed.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e9:httpseedsl0:e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡e8:url-listl0:ee diff --git a/test/test_torrents/empty_path.torrent b/test/test_torrents/empty_path.torrent new file mode 100644 index 000000000..05fb6aa70 --- /dev/null +++ b/test/test_torrents/empty_path.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl0:0:eee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/hidden_parent_path.torrent b/test/test_torrents/hidden_parent_path.torrent new file mode 100644 index 000000000..6feeb912d --- /dev/null +++ b/test/test_torrents/hidden_parent_path.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl16:foo/../../../bar3:bareee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/httpseed.torrent b/test/test_torrents/httpseed.torrent new file mode 100644 index 000000000..2b5ea989d --- /dev/null +++ b/test/test_torrents/httpseed.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e9:httpseedsl18:http://foobar.com/e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡e8:url-listl0:ee diff --git a/test/test_torrents/parent_path.torrent b/test/test_torrents/parent_path.torrent new file mode 100644 index 000000000..d36c30b72 --- /dev/null +++ b/test/test_torrents/parent_path.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl2:..2:..3:bareee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/single_multi_file.torrent b/test/test_torrents/single_multi_file.torrent new file mode 100644 index 000000000..a9f30115a --- /dev/null +++ b/test/test_torrents/single_multi_file.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl3:foo3:bareee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/slash_path.torrent b/test/test_torrents/slash_path.torrent new file mode 100644 index 000000000..9b8997242 --- /dev/null +++ b/test/test_torrents/slash_path.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod5:filesld6:lengthi425e4:pathl1:/1:/3:bareee4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡ee diff --git a/test/test_torrents/url_list.torrent b/test/test_torrents/url_list.torrent new file mode 100644 index 000000000..4d0b9caf4 --- /dev/null +++ b/test/test_torrents/url_list.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡e8:url-listl0:ee diff --git a/test/test_torrents/url_list2.torrent b/test/test_torrents/url_list2.torrent new file mode 100644 index 000000000..642762403 --- /dev/null +++ b/test/test_torrents/url_list2.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡e8:url-listll0:eee diff --git a/test/test_torrents/url_list3.torrent b/test/test_torrents/url_list3.torrent new file mode 100644 index 000000000..130e55931 --- /dev/null +++ b/test/test_torrents/url_list3.torrent @@ -0,0 +1 @@ +d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi425e4:name4:temp12:piece lengthi16384e6:pieces20:‚ž¼Œ&¾ÇJW›}ÜA4u,·¼‘‡e8:url-listli-20eee