premiere-libtorrent/test/test_pe_crypto.cpp

324 lines
8.8 KiB
C++
Raw Normal View History

2007-06-06 02:41:20 +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/hasher.hpp"
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/session.hpp"
2014-07-19 04:17:19 +02:00
#include "libtorrent/random.hpp"
2007-06-06 02:41:20 +02:00
#include "setup_transfer.hpp"
#include "test.hpp"
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
2013-12-02 05:24:10 +01:00
char const* pe_policy(boost::uint8_t policy)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
2015-05-18 01:32:13 +02:00
2014-07-06 21:18:00 +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";
2007-06-06 02:41:20 +02:00
}
2014-07-06 21:18:00 +02:00
void display_settings(libtorrent::settings_pack const& s)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
2015-05-18 01:32:13 +02:00
fprintf(stderr, "out_enc_policy - %s\tin_enc_policy - %s\n"
2014-07-06 21:18:00 +02:00
, pe_policy(s.get_int(settings_pack::out_enc_policy))
, pe_policy(s.get_int(settings_pack::in_enc_policy)));
2015-05-18 01:32:13 +02:00
fprintf(stderr, "enc_level - %s\t\tprefer_rc4 - %s\n"
2014-07-06 21:18:00 +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");
2007-06-06 02:41:20 +02:00
}
2014-07-06 21:18:00 +02:00
void test_transfer(libtorrent::settings_pack::enc_policy policy
, int timeout
2014-07-06 21:18:00 +02:00
, libtorrent::settings_pack::enc_level level = libtorrent::settings_pack::pe_both
, bool pref_rc4 = false)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
namespace lt = libtorrent;
2007-06-06 02:41:20 +02:00
// 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;
2014-07-06 21:18:00 +02:00
settings_pack s;
2015-05-18 01:32:13 +02:00
2014-07-06 21:18:00 +02:00
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);
2014-07-06 21:18:00 +02:00
fprintf(stderr, " Session2 \n");
display_settings(s);
2007-06-06 02:41:20 +02:00
2014-07-06 21:18:00 +02:00
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);
2007-06-06 02:41:20 +02:00
fprintf(stderr, " Session1 \n");
2014-07-06 21:18:00 +02:00
display_settings(s);
2007-06-06 02:41:20 +02:00
torrent_handle tor1;
torrent_handle tor2;
2007-06-10 22:46:09 +02:00
using boost::tuples::ignore;
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, true, false, true
2015-05-18 01:32:13 +02:00
, "_pe", 16 * 1024, 0, false, 0, true);
2007-06-06 02:41:20 +02:00
fprintf(stderr, "waiting for transfer to complete\n");
2007-06-06 02:41:20 +02:00
for (int i = 0; i < timeout * 10; ++i)
2007-06-06 02:41:20 +02:00
{
torrent_status s = tor2.status();
print_alerts(ses1, "ses1");
print_alerts(ses2, "ses2");
2007-06-06 02:41:20 +02:00
if (s.is_seeding) break;
test_sleep(100);
2007-06-06 02:41:20 +02:00
}
TEST_CHECK(tor2.status().is_seeding);
2015-05-18 01:32:13 +02:00
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;
2012-03-17 20:06:11 +01:00
remove_all("tmp1_pe", ec);
remove_all("tmp2_pe", ec);
remove_all("tmp3_pe", ec);
2007-06-06 02:41:20 +02:00
}
void test_enc_handler(libtorrent::crypto_plugin* a, libtorrent::crypto_plugin* b)
{
#ifdef TORRENT_USE_VALGRIND
const int repcount = 10;
#else
const int repcount = 128;
#endif
for (int rep = 0; rep < repcount; ++rep)
{
2015-05-18 01:32:13 +02:00
int buf_len = rand() % (512 * 1024);
char* buf = new char[buf_len];
char* cmp_buf = new char[buf_len];
2015-05-18 01:32:13 +02:00
std::generate(buf, buf + buf_len, &std::rand);
std::memcpy(cmp_buf, buf, buf_len);
2015-05-18 01:32:13 +02:00
using namespace boost::asio;
std::vector<mutable_buffer> iovec;
iovec.push_back(mutable_buffer(buf, buf_len));
a->encrypt(iovec);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
TEST_CHECK(iovec.empty());
int consume = 0;
int produce = buf_len;
int packet_size = 0;
iovec.push_back(mutable_buffer(buf, buf_len));
b->decrypt(iovec, consume, produce, packet_size);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
TEST_CHECK(iovec.empty());
TEST_EQUAL(consume, 0);
TEST_EQUAL(produce, buf_len);
TEST_EQUAL(packet_size, 0);
2015-05-18 01:32:13 +02:00
iovec.push_back(mutable_buffer(buf, buf_len));
b->encrypt(iovec);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
TEST_CHECK(iovec.empty());
consume = 0;
produce = buf_len;
packet_size = 0;
iovec.push_back(mutable_buffer(buf, buf_len));
a->decrypt(iovec, consume, produce, packet_size);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
TEST_CHECK(iovec.empty());
TEST_EQUAL(consume, 0);
TEST_EQUAL(produce, buf_len);
TEST_EQUAL(packet_size, 0);
2015-05-18 01:32:13 +02:00
delete[] buf;
delete[] cmp_buf;
}
}
2007-06-06 02:41:20 +02:00
void print_key(char const* key)
{
for (int i = 0;i < 96; ++i)
{
printf("%02x ", unsigned(key[i]));
}
printf("\n");
}
2013-09-01 08:52:50 +02:00
TORRENT_TEST(diffie_hellman)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
2013-09-01 08:52:50 +02:00
#ifdef TORRENT_USE_VALGRIND
const int repcount = 10;
#else
const int repcount = 128;
#endif
2007-06-06 02:41:20 +02:00
for (int rep = 0; rep < repcount; ++rep)
{
2008-06-29 20:55:23 +02:00
dh_key_exchange DH1, DH2;
2015-05-18 01:32:13 +02:00
2007-06-06 02:41:20 +02:00
DH1.compute_secret(DH2.get_local_key());
DH2.compute_secret(DH1.get_local_key());
2015-05-18 01:32:13 +02:00
2007-06-06 02:41:20 +02:00
TEST_CHECK(std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()));
if (!std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()))
{
printf("DH1 local: ");
print_key(DH1.get_local_key());
printf("DH2 local: ");
print_key(DH2.get_local_key());
printf("DH1 shared_secret: ");
print_key(DH1.get_secret());
2007-06-06 02:41:20 +02:00
printf("DH2 shared_secret: ");
print_key(DH2.get_secret());
}
}
}
2007-06-06 02:41:20 +02:00
TORRENT_TEST(rc4)
{
using namespace libtorrent;
2007-06-06 02:41:20 +02:00
sha1_hash test1_key = hasher("test1_key",8).final();
sha1_hash test2_key = hasher("test2_key",8).final();
fprintf(stderr, "testing RC4 handler\n");
rc4_handler rc41;
rc41.set_incoming_key(&test2_key[0], 20);
rc41.set_outgoing_key(&test1_key[0], 20);
rc4_handler rc42;
rc42.set_incoming_key(&test1_key[0], 20);
rc42.set_outgoing_key(&test2_key[0], 20);
test_enc_handler(&rc41, &rc42);
}
2015-05-18 01:32:13 +02:00
#ifdef TORRENT_USE_VALGRIND
const int timeout = 10;
#else
const int timeout = 5;
#endif
TORRENT_TEST(pe_disabled)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_disabled, timeout);
}
2007-06-06 02:41:20 +02:00
TORRENT_TEST(forced_plaintext)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_plaintext);
}
TORRENT_TEST(forced_rc4)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_rc4);
}
TORRENT_TEST(forced_both)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_both, false);
}
TORRENT_TEST(forced_both_prefer_rc4)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_forced, timeout, settings_pack::pe_both, true);
}
2007-06-06 02:41:20 +02:00
TORRENT_TEST(enabled_plaintext)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_plaintext);
}
TORRENT_TEST(enabled_rc4)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_rc4);
}
TORRENT_TEST(enabled_both)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_both, false);
}
TORRENT_TEST(enabled_both_prefer_rc4)
{
using namespace libtorrent;
2014-07-06 21:18:00 +02:00
test_transfer(settings_pack::pe_enabled, timeout, settings_pack::pe_both, true);
}
2013-09-01 08:52:50 +02:00
#else
TORRENT_TEST(disabled)
{
2013-09-01 08:52:50 +02:00
fprintf(stderr, "PE test not run because it's disabled\n");
}
#endif
2013-09-01 08:52:50 +02:00