fixed web server tests to not require a fixed port. Should be much more likely to pass now

This commit is contained in:
Arvid Norberg 2010-01-23 23:57:11 +00:00
parent 942c4e0a7c
commit a47c40d7fa
5 changed files with 61 additions and 42 deletions

View File

@ -341,7 +341,7 @@ boost::shared_ptr<libtorrent::thread> web_server;
libtorrent::mutex web_lock;
libtorrent::condition web_initialized;
void stop_web_server(int port)
void stop_web_server()
{
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);
web_initialized.clear(l);
}
fprintf(stderr, "starting web server on port %d\n", port);
web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, port, ssl)));
int port = 0;
web_server.reset(new libtorrent::thread(boost::bind(&web_server_thread, &port, ssl)));
{
mutex::scoped_lock l(web_lock);
@ -376,6 +377,7 @@ void start_web_server(int port, bool ssl)
error_code ec;
create_directory("relative", ec);
test_sleep(100);
return port;
}
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
@ -432,14 +434,15 @@ void web_server_thread(int port, bool ssl)
web_initialized.signal(l);
return;
}
acceptor.bind(tcp::endpoint(address_v4::any(), port), ec);
acceptor.bind(tcp::endpoint(address_v4::any(), 0), 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);
web_initialized.signal(l);
return;
}
*port = acceptor.local_endpoint().port();
acceptor.listen(10, ec);
if (ec)
{
@ -451,7 +454,7 @@ void web_server_thread(int port, bool ssl)
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);
@ -590,7 +593,11 @@ void web_server_thread(int port, bool ssl)
snprintf(eh, sizeof(eh), "%sContent-Range: bytes %d-%d\r\n"
, extra_header ? extra_header : "", start, end);
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);
}
else

View File

@ -61,8 +61,8 @@ setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2
, boost::intrusive_ptr<libtorrent::torrent_info>* torrent = 0, bool super_seeding = false
, libtorrent::add_torrent_params const* p = 0);
void start_web_server(int port, bool ssl = false);
void stop_web_server(int port);
int start_web_server(bool ssl = false);
void stop_web_server();
void start_proxy(int port, int type);
void stop_proxy(int port);

View File

@ -132,7 +132,7 @@ void run_test(std::string const& url, int size, int status, int connected
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)
{
@ -147,12 +147,16 @@ void run_suite(std::string const& protocol, proxy_settings const& ps)
// 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 + "://127.0.0.1:8001/relative/redirect", 3216, 200, 2, error_code(), ps);
run_test(protocol + "://127.0.0.1:8001/redirect", 3216, 200, 2, error_code(), ps);
run_test(protocol + "://127.0.0.1:8001/infinite_redirect", 0, 301, 6, error_code(), ps);
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(protocol + "://127.0.0.1:8001/non-existing-file", -1, 404, 1, err(), ps);
char url[256];
snprintf(url, sizeof(url), "%s://127.0.0.1:%d/", protocol.c_str(), port);
std::string url_base(url);
run_test(url_base + "relative/redirect", 3216, 200, 2, error_code(), 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
// resolution failed
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.password = "testpass";
start_web_server(8001);
int port = start_web_server();
for (int i = 0; i < 5; ++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
start_web_server(8001, true);
port = start_web_server(true);
for (int i = 0; i < 5; ++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
std::remove("test_file");

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace libtorrent;
broadcast_socket* sock = 0;
int g_port = 0;
char upnp_xml[] =
"<root>"
@ -52,7 +53,7 @@ char upnp_xml[] =
"<major>1</major>"
"<minor>0</minor>"
"</specVersion>"
"<URLBase>http://127.0.0.1:8888</URLBase>"
"<URLBase>http://127.0.0.1:%d</URLBase>"
"<device>"
"<deviceType>"
"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"
"ST: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"
"EXT:\r\n"
"Cache-Control:max-age=180\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;
sock->send(msg, sizeof(msg)-1, ec);
sock->send(buf, len, ec);
if (ec) std::cerr << "*** error sending " << ec.message() << std::endl;
}
@ -200,12 +206,12 @@ int test_main()
{
libtorrent::io_service ios;
start_web_server(8888);
std::ofstream xml("upnp.xml", std::ios::trunc);
xml.write(upnp_xml, sizeof(upnp_xml) - 1);
xml.close();
g_port = start_web_server();
FILE* xml_file = fopen("upnp.xml", "w+");
fprintf(xml_file, upnp_xml, g_port);
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.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(), expected2) == 1);
stop_web_server(8888);
stop_web_server();
delete sock;
return 0;

View File

@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace libtorrent;
// 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;
@ -171,22 +171,24 @@ int test_main()
file_storage fs;
add_files(fs, "./tmp1_web_seed/test_torrent_dir");
libtorrent::create_torrent t(fs, 16 * 1024);
t.add_url_seed("http://127.0.0.1:8000/tmp1_web_seed");
int port = start_web_server();
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
set_piece_hashes(t, "./tmp1_web_seed", ec);
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
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");
test_transfer(torrent_file, 0);
test_transfer(torrent_file, 0, port);
stop_web_server(8000);
stop_web_server();
remove_all("./tmp1_web_seed", ec);
return 0;
}