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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
|
/*
Copyright (c) 2010, 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 "test.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "setup_transfer.hpp" // for addr()
#include "utils.hpp" // for print_alerts
#include "create_torrent.hpp"
#include "simulator/simulator.hpp"
#include "simulator/http_server.hpp"
#include "simulator/utils.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/file_storage.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/aux_/ip_helpers.hpp" // for is_v4
#include <boost/optional.hpp>
#include <iostream>
using namespace lt;
using namespace sim;
using chrono::duration_cast;
// seconds
const int duration = 10000;
template <typename Tp1, typename Tp2>
bool eq(Tp1 const lhs, Tp2 const rhs)
{
return std::abs(lt::duration_cast<seconds>(lhs - rhs).count()) <= 1;
}
void test_interval(int interval)
{
using sim::asio::ip::address_v4;
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
bool ran_to_completion = false;
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
// the timestamps of all announces
std::vector<lt::time_point> announces;
http.register_handler("/announce"
, [&announces,interval,&ran_to_completion](std::string /* method */
, std::string /* req */
, std::map<std::string, std::string>&)
{
// don't collect events once we're done. We're not interested in the
// tracker stopped announce for instance
if (!ran_to_completion)
announces.push_back(lt::clock_type::now());
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
return sim::send_response(200, "OK", size) + response;
});
std::vector<lt::time_point> announce_alerts;
lt::settings_pack default_settings = settings();
// since the test tracker is only listening on IPv4 we need to configure the
// client to do the same so that the number of tracker_announce_alerts matches
// the number of announces seen by the tracker
default_settings.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881");
lt::add_torrent_params default_add_torrent;
setup_swarm(1, swarm_test::upload, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack&) {}
// add torrent
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
// on alert
, [&](lt::alert const* a, lt::session&) {
if (ran_to_completion) return;
if (lt::alert_cast<lt::tracker_announce_alert>(a))
{
announce_alerts.push_back(a->timestamp());
}
}
// terminate
, [&](int const ticks, lt::session&) -> bool {
if (ticks > duration + 1)
{
ran_to_completion = true;
return true;
}
return false;
});
TEST_CHECK(ran_to_completion);
TEST_EQUAL(announce_alerts.size(), announces.size());
TEST_CHECK(announces.size() % 2 == 0);
lt::time_point last_announce = announces[0];
lt::time_point last_alert = announce_alerts[0];
for (int i = 2; i < int(announces.size()); i += 2)
{
// make sure the interval is within 1 second of what it's supposed to be
// (this accounts for network latencies, and the second-granularity
// timestamps)
TEST_CHECK(eq(duration_cast<lt::seconds>(announces[i] - last_announce), lt::seconds(interval)));
last_announce = announces[i];
TEST_CHECK(eq(duration_cast<lt::milliseconds>(announce_alerts[i] - last_alert), lt::seconds(interval)));
last_alert = announce_alerts[i];
}
}
template <typename AddTorrent, typename OnAlert>
std::vector<std::string> test_event(swarm_test_t const type
, AddTorrent add_torrent
, OnAlert on_alert)
{
using sim::asio::ip::address_v4;
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
// the request strings of all announces
std::vector<std::string> announces;
const int interval = 500;
http.register_handler("/announce"
, [&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
announces.push_back(req);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
return sim::send_response(200, "OK", size) + response;
});
lt::settings_pack default_settings = settings();
lt::add_torrent_params default_add_torrent;
setup_swarm(2, type, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack&) { }
// add torrent
, add_torrent
// on alert
, on_alert
// terminate
, [&](int const ticks, lt::session& ses) -> bool
{
return ticks > duration;
});
// this is some basic sanity checking of the announces that should always be
// true.
// the first announce should be event=started then no other announce should
// have event=started.
// only the last announce should have event=stopped.
TEST_CHECK(announces.size() > 2);
// to keep things simple, just consider one of the v1 or v2 announces, since
// we use a hybrid torrent, we get double announces.
std::map<std::string, std::vector<std::string>> announces_ih;
for (auto&& a : announces)
{
auto const ih = a.find("info_hash=");
TEST_CHECK(ih != std::string::npos);
auto const key = a.substr(ih, 20);
announces_ih[key].push_back(std::move(a));
}
for (auto const& entry : announces_ih)
{
auto const& ann = entry.second;
TEST_CHECK(ann.size() > 2);
TEST_CHECK(ann.front().find("&event=started") != std::string::npos);
for (auto const& a : span<std::string const>(ann).subspan(1))
TEST_CHECK(a.find("&event=started") == std::string::npos);
TEST_CHECK(ann.back().find("&event=stopped") != std::string::npos);
for (auto const& a : span<std::string const>(ann).first(ann.size() - 1))
TEST_CHECK(a.find("&event=stopped") == std::string::npos);
}
return announces_ih.begin()->second;
}
TORRENT_TEST(event_completed_downloading)
{
auto const announces = test_event(swarm_test::download
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
, [&](lt::alert const*, lt::session&) {}
);
// make sure there's exactly one event=completed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 1);
}
TORRENT_TEST(event_completed_downloading_replace_trackers)
{
auto const announces = test_event(swarm_test::download
, [](lt::add_torrent_params& params) {}
, [&](lt::alert const* a, lt::session&) {
if (auto const* at = alert_cast<add_torrent_alert>(a))
at->handle.replace_trackers({announce_entry{"http://2.2.2.2:8080/announce"}});
}
);
// make sure there's exactly one event=completed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 1);
}
TORRENT_TEST(event_completed_seeding)
{
auto const announces = test_event(swarm_test::upload | swarm_test::no_auto_stop
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
, [&](lt::alert const*, lt::session&) {}
);
// make sure there are no event=completed, since we added the torrent as a
// seed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 0);
}
TORRENT_TEST(event_completed_seeding_replace_trackers)
{
auto const announces = test_event(swarm_test::upload | swarm_test::no_auto_stop
, [](lt::add_torrent_params& params) {}
, [&](lt::alert const* a, lt::session&) {
if (auto const* at = alert_cast<add_torrent_alert>(a))
at->handle.replace_trackers({announce_entry{"http://2.2.2.2:8080/announce"}});
}
);
// make sure there are no event=completed, since we added the torrent as a
// seed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 0);
}
TORRENT_TEST(announce_interval_440)
{
test_interval(440);
}
TORRENT_TEST(announce_interval_1800)
{
test_interval(1800);
}
TORRENT_TEST(announce_interval_1200)
{
test_interval(3600);
}
struct sim_config : sim::default_config
{
explicit sim_config(bool ipv6 = true) : ipv6(ipv6) {}
chrono::high_resolution_clock::duration hostname_lookup(
asio::ip::address const& requestor
, std::string hostname
, std::vector<asio::ip::address>& result
, boost::system::error_code& ec) override
{
if (hostname == "tracker.com")
{
result.push_back(make_address_v4("123.0.0.2"));
if (ipv6)
result.push_back(make_address_v6("ff::dead:beef"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
if (hostname == "localhost")
{
result.push_back(make_address_v4("127.0.0.1"));
if (ipv6)
result.push_back(make_address_v6("::1"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(1));
}
if (hostname == "xn--tracker-.com")
{
result.push_back(make_address_v4("123.0.0.2"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
if (hostname == "redirector.com")
{
result.push_back(make_address_v4("123.0.0.4"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
return default_config::hostname_lookup(requestor, hostname, result, ec);
}
bool ipv6;
};
void on_alert_notify(lt::session* ses)
{
post(ses->get_context(), [ses] {
std::vector<lt::alert*> alerts;
ses->pop_alerts(&alerts);
for (lt::alert* a : alerts)
{
lt::time_duration d = a->timestamp().time_since_epoch();
std::uint32_t const millis = std::uint32_t(
lt::duration_cast<lt::milliseconds>(d).count());
std::printf("%4u.%03u: %s\n", millis / 1000, millis % 1000,
a->message().c_str());
}
});
}
void test_announce()
{
using sim::asio::ip::address_v4;
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
int announces = 0;
// expect announced IP & port
std::string const expect_port = "&port=1234";
std::string const expect_ip = "&ip=1.2.3.4";
http.register_handler("/announce"
, [&announces, expect_port, expect_ip](std::string method, std::string req
, std::map<std::string, std::string>&)
{
++announces;
TEST_EQUAL(method, "GET");
TEST_CHECK(req.find(expect_port) != std::string::npos);
TEST_CHECK(req.find(expect_ip) != std::string::npos);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
});
{
lt::session_proxy zombie;
std::vector<asio::ip::address> ips;
ips.push_back(make_address("123.0.0.3"));
asio::io_context ios(sim, ips);
lt::settings_pack sett = settings();
sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881");
sett.set_str(settings_pack::announce_ip, "1.2.3.4");
sett.set_int(settings_pack::announce_port, 1234);
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("http://2.2.2.2:8080/announce");
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
{
t.pause();
}
});
// then shut down 10 seconds in
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TEST_EQUAL(announces, 2);
}
// this test makes sure that a seed can overwrite its announced IP & port
TORRENT_TEST(announce_ip_port) {
test_announce();
}
static const int num_interfaces = 3;
void test_ipv6_support(char const* listen_interfaces
, int const expect_v4, int const expect_v6)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server_v4(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context web_server_v6(sim, make_address_v6("ff::dead:beef"));
// listen on port 8080
sim::http_server http_v4(web_server_v4, 8080);
sim::http_server http_v6(web_server_v6, 8080);
int v4_announces = 0;
int v6_announces = 0;
// if we're not listening we'll just report port 0
std::string const expect_port = (listen_interfaces && listen_interfaces == ""_sv)
? "&port=1" : "&port=6881";
http_v4.register_handler("/announce"
, [&v4_announces,expect_port](std::string method, std::string req
, std::map<std::string, std::string>&)
{
++v4_announces;
TEST_EQUAL(method, "GET");
TEST_CHECK(req.find(expect_port) != std::string::npos);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
});
http_v6.register_handler("/announce"
, [&v6_announces,expect_port](std::string method, std::string req
, std::map<std::string, std::string>&)
{
++v6_announces;
TEST_EQUAL(method, "GET");
TEST_CHECK(req.find(expect_port) != std::string::npos);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
});
{
lt::session_proxy zombie;
std::vector<asio::ip::address> ips;
for (int i = 0; i < num_interfaces; i++)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "123.0.0.%d", i + 1);
ips.push_back(make_address(ep));
std::snprintf(ep, sizeof(ep), "ffff::1337:%d", i + 1);
ips.push_back(make_address(ep));
}
asio::io_context ios(sim, ips);
lt::settings_pack sett = settings();
if (listen_interfaces)
{
sett.set_str(settings_pack::listen_interfaces, listen_interfaces);
}
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
//TODO: parameterize http vs. udp here
p.trackers.push_back("http://tracker.com:8080/announce");
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
{
t.pause();
}
});
// then shut down 10 seconds in
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TEST_EQUAL(v4_announces, expect_v4);
TEST_EQUAL(v6_announces, expect_v6);
}
void test_udpv6_support(char const* listen_interfaces
, int const expect_v4, int const expect_v6)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server_v4(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context web_server_v6(sim, make_address_v6("ff::dead:beef"));
int v4_announces = 0;
int v6_announces = 0;
{
lt::session_proxy zombie;
std::vector<asio::ip::address> ips;
for (int i = 0; i < num_interfaces; i++)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "123.0.0.%d", i + 1);
ips.push_back(make_address(ep));
std::snprintf(ep, sizeof(ep), "ffff::1337:%d", i + 1);
ips.push_back(make_address(ep));
}
asio::io_context ios(sim, ips);
lt::settings_pack sett = settings();
if (listen_interfaces)
{
sett.set_str(settings_pack::listen_interfaces, listen_interfaces);
}
auto ses = std::make_unique<lt::session>(sett, ios);
// since we don't have a udp tracker to run in the sim, looking for the
// alerts is the closest proxy
ses->set_alert_notify([&]{
post(ses->get_context(), [&] {
std::vector<lt::alert*> alerts;
ses->pop_alerts(&alerts);
for (lt::alert* a : alerts)
{
lt::time_duration d = a->timestamp().time_since_epoch();
std::uint32_t const millis = std::uint32_t(
lt::duration_cast<lt::milliseconds>(d).count());
std::printf("%4u.%03u: %s\n", millis / 1000, millis % 1000,
a->message().c_str());
if (auto tr = alert_cast<tracker_announce_alert>(a))
{
if (lt::aux::is_v4(tr->local_endpoint))
++v4_announces;
else
++v6_announces;
}
else if (alert_cast<tracker_error_alert>(a))
{
TEST_ERROR("unexpected tracker error");
}
}
});
});
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("udp://tracker.com:8080/announce");
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
{
t.pause();
}
});
// then shut down 10 seconds in
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TEST_EQUAL(v4_announces, expect_v4);
TEST_EQUAL(v6_announces, expect_v6);
}
// this test makes sure that a tracker whose host name resolves to both IPv6 and
// IPv4 addresses will be announced to twice, once for each address family
TORRENT_TEST(ipv6_support)
{
// null means default
test_ipv6_support(nullptr, num_interfaces * 2, num_interfaces * 2);
}
TORRENT_TEST(announce_no_listen)
{
// if we don't listen on any sockets at all we should not announce to trackers
test_ipv6_support("", 0, 0);
}
TORRENT_TEST(announce_udp_no_listen)
{
// if we don't listen on any sockets at all we should not announce to trackers
test_udpv6_support("", 0, 0);
}
TORRENT_TEST(ipv6_support_bind_v4_v6_any)
{
// 2 because there's one announce on startup and one when shutting down
// IPv6 will send announces for each interface
test_ipv6_support("0.0.0.0:6881,[::0]:6881", num_interfaces * 2, num_interfaces * 2);
}
TORRENT_TEST(ipv6_support_bind_v6_any)
{
test_ipv6_support("[::0]:6881", 0, num_interfaces * 2);
}
TORRENT_TEST(ipv6_support_bind_v4)
{
test_ipv6_support("123.0.0.3:6881", 2, 0);
}
TORRENT_TEST(ipv6_support_bind_v6)
{
test_ipv6_support("[ffff::1337:1]:6881", 0, 2);
}
TORRENT_TEST(ipv6_support_bind_v6_3interfaces)
{
test_ipv6_support("[ffff::1337:1]:6881,[ffff::1337:2]:6881,[ffff::1337:3]:6881", 0, 3 * 2);
}
TORRENT_TEST(ipv6_support_bind_v4_v6)
{
test_ipv6_support("123.0.0.3:6881,[ffff::1337:1]:6881", 2, 2);
}
TORRENT_TEST(ipv6_support_bind_v6_v4)
{
test_ipv6_support("[ffff::1337:1]:6881,123.0.0.3:6881", 2, 2);
}
// this runs a simulation of a torrent with tracker(s), making sure the request
// received by the tracker matches the expectation.
// The Setup function is run first, giving the test an opportunity to add
// trackers to the torrent. It's expected to return the number of seconds to
// wait until test2 is called.
// The Announce function is called on http requests. Test1 is run on the session
// 5 seconds after startup. The tracker is running at 123.0.0.2 (or tracker.com)
// port 8080.
template <typename Setup, typename Announce, typename Test1, typename Test2>
void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
, char const* url_path = "/announce"
, char const* redirect = "http://123.0.0.2/announce")
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context tracker_ios(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context tracker_ios6(sim, make_address_v6("ff::dead:beef"));
sim::asio::io_context redirector_ios(sim, make_address_v4("123.0.0.4"));
sim::asio::io_context tracker_lo_ios(sim, make_address_v4("127.0.0.1"));
sim::asio::io_context tracker_lo_ios6(sim, make_address_v6("::1"));
// listen on port 8080
sim::http_server http(tracker_ios, 8080);
sim::http_server http6(tracker_ios6, 8080);
sim::http_server http_lo(tracker_lo_ios, 8080);
sim::http_server http6_lo(tracker_lo_ios6, 8080);
sim::http_server http_redirect(redirector_ios, 8080);
http.register_handler(url_path, a);
http6.register_handler(url_path, a);
http_lo.register_handler(url_path, a);
http6_lo.register_handler(url_path, a);
http_redirect.register_redirect(url_path, redirect);
lt::session_proxy zombie;
asio::io_context ios(sim, { make_address_v4("123.0.0.3")
, make_address_v6("ffff::1337") });
lt::settings_pack sett = settings();
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
int const delay = setup(p, *ses);
ses->async_add_torrent(p);
// run the test 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses,&test1](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
test1(h);
});
sim::timer t2(sim, lt::seconds(5 + delay)
, [&ses,&test2](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
test2(h);
});
// then shut down 10 seconds in
sim::timer t3(sim, lt::seconds(10 + delay)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
template <typename Announce, typename Test1, typename Test2>
void tracker_test(Announce a, Test1 test1, Test2 test2, char const* url_path = "/announce")
{
tracker_test([](lt::add_torrent_params& p, lt::session&) {
p.trackers.push_back("http://tracker.com:8080/announce");
return 5;
},
a, test1, test2, url_path);
}
template <typename Announce, typename Test>
void announce_entry_test(Announce a, Test t, char const* url_path = "/announce")
{
tracker_test(a
, [&t] (torrent_handle h) {
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
t(ae);
}
, [](torrent_handle){}
, url_path);
}
// test that we correctly omit announcing an event=stopped to a tracker we never
// managed to send an event=start to
TORRENT_TEST(omit_stop_event)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
lt::session_proxy zombie;
asio::io_context ios(sim, { make_address_v4("123.0.0.3"), make_address_v6("ff::dead:beef")});
lt::settings_pack sett = settings();
std::unique_ptr<lt::session> ses(new lt::session(sett, ios));
print_alerts(*ses);
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("udp://tracker.com:8080/announce");
ses->async_add_torrent(p);
// run the test 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
});
int stop_announces = 0;
sim::timer t2(sim, lt::seconds(1800)
, [&](boost::system::error_code const&)
{
// make sure we don't announce a stopped event when stopping
print_alerts(*ses, [&](lt::session&, lt::alert const* a) {
if (alert_cast<lt::tracker_announce_alert>(a))
++stop_announces;
});
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
h.set_flags(torrent_flags::paused, torrent_flags::paused | torrent_flags::auto_managed);
});
// then shut down 10 seconds in
sim::timer t3(sim, lt::seconds(1810)
, [&](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
TEST_EQUAL(stop_announces, 0);
}
TORRENT_TEST(test_error)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d14:failure reason4:teste");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "test");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code(errors::tracker_failure));
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 1);
}
});
}
TORRENT_TEST(test_no_announce_path)
{
tracker_test(
[](lt::add_torrent_params& p, lt::session&) {
p.trackers.push_back("http://tracker.com:8080");
return 5;
},
[](std::string method, std::string req, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d5:peers6:aaaaaae");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h)
{
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
TEST_EQUAL(ae.url, "http://tracker.com:8080");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
}
, [](torrent_handle){}
, "/");
}
TORRENT_TEST(paused_session)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context tracker_ios(sim, make_address_v4("123.0.0.2"));
// listen on port 8080
sim::http_server http(tracker_ios, 8080);
int announces = 0;
http.register_handler("/announce",
[&announces](std::string method, std::string req, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
++announces;
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers6:aaaaaae");
return sim::send_response(200, "OK", size) + response;
}
);
lt::session_proxy zombie;
asio::io_context ios(sim, { make_address_v4("123.0.0.3")
, make_address_v6("ffff::1337") });
lt::settings_pack sett = settings();
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("http://123.0.0.2:8080/announce");
ses->async_add_torrent(p);
lt::seconds timeline(5);
// pause the session
sim::timer t1(sim, timeline
, [&announces,&ses](boost::system::error_code const&)
{
// make sure we got 1 announce
TEST_EQUAL(announces, 1);
ses->pause();
});
// wait until the next tracker announce should have happened, but didn't
// because the session is paused
timeline += seconds(1801);
sim::timer t2(sim, timeline
, [&announces,&ses](boost::system::error_code const&)
{
// the stop is announced
TEST_EQUAL(announces, 2);
ses->resume();
});
timeline += seconds(5);
sim::timer t3(sim, timeline
, [&announces](boost::system::error_code const&)
{
// make sure we got another announce
TEST_EQUAL(announces, 3);
});
timeline += seconds(5);
// then shut down
sim::timer t4(sim, timeline
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TORRENT_TEST(test_warning)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d5:peers6:aaaaaa15:warning message5:test2e");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "test2");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
});
}
TORRENT_TEST(test_scrape_data_in_announce)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response),
"d5:peers6:aaaaaa8:completei1e10:incompletei2e10:downloadedi3e11:downloadersi4ee");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_complete, 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_incomplete, 2);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_downloaded, 3);
}
});
}
TORRENT_TEST(test_scrape)
{
tracker_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response),
"d5:filesd20:ababababababababababd8:completei1e10:downloadedi3e10:incompletei2eeee");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h)
{
h.scrape_tracker();
}
, [](torrent_handle h)
{
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_incomplete, 2);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_complete, 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_downloaded, 3);
}
}
, "/scrape");
}
TORRENT_TEST(test_http_status)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
return sim::send_response(410, "Not A Tracker", 0);
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "Not A Tracker");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code(410, http_category()));
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 1);
}
});
}
TORRENT_TEST(test_interval)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response)
, "d10:tracker id8:testteste");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
TEST_EQUAL(ae.trackerid, "testtest");
});
}
TORRENT_TEST(test_invalid_bencoding)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response)
, "d10:tracer idteste");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code(bdecode_errors::expected_value
, bdecode_category()));
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 1);
}
});
}
TORRENT_TEST(try_next)
{
// test that we move on to try the next tier if the first one fails
bool got_announce = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session&)
{
// TODO: 3 use tracker_tiers here to put the trackers in different tiers
p.trackers.push_back("udp://failing-tracker.com/announce");
p.trackers.push_back("http://failing-tracker.com/announce");
// this is the working tracker
p.trackers.push_back("http://tracker.com:8080/announce");
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
got_announce = true;
TEST_EQUAL(method, "GET");
char response[500];
// respond with an empty peer list
int const size = std::snprintf(response, sizeof(response), "d5:peers0:e");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h) {}
, [](torrent_handle h)
{
torrent_status st = h.status();
TEST_EQUAL(st.current_tracker, "http://tracker.com:8080/announce");
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 3);
for (int i = 0; i < int(tr.size()); ++i)
{
std::printf("tracker \"%s\"\n", tr[i].url.c_str());
if (tr[i].url == "http://tracker.com:8080/announce")
{
for (auto const& aep : tr[i].endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
TEST_EQUAL(tr[i].verified, true);
}
else if (tr[i].url == "http://failing-tracker.com/announce")
{
for (auto const& aep : tr[i].endpoints)
{
TEST_CHECK(aep.info_hashes[protocol_version::V1].fails >= 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error
, error_code(boost::asio::error::host_not_found));
}
TEST_EQUAL(tr[i].verified, false);
}
else if (tr[i].url == "udp://failing-tracker.com/announce")
{
TEST_EQUAL(tr[i].verified, false);
for (auto const& aep : tr[i].endpoints)
{
TEST_CHECK(aep.info_hashes[protocol_version::V1].fails >= 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error
, error_code(boost::asio::error::host_not_found));
}
}
else
{
TEST_ERROR(("unexpected tracker URL: " + tr[i].url).c_str());
}
}
});
TEST_EQUAL(got_announce, true);
}
TORRENT_TEST(clear_error)
{
// make sure we clear the error from a previous attempt when succeeding
// a tracker announce
int num_announces = 0;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
// make sure we just listen on a single listen interface
pack.set_str(settings_pack::listen_interfaces, "123.0.0.3:0");
pack.set_int(settings_pack::min_announce_interval, 1);
pack.set_int(settings_pack::tracker_backoff, 1);
ses.apply_settings(pack);
p.trackers.push_back("http://tracker.com:8080/announce");
return 60;
},
[&](std::string method, std::string req, std::map<std::string, std::string>&)
{
// don't count the stopped event when shutting down
if (req.find("&event=stopped&") != std::string::npos)
{
return sim::send_response(200, "OK", 2) + "de";
}
if (num_announces++ == 0)
{
// the first announce fails
return std::string{};
}
// the second announce succeeds, with an empty peer list
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h) {
}
, [&](torrent_handle h)
{
std::vector<announce_entry> const tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
std::printf("tracker \"%s\"\n", tr[0].url.c_str());
TEST_EQUAL(tr[0].url, "http://tracker.com:8080/announce");
TEST_EQUAL(tr[0].endpoints.size(), 1);
auto const& aep = tr[0].endpoints[0];
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
TEST_CHECK(!aep.info_hashes[protocol_version::V1].last_error);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
#if TORRENT_ABI_VERSION <= 2
TEST_EQUAL(aep.fails, 0);
TEST_CHECK(!aep.last_error);
TEST_EQUAL(aep.message, "");
#endif
});
TEST_EQUAL(num_announces, 2);
}
std::shared_ptr<torrent_info> make_torrent(bool priv)
{
file_storage fs;
fs.add_file("foobar", 13241);
lt::create_torrent ct(fs);
ct.add_tracker("http://tracker.com:8080/announce");
for (piece_index_t i(0); i < piece_index_t(ct.num_pieces()); ++i)
ct.set_hash(i, sha1_hash::max());
ct.set_priv(priv);
entry e = ct.generate();
std::vector<char> buf;
bencode(std::back_inserter(buf), e);
return std::make_shared<torrent_info>(buf, from_span);
}
// make sure we _do_ send our IPv6 address to trackers for private torrents
TORRENT_TEST(tracker_ipv6_argument)
{
bool got_announce = false;
bool got_ipv6 = false;
bool got_ipv4 = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::anonymous_mode, false);
pack.set_str(settings_pack::listen_interfaces, "123.0.0.3:0,[ffff::1337]:0");
ses.apply_settings(pack);
p.ti = make_torrent(true);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
got_announce = true;
bool const stop_event = req.find("&event=stopped") != std::string::npos;
// stop events don't need to advertise the IPv6/IPv4 address
{
std::string::size_type const pos = req.find("&ipv6=");
TEST_CHECK(pos != std::string::npos || stop_event);
got_ipv6 |= pos != std::string::npos;
// make sure the IPv6 argument is url encoded
TEST_EQUAL(req.substr(pos + 6, req.substr(pos + 6).find_first_of('&'))
, "ffff%3a%3a1337");
}
{
std::string::size_type const pos = req.find("&ipv4=");
TEST_CHECK(pos != std::string::npos || stop_event);
got_ipv4 |= pos != std::string::npos;
TEST_EQUAL(req.substr(pos + 6, req.substr(pos + 6).find_first_of('&')), "123.0.0.3");
}
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle) {}
, [](torrent_handle) {});
TEST_EQUAL(got_announce, true);
TEST_EQUAL(got_ipv6, true);
}
TORRENT_TEST(tracker_key_argument)
{
std::set<std::string> keys;
tracker_test(
[](lt::add_torrent_params& p, lt::session&)
{
p.ti = make_torrent(true);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string, std::string req
, std::map<std::string, std::string>&)
{
auto const pos = req.find("&key=");
TEST_CHECK(pos != std::string::npos);
keys.insert(req.substr(pos + 5, req.find_first_of('&', pos + 5) - pos - 5));
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle h) {}
, [](torrent_handle h) {});
// make sure we got the same key for all listen socket interface
TEST_EQUAL(keys.size(), 1);
}
// make sure we do _not_ send our IPv6 address to trackers for non-private
// torrents
TORRENT_TEST(tracker_ipv6_argument_non_private)
{
bool got_announce = false;
bool got_ipv6 = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::anonymous_mode, false);
ses.apply_settings(pack);
p.ti = make_torrent(false);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
got_announce = true;
std::string::size_type pos = req.find("&ipv6=");
TEST_CHECK(pos == std::string::npos);
got_ipv6 |= pos != std::string::npos;
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle) {}
, [](torrent_handle) {});
TEST_EQUAL(got_announce, true);
TEST_EQUAL(got_ipv6, false);
}
TORRENT_TEST(tracker_ipv6_argument_privacy_mode)
{
bool got_announce = false;
bool got_ipv6 = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::anonymous_mode, true);
ses.apply_settings(pack);
p.ti = make_torrent(true);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
got_announce = true;
std::string::size_type pos = req.find("&ipv6=");
TEST_CHECK(pos == std::string::npos);
got_ipv6 |= pos != std::string::npos;
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle) {}
, [](torrent_handle) {});
TEST_EQUAL(got_announce, true);
TEST_EQUAL(got_ipv6, false);
}
TORRENT_TEST(tracker_user_agent_privacy_mode_public_torrent)
{
bool got_announce = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::anonymous_mode, true);
pack.set_str(settings_pack::user_agent, "test_agent/1.2.3");
ses.apply_settings(pack);
p.ti = make_torrent(false);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>& headers)
{
got_announce = true;
// in anonymous mode we should send a generic user agent
TEST_CHECK(headers["user-agent"] == "curl/7.81.0");
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle h) {}
, [](torrent_handle h) {});
TEST_EQUAL(got_announce, true);
}
TORRENT_TEST(tracker_user_agent_privacy_mode_private_torrent)
{
bool got_announce = false;
tracker_test(
[](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::anonymous_mode, true);
pack.set_str(settings_pack::user_agent, "test_agent/1.2.3");
ses.apply_settings(pack);
p.ti = make_torrent(true);
p.info_hashes = lt::info_hash_t{};
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>& headers)
{
got_announce = true;
// in anonymous mode we should still send the user agent for private
// torrents (since private trackers sometimes require it)
TEST_CHECK(headers["user-agent"] == "test_agent/1.2.3");
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle h) {}
, [](torrent_handle h) {});
TEST_EQUAL(got_announce, true);
}
bool test_ssrf(char const* announce_path, bool const feature_on
, char const* tracker_url)
{
bool got_announce = false;
tracker_test(
[&](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::ssrf_mitigation, feature_on);
ses.apply_settings(pack);
p.trackers.emplace_back(tracker_url);
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>& headers)
{
got_announce = true;
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle h) {}
, [](torrent_handle h) {}
, announce_path);
return got_announce;
}
TORRENT_TEST(ssrf_localhost)
{
TEST_CHECK(test_ssrf("/announce", true, "http://localhost:8080/announce"));
TEST_CHECK(!test_ssrf("/unusual-announce-path", true, "http://localhost:8080/unusual-announce-path"));
TEST_CHECK(test_ssrf("/unusual-announce-path", false, "http://localhost:8080/unusual-announce-path"));
TEST_CHECK(!test_ssrf("/short", true, "http://localhost:8080/short"));
TEST_CHECK(test_ssrf("/short", false, "http://localhost:8080/short"));
}
TORRENT_TEST(ssrf_IPv4)
{
TEST_CHECK(test_ssrf("/announce", true, "http://127.0.0.1:8080/announce"));
TEST_CHECK(!test_ssrf("/unusual-announce-path", true, "http://127.0.0.1:8080/unusual-announce-path"));
TEST_CHECK(test_ssrf("/unusual-announce-path", false, "http://127.0.0.1:8080/unusual-announce-path"));
}
TORRENT_TEST(ssrf_IPv6)
{
TEST_CHECK(test_ssrf("/announce", true, "http://[::1]:8080/announce"));
TEST_CHECK(!test_ssrf("/unusual-announce-path", true, "http://[::1]:8080/unusual-announce-path"));
TEST_CHECK(test_ssrf("/unusual-announce-path", false, "http://[::1]:8080/unusual-announce-path"));
}
TORRENT_TEST(ssrf_query_string)
{
// tracker URLs that come pre-baked with query string arguments will be
// rejected when SSRF-mitigation is enabled
TEST_CHECK(!test_ssrf("/announce", true, "http://tracker.com:8080/announce?info_hash=abc"));
TEST_CHECK(!test_ssrf("/announce", true, "http://tracker.com:8080/announce?iNfo_HaSh=abc"));
TEST_CHECK(!test_ssrf("/announce", true, "http://tracker.com:8080/announce?event=abc"));
TEST_CHECK(!test_ssrf("/announce", true, "http://tracker.com:8080/announce?EvEnT=abc"));
TEST_CHECK(test_ssrf("/announce", false, "http://tracker.com:8080/announce?info_hash=abc"));
TEST_CHECK(test_ssrf("/announce", false, "http://tracker.com:8080/announce?iNfo_HaSh=abc"));
TEST_CHECK(test_ssrf("/announce", false, "http://tracker.com:8080/announce?event=abc"));
}
bool test_idna(char const* tracker_url, char const* redirect
, bool const feature_on)
{
bool got_announce = false;
tracker_test(
[&](lt::add_torrent_params& p, lt::session& ses)
{
settings_pack pack;
pack.set_bool(settings_pack::allow_idna, feature_on);
ses.apply_settings(pack);
p.trackers.emplace_back(tracker_url);
return 60;
},
[&](std::string method, std::string req
, std::map<std::string, std::string>& headers)
{
got_announce = true;
return sim::send_response(200, "OK", 11) + "d5:peers0:e";
}
, [](torrent_handle h) {}
, [](torrent_handle h) {}
, "/announce"
, redirect ? redirect : ""
);
return got_announce;
}
TORRENT_TEST(tracker_idna)
{
TEST_EQUAL(test_idna("http://tracker.com:8080/announce", nullptr, true), true);
TEST_EQUAL(test_idna("http://tracker.com:8080/announce", nullptr, false), true);
TEST_EQUAL(test_idna("http://xn--tracker-.com:8080/announce", nullptr, true), true);
TEST_EQUAL(test_idna("http://xn--tracker-.com:8080/announce", nullptr, false), false);
}
TORRENT_TEST(tracker_idna_redirect)
{
TEST_EQUAL(test_idna("http://redirector.com:8080/announce", "http://xn--tracker-.com:8080/announce", true), true);
TEST_EQUAL(test_idna("http://redirector.com:8080/announce", "http://xn--tracker-.com:8080/announce", false), false);
}
// This test sets up two peers, one seed an one downloader. The downloader has
// two trackers, both in tier 0. The behavior we expect is that it picks one of
// the trackers at random and announces to it. Since both trackers are working,
// it should not announce to the tracker it did not initially pick.
struct tracker_ent
{
std::string url;
int tier;
};
void test_tracker_tiers(lt::settings_pack pack
, std::vector<address> local_addresses
, std::vector<tracker_ent> trackers
, std::function<void(int (&)[7])> test
, boost::optional<std::function<void(int (&)[7])>> test2 = boost::none)
{
using namespace libtorrent;
pack.set_int(settings_pack::alert_mask, alert_category::error
| alert_category::status
| alert_category::torrent_log);
// setup the simulation
struct sim_config : sim::default_config
{
chrono::high_resolution_clock::duration hostname_lookup(
asio::ip::address const& requestor
, std::string hostname
, std::vector<asio::ip::address>& result
, boost::system::error_code& ec)
{
if (hostname == "ipv6-only-tracker.com")
{
result.push_back(addr("f8e0::1"));
}
else if (hostname == "ipv4-only-tracker.com")
{
result.push_back(addr("3.0.0.1"));
}
else if (hostname == "dual-tracker.com")
{
result.push_back(addr("f8e0::2"));
result.push_back(addr("3.0.0.2"));
}
else return default_config::hostname_lookup(requestor, hostname, result, ec);
return lt::duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
};
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context ios0 { sim, local_addresses};
sim::asio::io_context tracker1(sim, addr("3.0.0.1"));
sim::asio::io_context tracker2(sim, addr("3.0.0.2"));
sim::asio::io_context tracker3(sim, addr("3.0.0.3"));
sim::asio::io_context tracker4(sim, addr("3.0.0.4"));
sim::asio::io_context tracker5(sim, addr("f8e0::1"));
sim::asio::io_context tracker6(sim, addr("f8e0::2"));
sim::asio::io_context tracker7(sim, addr("3.0.0.5"));
sim::http_server http1(tracker1, 8080);
sim::http_server http2(tracker2, 8080);
sim::http_server http3(tracker3, 8080);
sim::http_server http4(tracker4, 8080);
sim::http_server http5(tracker5, 8080);
sim::http_server http6(tracker6, 8080);
sim::http_server http7(tracker7, 8080);
int received_announce[7] = {0, 0, 0, 0, 0, 0, 0};
auto const return_no_peers = [&](std::string method, std::string req
, std::map<std::string, std::string>&, int const tracker_index)
{
++received_announce[tracker_index];
std::string const ret = "d8:intervali1800e5:peers0:e";
return sim::send_response(200, "OK", static_cast<int>(ret.size())) + ret;
};
auto const return_404 = [&](std::string method, std::string req
, std::map<std::string, std::string>&, int const tracker_index)
{
++received_announce[tracker_index];
return sim::send_response(404, "Not Found", 0);
};
using namespace std::placeholders;
http1.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 0));
http2.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 1));
http3.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 2));
http4.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 3));
http5.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 4));
http6.register_handler("/announce", std::bind(return_no_peers, _1, _2, _3, 5));
http7.register_handler("/announce", std::bind(return_404, _1, _2, _3, 6));
lt::session_proxy zombie;
// create session
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881,[::]:6881");
auto ses = std::make_shared<lt::session>(pack, ios0);
print_alerts(*ses);
lt::add_torrent_params params = ::create_torrent(1);
params.flags &= ~lt::torrent_flags::auto_managed;
params.flags &= ~lt::torrent_flags::paused;
for (auto const& t : trackers)
params.ti->add_tracker("http://" + t.url + ":8080/announce", t.tier);
params.save_path = save_path(0);
ses->async_add_torrent(params);
sim::timer t(sim, lt::seconds(30), [&](boost::system::error_code const&)
{
test(received_announce);
if (test2)
{
std::memset(&received_announce, 0, sizeof(received_announce));
}
else
{
zombie = ses->abort();
ses.reset();
}
});
if (test2)
{
sim::timer t2(sim, lt::minutes(31), [&](boost::system::error_code const&)
{
(*test2)(received_announce);
zombie = ses->abort();
ses.reset();
});
sim.run();
}
else
{
sim.run();
}
}
bool one_of(int a, int b)
{
return (a == 2 && b == 0) || (a == 0 && b == 2);
}
// the torrent is a hybrid v1 and v2 torrent, so there is one announce per
// info-hash
TORRENT_TEST(tracker_tiers_multi_homed)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_CHECK(one_of(a[0], a[1]));
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
TEST_EQUAL(a[6], 0);
});
}
TORRENT_TEST(tracker_tiers_all_trackers_multi_homed)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
TEST_EQUAL(a[6], 0);
});
}
TORRENT_TEST(tracker_tiers_all_tiers_multi_homed)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_CHECK(one_of(a[0], a[1]));
TEST_CHECK(one_of(a[2], a[3]));
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
TEST_EQUAL(a[6], 0);
});
}
TORRENT_TEST(tracker_tiers_all_trackers_and_tiers_multi_homed)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 2);
TEST_EQUAL(a[3], 2);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_CHECK(one_of(a[0], a[1]));
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers_all_trackers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers_all_tiers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_CHECK(one_of(a[0], a[1]));
TEST_CHECK(one_of(a[2], a[3]));
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers_all_trackers_and_tiers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 2);
TEST_EQUAL(a[3], 2);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
// in this case, we only have an IPv4 address, and the first tracker resolves
// only to an IPv6 address. Make sure we move on to the next one in the tier
TORRENT_TEST(tracker_tiers_unreachable_tracker)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"f8e0::1", 0}, {"3.0.0.2", 0}, {"3.0.0.3", 1}, {"3.0.0.4", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
});
}
// in this test, we have both v6 and v4 connectivity, and we have two trackers
// One is v6 only and one is dual. Since the first tracker was announced to
// using IPv6, the second tracker will *only* be used for IPv4, and not to
// announce IPv6 to again.
TORRENT_TEST(tracker_tiers_v4_and_v6_same_tier)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"ipv6-only-tracker.com", 0}, {"dual-tracker.com", 0}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 2);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers_v4_and_v6_different_tiers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"ipv6-only-tracker.com", 0}, {"dual-tracker.com", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 2);
TEST_EQUAL(a[5], 0);
});
}
// in the same scenario as above, if we announce to all trackers, we expect to
// continue to visit all trackers in the tier, and announce to that additional
// IPv6 address as well
TORRENT_TEST(tracker_tiers_v4_and_v6_all_trackers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"ipv6-only-tracker.com", 0}, {"dual-tracker.com", 0}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 2);
TEST_EQUAL(a[5], 2);
});
}
TORRENT_TEST(tracker_tiers_v4_and_v6_different_tiers_all_trackers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, false);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"ipv6-only-tracker.com", 0}, {"dual-tracker.com", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 2);
TEST_EQUAL(a[5], 0);
});
}
TORRENT_TEST(tracker_tiers_v4_and_v6_different_tiers_all_tiers)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, false);
test_tracker_tiers(pack, { addr("50.0.0.1"), addr("f8e0::10") }
, { {"ipv6-only-tracker.com", 0}, {"dual-tracker.com", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 0);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 2);
TEST_EQUAL(a[5], 2);
});
}
TORRENT_TEST(tracker_tiers_retry_all)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
// the torrent is a hybrid torrent, so it will announce twice, once for v1
// and once for v2
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.5", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 0);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
// the failing tracker is retried 17 seconds later
TEST_EQUAL(a[6], 4);
}, std::function<void (int (&)[7])>([](int (&a)[7]) {
// this is 31 minutes later
// the working tracker is re-announced once, since interval is 1800
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 0);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
// The failing tracker is retried after:
// 72 seconds
// 189 seconds
// 396 seconds
// 711 seconds
// 1166 seconds
// 1783 seconds
// 6 * 2 = 12
TEST_EQUAL(a[6], 12);
}));
}
TORRENT_TEST(tracker_tiers_retry_all_multiple_trackers_per_tier)
{
settings_pack pack = settings();
pack.set_bool(settings_pack::announce_to_all_tiers, true);
pack.set_bool(settings_pack::announce_to_all_trackers, true);
// the torrent is a hybrid torrent, so it will announce twice, once for v1
// and once for v2
test_tracker_tiers(pack, { addr("50.0.0.1") }
, { {"3.0.0.1", 0}, {"3.0.0.5", 1}, {"3.0.0.2", 1}}
, [](int (&a)[7]) {
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
// the failing tracker is retried 17 seconds later
TEST_EQUAL(a[6], 4);
}, std::function<void (int (&)[7])>([](int (&a)[7]) {
// this is 31 minutes later
// the working tracker is re-announced once, since interval is 1800
TEST_EQUAL(a[0], 2);
TEST_EQUAL(a[1], 2);
TEST_EQUAL(a[2], 0);
TEST_EQUAL(a[3], 0);
TEST_EQUAL(a[4], 0);
TEST_EQUAL(a[5], 0);
// The failing tracker is retried after:
// 72 seconds
// 189 seconds
// 396 seconds
// 711 seconds
// 1166 seconds
// 1783 seconds
// 6 * 2 = 12
TEST_EQUAL(a[6], 12);
}));
}
// TODO: test external IP
// TODO: test with different queuing settings
// TODO: test when a torrent transitions from downloading to finished and
// finished to seeding
// TODO: test that left, downloaded and uploaded are reported correctly
// TODO: test scrape
|