2022-07-16 20:25:44 +00:00
|
|
|
// This file Copyright (C) 2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-07-16 20:25:44 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <algorithm>
|
2022-07-16 20:25:44 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <event2/util.h>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/handshake.h>
|
|
|
|
#include <libtransmission/peer-io.h>
|
|
|
|
#include <libtransmission/session.h> // tr_peerIdInit()
|
|
|
|
#include <libtransmission/timer.h>
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define LOCAL_SOCKETPAIR_AF AF_INET
|
|
|
|
#else
|
|
|
|
#define LOCAL_SOCKETPAIR_AF AF_UNIX
|
|
|
|
#endif
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
auto constexpr MaxWaitMsec = int{ 5000 };
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
class HandshakeTest : public SessionTest
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-12-13 17:59:21 +00:00
|
|
|
class MediatorMock final : public tr_handshake::Mediator
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
public:
|
|
|
|
explicit MediatorMock(tr_session* session)
|
|
|
|
: session_{ session }
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
[[nodiscard]] std::optional<TorrentInfo> torrent(tr_sha1_digest_t const& info_hash) const override
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
if (auto const iter = torrents.find(info_hash); iter != std::end(torrents))
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
return iter->second;
|
2022-07-16 20:25:44 +00:00
|
|
|
}
|
2022-08-30 17:38:30 +00:00
|
|
|
|
|
|
|
return {};
|
2022-07-16 20:25:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
[[nodiscard]] std::optional<TorrentInfo> torrent_from_obfuscated(tr_sha1_digest_t const& obfuscated) const override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
for (auto const& [info_hash, info] : torrents)
|
|
|
|
{
|
|
|
|
if (obfuscated == tr_sha1::digest("req2"sv, info.info_hash))
|
|
|
|
{
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] libtransmission::TimerMaker& timer_maker() override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
2022-10-01 14:12:49 +00:00
|
|
|
return session_->timerMaker();
|
2022-08-30 17:38:30 +00:00
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] bool allows_dht() const override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-26 02:27:11 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] bool allows_tcp() const override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2023-06-22 20:21:44 +00:00
|
|
|
[[nodiscard]] bool is_peer_known_seed(
|
|
|
|
tr_torrent_id_t /*tor_id*/,
|
|
|
|
std::pair<tr_address, tr_port> const& /*socket_address*/) const override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
[[nodiscard]] size_t pad(void* setme, [[maybe_unused]] size_t maxlen) const override
|
|
|
|
{
|
|
|
|
TR_ASSERT(maxlen > 10);
|
|
|
|
auto const len = size_t{ 10 };
|
|
|
|
std::fill_n(static_cast<char*>(setme), 10, ' ');
|
|
|
|
return len;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
[[nodiscard]] tr_message_stream_encryption::DH::private_key_bigend_t private_key() const override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
return private_key_;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2023-06-22 20:21:44 +00:00
|
|
|
void set_utp_failed(tr_sha1_digest_t const& /*info_hash*/, std::pair<tr_address, tr_port> const& /*socket_address*/)
|
|
|
|
override
|
2022-08-30 17:38:30 +00:00
|
|
|
{
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
void setPrivateKeyFromBase64(std::string_view b64)
|
|
|
|
{
|
|
|
|
auto const str = tr_base64_decode(b64);
|
|
|
|
assert(std::size(str) == std::size(private_key_));
|
|
|
|
std::copy_n(reinterpret_cast<std::byte const*>(std::data(str)), std::size(str), std::begin(private_key_));
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_session* const session_;
|
2022-12-13 17:59:21 +00:00
|
|
|
std::map<tr_sha1_digest_t, TorrentInfo> torrents;
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_message_stream_encryption::DH::private_key_bigend_t private_key_ = {};
|
|
|
|
};
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
template<typename Span>
|
|
|
|
void sendToClient(evutil_socket_t sock, Span const& data)
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const* walk = std::data(data);
|
|
|
|
static_assert(sizeof(*walk) == 1);
|
|
|
|
size_t len = std::size(data);
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2022-07-16 20:25:44 +00:00
|
|
|
#if defined(_WIN32)
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const n = send(sock, reinterpret_cast<char const*>(walk), len, 0);
|
2022-07-16 20:25:44 +00:00
|
|
|
#else
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const n = write(sock, walk, len);
|
2022-07-16 20:25:44 +00:00
|
|
|
#endif
|
2022-08-30 17:38:30 +00:00
|
|
|
assert(n >= 0);
|
|
|
|
len -= n;
|
|
|
|
walk += n;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
void sendB64ToClient(evutil_socket_t sock, std::string_view b64)
|
|
|
|
{
|
|
|
|
sendToClient(sock, tr_base64_decode(b64));
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
static auto constexpr ReservedBytesNoExtensions = std::array<uint8_t, 8>{ 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
static auto constexpr PlaintextProtocolName = "\023BitTorrent protocol"sv;
|
|
|
|
|
2023-07-05 01:29:12 +00:00
|
|
|
std::pair<tr_address, tr_port> const DefaultPeerSockAddr{ *tr_address::from_string("127.0.0.1"sv),
|
|
|
|
tr_port::fromHost(8080) };
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_handshake::Mediator::TorrentInfo const TorrentWeAreSeeding{ tr_sha1::digest("abcde"sv),
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_peerIdInit(),
|
|
|
|
tr_torrent_id_t{ 100 },
|
|
|
|
true /*is_done*/ };
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_handshake::Mediator::TorrentInfo const UbuntuTorrent{ *tr_sha1_from_string("2c6b6858d61da9543d4231a71db4b1c9264b0685"sv),
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_peerIdInit(),
|
|
|
|
tr_torrent_id_t{ 101 },
|
|
|
|
false /*is_done*/ };
|
|
|
|
|
|
|
|
auto createIncomingIo(tr_session* session)
|
|
|
|
{
|
|
|
|
auto sockpair = std::array<evutil_socket_t, 2>{ -1, -1 };
|
|
|
|
EXPECT_EQ(0, evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, std::data(sockpair))) << tr_strerror(errno);
|
2023-07-05 01:29:12 +00:00
|
|
|
return std::pair{ tr_peerIo::new_incoming(
|
|
|
|
session,
|
|
|
|
&session->top_bandwidth_,
|
|
|
|
tr_peer_socket(session, DefaultPeerSockAddr, sockpair[0])),
|
|
|
|
sockpair[1] };
|
2022-08-30 17:38:30 +00:00
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
auto createOutgoingIo(tr_session* session, tr_sha1_digest_t const& info_hash)
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
auto sockpair = std::array<evutil_socket_t, 2>{ -1, -1 };
|
|
|
|
EXPECT_EQ(0, evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, std::data(sockpair))) << tr_strerror(errno);
|
2022-12-16 07:23:12 +00:00
|
|
|
auto peer_io = tr_peerIo::create(session, &session->top_bandwidth_, &info_hash, false /*incoming*/, false /*seed*/);
|
2023-07-05 01:29:12 +00:00
|
|
|
peer_io->set_socket(tr_peer_socket(session, DefaultPeerSockAddr, sockpair[0]));
|
|
|
|
return std::pair{ std::move(peer_io), sockpair[1] };
|
2022-07-16 20:25:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
static constexpr auto makePeerId(std::string_view sv)
|
|
|
|
{
|
|
|
|
auto peer_id = tr_peer_id_t{};
|
|
|
|
for (size_t i = 0, n = std::size(sv); i < n; ++i)
|
|
|
|
{
|
|
|
|
peer_id[i] = sv[i];
|
|
|
|
}
|
|
|
|
return peer_id;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
static auto makeRandomPeerId()
|
|
|
|
{
|
2022-11-25 21:04:37 +00:00
|
|
|
auto peer_id = tr_rand_obj<tr_peer_id_t>();
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const peer_id_prefix = "-UW110Q-"sv;
|
|
|
|
std::copy(std::begin(peer_id_prefix), std::end(peer_id_prefix), std::begin(peer_id));
|
|
|
|
return peer_id;
|
|
|
|
}
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
static auto runHandshake(
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_handshake::Mediator* mediator,
|
|
|
|
std::shared_ptr<tr_peerIo> const& peer_io,
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_encryption_mode encryption_mode = TR_CLEAR_PREFERRED)
|
2022-07-16 20:25:44 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
auto result = std::optional<tr_handshake::Result>{};
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto handshake = tr_handshake{ mediator,
|
|
|
|
peer_io,
|
|
|
|
encryption_mode,
|
|
|
|
[&result](auto const& resin)
|
|
|
|
{
|
|
|
|
result = resin;
|
|
|
|
return true;
|
|
|
|
} };
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
waitFor([&result]() { return result.has_value(); }, MaxWaitMsec);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
TEST_F(HandshakeTest, incomingPlaintext)
|
|
|
|
{
|
|
|
|
auto const peer_id = makeRandomPeerId();
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.torrents.emplace(TorrentWeAreSeeding.info_hash, TorrentWeAreSeeding);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// The simplest handshake there is. "The handshake starts with character
|
|
|
|
// nineteen (decimal) followed by the string 'BitTorrent protocol'.
|
|
|
|
// The leading character is a length prefix[.]. After the fixed headers
|
|
|
|
// come eight reserved bytes, which are all zero in all current
|
|
|
|
// implementations[.] Next comes the 20 byte sha1 hash of the bencoded
|
|
|
|
// form of the info value from the metainfo file[.] After the download
|
|
|
|
// hash comes the 20-byte peer id which is reported in tracker requests
|
2022-10-13 14:35:10 +00:00
|
|
|
// and contained in peer lists in tracker responses."
|
|
|
|
// https://www.bittorrent.org/beps/bep_0052.html
|
2022-07-16 20:25:44 +00:00
|
|
|
auto [io, sock] = createIncomingIo(session_);
|
|
|
|
sendToClient(sock, PlaintextProtocolName);
|
|
|
|
sendToClient(sock, ReservedBytesNoExtensions);
|
2022-07-18 18:00:26 +00:00
|
|
|
sendToClient(sock, TorrentWeAreSeeding.info_hash);
|
2022-07-16 20:25:44 +00:00
|
|
|
sendToClient(sock, peer_id);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_TRUE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-07-16 20:25:44 +00:00
|
|
|
EXPECT_EQ(io, res->io);
|
|
|
|
EXPECT_TRUE(res->peer_id);
|
|
|
|
EXPECT_EQ(peer_id, res->peer_id);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(TorrentWeAreSeeding.info_hash, io->torrent_hash());
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The datastream is identical to HandshakeTest.incomingPlaintext,
|
|
|
|
// but this time we don't recognize the infohash sent by the peer.
|
|
|
|
TEST_F(HandshakeTest, incomingPlaintextUnknownInfoHash)
|
|
|
|
{
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.torrents.emplace(TorrentWeAreSeeding.info_hash, TorrentWeAreSeeding);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
auto [io, sock] = createIncomingIo(session_);
|
|
|
|
sendToClient(sock, PlaintextProtocolName);
|
|
|
|
sendToClient(sock, ReservedBytesNoExtensions);
|
2022-07-31 20:58:14 +00:00
|
|
|
sendToClient(sock, tr_sha1::digest("some other torrent unknown to us"sv));
|
2022-07-16 20:25:44 +00:00
|
|
|
sendToClient(sock, makeRandomPeerId());
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_FALSE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-07-16 20:25:44 +00:00
|
|
|
EXPECT_EQ(io, res->io);
|
|
|
|
EXPECT_FALSE(res->peer_id);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(tr_sha1_digest_t{}, io->torrent_hash());
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HandshakeTest, outgoingPlaintext)
|
|
|
|
{
|
|
|
|
auto const peer_id = makeRandomPeerId();
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.torrents.emplace(UbuntuTorrent.info_hash, TorrentWeAreSeeding);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-07-18 18:00:26 +00:00
|
|
|
auto [io, sock] = createOutgoingIo(session_, UbuntuTorrent.info_hash);
|
2022-07-16 20:25:44 +00:00
|
|
|
sendToClient(sock, PlaintextProtocolName);
|
|
|
|
sendToClient(sock, ReservedBytesNoExtensions);
|
2022-07-18 18:00:26 +00:00
|
|
|
sendToClient(sock, UbuntuTorrent.info_hash);
|
2022-07-16 20:25:44 +00:00
|
|
|
sendToClient(sock, peer_id);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_TRUE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-07-16 20:25:44 +00:00
|
|
|
EXPECT_EQ(io, res->io);
|
|
|
|
EXPECT_TRUE(res->peer_id);
|
|
|
|
EXPECT_EQ(peer_id, res->peer_id);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(UbuntuTorrent.info_hash, io->torrent_hash());
|
|
|
|
EXPECT_EQ(tr_sha1_to_string(UbuntuTorrent.info_hash), tr_sha1_to_string(io->torrent_hash()));
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HandshakeTest, incomingEncrypted)
|
|
|
|
{
|
|
|
|
static auto constexpr ExpectedPeerId = makePeerId("-TR300Z-w4bd4mkebkbi"sv);
|
|
|
|
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.torrents.emplace(UbuntuTorrent.info_hash, UbuntuTorrent);
|
|
|
|
mediator.setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
auto [io, sock] = createIncomingIo(session_);
|
|
|
|
|
|
|
|
// Peer->Client data from a successful encrypted handshake recorded
|
|
|
|
// in the wild for replay here
|
|
|
|
sendB64ToClient(
|
|
|
|
sock,
|
|
|
|
"svkySIFcCsrDTeHjPt516UFbsoR+5vfbe5/m6stE7u5JLZ10kJ19NmP64E10qI"
|
|
|
|
"nn78sCrJgjw1yEHHwrzOcKiRlYvcMotzJMe+SjrFUnaw3KBfn2bcKBhxb/sfM9"
|
|
|
|
"J7nJ"sv);
|
|
|
|
sendB64ToClient(
|
|
|
|
sock,
|
|
|
|
"ICAgICAgICAgIKdr4jIBZ4xFfO4xNiRV7Gl2azTSuTFuu06NU1WyRPif018JYe"
|
|
|
|
"VGwrTPstEPu3V5lmzjtMGVLaL5EErlpJ93Xrz+ea6EIQEUZA+D4jKaV/to9NVi"
|
|
|
|
"04/1W1A2PHgg+I9puac/i9BsFPcjdQeoVtU73lNCbTDQgTieyjDWmwo="sv);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_TRUE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-07-16 20:25:44 +00:00
|
|
|
EXPECT_EQ(io, res->io);
|
|
|
|
EXPECT_TRUE(res->peer_id);
|
|
|
|
EXPECT_EQ(ExpectedPeerId, res->peer_id);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(UbuntuTorrent.info_hash, io->torrent_hash());
|
|
|
|
EXPECT_EQ(tr_sha1_to_string(UbuntuTorrent.info_hash), tr_sha1_to_string(io->torrent_hash()));
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The datastream is identical to HandshakeTest.incomingEncrypted,
|
|
|
|
// but this time we don't recognize the infohash sent by the peer.
|
|
|
|
TEST_F(HandshakeTest, incomingEncryptedUnknownInfoHash)
|
|
|
|
{
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
auto [io, sock] = createIncomingIo(session_);
|
|
|
|
|
|
|
|
// Peer->Client data from a successful encrypted handshake recorded
|
|
|
|
// in the wild for replay here
|
|
|
|
sendB64ToClient(
|
|
|
|
sock,
|
|
|
|
"svkySIFcCsrDTeHjPt516UFbsoR+5vfbe5/m6stE7u5JLZ10kJ19NmP64E10qI"
|
|
|
|
"nn78sCrJgjw1yEHHwrzOcKiRlYvcMotzJMe+SjrFUnaw3KBfn2bcKBhxb/sfM9"
|
|
|
|
"J7nJ"sv);
|
|
|
|
sendB64ToClient(
|
|
|
|
sock,
|
|
|
|
"ICAgICAgICAgIKdr4jIBZ4xFfO4xNiRV7Gl2azTSuTFuu06NU1WyRPif018JYe"
|
|
|
|
"VGwrTPstEPu3V5lmzjtMGVLaL5EErlpJ93Xrz+ea6EIQEUZA+D4jKaV/to9NVi"
|
|
|
|
"04/1W1A2PHgg+I9puac/i9BsFPcjdQeoVtU73lNCbTDQgTieyjDWmwo="sv);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_FALSE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(tr_sha1_digest_t{}, io->torrent_hash());
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HandshakeTest, outgoingEncrypted)
|
|
|
|
{
|
|
|
|
static auto constexpr ExpectedPeerId = makePeerId("-qB4250-scysDI_JuVN3"sv);
|
|
|
|
|
2022-12-05 17:47:11 +00:00
|
|
|
auto mediator = MediatorMock{ session_ };
|
|
|
|
mediator.torrents.emplace(UbuntuTorrent.info_hash, UbuntuTorrent);
|
|
|
|
mediator.setPrivateKeyFromBase64("0EYKCwBWQ4Dg9kX3c5xxjVtBDKw="sv);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
2022-07-18 18:00:26 +00:00
|
|
|
auto [io, sock] = createOutgoingIo(session_, UbuntuTorrent.info_hash);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// Peer->Client data from a successful encrypted handshake recorded
|
|
|
|
// in the wild for replay here
|
|
|
|
sendB64ToClient(
|
|
|
|
sock,
|
|
|
|
"Sfgoq/nrQfD4Iwirfk+uhOmQMOC/QwK/vYiOact1NF9TpWXms3cvlKEKxs0VU"
|
|
|
|
"mnmytRh9bh4Lcs1bswlC6R05XrJGzLhZqAqcLUUAR1VTLA5oKSjR1038zFbhn"
|
|
|
|
"c71jqlpney15ChMTnx02Qt+88l0Z9OWLUUJrUVy+OoIaTMSKDDFVOjuj0y+Ii"
|
|
|
|
"cE0ZnN61e0/R/g+APRK5tegw0SLZ3Nr8+y4Dl77sZyc141PR9xvDj0da1eAvf"
|
|
|
|
"BvXyyDem4vUjqiLUNCEV8KDXEMPCPYAQoDZzLvMyOEtJM/if0o0UN88SWtt1k"
|
|
|
|
"jRD8UNvUlXIfM0YsnJhKA6fJ7/4geK7+Wo2aicfaLFOyG5IEJbTg9OQYbDHFa"
|
|
|
|
"oVzD0xY0Dx+J0loqM+CzrPj8UpeXIcbD7pJrT3XPECbFQ12cCY5LW5RymVIx8"
|
|
|
|
"TP0ajGiTxou1L7DbGD54SYgV/4qFbafRsWp9AO+YDJcouFd/jiVN+r3loxvfT"
|
|
|
|
"0A9H9DRAMR0rZKpQpXZ1ZAhAuAOXGHFIvtw8wd6dPybeu5+LoR2S90/IpwHWI"
|
|
|
|
"jbNbypQZuA9hn4JfFMWPP9TG/E11loB4+MkrP22U72ezjL5ipd74AEEP0/u8w"
|
|
|
|
"Gj1t2kXhND9ONfasA+pY25y8GM04M0B7+0xKmsHP7tntwQLAGZATH83rOxaSO"
|
|
|
|
"3+o/RdiKQJAsGxMIU08scBc5VOmrAmjeYrLNpFnpXVuavH5if7490zMCu3DEn"
|
|
|
|
"G9hpbYbiX95T+EUcRbM6pSCvr3Twq1Q="sv);
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const res = runHandshake(&mediator, io, TR_ENCRYPTION_PREFERRED);
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
// check the results
|
2023-01-26 16:50:42 +00:00
|
|
|
EXPECT_TRUE(res.has_value());
|
|
|
|
assert(res.has_value());
|
2022-12-13 17:59:21 +00:00
|
|
|
EXPECT_TRUE(res->is_connected);
|
|
|
|
EXPECT_TRUE(res->read_anything_from_peer);
|
2022-07-16 20:25:44 +00:00
|
|
|
EXPECT_EQ(io, res->io);
|
|
|
|
EXPECT_TRUE(res->peer_id);
|
|
|
|
EXPECT_EQ(ExpectedPeerId, res->peer_id);
|
2022-12-16 07:23:12 +00:00
|
|
|
EXPECT_EQ(UbuntuTorrent.info_hash, io->torrent_hash());
|
|
|
|
EXPECT_EQ(tr_sha1_to_string(UbuntuTorrent.info_hash), tr_sha1_to_string(io->torrent_hash()));
|
2022-07-16 20:25:44 +00:00
|
|
|
|
|
|
|
evutil_closesocket(sock);
|
|
|
|
}
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|