transition the test_pe_crypto system tests to be simulation

This commit is contained in:
arvidn 2015-08-10 06:57:51 -04:00
parent e557e63d05
commit a3dbbd6a37
9 changed files with 348 additions and 232 deletions

View File

@ -6,6 +6,7 @@ use-project /libtorrent_test : ../test ;
use-project /libsimulator : libsimulator ;
# TODO: create another library with the libtorrent simulator utilities
project
: requirements
<simulator>on
@ -25,5 +26,6 @@ alias libtorrent-sims :
[ run test_swarm.cpp ]
[ run test_utp.cpp ]
[ run test_dht.cpp ]
[ run test_pe_crypto.cpp ]
;

@ -1 +1 @@
Subproject commit 83b77075c336c8e36e936453bf3d0e49d763114f
Subproject commit 48c4dd76d728d153ea183dc1cfeeff4ac0f8d7d7

View File

@ -176,7 +176,8 @@ private:
network_setup_provider& m_config;
sim::simulation m_sim;
sim::default_config cfg;
sim::simulation m_sim{cfg};
asio::io_service m_ios;
lt::time_point m_start_time;

View File

@ -187,7 +187,8 @@ private:
swarm_setup_provider& m_config;
sim::simulation m_sim;
sim::default_config cfg;
sim::simulation m_sim{cfg};
asio::io_service m_ios;
lt::time_point m_start_time;

View File

@ -34,6 +34,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/settings_pack.hpp"
#include "libtorrent/add_torrent_params.hpp"
#ifndef TORRENT_SWARM_SETUP_PROVIDER_HPP_INCLUDED
#define TORRENT_SWARM_SETUP_PROVIDER_HPP_INCLUDED
struct swarm_setup_provider
{
// can be used to check expected end conditions
@ -55,3 +58,5 @@ struct swarm_setup_provider
void setup_swarm(int num_nodes, swarm_setup_provider& config);
#endif

139
simulation/swarm_config.hpp Normal file
View File

@ -0,0 +1,139 @@
/*
Copyright (c) 2015, 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.
*/
#ifndef TORRENT_SWARM_CONFIG_HPP_INCLUDED
#define TORRENT_SWARM_CONFIG_HPP_INCLUDED
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/add_torrent_params.hpp"
#include "libtorrent/time.hpp"
#include "setup_swarm.hpp"
#include "settings.hpp"
#include <string>
#include <fstream>
using namespace libtorrent;
namespace lt = libtorrent;
struct swarm_config : swarm_setup_provider
{
swarm_config()
: m_start_time(lt::clock_type::now())
{
m_swarm_id = test_counter();
error_code ec;
std::string path = save_path(0);
create_directory(path, ec);
if (ec) fprintf(stderr, "failed to create directory: \"%s\": %s\n"
, path.c_str(), ec.message().c_str());
std::ofstream file(combine_path(path, "temporary").c_str());
m_ti = ::create_torrent(&file, 0x4000, 9, false);
file.close();
}
virtual void on_exit(std::vector<torrent_handle> const& torrents)
{
TEST_CHECK(torrents.size() > 0);
for (int i = 0; i < int(torrents.size()); ++i)
{
torrent_status st = torrents[i].status();
TEST_CHECK(st.is_seeding);
TEST_CHECK(st.total_upload > 0 || st.total_download > 0);
}
}
// called for every alert. if the simulation is done, return true
virtual bool on_alert(libtorrent::alert const* alert
, int session_idx
, std::vector<libtorrent::torrent_handle> const& torrents)
{
if (torrents.empty()) return false;
bool all_are_seeding = true;
for (int i = 0; i < int(torrents.size()); ++i)
{
if (torrents[i].status().is_seeding)
continue;
all_are_seeding = false;
break;
}
// if all torrents are seeds, terminate the simulation, we're done
return all_are_seeding;
}
// called for every torrent that's added (and every session that's started).
// this is useful to give every session a unique save path and to make some
// sessions seeds and others downloaders
virtual libtorrent::add_torrent_params add_torrent(int idx)
{
add_torrent_params p;
p.flags &= ~add_torrent_params::flag_paused;
p.flags &= ~add_torrent_params::flag_auto_managed;
p.ti = m_ti;
p.save_path = save_path(idx);
return p;
}
std::string save_path(int idx) const
{
char path[200];
snprintf(path, sizeof(path), "swarm-%04d-peer-%02d"
, m_swarm_id, idx);
return path;
}
// called for every session that's added
virtual libtorrent::settings_pack add_session(int idx)
{
settings_pack pack = settings();
// make sure the sessions have different peer ids
lt::peer_id pid;
std::generate(&pid[0], &pid[0] + 20, &random_byte);
pack.set_str(lt::settings_pack::peer_fingerprint, pid.to_string());
return pack;
}
protected:
int m_swarm_id;
lt::time_point m_start_time;
boost::shared_ptr<libtorrent::torrent_info> m_ti;
};
#endif // TORRENT_SWARM_CONFIG_HPP_INCLUDED

View File

@ -1,6 +1,6 @@
/*
Copyright (c) 2014, Arvid Norberg
Copyright (c) 2015, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -35,82 +35,36 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alert_types.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/time.hpp" // for clock_type
#include <fstream>
#include "test.hpp"
#include "setup_transfer.hpp" // for create_torrent (factor this out!)
#include "setup_swarm.hpp"
#include "swarm_suite.hpp"
#include "settings.hpp"
#include "swarm_config.hpp"
using namespace libtorrent;
namespace lt = libtorrent;
struct swarm_config : swarm_setup_provider
struct test_swarm_config : swarm_config
{
swarm_config(int flags)
: m_flags(flags)
, m_start_time(lt::clock_type::now())
{
m_swarm_id = test_counter();
test_swarm_config(int flags)
: swarm_config()
, m_flags(flags)
{}
// in case the previous run was terminated
error_code ec;
char save_path[200];
snprintf(save_path, sizeof(save_path), "swarm-%04d-peer-%02d"
, m_swarm_id, 0);
create_directory(save_path, ec);
if (ec) fprintf(stderr, "failed to create directory: \"%s\": %s\n"
, save_path, ec.message().c_str());
std::ofstream file(combine_path(save_path, "temporary").c_str());
m_ti = ::create_torrent(&file, 0x4000, 9, false);
file.close();
}
virtual void on_exit(std::vector<torrent_handle> const& torrents)
virtual void on_exit(std::vector<torrent_handle> const& torrents) override
{
TEST_CHECK(torrents.size() > 0);
for (int i = 0; i < int(torrents.size()); ++i)
{
torrent_status st = torrents[i].status();
TEST_CHECK(st.is_seeding);
TEST_CHECK(st.total_upload > 0 || st.total_download > 0);
}
swarm_config::on_exit(torrents);
TEST_CHECK(lt::clock_type::now() < m_start_time + lt::milliseconds(2100));
}
// called for every alert. if the simulation is done, return true
virtual bool on_alert(libtorrent::alert const* alert
, int session_idx
, std::vector<libtorrent::torrent_handle> const& torrents)
{
if (torrents.empty()) return false;
bool all_are_seeding = true;
for (int i = 0; i < int(torrents.size()); ++i)
{
if (torrents[i].status().is_seeding)
continue;
all_are_seeding = false;
break;
}
// if all torrents are seeds, terminate the simulation, we're done
return all_are_seeding;
}
// called for every torrent that's added (and every session that's started).
// this is useful to give every session a unique save path and to make some
// sessions seeds and others downloaders
virtual libtorrent::add_torrent_params add_torrent(int idx)
virtual libtorrent::add_torrent_params add_torrent(int idx) override
{
add_torrent_params p;
p.flags &= ~add_torrent_params::flag_paused;
p.flags &= ~add_torrent_params::flag_auto_managed;
p.ti = m_ti;
add_torrent_params p = swarm_config::add_torrent(idx);
// only the first session is set to seed mode
if (idx == 0)
@ -118,17 +72,13 @@ struct swarm_config : swarm_setup_provider
if (m_flags & seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
}
char save_path[200];
snprintf(save_path, sizeof(save_path), "swarm-%04d-peer-%02d"
, m_swarm_id, idx);
p.save_path = save_path;
return p;
}
// called for every session that's added
virtual libtorrent::settings_pack add_session(int idx)
virtual libtorrent::settings_pack add_session(int idx) override
{
settings_pack pack = settings();
settings_pack pack = swarm_config::add_session(idx);
pack.set_bool(settings_pack::strict_super_seeding, m_flags & strict_super_seeding);
@ -162,19 +112,11 @@ struct swarm_config : swarm_setup_provider
pack.set_bool(settings_pack::enable_outgoing_tcp, true);
}
// make sure the sessions have different peer ids
lt::peer_id pid;
std::generate(&pid[0], &pid[0] + 20, &random_byte);
pack.set_str(lt::settings_pack::peer_fingerprint, pid.to_string());
return pack;
}
private:
int m_flags;
int m_swarm_id;
lt::time_point m_start_time;
boost::shared_ptr<libtorrent::torrent_info> m_ti;
};
void simulate_swarm(int flags)
@ -189,7 +131,7 @@ void simulate_swarm(int flags)
, (flags & utp_only) ? "utp-only": ""
);
swarm_config cfg(flags);
test_swarm_config cfg(flags);
setup_swarm(2, cfg);
}

View File

@ -0,0 +1,183 @@
/*
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/hasher.hpp"
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/random.hpp"
#include "setup_transfer.hpp"
#include "swarm_config.hpp"
#include "test.hpp"
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
using namespace libtorrent;
namespace lt = libtorrent;
char const* pe_policy(boost::uint8_t policy)
{
using namespace libtorrent;
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_settings(libtorrent::settings_pack const& s)
{
using namespace libtorrent;
fprintf(stderr, "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)));
fprintf(stderr, "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");
}
struct test_swarm_config : swarm_config
{
test_swarm_config(libtorrent::settings_pack::enc_policy m_policy
, libtorrent::settings_pack::enc_level level
, bool prefer_rc4)
: swarm_config()
, m_level(level)
, m_prefer_rc4(prefer_rc4)
{}
// called for every session that's added
virtual libtorrent::settings_pack add_session(int idx) override
{
settings_pack s = swarm_config::add_session(idx);
fprintf(stderr, " session %d\n", idx);
s.set_int(settings_pack::out_enc_policy, settings_pack::pe_enabled);
s.set_int(settings_pack::in_enc_policy, settings_pack::pe_enabled);
s.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
if (idx == 1)
{
s.set_int(settings_pack::out_enc_policy, m_policy);
s.set_int(settings_pack::in_enc_policy, m_policy);
s.set_int(settings_pack::allowed_enc_level, m_level);
s.set_bool(settings_pack::prefer_rc4, m_prefer_rc4);
}
display_settings(s);
return s;
}
private:
libtorrent::settings_pack::enc_policy m_policy;
libtorrent::settings_pack::enc_level m_level;
bool m_prefer_rc4;
};
TORRENT_TEST(pe_disabled)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_disabled, settings_pack::pe_both, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(forced_plaintext)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_forced, settings_pack::pe_both, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(forced_rc4)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_forced, settings_pack::pe_rc4, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(forced_both)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_forced, settings_pack::pe_both, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(forced_both_prefer_rc4)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_forced, settings_pack::pe_both, true);
setup_swarm(2, cfg);
}
TORRENT_TEST(enabled_plaintext)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_enabled, settings_pack::pe_plaintext, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(enabled_rc4)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_enabled, settings_pack::pe_rc4, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(enabled_both)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_enabled, settings_pack::pe_both, false);
setup_swarm(2, cfg);
}
TORRENT_TEST(enabled_both_prefer_rc4)
{
using namespace libtorrent;
test_swarm_config cfg(settings_pack::pe_enabled, settings_pack::pe_both, true);
setup_swarm(2, cfg);
}
#else
TORRENT_TEST(disabled)
{
fprintf(stderr, "PE test not run because it's disabled\n");
}
#endif

View File

@ -43,104 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
char const* pe_policy(boost::uint8_t policy)
{
using namespace libtorrent;
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_settings(libtorrent::settings_pack const& s)
{
using namespace libtorrent;
fprintf(stderr, "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)));
fprintf(stderr, "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(libtorrent::settings_pack::enc_policy policy
, int timeout
, libtorrent::settings_pack::enc_level level = libtorrent::settings_pack::pe_both
, bool pref_rc4 = false)
{
using namespace libtorrent;
namespace lt = libtorrent;
// these are declared before the session objects
// so that they are destructed last. This enables
// the sessions to destruct in parallel
session_proxy p1;
session_proxy p2;
settings_pack s;
s.set_int(settings_pack::out_enc_policy, settings_pack::pe_enabled);
s.set_int(settings_pack::in_enc_policy, settings_pack::pe_enabled);
s.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
s.set_str(settings_pack::listen_interfaces, "0.0.0.0:48800");
lt::session ses2(s, 0);
fprintf(stderr, " Session2 \n");
display_settings(s);
s.set_int(settings_pack::out_enc_policy, policy);
s.set_int(settings_pack::in_enc_policy, policy);
s.set_int(settings_pack::allowed_enc_level, level);
s.set_bool(settings_pack::prefer_rc4, pref_rc4);
s.set_str(settings_pack::listen_interfaces, "0.0.0.0:49800");
lt::session ses1(s, 0);
fprintf(stderr, " Session1 \n");
display_settings(s);
torrent_handle tor1;
torrent_handle tor2;
using boost::tuples::ignore;
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, true, false, true
, "_pe", 16 * 1024, 0, false, 0, true);
fprintf(stderr, "waiting for transfer to complete\n");
for (int i = 0; i < timeout * 10; ++i)
{
torrent_status s = tor2.status();
print_alerts(ses1, "ses1");
print_alerts(ses2, "ses2");
if (s.is_seeding) break;
test_sleep(100);
}
TEST_CHECK(tor2.status().is_seeding);
if (tor2.status().is_seeding) fprintf(stderr, "done\n");
ses1.remove_torrent(tor1);
ses2.remove_torrent(tor2);
// this allows shutting down the sessions in parallel
p1 = ses1.abort();
p2 = ses2.abort();
error_code ec;
remove_all("tmp1_pe", ec);
remove_all("tmp2_pe", ec);
remove_all("tmp3_pe", ec);
}
void test_enc_handler(libtorrent::crypto_plugin* a, libtorrent::crypto_plugin* b)
{
#ifdef TORRENT_USE_VALGRIND
@ -255,65 +157,6 @@ TORRENT_TEST(rc4)
test_enc_handler(&rc41, &rc42);
}
#ifdef TORRENT_USE_VALGRIND
const int timeout = 10;
#else
const int timeout = 5;
#endif
TORRENT_TEST(pe_disabled)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_disabled, timeout);
}
TORRENT_TEST(forced_plaintext)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_plaintext);
}
TORRENT_TEST(forced_rc4)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_rc4);
}
TORRENT_TEST(forced_both)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_both, false);
}
TORRENT_TEST(forced_both_prefer_rc4)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_both, true);
}
TORRENT_TEST(enabled_plaintext)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_plaintext);
}
TORRENT_TEST(enabled_rc4)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_rc4);
}
TORRENT_TEST(enabled_both)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_both, false);
}
TORRENT_TEST(enabled_both_prefer_rc4)
{
using namespace libtorrent;
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_both, true);
}
#else
TORRENT_TEST(disabled)
{