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
|
/*
Copyright (c) 2015, 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.
*/
#include "transfer_sim.hpp"
using namespace sim;
using namespace lt;
TORRENT_TEST(socks4_tcp)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses0, settings_pack::socks4);
filter_ips(ses1);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(socks5_tcp_connect)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses0, settings_pack::socks5);
filter_ips(ses1);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(encryption_tcp)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{ enable_enc(ses0); enable_enc(ses1); },
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(no_proxy_tcp_ipv6)
{
run_test(
no_init,
[](lt::session&, lt::alert const*) {},
expect_seed(true),
tx::ipv6
);
}
TORRENT_TEST(no_proxy_utp_ipv6)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{ utp_only(ses0); utp_only(ses1); },
[](lt::session&, lt::alert const*) {},
expect_seed(true),
tx::ipv6
);
}
// TODO: the socks server does not support IPv6 addresses yet
/*
TORRENT_TEST(socks5_tcp_ipv6)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses0, settings_pack::socks5);
filter_ips(ses1);
},
[](lt::session&, lt::alert const*) {},
[](std::shared_ptr<lt::session> ses[2]) {
TEST_EQUAL(is_seed(*ses[0]), true);
},
tx::ipv6
);
}
*/
TORRENT_TEST(no_proxy_tcp)
{
run_test(
no_init,
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(no_proxy_utp)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{ utp_only(ses0); utp_only(ses1); },
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(encryption_utp)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
enable_enc(ses0);
enable_enc(ses1);
utp_only(ses0);
utp_only(ses1);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(socks5_utp)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses0, settings_pack::socks5);
utp_only(ses0);
filter_ips(ses1);
utp_only(ses1);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(socks5_utp_incoming)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses1, settings_pack::socks5);
utp_only(ses0);
utp_only(ses1);
filter_ips(ses0);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true),
tx::connect_proxy
);
}
TORRENT_TEST(socks5_utp_circumvent_proxy_reject)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses1, settings_pack::socks5);
utp_only(ses0);
utp_only(ses1);
},
[](lt::session&, lt::alert const*) {},
expect_seed(false)
);
}
// if we're not proxying peer connections, it's OK to accept incoming
// connections
TORRENT_TEST(socks5_utp_circumvent_proxy_ok)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses1, settings_pack::socks5, {}, false);
utp_only(ses0);
utp_only(ses1);
},
[](lt::session&, lt::alert const*) {},
// the UDP socket socks5 proxy support doesn't allow accepting direct
// connections, circumventing the proxy, so this transfer will fail,
// even though it would be reasonable for it to pass as well
expect_seed(false)
);
}
TORRENT_TEST(http_tcp_circumvent_proxy_reject)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses1, settings_pack::http);
},
[](lt::session&, lt::alert const*) {},
expect_seed(false)
);
}
// if we're not proxying peer connections, it's OK to accept incoming
// connections
TORRENT_TEST(http_tcp_circumvent_proxy_ok)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
set_proxy(ses1, settings_pack::http, {}, false);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
// the purpose of these tests is to make sure that the sessions can't actually
// talk directly to each other. i.e. they are negative tests. If they can talk
// directly to each other, all other tests in here may be broken.
TORRENT_TEST(no_proxy_tcp_banned)
{
run_test(
[](lt::session&, lt::session& ses1) { filter_ips(ses1); },
[](lt::session&, lt::alert const*) {},
expect_seed(false)
);
}
TORRENT_TEST(no_proxy_utp_banned)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{ utp_only(ses0); utp_only(ses1); filter_ips(ses1); },
[](lt::session&, lt::alert const*) {},
expect_seed(false)
);
}
TORRENT_TEST(piece_extent_affinity)
{
run_test(
[](lt::session& ses0, lt::session& ses1)
{
settings_pack p;
p.set_bool(settings_pack::piece_extent_affinity, true);
ses0.apply_settings(p);
ses1.apply_settings(p);
},
[](lt::session&, lt::alert const*) {},
expect_seed(true)
);
}
TORRENT_TEST(is_finished)
{
run_test(no_init
, [](lt::session& ses, lt::alert const* a) {
if (alert_cast<piece_finished_alert>(a))
{
TEST_EQUAL(is_finished(ses), false);
std::vector<download_priority_t> prio(4, dont_download);
ses.get_torrents()[0].prioritize_files(prio);
// applying the priorities is asynchronous. the torrent may not
// finish immediately
}
},
[](std::shared_ptr<lt::session> ses[2]) {
TEST_EQUAL(is_finished(*ses[0]), true);
TEST_EQUAL(is_finished(*ses[1]), true);
}
);
}
TORRENT_TEST(v1_only_magnet)
{
std::set<piece_index_t> passed;
run_test(no_init
, record_finished_pieces(passed)
, expect_seed(true)
, tx::v1_only | tx::magnet_download
);
TEST_EQUAL(passed.size(), 11);
}
TORRENT_TEST(disk_full)
{
run_test(no_init
, [](lt::session&, lt::alert const*) {}
// the disk filled up, we failed to complete the download
, expect_seed(false)
, {}
, test_disk().set_space_left(5 * lt::default_block_size)
);
}
TORRENT_TEST(disk_full_recover)
{
run_test(
[](lt::session& ses0, lt::session&)
{
settings_pack p;
p.set_int(settings_pack::optimistic_disk_retry, 30);
ses0.apply_settings(p);
},
[](lt::session&, lt::alert const* a) {
if (auto ta = alert_cast<lt::add_torrent_alert>(a))
{
// the torrent has to be auto-managed in order to automatically
// leave upload mode after it hits disk-full
ta->handle.set_flags(torrent_flags::auto_managed);
}
}
// the disk filled up, we failed to complete the download, but then the
// disk recovered and we completed it
, expect_seed(true)
, {}
, test_disk().set_space_left(10 * lt::default_block_size).set_recover_full_disk()
, test_disk()
, lt::seconds(65)
);
}
TORRENT_TEST(disk_full_recover_large_pieces)
{
run_test(
[](lt::session& ses0, lt::session&)
{
settings_pack p;
p.set_int(settings_pack::optimistic_disk_retry, 30);
ses0.apply_settings(p);
},
[](lt::session&, lt::alert const* a) {
if (auto ta = alert_cast<lt::add_torrent_alert>(a))
{
// the torrent has to be auto-managed in order to automatically
// leave upload mode after it hits disk-full
ta->handle.set_flags(torrent_flags::auto_managed);
}
}
// the disk filled up, we failed to complete the download, but then the
// disk recovered and we completed it
, expect_seed(true)
, tx::large_pieces
, test_disk().set_space_left(10 * lt::default_block_size).set_recover_full_disk()
, test_disk()
, lt::seconds(70)
);
}
// Below is a series of tests to transfer torrents with varying pad-file related
// traits
void run_torrent_test(std::shared_ptr<lt::torrent_info> ti)
{
using asio::ip::address;
address peer0 = addr("50.0.0.1");
address peer1 = addr("50.0.0.2");
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context ios0 { sim, peer0 };
sim::asio::io_context ios1 { sim, peer1 };
lt::session_proxy zombie[2];
lt::session_params params;
// setup settings pack to use for the session (customization point)
lt::settings_pack& pack = params.settings;
pack = settings();
pack.set_bool(settings_pack::disable_hash_checks, false);
// disable utp by default
pack.set_bool(settings_pack::enable_outgoing_utp, false);
pack.set_bool(settings_pack::enable_incoming_utp, false);
// disable encryption by default
pack.set_bool(settings_pack::prefer_rc4, false);
pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
pack.set_int(settings_pack::allowed_enc_level, settings_pack::pe_plaintext);
pack.set_str(settings_pack::listen_interfaces, "50.0.0.1:6881");
// create session
std::shared_ptr<lt::session> ses[2];
// session 0 is a downloader, session 1 is a seed
params.disk_io_constructor = test_disk();
ses[0] = std::make_shared<lt::session>(params, ios0);
pack.set_str(settings_pack::listen_interfaces, "50.0.0.2:6881");
params.disk_io_constructor = test_disk().set_files(existing_files_mode::full_valid);
ses[1] = std::make_shared<lt::session>(params, ios1);
// only monitor alerts for session 0 (the downloader)
print_alerts(*ses[0], [=](lt::session& ses, lt::alert const* a) {
if (auto ta = alert_cast<lt::add_torrent_alert>(a))
{
ta->handle.connect_peer(lt::tcp::endpoint(peer1, 6881));
}
}, 0);
print_alerts(*ses[1], [](lt::session&, lt::alert const*){}, 1);
lt::add_torrent_params atp;
atp.ti = ti;
atp.save_path = ".";
atp.flags &= ~lt::torrent_flags::auto_managed;
atp.flags &= ~lt::torrent_flags::paused;
ses[1]->async_add_torrent(atp);
auto torrent = atp.ti;
ses[0]->async_add_torrent(atp);
sim::timer t(sim, lt::seconds(10), [&](boost::system::error_code const&)
{
auto h = ses[0]->get_torrents();
auto ti = h[0].torrent_file_with_hashes();
if (ti->v2())
TEST_EQUAL(ti->v2_piece_hashes_verified(), true);
auto downloaded = serialize(*ti);
auto added = serialize(*torrent);
TEST_CHECK(downloaded == added);
TEST_CHECK(is_seed(*ses[0]));
TEST_CHECK(is_seed(*ses[1]));
h[0].force_recheck();
});
sim::timer t2(sim, lt::minutes(1), [&](boost::system::error_code const&)
{
// shut down
int idx = 0;
for (auto& s : ses)
{
zombie[idx++] = s->abort();
s.reset();
}
});
sim.run();
}
namespace {
std::shared_ptr<lt::torrent_info> test_torrent(lt::file_storage fs, lt::create_flags_t const flags)
{
lt::create_torrent ct(fs, fs.piece_length(), flags);
lt::settings_pack pack;
lt::error_code ec;
lt::set_piece_hashes(ct, "", pack, test_disk().set_files(existing_files_mode::full_valid)
, [](lt::piece_index_t p) { std::cout << "."; std::cout.flush();}, ec);
auto e = ct.generate();
std::vector<char> buf;
lt::bencode(std::back_inserter(buf), e);
return std::make_shared<lt::torrent_info>(buf, lt::from_span);
}
}
TORRENT_TEST(simple_torrent)
{
run_torrent_test(test_torrent(make_files(
{{0x3ff0, false}, {0x10, true}}, 0x4000), {}));
}
TORRENT_TEST(odd_last_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x4100, false}, {0x10, true}}, 0x4000), {}));
}
TORRENT_TEST(small_piece_size)
{
run_torrent_test(test_torrent(make_files(
{{0x3ff0, false}, {0x10, true}}, 0x2000), {}));
}
TORRENT_TEST(odd_piece_size)
{
run_torrent_test(test_torrent(make_files(
{{0x1ffe, false}, {0x1, true}}, 0x1fff), {}));
}
TORRENT_TEST(large_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x5000, false}, {0x100000000 - 0x5000, true}}, 0x100000), {}));
}
TORRENT_TEST(unaligned_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x3fff, false}, {0x10, true}}, 0x4000), {}));
}
TORRENT_TEST(piece_size_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x8000, false}, {0x8000, true}}, 0x8000), {}));
}
TORRENT_TEST(block_size_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x4000, false}, {0x4000, true}}, 0x4000), {}));
}
TORRENT_TEST(back_to_back_pad_file)
{
run_torrent_test(test_torrent(make_files(
{{0x3000, false}, {0x800, true}, {0x800, true}}, 0x4000), {}));
}
TORRENT_TEST(small_file_large_piece)
{
run_torrent_test(test_torrent(make_files(
{{0x833ed, false}, {0x7cc13, true}, {0x3d, false}, {0x7ffc3, true}, {0x14000, false}}, 0x80000), {}));
}
TORRENT_TEST(empty_file)
{
run_torrent_test(test_torrent(make_files(
{{0x3000, false}, {0, false}, {0x8000, false}}, 0x4000), {}));
}
|