From c6b793021dffdd519c595999e16e439ca17cbbbf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 5 Nov 2008 03:17:41 +0000 Subject: [PATCH] make tests not depend on exceptions --- test/main.cpp | 1 + test/setup_transfer.cpp | 7 ++-- test/test_bandwidth_limiter.cpp | 26 +++++++++----- test/test_fast_extension.cpp | 6 ++-- test/test_http_connection.cpp | 8 +++-- test/test_ip_filter.cpp | 63 ++++++++++++++++++--------------- test/test_natpmp.cpp | 11 +++--- test/test_pex.cpp | 5 +-- test/test_storage.cpp | 20 ++++++----- test/test_upnp.cpp | 6 ++-- 10 files changed, 88 insertions(+), 65 deletions(-) diff --git a/test/main.cpp b/test/main.cpp index 8adb795e2..03f6c792a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include +#include int test_main(); diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 74861e164..d20ca9066 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -309,7 +309,8 @@ setup_transfer(session* ses1, session* ses2, session* ses3 if (connect_peers) { std::cerr << "connecting peer\n"; - tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1") + error_code ec; + tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec) , ses2->listen_port())); if (ses3) @@ -317,10 +318,10 @@ setup_transfer(session* ses1, session* ses2, session* ses3 // give the other peers some time to get an initial // set of pieces before they start sharing with each-other tor3.connect_peer(tcp::endpoint( - address::from_string("127.0.0.1") + address::from_string("127.0.0.1", ec) , ses2->listen_port())); tor3.connect_peer(tcp::endpoint( - address::from_string("127.0.0.1") + address::from_string("127.0.0.1", ec) , ses1->listen_port())); } } diff --git a/test/test_bandwidth_limiter.cpp b/test/test_bandwidth_limiter.cpp index 7da7cd78f..190f86b8e 100644 --- a/test/test_bandwidth_limiter.cpp +++ b/test/test_bandwidth_limiter.cpp @@ -280,14 +280,16 @@ void do_tick(error_code const&e, deadline_timer& tick, connections_t& v) } std::for_each(v.begin(), v.end() , boost::bind(&peer_connection::tick, _1)); - tick.expires_from_now(seconds(1)); + error_code ec; + tick.expires_from_now(seconds(1), ec); tick.async_wait(boost::bind(&do_tick, _1, boost::ref(tick), boost::ref(v))); } void do_stop(deadline_timer& tick, connections_t& v) { abort_tick = true; - tick.cancel(); + error_code ec; + tick.cancel(ec); std::for_each(v.begin(), v.end() , boost::bind(&peer_connection::stop, _1)); std::cerr << " stopping..." << std::endl; @@ -312,7 +314,8 @@ void do_change_rate(error_code const&e, deadline_timer& tick t1->m_bandwidth_limit[0].throttle(limit + limit / 2 * ((counter & 1)?-1:1)); t2->m_bandwidth_limit[0].throttle(limit + limit / 2 * ((counter & 1)?1:-1)); - tick.expires_from_now(milliseconds(1600)); + error_code ec; + tick.expires_from_now(milliseconds(1600), ec); tick.async_wait(boost::bind(&do_change_rate, _1, boost::ref(tick), t1, t2, limit, counter-1)); } @@ -335,7 +338,8 @@ void do_change_peer_rate(error_code const&e, deadline_timer& tick for (connections_t::iterator i = v.begin(); i != v.end(); ++i, ++c) i->get()->throttle(limit + limit / 2 * ((c & 1)?-1:1)); - tick.expires_from_now(milliseconds(1100)); + error_code ec; + tick.expires_from_now(milliseconds(1100), ec); tick.async_wait(boost::bind(&do_change_peer_rate, _1, boost::ref(tick), boost::ref(v), limit, counter-1)); } @@ -344,17 +348,19 @@ void run_test(io_service& ios, connections_t& v) abort_tick = false; std::cerr << "-------------" << std::endl; deadline_timer tick(ios); - tick.expires_from_now(seconds(1)); + + error_code ec; + tick.expires_from_now(seconds(1), ec); tick.async_wait(boost::bind(&do_tick, _1, boost::ref(tick), boost::ref(v))); deadline_timer complete(ios); - complete.expires_from_now(milliseconds(int(sample_time * 1000))); + complete.expires_from_now(milliseconds(int(sample_time * 1000)), ec); complete.async_wait(boost::bind(&do_stop, boost::ref(tick), boost::ref(v))); std::for_each(v.begin(), v.end() , boost::bind(&peer_connection::start, _1)); - ios.run(); + ios.run(ec); } bool close_to(float val, float comp, float err) @@ -420,8 +426,9 @@ void test_connections_variable_rate(int num, int limit, int torrent_limit) std::for_each(v.begin(), v.end() , boost::bind(&peer_connection::throttle, _1, limit)); + error_code ec; deadline_timer change_rate(ios); - change_rate.expires_from_now(milliseconds(1600)); + change_rate.expires_from_now(milliseconds(1600), ec); change_rate.async_wait(boost::bind(&do_change_peer_rate, _1, boost::ref(change_rate) , boost::ref(v), limit, 9)); run_test(ios, v); @@ -552,8 +559,9 @@ void test_torrents_variable_rate(int num, int limit, int global_limit) std::copy(v1.begin(), v1.end(), std::back_inserter(v)); std::copy(v2.begin(), v2.end(), std::back_inserter(v)); + error_code ec; deadline_timer change_rate(ios); - change_rate.expires_from_now(milliseconds(1100)); + change_rate.expires_from_now(milliseconds(1100), ec); change_rate.async_wait(boost::bind(&do_change_rate, _1, boost::ref(change_rate), t1, t2, limit, 9)); run_test(ios, v); diff --git a/test/test_fast_extension.cpp b/test/test_fast_extension.cpp index 2a5166c63..6940949f2 100644 --- a/test/test_fast_extension.cpp +++ b/test/test_fast_extension.cpp @@ -168,7 +168,8 @@ void test_reject_fast() io_service ios; stream_socket s(ios); - s.connect(tcp::endpoint(address::from_string("127.0.0.1"), ses1.listen_port())); + error_code ec; + s.connect(tcp::endpoint(address::from_string("127.0.0.1", ec), ses1.listen_port()), ec); char recv_buffer[1000]; do_handshake(s, ih, recv_buffer); @@ -230,8 +231,9 @@ void test_respect_suggest() test_sleep(2000); io_service ios; + error_code ec; stream_socket s(ios); - s.connect(tcp::endpoint(address::from_string("127.0.0.1"), ses1.listen_port())); + s.connect(tcp::endpoint(address::from_string("127.0.0.1", ec), ses1.listen_port()), ec); char recv_buffer[1000]; do_handshake(s, ih, recv_buffer); diff --git a/test/test_http_connection.cpp b/test/test_http_connection.cpp index 3fbdf5189..3b44dfd02 100644 --- a/test/test_http_connection.cpp +++ b/test/test_http_connection.cpp @@ -66,8 +66,9 @@ void http_connect_handler(http_connection& c) { ++connect_handler_called; TEST_CHECK(c.socket().is_open()); - std::cerr << "connected to: " << c.socket().remote_endpoint() << std::endl; - TEST_CHECK(c.socket().remote_endpoint().address() == address::from_string("127.0.0.1")); + error_code ec; + std::cerr << "connected to: " << c.socket().remote_endpoint(ec) << std::endl; + TEST_CHECK(c.socket().remote_endpoint(ec).address() == address::from_string("127.0.0.1", ec)); } void http_handler(error_code const& ec, http_parser const& parser @@ -110,7 +111,8 @@ void run_test(std::string const& url, int size, int status, int connected , &::http_handler, true, &::http_connect_handler)); h->get(url, seconds(5), 0, &ps); ios.reset(); - ios.run(); + error_code e; + ios.run(e); std::cerr << "connect_handler_called: " << connect_handler_called << std::endl; std::cerr << "handler_called: " << handler_called << std::endl; diff --git a/test/test_ip_filter.cpp b/test/test_ip_filter.cpp index f2046c4aa..bde665f3c 100644 --- a/test/test_ip_filter.cpp +++ b/test/test_ip_filter.cpp @@ -56,14 +56,18 @@ bool compare(ip_range const& lhs && lhs.flags == rhs.flags; } +#define IP(x) address::from_string(x, ec) +#define IP4(x) address_v4::from_string(x, ec) + void test_rules_invariant(std::vector > const& r, ip_filter const& f) { typedef std::vector >::const_iterator iterator; TEST_CHECK(!r.empty()); if (r.empty()) return; - TEST_CHECK(r.front().first == address::from_string("0.0.0.0")); - TEST_CHECK(r.back().last == address::from_string("255.255.255.255")); + error_code ec; + TEST_CHECK(r.front().first == IP("0.0.0.0")); + TEST_CHECK(r.back().last == IP("255.255.255.255")); iterator i = r.begin(); iterator j = boost::next(i); @@ -81,19 +85,20 @@ int test_main() using namespace libtorrent; std::vector > range; + error_code ec; // **** test joining of ranges at the end **** ip_range expected1[] = { - {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.255.255.255"), 0} - , {address_v4::from_string("1.0.0.0"), address_v4::from_string("3.0.0.0"), ip_filter::blocked} - , {address_v4::from_string("3.0.0.1"), address_v4::from_string("255.255.255.255"), 0} + {IP4("0.0.0.0"), IP4("0.255.255.255"), 0} + , {IP4("1.0.0.0"), IP4("3.0.0.0"), ip_filter::blocked} + , {IP4("3.0.0.1"), IP4("255.255.255.255"), 0} }; { ip_filter f; - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.0.0.0"), ip_filter::blocked); + f.add_rule(IP("2.0.0.1"), IP("3.0.0.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -107,8 +112,8 @@ int test_main() { ip_filter f; - f.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked); + f.add_rule(IP("2.0.0.1"), IP("3.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.0.0.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -123,8 +128,8 @@ int test_main() { ip_filter f; - f.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.4.0.0"), ip_filter::blocked); + f.add_rule(IP("2.0.0.1"), IP("3.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.4.0.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -139,8 +144,8 @@ int test_main() { ip_filter f; - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.4.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.4.0.0"), ip_filter::blocked); + f.add_rule(IP("2.0.0.1"), IP("3.0.0.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -155,12 +160,12 @@ int test_main() { ip_filter f; - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("3.0.0.0"), address::from_string("4.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("5.0.0.0"), address::from_string("6.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("7.0.0.0"), address::from_string("8.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.0.0.0"), ip_filter::blocked); + f.add_rule(IP("3.0.0.0"), IP("4.0.0.0"), ip_filter::blocked); + f.add_rule(IP("5.0.0.0"), IP("6.0.0.0"), ip_filter::blocked); + f.add_rule(IP("7.0.0.0"), IP("8.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("1.0.1.0"), address::from_string("9.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.1.0"), IP("9.0.0.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -168,9 +173,9 @@ int test_main() TEST_CHECK(range.size() == 3); ip_range expected[] = { - {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.255.255.255"), 0} - , {address_v4::from_string("1.0.0.0"), address_v4::from_string("9.0.0.0"), ip_filter::blocked} - , {address_v4::from_string("9.0.0.1"), address_v4::from_string("255.255.255.255"), 0} + {IP4("0.0.0.0"), IP4("0.255.255.255"), 0} + , {IP4("1.0.0.0"), IP4("9.0.0.0"), ip_filter::blocked} + , {IP4("9.0.0.1"), IP4("255.255.255.255"), 0} }; TEST_CHECK(std::equal(range.begin(), range.end(), expected, &compare)); @@ -181,12 +186,12 @@ int test_main() { ip_filter f; - f.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("3.0.0.0"), address::from_string("4.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("5.0.0.0"), address::from_string("6.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("7.0.0.0"), address::from_string("8.0.0.0"), ip_filter::blocked); + f.add_rule(IP("1.0.0.0"), IP("2.0.0.0"), ip_filter::blocked); + f.add_rule(IP("3.0.0.0"), IP("4.0.0.0"), ip_filter::blocked); + f.add_rule(IP("5.0.0.0"), IP("6.0.0.0"), ip_filter::blocked); + f.add_rule(IP("7.0.0.0"), IP("8.0.0.0"), ip_filter::blocked); - f.add_rule(address::from_string("0.0.1.0"), address::from_string("7.0.4.0"), ip_filter::blocked); + f.add_rule(IP("0.0.1.0"), IP("7.0.4.0"), ip_filter::blocked); range = boost::get<0>(f.export_filter()); test_rules_invariant(range, f); @@ -194,9 +199,9 @@ int test_main() TEST_CHECK(range.size() == 3); ip_range expected[] = { - {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.0.0.255"), 0} - , {address_v4::from_string("0.0.1.0"), address_v4::from_string("8.0.0.0"), ip_filter::blocked} - , {address_v4::from_string("8.0.0.1"), address_v4::from_string("255.255.255.255"), 0} + {IP4("0.0.0.0"), IP4("0.0.0.255"), 0} + , {IP4("0.0.1.0"), IP4("8.0.0.0"), ip_filter::blocked} + , {IP4("8.0.0.1"), IP4("255.255.255.255"), 0} }; TEST_CHECK(std::equal(range.begin(), range.end(), expected, &compare)); diff --git a/test/test_natpmp.cpp b/test/test_natpmp.cpp index 836752a55..060e29f31 100644 --- a/test/test_natpmp.cpp +++ b/test/test_natpmp.cpp @@ -31,25 +31,26 @@ int main(int argc, char* argv[]) int tcp_map = natpmp_handler->add_mapping(natpmp::tcp, atoi(argv[1]), atoi(argv[1])); int udp_map = natpmp_handler->add_mapping(natpmp::udp, atoi(argv[2]), atoi(argv[2])); - timer.expires_from_now(seconds(2)); + error_code ec; + timer.expires_from_now(seconds(2), ec); timer.async_wait(boost::bind(&io_service::stop, boost::ref(ios))); std::cerr << "mapping ports TCP: " << argv[1] << " UDP: " << argv[2] << std::endl; ios.reset(); - ios.run(); - timer.expires_from_now(seconds(2)); + ios.run(ec); + timer.expires_from_now(seconds(2), ec); timer.async_wait(boost::bind(&io_service::stop, boost::ref(ios))); std::cerr << "removing mapping " << tcp_map << std::endl; natpmp_handler->delete_mapping(tcp_map); ios.reset(); - ios.run(); + ios.run(ec); std::cerr << "removing mappings" << std::endl; natpmp_handler->close(); ios.reset(); - ios.run(); + ios.run(ec); std::cerr << "closing" << std::endl; } diff --git a/test/test_pex.cpp b/test/test_pex.cpp index b6e873380..ad3c6b3e9 100644 --- a/test/test_pex.cpp +++ b/test/test_pex.cpp @@ -90,8 +90,9 @@ void test_pex() test_sleep(1000); - tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1"), ses1.listen_port())); - tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1"), ses3.listen_port())); + error_code ec; + tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec), ses1.listen_port())); + tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec), ses3.listen_port())); for (int i = 0; i < 90; ++i) { diff --git a/test/test_storage.cpp b/test/test_storage.cpp index dedf14fcb..74dec9a43 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -156,19 +156,20 @@ void run_storage_tests(boost::intrusive_ptr info , test_path, fp, io, default_storage_constructor, storage_mode); boost::mutex lock; + error_code ec; lazy_entry frd; pm->async_check_fastresume(&frd, &on_check_resume_data); ios.reset(); - ios.run(); + ios.run(ec); pm->async_check_files(&on_check_files); for (int i = 0; i < 4; ++i) { ios.reset(); - ios.run_one(); + ios.run_one(ec); } ios.reset(); - ios.poll(); + ios.poll(ec); // test move_storage boost::function none; @@ -177,7 +178,7 @@ void run_storage_tests(boost::intrusive_ptr info test_sleep(2000); ios.reset(); - ios.poll(); + ios.poll(ec); TEST_CHECK(!exists(test_path / "temp_storage")); TEST_CHECK(exists(test_path / "temp_storage2/temp_storage")); @@ -185,7 +186,7 @@ void run_storage_tests(boost::intrusive_ptr info test_sleep(2000); ios.reset(); - ios.poll(); + ios.poll(ec); TEST_CHECK(!exists(test_path / "temp_storage2/temp_storage")); remove_all(test_path / "temp_storage2"); @@ -198,7 +199,7 @@ void run_storage_tests(boost::intrusive_ptr info test_sleep(2000); ios.reset(); - ios.poll(); + ios.poll(ec); TEST_CHECK(!exists(test_path / "temp_storage/test1.tmp")); TEST_CHECK(exists(test_path / "part0")); @@ -218,7 +219,7 @@ void run_storage_tests(boost::intrusive_ptr info test_sleep(1000); TEST_CHECK(!exists(test_path / "part0")); - ios.run(); + ios.run(ec); io.join(); } @@ -312,10 +313,11 @@ void test_check_files(path const& test_path , test_path, fp, io, default_storage_constructor, storage_mode); boost::mutex lock; + error_code ec; lazy_entry frd; pm->async_check_fastresume(&frd, &on_check_resume_data); ios.reset(); - ios.run(); + ios.run(ec); bool pieces[4] = {false, false, false, false}; bool done = false; @@ -324,7 +326,7 @@ void test_check_files(path const& test_path while (!done) { ios.reset(); - ios.run_one(); + ios.run_one(ec); } TEST_CHECK(pieces[0] == true); TEST_CHECK(pieces[1] == false); diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index dfacd5ba2..1c8390e21 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -72,7 +72,7 @@ int main(int argc, char* argv[]) std::cerr << "broadcasting for UPnP device" << std::endl; ios.reset(); - ios.run(); + ios.run(ec); upnp_handler->add_mapping(upnp::tcp, atoi(argv[1]), atoi(argv[1])); upnp_handler->add_mapping(upnp::udp, atoi(argv[2]), atoi(argv[2])); @@ -82,13 +82,13 @@ int main(int argc, char* argv[]) << " UDP: " << argv[2] << std::endl; ios.reset(); - ios.run(); + ios.run(ec); std::cerr << "router: " << upnp_handler->router_model() << std::endl; std::cerr << "removing mappings" << std::endl; upnp_handler->close(); ios.reset(); - ios.run(); + ios.run(ec); std::cerr << "closing" << std::endl; }