diff --git a/test/Jamfile b/test/Jamfile index 774954072..dbbdbdeb7 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -144,6 +144,7 @@ test-suite libtorrent : [ run test_bdecode_performance.cpp ] [ run test_pe_crypto.cpp ] + [ run test_remap_files.cpp ] [ run test_utp.cpp ] [ run test_auto_unchoke.cpp ] [ run test_http_connection.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index dece3e038..88f3890be 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -43,6 +43,7 @@ test_programs = \ test_session \ test_ssl \ test_web_seed + test_remap_files if ENABLE_TESTS check_PROGRAMS = $(test_programs) @@ -166,6 +167,7 @@ test_utp_SOURCES = test_utp.cpp test_session_SOURCES = test_session.cpp test_ssl_SOURCES = test_ssl.cpp test_web_seed_SOURCES = test_web_seed.cpp +test_remap_files_SOURCES = test_remap_files.cpp LDADD = $(top_builddir)/src/libtorrent-rasterbar.la libtest.la diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index c5849758b..e19ed1886 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -279,13 +279,16 @@ void print_ses_rate(float time , libtorrent::torrent_status const* st2 , libtorrent::torrent_status const* st3) { - fprintf(stderr, "%3.1fs | %dkB/s %dkB/s %d%% %d cc:%d%s", time - , int(st1->download_payload_rate / 1000) - , int(st1->upload_payload_rate / 1000) - , int(st1->progress * 100) - , st1->num_peers - , st1->connect_candidates - , st1->error.empty() ? "" : (" [" + st1->error + "]").c_str()); + if (st1) + { + fprintf(stderr, "%3.1fs | %dkB/s %dkB/s %d%% %d cc:%d%s", time + , int(st1->download_payload_rate / 1000) + , int(st1->upload_payload_rate / 1000) + , int(st1->progress * 100) + , st1->num_peers + , st1->connect_candidates + , st1->error.empty() ? "" : (" [" + st1->error + "]").c_str()); + } if (st2) fprintf(stderr, " : %3.1fs | %dkB/s %dkB/s %d%% %d cc:%d%s", time , int(st2->download_payload_rate / 1000) @@ -589,7 +592,8 @@ setup_transfer(session* ses1, session* ses2, session* ses3 , bool clear_files, bool use_metadata_transfer, bool connect_peers , std::string suffix, int piece_size , boost::intrusive_ptr* torrent, bool super_seeding - , add_torrent_params const* p, bool stop_lsd, bool use_ssl_ports) + , add_torrent_params const* p, bool stop_lsd, bool use_ssl_ports + , boost::intrusive_ptr* torrent2) { assert(ses1); assert(ses2); @@ -689,6 +693,10 @@ setup_transfer(session* ses1, session* ses2, session* ses3 param.ti = 0; param.info_hash = t->info_hash(); } + else if (torrent2) + { + param.ti = clone_ptr(*torrent2); + } else { param.ti = clone_ptr(t); diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index d2bea3a71..97a1b18dd 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -83,7 +83,8 @@ EXPORT setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2 , libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true , bool connect = true, std::string suffix = "", int piece_size = 16 * 1024 , boost::intrusive_ptr* torrent = 0, bool super_seeding = false - , libtorrent::add_torrent_params const* p = 0, bool stop_lsd = true, bool use_ssl_ports = false); + , libtorrent::add_torrent_params const* p = 0, bool stop_lsd = true, bool use_ssl_ports = false + , boost::intrusive_ptr* torrent2 = 0); int EXPORT start_web_server(bool ssl = false, bool chunked = false); void EXPORT stop_web_server(); diff --git a/test/test_remap_files.cpp b/test/test_remap_files.cpp new file mode 100644 index 000000000..03aec2088 --- /dev/null +++ b/test/test_remap_files.cpp @@ -0,0 +1,349 @@ +/* + +Copyright (c) 2014, 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 "libtorrent/session.hpp" +#include "libtorrent/alert_types.hpp" +#include "libtorrent/create_torrent.hpp" +#include "setup_transfer.hpp" +#include "test.hpp" + +#include +#include + +using namespace libtorrent; +using boost::tuples::ignore; + +template +boost::intrusive_ptr clone_ptr(boost::intrusive_ptr const& ptr) +{ + return boost::intrusive_ptr(new T(*ptr)); +} + +int peer_disconnects = 0; + +bool on_alert(alert* a) +{ + if (alert_cast(a)) + ++peer_disconnects; + + return false; +} + +void test_remap_files_gather(storage_mode_t storage_mode = storage_mode_sparse) +{ + // in case the previous run was terminated + error_code ec; + + int const alert_mask = alert::all_categories + & ~alert::progress_notification + & ~alert::stats_notification; + + session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48075, 49000), "0.0.0.0", 0, alert_mask); + session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49075, 50000), "0.0.0.0", 0, alert_mask); + + torrent_handle tor1; + torrent_handle tor2; + + create_directory("tmp1_remap", ec); + create_directory("tmp1_remap/test_torrent_dir", ec); + if (ec) + { + fprintf(stderr, "error creating directory: %s\n" + , ec.message().c_str()); + TEST_CHECK(false); + return; + } + + static const int file_sizes[] = + { 50, 16000-50, 16000, 1700, 100, 8000, 8000, 1,1,10,10,10,1000,10,10,10,10,1000,10,10,10,1,1,1 + ,10,1000,1000,1000,10,1000,130,65000,340,750,20,300,400,5000,23000,900,43000,4000,43000,60, 40}; + + create_random_files("tmp1_remap/test_torrent_dir", file_sizes + , sizeof(file_sizes)/sizeof(file_sizes[0])); + file_storage fs; + add_files(fs, "tmp1_remap/test_torrent_dir"); + + // generate a torrent with pad files to make sure they + // are not requested web seeds + libtorrent::create_torrent ct(fs, 0x8000, 0x4000); + set_piece_hashes(ct, "tmp1_remap", ec); + if (ec) + { + fprintf(stderr, "error creating hashes for test torrent: %s\n" + , ec.message().c_str()); + TEST_CHECK(false); + return; + } + std::vector buf; + bencode(std::back_inserter(buf), ct.generate()); + boost::intrusive_ptr t(new torrent_info(&buf[0], buf.size(), ec)); + boost::intrusive_ptr t2(new torrent_info(&buf[0], buf.size(), ec)); + + // remap the files to a single one + file_storage st; + st.add_file("single_file", t->total_size()); + t2->remap_files(st); + + add_torrent_params params; + params.storage_mode = storage_mode; + params.flags &= ~add_torrent_params::flag_paused; + params.flags &= ~add_torrent_params::flag_auto_managed; + + wait_for_listen(ses1, "ses1"); + wait_for_listen(ses2, "ses2"); + + peer_disconnects = 0; + + // test using piece sizes smaller than 16kB + boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 + , true, false, true, "_remap", 8 * 1024, &t, false, ¶ms + , true, false, &t2); + + fprintf(stderr, "\ntesting remap gather\n\n"); + + for (int i = 0; i < 50; ++i) + { + print_alerts(ses1, "ses1", true, true, true, on_alert); + print_alerts(ses2, "ses2", true, true, true, on_alert); + + torrent_status st1 = tor1.status(); + torrent_status st2 = tor2.status(); + + if (i % 10 == 0) + { + print_ses_rate(i / 10.f, &st1, &st2); + } + + if (st2.is_finished) break; + + if (st2.state != torrent_status::downloading) + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating", "checking (r)"}; + std::cerr << "st2 state: " << state_str[st2.state] << std::endl; + } + + TEST_CHECK(st1.state == torrent_status::seeding + || st1.state == torrent_status::checking_files); + TEST_CHECK(st2.state == torrent_status::downloading + || st2.state == torrent_status::checking_resume_data); + + if (peer_disconnects >= 2) break; + + test_sleep(100); + } + + torrent_status st2 = tor2.status(); + TEST_CHECK(st2.is_seeding); + + if (!st2.is_seeding) return; + + fprintf(stderr, "\ntesting force recheck\n\n"); + + // test force rechecking a seeding torrent with remapped files + tor2.force_recheck(); + + for (int i = 0; i < 50; ++i) + { + print_alerts(ses2, "ses2", true, true, true, on_alert); + + torrent_status st2 = tor2.status(); + + if (i % 10 == 0) + { + print_ses_rate(i / 10.f, NULL, &st2); + } + + if (st2.state != torrent_status::checking_files) + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating", "checking (r)"}; + std::cerr << "st2 state: " << state_str[st2.state] << std::endl; + } + + if (st2.progress == 1.0) break; + + test_sleep(100); + } + + st2 = tor2.status(); + TEST_CHECK(st2.is_seeding); +} + +void test_remap_files_scatter(storage_mode_t storage_mode = storage_mode_sparse) +{ + // in case the previous run was terminated + error_code ec; + + int const alert_mask = alert::all_categories + & ~alert::progress_notification + & ~alert::stats_notification; + + session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48075, 49000), "0.0.0.0", 0, alert_mask); + session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49075, 50000), "0.0.0.0", 0, alert_mask); + + torrent_handle tor1; + torrent_handle tor2; + + create_directory("tmp1_remap2", ec); + std::ofstream file("tmp1_remap2/temporary"); + boost::intrusive_ptr t = ::create_torrent(&file, 32 * 1024, 7); + file.close(); + + file_storage fs; + for (int i = 0; i < 9; ++i) + { + char name[100]; + snprintf(name, sizeof(name), "multifile/file%d.txt", i); + fs.add_file(name, t->total_size() / 10); + } + char name[100]; + snprintf(name, sizeof(name), "multifile/file%d.txt", 9); + fs.add_file(name, t->total_size() - fs.total_size()); + + boost::intrusive_ptr t2 = clone_ptr(t); + + t2->remap_files(fs); + + add_torrent_params params; + params.storage_mode = storage_mode; + params.flags &= ~add_torrent_params::flag_paused; + params.flags &= ~add_torrent_params::flag_auto_managed; + + wait_for_listen(ses1, "ses1"); + wait_for_listen(ses2, "ses2"); + + peer_disconnects = 0; + + // test using piece sizes smaller than 16kB + boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0 + , true, false, true, "_remap2", 8 * 1024, &t, false, ¶ms + , true, false, &t2); + + fprintf(stderr, "\ntesting remap scatter\n\n"); + + for (int i = 0; i < 50; ++i) + { + print_alerts(ses1, "ses1", true, true, true, on_alert); + print_alerts(ses2, "ses2", true, true, true, on_alert); + + torrent_status st1 = tor1.status(); + torrent_status st2 = tor2.status(); + + if (i % 10 == 0) + { + print_ses_rate(i / 10.f, &st1, &st2); + } + + if (st2.is_finished) break; + + if (st2.state != torrent_status::downloading) + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating", "checking (r)"}; + std::cerr << "st2 state: " << state_str[st2.state] << std::endl; + } + + TEST_CHECK(st1.state == torrent_status::seeding + || st1.state == torrent_status::checking_files); + TEST_CHECK(st2.state == torrent_status::downloading + || st2.state == torrent_status::checking_resume_data); + + if (peer_disconnects >= 2) break; + + test_sleep(100); + } + + torrent_status st2 = tor2.status(); + TEST_CHECK(st2.is_seeding); + + if (!st2.is_seeding) return; + + fprintf(stderr, "\ntesting force recheck\n\n"); + + // test force rechecking a seeding torrent with remapped files + tor2.force_recheck(); + + for (int i = 0; i < 50; ++i) + { + print_alerts(ses2, "ses2", true, true, true, on_alert); + + torrent_status st2 = tor2.status(); + + if (i % 10 == 0) + { + print_ses_rate(i / 10.f, NULL, &st2); + } + + if (st2.state != torrent_status::checking_files) + { + static char const* state_str[] = + {"checking (q)", "checking", "dl metadata" + , "downloading", "finished", "seeding", "allocating", "checking (r)"}; + std::cerr << "st2 state: " << state_str[st2.state] << std::endl; + } + + if (st2.progress == 1.0) break; + + test_sleep(100); + } + + st2 = tor2.status(); + TEST_CHECK(st2.is_seeding); +} + +int test_main() +{ + using namespace libtorrent; + + error_code ec; + remove_all("tmp1_remap", ec); + remove_all("tmp2_remap", ec); + + test_remap_files_gather(); + + remove_all("tmp1_remap", ec); + remove_all("tmp2_remap", ec); + + test_remap_files_scatter(); + + remove_all("tmp1_remap", ec); + remove_all("tmp2_remap", ec); + remove_all("tmp1_remap2", ec); + remove_all("tmp2_remap2", ec); + + return 0; +} +