premiere-libtorrent/test/setup_transfer.cpp

399 lines
11 KiB
C++
Raw Normal View History

/*
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-09-10 08:12:41 +02:00
#include <fstream>
#include <sstream>
2007-09-10 08:12:41 +02:00
#include "libtorrent/session.hpp"
#include "libtorrent/hasher.hpp"
2007-09-10 08:12:41 +02:00
#include "libtorrent/thread.hpp"
#include <boost/tuple/tuple.hpp>
#include "test.hpp"
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/create_torrent.hpp"
using namespace libtorrent;
2009-04-10 10:40:13 +02:00
bool tests_failure = false;
void report_failure(char const* err, char const* file, int line)
{
#ifdef TORRENT_WINDOWS
HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, 0, CONSOLE_TEXTMODE_BUFFER, 0);
SetConsoleTextAttribute(console, FOREGROUND_RED);
std::cerr << "\n**** " << file << ":" << line << " \"" << err << " ****\n\n";
CloseHandle(console);
#else
2009-04-10 10:40:13 +02:00
std::cerr << "\033[31m" << file << ":" << line << " \"" << err << "\"\033[0m\n";
#endif
2009-04-10 10:40:13 +02:00
tests_failure = true;
}
2009-01-02 10:42:51 +01:00
bool print_alerts(libtorrent::session& ses, char const* name
, bool allow_disconnects, bool allow_no_torrents, bool allow_failed_fastresume
, bool (*predicate)(libtorrent::alert*))
{
2009-01-02 10:42:51 +01:00
bool ret = false;
std::vector<torrent_handle> handles = ses.get_torrents();
TEST_CHECK(!handles.empty() || allow_no_torrents);
torrent_handle h;
if (!handles.empty()) h = handles[0];
std::auto_ptr<alert> a;
a = ses.pop_alert();
while (a.get())
{
2009-01-02 10:42:51 +01:00
if (predicate && predicate(a.get())) ret = true;
if (peer_disconnected_alert* p = dynamic_cast<peer_disconnected_alert*>(a.get()))
{
2008-07-08 02:00:49 +02:00
std::cerr << name << "(" << p->ip << "): " << p->message() << "\n";
}
2008-07-08 02:00:49 +02:00
else if (a->message() != "block downloading"
&& a->message() != "block finished"
&& a->message() != "piece finished")
{
2008-07-08 02:00:49 +02:00
std::cerr << name << ": " << a->message() << "\n";
}
TEST_CHECK(dynamic_cast<fastresume_rejected_alert*>(a.get()) == 0 || allow_failed_fastresume);
TEST_CHECK(dynamic_cast<peer_error_alert*>(a.get()) == 0
|| (!handles.empty() && h.is_seed())
2008-07-08 02:00:49 +02:00
|| a->message() == "connecting to peer"
|| a->message() == "closing connection to ourself"
|| a->message() == "duplicate connection"
|| a->message() == "duplicate peer-id, connection closed"
|| (allow_disconnects && a->message() == "Broken pipe")
|| (allow_disconnects && a->message() == "Connection reset by peer")
|| (allow_disconnects && a->message() == "End of file."));
a = ses.pop_alert();
}
2009-01-02 10:42:51 +01:00
return ret;
}
2007-06-10 22:46:09 +02:00
void test_sleep(int millisec)
{
libtorrent::sleep(millisec);
}
2007-11-28 01:16:14 +01:00
void stop_web_server(int port)
{
char buf[100];
snprintf(buf, sizeof(buf), "kill `cat ./lighty%d.pid` >/dev/null", port);
system(buf);
2007-11-28 01:16:14 +01:00
}
void start_web_server(int port, bool ssl)
{
2007-11-28 01:16:14 +01:00
stop_web_server(port);
if (ssl)
{
fprintf(stderr, "generating SSL key\n");
system("echo . > tmp");
system("echo test province >>tmp");
system("echo test city >> tmp");
system("echo test company >> tmp");
system("echo test department >> tmp");
system("echo tester >> tmp");
system("echo test@test.com >> tmp");
system("openssl req -new -x509 -keyout server.pem -out server.pem "
"-days 365 -nodes <tmp");
}
error_code ec;
file f("lighty_config", file::write_only, ec);
if (ec)
{
fprintf(stderr, "error writing lighty config file: %s\n", ec.message().c_str());
return;
}
// this requires lighttpd to be built with ssl support.
// The port distribution for mac is not built with ssl
// support by default.
char buf[1024];
int buf_size = snprintf(buf, sizeof(buf),
"server.modules = (\"mod_access\", \"mod_redirect\", \"mod_setenv\")\n"
"server.document-root = \"%s\"\n"
"server.range-requests = \"enable\"\n"
"server.port = %d\n"
"server.pid-file = \"./lighty%d.pid\"\n"
"url.redirect = ("
"\"^/redirect$\" => \"%s://127.0.0.1:%d/test_file\""
", \"^/infinite_redirect$\" => \"%s://127.0.0.1:%d/infinite_redirect\""
", \"^/relative/redirect$\" => \"../test_file\""
")\n"
"$HTTP[\"url\"] == \"/test_file.gz\" {\n"
" setenv.add-response-header = ( \"Content-Encoding\" => \"gzip\" )\n"
"}\n"
"ssl.engine = \"%s\"\n"
"ssl.pemfile = \"server.pem\"\n"
, current_working_directory().c_str(), port, port
, (ssl?"https":"http"), port, (ssl?"https":"http"), port
, (ssl?"enable":"disable"));
file::iovec_t b = { buf, buf_size };
f.writev(0, &b, 1, ec);
if (ec)
{
fprintf(stderr, "error writing lighty config file: %s\n", ec.message().c_str());
return;
}
f.close();
fprintf(stderr, "starting lighty\n\n%s\n\n", buf);
2008-11-14 08:59:54 +01:00
system("lighttpd -f lighty_config 2> lighty.err >lighty.log &");
test_sleep(1000);
}
2007-11-28 01:16:14 +01:00
void stop_proxy(int port)
{
char buf[100];
snprintf(buf, sizeof(buf), "delegated -P%d -Fkill", port);
system(buf);
2007-11-28 01:16:14 +01:00
}
void start_proxy(int port, int proxy_type)
{
using namespace libtorrent;
stop_proxy(port);
char const* type = "";
char const* auth = "";
2007-11-28 01:16:14 +01:00
switch (proxy_type)
{
case proxy_settings::socks4:
type = "socks4";
2007-11-28 01:16:14 +01:00
break;
case proxy_settings::socks5:
type = "socks5";
2007-11-28 01:16:14 +01:00
break;
case proxy_settings::socks5_pw:
type = "socks5";
auth = "AUTHORIZER=-list{testuser:testpass}";
2007-11-28 01:16:14 +01:00
break;
case proxy_settings::http:
type = "http";
2007-11-28 01:16:14 +01:00
break;
case proxy_settings::http_pw:
type = "http";
auth = "AUTHORIZER=-list{testuser:testpass}";
2007-11-28 01:16:14 +01:00
break;
}
char buf[512];
// we need to echo n since dg will ask us to configure it
snprintf(buf, sizeof(buf), "echo n | delegated -P%d ADMIN=test@test.com "
"PERMIT=\"*:*:localhost\" REMITTABLE=+,https RELAY=proxy,delegate "
"SERVER=%s %s"
, port, type, auth);
system(buf);
test_sleep(1000);
}
using namespace libtorrent;
2007-09-01 06:43:13 +02:00
template <class T>
boost::intrusive_ptr<T> clone_ptr(boost::intrusive_ptr<T> const& ptr)
{
return boost::intrusive_ptr<T>(new T(*ptr));
}
boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file, int piece_size, int num_pieces)
{
char const* tracker_url = "http://non-existent-name.com/announce";
// excercise the path when encountering invalid urls
char const* invalid_tracker_url = "http:";
2009-06-23 03:52:44 +02:00
char const* invalid_tracker_protocol = "foo://non/existent-name.com/announce";
file_storage fs;
int total_size = piece_size * num_pieces;
fs.add_file("temporary", total_size);
libtorrent::create_torrent t(fs, piece_size);
t.add_tracker(tracker_url);
t.add_tracker(invalid_tracker_url);
2009-06-23 03:52:44 +02:00
t.add_tracker(invalid_tracker_protocol);
std::vector<char> piece(piece_size);
for (int i = 0; i < int(piece.size()); ++i)
piece[i] = (i % 26) + 'A';
// calculate the hash for all pieces
int num = t.num_pieces();
sha1_hash ph = hasher(&piece[0], piece.size()).final();
for (int i = 0; i < num; ++i)
t.set_hash(i, ph);
if (file)
{
while (total_size > 0)
{
file->write(&piece[0], (std::min)(int(piece.size()), total_size));
total_size -= piece.size();
}
}
std::vector<char> tmp;
std::back_insert_iterator<std::vector<char> > out(tmp);
bencode(out, t.generate());
return boost::intrusive_ptr<torrent_info>(new torrent_info(&tmp[0], tmp.size()));
}
boost::tuple<torrent_handle, torrent_handle, torrent_handle>
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_info>* torrent, bool super_seeding
, add_torrent_params const* p)
{
assert(ses1);
assert(ses2);
2009-04-10 10:40:13 +02:00
session_settings sess_set;
2009-04-10 23:25:37 +02:00
sess_set.allow_multiple_connections_per_ip = true;
2009-04-10 10:40:13 +02:00
sess_set.ignore_limits_on_local_network = false;
ses1->set_settings(sess_set);
ses2->set_settings(sess_set);
if (ses3) ses3->set_settings(sess_set);
ses1->set_alert_mask(~alert::progress_notification);
ses2->set_alert_mask(~alert::progress_notification);
if (ses3) ses3->set_alert_mask(~alert::progress_notification);
2009-04-10 10:40:13 +02:00
2008-10-30 06:31:26 +01:00
std::srand(time(0));
peer_id pid;
std::generate(&pid[0], &pid[0] + 20, std::rand);
ses1->set_peer_id(pid);
std::generate(&pid[0], &pid[0] + 20, std::rand);
ses2->set_peer_id(pid);
assert(ses1->id() != ses2->id());
if (ses3)
2008-10-30 06:31:26 +01:00
{
std::generate(&pid[0], &pid[0] + 20, std::rand);
ses3->set_peer_id(pid);
assert(ses3->id() != ses2->id());
2008-10-30 06:31:26 +01:00
}
boost::intrusive_ptr<torrent_info> t;
if (torrent == 0)
{
error_code ec;
create_directory("./tmp1" + suffix, ec);
std::ofstream file(("./tmp1" + suffix + "/temporary").c_str());
2009-04-10 10:40:13 +02:00
t = ::create_torrent(&file, piece_size, 19);
file.close();
if (clear_files)
{
remove_all("./tmp2" + suffix + "/temporary", ec);
remove_all("./tmp3" + suffix + "/temporary", ec);
}
2009-04-04 23:44:04 +02:00
char ih_hex[41];
to_hex((char const*)&t->info_hash()[0], 20, ih_hex);
std::cerr << "generated torrent: " << ih_hex << " ./tmp1" << suffix << "/temporary" << std::endl;
}
else
{
t = *torrent;
}
// they should not use the same save dir, because the
// file pool will complain if two torrents are trying to
// use the same files
2007-09-01 06:43:13 +02:00
sha1_hash info_hash = t->info_hash();
add_torrent_params param;
if (p) param = *p;
param.ti = clone_ptr(t);
param.save_path = "./tmp1" + suffix;
torrent_handle tor1 = ses1->add_torrent(param);
2008-12-08 07:36:22 +01:00
tor1.super_seeding(super_seeding);
TEST_CHECK(!ses1->get_torrents().empty());
2007-06-06 02:41:20 +02:00
torrent_handle tor2;
torrent_handle tor3;
// the downloader cannot use seed_mode
param.seed_mode = false;
if (ses3)
{
param.ti = clone_ptr(t);
param.save_path = "./tmp3" + suffix;
tor3 = ses3->add_torrent(param);
TEST_CHECK(!ses3->get_torrents().empty());
}
2007-06-06 02:41:20 +02:00
if (use_metadata_transfer)
{
param.ti = 0;
param.info_hash = t->info_hash();
}
2007-06-06 02:41:20 +02:00
else
{
param.ti = clone_ptr(t);
}
param.save_path = "./tmp2" + suffix;
tor2 = ses2->add_torrent(param);
TEST_CHECK(!ses2->get_torrents().empty());
2007-06-10 22:46:09 +02:00
assert(ses1->get_torrents().size() == 1);
assert(ses2->get_torrents().size() == 1);
test_sleep(100);
2007-10-01 04:09:12 +02:00
if (connect_peers)
{
2007-10-01 04:09:12 +02:00
std::cerr << "connecting peer\n";
2008-11-05 04:17:41 +01:00
error_code ec;
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
, ses2->listen_port()));
2007-10-01 04:09:12 +02:00
if (ses3)
{
// give the other peers some time to get an initial
// set of pieces before they start sharing with each-other
tor3.connect_peer(tcp::endpoint(
2008-11-05 04:17:41 +01:00
address::from_string("127.0.0.1", ec)
2007-10-01 04:09:12 +02:00
, ses2->listen_port()));
tor3.connect_peer(tcp::endpoint(
2008-11-05 04:17:41 +01:00
address::from_string("127.0.0.1", ec)
2007-10-01 04:09:12 +02:00
, ses1->listen_port()));
}
}
return boost::make_tuple(tor1, tor2, tor3);
}