2008-08-18 22:02:50 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, 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/session_settings.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
2009-11-02 03:00:41 +01:00
|
|
|
#include "libtorrent/thread.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/time.hpp"
|
2010-02-18 05:37:02 +01:00
|
|
|
#include "libtorrent/file.hpp"
|
2008-08-18 22:02:50 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/bind.hpp>
|
2008-08-18 22:02:50 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <fstream>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2008-08-19 17:04:14 +02:00
|
|
|
using namespace libtorrent;
|
|
|
|
using boost::tuples::ignore;
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-24 19:15:01 +01:00
|
|
|
int const alert_mask = alert::all_categories
|
|
|
|
& ~alert::progress_notification
|
|
|
|
& ~alert::stats_notification;
|
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
int peer_disconnects = 0;
|
|
|
|
|
2013-08-01 18:15:02 +02:00
|
|
|
int tracker_responses = 0;
|
|
|
|
|
|
|
|
bool on_alert(alert* a)
|
2013-07-28 17:06:28 +02:00
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
if (alert_cast<tracker_reply_alert>(a))
|
|
|
|
++tracker_responses;
|
|
|
|
else if (alert_cast<peer_disconnected_alert>(a))
|
2013-07-28 17:06:28 +02:00
|
|
|
++peer_disconnects;
|
2013-08-01 05:50:57 +02:00
|
|
|
else if (alert_cast<peer_error_alert>(a))
|
|
|
|
++peer_disconnects;
|
2013-08-01 18:15:02 +02:00
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-19 17:04:14 +02:00
|
|
|
// test the maximum transfer rate
|
|
|
|
void test_rate()
|
2008-08-18 22:02:50 +02:00
|
|
|
{
|
2009-06-21 01:23:41 +02:00
|
|
|
// in case the previous run was terminated
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2012-03-17 20:06:11 +01:00
|
|
|
remove_all("tmp1_transfer", ec);
|
|
|
|
remove_all("tmp2_transfer", ec);
|
|
|
|
remove_all("tmp1_transfer_moved", ec);
|
|
|
|
remove_all("tmp2_transfer_moved", ec);
|
2009-06-21 01:23:41 +02:00
|
|
|
|
2013-08-31 22:19:03 +02:00
|
|
|
// these are declared before the session objects
|
|
|
|
// so that they are destructed last. This enables
|
|
|
|
// the sessions to destruct in parallel
|
|
|
|
session_proxy p1;
|
|
|
|
session_proxy p2;
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48575, 49000), "0.0.0.0", 0, alert_mask);
|
|
|
|
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49575, 50000), "0.0.0.0", 0, alert_mask);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2008-08-19 17:04:14 +02:00
|
|
|
torrent_handle tor1;
|
|
|
|
torrent_handle tor2;
|
|
|
|
|
2012-03-17 20:06:11 +01:00
|
|
|
create_directory("tmp1_transfer", ec);
|
|
|
|
std::ofstream file("tmp1_transfer/temporary");
|
2009-04-10 10:40:13 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 4 * 1024 * 1024, 7);
|
2008-08-19 17:04:14 +02:00
|
|
|
file.close();
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
wait_for_listen(ses1, "ses1");
|
2013-09-03 10:39:30 +02:00
|
|
|
wait_for_listen(ses2, "ses2");
|
2010-12-24 01:37:01 +01:00
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
peer_disconnects = 0;
|
|
|
|
|
2013-09-02 11:24:34 +02:00
|
|
|
session_settings sett = high_performance_seed();
|
2013-09-03 10:39:30 +02:00
|
|
|
sett.enable_outgoing_utp = true;
|
|
|
|
sett.enable_incoming_utp = true;
|
|
|
|
sett.enable_outgoing_tcp = false;
|
|
|
|
sett.enable_incoming_tcp = false;
|
2013-09-02 11:24:34 +02:00
|
|
|
ses1.set_settings(sett);
|
|
|
|
ses2.set_settings(sett);
|
|
|
|
|
2008-08-19 17:04:14 +02:00
|
|
|
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0
|
|
|
|
, true, false, true, "_transfer", 0, &t);
|
|
|
|
|
|
|
|
ptime start = time_now();
|
|
|
|
|
2013-09-02 19:41:01 +02:00
|
|
|
// it shouldn't take more than 7 seconds
|
|
|
|
for (int i = 0; i < 70; ++i)
|
2008-08-19 17:04:14 +02:00
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses1, "ses1", true, true, true, &on_alert);
|
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2008-08-19 17:04:14 +02:00
|
|
|
|
|
|
|
torrent_status st1 = tor1.status();
|
|
|
|
torrent_status st2 = tor2.status();
|
|
|
|
|
2010-10-09 21:56:10 +02:00
|
|
|
if (i % 10 == 0)
|
2013-06-20 07:28:42 +02:00
|
|
|
print_ses_rate(i / 10.f, &st1, &st2);
|
2008-08-19 17:04:14 +02:00
|
|
|
|
2013-08-04 06:04:44 +02:00
|
|
|
if (peer_disconnects >= 1) break;
|
2010-10-30 19:23:30 +02:00
|
|
|
if (st2.is_seeding) break;
|
2010-10-09 21:56:10 +02:00
|
|
|
test_sleep(100);
|
2008-08-19 17:04:14 +02:00
|
|
|
}
|
|
|
|
|
2010-10-30 19:23:30 +02:00
|
|
|
TEST_CHECK(tor2.status().is_seeding);
|
2008-08-19 17:04:14 +02:00
|
|
|
|
|
|
|
time_duration dt = time_now() - start;
|
|
|
|
|
|
|
|
std::cerr << "downloaded " << t->total_size() << " bytes "
|
|
|
|
"in " << (total_milliseconds(dt) / 1000.f) << " seconds" << std::endl;
|
|
|
|
|
2009-04-30 03:44:50 +02:00
|
|
|
std::cerr << "average download rate: " << (t->total_size() / (std::max)(total_milliseconds(dt), 1))
|
2008-08-19 17:04:14 +02:00
|
|
|
<< " kB/s" << std::endl;
|
2013-08-31 22:19:03 +02:00
|
|
|
|
|
|
|
// this allows shutting down the sessions in parallel
|
|
|
|
p1 = ses1.abort();
|
|
|
|
p2 = ses2.abort();
|
2008-08-19 17:04:14 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
void print_alert(std::auto_ptr<alert>)
|
2008-12-15 08:12:08 +01:00
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
std::cout << "ses1 (alert dispatch function): "/* << a.message() */ << std::endl;
|
2008-12-15 08:12:08 +01:00
|
|
|
}
|
|
|
|
|
2009-06-10 11:20:55 +02:00
|
|
|
// simulate a full disk
|
|
|
|
struct test_storage : storage_interface
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
test_storage(file_storage const& fs, std::string const& p, file_pool& fp)
|
2010-07-15 03:14:36 +02:00
|
|
|
: m_lower_layer(default_storage_constructor(fs, 0, p, fp, std::vector<boost::uint8_t>()))
|
2009-06-10 11:20:55 +02:00
|
|
|
, m_written(0)
|
|
|
|
, m_limit(16 * 1024 * 2)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual bool initialize(bool allocate_files)
|
|
|
|
{ return m_lower_layer->initialize(allocate_files); }
|
|
|
|
|
|
|
|
virtual bool has_any_file()
|
|
|
|
{ return m_lower_layer->has_any_file(); }
|
|
|
|
|
2013-03-03 05:47:19 +01:00
|
|
|
virtual int readv(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags)
|
|
|
|
{ return m_lower_layer->readv(bufs, slot, offset, num_bufs, flags); }
|
2009-06-10 11:20:55 +02:00
|
|
|
|
2013-03-03 05:47:19 +01:00
|
|
|
virtual int writev(file::iovec_t const* bufs, int slot, int offset, int num_bufs, int flags)
|
2009-06-10 11:20:55 +02:00
|
|
|
{
|
2013-03-03 05:47:19 +01:00
|
|
|
int ret = m_lower_layer->writev(bufs, slot, offset, num_bufs, flags);
|
2009-06-10 11:20:55 +02:00
|
|
|
if (ret > 0) m_written += ret;
|
|
|
|
if (m_written > m_limit)
|
|
|
|
{
|
2009-06-19 19:39:49 +02:00
|
|
|
#if BOOST_VERSION == 103500
|
|
|
|
set_error("", error_code(boost::system::posix_error::no_space_on_device, get_posix_category()));
|
|
|
|
#elif BOOST_VERSION > 103500
|
2009-06-10 11:20:55 +02:00
|
|
|
set_error("", error_code(boost::system::errc::no_space_on_device, get_posix_category()));
|
2009-06-19 19:39:49 +02:00
|
|
|
#else
|
|
|
|
set_error("", error_code(ENOSPC, get_posix_category()));
|
|
|
|
#endif
|
2009-06-10 11:20:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-05 09:21:10 +02:00
|
|
|
virtual size_type physical_offset(int piece_index, int offset)
|
|
|
|
{ return m_lower_layer->physical_offset(piece_index, offset); }
|
|
|
|
|
2009-06-10 11:20:55 +02:00
|
|
|
virtual int read(char* buf, int slot, int offset, int size)
|
|
|
|
{ return m_lower_layer->read(buf, slot, offset, size); }
|
|
|
|
|
|
|
|
virtual int write(const char* buf, int slot, int offset, int size)
|
|
|
|
{
|
|
|
|
int ret = m_lower_layer->write(buf, slot, offset, size);
|
|
|
|
if (ret > 0) m_written += ret;
|
|
|
|
if (m_written > m_limit)
|
|
|
|
{
|
2009-06-19 19:39:49 +02:00
|
|
|
#if BOOST_VERSION == 103500
|
|
|
|
set_error("", error_code(boost::system::posix_error::no_space_on_device, get_posix_category()));
|
|
|
|
#elif BOOST_VERSION > 103500
|
2009-06-10 11:20:55 +02:00
|
|
|
set_error("", error_code(boost::system::errc::no_space_on_device, get_posix_category()));
|
2009-06-19 19:39:49 +02:00
|
|
|
#else
|
|
|
|
set_error("", error_code(ENOSPC, get_posix_category()));
|
|
|
|
#endif
|
2009-06-10 11:20:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int sparse_end(int start) const
|
|
|
|
{ return m_lower_layer->sparse_end(start); }
|
|
|
|
|
2013-05-09 04:50:16 +02:00
|
|
|
virtual int move_storage(std::string const& save_path, int flags)
|
|
|
|
{ return m_lower_layer->move_storage(save_path, flags); }
|
2009-06-10 11:20:55 +02:00
|
|
|
|
2009-06-28 02:36:41 +02:00
|
|
|
virtual bool verify_resume_data(lazy_entry const& rd, error_code& error)
|
2009-06-10 11:20:55 +02:00
|
|
|
{ return m_lower_layer->verify_resume_data(rd, error); }
|
|
|
|
|
|
|
|
virtual bool write_resume_data(entry& rd) const
|
|
|
|
{ return m_lower_layer->write_resume_data(rd); }
|
|
|
|
|
|
|
|
virtual bool move_slot(int src_slot, int dst_slot)
|
|
|
|
{ return m_lower_layer->move_slot(src_slot, dst_slot); }
|
|
|
|
|
|
|
|
virtual bool swap_slots(int slot1, int slot2)
|
|
|
|
{ return m_lower_layer->swap_slots(slot1, slot2); }
|
|
|
|
|
|
|
|
virtual bool swap_slots3(int slot1, int slot2, int slot3)
|
|
|
|
{ return m_lower_layer->swap_slots3(slot1, slot2, slot3); }
|
|
|
|
|
|
|
|
virtual bool release_files() { return m_lower_layer->release_files(); }
|
|
|
|
|
|
|
|
virtual bool rename_file(int index, std::string const& new_filename)
|
|
|
|
{ return m_lower_layer->rename_file(index, new_filename); }
|
|
|
|
|
|
|
|
virtual bool delete_files() { return m_lower_layer->delete_files(); }
|
|
|
|
|
|
|
|
virtual ~test_storage() {}
|
|
|
|
|
|
|
|
boost::scoped_ptr<storage_interface> m_lower_layer;
|
|
|
|
int m_written;
|
|
|
|
int m_limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
storage_interface* test_storage_constructor(file_storage const& fs
|
2010-07-15 03:14:36 +02:00
|
|
|
, file_storage const*, std::string const& path, file_pool& fp, std::vector<boost::uint8_t> const&)
|
2009-06-10 11:20:55 +02:00
|
|
|
{
|
|
|
|
return new test_storage(fs, path, fp);
|
|
|
|
}
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
void test_transfer(int proxy_type, bool test_disk_full = false, bool test_allowed_fast = false, bool test_priorities = false)
|
2010-02-18 05:37:02 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
|
|
|
|
|
2013-08-18 03:08:18 +02:00
|
|
|
fprintf(stderr, "\n\n ==== TESTING %s proxy ==== disk-full: %s allow-fast: %s priorities: %s\n\n\n"
|
|
|
|
, test_name[proxy_type], test_disk_full ? "true": "false", test_allowed_fast ? "true" : "false"
|
|
|
|
, test_priorities ? "true" : "false");
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2009-06-21 01:23:41 +02:00
|
|
|
// in case the previous run was terminated
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2012-03-17 20:06:11 +01:00
|
|
|
remove_all("tmp1_transfer", ec);
|
|
|
|
remove_all("tmp2_transfer", ec);
|
|
|
|
remove_all("tmp1_transfer_moved", ec);
|
|
|
|
remove_all("tmp2_transfer_moved", ec);
|
2009-06-21 01:23:41 +02:00
|
|
|
|
2013-08-31 22:19:03 +02:00
|
|
|
// these are declared before the session objects
|
|
|
|
// so that they are destructed last. This enables
|
|
|
|
// the sessions to destruct in parallel
|
|
|
|
session_proxy p1;
|
|
|
|
session_proxy p2;
|
|
|
|
|
2010-12-24 19:15:01 +01:00
|
|
|
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);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2012-06-09 18:58:16 +02:00
|
|
|
proxy_settings ps;
|
2010-02-18 05:37:02 +01:00
|
|
|
if (proxy_type)
|
|
|
|
{
|
2012-06-09 18:58:16 +02:00
|
|
|
ps.port = start_proxy(proxy_type);
|
2010-02-18 05:37:02 +01:00
|
|
|
ps.hostname = "127.0.0.1";
|
|
|
|
ps.username = "testuser";
|
|
|
|
ps.password = "testpass";
|
|
|
|
ps.type = (proxy_settings::proxy_type)proxy_type;
|
2010-08-23 08:27:18 +02:00
|
|
|
ses1.set_proxy(ps);
|
|
|
|
ses2.set_proxy(ps);
|
2010-02-18 05:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
session_settings sett;
|
2010-12-24 01:37:01 +01:00
|
|
|
sett.allow_multiple_connections_per_ip = false;
|
2012-06-08 06:28:37 +02:00
|
|
|
sett.ignore_limits_on_local_network = false;
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2009-06-21 01:23:41 +02:00
|
|
|
if (test_allowed_fast)
|
|
|
|
{
|
|
|
|
sett.allowed_fast_set_size = 2000;
|
2010-10-09 21:09:38 +02:00
|
|
|
sett.unchoke_slots_limit = 0;
|
2009-06-21 01:23:41 +02:00
|
|
|
}
|
|
|
|
|
2011-12-19 06:53:11 +01:00
|
|
|
sett.unchoke_slots_limit = 0;
|
|
|
|
ses1.set_settings(sett);
|
|
|
|
TEST_CHECK(ses1.settings().unchoke_slots_limit == 0);
|
|
|
|
sett.unchoke_slots_limit = -1;
|
|
|
|
ses1.set_settings(sett);
|
|
|
|
TEST_CHECK(ses1.settings().unchoke_slots_limit == -1);
|
|
|
|
sett.unchoke_slots_limit = 8;
|
|
|
|
ses1.set_settings(sett);
|
|
|
|
TEST_CHECK(ses1.settings().unchoke_slots_limit == 8);
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
// we need a short reconnect time since we
|
|
|
|
// finish the torrent and then restart it
|
|
|
|
// immediately to complete the second half.
|
|
|
|
// using a reconnect time > 0 will just add
|
|
|
|
// to the time it will take to complete the test
|
|
|
|
sett.min_reconnect_time = 0;
|
2010-12-24 19:15:01 +01:00
|
|
|
sett.stop_tracker_timeout = 1;
|
2010-02-18 05:37:02 +01:00
|
|
|
sett.announce_to_all_trackers = true;
|
|
|
|
sett.announce_to_all_tiers = true;
|
|
|
|
// make sure we announce to both http and udp trackers
|
|
|
|
sett.prefer_udp_trackers = false;
|
2010-12-20 01:20:56 +01:00
|
|
|
sett.enable_outgoing_utp = false;
|
|
|
|
sett.enable_incoming_utp = false;
|
2010-02-18 05:37:02 +01:00
|
|
|
|
|
|
|
ses1.set_settings(sett);
|
|
|
|
ses2.set_settings(sett);
|
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
pe_settings pes;
|
2010-08-25 08:22:49 +02:00
|
|
|
pes.out_enc_policy = pe_settings::disabled;
|
|
|
|
pes.in_enc_policy = pe_settings::disabled;
|
2008-08-18 22:02:50 +02:00
|
|
|
ses1.set_pe_settings(pes);
|
|
|
|
ses2.set_pe_settings(pes);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
torrent_handle tor1;
|
|
|
|
torrent_handle tor2;
|
|
|
|
|
2012-03-17 20:06:11 +01:00
|
|
|
create_directory("tmp1_transfer", ec);
|
|
|
|
std::ofstream file("tmp1_transfer/temporary");
|
2010-02-18 05:37:02 +01:00
|
|
|
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 16 * 1024, 13, false);
|
2008-08-18 22:02:50 +02:00
|
|
|
file.close();
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
if (test_priorities)
|
|
|
|
{
|
|
|
|
int udp_tracker_port = start_tracker();
|
|
|
|
int tracker_port = start_web_server();
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
char tracker_url[200];
|
|
|
|
snprintf(tracker_url, sizeof(tracker_url), "http://127.0.0.1:%d/announce", tracker_port);
|
|
|
|
t->add_tracker(tracker_url);
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
snprintf(tracker_url, sizeof(tracker_url), "udp://127.0.0.1:%d/announce", udp_tracker_port);
|
|
|
|
t->add_tracker(tracker_url);
|
|
|
|
}
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2009-06-10 11:20:55 +02:00
|
|
|
add_torrent_params addp(&test_storage_constructor);
|
2012-03-19 05:02:13 +01:00
|
|
|
addp.flags &= ~add_torrent_params::flag_paused;
|
|
|
|
addp.flags &= ~add_torrent_params::flag_auto_managed;
|
2009-06-10 11:20:55 +02:00
|
|
|
|
2010-12-24 19:15:01 +01:00
|
|
|
wait_for_listen(ses1, "ses1");
|
|
|
|
wait_for_listen(ses2, "ses1");
|
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
peer_disconnects = 0;
|
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
// test using piece sizes smaller than 16kB
|
|
|
|
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0
|
2009-06-10 11:20:55 +02:00
|
|
|
, true, false, true, "_transfer", 8 * 1024, &t, false, test_disk_full?&addp:0);
|
|
|
|
|
2013-06-18 02:33:16 +02:00
|
|
|
int num_pieces = tor2.torrent_file()->num_pieces();
|
2008-08-18 22:02:50 +02:00
|
|
|
std::vector<int> priorities(num_pieces, 1);
|
2010-12-27 23:54:14 +01:00
|
|
|
if (test_priorities)
|
|
|
|
{
|
|
|
|
// set half of the pieces to priority 0
|
|
|
|
std::fill(priorities.begin(), priorities.begin() + (num_pieces / 2), 0);
|
|
|
|
tor2.prioritize_pieces(priorities);
|
|
|
|
std::cerr << "setting priorities: ";
|
|
|
|
std::copy(priorities.begin(), priorities.end(), std::ostream_iterator<int>(std::cerr, ", "));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-08-25 08:22:49 +02:00
|
|
|
// ses1.set_alert_dispatch(&print_alert);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
// sett = ses2.settings();
|
|
|
|
// sett.download_rate_limit = tor2.get_torrent_info().piece_length() * 5;
|
|
|
|
// ses2.set_settings(sett);
|
2009-04-10 10:40:13 +02:00
|
|
|
|
2009-03-30 06:55:23 +02:00
|
|
|
// also test to move the storage of the downloader and the uploader
|
|
|
|
// to make sure it can handle switching paths
|
|
|
|
bool test_move_storage = false;
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_responses = 0;
|
|
|
|
|
2013-08-04 02:02:32 +02:00
|
|
|
for (int i = 0; i < 200; ++i)
|
2008-08-18 22:02:50 +02:00
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses1, "ses1", true, true, true, &on_alert);
|
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
|
|
|
torrent_status st1 = tor1.status();
|
|
|
|
torrent_status st2 = tor2.status();
|
|
|
|
|
2010-10-09 21:56:10 +02:00
|
|
|
if (i % 10 == 0)
|
|
|
|
{
|
2013-06-20 07:28:42 +02:00
|
|
|
print_ses_rate(i / 10.f, &st1, &st2);
|
2010-10-09 21:56:10 +02:00
|
|
|
}
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2009-03-30 06:55:23 +02:00
|
|
|
if (!test_move_storage && st2.progress > 0.25f)
|
|
|
|
{
|
|
|
|
test_move_storage = true;
|
2012-03-17 20:06:11 +01:00
|
|
|
tor1.move_storage("tmp1_transfer_moved");
|
|
|
|
tor2.move_storage("tmp2_transfer_moved");
|
2009-03-30 06:55:23 +02:00
|
|
|
std::cerr << "moving storage" << std::endl;
|
|
|
|
}
|
|
|
|
|
2009-06-19 00:32:55 +02:00
|
|
|
if (test_disk_full && st2.upload_mode)
|
2009-06-10 11:20:55 +02:00
|
|
|
{
|
|
|
|
test_disk_full = false;
|
|
|
|
((test_storage*)tor2.get_storage_impl())->m_limit = 16 * 1024 * 1024;
|
2009-06-19 00:32:55 +02:00
|
|
|
tor2.set_upload_mode(false);
|
2013-08-18 03:08:18 +02:00
|
|
|
TEST_CHECK(tor2.status().is_finished == false);
|
|
|
|
TEST_EQUAL(peer_disconnects, 2);
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: discovered disk full mode. Raise limit and disable upload-mode\n", time_now_string());
|
2013-08-18 03:08:18 +02:00
|
|
|
peer_disconnects = -1;
|
2013-08-01 05:50:57 +02:00
|
|
|
test_sleep(100);
|
2009-06-10 11:20:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-10-30 19:23:30 +02:00
|
|
|
if (!test_disk_full && st2.is_finished) break;
|
2009-04-10 10:40:13 +02:00
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-02 10:03:04 +01:00
|
|
|
TEST_CHECK(st1.state == torrent_status::seeding
|
|
|
|
|| st1.state == torrent_status::checking_files);
|
2009-06-10 11:20:55 +02:00
|
|
|
TEST_CHECK(st2.state == torrent_status::downloading
|
2013-07-30 09:13:12 +02:00
|
|
|
|| st2.state == torrent_status::checking_resume_data
|
2009-06-10 11:20:55 +02:00
|
|
|
|| (test_disk_full && !st2.error.empty()));
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2013-08-18 03:08:18 +02:00
|
|
|
if (peer_disconnects >= 2) break;
|
2013-07-28 17:06:28 +02:00
|
|
|
|
2013-08-04 02:02:32 +02:00
|
|
|
// if nothing is being transferred after 2 seconds, we're failing the test
|
|
|
|
if (st1.upload_payload_rate == 0 && i > 20) break;
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
test_sleep(100);
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
if (test_priorities)
|
2008-08-24 10:30:00 +02:00
|
|
|
{
|
2010-12-27 23:54:14 +01:00
|
|
|
// 1 announce per tracker to start
|
|
|
|
TEST_CHECK(tracker_responses >= 2);
|
2008-08-24 10:30:00 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
TEST_CHECK(!tor2.status().is_seeding);
|
|
|
|
TEST_CHECK(tor2.status().is_finished);
|
2008-08-24 10:30:00 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
if (tor2.status().is_finished)
|
|
|
|
std::cerr << "torrent is finished (50% complete)" << std::endl;
|
|
|
|
else return;
|
2008-08-24 10:30:00 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::cerr << "force recheck" << std::endl;
|
|
|
|
tor2.force_recheck();
|
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
peer_disconnects = 0;
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
for (int i = 0; i < 50; ++i)
|
2010-04-24 21:36:16 +02:00
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2013-07-28 17:06:28 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
torrent_status st2 = tor2.status();
|
|
|
|
if (i % 10 == 0)
|
2013-06-14 18:38:26 +02:00
|
|
|
{
|
|
|
|
std::cerr << int(st2.progress * 100) << "% " << std::endl;
|
|
|
|
}
|
2010-12-27 23:54:14 +01:00
|
|
|
if (st2.state != torrent_status::checking_files) break;
|
2013-08-04 06:04:44 +02:00
|
|
|
if (peer_disconnects >= 1) break;
|
2013-08-04 01:44:03 +02:00
|
|
|
test_sleep(100);
|
2010-04-24 21:36:16 +02:00
|
|
|
}
|
2009-11-20 18:23:09 +01:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::vector<int> priorities2 = tor2.piece_priorities();
|
|
|
|
TEST_CHECK(std::equal(priorities.begin(), priorities.end(), priorities2.begin()));
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2013-08-04 01:44:03 +02:00
|
|
|
peer_disconnects = 0;
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
for (int i = 0; i < 5; ++i)
|
2008-08-18 22:02:50 +02:00
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2010-12-27 23:54:14 +01:00
|
|
|
torrent_status st2 = tor2.status();
|
|
|
|
TEST_CHECK(st2.state == torrent_status::finished);
|
2013-08-04 06:04:44 +02:00
|
|
|
if (peer_disconnects >= 1) break;
|
2010-12-27 23:54:14 +01:00
|
|
|
test_sleep(100);
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
tor2.pause();
|
|
|
|
alert const* a = ses2.wait_for_alert(seconds(10));
|
|
|
|
bool got_paused_alert = false;
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
std::auto_ptr<alert> holder = ses2.pop_alert();
|
|
|
|
std::cerr << "ses2: " << a->message() << std::endl;
|
|
|
|
if (alert_cast<torrent_paused_alert>(a))
|
|
|
|
{
|
|
|
|
got_paused_alert = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
a = ses2.wait_for_alert(seconds(10));
|
|
|
|
}
|
|
|
|
TEST_CHECK(got_paused_alert);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::vector<announce_entry> tr = tor2.trackers();
|
|
|
|
tr.push_back(announce_entry("http://test.com/announce"));
|
|
|
|
tor2.replace_trackers(tr);
|
|
|
|
tr.clear();
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
tor2.save_resume_data();
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::vector<char> resume_data;
|
|
|
|
a = ses2.wait_for_alert(seconds(10));
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
std::auto_ptr<alert> holder = ses2.pop_alert();
|
|
|
|
std::cerr << "ses2: " << a->message() << std::endl;
|
|
|
|
if (alert_cast<save_resume_data_alert>(a))
|
|
|
|
{
|
|
|
|
bencode(std::back_inserter(resume_data)
|
|
|
|
, *alert_cast<save_resume_data_alert>(a)->resume_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
a = ses2.wait_for_alert(seconds(10));
|
|
|
|
}
|
|
|
|
TEST_CHECK(resume_data.size());
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::cerr << "saved resume data" << std::endl;
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
ses2.remove_torrent(tor2);
|
2009-11-20 18:23:09 +01:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::cerr << "removed" << std::endl;
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
test_sleep(100);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::cout << "re-adding" << std::endl;
|
|
|
|
add_torrent_params p;
|
2012-03-19 05:02:13 +01:00
|
|
|
p.flags &= ~add_torrent_params::flag_paused;
|
|
|
|
p.flags &= ~add_torrent_params::flag_auto_managed;
|
2010-12-27 23:54:14 +01:00
|
|
|
p.ti = t;
|
2012-03-17 20:06:11 +01:00
|
|
|
p.save_path = "tmp2_transfer_moved";
|
2013-07-17 22:21:48 +02:00
|
|
|
p.resume_data = resume_data;
|
2010-12-27 23:54:14 +01:00
|
|
|
tor2 = ses2.add_torrent(p, ec);
|
|
|
|
ses2.set_alert_mask(alert::all_categories
|
|
|
|
& ~alert::progress_notification
|
|
|
|
& ~alert::stats_notification);
|
|
|
|
tor2.prioritize_pieces(priorities);
|
|
|
|
std::cout << "resetting priorities" << std::endl;
|
|
|
|
tor2.resume();
|
|
|
|
|
|
|
|
tr = tor2.trackers();
|
|
|
|
TEST_CHECK(std::find_if(tr.begin(), tr.end()
|
|
|
|
, boost::bind(&announce_entry::url, _1) == "http://test.com/announce") != tr.end());
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2013-08-04 01:44:03 +02:00
|
|
|
peer_disconnects = 0;
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses1, "ses1", true, true, true, &on_alert);
|
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
torrent_status st1 = tor1.status();
|
|
|
|
torrent_status st2 = tor2.status();
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
TEST_CHECK(st1.state == torrent_status::seeding);
|
|
|
|
TEST_CHECK(st2.state == torrent_status::finished);
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2013-08-04 06:04:44 +02:00
|
|
|
if (peer_disconnects >= 1) break;
|
2013-08-04 01:44:03 +02:00
|
|
|
|
2013-08-18 03:08:18 +02:00
|
|
|
if (st2.is_finished) break;
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
test_sleep(100);
|
2010-10-09 21:56:10 +02:00
|
|
|
}
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
TEST_CHECK(!tor2.status().is_seeding);
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
std::fill(priorities.begin(), priorities.end(), 1);
|
|
|
|
tor2.prioritize_pieces(priorities);
|
|
|
|
std::cout << "setting priorities to 1" << std::endl;
|
2013-08-18 03:08:18 +02:00
|
|
|
TEST_EQUAL(tor2.status().is_finished, false);
|
2008-08-19 09:11:17 +02:00
|
|
|
|
2013-08-04 01:44:03 +02:00
|
|
|
peer_disconnects = 0;
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
for (int i = 0; i < 130; ++i)
|
|
|
|
{
|
2013-08-01 18:15:02 +02:00
|
|
|
print_alerts(ses1, "ses1", true, true, true, &on_alert);
|
|
|
|
print_alerts(ses2, "ses2", true, true, true, &on_alert);
|
2010-12-27 23:54:14 +01:00
|
|
|
|
|
|
|
torrent_status st1 = tor1.status();
|
|
|
|
torrent_status st2 = tor2.status();
|
|
|
|
|
|
|
|
if (i % 10 == 0)
|
2013-06-20 07:28:42 +02:00
|
|
|
print_ses_rate(i / 10.f, &st1, &st2);
|
2010-12-27 23:54:14 +01:00
|
|
|
|
2013-08-18 03:08:18 +02:00
|
|
|
if (st2.is_seeding) break;
|
2010-12-27 23:54:14 +01:00
|
|
|
|
2013-08-18 03:08:18 +02:00
|
|
|
TEST_EQUAL(st1.state, torrent_status::seeding);
|
|
|
|
TEST_EQUAL(st2.state, torrent_status::downloading);
|
2010-12-27 23:54:14 +01:00
|
|
|
|
2013-08-04 06:04:44 +02:00
|
|
|
if (peer_disconnects >= 1) break;
|
2013-08-04 01:44:03 +02:00
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
test_sleep(100);
|
|
|
|
}
|
2008-08-19 09:11:17 +02:00
|
|
|
}
|
|
|
|
|
2010-10-30 19:23:30 +02:00
|
|
|
TEST_CHECK(tor2.status().is_seeding);
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2013-08-31 22:19:03 +02:00
|
|
|
// this allows shutting down the sessions in parallel
|
|
|
|
p1 = ses1.abort();
|
|
|
|
p2 = ses2.abort();
|
|
|
|
|
2010-12-27 23:54:14 +01:00
|
|
|
if (test_priorities)
|
|
|
|
{
|
|
|
|
stop_tracker();
|
|
|
|
stop_web_server();
|
|
|
|
}
|
2012-06-09 18:58:16 +02:00
|
|
|
if (proxy_type) stop_proxy(ps.port);
|
2013-08-31 22:19:03 +02:00
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int test_main()
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2013-09-02 11:24:34 +02:00
|
|
|
#if !defined TORRENT_DEBUG \
|
|
|
|
&& defined TORRENT_DISABLE_INVARIANT_CHECKS \
|
|
|
|
&& !defined _GLIBCXX_DEBUG
|
2008-08-19 17:04:14 +02:00
|
|
|
// test rate only makes sense in release mode
|
|
|
|
test_rate();
|
2013-09-02 11:24:34 +02:00
|
|
|
return 0;
|
2008-08-19 17:04:14 +02:00
|
|
|
#endif
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
// test with all kinds of proxies
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
test_transfer(i);
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2009-06-21 01:23:41 +02:00
|
|
|
// test with a (simulated) full disk
|
2010-12-27 23:54:14 +01:00
|
|
|
test_transfer(0, true, true);
|
2009-06-21 01:23:41 +02:00
|
|
|
|
|
|
|
// test allowed fast
|
2010-12-27 23:54:14 +01:00
|
|
|
test_transfer(0, false, true, true);
|
2009-06-21 01:23:41 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2012-03-17 20:06:11 +01:00
|
|
|
remove_all("tmp1_transfer", ec);
|
|
|
|
remove_all("tmp2_transfer", ec);
|
|
|
|
remove_all("tmp1_transfer_moved", ec);
|
|
|
|
remove_all("tmp2_transfer_moved", ec);
|
2008-08-19 17:04:14 +02:00
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|