2015-08-10 12:57:51 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Un Shyam
|
|
|
|
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 <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "libtorrent/pe_crypto.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "test.hpp"
|
2015-12-10 07:56:33 +01:00
|
|
|
#include "settings.hpp"
|
|
|
|
#include "setup_swarm.hpp"
|
2015-08-10 12:57:51 +02:00
|
|
|
|
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
namespace lt = libtorrent;
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
char const* pe_policy(std::uint8_t policy)
|
2015-08-10 12:57:51 +02:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
void display_pe_settings(libtorrent::settings_pack const& s)
|
2015-08-10 12:57:51 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "out_enc_policy - %s\tin_enc_policy - %s\n"
|
2015-08-10 12:57:51 +02:00
|
|
|
, pe_policy(s.get_int(settings_pack::out_enc_policy))
|
|
|
|
, pe_policy(s.get_int(settings_pack::in_enc_policy)));
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "enc_level - %s\t\tprefer_rc4 - %s\n"
|
2015-08-10 12:57:51 +02:00
|
|
|
, 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");
|
|
|
|
}
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
void test_transfer(int enc_policy, int level, bool prefer_rc4)
|
2015-08-10 12:57:51 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
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::add_torrent_params::flag_paused;
|
|
|
|
default_add_torrent.flags &= ~lt::add_torrent_params::flag_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);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
2015-12-10 07:56:33 +01:00
|
|
|
// add torrent
|
|
|
|
, [](lt::add_torrent_params& params) {}
|
|
|
|
// on alert
|
|
|
|
, [](lt::alert const* a, lt::session& ses) {}
|
|
|
|
// terminate
|
|
|
|
, [](int ticks, lt::session& ses) -> bool
|
|
|
|
{
|
|
|
|
if (ticks > 20)
|
|
|
|
{
|
|
|
|
TEST_ERROR("timeout");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return is_seed(ses);
|
|
|
|
});
|
|
|
|
}
|
2015-08-10 12:57:51 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(pe_disabled)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_disabled, settings_pack::pe_plaintext, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(forced_plaintext)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(forced_rc4)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_forced, settings_pack::pe_rc4, true);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(forced_both)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_forced, settings_pack::pe_both, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(forced_both_prefer_rc4)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_forced, settings_pack::pe_both, true);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(enabled_plaintext)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(enabled_rc4)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_enabled, settings_pack::pe_rc4, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(enabled_both)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
test_transfer(settings_pack::pe_enabled, settings_pack::pe_both, false);
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(enabled_both_prefer_rc4)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
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::add_torrent_params::flag_paused;
|
|
|
|
default_add_torrent.flags &= ~lt::add_torrent_params::flag_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& params) {}
|
|
|
|
// on alert
|
|
|
|
, [](lt::alert const* a, lt::session& ses) {}
|
|
|
|
// terminate
|
|
|
|
, [](int ticks, lt::session& ses) -> bool
|
|
|
|
{
|
|
|
|
// this download should never succeed
|
|
|
|
TEST_CHECK(!is_seed(ses));
|
|
|
|
return ticks > 120;
|
|
|
|
});
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
TORRENT_TEST(disabled)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PE test not run because it's disabled\n");
|
2015-08-10 12:57:51 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|