1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
/*
Copyright (c) 2018, Steven Siloti
Copyright (c) 2013-2021, Arvid Norberg
Copyright (c) 2017-2018, 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.
*/
#include "libtorrent/session.hpp"
#include "libtorrent/session_params.hpp"
#include <functional>
#include <thread>
#include "test.hpp"
#include "setup_transfer.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/session_stats.hpp"
#include "libtorrent/performance_counters.hpp"
#include "libtorrent/bdecode.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/torrent_info.hpp"
#include "settings.hpp"
#include <functional>
#include <fstream>
using namespace std::placeholders;
using namespace lt;
TORRENT_TEST(session)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
settings_pack sett = settings();
sett.set_int(settings_pack::num_optimistic_unchoke_slots, 10);
sett.set_int(settings_pack::unchoke_slots_limit, 10);
sett.set_int(settings_pack::resolver_cache_timeout, 1000);
ses.apply_settings(sett);
// verify that we get the appropriate performance warning
alert const* a;
for (;;)
{
a = wait_for_alert(ses, performance_alert::alert_type, "ses1");
if (a == nullptr) break;
TEST_EQUAL(a->type(), performance_alert::alert_type);
if (alert_cast<performance_alert>(a)->warning_code
== performance_alert::too_many_optimistic_unchoke_slots)
break;
}
TEST_CHECK(a);
sett.set_int(settings_pack::unchoke_slots_limit, 0);
ses.apply_settings(sett);
TEST_CHECK(ses.get_settings().get_int(settings_pack::unchoke_slots_limit) == 0);
sett.set_int(settings_pack::unchoke_slots_limit, -1);
ses.apply_settings(sett);
TEST_CHECK(ses.get_settings().get_int(settings_pack::unchoke_slots_limit) == -1);
sett.set_int(settings_pack::unchoke_slots_limit, 8);
ses.apply_settings(sett);
TEST_CHECK(ses.get_settings().get_int(settings_pack::unchoke_slots_limit) == 8);
TEST_EQUAL(ses.get_settings().get_int(settings_pack::resolver_cache_timeout), 1000);
sett.set_int(settings_pack::resolver_cache_timeout, 1001);
ses.apply_settings(sett);
TEST_EQUAL(ses.get_settings().get_int(settings_pack::resolver_cache_timeout), 1001);
// make sure the destructor waits properly
// for the asynchronous call to set the alert
// mask completes, before it goes on to destruct
// the session object
}
#if TORRENT_ABI_VERSION < 3
TORRENT_TEST(async_add_torrent_deprecated_magnet)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
atp.info_hash = sha1_hash("abababababababababab");
atp.save_path = ".";
ses.async_add_torrent(atp);
auto* a = alert_cast<add_torrent_alert>(wait_for_alert(ses, add_torrent_alert::alert_type, "ses"));
TEST_CHECK(a);
if (a == nullptr) return;
TEST_EQUAL(a->params.info_hashes.v1, sha1_hash("abababababababababab"));
}
#endif
TORRENT_TEST(async_add_torrent_no_save_path)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
atp.info_hashes.v1.assign("abababababababababab");
atp.save_path = "";
TEST_THROW(ses.add_torrent(atp));
TEST_THROW(ses.async_add_torrent(atp));
lt::error_code ec;
ses.add_torrent(atp, ec);
TORRENT_ASSERT(ec == error_code(lt::errors::invalid_save_path));
}
TORRENT_TEST(async_add_torrent_duplicate_error)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
atp.info_hashes.v1.assign("abababababababababab");
atp.save_path = ".";
ses.async_add_torrent(atp);
auto* a = alert_cast<add_torrent_alert>(wait_for_alert(ses, add_torrent_alert::alert_type, "ses"));
TEST_CHECK(a);
if (a == nullptr) return;
atp.flags |= torrent_flags::duplicate_is_error;
ses.async_add_torrent(atp);
a = alert_cast<add_torrent_alert>(wait_for_alert(ses, add_torrent_alert::alert_type, "ses"));
TEST_CHECK(a);
if (a == nullptr) return;
TEST_CHECK(!a->handle.is_valid());
TEST_CHECK(a->error);
}
TORRENT_TEST(async_add_torrent_duplicate)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
atp.info_hashes.v1.assign("abababababababababab");
atp.save_path = ".";
ses.async_add_torrent(atp);
auto* a = alert_cast<add_torrent_alert>(wait_for_alert(ses, add_torrent_alert::alert_type, "ses"));
TEST_CHECK(a);
if (a == nullptr) return;
torrent_handle h = a->handle;
TEST_CHECK(!a->error);
atp.flags &= ~torrent_flags::duplicate_is_error;
ses.async_add_torrent(atp);
a = alert_cast<add_torrent_alert>(wait_for_alert(ses, add_torrent_alert::alert_type, "ses"));
TEST_CHECK(a);
if (a == nullptr) return;
TEST_CHECK(a->handle == h);
TEST_CHECK(!a->error);
}
TORRENT_TEST(async_add_torrent_duplicate_back_to_back)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
atp.info_hashes.v1.assign("abababababababababab");
atp.save_path = ".";
atp.flags |= torrent_flags::paused;
atp.flags &= ~torrent_flags::apply_ip_filter;
atp.flags &= ~torrent_flags::auto_managed;
ses.async_add_torrent(atp);
atp.flags &= ~torrent_flags::duplicate_is_error;
ses.async_add_torrent(atp);
auto* a = alert_cast<add_torrent_alert>(wait_for_alert(ses
, add_torrent_alert::alert_type, "ses", pop_alerts::cache_alerts));
TEST_CHECK(a);
if (a == nullptr) return;
torrent_handle h = a->handle;
TEST_CHECK(!a->error);
a = alert_cast<add_torrent_alert>(wait_for_alert(ses
, add_torrent_alert::alert_type, "ses", pop_alerts::cache_alerts));
TEST_CHECK(a);
if (a == nullptr) return;
TEST_CHECK(a->handle == h);
TEST_CHECK(!a->error);
torrent_status st = h.status();
TEST_CHECK(st.flags & torrent_flags::paused);
TEST_CHECK(!(st.flags & torrent_flags::apply_ip_filter));
TEST_CHECK(!(st.flags & torrent_flags::auto_managed));
}
TORRENT_TEST(load_empty_file)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);
add_torrent_params atp;
error_code ignore_errors;
atp.ti = std::make_shared<torrent_info>("", std::ref(ignore_errors), from_span);
atp.save_path = ".";
error_code ec;
torrent_handle h = ses.add_torrent(std::move(atp), ec);
TEST_CHECK(!h.is_valid());
TEST_CHECK(ec == error_code(errors::no_metadata));
}
TORRENT_TEST(session_stats)
{
std::vector<stats_metric> stats = session_stats_metrics();
std::sort(stats.begin(), stats.end()
, [](stats_metric const& lhs, stats_metric const& rhs)
{ return lhs.value_index < rhs.value_index; });
TEST_EQUAL(stats.size(), lt::counters::num_counters);
// make sure every stat index is represented in the stats_metric vector
for (int i = 0; i < int(stats.size()); ++i)
{
TEST_EQUAL(stats[std::size_t(i)].value_index, i);
}
TEST_EQUAL(lt::find_metric_idx("peer.incoming_connections")
, lt::counters::incoming_connections);
TEST_EQUAL(lt::find_metric_idx("utp.utp_packet_resend")
, lt::counters::utp_packet_resend);
TEST_EQUAL(lt::find_metric_idx("utp.utp_fast_retransmit")
, lt::counters::utp_fast_retransmit);
}
TORRENT_TEST(paused_session)
{
lt::session s(settings());
s.pause();
lt::add_torrent_params ps;
std::ofstream file("temporary");
ps.ti = ::create_torrent(&file, "temporary", 16 * 1024, 13, false);
ps.flags = lt::torrent_flags::paused;
ps.save_path = ".";
torrent_handle h = s.add_torrent(std::move(ps));
std::this_thread::sleep_for(lt::milliseconds(2000));
h.resume();
std::this_thread::sleep_for(lt::milliseconds(1000));
TEST_CHECK(!(h.flags() & torrent_flags::paused));
}
template <typename Set, typename Save, typename Test>
void test_save_restore(Set setup, Save s, Test t)
{
entry st;
{
settings_pack p = settings();
setup(p);
lt::session ses(p);
s(ses, st);
}
{
// the loading function takes a bdecode_node, so we have to transform the
// entry
std::printf("%s\n", st.to_string().c_str());
std::vector<char> buf;
bencode(std::back_inserter(buf), st);
lt::session ses(read_session_params(buf));
t(ses);
}
}
TORRENT_TEST(save_restore_state)
{
test_save_restore(
[](settings_pack& p) {
// set the cache size
p.set_int(settings_pack::request_queue_time, 1337);
},
[](lt::session& ses, entry& st) {
st = write_session_params(ses.session_state());
},
[](lt::session& ses) {
// make sure we loaded the cache size correctly
settings_pack sett = ses.get_settings();
TEST_EQUAL(sett.get_int(settings_pack::request_queue_time), 1337);
});
}
TORRENT_TEST(save_restore_state_save_filter)
{
test_save_restore(
[](settings_pack& p) {
// set the cache size
p.set_int(settings_pack::request_queue_time, 1337);
},
[](lt::session& ses, entry& st) {
// save everything _but_ the settings
st = write_session_params(ses.session_state(~session::save_settings));
},
[](lt::session& ses) {
// make sure whatever we loaded did not include the cache size
settings_pack sett = ses.get_settings();
TEST_EQUAL(sett.get_int(settings_pack::request_queue_time), 3);
});
}
TORRENT_TEST(session_shutdown)
{
lt::settings_pack pack;
lt::session ses(pack);
}
TORRENT_TEST(save_state_fingerprint)
{
lt::session_proxy p1;
lt::session_proxy p2;
lt::settings_pack pack;
pack.set_str(settings_pack::peer_fingerprint, "AAA");
lt::session ses(pack);
TEST_EQUAL(ses.get_settings().get_str(settings_pack::peer_fingerprint), "AAA");
auto const st = write_session_params_buf(ses.session_state());
pack.set_str(settings_pack::peer_fingerprint, "foobar");
ses.apply_settings(pack);
TEST_EQUAL(ses.get_settings().get_str(settings_pack::peer_fingerprint), "foobar");
lt::session ses2(read_session_params(st));
TEST_EQUAL(ses2.get_settings().get_str(settings_pack::peer_fingerprint), "AAA");
p1 = ses.abort();
p2 = ses2.abort();
}
TORRENT_TEST(pop_alert_clear)
{
session s;
// make sure the vector is cleared if there are no alerts to be popped
std::vector<alert*> alerts(100);
for (int i = 0; i < 10; ++i)
{
alerts.resize(100);
s.pop_alerts(&alerts);
if (alerts.empty()) break;
}
TEST_CHECK(alerts.empty());
}
namespace {
lt::session_proxy test_move_session(lt::session ses)
{
std::this_thread::sleep_for(lt::milliseconds(100));
auto t = ses.get_torrents();
return ses.abort();
}
}
TORRENT_TEST(move_session)
{
lt::settings_pack pack;
lt::session ses(pack);
lt::session_proxy p = test_move_session(std::move(ses));
}
#if !defined TORRENT_DISABLE_LOGGING
#if !defined TORRENT_DISABLE_ALERT_MSG
#if !defined TORRENT_DISABLE_DHT
auto const count_dht_inits = [](session& ses)
{
int count = 0;
int num = 200; // this number is adjusted per version, an estimate
time_point const end_time = clock_type::now() + seconds(15);
while (true)
{
time_point const now = clock_type::now();
if (now > end_time) return count;
ses.wait_for_alert(end_time - now);
std::vector<alert*> alerts;
ses.pop_alerts(&alerts);
for (auto a : alerts)
{
std::printf("%d: [%s] %s\n", num, a->what(), a->message().c_str());
if (a->type() == log_alert::alert_type)
{
std::string const msg = a->message();
if (msg.find("starting DHT, running: ") != std::string::npos)
count++;
}
num--;
}
if (num <= 0) return count;
}
};
TORRENT_TEST(init_dht_default_bootstrap)
{
settings_pack p = settings();
p.set_bool(settings_pack::enable_dht, true);
p.set_int(settings_pack::alert_mask, alert_category::all);
// default value
p.set_str(settings_pack::dht_bootstrap_nodes, "dht.libtorrent.org:25401");
lt::session s(p);
int const count = count_dht_inits(s);
TEST_EQUAL(count, 1);
}
TORRENT_TEST(init_dht_invalid_bootstrap)
{
settings_pack p = settings();
p.set_bool(settings_pack::enable_dht, true);
p.set_int(settings_pack::alert_mask, alert_category::all);
// no default value
p.set_str(settings_pack::dht_bootstrap_nodes, "test.libtorrent.org:25401:8888");
lt::session s(p);
int const count = count_dht_inits(s);
TEST_EQUAL(count, 1);
}
TORRENT_TEST(init_dht_empty_bootstrap)
{
settings_pack p = settings();
p.set_bool(settings_pack::enable_dht, true);
p.set_int(settings_pack::alert_mask, alert_category::all);
// empty value
p.set_str(settings_pack::dht_bootstrap_nodes, "");
lt::session s(p);
int const count = count_dht_inits(s);
TEST_EQUAL(count, 1);
}
TORRENT_TEST(dht_upload_rate_overflow_pack)
{
settings_pack p = settings();
// make sure this doesn't cause an overflow
p.set_int(settings_pack::dht_upload_rate_limit, std::numeric_limits<int>::max());
p.set_int(settings_pack::alert_mask, alert_category_t(std::uint32_t(p.get_int(settings_pack::alert_mask)))
| alert_category::dht_log);
p.set_bool(settings_pack::enable_dht, true);
lt::session s(p);
p = s.get_settings();
TEST_EQUAL(p.get_int(settings_pack::dht_upload_rate_limit), std::numeric_limits<int>::max() / 3);
int const count = count_dht_inits(s);
TEST_EQUAL(count, 1);
}
#if TORRENT_ABI_VERSION <= 2
TORRENT_TEST(dht_upload_rate_overflow)
{
settings_pack p = settings();
p.set_bool(settings_pack::enable_dht, true);
p.set_int(settings_pack::alert_mask, alert_category_t(std::uint32_t(p.get_int(settings_pack::alert_mask)))
| alert_category::dht_log);
lt::session s(p);
// make sure this doesn't cause an overflow
dht::dht_settings sett;
sett.upload_rate_limit = std::numeric_limits<int>::max();
s.set_dht_settings(sett);
p = s.get_settings();
TEST_EQUAL(p.get_int(settings_pack::dht_upload_rate_limit), std::numeric_limits<int>::max() / 3);
int const count = count_dht_inits(s);
TEST_EQUAL(count, 1);
}
#endif
#endif // TORRENT_DISABLE_DHT
TORRENT_TEST(reopen_network_sockets)
{
auto count_alerts = [](session& ses, int const listen, int const portmap)
{
int count_listen = 0;
int count_portmap = 0;
int num = 60; // this number is adjusted per version, an estimate
time_point const end_time = clock_type::now() + seconds(3);
while (true)
{
time_point const now = clock_type::now();
if (now > end_time)
break;
ses.wait_for_alert(end_time - now);
std::vector<alert*> alerts;
ses.pop_alerts(&alerts);
for (auto a : alerts)
{
std::printf("%d: [%s] %s\n", num, a->what(), a->message().c_str());
std::string const msg = a->message();
if (msg.find("successfully listening on") != std::string::npos)
count_listen++;
// upnp
if (msg.find("adding port map:") != std::string::npos)
count_portmap++;
// natpmp
if (msg.find("add-mapping: proto:") != std::string::npos)
count_portmap++;
num--;
}
if (num <= 0)
break;
}
std::printf("count_listen: %d (expect: %d), count_portmap: %d (expect: %d)\n"
, count_listen, listen, count_portmap, portmap);
return count_listen == listen && count_portmap == portmap;
};
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, alert_category::all);
p.set_str(settings_pack::listen_interfaces, "127.0.0.1:6881l");
p.set_bool(settings_pack::enable_upnp, true);
p.set_bool(settings_pack::enable_natpmp, true);
lt::session s(p);
// NAT-PMP nad UPnP will be disabled when we only listen on loopback
TEST_CHECK(count_alerts(s, 2, 0));
// this is a bit of a pointless test now, since neither UPnP nor NAT-PMP are
// enabled for loopback
s.reopen_network_sockets(session_handle::reopen_map_ports);
TEST_CHECK(count_alerts(s, 0, 0));
s.reopen_network_sockets({});
TEST_CHECK(count_alerts(s, 0, 0));
p.set_bool(settings_pack::enable_upnp, false);
p.set_bool(settings_pack::enable_natpmp, false);
s.apply_settings(p);
s.reopen_network_sockets(session_handle::reopen_map_ports);
TEST_CHECK(count_alerts(s, 0, 0));
}
#endif // TORRENT_DISABLE_ALERT_MSG
#endif
|