Added asserts related to total_size in create_torrent (#642)

This commit is contained in:
Alden Torres 2016-04-26 00:37:47 -04:00 committed by Arvid Norberg
parent 7c4fabadfd
commit d08bbbc0cc
6 changed files with 55 additions and 30 deletions

View File

@ -755,7 +755,7 @@ namespace libtorrent
// the peer class all TCP peers belong to by default
// all tcp peer connections are subject to these
// bandwidth limits. Local peers are excempted
// bandwidth limits. Local peers are exempted
// from this limit. The purpose is to be able to
// throttle TCP that passes over the internet
// bottleneck (i.e. modem) to avoid starving out
@ -920,7 +920,7 @@ namespace libtorrent
int m_auto_manage_time_scaler;
// works like unchoke_time_scaler but it
// is only decresed when the unchoke set
// is only decreased when the unchoke set
// is recomputed, and when it reaches zero,
// the optimistic unchoke is moved to another peer.
// TODO: replace this by a proper asio timer
@ -985,7 +985,7 @@ namespace libtorrent
boost::uint16_t session_time() const TORRENT_OVERRIDE
{
// +1 is here to make it possible to distinguish uninitialized (to
// 0) timestamps and timestamps of things that happend during the
// 0) timestamps and timestamps of things that happened during the
// first second after the session was constructed
boost::int64_t const ret = total_seconds(aux::time_now() - m_created) + 1;
TORRENT_ASSERT(ret >= 0);
@ -1049,7 +1049,7 @@ namespace libtorrent
libtorrent::utp_socket_manager m_utp_socket_manager;
#ifdef TORRENT_USE_OPENSSL
// used for uTP connectons over SSL
// used for uTP connections over SSL
udp_socket m_ssl_udp_socket;
libtorrent::utp_socket_manager m_ssl_utp_socket_manager;
#endif

View File

@ -44,7 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
// The bitfiled type stores any number of bits as a bitfield
// The bitfield type stores any number of bits as a bitfield
// in a heap allocated array.
struct TORRENT_EXPORT bitfield
{
@ -55,7 +55,7 @@ namespace libtorrent
//
// The constructor taking a pointer ``b`` and ``bits`` copies a bitfield
// from the specified buffer, and ``bits`` number of bits (rounded up to
// the nearest byte boundry).
// the nearest byte boundary).
bitfield(): m_buf(NULL) {}
bitfield(int bits): m_buf(NULL)
{ resize(bits); }

View File

@ -157,4 +157,3 @@ namespace libtorrent
TORRENT_ASSERT(size() == bits);
}
}

View File

@ -325,9 +325,10 @@ namespace libtorrent
, m_include_symlinks((flags & symlinks) != 0)
{
TORRENT_ASSERT(fs.num_files() > 0);
TORRENT_ASSERT(fs.total_size() > 0);
// return instead of crash in release mode
if (fs.num_files() == 0) return;
if (fs.num_files() == 0 || fs.total_size() == 0) return;
if (!m_multifile && has_parent_path(m_files.file_path(0))) m_multifile = true;
@ -386,6 +387,10 @@ namespace libtorrent
, m_include_symlinks(false)
{
TORRENT_ASSERT(ti.is_valid());
TORRENT_ASSERT(ti.num_pieces() > 0);
TORRENT_ASSERT(ti.num_files() > 0);
TORRENT_ASSERT(ti.total_size() > 0);
if (ti.creation_date()) m_creation_date = *ti.creation_date();
if (!ti.creator().empty()) set_creator(ti.creator().c_str());

View File

@ -474,7 +474,7 @@ public:
// we set our cwnd to 1 MSS. This condition can happen either because
// a packet has timed out and needs to be resent or because our
// cwnd is set to less than one MSS during congestion control.
// it can also happen if the other end sends an advertized window
// it can also happen if the other end sends an advertised window
// size less than one MSS.
time_point m_timeout;
@ -1824,7 +1824,7 @@ bool utp_socket_impl::send_pkt(int flags)
, effective_mtu - header_size);
// if we have one MSS worth of data, make sure it fits in our
// congestion window and the advertized receive window from
// congestion window and the advertised receive window from
// the other end.
if (m_bytes_in_flight + payload_size > (std::min)(int(m_cwnd >> 16)
, int(m_adv_wnd - m_bytes_in_flight)))
@ -2430,7 +2430,7 @@ void utp_socket_impl::ack_packet(packet* p, time_point const& receive_time
// this means our clock is not monotonic. Just assume the RTT was 100 ms
rtt = 100000;
// the clock for this plaform is not monotonic!
// the clock for this platform is not monotonic!
TORRENT_ASSERT(false);
}
@ -2548,7 +2548,7 @@ bool utp_socket_impl::consume_incoming_data(
{
// if we don't have a buffer from the upper layer, and the
// number of queued up bytes, waiting for the upper layer,
// exceeds the advertized receive window, start ignoring
// exceeds the advertised receive window, start ignoring
// more data packets
UTP_LOG("%8p: ERROR: our advertized window is not honored. "
"recv_buf: %d buffered_in: %d max_size: %d\n"
@ -2855,7 +2855,7 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size
{
// this is too far out to fit in our reorder buffer. Drop it
// This is either an attack to try to break the connection
// or a seariously damaged connection that lost a lot of
// or a seriously damaged connection that lost a lot of
// packets. Neither is very likely, and it should be OK
// to drop the timestamp information.
UTP_LOG("%8p: ERROR: incoming packet seq_nr:%d our ack_nr:%d (ignored)\n"
@ -3194,7 +3194,7 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size
{
// we need to ack some data we received, and we didn't
// end up sending any payload packets in the loop
// above (becasue m_out_packets would have been incremented
// above (because m_out_packets would have been incremented
// in that case). This means we need to send an ack.
// don't do it right away, because we may still receive
// more packets. defer the ack to send as few acks as possible
@ -3207,7 +3207,7 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size
if (m_state == UTP_STATE_ERROR_WAIT || m_state == UTP_STATE_DELETE) return true;
// Everything up to the FIN has been receieved, respond with a FIN
// Everything up to the FIN has been received, respond with a FIN
// from our side.
if (m_eof && m_ack_nr == ((m_eof_seq_nr - 1) & ACK_MASK))
{
@ -3411,7 +3411,7 @@ void utp_socket_impl::do_ledbat(const int acked_bytes, const int delay
// the portion of the in-flight bytes that were acked. This is used to make
// the gain factor be scaled by the rtt. The formula is applied once per
// rtt, or on every ACK skaled by the number of ACKs per rtt
// rtt, or on every ACK scaled by the number of ACKs per rtt
TORRENT_ASSERT(in_flight > 0);
TORRENT_ASSERT(acked_bytes > 0);
@ -3457,7 +3457,7 @@ void utp_socket_impl::do_ledbat(const int acked_bytes, const int delay
// bytes to cwnd
if (m_ssthres != 0 && ((m_cwnd + exponential_gain) >> 16) > m_ssthres)
{
// if we would exeed the slow start threshold by growing the cwnd
// if we would exceed the slow start threshold by growing the cwnd
// exponentially, don't do it, and leave slow-start mode. This
// make us avoid causing more delay and/or packet loss by being too
// aggressive
@ -3767,4 +3767,3 @@ void utp_socket_impl::check_invariant() const
}
#endif
}

View File

@ -82,7 +82,7 @@ void test_running_torrent(boost::shared_ptr<torrent_info> info, boost::int64_t f
// test_sleep(500);
torrent_status st = h.status();
TEST_EQUAL(st.total_wanted, file_size * 3);
TEST_EQUAL(st.total_wanted, file_size); // we want the single file
TEST_EQUAL(st.total_wanted_done, 0);
std::vector<int> prio(info->num_files(), 1);
@ -90,7 +90,7 @@ void test_running_torrent(boost::shared_ptr<torrent_info> info, boost::int64_t f
h.prioritize_files(prio);
st = h.status();
TEST_EQUAL(st.total_wanted, file_size * 2);
TEST_EQUAL(st.total_wanted, 0); // we don't want anything
TEST_EQUAL(st.total_wanted_done, 0);
TEST_EQUAL(int(h.file_priorities().size()), info->num_files());
if (!st.is_seeding)
@ -129,13 +129,12 @@ void test_running_torrent(boost::shared_ptr<torrent_info> info, boost::int64_t f
std::vector<char> piece(info->piece_length());
for (int i = 0; i < int(piece.size()); ++i)
piece[i] = (i % 26) + 'A';
h.add_piece(0, &piece[0]);
h.add_piece(0, &piece[0], torrent_handle::overwrite_existing);
// wait until the piece is done writing and hashing
// TODO: wait for an alert rather than just waiting 10 seconds. This is kind of silly
test_sleep(2000);
wait_for_alert(ses, piece_finished_alert::alert_type, "piece_finished_alert");
st = h.status();
TEST_CHECK(st.pieces.size() > 0 && st.pieces[0] == true);
TEST_CHECK(st.pieces.size() > 0);
std::cout << "reading piece 0" << std::endl;
h.read_piece(0);
@ -146,7 +145,7 @@ void test_running_torrent(boost::shared_ptr<torrent_info> info, boost::int64_t f
if (rpa)
{
std::cout << "SUCCEEDED!" << std::endl;
TEST_CHECK(memcmp(&piece[0], rpa->buffer.get(), piece.size()) == 0);
TEST_CHECK(memcmp(&piece[0], rpa->buffer.get(), info->piece_size(0)) == 0);
TEST_CHECK(rpa->size == info->piece_size(0));
TEST_CHECK(rpa->piece == 0);
TEST_CHECK(hasher(&piece[0], piece.size()).final() == info->hash_for_piece(0));
@ -251,27 +250,50 @@ TORRENT_TEST(torrent)
{
file_storage fs;
fs.add_file("test_torrent_dir2/tmp1", 0);
fs.add_file("test_torrent_dir2/tmp1", 1024);
libtorrent::create_torrent t(fs, 128 * 1024, 6);
std::vector<char> piece(128 * 1024);
for (int i = 0; i < int(piece.size()); ++i)
piece[i] = (i % 26) + 'A';
// calculate the hash for all pieces
sha1_hash ph = hasher(&piece[0], piece.size()).final();
int num = t.num_pieces();
TEST_CHECK(t.num_pieces() > 0);
for (int i = 0; i < num; ++i)
t.set_hash(i, ph);
std::vector<char> tmp;
std::back_insert_iterator<std::vector<char> > out(tmp);
bencode(out, t.generate());
error_code ec;
boost::shared_ptr<torrent_info> info(boost::make_shared<torrent_info>(&tmp[0], tmp.size(), boost::ref(ec), 0));
test_running_torrent(info, 0);
test_running_torrent(info, 1024);
}
}
TORRENT_TEST(torrent_total_size_zero)
{
file_storage fs;
error_code ec;
fs.add_file("test_torrent_dir2/tmp1", 0);
libtorrent::create_torrent t(fs);
TEST_CHECK(fs.num_files() == 1);
TEST_CHECK(fs.total_size() == 0);
error_code ec;
set_piece_hashes(t, ".", ec);
ec.clear();
libtorrent::create_torrent t1(fs);
set_piece_hashes(t1, ".", ec);
TEST_CHECK(ec);
fs.add_file("test_torrent_dir2/tmp2", 0);
TEST_CHECK(fs.num_files() == 2);
TEST_CHECK(fs.total_size() == 0);
ec.clear();
libtorrent::create_torrent t2(fs);
set_piece_hashes(t2, ".", ec);
TEST_CHECK(ec);
}