From 9856a1fe0c2bdd2e945392fc0dd1b40cb1ecbf2b Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 23 May 2015 01:38:47 +0000 Subject: [PATCH] break out more tests from test_primitives --- include/libtorrent/hasher.hpp | 2 +- include/libtorrent/identify_client.hpp | 12 ++- include/libtorrent/kademlia/node.hpp | 2 +- include/libtorrent/sha1_hash.hpp | 4 +- src/bt_peer_connection.cpp | 10 +- src/identify_client.cpp | 6 +- test/Jamfile | 7 +- test/Makefile.am | 3 + test/test_bitfield.cpp | 2 +- test/test_bloom_filter.cpp | 132 +++++++++++++++++++++++ test/test_identify_client.cpp | 48 +++++++++ test/test_primitives.cpp | 143 +------------------------ test/test_sha1_hash.cpp | 112 +++++++++++++++++++ tools/test_coverage.sh | 5 +- 14 files changed, 328 insertions(+), 160 deletions(-) create mode 100644 test/test_bloom_filter.cpp create mode 100644 test/test_identify_client.cpp create mode 100644 test/test_sha1_hash.cpp diff --git a/include/libtorrent/hasher.hpp b/include/libtorrent/hasher.hpp index 52f37c018..e7a302a16 100644 --- a/include/libtorrent/hasher.hpp +++ b/include/libtorrent/hasher.hpp @@ -93,7 +93,7 @@ namespace libtorrent // append the following bytes to what is being hashed hasher& update(std::string const& data) { update(data.c_str(), int(data.size())); return *this; } hasher& update(const char* data, int len); - + // returns the SHA-1 digest of the buffers previously passed to // update() and the hasher constructor. sha1_hash final(); diff --git a/include/libtorrent/identify_client.hpp b/include/libtorrent/identify_client.hpp index babdf10d0..c16f8944b 100644 --- a/include/libtorrent/identify_client.hpp +++ b/include/libtorrent/identify_client.hpp @@ -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 @@ -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 client_fingerprint(peer_id const& p); + TORRENT_DEPRECATED_EXPORT boost::optional + client_fingerprint(peer_id const& p) TORRENT_DEPRECATED; } diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index 856059dec..1b46dcd27 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -210,7 +210,7 @@ public: void bootstrap(std::vector const& nodes , find_data::nodes_callback const& f); void add_router_node(udp::endpoint router); - + void unreachable(udp::endpoint const& ep); void incoming(msg const& m); diff --git a/include/libtorrent/sha1_hash.hpp b/include/libtorrent/sha1_hash.hpp index 7d9bbde6c..3c8729993 100644 --- a/include/libtorrent/sha1_hash.hpp +++ b/include/libtorrent/sha1_hash.hpp @@ -226,7 +226,7 @@ namespace libtorrent } return false; } - + // returns a bit-wise negated copy of the sha1-hash sha1_hash operator~() { @@ -235,7 +235,7 @@ namespace libtorrent ret.m_number[i] = ~m_number[i]; return ret; } - + // returns the bit-wise XOR of the two sha1-hashes. sha1_hash operator^(sha1_hash const& n) const { diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 047799651..58ce77e4a 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -431,7 +431,7 @@ namespace libtorrent p.client = m_client_version; p.connection_type = peer_info::standard_bittorrent; } - + bool bt_peer_connection::in_handshake() const { return m_state < read_packet_size; @@ -1859,7 +1859,7 @@ namespace libtorrent int last_seen_complete = boost::uint8_t(root.dict_find_int_value("complete_ago", -1)); if (last_seen_complete >= 0) set_last_seen_complete(last_seen_complete); - + std::string client_info = root.dict_find_string_value("v"); if (!client_info.empty()) m_client_version = client_info; @@ -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) { @@ -3350,7 +3350,7 @@ namespace libtorrent return; } TORRENT_ASSERT(m_recv_buffer.packet_size() == 20); - + if (!m_recv_buffer.packet_finished()) return; recv_buffer = m_recv_buffer.get(); @@ -3409,7 +3409,7 @@ namespace libtorrent disconnect(errors::self_connection, op_bittorrent, 1); return; } - + m_client_version = identify_client(pid); boost::optional f = client_fingerprint(pid); if (f && std::equal(f->name, f->name + 2, "BC")) diff --git a/src/identify_client.cpp b/src/identify_client.cpp index 4f64b32da..5bfeb4aca 100644 --- a/src/identify_client.cpp +++ b/src/identify_client.cpp @@ -381,12 +381,11 @@ 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); - return std::string("eXeem ('") + user.c_str() + "')"; + return std::string("eXeem ('") + user.c_str() + "')"; } if (std::equal(PID, PID + 13, "\0\0\0\0\0\0\0\0\0\0\0\0\x97")) @@ -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; } - } + diff --git a/test/Jamfile b/test/Jamfile index ce0705356..a8fa4eb64 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -81,11 +81,11 @@ explicit libtorrent_test ; lib advapi32 : : Advapi32 ; project - : requirements + : requirements main.cpp @link_test @link_libtorrent - : default-build + : default-build multi full on @@ -97,6 +97,9 @@ feature launcher : none valgrind : composite ; feature.compose valgrind : "valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222 --suppressions=valgrind_suppressions.txt" 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 ] diff --git a/test/Makefile.am b/test/Makefile.am index 10d10fc4a..0bfd8acdf 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 \ diff --git a/test/test_bitfield.cpp b/test/test_bitfield.cpp index 703690da4..06f641bef 100644 --- a/test/test_bitfield.cpp +++ b/test/test_bitfield.cpp @@ -84,7 +84,7 @@ int test_main() int distance = std::distance(test1.begin(), test1.end()); printf("distance: %d\n", distance); TEST_CHECK(distance == 10); - + print_bitfield(test1); test1.set_all(); diff --git a/test/test_bloom_filter.cpp b/test/test_bloom_filter.cpp new file mode 100644 index 000000000..b8c251205 --- /dev/null +++ b/test/test_bloom_filter.cpp @@ -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(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() +} + diff --git a/test/test_identify_client.cpp b/test/test_identify_client.cpp new file mode 100644 index 000000000..5331154dc --- /dev/null +++ b/test/test_identify_client.cpp @@ -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"); +} + diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 171c982fa..b852318a2 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -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 #include @@ -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"); diff --git a/test/test_sha1_hash.cpp b/test/test_sha1_hash.cpp new file mode 100644 index 000000000..93777101a --- /dev/null +++ b/test/test_sha1_hash.cpp @@ -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")); +} + diff --git a/tools/test_coverage.sh b/tools/test_coverage.sh index a882369aa..6f2d66278 100755 --- a/tools/test_coverage.sh +++ b/tools/test_coverage.sh @@ -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.*"