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);
#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);
if (bt->supports_encryption()) m_counters.inc_stats_counter(

View File

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

View File

@ -1899,32 +1899,38 @@ retry:
}
#ifdef TORRENT_USE_OPENSSL
// TODO: 2 use bind_to_device in udp_socket
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 (ec)
// if ssl port is 0, we don't want to listen on an SSL port
if (ssl_port != 0)
{
#if defined TORRENT_LOGGING
session_log("SSL: cannot bind to UDP interface \"%s\": %s"
, print_endpoint(m_listen_interface).c_str(), ec.message().c_str());
#endif
if (m_alerts.should_post<listen_failed_alert>())
udp::endpoint ssl_bind_if(m_listen_interface.address(), ssl_port);
// TODO: 2 use bind_to_device in udp_socket
m_ssl_udp_socket.bind(ssl_bind_if, ec);
if (ec)
{
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));
}
#if defined TORRENT_LOGGING
session_log("SSL: cannot bind to UDP interface \"%s\": %s"
, print_endpoint(m_listen_interface).c_str(), ec.message().c_str());
#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
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 seed_has_cert;
bool downloader_has_cert;
bool downloader_has_ssl_listen_port;
bool expected_to_complete;
int peer_errors;
int ssl_disconnects;
@ -67,14 +68,18 @@ struct test_config_t
test_config_t test_config[] =
{
{"nobody has a cert (connect to regular port)", false, false, false, false, 0, 0},
{"nobody has a cert (connect to ssl port)", true, false, false, false, 1, 1},
{"seed has a cert, but not downloader (connect to regular port)", false, true, false, false, 0, 0},
{"seed has a cert, but not downloader (connect to ssl port)", true, true, false, false, 1, 1},
{"downloader has a cert, but not seed (connect to regular port)", false, false, true, false, 0, 0},
{"downloader has a cert, but not seed (connect to ssl port)", true, false, true, false, 1, 1},
{"both downloader and seed has a cert (connect to regular port)", false, true, true, false, 0, 0},
{"both downloader and seed has a cert (connect to ssl port)", true, true, true, true, 0, 0},
// name sslport sd-cert dl-cert dl-port expect peer-error ssl-disconn
{"nobody has a cert (connect to regular port)", false, false, false, true, false, 0, 1},
{"nobody has a cert (connect to ssl port)", true, false, false, true, false, 1, 1},
{"seed has a cert, but not downloader (connect to regular port)", false, true, false, true, false, 0, 1},
{"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 regular port)", false, false, true, true, false, 0, 1},
{"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 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;
@ -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_upnp, 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);
libtorrent::session ses1(sett, 0);
if (!test.downloader_has_cert)
// this disables outgoing SSL connections
sett.set_int(settings_pack::ssl_listen, 0);
else
if (test.downloader_has_ssl_listen_port)
sett.set_int(settings_pack::ssl_listen, ssl_port + 20);
else
sett.set_int(settings_pack::ssl_listen, 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
int port = 0;
if (test.use_ssl_ports)
port = ses2.ssl_listen_port();
if (port == 0)
if (test.downloader_has_ssl_listen_port)
port = ses2.ssl_listen_port();
else
port = 13512;
else
port = ses2.listen_port();
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"
, 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);
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: 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
p1 = ses1.abort();