From 68cefe7d8951d61390afcf7f1cfd391305101d91 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 21 Mar 2012 16:27:50 +0000 Subject: [PATCH] fixed bug in magnet link parser, and improved unit test --- src/magnet_uri.cpp | 13 +++++---- test/test_primitives.cpp | 63 +++++++++++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index 8c418dc65..a3fb3abaa 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -157,17 +157,18 @@ namespace libtorrent // parse trackers out of the magnet link std::string::size_type pos = std::string::npos; - do + std::string url = url_has_argument(uri, "tr", &pos); + while (pos != std::string::npos) { + error_code e; + url = unescape_string(url, e); + if (e) continue; + p.trackers.push_back(url); pos = uri.find("&tr=", pos); if (pos == std::string::npos) break; pos += 4; - error_code e; - std::string url = unescape_string(uri.substr(pos, uri.find('&', pos) - pos), e); - if (e) continue; - p.trackers.push_back(url); + url = uri.substr(pos, uri.find('&', pos) - pos); } - while (pos != std::string::npos); std::string btih = url_has_argument(uri, "xt"); if (btih.empty()) diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 5ebfd57f7..3ec3cafc2 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -728,27 +728,15 @@ int test_main() p.save_path = "."; error_code ec; p.url = "magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" - "&tr=http://1&tr=http://2&tr=http://3&dn=foo&dht=127.0.0.1:43"; + "&tr=http://1" + "&tr=http://2" + "&tr=http://3" + "&dn=foo" + "&dht=127.0.0.1:43"; torrent_handle t = s->add_torrent(p, ec); TEST_CHECK(!ec); if (ec) fprintf(stderr, "%s\n", ec.message().c_str()); - p.url = "magnet:" - "?tr=http://1&tr=http://2&tr=http://3&dn=foo&dht=127.0.0.1:43" - "&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"; - torrent_handle t2 = s->add_torrent(p, ec); - TEST_CHECK(!ec); - if (ec) fprintf(stderr, "%s\n", ec.message().c_str()); - - p.url = "magnet:" - "?tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80" - "&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80" - "&tr=udp%3A%2F%2Ftracker.ccc.de%3A80" - "&xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07&dn=Ubuntu+11.04+%28Final%29"; - torrent_handle t3 = s->add_torrent(p, ec); - TEST_CHECK(!ec); - if (ec) fprintf(stderr, "%s\n", ec.message().c_str()); - std::vector trackers = t.trackers(); TEST_EQUAL(trackers.size(), 3); if (trackers.size() > 0) @@ -767,6 +755,47 @@ int test_main() fprintf(stderr, "3: %s\n", trackers[2].url.c_str()); } + p.url = "magnet:" + "?tr=http://1" + "&tr=http://2" + "&dn=foo" + "&dht=127.0.0.1:43" + "&xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"; + torrent_handle t2 = s->add_torrent(p, ec); + TEST_CHECK(!ec); + if (ec) fprintf(stderr, "%s\n", ec.message().c_str()); + + trackers = t2.trackers(); + TEST_EQUAL(trackers.size(), 2); + + p.url = "magnet:" + "?tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80" + "&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80" + "&tr=udp%3A%2F%2Ftracker.ccc.de%3A80" + "&xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07" + "&dn=Ubuntu+11.04+%28Final%29"; + torrent_handle t3 = s->add_torrent(p, ec); + TEST_CHECK(!ec); + if (ec) fprintf(stderr, "%s\n", ec.message().c_str()); + + trackers = t3.trackers(); + TEST_EQUAL(trackers.size(), 3); + if (trackers.size() > 0) + { + TEST_EQUAL(trackers[0].url, "udp://tracker.openbittorrent.com:80"); + fprintf(stderr, "1: %s\n", trackers[0].url.c_str()); + } + if (trackers.size() > 1) + { + TEST_EQUAL(trackers[1].url, "udp://tracker.publicbt.com:80"); + fprintf(stderr, "2: %s\n", trackers[1].url.c_str()); + } + if (trackers.size() > 2) + { + TEST_EQUAL(trackers[2].url, "udp://tracker.ccc.de:80"); + fprintf(stderr, "3: %s\n", trackers[2].url.c_str()); + } + TEST_EQUAL(to_hex(t.info_hash().to_string()), "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"); delete s;