162 lines
4.6 KiB
C++
162 lines
4.6 KiB
C++
/*
|
|
|
|
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 "libtorrent/aux_/array_view.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
#include "test.hpp"
|
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
|
|
|
namespace lt = libtorrent;
|
|
|
|
void test_enc_handler(libtorrent::crypto_plugin& a, libtorrent::crypto_plugin& b)
|
|
{
|
|
int const repcount = 128;
|
|
for (int rep = 0; rep < repcount; ++rep)
|
|
{
|
|
int const buf_len = rand() % (512 * 1024);
|
|
std::vector<char> buf(buf_len);
|
|
std::vector<char> cmp_buf(buf_len);
|
|
|
|
std::generate(buf.begin(), buf.end(), &std::rand);
|
|
std::copy(buf.begin(), buf.end(), cmp_buf.begin());
|
|
|
|
using namespace boost::asio;
|
|
|
|
{
|
|
mutable_buffer iovec(&buf[0], buf_len);
|
|
int next_barrier;
|
|
lt::aux::array_view<const_buffer> iovec_out;
|
|
std::tie(next_barrier, iovec_out) = a.encrypt(iovec);
|
|
TEST_CHECK(buf != cmp_buf);
|
|
TEST_EQUAL(iovec_out.size(), 0);
|
|
TEST_EQUAL(next_barrier, buf_len);
|
|
}
|
|
|
|
{
|
|
int consume = 0;
|
|
int produce = buf_len;
|
|
int packet_size = 0;
|
|
mutable_buffer iovec(&buf[0], buf_len);
|
|
b.decrypt(iovec, consume, produce, packet_size);
|
|
TEST_CHECK(buf == cmp_buf);
|
|
TEST_EQUAL(consume, 0);
|
|
TEST_EQUAL(produce, buf_len);
|
|
TEST_EQUAL(packet_size, 0);
|
|
}
|
|
|
|
{
|
|
mutable_buffer iovec(&buf[0], buf_len);
|
|
int next_barrier;
|
|
lt::aux::array_view<const_buffer> iovec_out;
|
|
std::tie(next_barrier, iovec_out) = b.encrypt(iovec);
|
|
TEST_EQUAL(iovec_out.size(), 0);
|
|
TEST_CHECK(buf != cmp_buf);
|
|
TEST_EQUAL(next_barrier, buf_len);
|
|
|
|
int consume = 0;
|
|
int produce = buf_len;
|
|
int packet_size = 0;
|
|
mutable_buffer iovec2(&buf[0], buf_len);
|
|
a.decrypt(iovec2, consume, produce, packet_size);
|
|
TEST_CHECK(buf == cmp_buf);
|
|
TEST_EQUAL(consume, 0);
|
|
TEST_EQUAL(produce, buf_len);
|
|
TEST_EQUAL(packet_size, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
TORRENT_TEST(diffie_hellman)
|
|
{
|
|
using namespace libtorrent;
|
|
|
|
const int repcount = 128;
|
|
|
|
for (int rep = 0; rep < repcount; ++rep)
|
|
{
|
|
dh_key_exchange DH1, DH2;
|
|
|
|
DH1.compute_secret(DH2.get_local_key());
|
|
DH2.compute_secret(DH1.get_local_key());
|
|
|
|
TEST_EQUAL(DH1.get_secret(), DH2.get_secret());
|
|
if (!DH1.get_secret() != DH2.get_secret())
|
|
{
|
|
std::printf("DH1 local: ");
|
|
std::cout << DH1.get_local_key() << std::endl;
|
|
|
|
std::printf("DH2 local: ");
|
|
std::cout << DH2.get_local_key() << std::endl;
|
|
|
|
std::printf("DH1 shared_secret: ");
|
|
std::cout << DH1.get_secret() << std::endl;
|
|
|
|
std::printf("DH2 shared_secret: ");
|
|
std::cout << DH2.get_secret() << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
TORRENT_TEST(rc4)
|
|
{
|
|
using namespace libtorrent;
|
|
|
|
sha1_hash test1_key = hasher("test1_key",8).final();
|
|
sha1_hash test2_key = hasher("test2_key",8).final();
|
|
|
|
std::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);
|
|
}
|
|
|
|
#else
|
|
TORRENT_TEST(disabled)
|
|
{
|
|
std::fprintf(stderr, "PE test not run because it's disabled\n");
|
|
}
|
|
#endif
|
|
|