From 1dc491e7e134ae12693afafdd039ddb0e428b768 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 3 Sep 2013 04:41:49 +0000 Subject: [PATCH] fix test_upnp --- src/upnp.cpp | 10 ++++++ test/setup_transfer.cpp | 5 +-- test/test.hpp | 4 +-- test/test_upnp.cpp | 78 ++++++++++++++++++++++++++++------------- 4 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/upnp.cpp b/src/upnp.cpp index 678e0ed08..c2139583b 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -1415,6 +1415,16 @@ void upnp::on_upnp_unmap_response(error_code const& e log(msg, l); } + error_code_parse_state s; + xml_parse((char*)p.get_body().begin, (char*)p.get_body().end + , boost::bind(&find_error_code, _1, _2, boost::ref(s))); + + l.unlock(); + m_callback(mapping, address(), 0, p.status_code() != 200 + ? error_code(p.status_code(), get_http_category()) + : error_code(s.error_code, upnp_category)); + l.lock(); + d.mapping[mapping].protocol = none; next(d, mapping, l); diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index a3f065dd0..70d290e2e 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -60,7 +60,7 @@ POSSIBILITY OF SUCH DAMAGE. #include -#define DEBUG_WEB_SERVER 0 +#define DEBUG_WEB_SERVER 1 #define DLOG if (DEBUG_WEB_SERVER) fprintf @@ -925,6 +925,7 @@ void send_content(socket_type& s, char const* file, int size, bool chunked) else { write(s, boost::asio::buffer(file, size), boost::asio::transfer_all(), ec); + DLOG(stderr, " >> %s\n", std::string(file, size).c_str()); if (ec) fprintf(stderr, "*** send failed: %s\n", ec.message().c_str()); } } @@ -1310,7 +1311,7 @@ void web_server_thread(int* port, bool ssl, bool chunked) // remove the / from the path path = path.substr(1); error_code ec; - int res = load_file(path, file_buf, ec); + int res = load_file(path, file_buf, ec, 8000000); if (res == -1) { fprintf(stderr, ">> file not found: %s\n", path.c_str()); diff --git a/test/test.hpp b/test/test.hpp index 340e4e57b..3f0d0dd06 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -84,7 +84,7 @@ int EXPORT print_failures(); #define TEST_EQUAL(x, y) \ if (x != y) { \ std::stringstream s__; \ - s__ << "TEST_EQUAL_ERROR:\n" #x ": " << x << "\nexpected: " << y << std::endl; \ + s__ << "TEST_EQUAL_ERROR:\n" #x ": " << x << "\nexpected: " << y; \ TEST_REPORT_AUX(s__.str().c_str(), __FILE__, __LINE__); \ } #else @@ -107,7 +107,7 @@ int EXPORT print_failures(); try { \ if (x != y) { \ std::stringstream s__; \ - s__ << "TEST_EQUAL_ERROR: " #x ": " << x << " expected: " << y << std::endl; \ + s__ << "TEST_EQUAL_ERROR: " #x ": " << x << " expected: " << y; \ TEST_REPORT_AUX(s__.str().c_str(), __FILE__, __LINE__); \ } \ } \ diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index b719f2509..38b0e9e5d 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -118,7 +118,6 @@ void callback(int mapping, address const& ip, int port, error_code const& err) callbacks.push_back(info); std::cerr << "mapping: " << mapping << ", port: " << port << ", IP: " << ip << ", error: \"" << err.message() << "\"\n"; - //TODO: store the callbacks and verify that the ports were successful } int run_upnp_test(char const* root_filename, char const* router_model, char const* control_name) @@ -158,37 +157,68 @@ int run_upnp_test(char const* root_filename, char const* router_model, char cons , user_agent, &callback, &log_callback, false); upnp_handler->discover_device(); - libtorrent::deadline_timer timer(ios); - timer.expires_from_now(seconds(10), ec); - timer.async_wait(boost::bind(&libtorrent::io_service::stop, boost::ref(ios))); + for (int i = 0; i < 20; ++i) + { + ios.reset(); + ios.poll(ec); + if (ec) + { + fprintf(stderr, "io_service::run(): %s\n", ec.message().c_str()); + ec.clear(); + break; + } + if (upnp_handler->router_model() != "") break; + test_sleep(100); + } - ios.reset(); - ios.run(ec); + std::cerr << "router: " << upnp_handler->router_model() << std::endl; + TEST_EQUAL(upnp_handler->router_model(), router_model); int mapping1 = upnp_handler->add_mapping(upnp::tcp, 500, 500); int mapping2 = upnp_handler->add_mapping(upnp::udp, 501, 501); - timer.expires_from_now(seconds(10), ec); - timer.async_wait(boost::bind(&libtorrent::io_service::stop, boost::ref(ios))); - ios.reset(); - ios.run(ec); - - xml.open("WANIPConnection", std::ios::trunc); - xml.write(soap_delete_response, sizeof(soap_delete_response)-1); - xml.close(); - - std::cerr << "router: " << upnp_handler->router_model() << std::endl; - TEST_CHECK(upnp_handler->router_model() == router_model); - upnp_handler->close(); - sock->close(); - - ios.reset(); - ios.run(ec); + for (int i = 0; i < 40; ++i) + { + ios.reset(); + ios.poll(ec); + if (ec) + { + fprintf(stderr, "io_service::run(): %s\n", ec.message().c_str()); + ec.clear(); + break; + } + if (callbacks.size() >= 2) break; + test_sleep(100); + } callback_info expected1 = {mapping1, 500, error_code()}; callback_info expected2 = {mapping2, 501, error_code()}; - TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected1) == 1); - TEST_CHECK(std::count(callbacks.begin(), callbacks.end(), expected2) == 1); + TEST_EQUAL(std::count(callbacks.begin(), callbacks.end(), expected1), 1); + TEST_EQUAL(std::count(callbacks.begin(), callbacks.end(), expected2), 1); + + xml.open(control_name, std::ios::trunc); + xml.write(soap_delete_response, sizeof(soap_delete_response)-1); + xml.close(); + + upnp_handler->close(); + sock->close(); + + for (int i = 0; i < 40; ++i) + { + ios.reset(); + ios.poll(ec); + if (ec) + { + fprintf(stderr, "io_service::run(): %s\n", ec.message().c_str()); + ec.clear(); + break; + } + if (callbacks.size() >= 4) break; + test_sleep(100); + } + + // there should have been two DeleteMapping calls + TEST_EQUAL(callbacks.size(), 4); stop_web_server();