From 4338140c85b9701e2408d7d52b76355045c7397d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 27 May 2008 08:07:59 +0000 Subject: [PATCH] added test and fixed total_wanted issue for files > 2 GB --- src/torrent.cpp | 10 +++--- test/Jamfile | 1 + test/test_torrent.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 test/test_torrent.cpp diff --git a/src/torrent.cpp b/src/torrent.cpp index d02c99304..4fdf13734 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -531,7 +531,7 @@ namespace libtorrent // parse have bitmask entry const* pieces = m_resume_data.find_key("pieces"); if (pieces && pieces->type() == entry::string_t - && pieces->string().length() == m_torrent_file->num_pieces()) + && int(pieces->string().length()) == m_torrent_file->num_pieces()) { std::string const& pieces_str = pieces->string(); for (int i = 0, end(pieces_str.size()); i < end; ++i) @@ -3906,20 +3906,22 @@ namespace libtorrent st.total_wanted = m_torrent_file->total_size(); TORRENT_ASSERT(st.total_wanted >= 0); + TORRENT_ASSERT(st.total_wanted >= m_torrent_file->piece_length() + * (m_torrent_file->num_pieces() - 1)); if (m_picker.get() && (m_picker->num_filtered() > 0 || m_picker->num_have_filtered() > 0)) { - int filtered_pieces = m_picker->num_filtered() + int num_filtered_pieces = m_picker->num_filtered() + m_picker->num_have_filtered(); int last_piece_index = m_torrent_file->num_pieces() - 1; if (m_picker->piece_priority(last_piece_index) == 0) { st.total_wanted -= m_torrent_file->piece_size(last_piece_index); - --filtered_pieces; + --num_filtered_pieces; } - st.total_wanted -= filtered_pieces * m_torrent_file->piece_length(); + st.total_wanted -= size_type(num_filtered_pieces) * m_torrent_file->piece_length(); } TORRENT_ASSERT(st.total_wanted >= st.total_wanted_done); diff --git a/test/Jamfile b/test/Jamfile index 6cca533b8..28329b1ce 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -32,6 +32,7 @@ test-suite libtorrent : [ run test_http_connection.cpp ] [ run test_buffer.cpp ] [ run test_storage.cpp ] + [ run test_torrent.cpp ] [ run test_piece_picker.cpp ] # [ run test_entry.cpp ] [ run test_fast_extension.cpp ] diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp new file mode 100644 index 000000000..f1e1704aa --- /dev/null +++ b/test/test_torrent.cpp @@ -0,0 +1,78 @@ +#include "libtorrent/session.hpp" +#include "libtorrent/session_settings.hpp" +#include "libtorrent/hasher.hpp" +#include "libtorrent/create_torrent.hpp" +#include +#include + +#include "test.hpp" +#include "setup_transfer.hpp" + +int test_main() +{ + using namespace libtorrent; + + session ses(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48130, 48140)); + + libtorrent::create_torrent t; + size_type file_size = 1 * 1024 * 1024 * 1024; + t.add_file("test_torrent/tmp1", file_size); + t.add_file("test_torrent/tmp2", file_size); + t.add_file("test_torrent/tmp3", file_size); + t.set_piece_size(4 * 1024 * 1024); + t.add_tracker("http://non-existing.com/announce"); + + std::vector piece(4 * 1024 * 1024); + for (int i = 0; i < int(piece.size()); ++i) + piece[i] = (i % 26) + 'A'; + + // calculate the hash for all pieces + sha1_hash ph = hasher(&piece[0], piece.size()).final(); + int num = t.num_pieces(); + for (int i = 0; i < num; ++i) + t.set_hash(i, ph); + + std::vector tmp; + std::back_insert_iterator > out(tmp); + bencode(out, t.generate()); + boost::intrusive_ptr info(new torrent_info(&tmp[0], tmp.size())); + + add_torrent_params p; + p.ti = info; + p.save_path = "."; + torrent_handle h = ses.add_torrent(p); + + test_sleep(500); + torrent_status st = h.status(); + + std::cout << "total_wanted: " << st.total_wanted << " : " << file_size * 3 << std::endl; + TEST_CHECK(st.total_wanted == file_size * 3); + std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl; + TEST_CHECK(st.total_wanted_done == 0); + + std::vector prio(3, 1); + prio[0] = 0; + h.prioritize_files(prio); + + test_sleep(500); + st = h.status(); + + std::cout << "total_wanted: " << st.total_wanted << " : " << file_size * 2 << std::endl; + TEST_CHECK(st.total_wanted == file_size * 2); + std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl; + TEST_CHECK(st.total_wanted_done == 0); + + prio[1] = 0; + h.prioritize_files(prio); + + test_sleep(500); + st = h.status(); + + std::cout << "total_wanted: " << st.total_wanted << " : " << file_size << std::endl; + TEST_CHECK(st.total_wanted == file_size); + std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl; + TEST_CHECK(st.total_wanted_done == 0); + return 0; +} + +