2008-07-01 20:59:13 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-02-20 02:42:12 +01:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/file_pool.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
2007-08-21 03:17:42 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2007-02-20 02:42:12 +01:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-08-21 03:17:42 +02:00
|
|
|
#include <fstream>
|
2007-02-20 02:42:12 +01:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
|
2008-05-14 07:29:42 +02:00
|
|
|
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
|
2007-02-20 02:42:12 +01:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2007-11-28 01:16:14 +01:00
|
|
|
|
2008-12-21 00:06:06 +01:00
|
|
|
session ses(fingerprint(" ", 0,0,0,0), 0);
|
2008-06-03 17:17:09 +02:00
|
|
|
session_settings settings;
|
|
|
|
settings.ignore_limits_on_local_network = false;
|
2009-06-10 10:30:55 +02:00
|
|
|
settings.max_queued_disk_bytes = 256 * 1024;
|
2008-06-03 17:17:09 +02:00
|
|
|
ses.set_settings(settings);
|
2008-12-24 21:07:34 +01:00
|
|
|
ses.set_alert_mask(~alert::progress_notification);
|
2007-12-30 10:36:01 +01:00
|
|
|
ses.listen_on(std::make_pair(51000, 52000));
|
2009-04-28 19:49:17 +02:00
|
|
|
ses.set_download_rate_limit(torrent_file->total_size() / 10);
|
2009-06-10 10:30:55 +02:00
|
|
|
remove_all("./tmp2_web_seed");
|
2007-11-28 01:16:14 +01:00
|
|
|
|
|
|
|
char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
|
|
|
|
|
|
|
|
std::cerr << " ==== TESTING " << test_name[proxy] << " proxy ====" << std::endl;
|
2007-02-20 02:42:12 +01:00
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
if (proxy)
|
|
|
|
{
|
|
|
|
start_proxy(8002, proxy);
|
|
|
|
proxy_settings ps;
|
|
|
|
ps.hostname = "127.0.0.1";
|
|
|
|
ps.port = 8002;
|
|
|
|
ps.username = "testuser";
|
|
|
|
ps.password = "testpass";
|
|
|
|
ps.type = (proxy_settings::proxy_type)proxy;
|
|
|
|
ses.set_web_seed_proxy(ps);
|
|
|
|
}
|
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
torrent_handle th = ses.add_torrent(*torrent_file, "./tmp2_web_seed");
|
2007-11-28 01:16:14 +01:00
|
|
|
|
|
|
|
std::vector<announce_entry> empty;
|
|
|
|
th.replace_trackers(empty);
|
|
|
|
|
2008-06-03 17:17:09 +02:00
|
|
|
const size_type total_size = torrent_file->total_size();
|
|
|
|
|
|
|
|
float rate_sum = 0.f;
|
|
|
|
float ses_rate_sum = 0.f;
|
|
|
|
|
2009-06-15 00:48:07 +02:00
|
|
|
cache_status cs;
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
for (int i = 0; i < 30; ++i)
|
|
|
|
{
|
|
|
|
torrent_status s = th.status();
|
2008-06-03 17:17:09 +02:00
|
|
|
session_status ss = ses.status();
|
2009-04-28 19:49:17 +02:00
|
|
|
rate_sum += s.download_payload_rate;
|
|
|
|
ses_rate_sum += ss.payload_download_rate;
|
2009-06-10 10:30:55 +02:00
|
|
|
|
2009-06-15 00:48:07 +02:00
|
|
|
cs = ses.get_cache_status();
|
2009-06-10 10:30:55 +02:00
|
|
|
if (cs.blocks_read < 1) cs.blocks_read = 1;
|
|
|
|
if (cs.blocks_written < 1) cs.blocks_written = 1;
|
|
|
|
|
2008-06-03 17:17:09 +02:00
|
|
|
std::cerr << (s.progress * 100.f) << " %"
|
|
|
|
<< " torrent rate: " << (s.download_rate / 1000.f) << " kB/s"
|
|
|
|
<< " session rate: " << (ss.download_rate / 1000.f) << " kB/s"
|
|
|
|
<< " session total: " << ss.total_payload_download
|
|
|
|
<< " torrent total: " << s.total_payload_download
|
2009-04-28 19:49:17 +02:00
|
|
|
<< " rate sum:" << ses_rate_sum
|
2009-06-10 10:30:55 +02:00
|
|
|
<< " cache: " << cs.cache_size
|
|
|
|
<< " rcache: " << cs.read_cache_size
|
|
|
|
<< " buffers: " << cs.total_used_buffers
|
2008-06-03 17:17:09 +02:00
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
print_alerts(ses, "ses");
|
2007-11-28 01:16:14 +01:00
|
|
|
|
2008-06-03 17:17:09 +02:00
|
|
|
if (th.is_seed() && ss.download_rate == 0.f)
|
|
|
|
{
|
|
|
|
TEST_CHECK(ses.status().total_payload_download == total_size);
|
|
|
|
TEST_CHECK(th.status().total_payload_download == total_size);
|
|
|
|
break;
|
|
|
|
}
|
2007-11-28 01:16:14 +01:00
|
|
|
test_sleep(1000);
|
|
|
|
}
|
|
|
|
|
2009-06-15 00:48:07 +02:00
|
|
|
TEST_CHECK(cs.cache_size == 0);
|
|
|
|
TEST_CHECK(cs.total_used_buffers == 0);
|
|
|
|
|
2008-06-03 17:17:09 +02:00
|
|
|
std::cerr << "total_size: " << total_size
|
|
|
|
<< " rate_sum: " << rate_sum
|
|
|
|
<< " session_rate_sum: " << ses_rate_sum
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
// the rates for each second should sum up to the total, with a 10% error margin
|
|
|
|
TEST_CHECK(fabs(rate_sum - total_size) < total_size * .1f);
|
|
|
|
TEST_CHECK(fabs(ses_rate_sum - total_size) < total_size * .1f);
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
TEST_CHECK(th.is_seed());
|
|
|
|
|
|
|
|
if (proxy) stop_proxy(8002);
|
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
TEST_CHECK(exists("./tmp2_web_seed" / torrent_file->file_at(0).path));
|
|
|
|
remove_all("./tmp2_web_seed");
|
2007-11-28 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int test_main()
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2008-11-02 01:26:13 +01:00
|
|
|
try {
|
2009-06-10 10:30:55 +02:00
|
|
|
create_directory("./tmp1_web_seed");
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
create_directory("./tmp1_web_seed/test_torrent_dir");
|
2008-11-02 01:26:13 +01:00
|
|
|
} catch (std::exception&) {}
|
|
|
|
|
2007-11-26 00:11:29 +01:00
|
|
|
char random_data[300000];
|
2009-06-10 10:30:55 +02:00
|
|
|
std::srand(10);
|
|
|
|
// memset(random_data, 1, sizeof(random_data));
|
2007-11-26 00:11:29 +01:00
|
|
|
std::generate(random_data, random_data + sizeof(random_data), &std::rand);
|
2009-06-10 10:30:55 +02:00
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test1").write(random_data, 35);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test2").write(random_data, 16536 - 35);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test3").write(random_data, 16536);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test4").write(random_data, 17);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test5").write(random_data, 16536);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test6").write(random_data, 300000);
|
|
|
|
std::ofstream("./tmp1_web_seed/test_torrent_dir/test7").write(random_data, 300000);
|
2007-11-26 00:11:29 +01:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
2009-06-10 10:30:55 +02:00
|
|
|
add_files(fs, path("./tmp1_web_seed/test_torrent_dir"));
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
libtorrent::create_torrent t(fs, 16 * 1024);
|
2009-06-10 10:30:55 +02:00
|
|
|
t.add_url_seed("http://127.0.0.1:8000/tmp1_web_seed");
|
2007-02-20 02:42:12 +01:00
|
|
|
|
2007-11-27 01:06:59 +01:00
|
|
|
start_web_server(8000);
|
2007-02-20 02:42:12 +01:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// calculate the hash for all pieces
|
2009-06-10 10:30:55 +02:00
|
|
|
set_piece_hashes(t, "./tmp1_web_seed");
|
2008-05-28 10:44:40 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
|
2007-02-20 02:42:12 +01:00
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
for (int i = 0; i < 6; ++i)
|
2008-05-14 07:29:42 +02:00
|
|
|
test_transfer(torrent_file, i);
|
2007-11-28 01:16:14 +01:00
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
torrent_file->rename_file(0, "./tmp2_web_seed/test_torrent_dir/renamed_test1");
|
2008-12-24 21:07:34 +01:00
|
|
|
test_transfer(torrent_file, 0);
|
|
|
|
|
2007-11-27 01:06:59 +01:00
|
|
|
stop_web_server(8000);
|
2009-06-10 10:30:55 +02:00
|
|
|
remove_all("./tmp1_web_seed");
|
2007-02-20 02:42:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|