fixed web server tests to not require a fixed port. Should be much more likely to pass now
This commit is contained in:
@@ -341,7 +341,7 @@ boost::shared_ptr<libtorrent::thread> web_server;
|
|||||||
libtorrent::mutex web_lock;
|
libtorrent::mutex web_lock;
|
||||||
libtorrent::condition web_initialized;
|
libtorrent::condition web_initialized;
|
||||||
|
|
||||||
void stop_web_server(int port)
|
void stop_web_server()
|
||||||
{
|
{
|
||||||
if (web_server && web_ios)
|
if (web_server && web_ios)
|
||||||
{
|
{
|
||||||
@@ -352,19 +352,20 @@ void stop_web_server(int port)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void web_server_thread(int port, bool ssl);
|
void web_server_thread(int* port, bool ssl);
|
||||||
|
|
||||||
void start_web_server(int port, bool ssl)
|
int start_web_server(bool ssl)
|
||||||
{
|
{
|
||||||
stop_web_server(port);
|
stop_web_server();
|
||||||
|
|
||||||
{
|
{
|
||||||
mutex::scoped_lock l(web_lock);
|
mutex::scoped_lock l(web_lock);
|
||||||
web_initialized.clear(l);
|
web_initialized.clear(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "starting web server on port %d\n", port);
|
int port = 0;
|
||||||
web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, port, ssl)));
|
|
||||||
|
web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, &port, ssl)));
|
||||||
|
|
||||||
{
|
{
|
||||||
mutex::scoped_lock l(web_lock);
|
mutex::scoped_lock l(web_lock);
|
||||||
@@ -376,6 +377,7 @@ void start_web_server(int port, bool ssl)
|
|||||||
error_code ec;
|
error_code ec;
|
||||||
create_directory("relative", ec);
|
create_directory("relative", ec);
|
||||||
test_sleep(100);
|
test_sleep(100);
|
||||||
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_response(stream_socket& s, error_code& ec
|
void send_response(stream_socket& s, error_code& ec
|
||||||
@@ -409,7 +411,7 @@ void on_accept(error_code const& ec)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void web_server_thread(int port, bool ssl)
|
void web_server_thread(int* port, bool ssl)
|
||||||
{
|
{
|
||||||
// TODO: support SSL
|
// TODO: support SSL
|
||||||
|
|
||||||
@@ -432,14 +434,15 @@ void web_server_thread(int port, bool ssl)
|
|||||||
web_initialized.signal(l);
|
web_initialized.signal(l);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
acceptor.bind(tcp::endpoint(address_v4::any(), port), ec);
|
acceptor.bind(tcp::endpoint(address_v4::any(), 0), ec);
|
||||||
if (ec)
|
if (ec)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error binding listen socket to port %d: %s\n", port, ec.message().c_str());
|
fprintf(stderr, "Error binding listen socket to port 0: %s\n", ec.message().c_str());
|
||||||
mutex::scoped_lock l(web_lock);
|
mutex::scoped_lock l(web_lock);
|
||||||
web_initialized.signal(l);
|
web_initialized.signal(l);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
*port = acceptor.local_endpoint().port();
|
||||||
acceptor.listen(10, ec);
|
acceptor.listen(10, ec);
|
||||||
if (ec)
|
if (ec)
|
||||||
{
|
{
|
||||||
@@ -451,7 +454,7 @@ void web_server_thread(int port, bool ssl)
|
|||||||
|
|
||||||
web_ios = &ios;
|
web_ios = &ios;
|
||||||
|
|
||||||
fprintf(stderr, "web server initialized on port %d\n", port);
|
fprintf(stderr, "web server initialized on port %d\n", *port);
|
||||||
|
|
||||||
{
|
{
|
||||||
mutex::scoped_lock l(web_lock);
|
mutex::scoped_lock l(web_lock);
|
||||||
@@ -590,7 +593,11 @@ void web_server_thread(int port, bool ssl)
|
|||||||
snprintf(eh, sizeof(eh), "%sContent-Range: bytes %d-%d\r\n"
|
snprintf(eh, sizeof(eh), "%sContent-Range: bytes %d-%d\r\n"
|
||||||
, extra_header ? extra_header : "", start, end);
|
, extra_header ? extra_header : "", start, end);
|
||||||
send_response(s, ec, 206, "Partial", eh, end - start + 1);
|
send_response(s, ec, 206, "Partial", eh, end - start + 1);
|
||||||
write(s, boost::asio::buffer(&file_buf[0] + start, end - start + 1), boost::asio::transfer_all(), ec);
|
if (!file_buf.empty())
|
||||||
|
{
|
||||||
|
write(s, boost::asio::buffer(&file_buf[0] + start, end - start + 1)
|
||||||
|
, boost::asio::transfer_all(), ec);
|
||||||
|
}
|
||||||
// fprintf(stderr, "send %d bytes of payload\n", end - start + 1);
|
// fprintf(stderr, "send %d bytes of payload\n", end - start + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@@ -61,8 +61,8 @@ setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2
|
|||||||
, boost::intrusive_ptr<libtorrent::torrent_info>* torrent = 0, bool super_seeding = false
|
, boost::intrusive_ptr<libtorrent::torrent_info>* torrent = 0, bool super_seeding = false
|
||||||
, libtorrent::add_torrent_params const* p = 0);
|
, libtorrent::add_torrent_params const* p = 0);
|
||||||
|
|
||||||
void start_web_server(int port, bool ssl = false);
|
int start_web_server(bool ssl = false);
|
||||||
void stop_web_server(int port);
|
void stop_web_server();
|
||||||
void start_proxy(int port, int type);
|
void start_proxy(int port, int type);
|
||||||
void stop_proxy(int port);
|
void stop_proxy(int port);
|
||||||
|
|
||||||
|
@@ -132,7 +132,7 @@ void run_test(std::string const& url, int size, int status, int connected
|
|||||||
TEST_CHECK(http_status == status || status == -1);
|
TEST_CHECK(http_status == status || status == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_suite(std::string const& protocol, proxy_settings const& ps)
|
void run_suite(std::string const& protocol, proxy_settings const& ps, int port)
|
||||||
{
|
{
|
||||||
if (ps.type != proxy_settings::none)
|
if (ps.type != proxy_settings::none)
|
||||||
{
|
{
|
||||||
@@ -147,12 +147,16 @@ void run_suite(std::string const& protocol, proxy_settings const& ps)
|
|||||||
// this requires the hosts file to be modified
|
// this requires the hosts file to be modified
|
||||||
// run_test(protocol + "://test.dns.ts:8001/test_file", 3216, 200, 1, error_code(), ps);
|
// run_test(protocol + "://test.dns.ts:8001/test_file", 3216, 200, 1, error_code(), ps);
|
||||||
|
|
||||||
run_test(protocol + "://127.0.0.1:8001/relative/redirect", 3216, 200, 2, error_code(), ps);
|
char url[256];
|
||||||
run_test(protocol + "://127.0.0.1:8001/redirect", 3216, 200, 2, error_code(), ps);
|
snprintf(url, sizeof(url), "%s://127.0.0.1:%d/", protocol.c_str(), port);
|
||||||
run_test(protocol + "://127.0.0.1:8001/infinite_redirect", 0, 301, 6, error_code(), ps);
|
std::string url_base(url);
|
||||||
run_test(protocol + "://127.0.0.1:8001/test_file", 3216, 200, 1, error_code(), ps);
|
|
||||||
run_test(protocol + "://127.0.0.1:8001/test_file.gz", 3216, 200, 1, error_code(), ps);
|
run_test(url_base + "relative/redirect", 3216, 200, 2, error_code(), ps);
|
||||||
run_test(protocol + "://127.0.0.1:8001/non-existing-file", -1, 404, 1, err(), ps);
|
run_test(url_base + "redirect", 3216, 200, 2, error_code(), ps);
|
||||||
|
run_test(url_base + "infinite_redirect", 0, 301, 6, error_code(), ps);
|
||||||
|
run_test(url_base + "test_file", 3216, 200, 1, error_code(), ps);
|
||||||
|
run_test(url_base + "test_file.gz", 3216, 200, 1, error_code(), ps);
|
||||||
|
run_test(url_base + "non-existing-file", -1, 404, 1, err(), ps);
|
||||||
// if we're going through an http proxy, we won't get the same error as if the hostname
|
// if we're going through an http proxy, we won't get the same error as if the hostname
|
||||||
// resolution failed
|
// resolution failed
|
||||||
if ((ps.type == proxy_settings::http || ps.type == proxy_settings::http_pw) && protocol != "https")
|
if ((ps.type == proxy_settings::http || ps.type == proxy_settings::http_pw) && protocol != "https")
|
||||||
@@ -185,22 +189,22 @@ int test_main()
|
|||||||
ps.username = "testuser";
|
ps.username = "testuser";
|
||||||
ps.password = "testpass";
|
ps.password = "testpass";
|
||||||
|
|
||||||
start_web_server(8001);
|
int port = start_web_server();
|
||||||
for (int i = 0; i < 5; ++i)
|
for (int i = 0; i < 5; ++i)
|
||||||
{
|
{
|
||||||
ps.type = (proxy_settings::proxy_type)i;
|
ps.type = (proxy_settings::proxy_type)i;
|
||||||
run_suite("http", ps);
|
run_suite("http", ps, port);
|
||||||
}
|
}
|
||||||
stop_web_server(8001);
|
stop_web_server();
|
||||||
|
|
||||||
#ifdef TORRENT_USE_OPENSSL
|
#ifdef TORRENT_USE_OPENSSL
|
||||||
start_web_server(8001, true);
|
port = start_web_server(true);
|
||||||
for (int i = 0; i < 5; ++i)
|
for (int i = 0; i < 5; ++i)
|
||||||
{
|
{
|
||||||
ps.type = (proxy_settings::proxy_type)i;
|
ps.type = (proxy_settings::proxy_type)i;
|
||||||
run_suite("https", ps);
|
run_suite("https", ps, port);
|
||||||
}
|
}
|
||||||
stop_web_server(8001);
|
stop_web_server();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::remove("test_file");
|
std::remove("test_file");
|
||||||
|
@@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
broadcast_socket* sock = 0;
|
broadcast_socket* sock = 0;
|
||||||
|
int g_port = 0;
|
||||||
|
|
||||||
char upnp_xml[] =
|
char upnp_xml[] =
|
||||||
"<root>"
|
"<root>"
|
||||||
@@ -52,7 +53,7 @@ char upnp_xml[] =
|
|||||||
"<major>1</major>"
|
"<major>1</major>"
|
||||||
"<minor>0</minor>"
|
"<minor>0</minor>"
|
||||||
"</specVersion>"
|
"</specVersion>"
|
||||||
"<URLBase>http://127.0.0.1:8888</URLBase>"
|
"<URLBase>http://127.0.0.1:%d</URLBase>"
|
||||||
"<device>"
|
"<device>"
|
||||||
"<deviceType>"
|
"<deviceType>"
|
||||||
"urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
"urn:schemas-upnp-org:device:InternetGatewayDevice:1"
|
||||||
@@ -159,14 +160,19 @@ void incoming_msearch(udp::endpoint const& from, char* buffer
|
|||||||
char msg[] = "HTTP/1.1 200 OK\r\n"
|
char msg[] = "HTTP/1.1 200 OK\r\n"
|
||||||
"ST:upnp:rootdevice\r\n"
|
"ST:upnp:rootdevice\r\n"
|
||||||
"USN:uuid:000f-66d6-7296000099dc::upnp:rootdevice\r\n"
|
"USN:uuid:000f-66d6-7296000099dc::upnp:rootdevice\r\n"
|
||||||
"Location: http://127.0.0.1:8888/upnp.xml\r\n"
|
"Location: http://127.0.0.1:%d/upnp.xml\r\n"
|
||||||
"Server: Custom/1.0 UPnP/1.0 Proc/Ver\r\n"
|
"Server: Custom/1.0 UPnP/1.0 Proc/Ver\r\n"
|
||||||
"EXT:\r\n"
|
"EXT:\r\n"
|
||||||
"Cache-Control:max-age=180\r\n"
|
"Cache-Control:max-age=180\r\n"
|
||||||
"DATE: Fri, 02 Jan 1970 08:10:38 GMT\r\n\r\n";
|
"DATE: Fri, 02 Jan 1970 08:10:38 GMT\r\n\r\n";
|
||||||
|
|
||||||
|
TORRENT_ASSERT(g_port != 0);
|
||||||
|
char buf[sizeof(msg) + 30];
|
||||||
|
int len = snprintf(buf, sizeof(buf), msg, g_port);
|
||||||
|
|
||||||
error_code ec;
|
error_code ec;
|
||||||
sock->send(msg, sizeof(msg)-1, ec);
|
sock->send(buf, len, ec);
|
||||||
|
|
||||||
if (ec) std::cerr << "*** error sending " << ec.message() << std::endl;
|
if (ec) std::cerr << "*** error sending " << ec.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,12 +206,12 @@ int test_main()
|
|||||||
{
|
{
|
||||||
libtorrent::io_service ios;
|
libtorrent::io_service ios;
|
||||||
|
|
||||||
start_web_server(8888);
|
g_port = start_web_server();
|
||||||
std::ofstream xml("upnp.xml", std::ios::trunc);
|
FILE* xml_file = fopen("upnp.xml", "w+");
|
||||||
xml.write(upnp_xml, sizeof(upnp_xml) - 1);
|
fprintf(xml_file, upnp_xml, g_port);
|
||||||
xml.close();
|
fclose(xml_file);
|
||||||
|
|
||||||
xml.open("WANIPConnection", std::ios::trunc);
|
std::ofstream xml("WANIPConnection", std::ios::trunc);
|
||||||
xml.write(soap_add_response, sizeof(soap_add_response)-1);
|
xml.write(soap_add_response, sizeof(soap_add_response)-1);
|
||||||
xml.close();
|
xml.close();
|
||||||
|
|
||||||
@@ -252,7 +258,7 @@ int test_main()
|
|||||||
TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected1) == 1);
|
TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected1) == 1);
|
||||||
TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected2) == 1);
|
TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected2) == 1);
|
||||||
|
|
||||||
stop_web_server(8888);
|
stop_web_server();
|
||||||
|
|
||||||
delete sock;
|
delete sock;
|
||||||
return 0;
|
return 0;
|
||||||
|
@@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
|
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
|
||||||
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
|
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy, int port)
|
||||||
{
|
{
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
@@ -171,22 +171,24 @@ int test_main()
|
|||||||
file_storage fs;
|
file_storage fs;
|
||||||
add_files(fs, "./tmp1_web_seed/test_torrent_dir");
|
add_files(fs, "./tmp1_web_seed/test_torrent_dir");
|
||||||
|
|
||||||
libtorrent::create_torrent t(fs, 16 * 1024);
|
int port = start_web_server();
|
||||||
t.add_url_seed("http://127.0.0.1:8000/tmp1_web_seed");
|
|
||||||
|
|
||||||
start_web_server(8000);
|
libtorrent::create_torrent t(fs, 16 * 1024);
|
||||||
|
char tmp[512];
|
||||||
|
snprintf(tmp, sizeof(tmp), "http://127.0.0.1:%d/tmp1_web_seed", port);
|
||||||
|
t.add_url_seed(tmp);
|
||||||
|
|
||||||
// calculate the hash for all pieces
|
// calculate the hash for all pieces
|
||||||
set_piece_hashes(t, "./tmp1_web_seed", ec);
|
set_piece_hashes(t, "./tmp1_web_seed", ec);
|
||||||
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
|
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
|
||||||
|
|
||||||
for (int i = 0; i < 6; ++i)
|
for (int i = 0; i < 6; ++i)
|
||||||
test_transfer(torrent_file, i);
|
test_transfer(torrent_file, i, port);
|
||||||
|
|
||||||
torrent_file->rename_file(0, "./tmp2_web_seed/test_torrent_dir/renamed_test1");
|
torrent_file->rename_file(0, "./tmp2_web_seed/test_torrent_dir/renamed_test1");
|
||||||
test_transfer(torrent_file, 0);
|
test_transfer(torrent_file, 0, port);
|
||||||
|
|
||||||
stop_web_server(8000);
|
stop_web_server();
|
||||||
remove_all("./tmp1_web_seed", ec);
|
remove_all("./tmp1_web_seed", ec);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user