forked from premiere/premiere-libtorrent
break out more tests from test_primitives
This commit is contained in:
parent
0052fa6c19
commit
9856a1fe0c
|
@ -33,6 +33,8 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#ifndef TORRENT_IDENTIFY_CLIENT_HPP_INCLUDED
|
||||
#define TORRENT_IDENTIFY_CLIENT_HPP_INCLUDED
|
||||
|
||||
#include "libtorrent/config.hpp"
|
||||
|
||||
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
@ -41,21 +43,25 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#include "libtorrent/peer_id.hpp"
|
||||
#include "libtorrent/fingerprint.hpp"
|
||||
#include "libtorrent/config.hpp"
|
||||
|
||||
namespace libtorrent
|
||||
{
|
||||
|
||||
// these functions don't really need to be public. This mechanism of
|
||||
// advertising client software and version is also out-dated.
|
||||
|
||||
// This function can can be used to extract a string describing a client
|
||||
// version from its peer-id. It will recognize most clients that have this
|
||||
// kind of identification in the peer-id.
|
||||
TORRENT_EXPORT std::string identify_client(const peer_id& p);
|
||||
TORRENT_DEPRECATED_EXPORT std::string identify_client(const peer_id& p)
|
||||
TORRENT_DEPRECATED;
|
||||
|
||||
// Returns an optional fingerprint if any can be identified from the peer
|
||||
// id. This can be used to automate the identification of clients. It will
|
||||
// not be able to identify peers with non- standard encodings. Only Azureus
|
||||
// style, Shadow's style and Mainline style.
|
||||
TORRENT_EXPORT boost::optional<fingerprint> client_fingerprint(peer_id const& p);
|
||||
TORRENT_DEPRECATED_EXPORT boost::optional<fingerprint>
|
||||
client_fingerprint(peer_id const& p) TORRENT_DEPRECATED;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3342,7 +3342,7 @@ namespace libtorrent
|
|||
{
|
||||
TORRENT_ASSERT(m_sent_handshake);
|
||||
received_bytes(0, bytes_transferred);
|
||||
// bytes_transferred = 0;
|
||||
|
||||
t = associated_torrent().lock();
|
||||
if (!t)
|
||||
{
|
||||
|
|
|
@ -382,7 +382,6 @@ namespace libtorrent
|
|||
if (find_string(PID, "-BOW") && PID[7] == '-')
|
||||
return "Bits on Wheels " + std::string((char const*)PID + 4, (char const*)PID + 7);
|
||||
|
||||
|
||||
if (find_string(PID, "eX"))
|
||||
{
|
||||
std::string user((char const*)PID + 2, (char const*)PID + 14);
|
||||
|
@ -395,7 +394,6 @@ namespace libtorrent
|
|||
if (std::equal(PID, PID + 13, "\0\0\0\0\0\0\0\0\0\0\0\0\0"))
|
||||
return "Experimental 3.1";
|
||||
|
||||
|
||||
// look for azureus style id
|
||||
f = parse_az_style(p);
|
||||
if (f) return lookup(*f);
|
||||
|
@ -420,5 +418,5 @@ namespace libtorrent
|
|||
unknown += "]";
|
||||
return unknown;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -81,11 +81,11 @@ explicit libtorrent_test ;
|
|||
lib advapi32 : : <name>Advapi32 ;
|
||||
|
||||
project
|
||||
: requirements
|
||||
: requirements
|
||||
<source>main.cpp
|
||||
<conditional>@link_test
|
||||
<conditional>@link_libtorrent
|
||||
: default-build
|
||||
: default-build
|
||||
<threading>multi
|
||||
<invariant-checks>full
|
||||
<debug-iterators>on
|
||||
|
@ -97,6 +97,9 @@ feature launcher : none valgrind : composite ;
|
|||
feature.compose <launcher>valgrind : <testing.launcher>"valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222 --suppressions=valgrind_suppressions.txt" <use-valgrind>on ;
|
||||
|
||||
test-suite libtorrent :
|
||||
[ run test_sha1_hash.cpp ]
|
||||
[ run test_bloom_filter.cpp ]
|
||||
[ run test_identify_client.cpp ]
|
||||
[ run test_merkle.cpp ]
|
||||
[ run test_alert_manager.cpp ]
|
||||
[ run test_resolve_links.cpp ]
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
AUTOMAKE_OPTIONS = subdir-objects
|
||||
|
||||
test_programs = \
|
||||
test_sha1_hash.cpp \
|
||||
test_bloom_filter \
|
||||
test_identify_client \
|
||||
test_alert_manager \
|
||||
test_bitfield \
|
||||
test_crc32 \
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#include "test.hpp"
|
||||
#include "libtorrent/bloom_filter.hpp"
|
||||
#include "libtorrent/hasher.hpp"
|
||||
#include "libtorrent/sha1_hash.hpp"
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
void test_set_and_get()
|
||||
{
|
||||
bloom_filter<32> filter;
|
||||
sha1_hash k1 = hasher("test1", 5).final();
|
||||
sha1_hash k2 = hasher("test2", 5).final();
|
||||
sha1_hash k3 = hasher("test3", 5).final();
|
||||
sha1_hash k4 = hasher("test4", 5).final();
|
||||
TEST_CHECK(!filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(!filter.find(k4));
|
||||
|
||||
filter.set(k1);
|
||||
TEST_CHECK(filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(!filter.find(k4));
|
||||
|
||||
filter.set(k4);
|
||||
TEST_CHECK(filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(filter.find(k4));
|
||||
}
|
||||
|
||||
void test_set_bits()
|
||||
{
|
||||
boost::uint8_t bits[4] = {0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
for (int i = 0; i < 4 * 8; ++i)
|
||||
{
|
||||
boost::uint8_t t[4] = { boost::uint8_t(i & 0xff), 0, boost::uint8_t(i & 0xff), 0 };
|
||||
TEST_CHECK(!has_bits(t, bits, 6));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4 * 8; i += 2)
|
||||
{
|
||||
boost::uint8_t t[4] = { boost::uint8_t(i & 0xff), 0, boost::uint8_t(i & 0xff), 0 };
|
||||
TEST_CHECK(!has_bits(t, bits, 4));
|
||||
set_bits(t, bits, 4);
|
||||
TEST_CHECK(has_bits(t, bits, 4));
|
||||
}
|
||||
|
||||
boost::uint8_t compare[4] = { 0x55, 0x55, 0x55, 0x55};
|
||||
TEST_EQUAL(memcmp(compare, bits, 4), 0);
|
||||
}
|
||||
|
||||
void test_count_zeroes()
|
||||
{
|
||||
boost::uint8_t bits[4] = {0x00, 0xff, 0x55, 0xaa};
|
||||
|
||||
TEST_EQUAL(count_zero_bits(bits, 4), 16);
|
||||
|
||||
boost::uint8_t t[4] = { 4, 0, 4, 0 };
|
||||
set_bits(t, bits, 4);
|
||||
TEST_EQUAL(count_zero_bits(bits, 4), 15);
|
||||
|
||||
boost::uint8_t compare[4] = { 0x10, 0xff, 0x55, 0xaa};
|
||||
TEST_EQUAL(memcmp(compare, bits, 4), 0);
|
||||
}
|
||||
|
||||
void test_to_from_string()
|
||||
{
|
||||
uint8_t bits[4] = { 0x10, 0xff, 0x55, 0xaa};
|
||||
|
||||
bloom_filter<4> filter;
|
||||
filter.from_string(reinterpret_cast<char*>(bits));
|
||||
|
||||
std::string bits_out = filter.to_string();
|
||||
TEST_EQUAL(memcmp(bits_out.c_str(), bits, 4), 0);
|
||||
|
||||
sha1_hash k( "\x01\x00\x02\x00 ");
|
||||
TEST_CHECK(!filter.find(k));
|
||||
filter.set(k);
|
||||
TEST_CHECK(filter.find(k));
|
||||
|
||||
boost::uint8_t compare[4] = { 0x16, 0xff, 0x55, 0xaa};
|
||||
|
||||
bits_out = filter.to_string();
|
||||
TEST_EQUAL(memcmp(compare, bits_out.c_str(), 4), 0);
|
||||
}
|
||||
|
||||
int test_main()
|
||||
{
|
||||
test_set_and_get();
|
||||
test_set_bits();
|
||||
test_count_zeroes();
|
||||
test_to_from_string();
|
||||
|
||||
// TODO: test size()
|
||||
// TODO: test clear()
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#include "test.hpp"
|
||||
#include "libtorrent/identify_client.hpp"
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
int test_main()
|
||||
{
|
||||
TEST_EQUAL(identify_client(peer_id("-AZ123B-............")), "Azureus 1.2.3.11");
|
||||
TEST_EQUAL(identify_client(peer_id("-AZ1230-............")), "Azureus 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("S123--..............")), "Shadow 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("S\x1\x2\x3....\0...........")), "Shadow 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("M1-2-3--............")), "Mainline 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("\0\0\0\0\0\0\0\0\0\0\0\0........")), "Generic");
|
||||
TEST_EQUAL(identify_client(peer_id("-xx1230-............")), "xx 1.2.3");
|
||||
}
|
||||
|
|
@ -37,15 +37,10 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/entry.hpp"
|
||||
#include "libtorrent/torrent_info.hpp"
|
||||
#include "libtorrent/broadcast_socket.hpp"
|
||||
#include "libtorrent/identify_client.hpp"
|
||||
#include "libtorrent/file.hpp"
|
||||
#include "libtorrent/session.hpp"
|
||||
#include "libtorrent/bencode.hpp"
|
||||
#include "libtorrent/timestamp_history.hpp"
|
||||
#include "libtorrent/enum_net.hpp"
|
||||
#include "libtorrent/bloom_filter.hpp"
|
||||
#include "libtorrent/aux_/session_impl.hpp"
|
||||
#include "libtorrent/ip_voter.hpp"
|
||||
#include "libtorrent/enum_net.hpp"
|
||||
#include "libtorrent/socket_io.hpp"
|
||||
#include <boost/bind.hpp>
|
||||
#include <iostream>
|
||||
|
@ -169,29 +164,7 @@ int test_main()
|
|||
TEST_CHECK(ipv2.external_address(rand_v6()) == real_external2);
|
||||
#endif
|
||||
|
||||
// test bloom_filter
|
||||
bloom_filter<32> filter;
|
||||
sha1_hash k1 = hasher("test1", 5).final();
|
||||
sha1_hash k2 = hasher("test2", 5).final();
|
||||
sha1_hash k3 = hasher("test3", 5).final();
|
||||
sha1_hash k4 = hasher("test4", 5).final();
|
||||
TEST_CHECK(!filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(!filter.find(k4));
|
||||
|
||||
filter.set(k1);
|
||||
TEST_CHECK(filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(!filter.find(k4));
|
||||
|
||||
filter.set(k4);
|
||||
TEST_CHECK(filter.find(k1));
|
||||
TEST_CHECK(!filter.find(k2));
|
||||
TEST_CHECK(!filter.find(k3));
|
||||
TEST_CHECK(filter.find(k4));
|
||||
|
||||
// TODO: 3 move this out to its own test
|
||||
// test timestamp_history
|
||||
{
|
||||
timestamp_history h;
|
||||
|
@ -312,19 +285,9 @@ int test_main()
|
|||
}
|
||||
}
|
||||
|
||||
// test identify_client
|
||||
|
||||
// TODO: 3 move testing of identify_client out to its own test
|
||||
TEST_EQUAL(identify_client(peer_id("-AZ123B-............")), "Azureus 1.2.3.11");
|
||||
TEST_EQUAL(identify_client(peer_id("-AZ1230-............")), "Azureus 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("S123--..............")), "Shadow 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("S\x1\x2\x3....\0...........")), "Shadow 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("M1-2-3--............")), "Mainline 1.2.3");
|
||||
TEST_EQUAL(identify_client(peer_id("\0\0\0\0\0\0\0\0\0\0\0\0........")), "Generic");
|
||||
TEST_EQUAL(identify_client(peer_id("-xx1230-............")), "xx 1.2.3");
|
||||
|
||||
// test network functions
|
||||
|
||||
// TODO: 3 move this out to a test_enum_net test
|
||||
TEST_CHECK(is_local(address::from_string("192.168.0.1", ec)));
|
||||
TEST_CHECK(is_local(address::from_string("10.1.1.56", ec)));
|
||||
TEST_CHECK(!is_local(address::from_string("14.14.251.63", ec)));
|
||||
|
@ -354,106 +317,6 @@ int test_main()
|
|||
address::from_string("10.1.3.3", ec),
|
||||
address::from_string("255.255.0.0", ec)));
|
||||
|
||||
// test peer_id/sha1_hash type
|
||||
|
||||
sha1_hash h1(0);
|
||||
sha1_hash h2(0);
|
||||
TEST_CHECK(h1 == h2);
|
||||
TEST_CHECK(!(h1 != h2));
|
||||
TEST_CHECK(!(h1 < h2));
|
||||
TEST_CHECK(!(h1 < h2));
|
||||
TEST_CHECK(h1.is_all_zeros());
|
||||
|
||||
h1 = to_hash("0123456789012345678901234567890123456789");
|
||||
h2 = to_hash("0113456789012345678901234567890123456789");
|
||||
|
||||
TEST_CHECK(h2 < h1);
|
||||
TEST_CHECK(h2 == h2);
|
||||
TEST_CHECK(h1 == h1);
|
||||
h2.clear();
|
||||
TEST_CHECK(h2.is_all_zeros());
|
||||
|
||||
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
|
||||
h1 &= h2;
|
||||
TEST_CHECK(h1 == to_hash("fffff000000000000000fffff000000000000000"));
|
||||
|
||||
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
|
||||
h1 |= h2;
|
||||
TEST_CHECK(h1 == to_hash("fffffffffffffff00000fffffffffffffff00000"));
|
||||
|
||||
h2 = to_hash("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f");
|
||||
h1 ^= h2;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f"));
|
||||
TEST_CHECK(h1 != h2);
|
||||
|
||||
h2 = sha1_hash(" ");
|
||||
TEST_CHECK(h2 == to_hash("2020202020202020202020202020202020202020"));
|
||||
|
||||
h1 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
h1 <<= 12;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("fffffff0000000000ffffffffff0000000000000"));
|
||||
h1 >>= 12;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("000fffffff0000000000ffffffffff0000000000"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 <<= 1;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("e000000000000000000000000000000000000000"));
|
||||
|
||||
h1 = to_hash("0000000000000000000000000000000000000007");
|
||||
h1 <<= 1;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("000000000000000000000000000000000000000e"));
|
||||
|
||||
h1 = to_hash("0000000000000000000000000000000000000007");
|
||||
h1 >>= 1;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("0000000000000000000000000000000000000003"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 >>= 1;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("3800000000000000000000000000000000000000"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 >>= 32;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("0000000070000000000000000000000000000000"));
|
||||
h1 >>= 33;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("0000000000000000380000000000000000000000"));
|
||||
h1 <<= 33;
|
||||
#if TORRENT_USE_IOSTREAM
|
||||
std::cerr << h1 << std::endl;
|
||||
#endif
|
||||
TEST_CHECK(h1 == to_hash("0000000070000000000000000000000000000000"));
|
||||
|
||||
// CIDR distance test
|
||||
h1 = to_hash("0123456789abcdef01232456789abcdef0123456");
|
||||
h2 = to_hash("0123456789abcdef01232456789abcdef0123456");
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#include "test.hpp"
|
||||
#include "libtorrent/sha1_hash.hpp"
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
sha1_hash to_hash(char const* s)
|
||||
{
|
||||
sha1_hash ret;
|
||||
from_hex(s, 40, (char*)&ret[0]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_main()
|
||||
{
|
||||
sha1_hash h1(0);
|
||||
sha1_hash h2(0);
|
||||
TEST_CHECK(h1 == h2);
|
||||
TEST_CHECK(!(h1 != h2));
|
||||
TEST_CHECK(!(h1 < h2));
|
||||
TEST_CHECK(!(h1 < h2));
|
||||
TEST_CHECK(h1.is_all_zeros());
|
||||
|
||||
h1 = to_hash("0123456789012345678901234567890123456789");
|
||||
h2 = to_hash("0113456789012345678901234567890123456789");
|
||||
|
||||
TEST_CHECK(h2 < h1);
|
||||
TEST_CHECK(h2 == h2);
|
||||
TEST_CHECK(h1 == h1);
|
||||
h2.clear();
|
||||
TEST_CHECK(h2.is_all_zeros());
|
||||
|
||||
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
|
||||
h1 &= h2;
|
||||
TEST_CHECK(h1 == to_hash("fffff000000000000000fffff000000000000000"));
|
||||
|
||||
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
|
||||
h1 |= h2;
|
||||
TEST_CHECK(h1 == to_hash("fffffffffffffff00000fffffffffffffff00000"));
|
||||
|
||||
h2 = to_hash("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f");
|
||||
h1 ^= h2;
|
||||
TEST_CHECK(h1 == to_hash("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f"));
|
||||
TEST_CHECK(h1 != h2);
|
||||
|
||||
h2 = sha1_hash(" ");
|
||||
TEST_CHECK(h2 == to_hash("2020202020202020202020202020202020202020"));
|
||||
|
||||
h1 = to_hash("ffffffffff0000000000ffffffffff0000000000");
|
||||
h1 <<= 12;
|
||||
TEST_CHECK(h1 == to_hash("fffffff0000000000ffffffffff0000000000000"));
|
||||
h1 >>= 12;
|
||||
TEST_CHECK(h1 == to_hash("000fffffff0000000000ffffffffff0000000000"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 <<= 1;
|
||||
TEST_CHECK(h1 == to_hash("e000000000000000000000000000000000000000"));
|
||||
|
||||
h1 = to_hash("0000000000000000000000000000000000000007");
|
||||
h1 <<= 1;
|
||||
TEST_CHECK(h1 == to_hash("000000000000000000000000000000000000000e"));
|
||||
|
||||
h1 = to_hash("0000000000000000000000000000000000000007");
|
||||
h1 >>= 1;
|
||||
TEST_CHECK(h1 == to_hash("0000000000000000000000000000000000000003"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 >>= 1;
|
||||
TEST_CHECK(h1 == to_hash("3800000000000000000000000000000000000000"));
|
||||
|
||||
h1 = to_hash("7000000000000000000000000000000000000000");
|
||||
h1 >>= 32;
|
||||
TEST_CHECK(h1 == to_hash("0000000070000000000000000000000000000000"));
|
||||
h1 >>= 33;
|
||||
TEST_CHECK(h1 == to_hash("0000000000000000380000000000000000000000"));
|
||||
h1 <<= 33;
|
||||
TEST_CHECK(h1 == to_hash("0000000070000000000000000000000000000000"));
|
||||
}
|
||||
|
|
@ -39,7 +39,10 @@ mkdir test-coverage
|
|||
rm test-coverage/coverage_all
|
||||
set -e
|
||||
|
||||
run_test test_packet_buffer "*/packet_buffer.*" -a
|
||||
run_test test_bloom_filter "*/bloom_filter.*" -a
|
||||
run_test test_sha1_hash "*/sha1_hash.*"
|
||||
run_test test_identify_client "*/identify_client.*"
|
||||
run_test test_packet_buffer "*/packet_buffer.*"
|
||||
run_test test_ip_voter "*/ip_voter.*"
|
||||
run_test test_bitfield "*/bitfield.*"
|
||||
run_test test_alert_manager "*/alert_manager.*"
|
||||
|
|
Loading…
Reference in New Issue