From 3fc73cb21999cfad13d5095d9a29568e68e6b4a7 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 20 Feb 2007 01:42:12 +0000 Subject: [PATCH] added a web seed test (requires manual environment setup for web server). fixed bug in torrent_info where file offsets could be left uninitialized. Fixed bug in torrent which would cause a crash in case a torrent without any trackers would be aborted. --- src/session_impl.cpp | 7 +++- src/torrent.cpp | 2 + src/torrent_info.cpp | 2 + test/Jamfile | 1 + test/test_web_seed.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/test_web_seed.cpp diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 3e90169ed..50b4383a0 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1072,7 +1072,12 @@ namespace libtorrent { namespace detail m_torrents.begin(); i != m_torrents.end(); ++i) { i->second->abort(); - if (!i->second->is_paused() || i->second->should_request()) + // generate a tracker request in case the torrent is not paused + // (in which case it's not currently announced with the tracker) + // or if the torrent itself thinks we should request. Do not build + // a request in case the torrent doesn't have any trackers + if ((!i->second->is_paused() || i->second->should_request()) + && !i->second->torrent_file().trackers().empty()) { tracker_request req = i->second->generate_tracker_request(); req.listen_port = m_listen_interface.port(); diff --git a/src/torrent.cpp b/src/torrent.cpp index 3ce495c20..eab84cd26 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1106,6 +1106,8 @@ namespace libtorrent { INVARIANT_CHECK; + assert(!m_trackers.empty()); + m_next_request = second_clock::universal_time() + boost::posix_time::seconds(tracker_retry_delay_max); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index d8601a536..238864283 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -540,6 +540,8 @@ namespace libtorrent file_entry e; e.path = file; e.size = size; + e.offset = m_files.empty() ? 0 : m_files.back().offset + + m_files.back().size; m_files.push_back(e); m_total_size += size; diff --git a/test/Jamfile b/test/Jamfile index 30f05c9de..293e70b33 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -18,5 +18,6 @@ test-suite libtorrent : [ run test_hasher.cpp ] [ run test_metadata_extension.cpp ] [ run test_allocate_resources.cpp ] + [ run test_web_seed.cpp ] ; diff --git a/test/test_web_seed.cpp b/test/test_web_seed.cpp new file mode 100644 index 000000000..2bcb7161c --- /dev/null +++ b/test/test_web_seed.cpp @@ -0,0 +1,91 @@ +#include "libtorrent/session.hpp" +#include "libtorrent/hasher.hpp" +#include "libtorrent/file_pool.hpp" +#include "libtorrent/storage.hpp" +#include +#include +#include + +#include "test.hpp" +#include "setup_transfer.hpp" + +using namespace boost::filesystem; +using namespace libtorrent; + +void add_files( + torrent_info& t + , path const& p + , path const& l) +{ + if (l.leaf()[0] == '.') return; + path f(p / l); + if (is_directory(f)) + { + for (directory_iterator i(f), end; i != end; ++i) + add_files(t, p, l / i->leaf()); + } + else + { + std::cerr << "adding \"" << l.string() << "\"\n"; + t.add_file(l, file_size(f)); + } +} + +void test_transfer() +{ + using namespace libtorrent; + + torrent_info torrent_file; + torrent_file.add_url_seed("http://127.0.0.1/bravia_paint_ad_70sec_1280x720.mov"); + + path full_path = "/Library/WebServer/Documents/bravia_paint_ad_70sec_1280x720.mov"; + add_files(torrent_file, full_path.branch_path(), full_path.leaf()); + + file_pool fp; + storage st(torrent_file, full_path.branch_path(), fp); + // calculate the hash for all pieces + int num = torrent_file.num_pieces(); + std::vector buf(torrent_file.piece_length()); + for (int i = 0; i < num; ++i) + { + st.read(&buf[0], i, 0, torrent_file.piece_size(i)); + hasher h(&buf[0], torrent_file.piece_size(i)); + torrent_file.set_hash(i, h.final()); + } + + // to calculate the info_hash + torrent_file.create_torrent(); + + session ses; + remove_all("./tmp1"); + torrent_handle th = ses.add_torrent(torrent_file, "./tmp1"); + + for (int i = 0; i < 50; ++i) + { + torrent_status s = th.status(); + std::cerr << s.progress << "\r"; + std::auto_ptr a; + a = ses.pop_alert(); + if (a.get()) + std::cerr << a->msg() << "\n"; + + if (th.is_seed()) break; + sleep(100); + } + + TEST_CHECK(th.is_seed()); +} + +int test_main() +{ + using namespace libtorrent; + using namespace boost::filesystem; + + test_transfer(); + + remove_all("./tmp1"); + remove_all("./tmp2"); + + return 0; +} +