From 647a6dba54a047028dcf602f44d4f82fab556f4d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 27 May 2015 21:27:41 +0000 Subject: [PATCH] moved some tests to the correct place and factor out some tests from test_primitives.cpp in order to be able to track test coverage better --- test/Jamfile | 1 + test/Makefile.am | 7 ++- test/test_primitives.cpp | 88 --------------------------------- test/test_timestamp_history.cpp | 57 +++++++++++++++++++++ test/test_torrent_info.cpp | 77 +++++++++++++++++++++++++++-- 5 files changed, 137 insertions(+), 93 deletions(-) create mode 100644 test/test_timestamp_history.cpp diff --git a/test/Jamfile b/test/Jamfile index a8fa4eb64..0e15c2ec3 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -97,6 +97,7 @@ 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_timestamp_history.cpp ] [ run test_sha1_hash.cpp ] [ run test_bloom_filter.cpp ] [ run test_identify_client.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index 0bfd8acdf..6b3303a9b 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,8 @@ AUTOMAKE_OPTIONS = subdir-objects test_programs = \ - test_sha1_hash.cpp \ + test_timestamp_history \ + test_sha1_hash \ test_bloom_filter \ test_identify_client \ test_alert_manager \ @@ -145,6 +146,10 @@ libtest_la_SOURCES = main.cpp \ swarm_suite.cpp \ test_utils.cpp +test_timestamp_history_SOURCES = test_timestamp_history.cpp +test_sha1_hash_SOURCES = test_sha1_hash.cpp +test_bloom_filter_SOURCES = test_bloom_filter.cpp +test_identify_client_SOURCES = test_identify_client.cpp test_alert_manager_SOURCES = test_alert_manager.cpp test_bitfield_SOURCES = test_bitfield.cpp test_crc32_SOURCES = test_crc32.cpp diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 0c9b492ac..8345d19ae 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -38,7 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/torrent_info.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/file.hpp" -#include "libtorrent/timestamp_history.hpp" #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/enum_net.hpp" #include "libtorrent/socket_io.hpp" @@ -164,28 +163,6 @@ int test_main() TEST_CHECK(ipv2.external_address(rand_v6()) == real_external2); #endif - // TODO: 3 move this out to its own test - // test timestamp_history - { - timestamp_history h; - TEST_EQUAL(h.add_sample(0x32, false), 0); - TEST_EQUAL(h.base(), 0x32); - TEST_EQUAL(h.add_sample(0x33, false), 0x1); - TEST_EQUAL(h.base(), 0x32); - TEST_EQUAL(h.add_sample(0x3433, false), 0x3401); - TEST_EQUAL(h.base(), 0x32); - TEST_EQUAL(h.add_sample(0x30, false), 0); - TEST_EQUAL(h.base(), 0x30); - - // test that wrapping of the timestamp is properly handled - h.add_sample(0xfffffff3, false); - TEST_EQUAL(h.base(), 0xfffffff3); - - - // TODO: test the case where we have > 120 samples (and have the base delay actually be updated) - // TODO: test the case where a sample is lower than the history entry but not lower than the base - } - // test error codes TEST_CHECK(error_code(errors::http_error).message() == "HTTP error"); TEST_CHECK(error_code(errors::missing_file_sizes).message() == "missing or invalid 'file sizes' entry"); @@ -203,71 +180,6 @@ int test_main() snprintf(msg, sizeof(msg), "too %s format string", "long"); TEST_CHECK(strcmp(msg, "too long ") == 0); - std::string path; - sanitize_append_path_element(path, "a...", 4); - TEST_EQUAL(path, "a"); - - path.clear(); - sanitize_append_path_element(path, "a ", 4); - TEST_EQUAL(path, "a"); - - path.clear(); - sanitize_append_path_element(path, "a...b", 5); - TEST_EQUAL(path, "a...b"); - - path.clear(); - sanitize_append_path_element(path, "a", 1); - sanitize_append_path_element(path, "..", 2); - sanitize_append_path_element(path, "c", 1); -#ifdef TORRENT_WINDOWS - TEST_EQUAL(path, "a\\c"); -#else - TEST_EQUAL(path, "a/c"); -#endif - - path.clear(); - sanitize_append_path_element(path, "/..", 3); - sanitize_append_path_element(path, ".", 1); - sanitize_append_path_element(path, "c", 1); - TEST_EQUAL(path, "c"); - - path.clear(); - sanitize_append_path_element(path, "dev:", 4); -#ifdef TORRENT_WINDOWS - TEST_EQUAL(path, "dev"); -#else - TEST_EQUAL(path, "dev:"); -#endif - - path.clear(); - sanitize_append_path_element(path, "c:", 2); - sanitize_append_path_element(path, "b", 1); -#ifdef TORRENT_WINDOWS - TEST_EQUAL(path, "c\\b"); -#else - TEST_EQUAL(path, "c:/b"); -#endif - - path.clear(); - sanitize_append_path_element(path, "c:", 2); - sanitize_append_path_element(path, ".", 1); - sanitize_append_path_element(path, "c", 1); -#ifdef TORRENT_WINDOWS - TEST_EQUAL(path, "c\\c"); -#else - TEST_EQUAL(path, "c:/c"); -#endif - - path.clear(); - sanitize_append_path_element(path, "\\c", 2); - sanitize_append_path_element(path, ".", 1); - sanitize_append_path_element(path, "c", 1); -#ifdef TORRENT_WINDOWS - TEST_EQUAL(path, "c\\c"); -#else - TEST_EQUAL(path, "c/c"); -#endif - if (supports_ipv6()) { // make sure the assumption we use in policy's peer list hold diff --git a/test/test_timestamp_history.cpp b/test/test_timestamp_history.cpp new file mode 100644 index 000000000..39290007e --- /dev/null +++ b/test/test_timestamp_history.cpp @@ -0,0 +1,57 @@ +/* + +Copyright (c) 2008-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/timestamp_history.hpp" + +int test_main() +{ + using namespace libtorrent; + + timestamp_history h; + TEST_EQUAL(h.add_sample(0x32, false), 0); + TEST_EQUAL(h.base(), 0x32); + TEST_EQUAL(h.add_sample(0x33, false), 0x1); + TEST_EQUAL(h.base(), 0x32); + TEST_EQUAL(h.add_sample(0x3433, false), 0x3401); + TEST_EQUAL(h.base(), 0x32); + TEST_EQUAL(h.add_sample(0x30, false), 0); + TEST_EQUAL(h.base(), 0x30); + + // test that wrapping of the timestamp is properly handled + h.add_sample(0xfffffff3, false); + TEST_EQUAL(h.base(), 0xfffffff3); + + // TODO: test the case where we have > 120 samples (and have the base delay actually be updated) + // TODO: test the case where a sample is lower than the history entry but not lower than the base +} + diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index 0ebb58756..2c98fff07 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -176,10 +176,8 @@ test_failing_torrent_t test_error_torrents[] = // TODO: torrent_info constructor that takes an invalid bencoded buffer // TODO: verify_encoding with a string that triggers character replacement -int test_torrent_parse() +void test_sanitize_path() { - error_code ec; - // test sanitize_append_path_element std::string path; @@ -221,6 +219,77 @@ int test_torrent_parse() TEST_EQUAL(path, "a/b/c"); #endif + path.clear(); + sanitize_append_path_element(path, "a...", 4); + TEST_EQUAL(path, "a"); + + path.clear(); + sanitize_append_path_element(path, "a ", 4); + TEST_EQUAL(path, "a"); + + path.clear(); + sanitize_append_path_element(path, "a...b", 5); + TEST_EQUAL(path, "a...b"); + + path.clear(); + sanitize_append_path_element(path, "a", 1); + sanitize_append_path_element(path, "..", 2); + sanitize_append_path_element(path, "c", 1); +#ifdef TORRENT_WINDOWS + TEST_EQUAL(path, "a\\c"); +#else + TEST_EQUAL(path, "a/c"); +#endif + + path.clear(); + sanitize_append_path_element(path, "/..", 3); + sanitize_append_path_element(path, ".", 1); + sanitize_append_path_element(path, "c", 1); + TEST_EQUAL(path, "c"); + + path.clear(); + sanitize_append_path_element(path, "dev:", 4); +#ifdef TORRENT_WINDOWS + TEST_EQUAL(path, "dev"); +#else + TEST_EQUAL(path, "dev:"); +#endif + + path.clear(); + sanitize_append_path_element(path, "c:", 2); + sanitize_append_path_element(path, "b", 1); +#ifdef TORRENT_WINDOWS + TEST_EQUAL(path, "c\\b"); +#else + TEST_EQUAL(path, "c:/b"); +#endif + + path.clear(); + sanitize_append_path_element(path, "c:", 2); + sanitize_append_path_element(path, ".", 1); + sanitize_append_path_element(path, "c", 1); +#ifdef TORRENT_WINDOWS + TEST_EQUAL(path, "c\\c"); +#else + TEST_EQUAL(path, "c:/c"); +#endif + + path.clear(); + sanitize_append_path_element(path, "\\c", 2); + sanitize_append_path_element(path, ".", 1); + sanitize_append_path_element(path, "c", 1); +#ifdef TORRENT_WINDOWS + TEST_EQUAL(path, "c\\c"); +#else + TEST_EQUAL(path, "c/c"); +#endif + +} + +void test_torrent_parse() +{ + error_code ec; + // test torrent parsing entry info; @@ -644,7 +713,6 @@ void test_copy() TEST_EQUAL(b->files().hash(i), file_hashes[i]); } - } int test_main() @@ -657,6 +725,7 @@ int test_main() test_mutable_torrents(); #endif test_torrent_parse(); + test_sanitize_path(); return 0; }