/*

Copyright (c) 2007, Un Shyam
Copyright (c) 2015-2018, 2021, Arvid Norberg
Copyright (c) 2021, Alden Torres
All rights reserved.

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

#include <algorithm>
#include <iostream>

#include "libtorrent/aux_/pe_crypto.hpp"
#include "libtorrent/session.hpp"

#include "setup_transfer.hpp"
#include "test.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"

#if !defined TORRENT_DISABLE_ENCRYPTION

using namespace lt;

char const* pe_policy(int const policy)
{
	if (policy == settings_pack::pe_disabled) return "disabled";
	else if (policy == settings_pack::pe_enabled) return "enabled";
	else if (policy == settings_pack::pe_forced) return "forced";
	return "unknown";
}

void display_pe_settings(lt::settings_pack const& s)
{
	std::printf("out_enc_policy - %s\tin_enc_policy - %s\n"
		, pe_policy(s.get_int(settings_pack::out_enc_policy))
		, pe_policy(s.get_int(settings_pack::in_enc_policy)));

	std::printf("enc_level - %s\t\tprefer_rc4 - %s\n"
		, s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_plaintext ? "plaintext"
		: s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_rc4 ? "rc4"
		: s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_both ? "both" : "unknown"
		, s.get_bool(settings_pack::prefer_rc4) ? "true": "false");
}

void test_transfer(int enc_policy, int level, bool prefer_rc4)
{
	lt::settings_pack default_settings = settings();
	default_settings.set_bool(settings_pack::prefer_rc4, prefer_rc4);
	default_settings.set_int(settings_pack::in_enc_policy, enc_policy);
	default_settings.set_int(settings_pack::out_enc_policy, enc_policy);
	default_settings.set_int(settings_pack::allowed_enc_level, level);
	display_pe_settings(default_settings);

	sim::default_config cfg;
	sim::simulation sim{cfg};

	lt::add_torrent_params default_add_torrent;
	default_add_torrent.flags &= ~lt::torrent_flags::paused;
	default_add_torrent.flags &= ~lt::torrent_flags::auto_managed;
	setup_swarm(2, swarm_test::download, sim, default_settings, default_add_torrent
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_enabled);
			pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_enabled);
			pack.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
			pack.set_bool(settings_pack::prefer_rc4, false);
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int ticks, lt::session& ses) -> bool
		{
			if (ticks > 20)
			{
				TEST_ERROR("timeout");
				return true;
			}
			return is_seed(ses);
		});
}

TORRENT_TEST(pe_disabled)
{
	test_transfer(settings_pack::pe_disabled, settings_pack::pe_plaintext, false);
}

TORRENT_TEST(forced_plaintext)
{
	test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
}

TORRENT_TEST(forced_rc4)
{
	test_transfer(settings_pack::pe_forced, settings_pack::pe_rc4, true);
}

TORRENT_TEST(forced_both)
{
	test_transfer(settings_pack::pe_forced, settings_pack::pe_both, false);
}

TORRENT_TEST(forced_both_prefer_rc4)
{
	test_transfer(settings_pack::pe_forced, settings_pack::pe_both, true);
}

TORRENT_TEST(enabled_plaintext)
{
	test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
}

TORRENT_TEST(enabled_rc4)
{
	test_transfer(settings_pack::pe_enabled, settings_pack::pe_rc4, false);
}

TORRENT_TEST(enabled_both)
{
	test_transfer(settings_pack::pe_enabled, settings_pack::pe_both, false);
}

TORRENT_TEST(enabled_both_prefer_rc4)
{
	test_transfer(settings_pack::pe_enabled, settings_pack::pe_both, true);
}

// make sure that a peer with encryption disabled cannot talk to a peer with
// encryption forced
TORRENT_TEST(disabled_failing)
{
	lt::settings_pack default_settings = settings();
	default_settings.set_bool(settings_pack::prefer_rc4, false);
	default_settings.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
	default_settings.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
	default_settings.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
	display_pe_settings(default_settings);

	sim::default_config cfg;
	sim::simulation sim{cfg};

	lt::add_torrent_params default_add_torrent;
	default_add_torrent.flags &= ~lt::torrent_flags::paused;
	default_add_torrent.flags &= ~lt::torrent_flags::auto_managed;
	setup_swarm(2, swarm_test::download, sim, default_settings, default_add_torrent
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced);
			pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced);
			pack.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
			pack.set_bool(settings_pack::prefer_rc4, true);
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int ticks, lt::session& ses) -> bool
		{
			// this download should never succeed
			TEST_CHECK(!is_seed(ses));
			return ticks > 120;
		});
}
#else
TORRENT_TEST(disabled)
{
	std::printf("PE test not run because it's disabled\n");
}
#endif

