From 6d865f0c60502ed122141c56062d616206074a58 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 3 Feb 2020 01:15:17 +0100 Subject: [PATCH] extend tracker test to test announce_to_all_trackers and announce_to_all_tiers as well --- simulation/test_tracker.cpp | 97 ++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp index 7a773c809..b5fdd7615 100644 --- a/simulation/test_tracker.cpp +++ b/simulation/test_tracker.cpp @@ -1291,13 +1291,12 @@ TORRENT_TEST(tracker_user_agent_privacy_mode_private_torrent) // 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. -// #error parameterize this test over adding the trackers into different tiers -// and setting "announce_to_all_tiers" - -TORRENT_TEST(tracker_tiers) +template +void test_tracker_tiers(lt::settings_pack pack, TestFun test) { using namespace libtorrent; + pack.set_int(settings_pack::alert_mask, alert::error_notification | alert::torrent_log_notification); char const* peer0_ip = "50.0.0.1"; char const* peer1_ip = "50.0.0.2"; @@ -1313,32 +1312,31 @@ TORRENT_TEST(tracker_tiers) sim::asio::io_service tracker1(sim, address_v4::from_string("3.0.0.1")); sim::asio::io_service tracker2(sim, address_v4::from_string("3.0.0.2")); + sim::asio::io_service tracker3(sim, address_v4::from_string("3.0.0.3")); + sim::asio::io_service tracker4(sim, address_v4::from_string("3.0.0.4")); sim::http_server http1(tracker1, 8080); sim::http_server http2(tracker2, 8080); + sim::http_server http3(tracker3, 8080); + sim::http_server http4(tracker4, 8080); - bool received_announce[2] = {false, false}; - http1.register_handler("/announce" - , [&](std::string method, std::string req - , std::map&) + bool received_announce[4] = {false, false, false, false}; + + auto const return_no_peers = [&](std::string method, std::string req + , std::map&, int const tracker_index) { - received_announce[0] = true; + received_announce[tracker_index] = true; std::string const ret = "d8:intervali60e5:peers0:e"; return sim::send_response(200, "OK", static_cast(ret.size())) + ret; - }); + }; - http2.register_handler("/announce" - , [&](std::string method, std::string req - , std::map&) - { - received_announce[1] = true; - std::string const ret = "d8:intervali60e5:peers0:e"; - return sim::send_response(200, "OK", static_cast(ret.size())) + ret; - }); + 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)); lt::session_proxy zombie[2]; - // setup settings pack to use for the session (customization point) - lt::settings_pack pack = settings(); // create session std::shared_ptr ses[2]; pack.set_str(settings_pack::listen_interfaces, peer0_ip + std::string(":6881")); @@ -1367,6 +1365,8 @@ TORRENT_TEST(tracker_tiers) // random and stick to it, never announce to the other one. params.ti->add_tracker("http://3.0.0.1:8080/announce", 0); params.ti->add_tracker("http://3.0.0.2:8080/announce", 0); + params.ti->add_tracker("http://3.0.0.3:8080/announce", 1); + params.ti->add_tracker("http://3.0.0.4:8080/announce", 1); params.save_path = save_path(0); ses[0]->async_add_torrent(params); @@ -1374,9 +1374,9 @@ TORRENT_TEST(tracker_tiers) params.save_path = save_path(1); ses[1]->async_add_torrent(params); - sim::timer t(sim, lt::minutes(30), [&](boost::system::error_code const&) + sim::timer t(sim, lt::seconds(30), [&](boost::system::error_code const&) { - TEST_CHECK(received_announce[0] != received_announce[1]); + test(received_announce); TEST_CHECK(ses[0]->get_torrents()[0].status().is_seeding); TEST_CHECK(ses[1]->get_torrents()[0].status().is_seeding); @@ -1392,6 +1392,59 @@ TORRENT_TEST(tracker_tiers) sim.run(); } +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); + // setup settings pack to use for the session (customization point) + test_tracker_tiers(pack, [](bool (&received_announce)[4]) { + TEST_CHECK(received_announce[0] != received_announce[1]); + TEST_EQUAL(received_announce[2], false); + TEST_EQUAL(received_announce[3], false); + }); +} + +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); + // setup settings pack to use for the session (customization point) + test_tracker_tiers(pack, [](bool (&received_announce)[4]) { + TEST_EQUAL(received_announce[0], true); + TEST_EQUAL(received_announce[1], true); + TEST_EQUAL(received_announce[2], false); + TEST_EQUAL(received_announce[3], false); + }); +} + +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); + // setup settings pack to use for the session (customization point) + test_tracker_tiers(pack, [](bool (&received_announce)[4]) { + TEST_CHECK(received_announce[0] != received_announce[1]); + TEST_CHECK(received_announce[2] != received_announce[3]); + }); +} +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); + // setup settings pack to use for the session (customization point) + test_tracker_tiers(pack, [](bool (&received_announce)[4]) { + TEST_EQUAL(received_announce[0], true); + TEST_EQUAL(received_announce[1], true); + TEST_EQUAL(received_announce[2], true); + TEST_EQUAL(received_announce[3], true); + }); +} + + // TODO: test external IP // TODO: test with different queuing settings // TODO: test when a torrent transitions from downloading to finished and