diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 1d246d604..633a83343 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -1,4 +1,5 @@ #include +#include #include "libtorrent/session.hpp" #include "libtorrent/hasher.hpp" @@ -30,6 +31,27 @@ void test_sleep(int millisec) 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; template diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 6b9454b20..77ff8eb5b 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -15,5 +15,8 @@ setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2 , libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true , bool connect = true); +void start_web_server(int port); +void stop_web_server(int port); + #endif diff --git a/test/test_http_connection.cpp b/test/test_http_connection.cpp index a65fd9084..9bee8c112 100644 --- a/test/test_http_connection.cpp +++ b/test/test_http_connection.cpp @@ -2,6 +2,10 @@ #include "libtorrent/socket.hpp" #include "libtorrent/connection_queue.hpp" #include "libtorrent/http_connection.hpp" +#include "setup_transfer.hpp" + +#include +#include using namespace libtorrent; @@ -13,17 +17,17 @@ int handler_called = 0; int data_size = 0; int http_status = 0; asio::error_code error_code; +char data_buffer[4000]; 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::const_iterator 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) @@ -40,7 +44,14 @@ void http_handler(asio::error_code const& ec, http_parser const& parser, char co data_size = size; 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); cq.close(); @@ -55,7 +66,7 @@ void reset_globals() 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 ec) { 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(handler_called == 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); } int test_main() { - run_test("http://127.0.0.1/disk_io.png", 17809, 200, 1, asio::error_code()); - run_test("http://127.0.0.1/non-existing-file", -1, 404, 1, asio::error::eof); - run_test("http://non-existent-domain.se/non-existing-file", -1, -1, 0, asio::error::host_not_found); + typedef boost::optional err; + start_web_server(8001); + 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; } diff --git a/test/test_web_seed.cpp b/test/test_web_seed.cpp index 64f5c9ce9..7bd179669 100644 --- a/test/test_web_seed.cpp +++ b/test/test_web_seed.cpp @@ -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() { using namespace libtorrent; @@ -72,7 +54,7 @@ void test_transfer() add_files(*torrent_file, complete("."), "test_torrent"); - start_web_server(); + start_web_server(8000); file_pool fp; boost::scoped_ptr s(default_storage_constructor( @@ -115,7 +97,7 @@ void test_transfer() TEST_CHECK(th.is_seed()); remove_all("./test_torrent"); - stop_web_server(); + stop_web_server(8000); } int test_main()