/*

Copyright (c) 2018, Steven Siloti
Copyright (c) 2013-2021, Arvid Norberg
Copyright (c) 2016-2018, Alden Torres
Copyright (c) 2018, d-komarov
All rights reserved.

You may use, distribute and modify this code under the terms of the BSD license,
see LICENSE file.
*/

#include "libtorrent/session.hpp"
#include "libtorrent/session_settings.hpp"
#include "libtorrent/session_params.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/aux_/path.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/read_resume_data.hpp"
#include "libtorrent/write_resume_data.hpp"
#include <tuple>
#include <functional>
#include <random>

#include "test.hpp"
#include "disk_io_test.hpp"
#include "test_utils.hpp"
#include "setup_transfer.hpp"
#include "settings.hpp"
#include <iostream>

using namespace lt;
using namespace std::chrono_literals;
using std::ignore;

namespace {

int peer_disconnects = 0;

bool on_alert(alert const* a)
{
	auto const* const pd = alert_cast<peer_disconnected_alert>(a);
	if (pd && pd->error != make_error_code(errors::self_connection)
		&& pd->error != make_error_code(errors::duplicate_peer_id))
		++peer_disconnects;
	else if (alert_cast<peer_error_alert>(a))
		++peer_disconnects;

	return false;
}

void cleanup()
{
	error_code ec;
	remove_all("tmp1_priority", ec);
	remove_all("tmp2_priority", ec);
	remove_all("tmp1_priority_moved", ec);
	remove_all("tmp2_priority_moved", ec);
}

void test_transfer(settings_pack const& sett,
	bool test_deprecated = false,
	lt::disk_io_constructor_type disk_io = lt::default_disk_io_constructor)
{
	// this allows shutting down the sessions in parallel
	std::vector<session_proxy> sp;

	cleanup();

	lt::settings_pack pack = sett;

	// we need a short reconnect time since we
	// finish the torrent and then restart it
	// immediately to complete the second half.
	// using a reconnect time > 0 will just add
	// to the time it will take to complete the test
	pack.set_int(settings_pack::min_reconnect_time, 0);

	pack.set_bool(settings_pack::enable_outgoing_utp, false);
	pack.set_bool(settings_pack::enable_incoming_utp, false);

	pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
	pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
	pack.set_bool(settings_pack::allow_multiple_connections_per_ip, false);
	pack.set_int(settings_pack::unchoke_slots_limit, 8);

	pack.set_bool(settings_pack::enable_upnp, false);
	pack.set_bool(settings_pack::enable_natpmp, false);
	pack.set_bool(settings_pack::enable_lsd, false);
	pack.set_bool(settings_pack::enable_dht, false);

	pack.set_str(settings_pack::listen_interfaces, test_listen_interface());
#if TORRENT_ABI_VERSION == 1
	pack.set_bool(settings_pack::rate_limit_utp, true);
#endif

	lt::session_params sp1(pack);
	sp1.disk_io_constructor = disk_io;
	lt::session ses1(sp1);

	pack.set_str(settings_pack::listen_interfaces, test_listen_interface());
	lt::session_params sp2(pack);
	sp2.disk_io_constructor = disk_io;
	lt::session ses2(sp2);

	torrent_handle tor1;
	torrent_handle tor2;

	error_code ec;
	create_directory("tmp1_priority", ec);
	ofstream file("tmp1_priority/temporary");
	add_torrent_params atp = ::create_torrent(&file, "temporary", 16 * 1024, 13, false);
	file.close();

	wait_for_listen(ses1, "ses1");
	wait_for_listen(ses2, "ses2");

	peer_disconnects = 0;

	// test using piece sizes smaller than 16kB
	std::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, nullptr
		, true, false, true, "_priority", 8 * 1024, &atp, false);

	int const num_pieces = tor2.torrent_file()->num_pieces();
	aux::vector<download_priority_t, piece_index_t> priorities(std::size_t(num_pieces), 1_pri);
	// set half of the pieces to priority 0
	std::fill(priorities.begin(), priorities.begin() + (num_pieces / 2), 0_pri);
	tor2.prioritize_pieces(priorities);
	std::cout << "setting priorities: ";
	for (auto p : priorities) std::cout << int(static_cast<std::uint8_t>(p)) << " ";
	std::cout << '\n';

	auto const start_time = lt::clock_type::now();
	for (int i = 0; i < 200; ++i)
	{
		print_alerts(ses1, "ses1", true, true, &on_alert);
		print_alerts(ses2, "ses2", true, true, &on_alert);

		torrent_status st1 = tor1.status();
		torrent_status st2 = tor2.status();

		if (i % 10 == 0)
		{
			print_ses_rate(start_time, &st1, &st2);
		}

		// st2 is finished when we have downloaded half of the pieces
		if (st2.is_finished) break;

		if (st2.state != torrent_status::downloading)
		{
			static char const* state_str[] =
				{"checking (q)", "checking", "dl metadata"
				, "downloading", "finished", "seeding", "allocating", "checking (r)"};
			std::cout << "st2 state: " << state_str[st2.state] << std::endl;
		}

		TEST_CHECK(st1.state == torrent_status::seeding
			|| st1.state == torrent_status::checking_resume_data
			|| st1.state == torrent_status::checking_files);
		TEST_CHECK(st2.state == torrent_status::downloading
			|| st2.state == torrent_status::checking_resume_data);

		if (peer_disconnects >= 2)
		{
			std::printf("too many disconnects (%d), exiting\n", peer_disconnects);
			break;
		}

		// if nothing is being transferred after 3 seconds, we're failing the test
		if (st1.upload_payload_rate == 0 && i > 30)
		{
			std::cout << "no upload in " << (i / 10) << " seconds, failing\n";
			break;
		}

		std::this_thread::sleep_for(lt::milliseconds(100));
	}

	TEST_CHECK(!tor2.status().is_seeding);
	TEST_CHECK(tor2.status().is_finished);

	if (tor2.status().is_finished)
		std::cout << "torrent is finished (50% complete)" << std::endl;
	else return;

	std::vector<download_priority_t> priorities2 = tor2.get_piece_priorities();
	std::copy(priorities2.begin(), priorities2.end()
		, std::ostream_iterator<download_priority_t>(std::cout, ", "));
	std::cout << std::endl;
	TEST_CHECK(std::equal(priorities.begin(), priorities.end(), priorities2.begin()));

	std::cout << "force recheck" << std::endl;
	tor2.force_recheck();

	priorities2 = tor2.get_piece_priorities();
	std::copy(priorities2.begin(), priorities2.end()
		, std::ostream_iterator<download_priority_t>(std::cout, ", "));
	std::cout << std::endl;
	TEST_CHECK(std::equal(priorities.begin(), priorities.end(), priorities2.begin()));

	peer_disconnects = 0;

	// when we're done checking, we're likely to be put in downloading state
	// for a split second before transitioning to finished. This loop waits
	// for the finished state
	torrent_status st2;
	for (int i = 0; i < 50; ++i)
	{
		print_alerts(ses1, "ses1", true, true, &on_alert);
		print_alerts(ses2, "ses2", true, true, &on_alert);

		st2 = tor2.status();
		if (i % 10 == 0)
		{
			std::cout << int(st2.progress * 100) << "% " << std::endl;
		}
		if (st2.state == torrent_status::finished) break;
		std::this_thread::sleep_for(lt::milliseconds(100));
	}

	TEST_EQUAL(st2.state, torrent_status::finished);

	if (st2.state != torrent_status::finished)
		return;

	std::cout << "recheck complete" << std::endl;

	priorities2 = tor2.get_piece_priorities();
	std::copy(priorities2.begin(), priorities2.end(), std::ostream_iterator<download_priority_t>(std::cout, ", "));
	std::cout << std::endl;
	TEST_CHECK(std::equal(priorities.begin(), priorities.end(), priorities2.begin()));

	tor2.pause();
	wait_for_alert(ses2, torrent_paused_alert::alert_type, "ses2");

	std::printf("save resume data\n");
	tor2.save_resume_data();

	std::vector<char> resume_data;

	time_point start = clock_type::now();
	while (true)
	{
		ses2.wait_for_alert(seconds(10));
		std::vector<alert*> alerts;
		ses2.pop_alerts(&alerts);
		if (alerts.empty()) break;
		for (std::vector<alert*>::iterator i = alerts.begin()
			, end(alerts.end()); i != end; ++i)
		{
			alert* a = *i;
			std::cout << "ses2: " << a->message() << std::endl;
			if (alert_cast<save_resume_data_alert>(a))
			{
				resume_data = write_resume_data_buf(alert_cast<save_resume_data_alert>(a)->params);
				std::printf("saved resume data\n");
				goto done;
			}
			else if (alert_cast<save_resume_data_failed_alert>(a))
			{
				std::printf("save resume failed\n");
				goto done;
			}
			if (total_seconds(clock_type::now() - start) > 10)
				goto done;
		}
	}
done:
	TEST_CHECK(resume_data.size());

	if (resume_data.empty())
		return;

	std::printf("%s\n", &resume_data[0]);

	ses2.remove_torrent(tor2);

	std::cout << "removed" << std::endl;

	wait_for_alert(ses2, torrent_removed_alert::alert_type, "ses2");

	std::cout << "re-adding" << std::endl;
	add_torrent_params p;
	TORRENT_UNUSED(test_deprecated);
#if TORRENT_ABI_VERSION == 1
	if (test_deprecated)
	{
		p.resume_data = resume_data;
	}
	else
#endif
	{
		error_code ec1;
		p = read_resume_data(resume_data, ec1);
		TEST_CHECK(!ec1);
	}
	p.flags &= ~torrent_flags::paused;
	p.flags &= ~torrent_flags::auto_managed;
	p.ti = atp.ti;
	p.save_path = "tmp2_priority";

	tor2 = ses2.add_torrent(p, ec);
	tor2.prioritize_pieces(priorities);
	std::cout << "resetting priorities" << std::endl;
	tor2.resume();

	// wait for torrent 2 to settle in back to finished state (it will
	// start as checking)
	torrent_status st1;
	for (int i = 0; i < 5; ++i)
	{
		print_alerts(ses1, "ses1", true, true, &on_alert);
		print_alerts(ses2, "ses2", true, true, &on_alert);

		st1 = tor1.status();
		st2 = tor2.status();

		TEST_CHECK(st1.state == torrent_status::seeding);

		if (st2.is_finished) break;

		std::this_thread::sleep_for(lt::milliseconds(100));
	}

	// torrent 2 should not be seeding yet, it should
	// just be 50% finished
	TEST_CHECK(!st2.is_seeding);
	TEST_CHECK(st2.is_finished);

	std::fill(priorities.begin(), priorities.end(), 1_pri);
	tor2.prioritize_pieces(priorities);
	std::cout << "setting priorities to 1" << std::endl;
	TEST_EQUAL(tor2.status().is_finished, false);

	std::copy(priorities.begin(), priorities.end()
		, std::ostream_iterator<download_priority_t>(std::cout, ", "));
	std::cout << std::endl;

	// drain alerts. The redundant seed connection may still be in the
	// process of being disconnected from when the torrent was briefly
	// finished (just before the priority bump above took effect), and its
	// peer_disconnected_alert (one from each side) can arrive with some
	// delay. Wait for both to show up so that disconnect is accounted for
	// here, rather than leaking into the reconnect loop below and being
	// mistaken for a reconnect failure.
	for (int i = 0; i < 10 && peer_disconnects < 2; ++i)
	{
		ses1.wait_for_alert(100ms);
		ses2.wait_for_alert(100ms);
		print_alerts(ses1, "ses1", true, true, &on_alert);
		print_alerts(ses2, "ses2", true, true, &on_alert);
	}

	peer_disconnects = 0;

	// this loop makes sure ses2 reconnects to the peer now that it's
	// in download mode again. If this fails, the reconnect logic may
	// not work or be inefficient
	st1 = tor1.status();
	st2 = tor2.status();
	for (int i = 0; i < 130; ++i)
	{
		print_alerts(ses1, "ses1", true, true, &on_alert);
		print_alerts(ses2, "ses2", true, true, &on_alert);

		st1 = tor1.status();
		st2 = tor2.status();

		if (i % 10 == 0)
			print_ses_rate(start_time, &st1, &st2);

		if (st2.is_seeding) break;

		TEST_EQUAL(st1.state, torrent_status::seeding);
		TEST_EQUAL(st2.state, torrent_status::downloading);

		if (peer_disconnects >= 2)
		{
			std::printf("too many disconnects (%d), exiting\n", peer_disconnects);
			break;
		}

		std::this_thread::sleep_for(lt::milliseconds(100));
	}

	st2 = tor2.status();
	if (!st2.is_seeding)
		std::printf("ses2 failed to reconnect to ses1!\n");
	TEST_CHECK(st2.is_seeding);

	sp.push_back(ses1.abort());
	sp.push_back(ses2.abort());
}

} // anonymous namespace

TORRENT_TEST_DISK_IO(priority)
{
	using namespace lt;
	settings_pack p = settings();
	test_transfer(p, false, disk_io);
	cleanup();
}

#if TORRENT_ABI_VERSION == 1
TORRENT_TEST_DISK_IO(priority_deprecated)
{
	using namespace lt;
	settings_pack p = settings();
	test_transfer(p, true, disk_io);
	cleanup();
}
#endif

// test to set piece and file priority on a torrent that doesn't have metadata
// yet
TORRENT_TEST(no_metadata_prioritize_files)
{
	lt::session ses(settings());

	add_torrent_params addp;
	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.info_hashes.v1 = sha1_hash("abababababababababab");
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	std::vector<lt::download_priority_t> prios(3);
	prios[0] = lt::dont_download;

	h.prioritize_files(prios);
	for (int i = 0; i < 50 && h.get_file_priorities() != prios; ++i)
		std::this_thread::sleep_for(100ms);
	TEST_CHECK(h.get_file_priorities() == prios);

	prios[0] = lt::low_priority;
	h.prioritize_files(prios);
	for (int i = 0; i < 50 && h.get_file_priorities() != prios; ++i)
		std::this_thread::sleep_for(100ms);
	TEST_CHECK(h.get_file_priorities() == prios);

	ses.remove_torrent(h);
}

TORRENT_TEST(no_metadata_file_prio)
{
	lt::session ses(settings());

	add_torrent_params addp;
	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.info_hashes.v1 = sha1_hash("abababababababababab");
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	h.file_priority(0_file, 0_pri);
	for (int i = 0; i < 50 && h.file_priority(0_file) != 0_pri; ++i)
		std::this_thread::sleep_for(100ms);
	TEST_EQUAL(h.file_priority(0_file), 0_pri);
	h.file_priority(0_file, 1_pri);
	for (int i = 0; i < 50 && h.file_priority(0_file) != 1_pri; ++i)
		std::this_thread::sleep_for(100ms);
	TEST_EQUAL(h.file_priority(0_file), 1_pri);

	ses.remove_torrent(h);
}

TORRENT_TEST(no_metadata_piece_prio)
{
	lt::session ses(settings());

	add_torrent_params addp;
	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.info_hashes.v1 = sha1_hash("abababababababababab");
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	// you can't set piece priorities before the metadata has been downloaded
	h.piece_priority(2_piece, 0_pri);
	TEST_EQUAL(h.piece_priority(2_piece), 4_pri);
	h.piece_priority(2_piece, 1_pri);
	TEST_EQUAL(h.piece_priority(2_piece), 4_pri);

	ses.remove_torrent(h);
}

TORRENT_TEST(file_priority_multiple_calls)
{
	settings_pack pack = settings();
	lt::session ses(pack);

	add_torrent_params addp = ::generate_torrent(true);

	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	for (file_index_t const i : addp.ti->layout().file_range())
		h.file_priority(i, lt::low_priority);

	std::vector<download_priority_t> const expected(
		std::size_t(addp.ti->num_files()), lt::low_priority);
	// bounded by a 5s ceiling, polled in short slices.
	for (int i = 0; i < 50; ++i)
	{
		auto const p = h.get_file_priorities();
		if (p == expected) return;
		std::this_thread::sleep_for(100ms);
	}
	TEST_CHECK(false);
}

TORRENT_TEST(post_file_priorities)
{
	settings_pack pack = settings();
	lt::session ses(pack);

	add_torrent_params addp = ::generate_torrent(true);

	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	std::vector<download_priority_t> const expected(
		std::size_t(addp.ti->num_files()), lt::low_priority);

	// set all priorities in one call to ensure a single disk round-trip and
	// one file_prio_alert
	h.prioritize_files(expected);

	// wait for priorities to be applied, then request them asynchronously
	wait_for_alert(ses, file_prio_alert::alert_type, "ses");
	h.post_file_priorities();

	alert const* a = wait_for_alert(ses, file_priorities_alert::alert_type, "ses");
	auto const* fpa = alert_cast<file_priorities_alert>(a);
	TORRENT_ASSERT(fpa != nullptr);

	TEST_CHECK(fpa->priorities == expected);
}

TORRENT_TEST(post_file_status)
{
	settings_pack pack = settings();
	lt::session ses(pack);

	error_code ec;
	create_directory("tmp_post_file_status", ec);
	ofstream file("tmp_post_file_status/temporary");
	add_torrent_params addp = ::create_torrent(&file, "temporary", 16 * 1024, 13, false);
	file.close();

	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	// wait for the torrent to be ready before requesting file status
	wait_for_alert(ses, torrent_finished_alert::alert_type, "ses");

	h.post_file_status();

	alert const* a = wait_for_alert(ses, file_status_alert::alert_type, "ses");
	auto const* fsa = alert_cast<file_status_alert>(a);
	TORRENT_ASSERT(fsa != nullptr);

	// the torrent has one file; entries in state (if any) must refer to it
	for (auto const& s : fsa->state)
		TEST_CHECK(s.file_index == file_index_t{0});
}

TORRENT_TEST(export_file_while_seed)
{
	settings_pack pack = settings();
	lt::session ses(pack);

	error_code ec;
	create_directory("tmp2_priority", ec);
	ofstream file("tmp2_priority/temporary");
	add_torrent_params addp = ::create_torrent(&file, "temporary", 16 * 1024, 13, false);
	file.close();

	addp.flags &= ~torrent_flags::paused;
	addp.flags &= ~torrent_flags::auto_managed;
	addp.save_path = ".";
	torrent_handle h = ses.add_torrent(addp);

	// write to the partfile
	h.file_priority(file_index_t{0}, lt::dont_download);

	std::vector<char> piece(16 * 1024);
	for (std::size_t i = 0; i < piece.size(); ++i)
		piece[i] = char((i % 26) + 'A');

	// prevent race conditions of adding pieces while checking
	lt::torrent_status st = h.status();
	for (int i = 0; i < 40; ++i)
	{
		print_alerts(ses, "ses", true, true);
		st = h.status();
		if (st.state != torrent_status::checking_files
			&& st.state != torrent_status::checking_resume_data)
			break;
		std::this_thread::sleep_for(lt::milliseconds(100));
	}
	TEST_CHECK(st.state != torrent_status::checking_files
		&& st.state != torrent_status::checking_files);
	TEST_CHECK(st.num_pieces == 0);

	for (piece_index_t i : addp.ti->piece_range())
		h.add_piece(i, piece.data());

	TEST_CHECK(!exists("temporary"));

	for (int i = 0; i < 40; ++i)
	{
		print_alerts(ses, "ses", true, true, &on_alert);
		if (h.status().is_seeding) break;
		std::this_thread::sleep_for(lt::milliseconds(100));
	}
	TEST_EQUAL(h.status().is_seeding, true);

	// this should cause the file to be exported
	h.file_priority(file_index_t{0}, lt::low_priority);

	for (int i = 0; i < 20; ++i)
	{
		print_alerts(ses, "ses", true, true, &on_alert);
		if (h.file_priority(file_index_t{0}) == lt::low_priority) break;
		std::this_thread::sleep_for(lt::milliseconds(100));
	}

	TEST_CHECK(exists("temporary"));
}

TORRENT_TEST(test_piece_priority_after_resume)
{
	auto const new_prio = lt::low_priority;

	add_torrent_params p = generate_torrent();
	auto ti = p.ti;
	{
		auto const prio = top_priority;

		p.save_path = ".";
		p.file_priorities.resize(1, prio);

		lt::session ses(settings());
		torrent_handle h = ses.add_torrent(p);

		TEST_EQUAL(h.piece_priority(0_piece), prio);

		using prio_vec = std::vector<std::pair<lt::piece_index_t, lt::download_priority_t>>;
		h.prioritize_pieces(prio_vec{{0_piece, new_prio}});
		TEST_EQUAL(h.piece_priority(0_piece), new_prio);

		ses.pause();
		h.save_resume_data();

		alert const* a = wait_for_alert(ses, save_resume_data_alert::alert_type);
		save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(a);

		p = rd->params;
	}
	{
		p.save_path = ".";
		p.ti = ti;

		lt::session ses(settings());
		torrent_handle h = ses.add_torrent(p);

		TEST_EQUAL(h.piece_priority(0_piece), new_prio);
	}
}

namespace {
template <typename Engine>
lt::file_index_t rand_file(Engine& rng, int num_files)
{
	auto i = std::uniform_int_distribution<int>(0, num_files - 1)(rng);
	return file_index_t(i);
}

template <typename Engine>
lt::download_priority_t rand_prio(Engine& rng)
{
	auto i = std::uniform_int_distribution<int>(0, 7)(rng);
	return lt::download_priority_t(static_cast<std::uint8_t>(i));
}
}

TORRENT_TEST(file_priority_stress_test)
{
	add_torrent_params atp = generate_torrent();
	int const num_files = atp.ti->num_files();

	lt::aux::vector<lt::download_priority_t, lt::file_index_t>
		local_prios(static_cast<std::size_t>(num_files), lt::default_priority);

	atp.save_path = ".";
	atp.file_priorities = local_prios;
	atp.flags &= ~torrent_flags::need_save_resume;

	lt::session ses(settings());
	torrent_handle h = ses.add_torrent(atp);
#if TORRENT_ABI_VERSION < 4
	TEST_CHECK(h.status().need_save_resume == false);
#endif
	TEST_CHECK(!(h.status().need_save_resume_data & torrent_handle::if_metadata_changed));

	std::mt19937 rng(0x82daf973);
	bool first = true;
	for (int i = 0; i < 1000; ++i)
	{
		auto const f = rand_file(rng, num_files);
		auto const p = rand_prio(rng);
		h.file_priority(f, p);
		local_prios[f] = p;

		// updating the file priorities is asynchronous, it shouldn't have taken
		// effect quite yet
		if (first)
		{
			auto const st = h.status();
#if TORRENT_ABI_VERSION < 4
			TEST_CHECK(st.need_save_resume == false);
#endif
			TEST_CHECK(!(st.need_save_resume_data & torrent_handle::if_config_changed));
			TEST_CHECK(!(st.need_save_resume_data & torrent_handle::if_counters_changed));
		}
		first = false;
	}

	auto tp = h.get_file_priorities();
	for (int i = 0; i < 10 && tp != local_prios; ++i)
	{
		std::this_thread::sleep_for(lt::milliseconds(500));
		tp = h.get_file_priorities();
	}

	std::cout << "torrent file prios:\n";
	for (auto const p : tp)
		std::cout << " " << p;
	std::cout << '\n';

	std::cout << "expected file prios:\n";
	for (auto const p : local_prios)
		std::cout << " " << p;
	std::cout << '\n';

	TEST_CHECK(tp == local_prios);
	auto const st = h.status();
#if TORRENT_ABI_VERSION < 4
	TEST_CHECK(st.need_save_resume == true);
#endif
	// the if_counters_changed may also be set here, depending on whether
	// second_tick() fired or not
	TEST_CHECK(st.need_save_resume_data & torrent_handle::if_config_changed);

	auto const pp = h.get_piece_priorities();
	auto const& fs = atp.ti->layout();
	lt::piece_index_t i(0);

	std::cout << "piece prios:\n";
	for (auto p : pp)
	{
		auto const files = fs.map_block(i, 0, fs.piece_length());
		auto expected_prio = lt::dont_download;
		for (auto const& f: files)
			expected_prio = std::max(local_prios[f.file_index], expected_prio);

		std::cout << " " << p;
		TEST_EQUAL(p, expected_prio);
		++i;
	}
	std::cout << '\n';
}
