fixed bug in magnet link parser, and improved unit test

This commit is contained in:
Arvid Norberg 2012-03-21 16:27:50 +00:00
parent cafbf2ca1d
commit 68cefe7d89
2 changed files with 53 additions and 23 deletions

View File

@ -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())

View File

@ -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<announce_entry> 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;