fix ssl unit test

This commit is contained in:
Arvid Norberg 2015-01-06 08:16:03 +00:00
parent 0f37e3403c
commit 1a53a454ec
4 changed files with 56 additions and 41 deletions

View File

@ -4019,7 +4019,7 @@ namespace libtorrent
else m_counters.inc_stats_counter(counters::error_incoming_peers); else m_counters.inc_stats_counter(counters::error_incoming_peers);
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
if (type() == bittorrent_connection) if (type() == bittorrent_connection && op != op_connect)
{ {
bt_peer_connection* bt = static_cast<bt_peer_connection*>(this); bt_peer_connection* bt = static_cast<bt_peer_connection*>(this);
if (bt->supports_encryption()) m_counters.inc_stats_counter( if (bt->supports_encryption()) m_counters.inc_stats_counter(

View File

@ -457,6 +457,7 @@ namespace libtorrent
|| p.web_seed || p.web_seed
|| !p.connectable || !p.connectable
|| (p.seed && m_finished) || (p.seed && m_finished)
// TODO: 3 settings_pack::max_failcount should be used here, not 3
|| int(p.failcount) >= 3) || int(p.failcount) >= 3)
return false; return false;

View File

@ -1899,32 +1899,38 @@ retry:
} }
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
// TODO: 2 use bind_to_device in udp_socket
int ssl_port = m_settings.get_int(settings_pack::ssl_listen); int ssl_port = m_settings.get_int(settings_pack::ssl_listen);
udp::endpoint ssl_bind_if(m_listen_interface.address(), ssl_port);
m_ssl_udp_socket.bind(ssl_bind_if, ec); // if ssl port is 0, we don't want to listen on an SSL port
if (ec) if (ssl_port != 0)
{ {
#if defined TORRENT_LOGGING udp::endpoint ssl_bind_if(m_listen_interface.address(), ssl_port);
session_log("SSL: cannot bind to UDP interface \"%s\": %s"
, print_endpoint(m_listen_interface).c_str(), ec.message().c_str()); // TODO: 2 use bind_to_device in udp_socket
#endif m_ssl_udp_socket.bind(ssl_bind_if, ec);
if (m_alerts.should_post<listen_failed_alert>()) if (ec)
{ {
error_code err; #if defined TORRENT_LOGGING
m_alerts.post_alert(listen_failed_alert(print_endpoint(ssl_bind_if) session_log("SSL: cannot bind to UDP interface \"%s\": %s"
, listen_failed_alert::bind, ec, listen_failed_alert::utp_ssl)); , print_endpoint(m_listen_interface).c_str(), ec.message().c_str());
}
ec.clear();
}
else
{
if (m_alerts.should_post<listen_succeeded_alert>())
m_alerts.post_alert(listen_succeeded_alert(
tcp::endpoint(ssl_bind_if.address(), ssl_bind_if.port())
, listen_succeeded_alert::utp_ssl));
}
#endif #endif
if (m_alerts.should_post<listen_failed_alert>())
{
error_code err;
m_alerts.post_alert(listen_failed_alert(print_endpoint(ssl_bind_if)
, listen_failed_alert::bind, ec, listen_failed_alert::utp_ssl));
}
ec.clear();
}
else
{
if (m_alerts.should_post<listen_succeeded_alert>())
m_alerts.post_alert(listen_succeeded_alert(
tcp::endpoint(ssl_bind_if.address(), ssl_bind_if.port())
, listen_succeeded_alert::utp_ssl));
}
}
#endif // TORRENT_USE_OPENSSL
// TODO: 2 use bind_to_device in udp_socket // TODO: 2 use bind_to_device in udp_socket
m_udp_socket.bind(udp::endpoint(m_listen_interface.address(), m_listen_interface.port()), ec); m_udp_socket.bind(udp::endpoint(m_listen_interface.address(), m_listen_interface.port()), ec);

View File

@ -60,6 +60,7 @@ struct test_config_t
bool use_ssl_ports; bool use_ssl_ports;
bool seed_has_cert; bool seed_has_cert;
bool downloader_has_cert; bool downloader_has_cert;
bool downloader_has_ssl_listen_port;
bool expected_to_complete; bool expected_to_complete;
int peer_errors; int peer_errors;
int ssl_disconnects; int ssl_disconnects;
@ -67,14 +68,18 @@ struct test_config_t
test_config_t test_config[] = test_config_t test_config[] =
{ {
{"nobody has a cert (connect to regular port)", false, false, false, false, 0, 0}, // name sslport sd-cert dl-cert dl-port expect peer-error ssl-disconn
{"nobody has a cert (connect to ssl port)", true, false, false, false, 1, 1}, {"nobody has a cert (connect to regular port)", false, false, false, true, false, 0, 1},
{"seed has a cert, but not downloader (connect to regular port)", false, true, false, false, 0, 0}, {"nobody has a cert (connect to ssl port)", true, false, false, true, false, 1, 1},
{"seed has a cert, but not downloader (connect to ssl port)", true, true, false, false, 1, 1}, {"seed has a cert, but not downloader (connect to regular port)", false, true, false, true, false, 0, 1},
{"downloader has a cert, but not seed (connect to regular port)", false, false, true, false, 0, 0}, {"seed has a cert, but not downloader (connect to ssl port)", true, true, false, true, false, 1, 1},
{"downloader has a cert, but not seed (connect to ssl port)", true, false, true, false, 1, 1}, {"downloader has a cert, but not seed (connect to regular port)", false, false, true, true, false, 0, 1},
{"both downloader and seed has a cert (connect to regular port)", false, true, true, false, 0, 0}, {"downloader has a cert, but not seed (connect to ssl port)", true, false, true, true, false, 1, 1},
{"both downloader and seed has a cert (connect to ssl port)", true, true, true, true, 0, 0}, {"both downloader and seed has a cert (connect to regular port)", false, true, true, true, false, 0, 1},
{"both downloader and seed has a cert (connect to ssl port)", true, true, true, true, true, 0, 0},
// there is a disconnect (or failed connection attempt), that's not a peer
// error though, so both counters stay 0
{"both downloader and seed has a cert (downloader has no SSL port)", true, true, true, false, false, 0, 0},
}; };
int peer_disconnects = 0; int peer_disconnects = 0;
@ -131,15 +136,16 @@ void test_ssl(int test_idx, bool use_utp)
sett.set_bool(settings_pack::enable_lsd, false); sett.set_bool(settings_pack::enable_lsd, false);
sett.set_bool(settings_pack::enable_upnp, false); sett.set_bool(settings_pack::enable_upnp, false);
sett.set_bool(settings_pack::enable_natpmp, false); sett.set_bool(settings_pack::enable_natpmp, false);
// if a pwer fails once, don't try it again
sett.set_int(settings_pack::max_failcount, 1);
sett.set_int(settings_pack::ssl_listen, ssl_port); sett.set_int(settings_pack::ssl_listen, ssl_port);
libtorrent::session ses1(sett, 0); libtorrent::session ses1(sett, 0);
if (!test.downloader_has_cert) if (test.downloader_has_ssl_listen_port)
// this disables outgoing SSL connections
sett.set_int(settings_pack::ssl_listen, 0);
else
sett.set_int(settings_pack::ssl_listen, ssl_port + 20); sett.set_int(settings_pack::ssl_listen, ssl_port + 20);
else
sett.set_int(settings_pack::ssl_listen, 0);
libtorrent::session ses2(sett, 0); libtorrent::session ses2(sett, 0);
@ -192,9 +198,11 @@ void test_ssl(int test_idx, bool use_utp)
// connect the peers after setting the certificates // connect the peers after setting the certificates
int port = 0; int port = 0;
if (test.use_ssl_ports) if (test.use_ssl_ports)
port = ses2.ssl_listen_port(); if (test.downloader_has_ssl_listen_port)
port = ses2.ssl_listen_port();
if (port == 0) else
port = 13512;
else
port = ses2.listen_port(); port = ses2.listen_port();
fprintf(stderr, "%s: ses1: connecting peer port: %d\n" fprintf(stderr, "%s: ses1: connecting peer port: %d\n"
@ -257,14 +265,14 @@ void test_ssl(int test_idx, bool use_utp)
fprintf(stderr, "peer_errors: %d expected_errors: %d\n" fprintf(stderr, "peer_errors: %d expected_errors: %d\n"
, peer_errors, test.peer_errors); , peer_errors, test.peer_errors);
TEST_EQUAL(peer_errors, test.peer_errors); TEST_EQUAL(peer_errors > 0, test.peer_errors > 0);
fprintf(stderr, "ssl_disconnects: %d expected: %d\n", ssl_peer_disconnects, test.ssl_disconnects); fprintf(stderr, "ssl_disconnects: %d expected: %d\n", ssl_peer_disconnects, test.ssl_disconnects);
TEST_EQUAL(ssl_peer_disconnects, test.ssl_disconnects); TEST_EQUAL(ssl_peer_disconnects > 0, test.ssl_disconnects > 0);
fprintf(stderr, "%s: EXPECT: %s\n", time_now_string(), test.expected_to_complete ? "SUCCEESS" : "FAILURE"); fprintf(stderr, "%s: EXPECT: %s\n", time_now_string(), test.expected_to_complete ? "SUCCEESS" : "FAILURE");
fprintf(stderr, "%s: RESULT: %s\n", time_now_string(), tor2.status().is_seeding ? "SUCCEESS" : "FAILURE"); fprintf(stderr, "%s: RESULT: %s\n", time_now_string(), tor2.status().is_seeding ? "SUCCEESS" : "FAILURE");
TEST_CHECK(tor2.status().is_seeding == test.expected_to_complete); TEST_EQUAL(tor2.status().is_seeding, test.expected_to_complete);
// this allows shutting down the sessions in parallel // this allows shutting down the sessions in parallel
p1 = ses1.abort(); p1 = ses1.abort();