added invariant checks

This commit is contained in:
unsh
2007-06-07 10:18:13 +00:00
parent 2030420f9a
commit 7996d35395

View File

@@ -277,6 +277,8 @@ namespace libtorrent
void bt_peer_connection::write_pe1_2_dhkey() void bt_peer_connection::write_pe1_2_dhkey()
{ {
INVARIANT_CHECK;
assert(!m_encrypted); assert(!m_encrypted);
assert(!m_rc4_encrypted); assert(!m_rc4_encrypted);
assert(!m_DH_key_exchange.get()); assert(!m_DH_key_exchange.get());
@@ -305,6 +307,8 @@ namespace libtorrent
void bt_peer_connection::write_pe3_sync() void bt_peer_connection::write_pe3_sync()
{ {
INVARIANT_CHECK;
assert (!m_encrypted); assert (!m_encrypted);
assert (!m_rc4_encrypted); assert (!m_rc4_encrypted);
assert (is_local()); assert (is_local());
@@ -383,6 +387,8 @@ namespace libtorrent
void bt_peer_connection::write_pe4_sync(int crypto_select) void bt_peer_connection::write_pe4_sync(int crypto_select)
{ {
INVARIANT_CHECK;
assert(!is_local()); assert(!is_local());
assert(!m_encrypted); assert(!m_encrypted);
assert(!m_rc4_encrypted); assert(!m_rc4_encrypted);
@@ -415,6 +421,8 @@ namespace libtorrent
void bt_peer_connection::write_pe_vc_cryptofield(buffer::interval& write_buf, void bt_peer_connection::write_pe_vc_cryptofield(buffer::interval& write_buf,
int crypto_field, int pad_size) int crypto_field, int pad_size)
{ {
INVARIANT_CHECK;
assert(crypto_field <= 0x03 && crypto_field > 0); assert(crypto_field <= 0x03 && crypto_field > 0);
assert(pad_size == 0); // pad not used yet assert(pad_size == 0); // pad not used yet
// vc,crypto_field,len(pad),pad, (len(ia)) // vc,crypto_field,len(pad),pad, (len(ia))
@@ -444,6 +452,8 @@ namespace libtorrent
void bt_peer_connection::init_pe_RC4_handler(char const* secret, sha1_hash const& stream_key) void bt_peer_connection::init_pe_RC4_handler(char const* secret, sha1_hash const& stream_key)
{ {
INVARIANT_CHECK;
assert(secret); assert(secret);
hasher h; hasher h;
@@ -483,8 +493,9 @@ namespace libtorrent
assert (begin); assert (begin);
assert (end); assert (end);
assert (end > begin); assert (end > begin);
assert (!m_rc4_encrypted || m_encrypted);
if (m_rc4_encrypted && m_encrypted) if (m_rc4_encrypted)
m_RC4_handler->encrypt(begin, end - begin); m_RC4_handler->encrypt(begin, end - begin);
peer_connection::send_buffer(begin, end); peer_connection::send_buffer(begin, end);
@@ -492,7 +503,9 @@ namespace libtorrent
buffer::interval bt_peer_connection::allocate_send_buffer(int size) buffer::interval bt_peer_connection::allocate_send_buffer(int size)
{ {
if (m_rc4_encrypted && m_encrypted) assert(!m_rc4_encrypted || m_encrypted);
if (m_rc4_encrypted)
{ {
m_enc_send_buffer = peer_connection::allocate_send_buffer(size); m_enc_send_buffer = peer_connection::allocate_send_buffer(size);
return m_enc_send_buffer; return m_enc_send_buffer;
@@ -506,7 +519,9 @@ namespace libtorrent
void bt_peer_connection::setup_send() void bt_peer_connection::setup_send()
{ {
if (m_rc4_encrypted && m_encrypted) assert(!m_rc4_encrypted || m_encrypted);
if (m_rc4_encrypted)
{ {
assert (m_enc_send_buffer.begin); assert (m_enc_send_buffer.begin);
assert (m_enc_send_buffer.end); assert (m_enc_send_buffer.end);
@@ -1383,7 +1398,8 @@ namespace libtorrent
m_statistics.received_bytes(0, bytes_transferred); m_statistics.received_bytes(0, bytes_transferred);
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
if (m_rc4_encrypted && m_encrypted) assert(in_handshake() || !m_rc4_encrypted || m_encrypted);
if (m_rc4_encrypted)
{ {
buffer::interval wr_buf = wr_recv_buffer(); buffer::interval wr_buf = wr_recv_buffer();
m_RC4_handler->decrypt((wr_buf.end - bytes_transferred), bytes_transferred); m_RC4_handler->decrypt((wr_buf.end - bytes_transferred), bytes_transferred);
@@ -1973,8 +1989,6 @@ namespace libtorrent
, (char*)info_hash.begin()); , (char*)info_hash.begin());
attach_to_torrent(info_hash); attach_to_torrent(info_hash);
t = associated_torrent().lock();
assert(t);
} }
else else
{ {
@@ -1991,11 +2005,11 @@ namespace libtorrent
#ifdef TORRENT_VERBOSE_LOGGING #ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << " info_hash received\n"; (*m_logger) << " info_hash received\n";
#endif #endif
t = associated_torrent().lock();
assert(t);
} }
// Respond with handshake and bitfield t = associated_torrent().lock();
assert(t);
if (!is_local()) if (!is_local())
{ {
write_handshake(); write_handshake();
@@ -2240,8 +2254,10 @@ namespace libtorrent
void bt_peer_connection::check_invariant() const void bt_peer_connection::check_invariant() const
{ {
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
assert(bool(m_state == read_pe_dhkey) == bool(m_DH_key_exchange) assert( (bool(m_state != read_pe_dhkey) || m_DH_key_exchange.get())
|| !is_local()); || !is_local());
assert(!m_rc4_encrypted || m_RC4_handler.get());
#endif #endif
if (!m_in_constructor) if (!m_in_constructor)
peer_connection::check_invariant(); peer_connection::check_invariant();