forked from premiere/premiere-libtorrent
made test_http_connection use lighty as well (the test is more self contained)
This commit is contained in:
parent
254e4ffce4
commit
8fc9f9e842
|
@ -1,4 +1,5 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "libtorrent/session.hpp"
|
#include "libtorrent/session.hpp"
|
||||||
#include "libtorrent/hasher.hpp"
|
#include "libtorrent/hasher.hpp"
|
||||||
|
@ -30,6 +31,27 @@ void test_sleep(int millisec)
|
||||||
boost::thread::sleep(xt);
|
boost::thread::sleep(xt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void start_web_server(int port)
|
||||||
|
{
|
||||||
|
std::ofstream f("./lighty_config");
|
||||||
|
f << "server.modules = (\"mod_access\")\n"
|
||||||
|
"server.document-root = \"" << boost::filesystem::initial_path().string() << "\"\n"
|
||||||
|
"server.range-requests = \"enable\"\n"
|
||||||
|
"server.port = " << port << "\n"
|
||||||
|
"server.pid-file = \"./lighty" << port << ".pid\"\n";
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
system("lighttpd -f lighty_config &");
|
||||||
|
test_sleep(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop_web_server(int port)
|
||||||
|
{
|
||||||
|
std::stringstream cmd;
|
||||||
|
cmd << "kill `cat ./lighty" << port << ".pid`";
|
||||||
|
system(cmd.str().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|
|
@ -15,5 +15,8 @@ setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2
|
||||||
, libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true
|
, libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true
|
||||||
, bool connect = true);
|
, bool connect = true);
|
||||||
|
|
||||||
|
void start_web_server(int port);
|
||||||
|
void stop_web_server(int port);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
#include "libtorrent/socket.hpp"
|
#include "libtorrent/socket.hpp"
|
||||||
#include "libtorrent/connection_queue.hpp"
|
#include "libtorrent/connection_queue.hpp"
|
||||||
#include "libtorrent/http_connection.hpp"
|
#include "libtorrent/http_connection.hpp"
|
||||||
|
#include "setup_transfer.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
|
||||||
|
@ -13,17 +17,17 @@ int handler_called = 0;
|
||||||
int data_size = 0;
|
int data_size = 0;
|
||||||
int http_status = 0;
|
int http_status = 0;
|
||||||
asio::error_code error_code;
|
asio::error_code error_code;
|
||||||
|
char data_buffer[4000];
|
||||||
|
|
||||||
void print_http_header(http_parser const& p)
|
void print_http_header(http_parser const& p)
|
||||||
{
|
{
|
||||||
std::cerr << p.status_code() << " " << p.message() << std::endl;
|
std::cerr << " < " << p.status_code() << " " << p.message() << std::endl;
|
||||||
|
|
||||||
for (std::map<std::string, std::string>::const_iterator i
|
for (std::map<std::string, std::string>::const_iterator i
|
||||||
= p.headers().begin(), end(p.headers().end()); i != end; ++i)
|
= p.headers().begin(), end(p.headers().end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
std::cerr << i->first << ": " << i->second << std::endl;
|
std::cerr << " < " << i->first << ": " << i->second << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_connect_handler(http_connection& c)
|
void http_connect_handler(http_connection& c)
|
||||||
|
@ -40,7 +44,14 @@ void http_handler(asio::error_code const& ec, http_parser const& parser, char co
|
||||||
data_size = size;
|
data_size = size;
|
||||||
error_code = ec;
|
error_code = ec;
|
||||||
|
|
||||||
if (parser.header_finished()) http_status = parser.status_code();
|
if (parser.header_finished())
|
||||||
|
{
|
||||||
|
http_status = parser.status_code();
|
||||||
|
if (http_status == 200)
|
||||||
|
{
|
||||||
|
TEST_CHECK(memcmp(data, data_buffer, size) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
print_http_header(parser);
|
print_http_header(parser);
|
||||||
|
|
||||||
cq.close();
|
cq.close();
|
||||||
|
@ -55,7 +66,7 @@ void reset_globals()
|
||||||
error_code = asio::error_code();
|
error_code = asio::error_code();
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_test(char const* url, int size, int status, int connected, asio::error_code const& ec)
|
void run_test(char const* url, int size, int status, int connected, boost::optional<asio::error_code> ec)
|
||||||
{
|
{
|
||||||
reset_globals();
|
reset_globals();
|
||||||
|
|
||||||
|
@ -75,15 +86,22 @@ void run_test(char const* url, int size, int status, int connected, asio::error_
|
||||||
TEST_CHECK(connect_handler_called == connected);
|
TEST_CHECK(connect_handler_called == connected);
|
||||||
TEST_CHECK(handler_called == 1);
|
TEST_CHECK(handler_called == 1);
|
||||||
TEST_CHECK(data_size == size || size == -1);
|
TEST_CHECK(data_size == size || size == -1);
|
||||||
TEST_CHECK(error_code == ec);
|
TEST_CHECK(!ec || error_code == ec);
|
||||||
TEST_CHECK(http_status == status || status == -1);
|
TEST_CHECK(http_status == status || status == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_main()
|
int test_main()
|
||||||
{
|
{
|
||||||
run_test("http://127.0.0.1/disk_io.png", 17809, 200, 1, asio::error_code());
|
typedef boost::optional<asio::error_code> err;
|
||||||
run_test("http://127.0.0.1/non-existing-file", -1, 404, 1, asio::error::eof);
|
start_web_server(8001);
|
||||||
run_test("http://non-existent-domain.se/non-existing-file", -1, -1, 0, asio::error::host_not_found);
|
std::srand(std::time(0));
|
||||||
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
||||||
|
std::ofstream("test_file").write(data_buffer, 3216);
|
||||||
|
run_test("http://127.0.0.1:8001/test_file", 3216, 200, 1, asio::error_code());
|
||||||
|
run_test("http://127.0.0.1:8001/non-existing-file", -1, 404, 1, err());
|
||||||
|
run_test("http://non-existent-domain.se/non-existing-file", -1, -1, 0, err(asio::error::host_not_found));
|
||||||
|
stop_web_server(8001);
|
||||||
|
std::remove("test_file");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,24 +33,6 @@ void add_files(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_web_server()
|
|
||||||
{
|
|
||||||
std::ofstream f("./lighty_config");
|
|
||||||
f << "server.modules = (\"mod_access\")\n"
|
|
||||||
"server.document-root = \"" << initial_path().string() << "\"\n"
|
|
||||||
"server.range-requests = \"enable\"\n"
|
|
||||||
"server.port = 8000\n"
|
|
||||||
"server.pid-file = \"./lighty.pid\"\n";
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
system("lighttpd -f lighty_config &");
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop_web_server()
|
|
||||||
{
|
|
||||||
system("kill `cat ./lighty.pid`");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_transfer()
|
void test_transfer()
|
||||||
{
|
{
|
||||||
using namespace libtorrent;
|
using namespace libtorrent;
|
||||||
|
@ -72,7 +54,7 @@ void test_transfer()
|
||||||
|
|
||||||
add_files(*torrent_file, complete("."), "test_torrent");
|
add_files(*torrent_file, complete("."), "test_torrent");
|
||||||
|
|
||||||
start_web_server();
|
start_web_server(8000);
|
||||||
|
|
||||||
file_pool fp;
|
file_pool fp;
|
||||||
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
|
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
|
||||||
|
@ -115,7 +97,7 @@ void test_transfer()
|
||||||
TEST_CHECK(th.is_seed());
|
TEST_CHECK(th.is_seed());
|
||||||
|
|
||||||
remove_all("./test_torrent");
|
remove_all("./test_torrent");
|
||||||
stop_web_server();
|
stop_web_server(8000);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_main()
|
int test_main()
|
||||||
|
|
Loading…
Reference in New Issue