2007-09-10 08:12:41 +02:00
|
|
|
#include <fstream>
|
2007-11-27 01:06:59 +01:00
|
|
|
#include <sstream>
|
2007-09-10 08:12:41 +02:00
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2006-10-11 16:02:21 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-01-02 04:18:29 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
using boost::filesystem::remove_all;
|
|
|
|
using boost::filesystem::create_directory;
|
2008-01-02 04:18:29 +01:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
void print_alerts(libtorrent::session& ses, char const* name, bool allow_disconnects, bool allow_no_torrents)
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2008-05-12 09:01:25 +02:00
|
|
|
if (peer_disconnected_alert* p = dynamic_cast<peer_disconnected_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
std::cerr << name << "(" << p->ip << "): " << p->msg() << "\n";
|
|
|
|
}
|
|
|
|
else if (a->msg() != "block downloading"
|
2008-01-05 07:55:17 +01:00
|
|
|
&& a->msg() != "block finished"
|
|
|
|
&& a->msg() != "piece finished")
|
2008-05-12 09:01:25 +02:00
|
|
|
{
|
2008-01-05 07:55:17 +01:00
|
|
|
std::cerr << name << ": " << a->msg() << "\n";
|
2008-05-12 09:01:25 +02:00
|
|
|
}
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(dynamic_cast<peer_error_alert*>(a.get()) == 0
|
|
|
|
|| (!handles.empty() && h.is_seed())
|
|
|
|
|| a->msg() == "connecting to peer"
|
|
|
|
|| a->msg() == "closing connection to ourself"
|
|
|
|
|| a->msg() == "duplicate connection"
|
2008-01-08 06:46:20 +01:00
|
|
|
|| a->msg() == "duplicate peer-id, connection closed"
|
|
|
|
|| (allow_disconnects && a->msg() == "Broken pipe")
|
|
|
|
|| (allow_disconnects && a->msg() == "Connection reset by peer")
|
2008-01-02 04:18:29 +01:00
|
|
|
|| (allow_disconnects && a->msg() == "End of file."));
|
|
|
|
a = ses.pop_alert();
|
|
|
|
}
|
|
|
|
}
|
2006-10-11 16:02:21 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void test_sleep(int millisec)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
|
|
|
boost::xtime xt;
|
|
|
|
boost::xtime_get(&xt, boost::TIME_UTC);
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::uint64_t nanosec = (millisec % 1000) * 1000000 + xt.nsec;
|
|
|
|
int sec = millisec / 1000;
|
|
|
|
if (nanosec > 1000000000)
|
|
|
|
{
|
|
|
|
nanosec -= 1000000000;
|
|
|
|
sec++;
|
|
|
|
}
|
|
|
|
xt.nsec = nanosec;
|
|
|
|
xt.sec += sec;
|
2006-05-28 21:03:54 +02:00
|
|
|
boost::thread::sleep(xt);
|
|
|
|
}
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
void stop_web_server(int port)
|
|
|
|
{
|
|
|
|
std::stringstream cmd;
|
|
|
|
cmd << "kill `cat ./lighty" << port << ".pid` >/dev/null";
|
|
|
|
system(cmd.str().c_str());
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
void start_web_server(int port, bool ssl)
|
2007-11-27 01:06:59 +01:00
|
|
|
{
|
2007-11-28 01:16:14 +01:00
|
|
|
stop_web_server(port);
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
if (ssl)
|
|
|
|
{
|
2008-03-21 06:54:01 +01:00
|
|
|
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");
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream f("lighty_config");
|
2008-01-30 19:32:13 +01:00
|
|
|
f << "server.modules = (\"mod_access\", \"mod_redirect\", \"mod_setenv\")\n"
|
2007-11-27 01:06:59 +01:00
|
|
|
"server.document-root = \"" << boost::filesystem::initial_path().string() << "\"\n"
|
|
|
|
"server.range-requests = \"enable\"\n"
|
|
|
|
"server.port = " << port << "\n"
|
2008-01-12 02:41:56 +01:00
|
|
|
"server.pid-file = \"./lighty" << port << ".pid\"\n"
|
2008-05-18 06:48:06 +02:00
|
|
|
"url.redirect = ("
|
|
|
|
"\"^/redirect$\" => \"" << (ssl?"https":"http") << "://127.0.0.1:" << port << "/test_file\""
|
|
|
|
", \"^/infinite_redirect$\" => \"" << (ssl?"https":"http") << "://127.0.0.1:" << port << "/infinite_redirect\""
|
|
|
|
", \"^/relative/redirect$\" => \"../test_file\""
|
|
|
|
")\n"
|
2008-01-30 19:32:13 +01:00
|
|
|
"$HTTP[\"url\"] == \"/test_file.gz\" {\n"
|
|
|
|
" setenv.add-response-header = ( \"Content-Encoding\" => \"gzip\" )\n"
|
2008-05-14 07:29:42 +02:00
|
|
|
"# mimetype.assign = ()\n"
|
2008-01-30 19:32:13 +01:00
|
|
|
"}\n";
|
2008-01-27 23:39:50 +01:00
|
|
|
// this requires lighttpd to be built with ssl support.
|
|
|
|
// The port distribution for mac is not built with ssl
|
|
|
|
// support by default.
|
|
|
|
if (ssl)
|
|
|
|
f << "ssl.engine = \"enable\"\n"
|
|
|
|
"ssl.pemfile = \"server.pem\"\n";
|
2007-11-27 01:06:59 +01:00
|
|
|
f.close();
|
|
|
|
|
|
|
|
system("lighttpd -f lighty_config &");
|
|
|
|
test_sleep(1000);
|
|
|
|
}
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
void stop_proxy(int port)
|
2007-11-27 01:06:59 +01:00
|
|
|
{
|
|
|
|
std::stringstream cmd;
|
2007-11-28 01:16:14 +01:00
|
|
|
cmd << "delegated -P" << port << " -Fkill";
|
|
|
|
system(cmd.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_proxy(int port, int proxy_type)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
stop_proxy(port);
|
|
|
|
std::stringstream cmd;
|
|
|
|
// we need to echo n since dg will ask us to configure it
|
2008-01-27 23:39:50 +01:00
|
|
|
cmd << "echo n | delegated -P" << port << " ADMIN=test@test.com "
|
|
|
|
"PERMIT=\"*:*:localhost\" REMITTABLE=+,https RELAY=proxy,delegate";
|
2007-11-28 01:16:14 +01:00
|
|
|
switch (proxy_type)
|
|
|
|
{
|
|
|
|
case proxy_settings::socks4:
|
|
|
|
cmd << " SERVER=socks4";
|
|
|
|
break;
|
|
|
|
case proxy_settings::socks5:
|
|
|
|
cmd << " SERVER=socks5";
|
|
|
|
break;
|
|
|
|
case proxy_settings::socks5_pw:
|
|
|
|
cmd << " SERVER=socks5 AUTHORIZER=-list{testuser:testpass}";
|
|
|
|
break;
|
|
|
|
case proxy_settings::http:
|
|
|
|
cmd << " SERVER=http";
|
|
|
|
break;
|
|
|
|
case proxy_settings::http_pw:
|
|
|
|
cmd << " SERVER=http AUTHORIZER=-list{testuser:testpass}";
|
|
|
|
break;
|
|
|
|
}
|
2007-11-27 01:06:59 +01:00
|
|
|
system(cmd.str().c_str());
|
2008-01-27 23:39:50 +01:00
|
|
|
test_sleep(1000);
|
2007-11-27 01:06:59 +01:00
|
|
|
}
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
|
|
|
char const* tracker_url = "http://non-existent-name.com/announce";
|
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
2007-06-09 01:02:31 +02:00
|
|
|
int total_size = 2 * 1024 * 1024;
|
2008-05-28 10:44:40 +02:00
|
|
|
fs.add_file(path("temporary"), total_size);
|
|
|
|
libtorrent::create_torrent t(fs, 16 * 1024);
|
2008-05-14 07:29:42 +02:00
|
|
|
t.add_tracker(tracker_url);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
std::vector<char> piece(16 * 1024);
|
2007-07-03 01:48:06 +02:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2007-06-20 20:41:53 +02:00
|
|
|
piece[i] = (i % 26) + 'A';
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2008-05-14 07:29:42 +02:00
|
|
|
int num = t.num_pieces();
|
2007-06-09 01:02:31 +02:00
|
|
|
sha1_hash ph = hasher(&piece[0], piece.size()).final();
|
2006-05-28 21:03:54 +02:00
|
|
|
for (int i = 0; i < num; ++i)
|
2008-05-14 07:29:42 +02:00
|
|
|
t.set_hash(i, ph);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
while (total_size > 0)
|
|
|
|
{
|
|
|
|
file->write(&piece[0], (std::min)(int(piece.size()), total_size));
|
|
|
|
total_size -= piece.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
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()));
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::tuple<torrent_handle, torrent_handle, torrent_handle>
|
|
|
|
setup_transfer(session* ses1, session* ses2, session* ses3
|
2007-12-30 10:36:01 +01:00
|
|
|
, bool clear_files, bool use_metadata_transfer, bool connect_peers
|
|
|
|
, std::string suffix)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
|
|
assert(ses1);
|
|
|
|
assert(ses2);
|
|
|
|
|
|
|
|
assert(ses1->id() != ses2->id());
|
|
|
|
if (ses3)
|
|
|
|
assert(ses3->id() != ses2->id());
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-12-30 10:36:01 +01:00
|
|
|
create_directory("./tmp1" + suffix);
|
|
|
|
std::ofstream file(("./tmp1" + suffix + "/temporary").c_str());
|
2008-05-14 07:29:42 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file);
|
2006-05-28 21:03:54 +02:00
|
|
|
file.close();
|
2007-12-30 10:36:01 +01:00
|
|
|
if (clear_files)
|
|
|
|
{
|
|
|
|
remove_all("./tmp2" + suffix + "/temporary");
|
|
|
|
remove_all("./tmp3" + suffix + "/temporary");
|
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-09-01 06:43:13 +02:00
|
|
|
std::cerr << "generated torrent: " << t->info_hash() << std::endl;
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
ses1->set_severity_level(alert::debug);
|
|
|
|
ses2->set_severity_level(alert::debug);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// 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();
|
2007-12-30 10:36:01 +01:00
|
|
|
torrent_handle tor1 = ses1->add_torrent(clone_ptr(t), "./tmp1" + suffix);
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(!ses1->get_torrents().empty());
|
2007-06-06 02:41:20 +02:00
|
|
|
torrent_handle tor2;
|
2007-06-09 01:02:31 +02:00
|
|
|
torrent_handle tor3;
|
2008-01-02 04:18:29 +01:00
|
|
|
if (ses3)
|
|
|
|
{
|
|
|
|
tor3 = ses3->add_torrent(clone_ptr(t), "./tmp3" + suffix);
|
|
|
|
TEST_CHECK(!ses3->get_torrents().empty());
|
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (use_metadata_transfer)
|
2007-10-04 11:32:09 +02:00
|
|
|
tor2 = ses2->add_torrent("http://non-existent-name.com/announce"
|
2007-12-30 10:36:01 +01:00
|
|
|
, t->info_hash(), 0, "./tmp2" + suffix);
|
2007-06-06 02:41:20 +02:00
|
|
|
else
|
2007-12-30 10:36:01 +01:00
|
|
|
tor2 = ses2->add_torrent(clone_ptr(t), "./tmp2" + suffix);
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(!ses2->get_torrents().empty());
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
assert(ses1->get_torrents().size() == 1);
|
|
|
|
assert(ses2->get_torrents().size() == 1);
|
|
|
|
|
|
|
|
test_sleep(100);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-10-01 04:09:12 +02:00
|
|
|
if (connect_peers)
|
2007-06-09 01:02:31 +02:00
|
|
|
{
|
2007-10-01 04:09:12 +02:00
|
|
|
std::cerr << "connecting peer\n";
|
|
|
|
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1")
|
2007-06-09 01:02:31 +02:00
|
|
|
, 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(
|
|
|
|
address::from_string("127.0.0.1")
|
|
|
|
, ses2->listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1")
|
|
|
|
, ses1->listen_port()));
|
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
return boost::make_tuple(tor1, tor2, tor3);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
|
|
|
|