2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
2018-09-22 02:25:56 +02:00
|
|
|
#include "settings.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "test_utils.hpp"
|
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
2017-01-27 18:43:34 +01:00
|
|
|
#include "libtorrent/aux_/alloca.hpp" // for use of private TORRENT_ALLOCA
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2014-09-04 10:55:24 +02:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2015-03-12 06:20:12 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2015-04-12 00:02:30 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2015-04-18 23:21:22 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
#include <cstring>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2015-04-18 23:21:22 +02:00
|
|
|
#include <cstdarg>
|
2016-11-26 09:09:01 +01:00
|
|
|
#include <cstdio> // for vsnprintf
|
2015-04-18 23:21:22 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-04-12 02:14:07 +02:00
|
|
|
void log(char const* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
|
|
|
|
char buf[1024];
|
2018-01-30 14:56:07 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
2016-11-26 09:09:01 +01:00
|
|
|
std::vsnprintf(buf, sizeof(buf), fmt, v);
|
2018-01-30 14:56:07 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2015-04-12 02:14:07 +02:00
|
|
|
va_end(v);
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("\x1b[1m\x1b[36m%s: %s\x1b[0m\n"
|
2015-04-24 05:48:08 +02:00
|
|
|
, time_now_string(), buf);
|
2015-04-12 02:14:07 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
void print_session_log(lt::session& ses)
|
|
|
|
{
|
2017-04-02 20:22:17 +02:00
|
|
|
print_alerts(ses, "ses", true);
|
2015-04-11 17:25:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
int read_message(tcp::socket& s, span<char> buffer)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2018-11-20 21:55:18 +01:00
|
|
|
boost::asio::read(s, boost::asio::buffer(buffer.data(), 4)
|
2015-06-06 07:22:53 +02:00
|
|
|
, boost::asio::transfer_all(), ec);
|
2008-03-20 03:20:01 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2015-06-14 21:48:11 +02:00
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
return -1;
|
2008-03-20 03:20:01 +01:00
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
char const* ptr = buffer.data();
|
|
|
|
int const length = read_int32(ptr);
|
|
|
|
if (length > buffer.size())
|
2015-04-12 02:14:07 +02:00
|
|
|
{
|
|
|
|
log("message size: %d", length);
|
2017-04-23 06:37:29 +02:00
|
|
|
TEST_ERROR("message size exceeds max limit");
|
2015-04-12 02:14:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
boost::asio::read(s, boost::asio::buffer(buffer.data(), std::size_t(length))
|
2015-06-06 07:22:53 +02:00
|
|
|
, boost::asio::transfer_all(), ec);
|
2008-03-20 03:20:01 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2015-06-14 21:48:11 +02:00
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
return -1;
|
2008-03-20 03:20:01 +01:00
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
void print_message(span<char const> buffer)
|
2011-02-13 04:16:53 +01:00
|
|
|
{
|
|
|
|
char const* message_name[] = {"choke", "unchoke", "interested", "not_interested"
|
|
|
|
, "have", "bitfield", "request", "piece", "cancel", "dht_port", "", "", ""
|
|
|
|
, "suggest_piece", "have_all", "have_none", "reject_request", "allowed_fast"};
|
|
|
|
|
2018-07-19 14:30:54 +02:00
|
|
|
std::stringstream message;
|
2011-02-13 04:16:53 +01:00
|
|
|
char extra[300];
|
|
|
|
extra[0] = 0;
|
2018-11-20 21:55:18 +01:00
|
|
|
if (buffer.empty())
|
2011-02-13 04:16:53 +01:00
|
|
|
{
|
2018-07-19 14:30:54 +02:00
|
|
|
message << "keepalive";
|
2011-02-13 04:16:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
int const msg = buffer[0];
|
2011-02-13 04:16:53 +01:00
|
|
|
if (msg >= 0 && msg < int(sizeof(message_name)/sizeof(message_name[0])))
|
2018-07-19 14:30:54 +02:00
|
|
|
message << message_name[msg];
|
2014-09-04 10:55:24 +02:00
|
|
|
else if (msg == 20)
|
2018-07-19 14:30:54 +02:00
|
|
|
message << "extension msg [" << int(buffer[1]) << "]";
|
2011-02-13 04:16:53 +01:00
|
|
|
else
|
2018-07-19 14:30:54 +02:00
|
|
|
message << "unknown[" << msg << "]";
|
2011-02-13 04:16:53 +01:00
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
if (msg == 0x6 && buffer.size() == 13)
|
2011-02-13 04:16:53 +01:00
|
|
|
{
|
|
|
|
peer_request r;
|
2018-11-20 21:55:18 +01:00
|
|
|
const char* ptr = buffer.data() + 1;
|
2016-12-22 16:42:33 +01:00
|
|
|
r.piece = piece_index_t(detail::read_int32(ptr));
|
2011-02-13 04:16:53 +01:00
|
|
|
r.start = detail::read_int32(ptr);
|
|
|
|
r.length = detail::read_int32(ptr);
|
2016-12-22 16:42:33 +01:00
|
|
|
std::snprintf(extra, sizeof(extra), "p: %d s: %d l: %d"
|
|
|
|
, static_cast<int>(r.piece), r.start, r.length);
|
2011-02-13 04:16:53 +01:00
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
else if (msg == 0x11 && buffer.size() == 5)
|
2011-02-13 04:16:53 +01:00
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
const char* ptr = buffer.data() + 1;
|
2011-02-13 04:16:53 +01:00
|
|
|
int index = detail::read_int32(ptr);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(extra, sizeof(extra), "p: %d", index);
|
2011-02-13 04:16:53 +01:00
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
else if (msg == 20 && buffer.size() > 4 && buffer[1] == 0 )
|
2015-04-12 02:14:07 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(extra, sizeof(extra), "%s"
|
2018-11-20 21:55:18 +01:00
|
|
|
, print_entry(bdecode(buffer.subspan(2))).c_str());
|
2015-04-12 02:14:07 +02:00
|
|
|
}
|
2011-02-13 04:16:53 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:30:54 +02:00
|
|
|
log("<== %s %s", message.str().c_str(), extra);
|
2011-02-13 04:16:53 +01:00
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_allow_fast(tcp::socket& s, int piece)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> allow fast: %d", piece);
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2007-10-04 11:32:09 +02:00
|
|
|
char msg[] = "\0\0\0\x05\x11\0\0\0\0";
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
write_int32(piece, ptr);
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 9)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_suggest_piece(tcp::socket& s, int piece)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> suggest piece: %d", piece);
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2007-10-04 11:32:09 +02:00
|
|
|
char msg[] = "\0\0\0\x05\x0d\0\0\0\0";
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
write_int32(piece, ptr);
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 9)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_keepalive(tcp::socket& s)
|
2009-04-21 21:27:52 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> keepalive");
|
2009-04-21 21:27:52 +02:00
|
|
|
char msg[] = "\0\0\0\0";
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 4)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2009-04-21 21:27:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_unchoke(tcp::socket& s)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> unchoke");
|
2007-10-04 11:32:09 +02:00
|
|
|
char msg[] = "\0\0\0\x01\x01";
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 5)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_have_all(tcp::socket& s)
|
2012-05-02 20:03:12 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> have_all");
|
2012-05-02 20:03:12 +02:00
|
|
|
char msg[] = "\0\0\0\x01\x0e"; // have_all
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 5)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2012-05-02 20:03:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_have_none(tcp::socket& s)
|
2012-05-02 20:03:12 +02:00
|
|
|
{
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> have_none");
|
2012-05-02 20:03:12 +02:00
|
|
|
char msg[] = "\0\0\0\x01\x0f"; // have_none
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 5)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2012-05-02 20:03:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_bitfield(tcp::socket& s, char const* bits)
|
2012-05-01 05:24:38 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2012-05-01 05:24:38 +02:00
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
int num_pieces = int(strlen(bits));
|
2012-05-01 05:24:38 +02:00
|
|
|
int packet_size = (num_pieces+7)/8 + 5;
|
2016-10-22 20:43:40 +02:00
|
|
|
TORRENT_ALLOCA(msg, char, packet_size);
|
|
|
|
std::fill(msg.begin(), msg.end(), 0);
|
|
|
|
char* ptr = msg.data();
|
2012-05-01 05:24:38 +02:00
|
|
|
write_int32(packet_size-4, ptr);
|
|
|
|
write_int8(5, ptr);
|
2015-04-12 04:54:43 +02:00
|
|
|
log("==> bitfield [%s]", bits);
|
2012-05-01 05:24:38 +02:00
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
|
|
|
ptr[i/8] |= (bits[i] == '1' ? 1 : 0) << i % 8;
|
|
|
|
}
|
|
|
|
error_code ec;
|
2018-11-01 23:05:30 +01:00
|
|
|
boost::asio::write(s, boost::asio::buffer(msg.data(), std::size_t(msg.size()))
|
2015-06-06 07:22:53 +02:00
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2012-05-01 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void do_handshake(tcp::socket& s, sha1_hash const& ih, char* buffer)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2014-09-04 10:55:24 +02:00
|
|
|
char handshake[] = "\x13" "BitTorrent protocol\0\0\0\0\0\x10\0\x04"
|
2007-10-04 11:32:09 +02:00
|
|
|
" " // space for info-hash
|
2012-05-02 20:03:12 +02:00
|
|
|
"aaaaaaaaaaaaaaaaaaaa"; // peer-id
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> handshake");
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-10-04 11:32:09 +02:00
|
|
|
std::memcpy(handshake + 28, ih.begin(), 20);
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(handshake, sizeof(handshake) - 1)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
return;
|
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
// read handshake
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::read(s, boost::asio::buffer(buffer, 68)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2008-03-20 03:20:01 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2015-04-11 17:25:24 +02:00
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
return;
|
2008-03-20 03:20:01 +01:00
|
|
|
}
|
2015-04-12 02:14:07 +02:00
|
|
|
log("<== handshake");
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
TEST_CHECK(buffer[0] == 19);
|
|
|
|
TEST_CHECK(std::memcmp(buffer + 1, "BitTorrent protocol", 19) == 0);
|
|
|
|
|
|
|
|
char* extensions = buffer + 20;
|
|
|
|
// check for fast extension support
|
|
|
|
TEST_CHECK(extensions[7] & 0x4);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
// check for extension protocol support
|
2016-06-08 03:45:48 +02:00
|
|
|
bool const lt_extension_protocol = (extensions[5] & 0x10) != 0;
|
|
|
|
TEST_CHECK(lt_extension_protocol == true);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
// check for DHT support
|
2016-06-08 03:45:48 +02:00
|
|
|
bool const dht_support = (extensions[7] & 0x1) != 0;
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
TEST_CHECK(dht_support == true);
|
|
|
|
#else
|
|
|
|
TEST_CHECK(dht_support == false);
|
2007-10-04 11:32:09 +02:00
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
TEST_CHECK(std::memcmp(buffer + 28, ih.begin(), 20) == 0);
|
|
|
|
}
|
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_extension_handshake(tcp::socket& s, entry const& e)
|
2015-04-12 00:02:30 +02:00
|
|
|
{
|
|
|
|
std::vector<char> buf;
|
|
|
|
|
|
|
|
// reserve space for the message header
|
2015-08-28 03:48:52 +02:00
|
|
|
// uint32: packet-length
|
2015-04-12 00:02:30 +02:00
|
|
|
// uint8: 20 (extension message)
|
|
|
|
// uint8: 0 (handshake)
|
|
|
|
buf.resize(4 + 1 + 1);
|
|
|
|
|
|
|
|
bencode(std::back_inserter(buf), e);
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2015-04-12 00:02:30 +02:00
|
|
|
|
|
|
|
char* ptr = &buf[0];
|
2016-04-25 23:22:09 +02:00
|
|
|
write_uint32(int(buf.size()) - 4, ptr);
|
2015-04-12 00:02:30 +02:00
|
|
|
write_uint8(20, ptr);
|
|
|
|
write_uint8(0, ptr);
|
|
|
|
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(&buf[0], buf.size())
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:15:48 +01:00
|
|
|
void send_request(tcp::socket& s, peer_request req)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2015-11-06 07:15:48 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
log("==> request %d (%d,%d)", static_cast<int>(req.piece), req.start, req.length);
|
2015-11-06 07:15:48 +01:00
|
|
|
char msg[] = "\0\0\0\x0d\x06 "; // have_none
|
|
|
|
char* ptr = msg + 5;
|
2016-12-22 16:42:33 +01:00
|
|
|
write_uint32(static_cast<int>(req.piece), ptr);
|
2015-11-06 07:15:48 +01:00
|
|
|
write_uint32(req.start, ptr);
|
|
|
|
write_uint32(req.length, ptr);
|
|
|
|
error_code ec;
|
|
|
|
boost::asio::write(s, boost::asio::buffer(msg, 17)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
|
|
|
if (ec) TEST_ERROR(ec.message());
|
|
|
|
}
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
entry read_extension_handshake(tcp::socket& s, span<char> recv_buffer)
|
2015-04-12 02:14:07 +02:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2015-04-12 02:14:07 +02:00
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
TEST_ERROR("failed to read message");
|
|
|
|
return entry();
|
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
recv_buffer = recv_buffer.first(len);
|
|
|
|
print_message(recv_buffer);
|
2015-04-12 02:14:07 +02:00
|
|
|
|
|
|
|
if (len < 4) continue;
|
|
|
|
int msg = recv_buffer[0];
|
|
|
|
if (msg != 20) continue;
|
|
|
|
int extmsg = recv_buffer[1];
|
|
|
|
if (extmsg != 0) continue;
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
return bdecode(recv_buffer.subspan(2));
|
2015-04-12 02:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2015-06-06 19:49:18 +02:00
|
|
|
void send_ut_metadata_msg(tcp::socket& s, int ut_metadata_msg, int type, int piece)
|
2015-04-12 02:14:07 +02:00
|
|
|
{
|
|
|
|
std::vector<char> buf;
|
|
|
|
|
|
|
|
// reserve space for the message header
|
2015-08-28 03:48:52 +02:00
|
|
|
// uint32: packet-length
|
2015-04-12 02:14:07 +02:00
|
|
|
// uint8: 20 (extension message)
|
|
|
|
// uint8: <ut_metadata_msg> (ut_metadata)
|
|
|
|
buf.resize(4 + 1 + 1);
|
|
|
|
|
|
|
|
entry e;
|
|
|
|
e["msg_type"] = type;
|
|
|
|
e["piece"] = piece;
|
|
|
|
bencode(std::back_inserter(buf), e);
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2015-04-12 02:14:07 +02:00
|
|
|
|
|
|
|
char* ptr = &buf[0];
|
2016-04-25 23:22:09 +02:00
|
|
|
write_uint32(int(buf.size()) - 4, ptr);
|
2015-04-12 02:14:07 +02:00
|
|
|
write_uint8(20, ptr);
|
|
|
|
write_uint8(ut_metadata_msg, ptr);
|
|
|
|
|
|
|
|
log("==> ut_metadata [ type: %d piece: %d ]", type, piece);
|
|
|
|
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(&buf[0], buf.size())
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 02:14:07 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
|
|
|
}
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
entry read_ut_metadata_msg(tcp::socket& s, span<char> recv_buffer)
|
2015-04-12 02:14:07 +02:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2015-04-12 02:14:07 +02:00
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
TEST_ERROR("failed to read message");
|
|
|
|
return entry();
|
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
auto const buffer = recv_buffer.first(len);
|
|
|
|
print_message(buffer);
|
2015-04-12 02:14:07 +02:00
|
|
|
|
|
|
|
if (len < 4) continue;
|
2018-11-20 21:55:18 +01:00
|
|
|
int const msg = buffer[0];
|
2015-04-12 02:14:07 +02:00
|
|
|
if (msg != 20) continue;
|
2018-11-20 21:55:18 +01:00
|
|
|
int const extmsg = buffer[1];
|
2015-04-12 02:14:07 +02:00
|
|
|
if (extmsg != 1) continue;
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
return bdecode(buffer.subspan(2));
|
2015-04-12 02:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-24 08:18:48 +02:00
|
|
|
#endif // TORRENT_DISABLE_EXTENSIONS
|
2015-04-12 02:14:07 +02:00
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> setup_peer(tcp::socket& s, sha1_hash& ih
|
2016-09-01 03:42:18 +02:00
|
|
|
, std::shared_ptr<lt::session>& ses, bool incoming = true
|
2017-07-09 16:32:01 +02:00
|
|
|
, torrent_flags_t const flags = torrent_flags_t{}
|
|
|
|
, torrent_handle* th = nullptr)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> t = ::create_torrent();
|
2012-05-02 20:03:12 +02:00
|
|
|
ih = t->info_hash();
|
2018-09-22 02:25:56 +02:00
|
|
|
settings_pack sett = settings();
|
2015-06-05 08:31:52 +02:00
|
|
|
sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:48900");
|
2015-06-07 23:54:08 +02:00
|
|
|
sett.set_bool(settings_pack::enable_upnp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_natpmp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_lsd, false);
|
|
|
|
sett.set_bool(settings_pack::enable_dht, false);
|
2016-06-08 03:45:48 +02:00
|
|
|
sett.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
|
|
|
|
sett.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
|
|
|
|
sett.set_bool(settings_pack::enable_outgoing_utp, false);
|
|
|
|
sett.set_bool(settings_pack::enable_incoming_utp, false);
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-12-08 09:49:33 +01:00
|
|
|
sett.set_bool(settings_pack::rate_limit_utp, true);
|
|
|
|
#endif
|
2015-09-02 07:30:40 +02:00
|
|
|
ses.reset(new lt::session(sett, lt::session::add_default_plugins));
|
2015-04-11 17:25:24 +02:00
|
|
|
|
2009-12-15 14:11:07 +01:00
|
|
|
error_code ec;
|
2009-12-16 11:42:34 +01:00
|
|
|
add_torrent_params p;
|
2017-07-09 16:32:01 +02:00
|
|
|
p.flags &= ~torrent_flags::paused;
|
|
|
|
p.flags &= ~torrent_flags::auto_managed;
|
2016-06-08 03:45:48 +02:00
|
|
|
p.flags |= flags;
|
2009-12-16 11:42:34 +01:00
|
|
|
p.ti = t;
|
|
|
|
p.save_path = "./tmp1_fast";
|
2011-09-15 11:44:07 +02:00
|
|
|
|
|
|
|
remove("./tmp1_fast/temporary", ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) log("remove(): %s", ec.message().c_str());
|
2011-09-15 11:44:07 +02:00
|
|
|
ec.clear();
|
2015-04-11 17:25:24 +02:00
|
|
|
torrent_handle ret = ses->add_torrent(p, ec);
|
|
|
|
if (th) *th = ret;
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
// wait for the torrent to be ready
|
2017-11-28 22:59:46 +01:00
|
|
|
wait_for_downloading(*ses, "ses");
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2016-06-08 03:45:48 +02:00
|
|
|
if (incoming)
|
|
|
|
{
|
|
|
|
s.connect(tcp::endpoint(address::from_string("127.0.0.1", ec), ses->listen_port()), ec);
|
|
|
|
if (ec) TEST_ERROR(ec.message());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-19 20:56:19 +02:00
|
|
|
tcp::acceptor l(lt::get_io_service(s));
|
2016-06-08 03:45:48 +02:00
|
|
|
l.open(tcp::v4());
|
2016-07-01 06:23:56 +02:00
|
|
|
l.bind(tcp::endpoint(address_v4::from_string("127.0.0.1"), 0));
|
2016-06-08 03:45:48 +02:00
|
|
|
l.listen();
|
|
|
|
tcp::endpoint addr = l.local_endpoint();
|
|
|
|
|
|
|
|
ret.connect_peer(addr);
|
|
|
|
print_session_log(*ses);
|
|
|
|
l.accept(s);
|
|
|
|
}
|
2015-04-11 17:25:24 +02:00
|
|
|
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-01-23 19:23:57 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
// makes sure that pieces that are allowed and then
|
|
|
|
// rejected aren't requested again
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(reject_fast)
|
2012-05-02 20:03:12 +02:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test reject ===\n" << std::endl;
|
2012-05-02 20:03:12 +02:00
|
|
|
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2007-10-04 11:32:09 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2012-05-02 20:03:12 +02:00
|
|
|
setup_peer(s, ih, ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2015-06-05 08:31:52 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
std::vector<int> allowed_fast;
|
|
|
|
allowed_fast.push_back(0);
|
|
|
|
allowed_fast.push_back(1);
|
|
|
|
allowed_fast.push_back(2);
|
|
|
|
allowed_fast.push_back(3);
|
|
|
|
|
|
|
|
std::for_each(allowed_fast.begin(), allowed_fast.end()
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::bind(&send_allow_fast, std::ref(s), _1));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
while (!allowed_fast.empty())
|
|
|
|
{
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2015-06-14 21:48:11 +02:00
|
|
|
if (len == -1) break;
|
2018-11-20 21:55:18 +01:00
|
|
|
auto buffer = span<char const>(recv_buffer).first(len);
|
|
|
|
print_message(buffer);
|
|
|
|
int msg = buffer[0];
|
2011-03-09 08:51:04 +01:00
|
|
|
if (msg != 0x6) continue;
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2018-11-20 21:55:18 +01:00
|
|
|
char const* ptr = buffer.data() + 1;
|
|
|
|
int const piece = read_int32(ptr);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
std::vector<int>::iterator i = std::find(allowed_fast.begin()
|
|
|
|
, allowed_fast.end(), piece);
|
|
|
|
TEST_CHECK(i != allowed_fast.end());
|
|
|
|
if (i != allowed_fast.end())
|
|
|
|
allowed_fast.erase(i);
|
|
|
|
// send reject request
|
|
|
|
recv_buffer[0] = 0x10;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> reject");
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer("\0\0\0\x0d", 4)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
break;
|
|
|
|
}
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(recv_buffer, 13)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
break;
|
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
s.close();
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 03:48:52 +02:00
|
|
|
TORRENT_TEST(invalid_suggest)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test suggest ===\n" << std::endl;
|
2015-08-28 03:48:52 +02:00
|
|
|
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2015-08-28 03:48:52 +02:00
|
|
|
io_service ios;
|
|
|
|
tcp::socket s(ios);
|
|
|
|
setup_peer(s, ih, ses);
|
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
send_have_all(s);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
// this is an invalid suggest message. We would not expect to receive a
|
|
|
|
// request for that piece index.
|
|
|
|
send_suggest_piece(s, -234);
|
|
|
|
send_unchoke(s);
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2015-08-28 03:48:52 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
int len = read_message(s, recv_buffer);
|
|
|
|
auto buffer = span<char const>(recv_buffer).first(len);
|
2015-08-28 03:48:52 +02:00
|
|
|
int idx = -1;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
if (buffer[0] == 6)
|
2015-08-28 03:48:52 +02:00
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
char const* ptr = buffer.data() + 1;
|
2015-08-28 03:48:52 +02:00
|
|
|
idx = detail::read_int32(ptr);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
len = read_message(s, recv_buffer);
|
|
|
|
buffer = span<char const>(recv_buffer).first(len);
|
2015-08-28 03:48:52 +02:00
|
|
|
}
|
|
|
|
TEST_CHECK(idx != -234);
|
|
|
|
TEST_CHECK(idx != -1);
|
|
|
|
s.close();
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(reject_suggest)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test suggest ===\n" << std::endl;
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2007-10-04 11:32:09 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2012-05-02 20:03:12 +02:00
|
|
|
setup_peer(s, ih, ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-10-04 11:32:09 +02:00
|
|
|
std::vector<int> suggested;
|
|
|
|
suggested.push_back(0);
|
|
|
|
suggested.push_back(1);
|
|
|
|
suggested.push_back(2);
|
|
|
|
suggested.push_back(3);
|
|
|
|
|
|
|
|
std::for_each(suggested.begin(), suggested.end()
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::bind(&send_suggest_piece, std::ref(s), _1));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
send_unchoke(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2009-04-21 21:27:52 +02:00
|
|
|
send_keepalive(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2009-04-21 21:27:52 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
int fail_counter = 100;
|
2007-10-04 11:32:09 +02:00
|
|
|
while (!suggested.empty() && fail_counter > 0)
|
|
|
|
{
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2015-06-14 21:48:11 +02:00
|
|
|
if (len == -1) break;
|
2018-11-20 21:55:18 +01:00
|
|
|
auto buffer = span<char const>(recv_buffer).first(len);
|
|
|
|
print_message(buffer);
|
|
|
|
int const msg = buffer[0];
|
2007-10-04 11:32:09 +02:00
|
|
|
fail_counter--;
|
2011-03-09 08:51:04 +01:00
|
|
|
if (msg != 0x6) continue;
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2018-11-20 21:55:18 +01:00
|
|
|
char const* ptr = buffer.data() + 1;
|
2016-06-13 02:36:05 +02:00
|
|
|
int const piece = read_int32(ptr);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
std::vector<int>::iterator i = std::find(suggested.begin()
|
|
|
|
, suggested.end(), piece);
|
|
|
|
TEST_CHECK(i != suggested.end());
|
|
|
|
if (i != suggested.end())
|
|
|
|
suggested.erase(i);
|
|
|
|
// send reject request
|
|
|
|
recv_buffer[0] = 0x10;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-04-12 02:14:07 +02:00
|
|
|
log("==> reject");
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer("\0\0\0\x0d", 4)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
break;
|
|
|
|
}
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(recv_buffer, 13)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
TEST_ERROR(ec.message());
|
|
|
|
break;
|
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
TEST_CHECK(fail_counter > 0);
|
2012-05-02 20:03:12 +02:00
|
|
|
|
2016-06-13 02:36:05 +02:00
|
|
|
s.close();
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2016-06-13 02:36:05 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(suggest_order)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test suggest ===\n" << std::endl;
|
2016-06-13 02:36:05 +02:00
|
|
|
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2016-06-13 02:36:05 +02:00
|
|
|
io_service ios;
|
|
|
|
tcp::socket s(ios);
|
|
|
|
setup_peer(s, ih, ses);
|
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
send_have_all(s);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
std::vector<int> suggested;
|
|
|
|
suggested.push_back(0);
|
|
|
|
suggested.push_back(1);
|
|
|
|
suggested.push_back(2);
|
|
|
|
suggested.push_back(3);
|
|
|
|
|
|
|
|
std::for_each(suggested.begin(), suggested.end()
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::bind(&send_suggest_piece, std::ref(s), _1));
|
2016-06-13 02:36:05 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
send_unchoke(s);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
int fail_counter = 100;
|
|
|
|
while (!suggested.empty() && fail_counter > 0)
|
|
|
|
{
|
|
|
|
print_session_log(*ses);
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2016-06-13 02:36:05 +02:00
|
|
|
if (len == -1) break;
|
2018-11-20 21:55:18 +01:00
|
|
|
auto const buffer = span<char const>(recv_buffer).first(len);
|
|
|
|
print_message({recv_buffer, len});
|
|
|
|
int const msg = recv_buffer[0];
|
2016-06-13 02:36:05 +02:00
|
|
|
fail_counter--;
|
|
|
|
|
|
|
|
// we're just interested in requests
|
|
|
|
if (msg != 0x6) continue;
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2018-11-20 21:55:18 +01:00
|
|
|
char const* ptr = buffer.data() + 1;
|
2016-06-13 02:36:05 +02:00
|
|
|
int const piece = read_int32(ptr);
|
|
|
|
|
|
|
|
// make sure we receive the requests inverse order of sending the suggest
|
|
|
|
// messages. The last suggest should be the highest priority
|
|
|
|
int const expected_piece = suggested.back();
|
|
|
|
suggested.pop_back();
|
|
|
|
TEST_EQUAL(piece, expected_piece);
|
|
|
|
}
|
|
|
|
print_session_log(*ses);
|
|
|
|
TEST_CHECK(fail_counter > 0);
|
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
s.close();
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(multiple_bitfields)
|
2012-05-01 05:24:38 +02:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test multiple bitfields ===\n" << std::endl;
|
2012-05-01 05:24:38 +02:00
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2012-05-01 05:24:38 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> ti = setup_peer(s, ih, ses);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
|
|
|
|
std::string bitfield;
|
2018-01-25 15:34:39 +01:00
|
|
|
bitfield.resize(std::size_t(ti->num_pieces()), '0');
|
2012-05-01 05:24:38 +02:00
|
|
|
send_bitfield(s, bitfield.c_str());
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
bitfield[0] = '1';
|
|
|
|
send_bitfield(s, bitfield.c_str());
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
bitfield[1] = '1';
|
|
|
|
send_bitfield(s, bitfield.c_str());
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
bitfield[2] = '1';
|
|
|
|
send_bitfield(s, bitfield.c_str());
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2012-05-01 05:24:38 +02:00
|
|
|
s.close();
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(multiple_have_all)
|
2012-05-02 20:03:12 +02:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test multiple have_all ===\n" << std::endl;
|
2012-05-01 05:24:38 +02:00
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2012-05-02 20:03:12 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> ti = setup_peer(s, ih, ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_none(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-02 20:03:12 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
s.close();
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(500));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2012-05-01 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2014-09-04 10:55:24 +02:00
|
|
|
// makes sure that pieces that are lost are not requested
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(dont_have)
|
2014-09-04 10:55:24 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test dont_have ===\n" << std::endl;
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
sha1_hash ih;
|
|
|
|
torrent_handle th;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2014-09-04 10:55:24 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2017-07-09 16:32:01 +02:00
|
|
|
std::shared_ptr<torrent_info> ti = setup_peer(s, ih, ses, true
|
|
|
|
, torrent_flags_t{}, &th);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2014-09-04 10:55:24 +02:00
|
|
|
send_have_all(s);
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(300));
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
|
|
|
std::vector<peer_info> pi;
|
|
|
|
th.get_peer_info(pi);
|
|
|
|
|
|
|
|
TEST_EQUAL(pi.size(), 1);
|
|
|
|
if (pi.size() != 1) return;
|
|
|
|
|
|
|
|
// at this point, the peer should be considered a seed
|
|
|
|
TEST_CHECK(pi[0].flags & peer_info::seed);
|
|
|
|
|
|
|
|
int lt_dont_have = 0;
|
2015-04-11 17:25:24 +02:00
|
|
|
error_code ec;
|
2014-09-04 10:55:24 +02:00
|
|
|
while (lt_dont_have == 0)
|
|
|
|
{
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2015-06-14 21:48:11 +02:00
|
|
|
if (len == -1) break;
|
2018-11-20 21:55:18 +01:00
|
|
|
auto const buffer = span<char const>(recv_buffer).first(len);
|
|
|
|
print_message(buffer);
|
2014-09-04 10:55:24 +02:00
|
|
|
if (len == 0) continue;
|
2018-11-20 21:55:18 +01:00
|
|
|
int const msg = buffer[0];
|
2014-09-04 10:55:24 +02:00
|
|
|
if (msg != 20) continue;
|
2018-11-20 21:55:18 +01:00
|
|
|
int const ext_msg = buffer[1];
|
2014-09-04 10:55:24 +02:00
|
|
|
if (ext_msg != 0) continue;
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
int pos = 0;
|
2018-11-18 12:59:52 +01:00
|
|
|
ec.clear();
|
2018-11-20 21:55:18 +01:00
|
|
|
bdecode_node e = bdecode(buffer.subspan(2), ec, &pos);
|
2018-11-18 12:59:52 +01:00
|
|
|
if (ec)
|
2015-03-12 06:20:12 +01:00
|
|
|
{
|
2015-04-12 00:02:30 +02:00
|
|
|
log("failed to parse extension handshake: %s at pos %d"
|
2015-03-12 06:20:12 +01:00
|
|
|
, ec.message().c_str(), pos);
|
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
TEST_CHECK(!ec);
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2015-04-12 00:02:30 +02:00
|
|
|
log("extension handshake: %s", print_entry(e).c_str());
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node m = e.dict_find_dict("m");
|
2014-09-04 10:55:24 +02:00
|
|
|
TEST_CHECK(m);
|
|
|
|
if (!m) return;
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node dont_have = m.dict_find_int("lt_donthave");
|
2014-09-04 10:55:24 +02:00
|
|
|
TEST_CHECK(dont_have);
|
|
|
|
if (!dont_have) return;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
lt_dont_have = int(dont_have.int_value());
|
2014-09-04 10:55:24 +02:00
|
|
|
}
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
|
|
|
char* ptr = recv_buffer;
|
|
|
|
write_uint32(6, ptr);
|
|
|
|
write_uint8(20, ptr);
|
|
|
|
write_uint8(lt_dont_have, ptr);
|
|
|
|
write_uint32(3, ptr);
|
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::write(s, boost::asio::buffer(recv_buffer, 10)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2015-04-12 00:02:30 +02:00
|
|
|
if (ec) TEST_ERROR(ec.message());
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(1000));
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2015-04-11 17:25:24 +02:00
|
|
|
print_session_log(*ses);
|
|
|
|
|
2014-09-04 10:55:24 +02:00
|
|
|
th.get_peer_info(pi);
|
|
|
|
|
|
|
|
TEST_EQUAL(pi.size(), 1);
|
|
|
|
if (pi.size() != 1) return;
|
|
|
|
|
2017-07-16 20:26:00 +02:00
|
|
|
TEST_CHECK(!(pi[0].flags & peer_info::seed));
|
2014-09-04 10:55:24 +02:00
|
|
|
TEST_EQUAL(pi[0].pieces.count(), pi[0].pieces.size() - 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(pi[0].pieces[piece_index_t(3)], false);
|
|
|
|
TEST_EQUAL(pi[0].pieces[piece_index_t(2)], true);
|
|
|
|
TEST_EQUAL(pi[0].pieces[piece_index_t(1)], true);
|
|
|
|
TEST_EQUAL(pi[0].pieces[piece_index_t(0)], true);
|
2015-04-11 17:25:24 +02:00
|
|
|
|
|
|
|
print_session_log(*ses);
|
2014-09-04 10:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:48 +02:00
|
|
|
TORRENT_TEST(extension_handshake)
|
|
|
|
{
|
|
|
|
using namespace lt::detail;
|
|
|
|
|
|
|
|
sha1_hash ih;
|
|
|
|
std::shared_ptr<lt::session> ses;
|
|
|
|
io_service ios;
|
|
|
|
tcp::socket s(ios);
|
|
|
|
std::shared_ptr<torrent_info> ti = setup_peer(s, ih, ses);
|
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
send_have_all(s);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
entry extensions;
|
|
|
|
send_extension_handshake(s, extensions);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
extensions = read_extension_handshake(s, recv_buffer);
|
2018-07-24 08:18:48 +02:00
|
|
|
|
|
|
|
std::cout << extensions << '\n';
|
|
|
|
|
|
|
|
// these extensions are built-in
|
|
|
|
TEST_CHECK(extensions["m"]["lt_donthave"].integer() != 0);
|
2020-03-22 17:10:25 +01:00
|
|
|
#ifndef TORRENT_DISABLE_SHARE_MODE
|
2018-07-24 08:18:48 +02:00
|
|
|
TEST_CHECK(extensions["m"]["share_mode"].integer() != 0);
|
2020-03-22 17:10:25 +01:00
|
|
|
#endif
|
2018-07-24 08:18:48 +02:00
|
|
|
TEST_CHECK(extensions["m"]["upload_only"].integer() != 0);
|
|
|
|
TEST_CHECK(extensions["m"]["ut_holepunch"].integer() != 0);
|
|
|
|
|
|
|
|
// these require extensions to be enabled
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
TEST_CHECK(extensions["m"]["ut_metadata"].integer() != 0);
|
|
|
|
TEST_CHECK(extensions["m"]["ut_pex"].integer() != 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2015-04-12 00:02:30 +02:00
|
|
|
// TEST metadata extension messages and edge cases
|
|
|
|
|
|
|
|
// this tests sending a request for a metadata piece that's too high. This is
|
|
|
|
// pos
|
2015-06-07 21:31:34 +02:00
|
|
|
TORRENT_TEST(invalid_metadata_request)
|
2015-04-12 00:02:30 +02:00
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt::detail;
|
2015-04-12 00:02:30 +02:00
|
|
|
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2015-04-12 00:02:30 +02:00
|
|
|
io_service ios;
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket s(ios);
|
2016-08-17 23:26:35 +02:00
|
|
|
std::shared_ptr<torrent_info> ti = setup_peer(s, ih, ses);
|
2015-04-12 00:02:30 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
send_have_all(s);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
entry extensions;
|
|
|
|
extensions["m"]["ut_metadata"] = 1;
|
|
|
|
send_extension_handshake(s, extensions);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
extensions = read_extension_handshake(s, recv_buffer);
|
2015-04-12 00:02:30 +02:00
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
int ut_metadata = int(extensions["m"]["ut_metadata"].integer());
|
2015-04-12 02:14:07 +02:00
|
|
|
|
|
|
|
log("ut_metadata: %d", ut_metadata);
|
|
|
|
|
|
|
|
// 0 = request
|
|
|
|
// 1 = piece
|
|
|
|
// 2 = dont-have
|
|
|
|
// first send an invalid request
|
|
|
|
send_ut_metadata_msg(s, ut_metadata, 0, 1);
|
|
|
|
|
|
|
|
// then send a valid one. If we get a response to the second one,
|
|
|
|
// we assume we were not disconnected because of the invalid one
|
|
|
|
send_ut_metadata_msg(s, ut_metadata, 0, 0);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
entry ut_metadata_msg = read_ut_metadata_msg(s, recv_buffer);
|
2015-04-12 00:02:30 +02:00
|
|
|
|
2015-04-12 02:14:07 +02:00
|
|
|
// the first response should be "dont-have"
|
|
|
|
TEST_EQUAL(ut_metadata_msg["msg_type"].integer(), 2);
|
|
|
|
TEST_EQUAL(ut_metadata_msg["piece"].integer(), 1);
|
|
|
|
|
2018-11-20 21:55:18 +01:00
|
|
|
ut_metadata_msg = read_ut_metadata_msg(s, recv_buffer);
|
2015-04-12 02:14:07 +02:00
|
|
|
|
|
|
|
// the second response should be the payload
|
|
|
|
TEST_EQUAL(ut_metadata_msg["msg_type"].integer(), 1);
|
|
|
|
TEST_EQUAL(ut_metadata_msg["piece"].integer(), 0);
|
|
|
|
|
|
|
|
print_session_log(*ses);
|
2015-04-12 00:02:30 +02:00
|
|
|
}
|
2018-07-24 08:18:48 +02:00
|
|
|
#endif // TORRENT_DISABLE_EXTENSIONS
|
|
|
|
|
2015-04-12 00:02:30 +02:00
|
|
|
|
2015-11-06 07:15:48 +01:00
|
|
|
TORRENT_TEST(invalid_request)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test request ===\n" << std::endl;
|
2015-11-06 07:15:48 +01:00
|
|
|
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2015-11-06 07:15:48 +01:00
|
|
|
io_service ios;
|
|
|
|
tcp::socket s(ios);
|
|
|
|
setup_peer(s, ih, ses);
|
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
send_have_none(s);
|
|
|
|
|
|
|
|
peer_request req;
|
2016-12-22 16:42:33 +01:00
|
|
|
req.piece = piece_index_t(124134235);
|
2015-11-06 07:15:48 +01:00
|
|
|
req.start = 0;
|
|
|
|
req.length = 0x4000;
|
|
|
|
send_request(s, req);
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
namespace {
|
|
|
|
|
2016-06-08 03:45:48 +02:00
|
|
|
void have_all_test(bool const incoming)
|
|
|
|
{
|
|
|
|
sha1_hash ih;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<lt::session> ses;
|
2016-06-08 03:45:48 +02:00
|
|
|
io_service ios;
|
|
|
|
tcp::socket s(ios);
|
2017-07-09 16:32:01 +02:00
|
|
|
setup_peer(s, ih, ses, incoming, torrent_flags::seed_mode);
|
2016-06-08 03:45:48 +02:00
|
|
|
|
|
|
|
char recv_buffer[1000];
|
|
|
|
do_handshake(s, ih, recv_buffer);
|
|
|
|
print_session_log(*ses);
|
|
|
|
|
|
|
|
// expect to receive a have-all (not a bitfield)
|
|
|
|
// since we advertised support for FAST extensions
|
|
|
|
for (;;)
|
|
|
|
{
|
2018-11-20 21:55:18 +01:00
|
|
|
int const len = read_message(s, recv_buffer);
|
2016-06-08 03:45:48 +02:00
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
TEST_ERROR("failed to receive have-all despite advertising support for FAST");
|
|
|
|
break;
|
|
|
|
}
|
2018-11-20 21:55:18 +01:00
|
|
|
auto const buffer = span<char const>(recv_buffer).first(len);
|
|
|
|
print_message(buffer);
|
|
|
|
int const msg = buffer[0];
|
2016-06-08 03:45:48 +02:00
|
|
|
if (msg == 0xe) // have-all
|
|
|
|
{
|
|
|
|
// success!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (msg == 5) // bitfield
|
|
|
|
{
|
|
|
|
TEST_ERROR("received bitfield from seed despite advertising support for FAST");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-06-08 03:45:48 +02:00
|
|
|
TORRENT_TEST(outgoing_have_all)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test outgoing have-all ===\n" << std::endl;
|
2016-06-08 03:45:48 +02:00
|
|
|
have_all_test(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(incoming_have_all)
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "\n === test outgoing have-all ===\n" << std::endl;
|
2016-06-08 03:45:48 +02:00
|
|
|
have_all_test(false);
|
|
|
|
}
|
|
|
|
|
2015-06-14 21:48:11 +02:00
|
|
|
// TODO: test sending invalid requests (out of bound piece index, offsets and
|
|
|
|
// sizes)
|