2009-09-27 05:38:41 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
|
2010-02-05 09:40:08 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
2013-12-27 05:28:25 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2009-09-27 05:38:41 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
2016-09-22 08:04:05 +02:00
|
|
|
#include "libtorrent/kademlia/msg.hpp" // for verify_message
|
|
|
|
#include "libtorrent/kademlia/node.hpp"
|
2009-09-27 19:41:51 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
2016-09-17 15:42:04 +02:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2011-05-23 02:45:36 +02:00
|
|
|
#include "libtorrent/socket_io.hpp" // for hash_address
|
2012-03-17 20:27:57 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for supports_ipv6
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp" // for counters
|
|
|
|
#include "libtorrent/random.hpp"
|
2016-08-30 02:28:42 +02:00
|
|
|
#include "libtorrent/kademlia/ed25519.hpp"
|
2016-05-23 14:15:39 +02:00
|
|
|
#include "libtorrent/hex.hpp" // to_hex, from_hex
|
2016-09-27 00:04:42 +02:00
|
|
|
#include "libtorrent/bloom_filter.hpp"
|
2013-09-01 03:10:50 +02:00
|
|
|
|
|
|
|
#include "libtorrent/kademlia/node_id.hpp"
|
|
|
|
#include "libtorrent/kademlia/routing_table.hpp"
|
2013-12-27 05:28:25 +01:00
|
|
|
#include "libtorrent/kademlia/item.hpp"
|
2015-05-09 20:06:02 +02:00
|
|
|
#include "libtorrent/kademlia/dht_observer.hpp"
|
2016-05-23 14:15:39 +02:00
|
|
|
|
2013-10-22 07:20:00 +02:00
|
|
|
#include <numeric>
|
2015-12-01 04:26:10 +01:00
|
|
|
#include <cstdarg>
|
2016-05-03 14:29:27 +02:00
|
|
|
#include <tuple>
|
2016-05-23 14:15:39 +02:00
|
|
|
#include <iostream>
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2013-06-26 21:16:21 +02:00
|
|
|
#include "setup_transfer.hpp"
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2009-09-27 19:41:51 +02:00
|
|
|
using namespace libtorrent;
|
2011-01-19 06:57:44 +01:00
|
|
|
using namespace libtorrent::dht;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
namespace {
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
void get_test_keypair(public_key& pk, secret_key& sk)
|
2016-07-20 16:53:33 +02:00
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
aux::from_hex({"77ff84905a91936367c01360803104f92432fcd904a43511876df5cdf3e7e548", 64}, pk.bytes.data());
|
|
|
|
aux::from_hex({"e06d3183d14159228433ed599221b80bd0a5ce8352e4bdf0262f76786ef1c74d"
|
|
|
|
"b7e7a9fea2c0eb269d61e3b38e450a22e754941ac78479d6c54e1faf6037881d", 128}, sk.bytes.data());
|
2016-07-24 00:57:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sequence_number prev_seq(sequence_number s)
|
|
|
|
{
|
|
|
|
return sequence_number(s.value - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence_number next_seq(sequence_number s)
|
|
|
|
{
|
|
|
|
return sequence_number(s.value + 1);
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
|
2013-09-01 03:10:50 +02:00
|
|
|
void add_and_replace(libtorrent::dht::node_id& dst, libtorrent::dht::node_id const& add)
|
|
|
|
{
|
|
|
|
bool carry = false;
|
|
|
|
for (int k = 19; k >= 0; --k)
|
|
|
|
{
|
|
|
|
int sum = dst[k] + add[k] + (carry?1:0);
|
|
|
|
dst[k] = sum & 255;
|
|
|
|
carry = sum > 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void node_push_back(void* userdata, libtorrent::dht::node_entry const& n)
|
|
|
|
{
|
|
|
|
using namespace libtorrent::dht;
|
|
|
|
std::vector<node_entry>* nv = (std::vector<node_entry>*)userdata;
|
|
|
|
nv->push_back(n);
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
void nop(void* userdata, libtorrent::dht::node_entry const& n) {}
|
|
|
|
void nop_node() {}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2013-12-27 05:28:25 +01:00
|
|
|
std::list<std::pair<udp::endpoint, entry> > g_sent_packets;
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2016-04-30 17:05:54 +02:00
|
|
|
struct mock_socket final : udp_socket_interface
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2016-04-30 17:05:54 +02:00
|
|
|
bool has_quota() override { return true; }
|
|
|
|
bool send_packet(entry& msg, udp::endpoint const& ep) override
|
2012-04-30 07:39:35 +02:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: 3 ideally the mock_socket would contain this queue of packets, to
|
2015-12-01 00:02:00 +01:00
|
|
|
// make tests independent
|
2013-12-27 05:28:25 +01:00
|
|
|
g_sent_packets.push_back(std::make_pair(ep, msg));
|
2012-04-30 07:39:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2011-01-19 06:57:44 +01:00
|
|
|
|
|
|
|
sha1_hash generate_next()
|
|
|
|
{
|
|
|
|
sha1_hash ret;
|
2014-02-05 10:38:32 +01:00
|
|
|
for (int i = 0; i < 20; ++i) ret[i] = rand() & 0xff;
|
2011-01-19 06:57:44 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 05:28:25 +01:00
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator
|
|
|
|
find_packet(udp::endpoint ep)
|
|
|
|
{
|
|
|
|
return std::find_if(g_sent_packets.begin(), g_sent_packets.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, [&ep] (std::pair<udp::endpoint, entry> const& p)
|
|
|
|
{ return p.first == ep; });
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
void lazy_from_entry(entry const& e, bdecode_node& l)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
static char inbuf[1500];
|
|
|
|
int len = bencode(inbuf, e);
|
2015-03-12 06:20:12 +01:00
|
|
|
int ret = bdecode(inbuf, inbuf + len, l, ec);
|
2013-12-27 05:28:25 +01:00
|
|
|
TEST_CHECK(ret == 0);
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:08:15 +02:00
|
|
|
entry write_peers(std::set<tcp::endpoint> const& peers)
|
2015-09-28 01:03:58 +02:00
|
|
|
{
|
2016-09-19 02:08:15 +02:00
|
|
|
entry r;
|
|
|
|
entry::list_type& pe = r.list();
|
2016-05-03 14:29:27 +02:00
|
|
|
for (auto const& p : peers)
|
2015-09-28 01:03:58 +02:00
|
|
|
{
|
|
|
|
std::string endpoint(18, '\0');
|
|
|
|
std::string::iterator out = endpoint.begin();
|
2016-05-03 14:29:27 +02:00
|
|
|
libtorrent::detail::write_endpoint(p, out);
|
2015-09-28 01:03:58 +02:00
|
|
|
endpoint.resize(out - endpoint.begin());
|
|
|
|
pe.push_back(entry(endpoint));
|
|
|
|
}
|
2016-09-19 02:08:15 +02:00
|
|
|
return r;
|
2015-09-28 01:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct msg_args
|
|
|
|
{
|
|
|
|
msg_args& info_hash(char const* i)
|
|
|
|
{ if (i) a["info_hash"] = std::string(i, 20); return *this; }
|
|
|
|
|
|
|
|
msg_args& name(char const* n)
|
|
|
|
{ if (n) a["n"] = n; return *this; }
|
|
|
|
|
|
|
|
msg_args& token(std::string t)
|
|
|
|
{ a["token"] = t; return *this; }
|
|
|
|
|
|
|
|
msg_args& port(int p)
|
|
|
|
{ a["port"] = p; return *this; }
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& target(sha1_hash const& t)
|
|
|
|
{ a["target"] = t.to_string(); return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
|
|
|
msg_args& value(entry const& v)
|
|
|
|
{ a["v"] = v; return *this; }
|
|
|
|
|
|
|
|
msg_args& scrape(bool s)
|
|
|
|
{ a["scrape"] = s ? 1 : 0; return *this; }
|
|
|
|
|
|
|
|
msg_args& seed(bool s)
|
|
|
|
{ a["seed"] = s ? 1 : 0; return *this; }
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& key(public_key const& k)
|
|
|
|
{ a["k"] = k.bytes; return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& sig(signature const& s)
|
|
|
|
{ a["sig"] = s.bytes; return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& seq(sequence_number s)
|
|
|
|
{ a["seq"] = s.value; return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& cas(sequence_number c)
|
|
|
|
{ a["cas"] = c.value; return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
|
|
|
msg_args& nid(sha1_hash const& n)
|
|
|
|
{ a["id"] = n.to_string(); return *this; }
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
msg_args& salt(span<char const> s)
|
|
|
|
{ if (!s.empty()) a["salt"] = s; return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
|
|
|
msg_args& want(std::string w)
|
|
|
|
{ a["want"].list().push_back(w); return *this; }
|
|
|
|
|
2016-09-19 02:08:15 +02:00
|
|
|
msg_args& nodes(std::vector<node_entry> const& n)
|
|
|
|
{ if (!n.empty()) a["nodes"] = dht::write_nodes_entry(n); return *this; }
|
2015-11-14 06:08:57 +01:00
|
|
|
|
2016-09-19 02:08:15 +02:00
|
|
|
msg_args& nodes6(std::vector<node_entry> const& n)
|
|
|
|
{ if (!n.empty()) a["nodes6"] = dht::write_nodes_entry(n); return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
|
|
|
msg_args& peers(std::set<tcp::endpoint> const& p)
|
2016-09-19 02:08:15 +02:00
|
|
|
{ if (!p.empty()) a.dict()["values"] = write_peers(p); return *this; }
|
2015-09-28 01:03:58 +02:00
|
|
|
|
|
|
|
entry a;
|
|
|
|
};
|
2015-09-04 00:49:59 +02:00
|
|
|
|
2015-05-09 21:00:22 +02:00
|
|
|
void send_dht_request(node& node, char const* msg, udp::endpoint const& ep
|
2015-11-23 04:02:37 +01:00
|
|
|
, bdecode_node* reply, msg_args const& args = msg_args()
|
|
|
|
, char const* t = "10", bool has_response = true)
|
2009-09-27 19:41:51 +02:00
|
|
|
{
|
2011-01-22 19:44:48 +01:00
|
|
|
// we're about to clear out the backing buffer
|
|
|
|
// for this lazy_entry, so we better clear it now
|
|
|
|
reply->clear();
|
2009-09-27 19:41:51 +02:00
|
|
|
entry e;
|
|
|
|
e["q"] = msg;
|
|
|
|
e["t"] = t;
|
|
|
|
e["y"] = "q";
|
2015-09-28 01:03:58 +02:00
|
|
|
e["a"] = args.a;
|
|
|
|
e["a"].dict().insert(std::make_pair("id", generate_next().to_string()));
|
2009-09-27 19:41:51 +02:00
|
|
|
char msg_buf[1500];
|
|
|
|
int size = bencode(msg_buf, e);
|
2013-10-16 10:11:19 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node decoded;
|
2011-05-01 21:28:13 +02:00
|
|
|
error_code ec;
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode(msg_buf, msg_buf + size, decoded, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("bdecode failed: %s\n", ec.message().c_str());
|
2011-05-01 21:28:13 +02:00
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
dht::msg m(decoded, ep);
|
|
|
|
node.incoming(m);
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2015-11-23 04:02:37 +01:00
|
|
|
// If the request is supposed to get a response, by now the node should have
|
|
|
|
// invoked the send function and put the response in g_sent_packets
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator i = find_packet(ep);
|
|
|
|
if (has_response)
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2015-11-23 04:02:37 +01:00
|
|
|
if (i == g_sent_packets.end())
|
|
|
|
{
|
|
|
|
TEST_ERROR("not response from DHT node");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_from_entry(i->second, *reply);
|
|
|
|
g_sent_packets.erase(i);
|
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-23 04:02:37 +01:00
|
|
|
// this request suppose won't be responsed.
|
|
|
|
if (i != g_sent_packets.end())
|
|
|
|
{
|
|
|
|
TEST_ERROR("shouldn't have response from DHT node");
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:00:22 +02:00
|
|
|
void send_dht_response(node& node, bdecode_node const& request, udp::endpoint const& ep
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args const& args = msg_args())
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
entry e;
|
|
|
|
e["y"] = "r";
|
2016-08-13 13:04:53 +02:00
|
|
|
e["t"] = request.dict_find_string_value("t").to_string();
|
2013-12-27 05:28:25 +01:00
|
|
|
// e["ip"] = endpoint_to_bytes(ep);
|
2015-09-28 01:03:58 +02:00
|
|
|
e["r"] = args.a;
|
|
|
|
e["r"].dict().insert(std::make_pair("id", generate_next().to_string()));
|
2013-12-27 05:28:25 +01:00
|
|
|
char msg_buf[1500];
|
2016-07-20 16:53:33 +02:00
|
|
|
int const size = bencode(msg_buf, e);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node decoded;
|
2013-12-27 05:28:25 +01:00
|
|
|
error_code ec;
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode(msg_buf, msg_buf + size, decoded, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("bdecode failed: %s\n", ec.message().c_str());
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
dht::msg m(decoded, ep);
|
|
|
|
node.incoming(m);
|
2009-09-27 19:41:51 +02:00
|
|
|
}
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
struct announce_item
|
2009-09-27 19:41:51 +02:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
announce_item(sha1_hash nxt, int const num)
|
|
|
|
: next(nxt)
|
|
|
|
, num_peers(num)
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2016-01-13 04:32:02 +01:00
|
|
|
num_peers = (rand() % 5) + 2;
|
2011-01-19 06:57:44 +01:00
|
|
|
ent["next"] = next.to_string();
|
|
|
|
ent["A"] = "a";
|
|
|
|
ent["B"] = "b";
|
|
|
|
ent["num_peers"] = num_peers;
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
char* ptr = buf;
|
|
|
|
int len = bencode(ptr, ent);
|
|
|
|
target = hasher(buf, len).final();
|
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
sha1_hash next;
|
|
|
|
int num_peers;
|
|
|
|
entry ent;
|
|
|
|
sha1_hash target;
|
2011-01-19 06:57:44 +01:00
|
|
|
};
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2015-05-09 21:00:22 +02:00
|
|
|
void announce_immutable_items(node& node, udp::endpoint const* eps
|
2011-05-25 04:26:07 +02:00
|
|
|
, announce_item const* items, int num_items)
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2011-05-25 04:26:07 +02:00
|
|
|
std::string token;
|
2011-01-19 06:57:44 +01:00
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < num_items; ++j)
|
|
|
|
{
|
|
|
|
if ((i % items[j].num_peers) == 0) continue;
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node response;
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "get", eps[i], &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(items[j].target));
|
2016-07-20 16:53:33 +02:00
|
|
|
|
|
|
|
key_desc_t const desc[] =
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0},
|
|
|
|
{ "token", bdecode_node::string_t, 0, 0},
|
|
|
|
{ "ip", bdecode_node::string_t, 0, key_desc_t::optional | key_desc_t::last_child},
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0},
|
2011-01-19 06:57:44 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node parsed[5];
|
2011-01-19 06:57:44 +01:00
|
|
|
char error_string[200];
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
// std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
int ret = verify_message(response, desc, parsed, error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
TEST_EQUAL(parsed[4].string_value(), "r");
|
2016-08-13 13:04:53 +02:00
|
|
|
token = parsed[2].string_value().to_string();
|
2016-10-10 02:23:45 +02:00
|
|
|
// std::printf("got token: %s\n", token.c_str());
|
2011-01-19 06:57:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid get response: %s\n", error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
TEST_ERROR(error_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed[3])
|
|
|
|
{
|
|
|
|
address_v4::bytes_type b;
|
2015-03-12 06:20:12 +01:00
|
|
|
memcpy(&b[0], parsed[3].string_ptr(), b.size());
|
2011-01-19 06:57:44 +01:00
|
|
|
address_v4 addr(b);
|
|
|
|
TEST_EQUAL(addr, eps[i].address());
|
|
|
|
}
|
|
|
|
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "put", eps[i], &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.token(token)
|
2016-07-24 00:57:04 +02:00
|
|
|
.target(items[j].target)
|
2015-09-28 01:03:58 +02:00
|
|
|
.value(items[j].ent));
|
2011-01-19 06:57:44 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
key_desc_t const desc2[] =
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0 }
|
2011-01-19 06:57:44 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node parsed2[1];
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc2, parsed2, error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
if (parsed2[0].string_value() != "r")
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2013-06-23 03:22:44 +02:00
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(parsed2[0].string_value(), "r");
|
2011-01-19 06:57:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid put response: %s\n", error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
TEST_ERROR(error_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
std::set<int> items_num;
|
|
|
|
for (int j = 0; j < num_items; ++j)
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node response;
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "get", eps[j], &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(items[j].target));
|
2015-09-28 01:03:58 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
key_desc_t const desc[] =
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "v", bdecode_node::dict_t, 0, 0},
|
|
|
|
{ "id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0},
|
2011-01-19 06:57:44 +01:00
|
|
|
};
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node parsed[4];
|
2011-01-19 06:57:44 +01:00
|
|
|
char error_string[200];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
int ret = verify_message(response, desc, parsed, error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-05-25 04:26:07 +02:00
|
|
|
items_num.insert(items_num.begin(), j);
|
2011-05-23 07:07:52 +02:00
|
|
|
}
|
2011-01-19 06:57:44 +01:00
|
|
|
}
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2016-01-13 04:32:02 +01:00
|
|
|
// TODO: check to make sure the "best" items are stored
|
2011-01-19 06:57:44 +01:00
|
|
|
TEST_EQUAL(items_num.size(), 4);
|
|
|
|
}
|
|
|
|
|
2013-10-22 07:20:00 +02:00
|
|
|
int sum_distance_exp(int s, node_entry const& e, node_id const& ref)
|
|
|
|
{
|
|
|
|
return s + distance_exp(e.id, ref);
|
|
|
|
}
|
|
|
|
|
2013-12-27 05:28:25 +01:00
|
|
|
std::vector<tcp::endpoint> g_got_peers;
|
|
|
|
|
|
|
|
void get_peers_cb(std::vector<tcp::endpoint> const& peers)
|
|
|
|
{
|
|
|
|
g_got_peers.insert(g_got_peers.end(), peers.begin(), peers.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<dht::item> g_got_items;
|
|
|
|
dht::item g_put_item;
|
|
|
|
int g_put_count;
|
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void get_mutable_item_cb(dht::item const& i, bool a)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
2015-09-22 20:10:57 +02:00
|
|
|
if (!a) return;
|
2013-12-27 05:28:25 +01:00
|
|
|
if (!i.empty())
|
|
|
|
g_got_items.push_back(i);
|
2015-09-22 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void put_mutable_item_data_cb(dht::item& i)
|
|
|
|
{
|
|
|
|
if (!i.empty())
|
|
|
|
g_got_items.push_back(i);
|
|
|
|
|
|
|
|
TEST_CHECK(!g_put_item.empty());
|
|
|
|
i = g_put_item;
|
|
|
|
g_put_count++;
|
|
|
|
}
|
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void put_mutable_item_cb(dht::item const&, int num, int expect)
|
2015-09-22 20:10:57 +02:00
|
|
|
{
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(num, expect);
|
2015-09-22 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void get_immutable_item_cb(dht::item const& i)
|
2015-09-22 20:10:57 +02:00
|
|
|
{
|
|
|
|
if (!i.empty())
|
|
|
|
g_got_items.push_back(i);
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
void put_immutable_item_cb(int num, int expect)
|
2015-09-22 20:10:57 +02:00
|
|
|
{
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(num, expect);
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 20:06:02 +02:00
|
|
|
struct obs : dht::dht_observer
|
|
|
|
{
|
2016-07-10 02:10:38 +02:00
|
|
|
void set_external_address(address const& addr
|
2016-04-30 17:05:54 +02:00
|
|
|
, address const& source) override
|
2016-07-27 08:38:48 +02:00
|
|
|
{
|
|
|
|
m_external_address = addr;
|
|
|
|
}
|
2015-05-09 20:06:02 +02:00
|
|
|
|
2016-07-10 02:10:38 +02:00
|
|
|
address external_address(udp proto) override
|
2015-05-09 20:06:02 +02:00
|
|
|
{
|
2016-07-27 08:38:48 +02:00
|
|
|
return m_external_address;
|
2015-05-09 20:06:02 +02:00
|
|
|
}
|
2016-07-10 02:10:38 +02:00
|
|
|
void get_peers(sha1_hash const& ih) override {}
|
|
|
|
void outgoing_get_peers(sha1_hash const& target
|
2016-04-30 17:05:54 +02:00
|
|
|
, sha1_hash const& sent_target, udp::endpoint const& ep) override {}
|
2016-07-10 02:10:38 +02:00
|
|
|
void announce(sha1_hash const& ih, address const& addr, int port) override {}
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-09 01:13:47 +02:00
|
|
|
bool should_log(module_t) const override { return true; }
|
2016-07-10 02:10:38 +02:00
|
|
|
void log(dht_logger::module_t l, char const* fmt, ...) override
|
2015-12-01 01:07:35 +01:00
|
|
|
{
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
char buf[1024];
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, v);
|
|
|
|
va_end(v);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", buf);
|
2015-12-01 01:07:35 +01:00
|
|
|
m_log.push_back(buf);
|
|
|
|
}
|
2016-09-28 05:21:18 +02:00
|
|
|
void log_packet(message_direction_t dir, span<char const> pkt
|
2016-09-16 03:13:43 +02:00
|
|
|
, udp::endpoint const& node) override {}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2016-08-15 22:17:13 +02:00
|
|
|
bool on_dht_request(string_view query
|
2016-04-30 17:05:54 +02:00
|
|
|
, dht::msg const& request, entry& response) override { return false; }
|
2015-12-01 01:07:35 +01:00
|
|
|
|
2016-07-27 08:38:48 +02:00
|
|
|
address m_external_address = addr4("236.0.0.1");
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-12-01 01:07:35 +01:00
|
|
|
std::vector<std::string> m_log;
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2015-05-09 20:06:02 +02:00
|
|
|
};
|
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
dht_settings test_settings()
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
|
|
|
dht_settings sett;
|
|
|
|
sett.max_torrents = 4;
|
2011-05-23 07:07:52 +02:00
|
|
|
sett.max_dht_items = 4;
|
2013-10-14 03:03:43 +02:00
|
|
|
sett.enforce_node_id = false;
|
2015-08-23 09:23:51 +02:00
|
|
|
return sett;
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
struct dht_test_setup
|
2015-08-23 09:23:51 +02:00
|
|
|
{
|
2016-10-09 20:51:24 +02:00
|
|
|
explicit dht_test_setup(udp::endpoint src)
|
2016-07-20 16:53:33 +02:00
|
|
|
: sett(test_settings())
|
|
|
|
, dht_storage(dht_default_storage_constructor(sett))
|
|
|
|
, source(src)
|
|
|
|
, dht_node(src.protocol(), &s, sett
|
|
|
|
, node_id(nullptr), &observer, cnt, nodes, *dht_storage)
|
|
|
|
{
|
|
|
|
dht_storage->update_node_ids({node_id::min()});
|
|
|
|
}
|
|
|
|
dht_settings sett;
|
2012-04-30 07:39:35 +02:00
|
|
|
mock_socket s;
|
2015-05-09 20:06:02 +02:00
|
|
|
obs observer;
|
2014-07-06 21:18:00 +02:00
|
|
|
counters cnt;
|
2016-07-20 16:53:33 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> dht_storage;
|
|
|
|
udp::endpoint source;
|
2015-11-14 06:08:57 +01:00
|
|
|
std::map<std::string, node*> nodes;
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::node dht_node;
|
|
|
|
char error_string[200];
|
|
|
|
};
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const err_desc[] = {
|
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"e", bdecode_node::list_t, 2, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
dht::key_desc_t const peer1_desc[] = {
|
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"r", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"token", bdecode_node::string_t, 0, 0},
|
|
|
|
{"id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
|
|
|
dht::key_desc_t const get_item_desc[] = {
|
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 3, 0},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"target", bdecode_node::string_t, 20, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
|
|
|
dht::key_desc_t const put_mutable_item_desc[] = {
|
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 3, 0},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"cas", bdecode_node::string_t, 20, key_desc_t::optional},
|
2016-07-24 00:57:04 +02:00
|
|
|
{"k", bdecode_node::string_t, public_key::len, 0},
|
2016-07-20 16:53:33 +02:00
|
|
|
{"seq", bdecode_node::int_t, 0, 0},
|
2016-07-24 00:57:04 +02:00
|
|
|
{"sig", bdecode_node::string_t, signature::len, 0},
|
2016-07-20 16:53:33 +02:00
|
|
|
{"token", bdecode_node::string_t, 2, 0},
|
|
|
|
{"v", bdecode_node::none_t, 0, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
void print_state(std::ostream& os, routing_table const& table)
|
|
|
|
{
|
|
|
|
std::vector<char> buf(2048);
|
|
|
|
int cursor = 0;
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "kademlia routing table state\n"
|
|
|
|
"bucket_size: %d\n"
|
|
|
|
"global node count: %" PRId64 "\n"
|
|
|
|
"node_id: %s\n\n"
|
|
|
|
"number of nodes per bucket:\n"
|
|
|
|
, table.bucket_size()
|
|
|
|
, table.num_global_nodes()
|
|
|
|
, aux::to_hex(table.id()).c_str());
|
|
|
|
if (cursor > int(buf.size()) - 500) buf.resize(buf.size() * 3 / 2);
|
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
for (auto i = table.buckets().begin(), end(table.buckets().end());
|
|
|
|
i != end; ++i, ++idx)
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "%2d: ", idx);
|
|
|
|
for (int k = 0; k < int(i->live_nodes.size()); ++k)
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor, "#");
|
|
|
|
for (int k = 0; k < int(i->replacements.size()); ++k)
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor, "-");
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor, "\n");
|
|
|
|
|
|
|
|
if (cursor > int(buf.size()) - 500) buf.resize(buf.size() * 3 / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_point now = aux::time_now();
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "\nnodes:");
|
|
|
|
|
|
|
|
int bucket_index = 0;
|
|
|
|
for (auto i = table.buckets().begin(), end(table.buckets().end());
|
|
|
|
i != end; ++i, ++bucket_index)
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "\n=== BUCKET == %d == %d|%d ==== \n"
|
|
|
|
, bucket_index, int(i->live_nodes.size())
|
|
|
|
, int(i->replacements.size()));
|
|
|
|
if (cursor > int(buf.size()) - 500) buf.resize(buf.size() * 3 / 2);
|
|
|
|
|
|
|
|
int id_shift;
|
|
|
|
// the last bucket is special, since it hasn't been split yet, it
|
|
|
|
// includes that top bit as well
|
|
|
|
if (bucket_index + 1 == int(table.buckets().size()))
|
|
|
|
id_shift = bucket_index;
|
|
|
|
else
|
|
|
|
id_shift = bucket_index + 1;
|
|
|
|
|
|
|
|
for (bucket_t::const_iterator j = i->live_nodes.begin()
|
|
|
|
, end2(i->live_nodes.end()); j != end2; ++j)
|
|
|
|
{
|
|
|
|
int bucket_size_limit = table.bucket_limit(bucket_index);
|
|
|
|
std::uint32_t top_mask = bucket_size_limit - 1;
|
|
|
|
int mask_shift = 0;
|
|
|
|
TORRENT_ASSERT_VAL(bucket_size_limit > 0, bucket_size_limit);
|
|
|
|
while ((top_mask & 0x80) == 0)
|
|
|
|
{
|
|
|
|
top_mask <<= 1;
|
|
|
|
++mask_shift;
|
|
|
|
}
|
|
|
|
top_mask = (0xff << mask_shift) & 0xff;
|
|
|
|
|
|
|
|
node_id id = j->id;
|
|
|
|
id <<= id_shift;
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " prefix: %2x id: %s"
|
|
|
|
, ((id[0] & top_mask) >> mask_shift)
|
|
|
|
, aux::to_hex(j->id).c_str());
|
|
|
|
|
|
|
|
if (j->rtt == 0xffff)
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " rtt: ");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " rtt: %4d", j->rtt);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " fail: %4d ping: %d dist: %3d"
|
|
|
|
, j->fail_count()
|
|
|
|
, j->pinged()
|
|
|
|
, distance_exp(table.id(), j->id));
|
|
|
|
|
|
|
|
if (j->last_queried == min_time())
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " query: ");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " query: %3d", int(total_seconds(now - j->last_queried)));
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, " ip: %s\n", print_endpoint(j->ep()).c_str());
|
|
|
|
if (cursor > int(buf.size()) - 500) buf.resize(buf.size() * 3 / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "\nnode spread per bucket:\n");
|
|
|
|
bucket_index = 0;
|
|
|
|
for (auto i = table.buckets().begin(), end(table.buckets().end());
|
|
|
|
i != end; ++i, ++bucket_index)
|
|
|
|
{
|
|
|
|
int bucket_size_limit = table.bucket_limit(bucket_index);
|
2016-07-20 16:53:33 +02:00
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
// mask out the first 3 bits, or more depending
|
|
|
|
// on the bucket_size_limit
|
|
|
|
// we have all the lower bits set in (bucket_size_limit-1)
|
|
|
|
// but we want the left-most bits to be set. Shift it
|
|
|
|
// until the MSB is set
|
|
|
|
std::uint32_t top_mask = bucket_size_limit - 1;
|
|
|
|
int mask_shift = 0;
|
|
|
|
TORRENT_ASSERT_VAL(bucket_size_limit > 0, bucket_size_limit);
|
|
|
|
while ((top_mask & 0x80) == 0)
|
|
|
|
{
|
|
|
|
top_mask <<= 1;
|
|
|
|
++mask_shift;
|
|
|
|
}
|
|
|
|
top_mask = (0xff << mask_shift) & 0xff;
|
|
|
|
bucket_size_limit = (top_mask >> mask_shift) + 1;
|
|
|
|
TORRENT_ASSERT_VAL(bucket_size_limit <= 256, bucket_size_limit);
|
|
|
|
bool sub_buckets[256];
|
|
|
|
std::memset(sub_buckets, 0, sizeof(sub_buckets));
|
|
|
|
|
|
|
|
int id_shift;
|
|
|
|
// the last bucket is special, since it hasn't been split yet, it
|
|
|
|
// includes that top bit as well
|
|
|
|
if (bucket_index + 1 == int(table.buckets().size()))
|
|
|
|
id_shift = bucket_index;
|
|
|
|
else
|
|
|
|
id_shift = bucket_index + 1;
|
|
|
|
|
|
|
|
for (bucket_t::const_iterator j = i->live_nodes.begin()
|
|
|
|
, end2(i->live_nodes.end()); j != end2; ++j)
|
|
|
|
{
|
|
|
|
node_id id = j->id;
|
|
|
|
id <<= id_shift;
|
|
|
|
int b = (id[0] & top_mask) >> mask_shift;
|
|
|
|
TORRENT_ASSERT(b >= 0 && b < int(sizeof(sub_buckets)/sizeof(sub_buckets[0])));
|
|
|
|
sub_buckets[b] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "%2d mask: %2x: [", bucket_index, (top_mask >> mask_shift));
|
|
|
|
|
|
|
|
for (int j = 0; j < bucket_size_limit; ++j)
|
|
|
|
{
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, (sub_buckets[j] ? "X" : " "));
|
|
|
|
}
|
|
|
|
cursor += std::snprintf(&buf[cursor], buf.size() - cursor
|
|
|
|
, "]\n");
|
|
|
|
if (cursor > int(buf.size()) - 500) buf.resize(buf.size() * 3 / 2);
|
|
|
|
}
|
|
|
|
buf[cursor] = '\0';
|
|
|
|
os << &buf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2016-07-20 16:53:33 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(ping)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node response;
|
2011-01-22 19:44:48 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "ping", t.source, &response);
|
2009-09-27 19:41:51 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const pong_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"r", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
2009-09-27 19:41:51 +02:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node pong_keys[4];
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, pong_desc, pong_keys, t.error_string);
|
2009-09-27 19:41:51 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(pong_keys[0].string_value() == "r");
|
|
|
|
TEST_CHECK(pong_keys[1].string_value() == "10");
|
2009-09-27 19:41:51 +02:00
|
|
|
}
|
2010-11-27 04:09:28 +01:00
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid ping response: %s\n", t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2009-09-27 19:41:51 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(invalid_message)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
bdecode_node response;
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node err_keys[2];
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "find_node", t.source, &response);
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, err_desc, err_keys, t.error_string);
|
2009-09-27 19:41:51 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(err_keys[0].string_value() == "e");
|
|
|
|
if (err_keys[1].list_at(0).type() == bdecode_node::int_t
|
|
|
|
&& err_keys[1].list_at(1).type() == bdecode_node::string_t)
|
2009-09-27 19:41:51 +02:00
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(err_keys[1].list_at(1).string_value() == "missing 'target' key");
|
2009-09-27 19:41:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ERROR("invalid error response");
|
|
|
|
}
|
|
|
|
}
|
2010-11-27 04:09:28 +01:00
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid error response: %s\n", t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(node_id_testng)
|
|
|
|
{
|
|
|
|
node_id rnd = generate_secret_id();
|
|
|
|
TEST_CHECK(verify_secret_id(rnd));
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
rnd[19] ^= 0x55;
|
|
|
|
TEST_CHECK(!verify_secret_id(rnd));
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
rnd = generate_random_id();
|
|
|
|
make_id_secret(rnd);
|
|
|
|
TEST_CHECK(verify_secret_id(rnd));
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(get_peers_announce)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
bdecode_node response;
|
|
|
|
|
|
|
|
send_dht_request(t.dht_node, "get_peers", t.source, &response
|
|
|
|
, msg_args().info_hash("01010101010101010101"));
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node peer1_keys[4];
|
|
|
|
|
2010-11-27 04:09:28 +01:00
|
|
|
std::string token;
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, peer1_desc, peer1_keys, t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(peer1_keys[0].string_value() == "r");
|
2016-08-13 13:04:53 +02:00
|
|
|
token = peer1_keys[2].string_value().to_string();
|
2016-10-10 02:23:45 +02:00
|
|
|
// std::printf("got token: %s\n", token.c_str());
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid get_peers response: %s\n", t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "announce_peer", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.info_hash("01010101010101010101")
|
|
|
|
.name("test")
|
|
|
|
.token(token)
|
|
|
|
.port(8080));
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const ann_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"r", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
2010-11-27 04:09:28 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node ann_keys[3];
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = dht::verify_message(response, ann_desc, ann_keys, t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(ann_keys[0].string_value() == "r");
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid announce response:\n");
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_scrape(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2015-08-13 08:06:30 +02:00
|
|
|
init_rand_address();
|
|
|
|
|
2011-05-23 02:45:36 +02:00
|
|
|
// announce from 100 random IPs and make sure scrape works
|
|
|
|
// 50 downloaders and 50 seeds
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
t.source = udp::endpoint(rand_addr(), 6000);
|
|
|
|
send_dht_request(t.dht_node, "get_peers", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args().info_hash("01010101010101010101"));
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node peer1_keys[4];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, peer1_desc, peer1_keys, t.error_string);
|
2011-05-23 02:45:36 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::string token;
|
2011-05-23 02:45:36 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(peer1_keys[0].string_value() == "r");
|
2016-08-13 13:04:53 +02:00
|
|
|
token = peer1_keys[2].string_value().to_string();
|
2011-05-23 02:45:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid get_peers response: %s\n", t.error_string);
|
2011-05-23 02:45:36 +02:00
|
|
|
}
|
|
|
|
response.clear();
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "announce_peer", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.info_hash("01010101010101010101")
|
|
|
|
.name("test")
|
|
|
|
.token(token)
|
|
|
|
.port(8080)
|
|
|
|
.seed(i >= 50));
|
|
|
|
|
2011-05-23 02:45:36 +02:00
|
|
|
response.clear();
|
|
|
|
}
|
|
|
|
|
2010-11-27 04:09:28 +01:00
|
|
|
// ====== get_peers ======
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "get_peers", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args().info_hash("01010101010101010101").scrape(true));
|
2010-11-27 04:09:28 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const peer2_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"r", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"BFpe", bdecode_node::string_t, 256, 0},
|
|
|
|
{"BFsd", bdecode_node::string_t, 256, 0},
|
|
|
|
{"id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
2010-11-27 04:09:28 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node peer2_keys[5];
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, peer2_desc, peer2_keys, t.error_string);
|
2010-11-27 04:09:28 +01:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(peer2_keys[0].string_value() == "r");
|
|
|
|
TEST_EQUAL(peer2_keys[1].dict_find_string_value("n"), "test");
|
2011-05-23 02:45:36 +02:00
|
|
|
|
|
|
|
bloom_filter<256> downloaders;
|
|
|
|
bloom_filter<256> seeds;
|
2015-12-30 05:27:46 +01:00
|
|
|
downloaders.from_string(peer2_keys[2].string_ptr());
|
|
|
|
seeds.from_string(peer2_keys[3].string_ptr());
|
2011-05-23 02:45:36 +02:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("seeds: %f\n", seeds.size());
|
|
|
|
std::printf("downloaders: %f\n", downloaders.size());
|
2011-05-23 02:45:36 +02:00
|
|
|
|
2011-05-25 04:41:48 +02:00
|
|
|
TEST_CHECK(fabs(seeds.size() - 50.f) <= 3.f);
|
|
|
|
TEST_CHECK(fabs(downloaders.size() - 50.f) <= 3.f);
|
2010-11-27 04:09:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("invalid get_peers response:\n");
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2011-01-19 06:57:44 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2011-01-22 19:44:48 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(scrape_v4)
|
|
|
|
{
|
|
|
|
test_scrape(rand_v4);
|
|
|
|
}
|
2014-11-08 17:58:18 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(scrape_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_scrape(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-03 07:15:51 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
void test_id_enforcement(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
2013-10-14 03:03:43 +02:00
|
|
|
|
|
|
|
// enable node_id enforcement
|
2016-07-20 16:53:33 +02:00
|
|
|
t.sett.enforce_node_id = true;
|
2013-10-14 03:03:43 +02:00
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
node_id nid;
|
2016-07-20 16:53:33 +02:00
|
|
|
if (t.source.protocol() == udp::v4())
|
2015-11-14 06:08:57 +01:00
|
|
|
{
|
|
|
|
// this is one of the test vectors from:
|
|
|
|
// http://libtorrent.org/dht_sec.html
|
2016-07-20 16:53:33 +02:00
|
|
|
t.source = udp::endpoint(addr("124.31.75.21"), 1);
|
2015-11-14 06:08:57 +01:00
|
|
|
nid = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee401");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
t.source = udp::endpoint(addr("2001:b829:2123:be84:e16c:d6ae:5290:49f1"), 1);
|
2015-11-14 06:08:57 +01:00
|
|
|
nid = to_hash("0a8ad123be84e16cd6ae529049f1f1bbe9ebb304");
|
|
|
|
}
|
2013-10-14 03:03:43 +02:00
|
|
|
|
|
|
|
// verify that we reject invalid node IDs
|
|
|
|
// this is now an invalid node-id for 'source'
|
|
|
|
nid[0] = 0x18;
|
2016-07-20 16:53:33 +02:00
|
|
|
int nodes_num = std::get<0>(t.dht_node.size());
|
|
|
|
send_dht_request(t.dht_node, "find_node", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args()
|
|
|
|
.target(sha1_hash("0101010101010101010101010101010101010101"))
|
|
|
|
.nid(nid));
|
2013-10-14 03:03:43 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node err_keys[2];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, err_desc, err_keys, t.error_string);
|
2013-10-14 03:03:43 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(err_keys[0].string_value() == "e");
|
|
|
|
if (err_keys[1].list_at(0).type() == bdecode_node::int_t
|
|
|
|
&& err_keys[1].list_at(1).type() == bdecode_node::string_t)
|
2013-10-14 03:03:43 +02:00
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(err_keys[1].list_at(1).string_value() == "invalid node ID");
|
2013-10-14 03:03:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2013-10-14 03:03:43 +02:00
|
|
|
TEST_ERROR("invalid error response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid error response: %s\n", t.error_string);
|
2013-10-14 03:03:43 +02:00
|
|
|
}
|
2015-12-01 00:02:00 +01:00
|
|
|
|
2015-09-09 21:36:07 +02:00
|
|
|
// a node with invalid node-id shouldn't be added to routing table.
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(std::get<0>(t.dht_node.size()), nodes_num);
|
2015-09-09 21:36:07 +02:00
|
|
|
|
|
|
|
// now the node-id is valid.
|
2016-07-20 16:53:33 +02:00
|
|
|
if (t.source.protocol() == udp::v4())
|
2015-11-14 06:08:57 +01:00
|
|
|
nid[0] = 0x5f;
|
|
|
|
else
|
|
|
|
nid[0] = 0x0a;
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "find_node", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args()
|
|
|
|
.target(sha1_hash("0101010101010101010101010101010101010101"))
|
|
|
|
.nid(nid));
|
2015-09-09 21:36:07 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const nodes_desc[] = {
|
2015-09-09 21:36:07 +02:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"r", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node nodes_keys[3];
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = dht::verify_message(response, nodes_desc, nodes_keys, t.error_string);
|
2015-09-09 21:36:07 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_CHECK(nodes_keys[0].string_value() == "r");
|
2015-09-09 21:36:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid error response: %s\n", t.error_string);
|
2015-09-09 21:36:07 +02:00
|
|
|
}
|
|
|
|
// node with valid node-id should be added to routing table.
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(std::get<0>(t.dht_node.size()), nodes_num + 1);
|
|
|
|
}
|
2013-10-14 03:03:43 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(id_enforcement_v4)
|
|
|
|
{
|
|
|
|
test_id_enforcement(rand_v4);
|
|
|
|
}
|
2013-10-14 03:03:43 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(id_enforcement_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_id_enforcement(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-14 03:03:43 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(bloom_filter)
|
|
|
|
{
|
2011-05-23 02:45:36 +02:00
|
|
|
bloom_filter<256> test;
|
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
char adr[50];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(adr, 50, "192.0.2.%d", i);
|
2016-07-20 16:53:33 +02:00
|
|
|
address a = addr(adr);
|
2016-09-16 03:13:43 +02:00
|
|
|
sha1_hash const iphash = hash_address(a);
|
2011-05-23 02:45:36 +02:00
|
|
|
test.set(iphash);
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:27:57 +01:00
|
|
|
if (supports_ipv6())
|
2011-05-23 02:45:36 +02:00
|
|
|
{
|
2012-03-17 20:27:57 +01:00
|
|
|
for (int i = 0; i < 0x3E8; ++i)
|
|
|
|
{
|
|
|
|
char adr[50];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(adr, 50, "2001:db8::%x", i);
|
2016-07-20 16:53:33 +02:00
|
|
|
address a = addr(adr);
|
2016-09-16 03:13:43 +02:00
|
|
|
sha1_hash const iphash = hash_address(a);
|
2012-03-17 20:27:57 +01:00
|
|
|
test.set(iphash);
|
|
|
|
}
|
2011-05-23 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2011-05-25 04:26:07 +02:00
|
|
|
// these are test vectors from BEP 33
|
|
|
|
// http://www.bittorrent.org/beps/bep_0033.html
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("test.size: %f\n", test.size());
|
2016-07-29 08:36:15 +02:00
|
|
|
std::string const bf_str = test.to_string();
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", aux::to_hex(bf_str).c_str());
|
2012-03-17 20:27:57 +01:00
|
|
|
if (supports_ipv6())
|
|
|
|
{
|
2012-03-19 01:42:12 +01:00
|
|
|
TEST_CHECK(fabs(test.size() - 1224.93f) < 0.001);
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_CHECK(aux::to_hex(bf_str) == "f6c3f5eaa07ffd91bde89f777f26fb2bff37bdb8fb2bbaa2fd3ddde7bacfff75ee7ccbaefe5eedb1fbfaff67f6abff5e43ddbca3fd9b9ffdf4ffd3e9dff12d1bdf59db53dbe9fa5b7ff3b8fdfcde1afb8bedd7be2f3ee71ebbbfe93bcdeefe148246c2bc5dbff7e7efdcf24fd8dc7adffd8fffdfddfff7a4bbeedf5cb95ce81fc7fcff1ff4ffffdfe5f7fdcbb7fd79b3fa1fc77bfe07fff905b7b7ffc7fefeffe0b8370bb0cd3f5b7f2bd93feb4386cfdd6f7fd5bfaf2e9ebffffeecd67adbf7c67f17efd5d75eba6ffeba7fff47a91eb1bfbb53e8abfb5762abe8ff237279bfefbfeef5ffc5febfdfe5adffadfee1fb737ffffbfd9f6aeffeee76b6fd8f72ef");
|
2012-03-17 20:27:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-19 01:56:38 +01:00
|
|
|
TEST_CHECK(fabs(test.size() - 257.854f) < 0.001);
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_CHECK(aux::to_hex(bf_str) == "24c0004020043000102012743e00480037110820422110008000c0e302854835a05401a4045021302a306c060001881002d8a0a3a8001901b40a800900310008d2108110c2496a0028700010d804188b01415200082004088026411104a804048002002000080680828c400080cc40020c042c0494447280928041402104080d4240040414a41f0205654800b0811830d2020042b002c5800004a71d0204804a0028120a004c10017801490b834004044106005421000c86900a0020500203510060144e900100924a1018141a028012913f0041802250042280481200002004430804210101c08111c10801001080002038008211004266848606b035001048");
|
2012-03-17 20:27:57 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2011-05-23 02:45:36 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
namespace {
|
|
|
|
announce_item const items[] =
|
2011-01-19 06:57:44 +01:00
|
|
|
{
|
2011-05-23 02:45:36 +02:00
|
|
|
{ generate_next(), 1 },
|
|
|
|
{ generate_next(), 2 },
|
|
|
|
{ generate_next(), 3 },
|
|
|
|
{ generate_next(), 4 },
|
|
|
|
{ generate_next(), 5 },
|
|
|
|
{ generate_next(), 6 },
|
|
|
|
{ generate_next(), 7 },
|
|
|
|
{ generate_next(), 8 }
|
2011-01-19 06:57:44 +01:00
|
|
|
};
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::array<node_entry, 8> build_nodes()
|
|
|
|
{
|
|
|
|
std::array<node_entry, 8> nodes = {
|
|
|
|
{ node_entry(items[0].target, udp::endpoint(addr4("1.1.1.1"), 1231))
|
|
|
|
, node_entry(items[1].target, udp::endpoint(addr4("2.2.2.2"), 1232))
|
|
|
|
, node_entry(items[2].target, udp::endpoint(addr4("3.3.3.3"), 1233))
|
|
|
|
, node_entry(items[3].target, udp::endpoint(addr4("4.4.4.4"), 1234))
|
|
|
|
, node_entry(items[4].target, udp::endpoint(addr4("5.5.5.5"), 1235))
|
|
|
|
, node_entry(items[5].target, udp::endpoint(addr4("6.6.6.6"), 1236))
|
|
|
|
, node_entry(items[6].target, udp::endpoint(addr4("7.7.7.7"), 1237))
|
|
|
|
, node_entry(items[7].target, udp::endpoint(addr4("8.8.8.8"), 1238)) }
|
|
|
|
};
|
|
|
|
return nodes;
|
|
|
|
}
|
2011-01-19 06:57:44 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::array<node_entry, 9> build_nodes(sha1_hash target)
|
|
|
|
{
|
|
|
|
std::array<node_entry, 9> nodes = {
|
|
|
|
{ node_entry(target, udp::endpoint(addr4("1.1.1.1"), 1231))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("2.2.2.2"), 1232))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("3.3.3.3"), 1233))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("4.4.4.4"), 1234))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("5.5.5.5"), 1235))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("6.6.6.6"), 1236))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("7.7.7.7"), 1237))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("8.8.8.8"), 1238))
|
|
|
|
, node_entry(target, udp::endpoint(addr4("9.9.9.9"), 1239)) }
|
|
|
|
};
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> const empty_salt;
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: 3 split this up into smaller tests
|
|
|
|
void test_put(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node response;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
// ====== put ======
|
|
|
|
|
|
|
|
init_rand_address();
|
|
|
|
udp::endpoint eps[1000];
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
eps[i] = udp::endpoint(rand_addr(), (rand() % 16534) + 1);
|
|
|
|
|
|
|
|
announce_immutable_items(t.dht_node, eps, items, sizeof(items)/sizeof(items[0]));
|
|
|
|
|
|
|
|
key_desc_t const desc2[] =
|
2014-01-03 05:18:46 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0 }
|
2014-01-03 05:18:46 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node desc2_keys[1];
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
key_desc_t const desc_error[] =
|
2014-01-03 05:18:46 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "e", bdecode_node::list_t, 2, 0 },
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0},
|
2014-01-03 05:18:46 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node desc_error_keys[2];
|
|
|
|
|
2011-05-25 04:26:07 +02:00
|
|
|
// ==== get / put mutable items ===
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> itemv;
|
2014-04-01 02:09:23 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
signature sig;
|
2014-01-03 05:18:46 +01:00
|
|
|
char buffer[1200];
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number seq(4);
|
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
2016-07-20 16:53:33 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
// TODO: 4 pass in the actual salt as a parameter
|
2014-01-03 05:18:46 +01:00
|
|
|
for (int with_salt = 0; with_salt < 2; ++with_salt)
|
|
|
|
{
|
2016-07-24 00:57:04 +02:00
|
|
|
seq = sequence_number(4);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("\nTEST GET/PUT%s \ngenerating ed25519 keys\n\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, with_salt ? " with-salt" : " no-salt");
|
2016-08-30 08:37:51 +02:00
|
|
|
std::array<char, 32> seed = ed25519_create_seed();
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-08-30 08:37:51 +02:00
|
|
|
std::tie(pk, sk) = ed25519_create_keypair(seed);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("pub: %s priv: %s\n"
|
2016-07-29 08:36:15 +02:00
|
|
|
, aux::to_hex(pk.bytes).c_str()
|
|
|
|
, aux::to_hex(sk.bytes).c_str());
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
std::string salt;
|
|
|
|
if (with_salt) salt = "foobar";
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
hasher h(pk.bytes);
|
|
|
|
if (with_salt) h.update(salt);
|
2014-01-03 05:18:46 +01:00
|
|
|
sha1_hash target_id = h.final();
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("target_id: %s\n"
|
2016-07-29 08:36:15 +02:00
|
|
|
, aux::to_hex(target_id).c_str());
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "get", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(target_id));
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
key_desc_t const desc[] =
|
2014-01-03 05:18:46 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0},
|
|
|
|
{ "token", bdecode_node::string_t, 0, 0},
|
|
|
|
{ "ip", bdecode_node::string_t, 0, key_desc_t::optional | key_desc_t::last_child},
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0},
|
2014-01-03 05:18:46 +01:00
|
|
|
};
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node desc_keys[5];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc, desc_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
std::string token;
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc_keys[4].string_value(), "r");
|
2016-08-13 13:04:53 +02:00
|
|
|
token = desc_keys[2].string_value().to_string();
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("get response: %s\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, print_entry(response).c_str());
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("got token: %s\n", aux::to_hex(token).c_str());
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid get response: %s\n%s\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
itemv = span<char const>(buffer, bencode(buffer, items[0].ent));
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(itemv, salt, seq, pk, sk);
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_EQUAL(verify_mutable_item(itemv, salt, seq, pk, sig), true);
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "put", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.token(token)
|
|
|
|
.value(items[0].ent)
|
2016-07-24 00:57:04 +02:00
|
|
|
.key(pk)
|
|
|
|
.sig(sig)
|
2015-09-28 01:03:58 +02:00
|
|
|
.seq(seq)
|
2016-07-24 00:57:04 +02:00
|
|
|
.salt(salt));
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc2, desc2_keys, t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("put response: %s\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, print_entry(response).c_str());
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc2_keys[0].string_value(), "r");
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid put response: %s\n%s\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
2011-05-25 04:26:07 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "get", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(target_id));
|
2011-05-23 07:07:52 +02:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("target_id: %s\n"
|
2016-07-29 08:36:15 +02:00
|
|
|
, aux::to_hex(target_id).c_str());
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
key_desc_t const desc3[] =
|
2014-01-03 05:18:46 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
2016-07-21 03:29:50 +02:00
|
|
|
{ "id", bdecode_node::string_t, 20, 0},
|
|
|
|
{ "v", bdecode_node::none_t, 0, 0},
|
|
|
|
{ "seq", bdecode_node::int_t, 0, 0},
|
|
|
|
{ "sig", bdecode_node::string_t, 0, 0},
|
|
|
|
{ "ip", bdecode_node::string_t, 0, key_desc_t::optional | key_desc_t::last_child},
|
2015-03-12 06:20:12 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0},
|
2014-01-03 05:18:46 +01:00
|
|
|
};
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node desc3_keys[7];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc3, desc3_keys, t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("msg: %s\n", print_entry(response).c_str());
|
|
|
|
std::printf(" invalid get response: %s\n%s\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("get response: %s\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, print_entry(response).c_str());
|
|
|
|
char value[1020];
|
|
|
|
char* ptr = value;
|
2016-07-24 00:57:04 +02:00
|
|
|
int const value_len = bencode(ptr, items[0].ent);
|
|
|
|
TEST_EQUAL(value_len, int(desc3_keys[2].data_section().size()));
|
|
|
|
TEST_CHECK(memcmp(desc3_keys[2].data_section().data(), value, value_len) == 0);
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_EQUAL(int(seq.value), desc3_keys[3].int_value());
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2014-01-03 05:18:46 +01:00
|
|
|
// also test that invalid signatures fail!
|
2013-10-22 03:24:33 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
itemv = span<char const>(buffer, bencode(buffer, items[0].ent));
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(itemv, salt, seq, pk, sk);
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_EQUAL(verify_mutable_item(itemv, salt, seq, pk, sig), 1);
|
2015-08-22 15:24:49 +02:00
|
|
|
// break the signature
|
2016-07-24 00:57:04 +02:00
|
|
|
sig.bytes[2] ^= 0xaa;
|
2013-10-22 03:24:33 +02:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("PUT broken signature\n");
|
2013-10-22 03:24:33 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_CHECK(verify_mutable_item(itemv, salt, seq, pk, sig) != 1);
|
2013-10-22 03:24:33 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "put", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.token(token)
|
|
|
|
.value(items[0].ent)
|
2016-07-24 00:57:04 +02:00
|
|
|
.key(pk)
|
|
|
|
.sig(sig)
|
2015-09-28 01:03:58 +02:00
|
|
|
.seq(seq)
|
2016-07-24 00:57:04 +02:00
|
|
|
.salt(salt));
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc_error, desc_error_keys, t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("put response: %s\n", print_entry(response).c_str());
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc_error_keys[1].string_value(), "e");
|
2014-01-03 05:18:46 +01:00
|
|
|
// 206 is the code for invalid signature
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc_error_keys[0].list_int_value_at(0), 206);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid put response: %s\n%s\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
2014-12-25 12:24:02 +01:00
|
|
|
|
|
|
|
// === test conditional get ===
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "get", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(target_id).seq(prev_seq(seq)));
|
2014-12-25 12:24:02 +01:00
|
|
|
|
|
|
|
{
|
2016-07-21 03:29:50 +02:00
|
|
|
bdecode_node const r = response.dict_find_dict("r");
|
2015-03-12 06:20:12 +01:00
|
|
|
TEST_CHECK(r.dict_find("v"));
|
|
|
|
TEST_CHECK(r.dict_find("k"));
|
|
|
|
TEST_CHECK(r.dict_find("sig"));
|
2014-12-25 12:24:02 +01:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "get", t.source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args().target(target_id).seq(seq));
|
2014-12-25 12:24:02 +01:00
|
|
|
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node r = response.dict_find_dict("r");
|
|
|
|
TEST_CHECK(!r.dict_find("v"));
|
|
|
|
TEST_CHECK(!r.dict_find("k"));
|
|
|
|
TEST_CHECK(!r.dict_find("sig"));
|
2014-12-25 12:24:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 05:18:46 +01:00
|
|
|
// === test CAS put ===
|
|
|
|
|
2014-08-27 07:57:37 +02:00
|
|
|
// this is the sequence number we expect to be there
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number cas = seq;
|
2014-08-27 07:57:37 +02:00
|
|
|
|
2014-01-03 05:18:46 +01:00
|
|
|
// increment sequence number
|
2016-07-24 00:57:04 +02:00
|
|
|
seq = next_seq(seq);
|
2014-01-03 05:18:46 +01:00
|
|
|
// put item 1
|
2016-07-24 00:57:04 +02:00
|
|
|
itemv = span<char const>(buffer, bencode(buffer, items[1].ent));
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(itemv, salt, seq, pk, sk);
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_EQUAL(verify_mutable_item(itemv, salt, seq, pk, sig), 1);
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_CHECK(item_target_id(salt, pk) == target_id);
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("PUT CAS 1\n");
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "put", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.token(token)
|
|
|
|
.value(items[1].ent)
|
2016-07-24 00:57:04 +02:00
|
|
|
.key(pk)
|
|
|
|
.sig(sig)
|
2015-09-28 01:03:58 +02:00
|
|
|
.seq(seq)
|
|
|
|
.cas(cas)
|
2016-07-24 00:57:04 +02:00
|
|
|
.salt(salt));
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc2, desc2_keys, t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("put response: %s\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, print_entry(response).c_str());
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc2_keys[0].string_value(), "r");
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid put response: %s\n%s\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("PUT CAS 2\n");
|
2014-01-03 05:18:46 +01:00
|
|
|
|
|
|
|
// put the same message again. This should fail because the
|
|
|
|
// CAS hash is outdated, it's not the hash of the value that's
|
|
|
|
// stored anymore
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_request(t.dht_node, "put", t.source, &response
|
2015-09-28 01:03:58 +02:00
|
|
|
, msg_args()
|
|
|
|
.token(token)
|
|
|
|
.value(items[1].ent)
|
2016-07-24 00:57:04 +02:00
|
|
|
.key(pk)
|
|
|
|
.sig(sig)
|
2015-09-28 01:03:58 +02:00
|
|
|
.seq(seq)
|
|
|
|
.cas(cas)
|
2016-07-24 00:57:04 +02:00
|
|
|
.salt(salt));
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, desc_error, desc_error_keys, t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("put response: %s\n"
|
2014-01-03 05:18:46 +01:00
|
|
|
, print_entry(response).c_str());
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc_error_keys[1].string_value(), "e");
|
2014-01-03 05:18:46 +01:00
|
|
|
// 301 is the error code for CAS hash mismatch
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(desc_error_keys[0].list_int_value_at(0), 301);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid put response: %s\n%s\nExpected failure 301 (CAS hash mismatch)\n"
|
2016-07-20 16:53:33 +02:00
|
|
|
, t.error_string, print_entry(response).c_str());
|
|
|
|
TEST_ERROR(t.error_string);
|
2014-01-03 05:18:46 +01:00
|
|
|
}
|
2013-09-03 02:45:48 +02:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2013-09-03 02:45:48 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(put_v4)
|
|
|
|
{
|
|
|
|
test_put(rand_v4);
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(put_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_put(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
void test_routing_table(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
2015-11-14 06:08:57 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// test kademlia routing table
|
|
|
|
dht_settings s;
|
|
|
|
s.extended_routing_table = false;
|
|
|
|
// s.restrict_routing_ips = false;
|
|
|
|
node_id id = to_hash("3123456789abcdef01232456789abcdef0123456");
|
|
|
|
const int bucket_size = 10;
|
|
|
|
dht::routing_table table(id, t.source.protocol(), bucket_size, s, &t.observer);
|
|
|
|
std::vector<node_entry> nodes;
|
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 0);
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
node_id tmp = id;
|
|
|
|
node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061");
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
address node_addr;
|
|
|
|
address node_near_addr;
|
|
|
|
if (t.source.protocol() == udp::v4())
|
|
|
|
{
|
|
|
|
node_addr = addr4("4.4.4.4");
|
|
|
|
node_near_addr = addr4("4.4.4.5");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node_addr = addr6("2001:1111:1111:1111:1111:1111:1111:1111");
|
|
|
|
node_near_addr = addr6("2001:1111:1111:1111:eeee:eeee:eeee:eeee");
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// test a node with the same IP:port changing ID
|
|
|
|
add_and_replace(tmp, diff);
|
|
|
|
table.node_seen(tmp, udp::endpoint(node_addr, 4), 10);
|
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
TEST_EQUAL(table.bucket_size(0), 1);
|
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 1);
|
|
|
|
TEST_EQUAL(nodes.size(), 1);
|
|
|
|
if (!nodes.empty())
|
|
|
|
{
|
|
|
|
TEST_EQUAL(nodes[0].id, tmp);
|
|
|
|
TEST_EQUAL(nodes[0].addr(), node_addr);
|
|
|
|
TEST_EQUAL(nodes[0].port(), 4);
|
|
|
|
TEST_EQUAL(nodes[0].timeout_count, 0);
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// set timeout_count to 1
|
|
|
|
table.node_failed(tmp, udp::endpoint(node_addr, 4));
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
nodes.clear();
|
|
|
|
table.for_each_node(node_push_back, nop, &nodes);
|
|
|
|
TEST_EQUAL(nodes.size(), 1);
|
|
|
|
if (!nodes.empty())
|
|
|
|
{
|
|
|
|
TEST_EQUAL(nodes[0].id, tmp);
|
|
|
|
TEST_EQUAL(nodes[0].addr(), node_addr);
|
|
|
|
TEST_EQUAL(nodes[0].port(), 4);
|
|
|
|
TEST_EQUAL(nodes[0].timeout_count, 1);
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// add the exact same node again, it should set the timeout_count to 0
|
|
|
|
table.node_seen(tmp, udp::endpoint(node_addr, 4), 10);
|
|
|
|
nodes.clear();
|
|
|
|
table.for_each_node(node_push_back, nop, &nodes);
|
|
|
|
TEST_EQUAL(nodes.size(), 1);
|
|
|
|
if (!nodes.empty())
|
|
|
|
{
|
|
|
|
TEST_EQUAL(nodes[0].id, tmp);
|
|
|
|
TEST_EQUAL(nodes[0].addr(), node_addr);
|
|
|
|
TEST_EQUAL(nodes[0].port(), 4);
|
|
|
|
TEST_EQUAL(nodes[0].timeout_count, 0);
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// test adding the same node ID again with a different IP (should be ignored)
|
|
|
|
table.node_seen(tmp, udp::endpoint(node_addr, 5), 10);
|
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
TEST_EQUAL(table.bucket_size(0), 1);
|
|
|
|
if (!nodes.empty())
|
|
|
|
{
|
|
|
|
TEST_EQUAL(nodes[0].id, tmp);
|
|
|
|
TEST_EQUAL(nodes[0].addr(), node_addr);
|
|
|
|
TEST_EQUAL(nodes[0].port(), 4);
|
|
|
|
}
|
2015-08-13 08:06:30 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// test adding a node that ends up in the same bucket with an IP
|
|
|
|
// very close to the current one (should be ignored)
|
|
|
|
// if restrict_routing_ips == true
|
|
|
|
table.node_seen(tmp, udp::endpoint(node_near_addr, 5), 10);
|
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
TEST_EQUAL(table.bucket_size(0), 1);
|
|
|
|
if (!nodes.empty())
|
|
|
|
{
|
|
|
|
TEST_EQUAL(nodes[0].id, tmp);
|
|
|
|
TEST_EQUAL(nodes[0].addr(), node_addr);
|
|
|
|
TEST_EQUAL(nodes[0].port(), 4);
|
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-24 03:57:04 +02:00
|
|
|
// test adding the same IP:port again with a new node ID (should remove the node)
|
|
|
|
add_and_replace(tmp, diff);
|
|
|
|
table.node_seen(tmp, udp::endpoint(node_addr, 4), 10);
|
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
TEST_EQUAL(table.bucket_size(0), 0);
|
|
|
|
TEST_EQUAL(nodes.size(), 0);
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
s.restrict_routing_ips = false;
|
|
|
|
|
|
|
|
init_rand_address();
|
|
|
|
|
|
|
|
add_and_replace(tmp, diff);
|
|
|
|
table.node_seen(id, udp::endpoint(rand_addr(), rand()), 10);
|
|
|
|
|
|
|
|
nodes.clear();
|
|
|
|
for (int i = 0; i < 7000; ++i)
|
|
|
|
{
|
|
|
|
table.node_seen(tmp, udp::endpoint(rand_addr(), rand()), 20 + (tmp[19] & 0xff));
|
|
|
|
add_and_replace(tmp, diff);
|
|
|
|
}
|
|
|
|
std::printf("active buckets: %d\n", table.num_active_buckets());
|
|
|
|
TEST_EQUAL(table.num_active_buckets(), 10);
|
|
|
|
TEST_CHECK(std::get<0>(table.size()) >= 10 * 10);
|
|
|
|
//TODO: 2 test num_global_nodes
|
|
|
|
//TODO: 2 test need_refresh
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, table);
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
table.for_each_node(node_push_back, nop, &nodes);
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::printf("nodes: %d\n", int(nodes.size()));
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::vector<node_entry> temp;
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::generate(tmp.begin(), tmp.end(), random_byte);
|
|
|
|
table.find_node(tmp, temp, 0, int(nodes.size()) * 2);
|
|
|
|
std::printf("returned-all: %d\n", int(temp.size()));
|
|
|
|
TEST_EQUAL(temp.size(), nodes.size());
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// This makes sure enough of the nodes returned are actually
|
|
|
|
// part of the closest nodes
|
|
|
|
std::set<node_id> duplicates;
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
const int reps = 50;
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
for (int r = 0; r < reps; ++r)
|
|
|
|
{
|
|
|
|
std::generate(tmp.begin(), tmp.end(), random_byte);
|
|
|
|
table.find_node(tmp, temp, 0, bucket_size * 2);
|
|
|
|
std::printf("returned: %d\n", int(temp.size()));
|
|
|
|
TEST_EQUAL(int(temp.size()), (std::min)(bucket_size * 2, int(nodes.size())));
|
|
|
|
|
|
|
|
std::sort(nodes.begin(), nodes.end(), std::bind(&compare_ref
|
|
|
|
, std::bind(&node_entry::id, _1)
|
|
|
|
, std::bind(&node_entry::id, _2), tmp));
|
|
|
|
|
|
|
|
int expected = std::accumulate(nodes.begin(), nodes.begin() + (bucket_size * 2)
|
|
|
|
, 0, std::bind(&sum_distance_exp, _1, _2, tmp));
|
|
|
|
int sum_hits = std::accumulate(temp.begin(), temp.end()
|
|
|
|
, 0, std::bind(&sum_distance_exp, _1, _2, tmp));
|
|
|
|
TEST_EQUAL(bucket_size * 2, int(temp.size()));
|
|
|
|
std::printf("expected: %d actual: %d\n", expected, sum_hits);
|
|
|
|
TEST_EQUAL(expected, sum_hits);
|
|
|
|
|
|
|
|
duplicates.clear();
|
|
|
|
// This makes sure enough of the nodes returned are actually
|
|
|
|
// part of the closest nodes
|
|
|
|
for (std::vector<node_entry>::iterator i = temp.begin()
|
|
|
|
, end(temp.end()); i != end; ++i)
|
2013-09-01 03:10:50 +02:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_CHECK(duplicates.count(i->id) == 0);
|
|
|
|
duplicates.insert(i->id);
|
2013-09-01 03:10:50 +02:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
using namespace libtorrent::dht;
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
char const* ips[] = {
|
|
|
|
"124.31.75.21",
|
|
|
|
"21.75.31.124",
|
|
|
|
"65.23.51.170",
|
|
|
|
"84.124.73.14",
|
|
|
|
"43.213.53.83",
|
|
|
|
};
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
int rs[] = { 1,86,22,65,90 };
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::uint8_t prefixes[][3] =
|
|
|
|
{
|
|
|
|
{ 0x5f, 0xbf, 0xbf },
|
|
|
|
{ 0x5a, 0x3c, 0xe9 },
|
|
|
|
{ 0xa5, 0xd4, 0x32 },
|
|
|
|
{ 0x1b, 0x03, 0x21 },
|
|
|
|
{ 0xe5, 0x6f, 0x6c }
|
|
|
|
};
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
address const a = addr4(ips[i]);
|
|
|
|
node_id const id = generate_id_impl(a, rs[i]);
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_CHECK(id[0] == prefixes[i][0]);
|
|
|
|
TEST_CHECK(id[1] == prefixes[i][1]);
|
|
|
|
TEST_CHECK((id[2] & 0xf8) == (prefixes[i][2] & 0xf8));
|
|
|
|
|
|
|
|
TEST_CHECK(id[19] == rs[i]);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("IP address: %s r: %d node ID: %s\n", ips[i]
|
2016-07-29 08:36:15 +02:00
|
|
|
, rs[i], aux::to_hex(id).c_str());
|
2013-09-01 03:10:50 +02:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(routing_table_v4)
|
|
|
|
{
|
|
|
|
test_routing_table(rand_v4);
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(routing_table_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_routing_table(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void test_bootstrap(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
dht::key_desc_t const find_node_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 9, 0},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"target", bdecode_node::string_t, 20, key_desc_t::optional},
|
|
|
|
{"info_hash", bdecode_node::string_t, 20, key_desc_t::optional | key_desc_t::last_child},
|
2013-12-27 05:28:25 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node find_node_keys[7];
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// bootstrap
|
|
|
|
|
|
|
|
g_sent_packets.clear();
|
|
|
|
|
|
|
|
udp::endpoint initial_node(rand_addr(), 1234);
|
|
|
|
std::vector<udp::endpoint> nodesv;
|
|
|
|
nodesv.push_back(initial_node);
|
|
|
|
t.dht_node.bootstrap(nodesv, std::bind(&nop_node));
|
|
|
|
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, find_node_desc, find_node_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(find_node_keys[0].string_value(), "q");
|
|
|
|
TEST_CHECK(find_node_keys[2].string_value() == "find_node"
|
|
|
|
|| find_node_keys[2].string_value() == "get_peers");
|
|
|
|
|
|
|
|
if (find_node_keys[0].string_value() != "q"
|
|
|
|
|| (find_node_keys[2].string_value() != "find_node"
|
|
|
|
&& find_node_keys[2].string_value() != "get_peers")) return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid find_node request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
udp::endpoint found_node(rand_addr(), 2235);
|
2016-09-19 02:08:15 +02:00
|
|
|
std::vector<node_entry> nodes;
|
2016-10-08 20:07:11 +02:00
|
|
|
nodes.push_back(node_entry{found_node});
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
|
|
|
if (initial_node.address().is_v4())
|
|
|
|
send_dht_response(t.dht_node, response, initial_node, msg_args().nodes(nodes));
|
|
|
|
else
|
|
|
|
send_dht_response(t.dht_node, response, initial_node, msg_args().nodes6(nodes));
|
|
|
|
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, found_node);
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, find_node_desc, find_node_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(find_node_keys[0].string_value(), "q");
|
|
|
|
TEST_CHECK(find_node_keys[2].string_value() == "find_node"
|
|
|
|
|| find_node_keys[2].string_value() == "get_peers");
|
|
|
|
if (find_node_keys[0].string_value() != "q"
|
|
|
|
|| (find_node_keys[2].string_value() != "find_node"
|
2016-09-28 19:28:21 +02:00
|
|
|
&& find_node_keys[2].string_value() != "get_peers")) return;
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid find_node request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_sent_packets.clear();
|
|
|
|
send_dht_response(t.dht_node, response, found_node);
|
|
|
|
|
|
|
|
TEST_CHECK(g_sent_packets.empty());
|
|
|
|
TEST_EQUAL(t.dht_node.num_global_nodes(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(bootstrap_v4)
|
|
|
|
{
|
|
|
|
test_bootstrap(rand_v4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(bootstrap_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_bootstrap(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-01 06:49:21 +02:00
|
|
|
// test that the node ignores a nodes entry which is too short
|
|
|
|
void test_short_nodes(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
dht::key_desc_t const find_node_desc[] = {
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0 },
|
|
|
|
{ "t", bdecode_node::string_t, 2, 0 },
|
|
|
|
{ "q", bdecode_node::string_t, 9, 0 },
|
|
|
|
{ "a", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0 },
|
|
|
|
{ "target", bdecode_node::string_t, 20, key_desc_t::optional },
|
|
|
|
{ "info_hash", bdecode_node::string_t, 20, key_desc_t::optional | key_desc_t::last_child },
|
|
|
|
};
|
|
|
|
|
|
|
|
bdecode_node find_node_keys[7];
|
|
|
|
|
|
|
|
// bootstrap
|
|
|
|
|
|
|
|
g_sent_packets.clear();
|
|
|
|
|
|
|
|
udp::endpoint initial_node(rand_addr(), 1234);
|
|
|
|
std::vector<udp::endpoint> nodesv;
|
|
|
|
nodesv.push_back(initial_node);
|
|
|
|
t.dht_node.bootstrap(nodesv, std::bind(&nop_node));
|
|
|
|
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, find_node_desc, find_node_keys, t.error_string);
|
2016-09-01 06:49:21 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(find_node_keys[0].string_value(), "q");
|
|
|
|
TEST_CHECK(find_node_keys[2].string_value() == "find_node"
|
|
|
|
|| find_node_keys[2].string_value() == "get_peers");
|
|
|
|
|
|
|
|
if (find_node_keys[0].string_value() != "q"
|
|
|
|
|| (find_node_keys[2].string_value() != "find_node"
|
|
|
|
&& find_node_keys[2].string_value() != "get_peers")) return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid find_node request: %s\n", print_entry(response).c_str());
|
2016-09-01 06:49:21 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
udp::endpoint found_node(rand_addr(), 2235);
|
2016-09-19 02:08:15 +02:00
|
|
|
std::vector<node_entry> nodes;
|
2016-10-08 20:07:11 +02:00
|
|
|
nodes.push_back(node_entry{found_node});
|
2016-09-01 06:49:21 +02:00
|
|
|
g_sent_packets.clear();
|
|
|
|
msg_args args;
|
|
|
|
// chop one byte off of the nodes string
|
|
|
|
if (initial_node.address().is_v4())
|
|
|
|
{
|
|
|
|
args.nodes(nodes);
|
|
|
|
args.a["nodes"] = args.a["nodes"].string().substr(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args.nodes6(nodes);
|
|
|
|
args.a["nodes6"] = args.a["nodes6"].string().substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
send_dht_response(t.dht_node, response, initial_node, args);
|
|
|
|
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(short_nodes_v4)
|
|
|
|
{
|
|
|
|
test_short_nodes(rand_v4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(short_nodes_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_short_nodes(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
void test_get_peers(address(&rand_addr)())
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
|
|
|
bdecode_node response;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
dht::key_desc_t const get_peers_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 9, 0},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"info_hash", bdecode_node::string_t, 20, key_desc_t::last_child},
|
2013-12-27 05:28:25 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node get_peers_keys[6];
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// get_peers
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::node_id const target = to_hash("1234876923549721020394873245098347598635");
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint const initial_node(rand_addr(), 1234);
|
2016-10-08 20:07:11 +02:00
|
|
|
t.dht_node.m_table.add_node(node_entry{initial_node});
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
t.dht_node.announce(target, 1234, false, get_peers_cb);
|
2014-11-02 10:41:29 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, get_peers_desc, get_peers_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(get_peers_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(get_peers_keys[2].string_value(), "get_peers");
|
|
|
|
TEST_EQUAL(get_peers_keys[5].string_value(), target.to_string());
|
|
|
|
if (get_peers_keys[0].string_value() != "q"
|
|
|
|
|| get_peers_keys[2].string_value() != "get_peers")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get_peers request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::set<tcp::endpoint> peers[2];
|
|
|
|
peers[0].insert(tcp::endpoint(rand_addr(), 4111));
|
|
|
|
peers[0].insert(tcp::endpoint(rand_addr(), 4112));
|
|
|
|
peers[0].insert(tcp::endpoint(rand_addr(), 4113));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint next_node(rand_addr(), 2235);
|
2016-09-19 02:08:15 +02:00
|
|
|
std::vector<node_entry> nodes;
|
2016-10-08 20:07:11 +02:00
|
|
|
nodes.push_back(node_entry{next_node});
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
|
|
|
if (initial_node.address().is_v4())
|
|
|
|
{
|
|
|
|
send_dht_response(t.dht_node, response, initial_node
|
|
|
|
, msg_args().nodes(nodes).token("10").port(1234).peers(peers[0]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_dht_response(t.dht_node, response, initial_node
|
|
|
|
, msg_args().nodes6(nodes).token("10").port(1234).peers(peers[0]));
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, next_node);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, get_peers_desc, get_peers_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(get_peers_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(get_peers_keys[2].string_value(), "get_peers");
|
|
|
|
TEST_EQUAL(get_peers_keys[5].string_value(), target.to_string());
|
|
|
|
if (get_peers_keys[0].string_value() != "q"
|
|
|
|
|| get_peers_keys[2].string_value() != "get_peers")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get_peers request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
peers[1].insert(tcp::endpoint(rand_addr(), 4114));
|
|
|
|
peers[1].insert(tcp::endpoint(rand_addr(), 4115));
|
|
|
|
peers[1].insert(tcp::endpoint(rand_addr(), 4116));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_response(t.dht_node, response, next_node
|
|
|
|
, msg_args().token("11").port(1234).peers(peers[1]));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
for (std::list<std::pair<udp::endpoint, entry> >::iterator i = g_sent_packets.begin()
|
|
|
|
, end(g_sent_packets.end()); i != end; ++i)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
// std::printf(" %s:%d: %s\n", i->first.address().to_string(ec).c_str()
|
2016-07-20 16:53:33 +02:00
|
|
|
// , i->first.port(), i->second.to_string().c_str());
|
|
|
|
TEST_EQUAL(i->second["q"].string(), "announce_peer");
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
for (std::set<tcp::endpoint>::iterator peer = peers[i].begin(); peer != peers[i].end(); ++peer)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_CHECK(std::find(g_got_peers.begin(), g_got_peers.end(), *peer) != g_got_peers.end());
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
g_got_peers.clear();
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(get_peers_v4)
|
|
|
|
{
|
|
|
|
test_get_peers(rand_v4);
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(get_peers_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_get_peers(rand_v6);
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
// TODO: 4 pass in th actual salt as the argument
|
2016-07-21 03:29:50 +02:00
|
|
|
void test_mutable_get(address(&rand_addr)(), bool const with_salt)
|
2016-07-20 16:53:33 +02:00
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_addr(), 20));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
char buffer[1200];
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number seq(4);
|
|
|
|
span<char const> itemv;
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node response;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
std::string salt;
|
|
|
|
if (with_salt) salt = "foobar";
|
2016-07-21 03:29:50 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// mutable get
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-21 03:29:50 +02:00
|
|
|
udp::endpoint const initial_node(rand_addr(), 1234);
|
2016-10-08 20:07:11 +02:00
|
|
|
t.dht_node.m_table.add_node(node_entry{initial_node});
|
2014-03-01 10:04:23 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
g_put_item.assign(items[0].ent, salt, seq, pk, sk);
|
|
|
|
t.dht_node.put_item(pk, std::string()
|
2016-07-20 16:53:33 +02:00
|
|
|
, std::bind(&put_mutable_item_cb, _1, _2, 0)
|
|
|
|
, put_mutable_item_data_cb);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// mutable_get
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
t.dht_node.get_item(pk, salt, get_mutable_item_cb);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
|
|
|
bdecode_node get_item_keys[6];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool const ret = verify_message(response, get_item_desc, get_item_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(get_item_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(get_item_keys[2].string_value(), "get");
|
|
|
|
if (get_item_keys[0].string_value() != "q"
|
|
|
|
|| get_item_keys[2].string_value() != "get")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
signature sig;
|
|
|
|
itemv = span<char const>(buffer, bencode(buffer, items[0].ent));
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(itemv, salt, seq, pk, sk);
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_response(t.dht_node, response, initial_node
|
|
|
|
, msg_args()
|
|
|
|
.token("10")
|
|
|
|
.port(1234)
|
|
|
|
.value(items[0].ent)
|
2016-07-24 00:57:04 +02:00
|
|
|
.key(pk)
|
|
|
|
.sig(sig)
|
|
|
|
.salt(salt)
|
2016-07-20 16:53:33 +02:00
|
|
|
.seq(seq));
|
|
|
|
|
|
|
|
TEST_CHECK(g_sent_packets.empty());
|
|
|
|
TEST_EQUAL(g_got_items.size(), 1);
|
|
|
|
if (g_got_items.empty()) return;
|
|
|
|
|
|
|
|
TEST_EQUAL(g_got_items.front().value(), items[0].ent);
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_CHECK(g_got_items.front().pk() == pk);
|
|
|
|
TEST_CHECK(g_got_items.front().sig() == sig);
|
|
|
|
TEST_CHECK(g_got_items.front().seq() == seq);
|
2016-07-20 16:53:33 +02:00
|
|
|
g_got_items.clear();
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(mutable_get_v4)
|
|
|
|
{
|
2016-07-21 03:29:50 +02:00
|
|
|
test_mutable_get(rand_v4, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(mutable_get_salt_v4)
|
|
|
|
{
|
|
|
|
test_mutable_get(rand_v4, true);
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
TORRENT_TEST(mutable_get_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
2016-07-21 03:29:50 +02:00
|
|
|
test_mutable_get(rand_v6, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(mutable_get_salt_v6)
|
|
|
|
{
|
|
|
|
if (supports_ipv6())
|
|
|
|
test_mutable_get(rand_v6, true);
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(immutable_get)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
bdecode_node response;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// immutable get
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint initial_node(addr4("4.4.4.4"), 1234);
|
2016-10-08 20:07:11 +02:00
|
|
|
t.dht_node.m_table.add_node(node_entry{initial_node});
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
t.dht_node.get_item(items[0].target, get_immutable_item_cb);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
if (g_sent_packets.empty()) return;
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
lazy_from_entry(g_sent_packets.front().second, response);
|
|
|
|
bdecode_node get_item_keys[6];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool const ret = verify_message(response, get_item_desc, get_item_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(get_item_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(get_item_keys[2].string_value(), "get");
|
|
|
|
TEST_EQUAL(get_item_keys[5].string_value(), items[0].target.to_string());
|
|
|
|
if (get_item_keys[0].string_value() != "q" || get_item_keys[2].string_value() != "get") return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
|
|
|
send_dht_response(t.dht_node, response, initial_node
|
|
|
|
, msg_args().token("10").port(1234).value(items[0].ent));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_CHECK(g_sent_packets.empty());
|
|
|
|
TEST_EQUAL(g_got_items.size(), 1);
|
|
|
|
if (g_got_items.empty()) return;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_got_items.front().value(), items[0].ent);
|
|
|
|
g_got_items.clear();
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(immutable_put)
|
|
|
|
{
|
|
|
|
bdecode_node response;
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> itemv;
|
2016-07-20 16:53:33 +02:00
|
|
|
char buffer[1200];
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const put_immutable_item_desc[] = {
|
2015-03-12 06:20:12 +01:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 3, 0},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"token", bdecode_node::string_t, 2, 0},
|
|
|
|
{"v", bdecode_node::none_t, 0, key_desc_t::last_child},
|
2013-12-27 05:28:25 +01:00
|
|
|
};
|
|
|
|
|
2015-12-30 05:27:46 +01:00
|
|
|
bdecode_node put_immutable_item_keys[7];
|
|
|
|
|
2013-12-27 05:28:25 +01:00
|
|
|
// immutable put
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2015-11-20 03:41:56 +01:00
|
|
|
for (int loop = 0; loop < 9; loop++)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
// set the branching factor to k to make this a little easier
|
2016-07-20 16:53:33 +02:00
|
|
|
t.sett.search_branching = 8;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
std::array<node_entry, 8> const nodes = build_nodes();
|
|
|
|
|
|
|
|
for (node_entry const& n : nodes)
|
|
|
|
t.dht_node.m_table.add_node(n);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
entry put_data;
|
|
|
|
put_data = "Hello world";
|
|
|
|
std::string flat_data;
|
|
|
|
bencode(std::back_inserter(flat_data), put_data);
|
|
|
|
sha1_hash target = item_target_id(
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const>(flat_data.c_str(), int(flat_data.size())));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
t.dht_node.put_item(target, put_data, std::bind(&put_immutable_item_cb, _1, loop));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
|
|
|
if (g_sent_packets.size() != 8) break;
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; ++i)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator packet = find_packet(nodes[i].ep());
|
|
|
|
TEST_CHECK(packet != g_sent_packets.end());
|
|
|
|
if (packet == g_sent_packets.end()) continue;
|
|
|
|
|
|
|
|
lazy_from_entry(packet->second, response);
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node get_item_keys[6];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool const ret = verify_message(response, get_item_desc, get_item_keys, t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
char tok[10];
|
|
|
|
std::snprintf(tok, sizeof(tok), "%02d", i);
|
2015-11-20 03:41:56 +01:00
|
|
|
|
|
|
|
msg_args args;
|
2016-09-19 02:08:15 +02:00
|
|
|
args.token(tok).port(1234).nid(nodes[i].id).nodes({nodes[i]});
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_response(t.dht_node, response, nodes[i].ep(), args);
|
2013-12-27 05:28:25 +01:00
|
|
|
g_sent_packets.erase(packet);
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
|
|
|
if (g_sent_packets.size() != 8) break;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
itemv = span<char const>(buffer, bencode(buffer, put_data));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
for (int i = 0; i < 8; ++i)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator packet = find_packet(nodes[i].ep());
|
|
|
|
TEST_CHECK(packet != g_sent_packets.end());
|
|
|
|
if (packet == g_sent_packets.end()) continue;
|
|
|
|
|
|
|
|
lazy_from_entry(packet->second, response);
|
2016-07-20 16:53:33 +02:00
|
|
|
bool const ret = verify_message(response, put_immutable_item_desc, put_immutable_item_keys
|
2016-09-22 08:04:05 +02:00
|
|
|
, t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(put_immutable_item_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(put_immutable_item_keys[2].string_value(), "put");
|
2016-07-24 00:57:04 +02:00
|
|
|
span<const char> v = put_immutable_item_keys[6].data_section();
|
|
|
|
TEST_EQUAL(std::string(v.data(), v.size()), flat_data);
|
2016-07-20 16:53:33 +02:00
|
|
|
char tok[10];
|
|
|
|
std::snprintf(tok, sizeof(tok), "%02d", i);
|
|
|
|
TEST_EQUAL(put_immutable_item_keys[5].string_value(), tok);
|
2015-12-30 05:27:46 +01:00
|
|
|
if (put_immutable_item_keys[0].string_value() != "q"
|
|
|
|
|| put_immutable_item_keys[2].string_value() != "put") continue;
|
2015-11-20 03:41:56 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
if (i < loop) send_dht_response(t.dht_node, response, nodes[i].ep());
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid immutable put request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_sent_packets.clear();
|
|
|
|
g_put_item.clear();
|
|
|
|
g_put_count = 0;
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(mutable_put)
|
|
|
|
{
|
|
|
|
bdecode_node response;
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> itemv;
|
2016-07-20 16:53:33 +02:00
|
|
|
char buffer[1200];
|
|
|
|
bdecode_node put_mutable_item_keys[11];
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
2016-07-20 16:53:33 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number seq(4);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
// mutable put
|
2014-11-02 10:41:29 +01:00
|
|
|
g_sent_packets.clear();
|
2015-11-20 03:41:56 +01:00
|
|
|
for (int loop = 0; loop < 9; loop++)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
// set the branching factor to k to make this a little easier
|
2016-07-20 16:53:33 +02:00
|
|
|
t.sett.search_branching = 8;
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
enum { num_test_nodes = 8 };
|
2016-07-20 16:53:33 +02:00
|
|
|
std::array<node_entry, num_test_nodes> const nodes = build_nodes();
|
2013-12-27 05:28:25 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < num_test_nodes; ++i)
|
2016-07-20 16:53:33 +02:00
|
|
|
t.dht_node.m_table.add_node(nodes[i]);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
g_put_item.assign(items[0].ent, empty_salt, seq, pk, sk);
|
|
|
|
signature const sig = g_put_item.sig();
|
|
|
|
t.dht_node.put_item(pk, std::string()
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&put_mutable_item_cb, _1, _2, loop)
|
2015-11-20 03:41:56 +01:00
|
|
|
, put_mutable_item_data_cb);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
|
|
|
if (g_sent_packets.size() != 8) break;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
for (int i = 0; i < 8; ++i)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator packet = find_packet(nodes[i].ep());
|
|
|
|
TEST_CHECK(packet != g_sent_packets.end());
|
|
|
|
if (packet == g_sent_packets.end()) continue;
|
|
|
|
|
|
|
|
lazy_from_entry(packet->second, response);
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node get_item_keys[6];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool const ret = verify_message(response, get_item_desc, get_item_keys, t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
char tok[10];
|
|
|
|
std::snprintf(tok, sizeof(tok), "%02d", i);
|
2015-11-20 03:41:56 +01:00
|
|
|
|
|
|
|
msg_args args;
|
2016-09-19 02:08:15 +02:00
|
|
|
args.token(tok).port(1234).nid(nodes[i].id).nodes({nodes[i]});
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_response(t.dht_node, response, nodes[i].ep(), args);
|
2013-12-27 05:28:25 +01:00
|
|
|
g_sent_packets.erase(packet);
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
|
|
|
if (g_sent_packets.size() != 8) break;
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
itemv = span<char const>(buffer, bencode(buffer, items[0].ent));
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2015-11-20 03:41:56 +01:00
|
|
|
for (int i = 0; i < 8; ++i)
|
2013-12-27 05:28:25 +01:00
|
|
|
{
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator packet = find_packet(nodes[i].ep());
|
|
|
|
TEST_CHECK(packet != g_sent_packets.end());
|
|
|
|
if (packet == g_sent_packets.end()) continue;
|
|
|
|
|
|
|
|
lazy_from_entry(packet->second, response);
|
2016-07-20 16:53:33 +02:00
|
|
|
bool const ret = verify_message(response, put_mutable_item_desc, put_mutable_item_keys
|
2016-09-22 08:04:05 +02:00
|
|
|
, t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-12-30 05:27:46 +01:00
|
|
|
TEST_EQUAL(put_mutable_item_keys[0].string_value(), "q");
|
|
|
|
TEST_EQUAL(put_mutable_item_keys[2].string_value(), "put");
|
2016-07-24 00:57:04 +02:00
|
|
|
TEST_EQUAL(put_mutable_item_keys[6].string_value(), std::string(pk.bytes.data(), public_key::len));
|
|
|
|
TEST_EQUAL(put_mutable_item_keys[7].int_value(), int(seq.value));
|
|
|
|
TEST_EQUAL(put_mutable_item_keys[8].string_value(), std::string(sig.bytes.data(), signature::len));
|
|
|
|
span<const char> v = put_mutable_item_keys[10].data_section();
|
|
|
|
TEST_EQUAL(v.size(), itemv.size());
|
|
|
|
TEST_CHECK(memcmp(v.data(), itemv.data(), itemv.size()) == 0);
|
2016-07-20 16:53:33 +02:00
|
|
|
char tok[10];
|
|
|
|
std::snprintf(tok, sizeof(tok), "%02d", i);
|
|
|
|
TEST_EQUAL(put_mutable_item_keys[9].string_value(), tok);
|
2015-12-30 05:27:46 +01:00
|
|
|
if (put_mutable_item_keys[0].string_value() != "q"
|
|
|
|
|| put_mutable_item_keys[2].string_value() != "put") continue;
|
2015-11-20 03:41:56 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
if (i < loop) send_dht_response(t.dht_node, response, nodes[i].ep());
|
2013-12-27 05:28:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid put request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
2013-12-27 05:28:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_sent_packets.clear();
|
|
|
|
g_put_item.clear();
|
|
|
|
g_put_count = 0;
|
2015-11-20 03:41:56 +01:00
|
|
|
}
|
2016-07-20 16:53:33 +02:00
|
|
|
}
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TORRENT_TEST(traversal_done)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// set the branching factor to k to make this a little easier
|
|
|
|
t.sett.search_branching = 8;
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
sequence_number seq(4);
|
2016-07-20 16:53:33 +02:00
|
|
|
bdecode_node response;
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// verify that done() is only invoked once
|
|
|
|
// See PR 252
|
|
|
|
g_sent_packets.clear();
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
sha1_hash const target = hasher(pk.bytes).final();
|
2016-07-20 16:53:33 +02:00
|
|
|
enum { num_test_nodes = 9 }; // we need K + 1 nodes to create the failing sequence
|
|
|
|
|
|
|
|
std::array<node_entry, 9> nodes = build_nodes(target);
|
|
|
|
|
|
|
|
// invert the ith most significant byte so that the test nodes are
|
|
|
|
// progressivly closer to the target item
|
|
|
|
for (int i = 0; i < num_test_nodes; ++i)
|
|
|
|
nodes[i].id[i] = ~nodes[i].id[i];
|
|
|
|
|
|
|
|
// add the first k nodes to the subject's routing table
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
t.dht_node.m_table.add_node(nodes[i]);
|
|
|
|
|
|
|
|
// kick off a mutable put request
|
2016-07-24 00:57:04 +02:00
|
|
|
g_put_item.assign(items[0].ent, empty_salt, seq, pk, sk);
|
|
|
|
t.dht_node.put_item(pk, std::string()
|
2016-07-20 16:53:33 +02:00
|
|
|
, std::bind(&put_mutable_item_cb, _1, _2, 0)
|
|
|
|
, put_mutable_item_data_cb);
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
|
|
|
if (g_sent_packets.size() != 8) return;
|
|
|
|
|
|
|
|
// first send responses for the k closest nodes
|
|
|
|
for (int i = 1;; ++i)
|
|
|
|
{
|
|
|
|
// once the k closest nodes have responded, send the final response
|
|
|
|
// from the farthest node, this shouldn't trigger a second call to
|
|
|
|
// get_item_cb
|
|
|
|
if (i == num_test_nodes) i = 0;
|
|
|
|
|
|
|
|
std::list<std::pair<udp::endpoint, entry> >::iterator packet = find_packet(nodes[i].ep());
|
|
|
|
TEST_CHECK(packet != g_sent_packets.end());
|
|
|
|
if (packet == g_sent_packets.end()) continue;
|
|
|
|
|
|
|
|
lazy_from_entry(packet->second, response);
|
|
|
|
bdecode_node get_item_keys[6];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool const ret = verify_message(response, get_item_desc, get_item_keys, t.error_string);
|
2016-07-20 16:53:33 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" invalid get request: %s\n", print_entry(response).c_str());
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_ERROR(t.error_string);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char tok[10];
|
|
|
|
std::snprintf(tok, sizeof(tok), "%02d", i);
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
msg_args args;
|
|
|
|
args.token(tok).port(1234).nid(nodes[i].id);
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// add the address of the closest node to the first response
|
|
|
|
if (i == 1)
|
2016-09-19 02:08:15 +02:00
|
|
|
args.nodes({nodes[8]});
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
send_dht_response(t.dht_node, response, nodes[i].ep(), args);
|
|
|
|
g_sent_packets.erase(packet);
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// once we've sent the response from the farthest node, we're done
|
|
|
|
if (i == 0) break;
|
|
|
|
}
|
2015-11-10 05:05:05 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(g_put_count, 1);
|
|
|
|
// k nodes should now have outstanding put requests
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 8);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
g_sent_packets.clear();
|
|
|
|
g_put_item.clear();
|
|
|
|
g_put_count = 0;
|
2015-11-14 06:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(dht_dual_stack)
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: 3 use dht_test_setup class to simplify the node setup
|
2015-11-14 06:08:57 +01:00
|
|
|
dht_settings sett = test_settings();
|
|
|
|
mock_socket s;
|
|
|
|
obs observer;
|
|
|
|
counters cnt;
|
|
|
|
std::map<std::string, node*> nodes;
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
|
2016-07-09 22:26:26 +02:00
|
|
|
dht_storage->update_node_ids({node_id(nullptr)});
|
|
|
|
dht::node node4(udp::v4(), &s, sett, node_id(nullptr), &observer, cnt, nodes, *dht_storage);
|
|
|
|
dht::node node6(udp::v6(), &s, sett, node_id(nullptr), &observer, cnt, nodes, *dht_storage);
|
2015-11-14 06:08:57 +01:00
|
|
|
nodes.insert(std::make_pair("n4", &node4));
|
|
|
|
nodes.insert(std::make_pair("n6", &node6));
|
|
|
|
|
|
|
|
// DHT should be running on port 48199 now
|
|
|
|
bdecode_node response;
|
|
|
|
char error_string[200];
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
node_id id = to_hash("3123456789abcdef01232456789abcdef0123456");
|
2016-07-20 16:53:33 +02:00
|
|
|
node4.m_table.node_seen(id, udp::endpoint(addr("4.4.4.4"), 4440), 10);
|
|
|
|
node6.m_table.node_seen(id, udp::endpoint(addr("4::4"), 4441), 10);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
// v4 node requesting v6 nodes
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint source(addr("10.0.0.1"), 20);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
send_dht_request(node4, "find_node", source, &response
|
2016-07-24 00:57:04 +02:00
|
|
|
, msg_args()
|
|
|
|
.target(sha1_hash("0101010101010101010101010101010101010101"))
|
|
|
|
.want("n6"));
|
2015-11-14 06:08:57 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const nodes6_desc[] = {
|
2015-11-14 06:08:57 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0 },
|
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0 },
|
|
|
|
{ "nodes6", bdecode_node::string_t, 38, key_desc_t::last_child }
|
|
|
|
};
|
|
|
|
|
|
|
|
bdecode_node nodes6_keys[4];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, nodes6_desc, nodes6_keys, error_string);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
char const* nodes_ptr = nodes6_keys[3].string_ptr();
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(memcmp(nodes_ptr, id.data(), id.size()) == 0);
|
|
|
|
nodes_ptr += id.size();
|
2015-11-14 06:08:57 +01:00
|
|
|
udp::endpoint rep = detail::read_v6_endpoint<udp::endpoint>(nodes_ptr);
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(rep, udp::endpoint(addr("4::4"), 4441));
|
2015-11-14 06:08:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("find_node response: %s\n", print_entry(response).c_str());
|
2015-11-14 06:08:57 +01:00
|
|
|
TEST_ERROR(error_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// v6 node requesting v4 nodes
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
source.address(addr("10::1"));
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
send_dht_request(node6, "get_peers", source, &response
|
|
|
|
, msg_args().info_hash("0101010101010101010101010101010101010101").want("n4"));
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const nodes_desc[] = {
|
2015-11-14 06:08:57 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0 },
|
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0 },
|
|
|
|
{ "nodes", bdecode_node::string_t, 26, key_desc_t::last_child }
|
|
|
|
};
|
|
|
|
|
|
|
|
bdecode_node nodes_keys[4];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, nodes_desc, nodes_keys, error_string);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
char const* nodes_ptr = nodes_keys[3].string_ptr();
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(memcmp(nodes_ptr, id.data(), id.size()) == 0);
|
|
|
|
nodes_ptr += id.size();
|
2015-11-14 06:08:57 +01:00
|
|
|
udp::endpoint rep = detail::read_v4_endpoint<udp::endpoint>(nodes_ptr);
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(rep, udp::endpoint(addr("4.4.4.4"), 4440));
|
2015-11-14 06:08:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("find_node response: %s\n", print_entry(response).c_str());
|
2015-11-14 06:08:57 +01:00
|
|
|
TEST_ERROR(error_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// v6 node requesting both v4 and v6 nodes
|
|
|
|
|
|
|
|
send_dht_request(node6, "find_nodes", source, &response
|
|
|
|
, msg_args().info_hash("0101010101010101010101010101010101010101")
|
|
|
|
.want("n4")
|
|
|
|
.want("n6"));
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const nodes46_desc[] = {
|
2015-11-14 06:08:57 +01:00
|
|
|
{ "y", bdecode_node::string_t, 1, 0 },
|
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, 0 },
|
|
|
|
{ "nodes", bdecode_node::string_t, 26, 0 },
|
|
|
|
{ "nodes6", bdecode_node::string_t, 38, key_desc_t::last_child }
|
|
|
|
};
|
|
|
|
|
|
|
|
bdecode_node nodes46_keys[5];
|
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(response, nodes46_desc, nodes46_keys, error_string);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
char const* nodes_ptr = nodes46_keys[3].string_ptr();
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(memcmp(nodes_ptr, id.data(), id.size()) == 0);
|
|
|
|
nodes_ptr += id.size();
|
2015-11-14 06:08:57 +01:00
|
|
|
udp::endpoint rep = detail::read_v4_endpoint<udp::endpoint>(nodes_ptr);
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(rep, udp::endpoint(addr("4.4.4.4"), 4440));
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
nodes_ptr = nodes46_keys[4].string_ptr();
|
2016-07-22 16:29:39 +02:00
|
|
|
TEST_CHECK(memcmp(nodes_ptr, id.data(), id.size()) == 0);
|
|
|
|
nodes_ptr += id.size();
|
2015-11-14 06:08:57 +01:00
|
|
|
rep = detail::read_v6_endpoint<udp::endpoint>(nodes_ptr);
|
2016-07-20 16:53:33 +02:00
|
|
|
TEST_EQUAL(rep, udp::endpoint(addr("4::4"), 4441));
|
2015-11-14 06:08:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("find_node response: %s\n", print_entry(response).c_str());
|
2015-11-14 06:08:57 +01:00
|
|
|
TEST_ERROR(error_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
TORRENT_TEST(signing_test1)
|
|
|
|
{
|
2014-01-03 05:18:46 +01:00
|
|
|
// test vector 1
|
|
|
|
|
|
|
|
// test content
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> test_content("12:Hello World!", 15);
|
2014-01-03 05:18:46 +01:00
|
|
|
// test salt
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> test_salt("foobar", 6);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
|
|
|
span<char const> empty_salt;
|
2013-12-31 09:37:42 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
signature sig;
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(test_content, empty_salt, sequence_number(1), pk, sk);
|
2013-12-31 09:37:42 +01:00
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_EQUAL(aux::to_hex(sig.bytes)
|
2013-12-31 09:37:42 +01:00
|
|
|
, "305ac8aeb6c9c151fa120f120ea2cfb923564e11552d06a5d856091e5e853cff"
|
|
|
|
"1260d3f39e4999684aa92eb73ffd136e6f4f3ecbfda0ce53a1608ecd7ae21f01");
|
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
sha1_hash const target_id = item_target_id(empty_salt, pk);
|
|
|
|
TEST_EQUAL(aux::to_hex(target_id), "4a533d47ec9c7d95b1ad75f576cffc641853b750");
|
2015-08-23 09:23:51 +02:00
|
|
|
}
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
TORRENT_TEST(signing_test2)
|
|
|
|
{
|
2016-07-24 00:57:04 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
|
|
|
get_test_keypair(pk, sk);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
|
|
|
// test content
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> test_content("12:Hello World!", 15);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
signature sig;
|
2015-08-23 09:23:51 +02:00
|
|
|
// test salt
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> test_salt("foobar", 6);
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
// test vector 2 (the keypair is the same as test 1)
|
2016-08-30 08:37:51 +02:00
|
|
|
sig = sign_mutable_item(test_content, test_salt, sequence_number(1), pk, sk);
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_EQUAL(aux::to_hex(sig.bytes)
|
2014-01-03 05:18:46 +01:00
|
|
|
, "6834284b6b24c3204eb2fea824d82f88883a3d95e8b4a21b8c0ded553d17d17d"
|
|
|
|
"df9a8a7104b1258f30bed3787e6cb896fca78c58f8e03b5f18f14951a87d9a08");
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
sha1_hash target_id = item_target_id(test_salt, pk);
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_EQUAL(aux::to_hex(target_id), "411eba73b6f087ca51a3795d9c8c938d365e32c1");
|
2015-08-23 09:23:51 +02:00
|
|
|
}
|
2014-01-03 05:18:46 +01:00
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
TORRENT_TEST(signing_test3)
|
|
|
|
{
|
2014-01-03 05:18:46 +01:00
|
|
|
// test vector 3
|
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
// test content
|
2016-07-24 00:57:04 +02:00
|
|
|
span<char const> test_content("12:Hello World!", 15);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
|
|
|
sha1_hash target_id = item_target_id(test_content);
|
2016-07-29 08:36:15 +02:00
|
|
|
TEST_EQUAL(aux::to_hex(target_id), "e5f96f6f38320f0f33959cb4d3d656452117aadb");
|
2009-09-27 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-23 09:23:51 +02:00
|
|
|
// TODO: 2 split this up into smaller test cases
|
|
|
|
TORRENT_TEST(verify_message)
|
|
|
|
{
|
|
|
|
char error_string[200];
|
|
|
|
|
|
|
|
// test verify_message
|
|
|
|
static const key_desc_t msg_desc[] = {
|
|
|
|
{"A", bdecode_node::string_t, 4, 0},
|
|
|
|
{"B", bdecode_node::dict_t, 0, key_desc_t::optional | key_desc_t::parse_children},
|
|
|
|
{"B1", bdecode_node::string_t, 0, 0},
|
|
|
|
{"B2", bdecode_node::string_t, 0, key_desc_t::last_child},
|
|
|
|
{"C", bdecode_node::dict_t, 0, key_desc_t::optional | key_desc_t::parse_children},
|
|
|
|
{"C1", bdecode_node::string_t, 0, 0},
|
|
|
|
{"C2", bdecode_node::string_t, 0, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
|
|
|
bdecode_node msg_keys[7];
|
|
|
|
|
|
|
|
bdecode_node ent;
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
char const test_msg[] = "d1:A4:test1:Bd2:B15:test22:B25:test3ee";
|
|
|
|
bdecode(test_msg, test_msg + sizeof(test_msg)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = verify_message(ent, msg_desc, msg_keys, error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
TEST_CHECK(msg_keys[0]);
|
|
|
|
if (msg_keys[0]) TEST_EQUAL(msg_keys[0].string_value(), "test");
|
|
|
|
TEST_CHECK(msg_keys[1]);
|
|
|
|
TEST_CHECK(msg_keys[2]);
|
|
|
|
if (msg_keys[2]) TEST_EQUAL(msg_keys[2].string_value(), "test2");
|
|
|
|
TEST_CHECK(msg_keys[3]);
|
|
|
|
if (msg_keys[3]) TEST_EQUAL(msg_keys[3].string_value(), "test3");
|
|
|
|
TEST_CHECK(!msg_keys[4]);
|
|
|
|
TEST_CHECK(!msg_keys[5]);
|
|
|
|
TEST_CHECK(!msg_keys[6]);
|
|
|
|
|
|
|
|
char const test_msg2[] = "d1:A4:test1:Cd2:C15:test22:C25:test3ee";
|
|
|
|
bdecode(test_msg2, test_msg2 + sizeof(test_msg2)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(ent, msg_desc, msg_keys, error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
TEST_CHECK(msg_keys[0]);
|
|
|
|
if (msg_keys[0]) TEST_EQUAL(msg_keys[0].string_value(), "test");
|
|
|
|
TEST_CHECK(!msg_keys[1]);
|
|
|
|
TEST_CHECK(!msg_keys[2]);
|
|
|
|
TEST_CHECK(!msg_keys[3]);
|
|
|
|
TEST_CHECK(msg_keys[4]);
|
|
|
|
TEST_CHECK(msg_keys[5]);
|
|
|
|
if (msg_keys[5]) TEST_EQUAL(msg_keys[5].string_value(), "test2");
|
|
|
|
TEST_CHECK(msg_keys[6]);
|
|
|
|
if (msg_keys[6]) TEST_EQUAL(msg_keys[6].string_value(), "test3");
|
|
|
|
|
|
|
|
|
|
|
|
char const test_msg3[] = "d1:Cd2:C15:test22:C25:test3ee";
|
|
|
|
bdecode(test_msg3, test_msg3 + sizeof(test_msg3)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(ent, msg_desc, msg_keys, error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(!ret);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_EQUAL(error_string, std::string("missing 'A' key"));
|
|
|
|
|
|
|
|
char const test_msg4[] = "d1:A6:foobare";
|
|
|
|
bdecode(test_msg4, test_msg4 + sizeof(test_msg4)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(ent, msg_desc, msg_keys, error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(!ret);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_EQUAL(error_string, std::string("invalid value for 'A'"));
|
|
|
|
|
|
|
|
char const test_msg5[] = "d1:A4:test1:Cd2:C15:test2ee";
|
|
|
|
bdecode(test_msg5, test_msg5 + sizeof(test_msg5)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(ent, msg_desc, msg_keys, error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(!ret);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", error_string);
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_EQUAL(error_string, std::string("missing 'C2' key"));
|
|
|
|
|
|
|
|
// test empty strings [ { "":1 }, "" ]
|
|
|
|
char const test_msg6[] = "ld0:i1ee0:e";
|
|
|
|
bdecode(test_msg6, test_msg6 + sizeof(test_msg6)-1, ent, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s\n", print_entry(ent).c_str());
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_CHECK(ent.type() == bdecode_node::list_t);
|
|
|
|
if (ent.type() == bdecode_node::list_t)
|
|
|
|
{
|
|
|
|
TEST_CHECK(ent.list_size() == 2);
|
|
|
|
if (ent.list_size() == 2)
|
|
|
|
{
|
|
|
|
TEST_CHECK(ent.list_at(0).dict_find_int_value("") == 1);
|
|
|
|
TEST_CHECK(ent.list_at(1).string_value() == "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(routing_table_uniform)
|
|
|
|
{
|
|
|
|
// test routing table
|
|
|
|
dht_settings sett = test_settings();
|
|
|
|
obs observer;
|
|
|
|
|
|
|
|
sett.extended_routing_table = false;
|
|
|
|
node_id id = to_hash("1234876923549721020394873245098347598635");
|
|
|
|
node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061");
|
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
routing_table tbl(id, udp::v4(), 8, sett, &observer);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
|
|
|
// insert 256 nodes evenly distributed across the ID space.
|
|
|
|
// we expect to fill the top 5 buckets
|
|
|
|
for (int i = 255; i >= 0; --i)
|
|
|
|
{
|
|
|
|
// test a node with the same IP:port changing ID
|
|
|
|
add_and_replace(id, diff);
|
|
|
|
// in order to make this node-load a bit more realistic, start from
|
|
|
|
// distant nodes and work our way in closer to the node id
|
|
|
|
// the routing table will reject nodes that are too imbalanced (if all
|
|
|
|
// nodes are very close to our ID and none are far away, it's
|
|
|
|
// suspicious).
|
|
|
|
id[0] ^= i;
|
|
|
|
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
|
|
|
|
|
|
|
|
// restore the first byte of the node ID
|
|
|
|
id[0] ^= i;
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("num_active_buckets: %d\n", tbl.num_active_buckets());
|
2015-08-23 09:23:51 +02:00
|
|
|
// number of nodes per tree level (when adding 256 evenly distributed
|
|
|
|
// nodes):
|
|
|
|
// 0: 128
|
|
|
|
// 1: 64
|
|
|
|
// 2: 32
|
|
|
|
// 3: 16
|
|
|
|
// 4: 8
|
|
|
|
// i.e. no more than 5 levels
|
|
|
|
TEST_EQUAL(tbl.num_active_buckets(), 5);
|
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, tbl);
|
2015-08-23 09:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(routing_table_balance)
|
|
|
|
{
|
|
|
|
dht_settings sett = test_settings();
|
|
|
|
obs observer;
|
|
|
|
|
|
|
|
sett.extended_routing_table = false;
|
|
|
|
node_id id = to_hash("1234876923549721020394873245098347598635");
|
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
routing_table tbl(id, udp::v4(), 8, sett, &observer);
|
2015-08-23 09:23:51 +02:00
|
|
|
|
|
|
|
// insert nodes in the routing table that will force it to split
|
|
|
|
// and make sure we don't end up with a table completely out of balance
|
|
|
|
for (int i = 0; i < 32; ++i)
|
|
|
|
{
|
|
|
|
id[4] = i;
|
|
|
|
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("num_active_buckets: %d\n", tbl.num_active_buckets());
|
2015-08-23 09:23:51 +02:00
|
|
|
TEST_EQUAL(tbl.num_active_buckets(), 2);
|
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, tbl);
|
2015-08-23 09:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(routing_table_extended)
|
|
|
|
{
|
|
|
|
dht_settings sett = test_settings();
|
|
|
|
obs observer;
|
|
|
|
sett.extended_routing_table = true;
|
|
|
|
node_id id = to_hash("1234876923549721020394873245098347598635");
|
|
|
|
node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061");
|
|
|
|
|
2016-01-02 04:51:01 +01:00
|
|
|
// we can't add the nodes in straight 0,1,2,3 order. That way the routing
|
|
|
|
// table would get unbalanced and intermediate nodes would be dropped
|
2016-06-18 20:01:38 +02:00
|
|
|
std::vector<std::uint8_t> node_id_prefix;
|
2016-01-02 04:51:01 +01:00
|
|
|
node_id_prefix.reserve(256);
|
|
|
|
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
|
2016-08-15 01:48:31 +02:00
|
|
|
aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
|
2016-01-02 04:51:01 +01:00
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
routing_table tbl(id, udp::v4(), 8, sett, &observer);
|
2015-08-23 09:23:51 +02:00
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
add_and_replace(id, diff);
|
2016-01-02 04:51:01 +01:00
|
|
|
id[0] = node_id_prefix[i];
|
2015-08-23 09:23:51 +02:00
|
|
|
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
|
|
|
|
}
|
|
|
|
TEST_EQUAL(tbl.num_active_buckets(), 6);
|
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, tbl);
|
2015-08-23 09:23:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-12 05:47:27 +01:00
|
|
|
void inserter(std::set<node_id>* nodes, node_entry const& ne)
|
|
|
|
{
|
|
|
|
nodes->insert(nodes->begin(), ne.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(routing_table_set_id)
|
|
|
|
{
|
|
|
|
dht_settings sett = test_settings();
|
|
|
|
sett.enforce_node_id = false;
|
|
|
|
sett.extended_routing_table = false;
|
|
|
|
obs observer;
|
|
|
|
node_id id = to_hash("0000000000000000000000000000000000000000");
|
|
|
|
|
|
|
|
// we can't add the nodes in straight 0,1,2,3 order. That way the routing
|
|
|
|
// table would get unbalanced and intermediate nodes would be dropped
|
2016-06-18 20:01:38 +02:00
|
|
|
std::vector<std::uint8_t> node_id_prefix;
|
2016-01-12 05:47:27 +01:00
|
|
|
node_id_prefix.reserve(256);
|
|
|
|
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
|
2016-08-15 01:48:31 +02:00
|
|
|
aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
|
2016-02-12 04:56:52 +01:00
|
|
|
routing_table tbl(id, udp::v4(), 8, sett, &observer);
|
2016-01-12 05:47:27 +01:00
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
id[0] = node_id_prefix[i];
|
|
|
|
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
|
|
|
|
}
|
|
|
|
TEST_EQUAL(tbl.num_active_buckets(), 6);
|
|
|
|
|
|
|
|
std::set<node_id> original_nodes;
|
2016-05-25 06:31:52 +02:00
|
|
|
tbl.for_each_node(std::bind(&inserter, &original_nodes, _1));
|
2016-01-12 05:47:27 +01:00
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, tbl);
|
2016-01-12 05:47:27 +01:00
|
|
|
|
|
|
|
id = to_hash("ffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
|
|
|
|
tbl.update_node_id(id);
|
|
|
|
|
|
|
|
TEST_CHECK(tbl.num_active_buckets() <= 4);
|
|
|
|
std::set<node_id> remaining_nodes;
|
2016-05-25 06:31:52 +02:00
|
|
|
tbl.for_each_node(std::bind(&inserter, &remaining_nodes, _1));
|
2016-01-12 05:47:27 +01:00
|
|
|
|
|
|
|
std::set<node_id> intersection;
|
|
|
|
std::set_intersection(remaining_nodes.begin(), remaining_nodes.end()
|
|
|
|
, original_nodes.begin(), original_nodes.end()
|
|
|
|
, std::inserter(intersection, intersection.begin()));
|
|
|
|
|
|
|
|
// all remaining nodes also exist in the original nodes
|
|
|
|
TEST_EQUAL(intersection.size(), remaining_nodes.size());
|
|
|
|
|
2016-09-13 14:18:47 +02:00
|
|
|
print_state(std::cerr, tbl);
|
2016-01-12 05:47:27 +01:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:38:48 +02:00
|
|
|
TORRENT_TEST(node_set_id)
|
|
|
|
{
|
|
|
|
dht_test_setup t(udp::endpoint(rand_v4(), 20));
|
|
|
|
node_id old_nid = t.dht_node.nid();
|
|
|
|
t.observer.set_external_address(addr4("237.0.0.1"), rand_v4());
|
|
|
|
t.dht_node.update_node_id();
|
|
|
|
TEST_CHECK(old_nid != t.dht_node.nid());
|
|
|
|
// now that we've changed the node's id, make sure the id sent in outgoing messages
|
|
|
|
// reflects the change
|
|
|
|
|
|
|
|
bdecode_node response;
|
|
|
|
send_dht_request(t.dht_node, "ping", t.source, &response);
|
|
|
|
|
|
|
|
dht::key_desc_t const pong_desc[] = {
|
|
|
|
{ "y", bdecode_node::string_t, 1, 0 },
|
|
|
|
{ "t", bdecode_node::string_t, 2, 0 },
|
|
|
|
{ "r", bdecode_node::dict_t, 0, key_desc_t::parse_children },
|
|
|
|
{ "id", bdecode_node::string_t, 20, key_desc_t::last_child },
|
|
|
|
};
|
|
|
|
bdecode_node pong_keys[4];
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = dht::verify_message(response, pong_desc, pong_keys, t.error_string);
|
2016-07-27 08:38:48 +02:00
|
|
|
TEST_CHECK(ret);
|
|
|
|
if (!ret) return;
|
|
|
|
|
|
|
|
TEST_EQUAL(node_id(pong_keys[3].string_ptr()), t.dht_node.nid());
|
|
|
|
}
|
2016-01-12 05:47:27 +01:00
|
|
|
|
2015-09-04 00:49:59 +02:00
|
|
|
TORRENT_TEST(read_only_node)
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: 3 use dht_test_setup class to simplify the node setup
|
2015-09-04 00:49:59 +02:00
|
|
|
dht_settings sett = test_settings();
|
|
|
|
sett.read_only = true;
|
|
|
|
mock_socket s;
|
|
|
|
obs observer;
|
|
|
|
counters cnt;
|
2015-11-14 06:08:57 +01:00
|
|
|
std::map<std::string, node*> nodes;
|
2015-12-01 00:02:00 +01:00
|
|
|
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
|
2016-07-09 22:26:26 +02:00
|
|
|
dht_storage->update_node_ids({node_id(nullptr)});
|
|
|
|
dht::node node(udp::v4(), &s, sett, node_id(nullptr), &observer, cnt, nodes, *dht_storage);
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint source(addr("10.0.0.1"), 20);
|
2015-09-04 00:49:59 +02:00
|
|
|
bdecode_node response;
|
2015-11-23 04:02:37 +01:00
|
|
|
msg_args args;
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
// for incoming requests, read_only node won't response.
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "ping", source, &response, args, "10", false);
|
2015-09-04 00:49:59 +02:00
|
|
|
TEST_EQUAL(response.type(), bdecode_node::none_t);
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
args.target(sha1_hash("01010101010101010101"));
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "get", source, &response, args, "10", false);
|
2015-09-04 00:49:59 +02:00
|
|
|
TEST_EQUAL(response.type(), bdecode_node::none_t);
|
|
|
|
|
|
|
|
// also, the sender shouldn't be added to routing table.
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(node.size()), 0);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
// for outgoing requests, read_only node will add 'ro' key (value == 1)
|
|
|
|
// in top-level of request.
|
|
|
|
bdecode_node parsed[7];
|
|
|
|
char error_string[200];
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint initial_node(addr("4.4.4.4"), 1234);
|
2016-10-08 20:07:11 +02:00
|
|
|
node.m_table.add_node(node_entry{initial_node});
|
2015-09-04 00:49:59 +02:00
|
|
|
bdecode_node request;
|
|
|
|
sha1_hash target = generate_next();
|
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
node.get_item(target, get_immutable_item_cb);
|
2015-09-04 00:49:59 +02:00
|
|
|
TEST_EQUAL(g_sent_packets.size(), 1);
|
|
|
|
TEST_EQUAL(g_sent_packets.front().first, initial_node);
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
dht::key_desc_t const get_item_desc[] = {
|
2015-09-04 00:49:59 +02:00
|
|
|
{"y", bdecode_node::string_t, 1, 0},
|
|
|
|
{"t", bdecode_node::string_t, 2, 0},
|
|
|
|
{"q", bdecode_node::string_t, 3, 0},
|
|
|
|
{"ro", bdecode_node::int_t, 4, key_desc_t::optional},
|
|
|
|
{"a", bdecode_node::dict_t, 0, key_desc_t::parse_children},
|
|
|
|
{"id", bdecode_node::string_t, 20, 0},
|
|
|
|
{"target", bdecode_node::string_t, 20, key_desc_t::last_child},
|
|
|
|
};
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.front().second, request);
|
2016-09-22 08:04:05 +02:00
|
|
|
bool ret = verify_message(request, get_item_desc, parsed, error_string);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
TEST_CHECK(ret);
|
|
|
|
TEST_EQUAL(parsed[3].int_value(), 1);
|
|
|
|
|
2015-11-23 04:02:37 +01:00
|
|
|
// should have one node now, which is 4.4.4.4:1234
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(node.size()), 1);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
// now, disable read_only, try again.
|
|
|
|
g_sent_packets.clear();
|
|
|
|
sett.read_only = false;
|
|
|
|
|
2015-11-23 04:02:37 +01:00
|
|
|
send_dht_request(node, "get", source, &response);
|
2015-09-09 21:36:07 +02:00
|
|
|
// sender should be added to routing table, there are 2 nodes now.
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(node.size()), 2);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
g_sent_packets.clear();
|
|
|
|
target = generate_next();
|
2015-09-22 20:10:57 +02:00
|
|
|
node.get_item(target, get_immutable_item_cb);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
// since we have 2 nodes, we should have two packets.
|
|
|
|
TEST_EQUAL(g_sent_packets.size(), 2);
|
|
|
|
|
2015-09-09 21:36:07 +02:00
|
|
|
// both of them shouldn't have a 'ro' key.
|
2015-09-04 00:49:59 +02:00
|
|
|
lazy_from_entry(g_sent_packets.front().second, request);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(request, get_item_desc, parsed, error_string);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
TEST_CHECK(ret);
|
|
|
|
TEST_CHECK(!parsed[3]);
|
|
|
|
|
|
|
|
lazy_from_entry(g_sent_packets.back().second, request);
|
2016-09-22 08:04:05 +02:00
|
|
|
ret = verify_message(request, get_item_desc, parsed, error_string);
|
2015-09-04 00:49:59 +02:00
|
|
|
|
|
|
|
TEST_CHECK(ret);
|
|
|
|
TEST_CHECK(!parsed[3]);
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-12-01 00:02:00 +01:00
|
|
|
TORRENT_TEST(invalid_error_msg)
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: 3 use dht_test_setup class to simplify the node setup
|
2015-12-01 00:02:00 +01:00
|
|
|
dht_settings sett = test_settings();
|
|
|
|
mock_socket s;
|
|
|
|
obs observer;
|
|
|
|
counters cnt;
|
2015-11-14 06:08:57 +01:00
|
|
|
std::map<std::string, node*> nodes;
|
2015-12-01 00:02:00 +01:00
|
|
|
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
|
2016-07-09 22:26:26 +02:00
|
|
|
dht_storage->update_node_ids({node_id(nullptr)});
|
|
|
|
dht::node node(udp::v4(), &s, sett, node_id(nullptr), &observer, cnt, nodes, *dht_storage);
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint source(addr("10.0.0.1"), 20);
|
2015-12-01 00:02:00 +01:00
|
|
|
|
|
|
|
entry e;
|
|
|
|
e["y"] = "e";
|
|
|
|
e["e"].string() = "Malformed Error";
|
|
|
|
char msg_buf[1500];
|
|
|
|
int size = bencode(msg_buf, e);
|
|
|
|
|
|
|
|
bdecode_node decoded;
|
|
|
|
error_code ec;
|
|
|
|
bdecode(msg_buf, msg_buf + size, decoded, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("bdecode failed: %s\n", ec.message().c_str());
|
2015-12-01 00:02:00 +01:00
|
|
|
|
|
|
|
dht::msg m(decoded, source);
|
|
|
|
node.incoming(m);
|
2015-12-01 01:07:35 +01:00
|
|
|
|
|
|
|
bool found = false;
|
2015-12-01 04:59:34 +01:00
|
|
|
for (int i = 0; i < int(observer.m_log.size()); ++i)
|
2015-12-01 01:07:35 +01:00
|
|
|
{
|
2015-12-03 07:08:27 +01:00
|
|
|
if (observer.m_log[i].find("INCOMING ERROR") != std::string::npos
|
|
|
|
&& observer.m_log[i].find("(malformed)") != std::string::npos)
|
2015-12-01 01:07:35 +01:00
|
|
|
found = true;
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s\n", observer.m_log[i].c_str());
|
2015-12-01 01:07:35 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 07:08:27 +01:00
|
|
|
TEST_EQUAL(found, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(rpc_invalid_error_msg)
|
|
|
|
{
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: 3 use dht_test_setup class to simplify the node setup
|
2015-12-03 07:08:27 +01:00
|
|
|
dht_settings sett = test_settings();
|
|
|
|
mock_socket s;
|
|
|
|
obs observer;
|
|
|
|
counters cnt;
|
2015-11-14 06:08:57 +01:00
|
|
|
std::map<std::string, node*> nodes;
|
2015-12-03 07:08:27 +01:00
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
dht::routing_table table(node_id(), udp::v4(), 8, sett, &observer);
|
2015-12-03 07:08:27 +01:00
|
|
|
dht::rpc_manager rpc(node_id(), sett, table, &s, &observer);
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> dht_storage(dht_default_storage_constructor(sett));
|
2016-07-09 22:26:26 +02:00
|
|
|
dht_storage->update_node_ids({node_id(nullptr)});
|
|
|
|
dht::node node(udp::v4(), &s, sett, node_id(nullptr), &observer, cnt, nodes, *dht_storage);
|
2015-12-03 07:08:27 +01:00
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
udp::endpoint source(addr("10.0.0.1"), 20);
|
2015-12-03 07:08:27 +01:00
|
|
|
|
|
|
|
// we need this to create an entry for this transaction ID, otherwise the
|
|
|
|
// incoming message will just be dropped
|
|
|
|
entry req;
|
|
|
|
req["y"] = "q";
|
|
|
|
req["q"] = "bogus_query";
|
|
|
|
req["t"] = "\0\0\0\0";
|
|
|
|
|
|
|
|
g_sent_packets.clear();
|
2016-09-02 22:42:55 +02:00
|
|
|
auto algo = std::make_shared<dht::traversal_algorithm>(node, node_id());
|
2015-12-03 07:08:27 +01:00
|
|
|
|
2016-09-03 03:05:11 +02:00
|
|
|
auto o = rpc.allocate_observer<null_observer>(algo, source, node_id());
|
2016-06-18 14:31:07 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2015-12-03 07:08:27 +01:00
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
rpc.invoke(req, source, o);
|
|
|
|
|
|
|
|
// here's the incoming (malformed) error message
|
|
|
|
entry err;
|
|
|
|
err["y"] = "e";
|
|
|
|
err["e"].string() = "Malformed Error";
|
|
|
|
err["t"] = g_sent_packets.begin()->second["t"].string();
|
|
|
|
char msg_buf[1500];
|
|
|
|
int size = bencode(msg_buf, err);
|
|
|
|
|
|
|
|
bdecode_node decoded;
|
|
|
|
error_code ec;
|
|
|
|
bdecode(msg_buf, msg_buf + size, decoded, ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("bdecode failed: %s\n", ec.message().c_str());
|
2015-12-03 07:08:27 +01:00
|
|
|
|
|
|
|
dht::msg m(decoded, source);
|
|
|
|
node_id nid;
|
|
|
|
rpc.incoming(m, &nid);
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < int(observer.m_log.size()); ++i)
|
|
|
|
{
|
|
|
|
if (observer.m_log[i].find("reply with") != std::string::npos
|
|
|
|
&& observer.m_log[i].find("(malformed)") != std::string::npos
|
|
|
|
&& observer.m_log[i].find("error") != std::string::npos)
|
|
|
|
found = true;
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s\n", observer.m_log[i].c_str());
|
2015-12-03 07:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_EQUAL(found, true);
|
2015-12-01 00:02:00 +01:00
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2015-12-01 00:02:00 +01:00
|
|
|
|
2016-01-01 15:21:07 +01:00
|
|
|
// test bucket distribution
|
|
|
|
TORRENT_TEST(node_id_bucket_distribution)
|
|
|
|
{
|
|
|
|
int nodes_per_bucket[160] = {0};
|
|
|
|
dht::node_id reference_id = generate_id(rand_v4());
|
|
|
|
int const num_samples = 100000;
|
|
|
|
for (int i = 0; i < num_samples; ++i)
|
|
|
|
{
|
|
|
|
dht::node_id nid = generate_id(rand_v4());
|
|
|
|
int const bucket = 159 - distance_exp(reference_id, nid);
|
|
|
|
++nodes_per_bucket[bucket];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 25; ++i)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%3d ", nodes_per_bucket[i]);
|
2016-01-01 15:21:07 +01:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n");
|
2016-01-01 15:21:07 +01:00
|
|
|
|
|
|
|
int expected = num_samples / 2;
|
|
|
|
for (int i = 0; i < 25; ++i)
|
|
|
|
{
|
|
|
|
TEST_CHECK(std::abs(nodes_per_bucket[i] - expected) < num_samples / 20);
|
|
|
|
expected /= 2;
|
|
|
|
}
|
|
|
|
}
|
2016-01-09 22:20:27 +01:00
|
|
|
|
2016-06-04 01:44:16 +02:00
|
|
|
TORRENT_TEST(node_id_min_distance_exp)
|
|
|
|
{
|
2016-06-04 20:04:29 +02:00
|
|
|
node_id const n1 = to_hash("0000000000000000000000000000000000000002");
|
|
|
|
node_id const n2 = to_hash("0000000000000000000000000000000000000004");
|
|
|
|
node_id const n3 = to_hash("0000000000000000000000000000000000000008");
|
2016-06-04 01:44:16 +02:00
|
|
|
|
|
|
|
std::vector<node_id> ids;
|
|
|
|
|
|
|
|
ids.push_back(n1);
|
|
|
|
|
|
|
|
TEST_EQUAL(min_distance_exp(sha1_hash::min(), ids), 1);
|
|
|
|
|
|
|
|
ids.push_back(n1);
|
|
|
|
ids.push_back(n2);
|
|
|
|
|
|
|
|
TEST_EQUAL(min_distance_exp(sha1_hash::min(), ids), 1);
|
|
|
|
|
|
|
|
ids.push_back(n1);
|
|
|
|
ids.push_back(n2);
|
|
|
|
ids.push_back(n3);
|
|
|
|
|
|
|
|
TEST_EQUAL(min_distance_exp(sha1_hash::min(), ids), 1);
|
|
|
|
|
|
|
|
ids.clear();
|
|
|
|
ids.push_back(n3);
|
|
|
|
ids.push_back(n2);
|
|
|
|
ids.push_back(n2);
|
|
|
|
|
|
|
|
TEST_EQUAL(min_distance_exp(sha1_hash::min(), ids), 2);
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:20:27 +01:00
|
|
|
TORRENT_TEST(dht_verify_node_address)
|
|
|
|
{
|
|
|
|
obs observer;
|
|
|
|
// initial setup taken from dht test above
|
|
|
|
dht_settings s;
|
|
|
|
s.extended_routing_table = false;
|
|
|
|
node_id id = to_hash("3123456789abcdef01232456789abcdef0123456");
|
|
|
|
const int bucket_size = 10;
|
2016-02-12 04:56:52 +01:00
|
|
|
dht::routing_table table(id, udp::v4(), bucket_size, s, &observer);
|
2016-01-09 22:20:27 +01:00
|
|
|
std::vector<node_entry> nodes;
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 0);
|
2016-01-09 22:20:27 +01:00
|
|
|
|
|
|
|
node_id tmp = id;
|
|
|
|
node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061");
|
|
|
|
|
|
|
|
add_and_replace(tmp, diff);
|
2016-07-20 16:53:33 +02:00
|
|
|
table.node_seen(tmp, udp::endpoint(addr("4.4.4.4"), 4), 10);
|
2016-01-09 22:20:27 +01:00
|
|
|
table.find_node(id, nodes, 0, 10);
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 1);
|
2016-01-09 22:20:27 +01:00
|
|
|
TEST_EQUAL(nodes.size(), 1);
|
|
|
|
|
|
|
|
// incorrect data, wrong IP
|
|
|
|
table.node_seen(tmp
|
2016-07-20 16:53:33 +02:00
|
|
|
, udp::endpoint(addr("4.4.4.6"), 4), 10);
|
2016-01-09 22:20:27 +01:00
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 1);
|
2016-01-09 22:20:27 +01:00
|
|
|
TEST_EQUAL(nodes.size(), 1);
|
2016-07-24 03:57:04 +02:00
|
|
|
|
|
|
|
// incorrect data, wrong id, should cause node to be removed
|
|
|
|
table.node_seen(to_hash("0123456789abcdef01232456789abcdef0123456")
|
|
|
|
, udp::endpoint(addr("4.4.4.4"), 4), 10);
|
|
|
|
table.find_node(id, nodes, 0, 10);
|
|
|
|
|
|
|
|
TEST_EQUAL(std::get<0>(table.size()), 0);
|
|
|
|
TEST_EQUAL(nodes.size(), 0);
|
2016-01-09 22:20:27 +01:00
|
|
|
}
|
2016-05-03 14:29:27 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(generate_prefix_mask)
|
|
|
|
{
|
|
|
|
// test node-id functions
|
|
|
|
using namespace libtorrent::dht;
|
|
|
|
|
|
|
|
std::vector<std::pair<int, char const*>> const test = {
|
|
|
|
{ 0, "0000000000000000000000000000000000000000" },
|
|
|
|
{ 1, "8000000000000000000000000000000000000000" },
|
|
|
|
{ 2, "c000000000000000000000000000000000000000" },
|
|
|
|
{ 11, "ffe0000000000000000000000000000000000000" },
|
|
|
|
{ 17, "ffff800000000000000000000000000000000000" },
|
|
|
|
{ 37, "fffffffff8000000000000000000000000000000" },
|
|
|
|
{ 160, "ffffffffffffffffffffffffffffffffffffffff" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const& i : test)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(generate_prefix_mask(i.first), to_hash(i.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(distance_exp)
|
|
|
|
{
|
|
|
|
// distance_exp
|
|
|
|
|
|
|
|
|
|
|
|
using tst = std::tuple<char const*, char const*, int>;
|
|
|
|
|
|
|
|
std::vector<std::tuple<char const*, char const*, int>> const distance_tests = {
|
|
|
|
tst{ "ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
, "0000000000000000000000000000000000000000", 159 },
|
|
|
|
|
|
|
|
tst{ "ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
, "7fffffffffffffffffffffffffffffffffffffff", 159 },
|
|
|
|
|
|
|
|
tst{ "ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
, "ffffffffffffffffffffffffffffffffffffffff", 0 },
|
|
|
|
|
|
|
|
tst{ "ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
, "fffffffffffffffffffffffffffffffffffffffe", 0 },
|
|
|
|
|
|
|
|
tst{ "8000000000000000000000000000000000000000"
|
|
|
|
, "fffffffffffffffffffffffffffffffffffffffe", 158 },
|
|
|
|
|
|
|
|
tst{ "c000000000000000000000000000000000000000"
|
|
|
|
, "fffffffffffffffffffffffffffffffffffffffe", 157 },
|
|
|
|
|
|
|
|
tst{ "e000000000000000000000000000000000000000"
|
|
|
|
, "fffffffffffffffffffffffffffffffffffffffe", 156 },
|
|
|
|
|
|
|
|
tst{ "f000000000000000000000000000000000000000"
|
|
|
|
, "fffffffffffffffffffffffffffffffffffffffe", 155 },
|
|
|
|
|
|
|
|
tst{ "f8f2340985723049587230495872304958703294"
|
|
|
|
, "f743589043r890f023980f90e203980d090c3840", 155 },
|
|
|
|
|
|
|
|
tst{ "ffff740985723049587230495872304958703294"
|
|
|
|
, "ffff889043r890f023980f90e203980d090c3840", 159 - 16 },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const& t : distance_tests)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s %s: %d\n"
|
2016-05-03 14:29:27 +02:00
|
|
|
, std::get<0>(t), std::get<1>(t), std::get<2>(t));
|
|
|
|
|
|
|
|
TEST_EQUAL(distance_exp(
|
|
|
|
to_hash(std::get<0>(t))
|
|
|
|
, to_hash(std::get<1>(t))
|
|
|
|
), std::get<2>(t));
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 05:22:25 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(compare_ip_cidr)
|
|
|
|
{
|
|
|
|
using tst = std::tuple<char const*, char const*, bool>;
|
|
|
|
std::vector<tst> const v4tests = {
|
|
|
|
tst{"10.255.255.0", "10.255.255.255", true},
|
|
|
|
tst{"11.0.0.0", "10.255.255.255", false},
|
|
|
|
tst{"0.0.0.0", "128.255.255.255", false},
|
|
|
|
tst{"0.0.0.0", "127.255.255.255", false},
|
|
|
|
tst{"255.255.255.0", "255.255.255.255", true},
|
|
|
|
tst{"255.254.255.0", "255.255.255.255", false},
|
|
|
|
tst{"0.0.0.0", "0.0.0.0", true},
|
|
|
|
tst{"255.255.255.255", "255.255.255.255", true},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const& t : v4tests)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s %s\n", std::get<0>(t), std::get<1>(t));
|
2016-05-04 05:22:25 +02:00
|
|
|
TEST_EQUAL(compare_ip_cidr(
|
2016-07-20 16:53:33 +02:00
|
|
|
addr4(std::get<0>(t)), addr4(std::get<1>(t))), std::get<2>(t));
|
2016-05-04 05:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
std::vector<tst> const v6tests = {
|
|
|
|
tst{"::1", "::ffff:ffff:ffff:ffff", true},
|
|
|
|
tst{"::2:0000:0000:0000:0000", "::1:ffff:ffff:ffff:ffff", false},
|
|
|
|
tst{"::ff:0000:0000:0000:0000", "::ffff:ffff:ffff:ffff", false},
|
|
|
|
tst{"::caca:0000:0000:0000:0000", "::ffff:ffff:ffff:ffff:ffff", false},
|
|
|
|
tst{"::a:0000:0000:0000:0000", "::b:ffff:ffff:ffff:ffff", false},
|
|
|
|
tst{"::7f:0000:0000:0000:0000", "::ffff:ffff:ffff:ffff", false},
|
|
|
|
tst{"7f::", "ff::", false},
|
|
|
|
tst{"ff::", "ff::", true},
|
|
|
|
tst{"::", "::", true},
|
|
|
|
tst{"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const& t : v6tests)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(compare_ip_cidr(
|
2016-07-20 16:53:33 +02:00
|
|
|
addr6(std::get<0>(t)), addr6(std::get<1>(t))), std::get<2>(t));
|
2016-05-04 05:22:25 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-17 15:42:04 +02:00
|
|
|
TORRENT_TEST(dht_state)
|
|
|
|
{
|
|
|
|
dht_state s;
|
|
|
|
|
|
|
|
s.nid = to_hash("0000000000000000000000000000000000000001");
|
|
|
|
s.nodes.push_back(uep("1.1.1.1", 1));
|
|
|
|
s.nodes.push_back(uep("2.2.2.2", 2));
|
|
|
|
// not important that IPv6 is disabled here
|
|
|
|
s.nid6 = to_hash("0000000000000000000000000000000000000002");
|
|
|
|
s.nodes6.push_back(uep("3.3.3.3", 3));
|
|
|
|
s.nodes6.push_back(uep("4.4.4.4", 4));
|
|
|
|
|
|
|
|
entry const e = save_dht_state(s);
|
|
|
|
|
|
|
|
std::vector<char> tmp;
|
|
|
|
bencode(std::back_inserter(tmp), e);
|
|
|
|
|
|
|
|
bdecode_node n;
|
|
|
|
error_code ec;
|
|
|
|
int r = bdecode(&tmp[0], &tmp[0] + tmp.size(), n, ec);
|
|
|
|
TEST_CHECK(!r);
|
|
|
|
|
|
|
|
dht_state const s1 = read_dht_state(n);
|
|
|
|
TEST_EQUAL(s1.nid, s.nid);
|
|
|
|
TEST_CHECK(s1.nodes == s.nodes);
|
|
|
|
TEST_EQUAL(s1.nid6, s.nid6);
|
|
|
|
TEST_CHECK(s1.nodes6 == s.nodes6);
|
|
|
|
|
|
|
|
// empty
|
|
|
|
bdecode_node n1;
|
|
|
|
dht_state const s2 = read_dht_state(n1);
|
|
|
|
TEST_EQUAL(s2.nid, node_id());
|
|
|
|
TEST_CHECK(s2.nodes.empty());
|
|
|
|
TEST_EQUAL(s2.nid6, node_id());
|
|
|
|
TEST_CHECK(s2.nodes6.empty());
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:53:33 +02:00
|
|
|
// TODO: test obfuscated_get_peers
|
|
|
|
|
|
|
|
#else
|
|
|
|
TORRENT_TEST(dht)
|
|
|
|
{
|
|
|
|
// dummy dht test
|
|
|
|
TEST_CHECK(true);
|
|
|
|
}
|
|
|
|
|
2010-02-05 09:40:08 +01:00
|
|
|
#endif
|