2015-09-22 05:49:22 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, Alden Torres
|
|
|
|
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-06-07 00:22:33 +02:00
|
|
|
|
2015-09-22 05:49:22 +02:00
|
|
|
#include "test.hpp"
|
2018-07-24 08:18:48 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
2015-09-22 05:49:22 +02:00
|
|
|
#include "settings.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "setup_transfer.hpp" // for ep()
|
2015-09-22 05:49:22 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/kademlia/dht_storage.hpp"
|
2017-09-02 23:58:10 +02:00
|
|
|
#include "libtorrent/kademlia/dht_settings.hpp"
|
2015-09-22 05:49:22 +02:00
|
|
|
|
|
|
|
#include "libtorrent/io_service.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/aux_/time.hpp"
|
|
|
|
|
|
|
|
#include "simulator/simulator.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
|
2015-09-22 05:49:22 +02:00
|
|
|
#include <functional>
|
2016-06-04 16:01:43 +02:00
|
|
|
#include <sstream>
|
2015-09-22 05:49:22 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
|
|
|
using namespace lt::dht;
|
2015-09-22 05:49:22 +02:00
|
|
|
using namespace sim;
|
2017-09-17 23:50:33 +02:00
|
|
|
using sim::chrono::high_resolution_clock;
|
2015-09-22 05:49:22 +02:00
|
|
|
using namespace sim::asio;
|
|
|
|
using sim::simulation;
|
|
|
|
using sim::default_config;
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2015-09-22 05:49:22 +02:00
|
|
|
namespace
|
|
|
|
{
|
2017-09-02 23:58:10 +02:00
|
|
|
dht::dht_settings test_settings() {
|
|
|
|
dht::dht_settings sett;
|
2015-09-22 05:49:22 +02:00
|
|
|
sett.max_torrents = 2;
|
|
|
|
sett.max_dht_items = 2;
|
2016-06-07 00:22:33 +02:00
|
|
|
sett.item_lifetime = int(seconds(120 * 60).count());
|
2015-09-22 05:49:22 +02:00
|
|
|
return sett;
|
|
|
|
}
|
|
|
|
|
2016-06-07 00:22:33 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> create_default_dht_storage(
|
2017-09-02 23:58:10 +02:00
|
|
|
dht::dht_settings const& sett)
|
2016-06-07 00:22:33 +02:00
|
|
|
{
|
|
|
|
std::unique_ptr<dht_storage_interface> s(dht_default_storage_constructor(sett));
|
|
|
|
TEST_CHECK(s.get() != nullptr);
|
|
|
|
|
|
|
|
s->update_node_ids({to_hash("0000000000000000000000000000000000000200")});
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2015-09-22 05:49:22 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 00:22:33 +02:00
|
|
|
void timer_tick(dht_storage_interface* s
|
2015-09-22 05:49:22 +02:00
|
|
|
, dht_storage_counters const& c
|
2016-06-19 01:24:27 +02:00
|
|
|
, boost::system::error_code const&)
|
2015-09-22 05:49:22 +02:00
|
|
|
{
|
|
|
|
s->tick();
|
|
|
|
|
|
|
|
TEST_EQUAL(s->counters().peers, c.peers);
|
|
|
|
TEST_EQUAL(s->counters().torrents, c.torrents);
|
|
|
|
TEST_EQUAL(s->counters().immutable_data, c.immutable_data);
|
|
|
|
TEST_EQUAL(s->counters().mutable_data, c.mutable_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_expiration(high_resolution_clock::duration const& expiry_time
|
2016-06-07 00:22:33 +02:00
|
|
|
, std::unique_ptr<dht_storage_interface>& s
|
2015-09-22 05:49:22 +02:00
|
|
|
, dht_storage_counters const& c)
|
|
|
|
{
|
|
|
|
default_config cfg;
|
|
|
|
simulation sim(cfg);
|
2016-07-24 00:57:04 +02:00
|
|
|
sim::asio::io_service ios(sim, addr("10.0.0.1"));
|
2015-09-22 05:49:22 +02:00
|
|
|
|
|
|
|
sim::asio::high_resolution_timer timer(ios);
|
|
|
|
timer.expires_from_now(expiry_time);
|
2016-06-07 00:22:33 +02:00
|
|
|
timer.async_wait(std::bind(&timer_tick, s.get(), c, _1));
|
2015-09-22 05:49:22 +02:00
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
sim.run(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(dht_storage_counters)
|
|
|
|
{
|
2017-09-02 23:58:10 +02:00
|
|
|
dht::dht_settings sett = test_settings();
|
2016-06-07 00:22:33 +02:00
|
|
|
std::unique_ptr<dht_storage_interface> s(create_default_dht_storage(sett));
|
2015-09-22 05:49:22 +02:00
|
|
|
|
2016-06-07 00:22:33 +02:00
|
|
|
TEST_CHECK(s.get() != nullptr);
|
2015-09-22 05:49:22 +02:00
|
|
|
|
2016-06-07 00:22:33 +02:00
|
|
|
sha1_hash const n1 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee401");
|
|
|
|
sha1_hash const n2 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee402");
|
|
|
|
sha1_hash const n3 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee403");
|
|
|
|
sha1_hash const n4 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee404");
|
2015-09-22 05:49:22 +02:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
tcp::endpoint const p1 = ep("124.31.75.21", 1);
|
|
|
|
tcp::endpoint const p2 = ep("124.31.75.22", 1);
|
|
|
|
tcp::endpoint const p3 = ep("124.31.75.23", 1);
|
|
|
|
tcp::endpoint const p4 = ep("124.31.75.24", 1);
|
2015-09-22 05:49:22 +02:00
|
|
|
|
|
|
|
s->announce_peer(n1, p1, "torrent_name", false);
|
|
|
|
s->announce_peer(n2, p2, "torrent_name1", false);
|
|
|
|
s->announce_peer(n2, p3, "torrent_name1", false);
|
|
|
|
s->announce_peer(n3, p4, "torrent_name2", false);
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
s->put_immutable_item(n4, {"123", 3}, addr("124.31.75.21"));
|
|
|
|
s->put_immutable_item(n1, {"123", 3}, addr("124.31.75.21"));
|
|
|
|
s->put_immutable_item(n2, {"123", 3}, addr("124.31.75.21"));
|
|
|
|
s->put_immutable_item(n3, {"123", 3}, addr("124.31.75.21"));
|
2015-09-22 05:49:22 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
dht::public_key pk;
|
|
|
|
dht::signature sig;
|
|
|
|
s->put_mutable_item(n4, {"123", 3}, sig, sequence_number(1), pk, {"salt", 4}
|
|
|
|
, addr("124.31.75.21"));
|
2015-09-22 05:49:22 +02:00
|
|
|
|
|
|
|
dht_storage_counters c;
|
|
|
|
// note that we are using the aux global timer
|
|
|
|
|
|
|
|
c.peers = 3;
|
|
|
|
c.torrents = 2;
|
|
|
|
c.immutable_data = 2;
|
|
|
|
c.mutable_data = 1;
|
|
|
|
test_expiration(minutes(30), s, c); // test expiration of torrents and peers
|
|
|
|
|
|
|
|
c.peers = 0;
|
|
|
|
c.torrents = 0;
|
|
|
|
c.immutable_data = 2;
|
|
|
|
c.mutable_data = 1;
|
|
|
|
test_expiration(minutes(80), s, c); // test expiration of items before 2 hours
|
|
|
|
|
|
|
|
c.peers = 0;
|
|
|
|
c.torrents = 0;
|
|
|
|
c.immutable_data = 0;
|
|
|
|
c.mutable_data = 0;
|
|
|
|
test_expiration(hours(1), s, c); // test expiration of everything after 3 hours
|
|
|
|
}
|
2015-11-27 03:18:58 +01:00
|
|
|
|
2017-01-29 02:24:53 +01:00
|
|
|
TORRENT_TEST(dht_storage_infohashes_sample)
|
|
|
|
{
|
2017-09-02 23:58:10 +02:00
|
|
|
dht::dht_settings sett = test_settings();
|
2017-01-29 02:24:53 +01:00
|
|
|
sett.max_torrents = 5;
|
|
|
|
sett.sample_infohashes_interval = 30;
|
|
|
|
sett.max_infohashes_sample_count = 2;
|
|
|
|
std::unique_ptr<dht_storage_interface> s(create_default_dht_storage(sett));
|
|
|
|
|
|
|
|
TEST_CHECK(s.get() != nullptr);
|
|
|
|
|
|
|
|
sha1_hash const n1 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee401");
|
|
|
|
sha1_hash const n2 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee402");
|
|
|
|
sha1_hash const n3 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee403");
|
|
|
|
sha1_hash const n4 = to_hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee404");
|
|
|
|
|
|
|
|
tcp::endpoint const p1 = ep("124.31.75.21", 1);
|
|
|
|
tcp::endpoint const p2 = ep("124.31.75.22", 1);
|
|
|
|
tcp::endpoint const p3 = ep("124.31.75.23", 1);
|
|
|
|
tcp::endpoint const p4 = ep("124.31.75.24", 1);
|
|
|
|
|
|
|
|
s->announce_peer(n1, p1, "torrent_name1", false);
|
|
|
|
s->announce_peer(n2, p2, "torrent_name2", false);
|
|
|
|
s->announce_peer(n3, p3, "torrent_name3", false);
|
|
|
|
s->announce_peer(n4, p4, "torrent_name4", false);
|
|
|
|
|
|
|
|
entry item;
|
|
|
|
int r = s->get_infohashes_sample(item);
|
|
|
|
TEST_EQUAL(r, 2);
|
|
|
|
|
|
|
|
default_config cfg;
|
|
|
|
simulation sim(cfg);
|
|
|
|
sim::asio::io_service ios(sim, addr("10.0.0.1"));
|
|
|
|
|
|
|
|
sim::asio::high_resolution_timer timer(ios);
|
|
|
|
timer.expires_from_now(hours(1)); // expiration of torrents
|
2018-11-16 16:19:12 +01:00
|
|
|
timer.async_wait([&s](boost::system::error_code const&)
|
2017-01-29 02:24:53 +01:00
|
|
|
{
|
|
|
|
// tick here to trigger the torrents expiration
|
|
|
|
s->tick();
|
|
|
|
|
|
|
|
entry item;
|
|
|
|
int r = s->get_infohashes_sample(item);
|
|
|
|
TEST_EQUAL(r, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
sim.run(ec);
|
|
|
|
}
|
2018-07-24 08:18:48 +02:00
|
|
|
#else
|
|
|
|
TORRENT_TEST(disabled) {}
|
2016-06-07 00:22:33 +02:00
|
|
|
#endif // TORRENT_DISABLE_DHT
|