From 13e54a3da862d150f19d353c8cbe8ecb3a9dff76 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Fri, 19 Jan 2018 16:40:39 -0500 Subject: [PATCH] fixing warnings in tests code, part1 (#2700) --- test/test_ed25519.cpp | 4 ++-- test/test_remap_files.cpp | 4 ++++ test/test_remove_torrent.cpp | 2 +- test/test_span.cpp | 8 ++++++++ test/test_ssl.cpp | 10 +++++++--- test/test_string.cpp | 12 ++++++++---- test/test_threads.cpp | 4 ++++ test/test_time.cpp | 6 +++++- test/test_torrent_info.cpp | 10 +++++++--- test/test_upnp.cpp | 12 ++++++++---- test/test_utp.cpp | 4 ++++ test/test_xml.cpp | 4 ++++ 12 files changed, 62 insertions(+), 18 deletions(-) diff --git a/test/test_ed25519.cpp b/test/test_ed25519.cpp index 026f1ed0a..8e6565ad1 100644 --- a/test/test_ed25519.cpp +++ b/test/test_ed25519.cpp @@ -50,7 +50,7 @@ namespace secret_key sk; public_key pk; signature sig; - std::vector msg(int(message.size()) / 2); + std::vector msg(message.size() / 2); aux::from_hex(seed, s.data()); std::tie(pk, sk) = ed25519_create_keypair(s); @@ -208,7 +208,7 @@ TORRENT_TEST(create_seed) int n1 = 0; int n2 = 0; - for (int i = 0; i < 32; i++) + for (std::size_t i = 0; i < 32; i++) { if (s1[i] != 0) n1++; if (s2[i] != 0) n2++; diff --git a/test/test_remap_files.cpp b/test/test_remap_files.cpp index ae4138e2b..41918a9ce 100644 --- a/test/test_remap_files.cpp +++ b/test/test_remap_files.cpp @@ -47,6 +47,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; using std::ignore; +namespace { + bool all_of(std::vector const& v) { return std::all_of(v.begin(), v.end(), [](bool v){ return v; }); @@ -196,6 +198,8 @@ void test_remap_files(storage_mode_t storage_mode = storage_mode_sparse) TEST_CHECK(st.is_seeding); } +} // anonymous namespace + TORRENT_TEST(remap_files) { test_remap_files(); diff --git a/test/test_remove_torrent.cpp b/test/test_remove_torrent.cpp index 00419ca47..faf203490 100644 --- a/test/test_remove_torrent.cpp +++ b/test/test_remove_torrent.cpp @@ -94,7 +94,7 @@ void test_remove_torrent(remove_flags_t const remove_options if (test == partial_download) { - std::vector priorities(num_pieces, low_priority); + std::vector priorities(std::size_t(num_pieces), low_priority); // set half of the pieces to priority 0 std::fill(priorities.begin(), priorities.begin() + (num_pieces / 2), dont_download); tor2.prioritize_pieces(priorities); diff --git a/test/test_span.cpp b/test/test_span.cpp index fdc0bb3e5..1f4add6ea 100644 --- a/test/test_span.cpp +++ b/test/test_span.cpp @@ -41,9 +41,13 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + span f(span x) { return x; } span> g(span> x) { return x; } +} // anonymous namespace + TORRENT_TEST(span_vector) { std::vector v1 = {1,2,3,4}; @@ -52,6 +56,8 @@ TORRENT_TEST(span_vector) TEST_CHECK(a.size() == 4); } +namespace { + void do_span_temp_vector(span a) { std::vector v1 = {1,2,3,4}; @@ -59,6 +65,8 @@ void do_span_temp_vector(span a) TEST_CHECK(a.size() == 4); } +} // anonymous namespace + TORRENT_TEST(span_temp_vector) { do_span_temp_vector(std::vector{1,2,3,4}); diff --git a/test/test_ssl.cpp b/test/test_ssl.cpp index b65119af4..d72c9fbb3 100644 --- a/test/test_ssl.cpp +++ b/test/test_ssl.cpp @@ -60,6 +60,8 @@ using namespace std::placeholders; using namespace lt; using std::ignore; +namespace { + auto const alert_mask = alert::all_categories & ~alert::progress_notification & ~alert::stats_notification; @@ -233,7 +235,7 @@ void test_ssl(int test_idx, bool use_utp) std::printf("\n\n%s: ses1: connecting peer port: %d\n\n\n" , time_now_string(), port); tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec) - , port)); + , std::uint16_t(port))); const int timeout = 40; for (int i = 0; i < timeout; ++i) @@ -301,7 +303,7 @@ void test_ssl(int test_idx, bool use_utp) p2 = ses2.abort(); } -std::string password_callback(int length, boost::asio::ssl::context::password_purpose p +std::string password_callback(int /*length*/, boost::asio::ssl::context::password_purpose p , std::string pw) { if (p != boost::asio::ssl::context::for_reading) return ""; @@ -442,7 +444,7 @@ bool try_connect(lt::session& ses1, int port std::printf("connecting 127.0.0.1:%d\n", port); ssl_sock.lowest_layer().connect(tcp::endpoint( - address_v4::from_string("127.0.0.1"), port), ec); + address_v4::from_string("127.0.0.1"), std::uint16_t(port)), ec); print_alerts(ses1, "ses1", true, true, &on_alert); if (ec) @@ -599,6 +601,8 @@ void test_malicious_peer() TEST_EQUAL(success, attacks[i].expect); } } + +} // anonymous namespace #endif // TORRENT_USE_OPENSSL TORRENT_TEST(malicious_peer) diff --git a/test/test_string.cpp b/test/test_string.cpp index c679c51cc..af1ca7ad7 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -59,7 +59,7 @@ TORRENT_TEST(hex) TEST_CHECK(aux::from_hex({str, 40}, bin)); char hex[41]; aux::to_hex(bin, hex); - TEST_CHECK(strcmp(hex, str) == 0); + TEST_CHECK(std::strcmp(hex, str) == 0); TEST_CHECK(aux::to_hex({"\x55\x73",2}) == "5573"); TEST_CHECK(aux::to_hex({"\xaB\xd0",2}) == "abd0"); @@ -68,9 +68,9 @@ TORRENT_TEST(hex) for (int i = 1; i < 255; ++i) { - bool const hex = strchr(hex_chars, i) != nullptr; - char const c = i; - TEST_EQUAL(aux::is_hex(c), hex); + bool const hex_loop = std::strchr(hex_chars, i) != nullptr; + char const c = char(i); + TEST_EQUAL(aux::is_hex(c), hex_loop); } TEST_EQUAL(aux::hex_to_int('0'), 0); @@ -355,6 +355,8 @@ TORRENT_TEST(path) #endif } +namespace { + void test_parse_interface(char const* input , std::vector expected , std::string output) @@ -371,6 +373,8 @@ void test_parse_interface(char const* input TEST_EQUAL(print_listen_interfaces(list), output); } +} // anonymous namespace + TORRENT_TEST(parse_list) { std::vector list; diff --git a/test/test_threads.cpp b/test/test_threads.cpp index 0baa05918..438ed9e55 100644 --- a/test/test_threads.cpp +++ b/test/test_threads.cpp @@ -42,6 +42,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + void fun(std::condition_variable* s, std::mutex* m, int* waiting, int i) { std::printf("thread %d waiting\n", i); @@ -71,6 +73,8 @@ void decrement(std::condition_variable* s, std::mutex* m, int* waiting, std::ato --*c; } +} // anonymous namespace + TORRENT_TEST(threads) { std::condition_variable cond; diff --git a/test/test_time.cpp b/test/test_time.cpp index 10932df81..a3d2ae19c 100644 --- a/test/test_time.cpp +++ b/test/test_time.cpp @@ -41,6 +41,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + void check_timer_loop(std::mutex& m, time_point& last, std::condition_variable& cv) { std::unique_lock l(m); @@ -49,13 +51,15 @@ void check_timer_loop(std::mutex& m, time_point& last, std::condition_variable& for (int i = 0; i < 10000; ++i) { - std::lock_guard l(m); + std::lock_guard ll(m); time_point now = clock_type::now(); TEST_CHECK(now >= last); last = now; } } +} // anonymous namespace + TORRENT_TEST(time) { // make sure the time classes have correct semantics diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index 0cedf528d..53bf928f1 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -84,6 +84,8 @@ TORRENT_TEST(mutable_torrents) } #endif +namespace { + struct test_torrent_t { char const* file; @@ -163,6 +165,8 @@ test_failing_torrent_t test_error_torrents[] = { "no_files.torrent", errors::no_files_in_torrent}, }; +} // anonymous namespace + // TODO: test remap_files // TODO: merkle torrents. specifically torrent_info::add_merkle_nodes and torrent with "root hash" // TODO: torrent with 'p' (padfile) attribute @@ -666,9 +670,9 @@ TORRENT_TEST(parse_torrents) std::vector buf; bencode(std::back_inserter(buf), torrent); - torrent_info ti(buf, from_span); - std::cout << ti.name() << std::endl; - TEST_CHECK(ti.name() == "test1"); + torrent_info ti1(buf, from_span); + std::cout << ti1.name() << std::endl; + TEST_CHECK(ti1.name() == "test1"); #ifdef TORRENT_WINDOWS info["name.utf-8"] = "c:/test1/test2/test3"; diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 5800eb672..81f5d3441 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -46,6 +46,8 @@ using namespace lt; using lt::portmap_protocol; +namespace { + broadcast_socket* sock = nullptr; int g_port = 0; @@ -115,14 +117,14 @@ struct callback_info std::list callbacks; -namespace +namespace // TODO: remove this nested namespace { struct upnp_callback : aux::portmap_callback { void on_port_mapping(port_mapping_t const mapping , address const& ip, int port , portmap_protocol const protocol, error_code const& err - , portmap_transport const transport) override + , portmap_transport) override { callback_info info = {mapping, port, err}; callbacks.push_back(info); @@ -132,12 +134,12 @@ namespace << ", error: \"" << err.message() << "\"\n"; } #ifndef TORRENT_DISABLE_LOGGING - virtual bool should_log_portmap(portmap_transport transport) const override + virtual bool should_log_portmap(portmap_transport) const override { return true; } - virtual void log_portmap(portmap_transport transport, char const* msg) const override + virtual void log_portmap(portmap_transport, char const* msg) const override { std::cout << "UPnP: " << msg << std::endl; //TODO: store the log and verify that some key messages are there @@ -253,6 +255,8 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con delete sock; } +} // anonymous namespace + TORRENT_TEST(upnp) { run_upnp_test(combine_path("..", "root1.xml").c_str(), "Xtreme N GIGABIT Router", "wipconn", 1); diff --git a/test/test_utp.cpp b/test/test_utp.cpp index 7e69d1282..d9cf8d13f 100644 --- a/test/test_utp.cpp +++ b/test/test_utp.cpp @@ -47,6 +47,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + void test_transfer() { // in case the previous run was terminated @@ -132,6 +134,8 @@ void test_transfer() p2 = ses2.abort(); } +} // anonymous namespace + TORRENT_TEST(utp) { test_transfer(); diff --git a/test/test_xml.cpp b/test/test_xml.cpp index 0047cf4b2..ccdd5e5ab 100644 --- a/test/test_xml.cpp +++ b/test/test_xml.cpp @@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +namespace { + char upnp_xml[] = "" "" @@ -306,6 +308,8 @@ void test_parse(char const* in, char const* expected) TEST_EQUAL(out, expected); } +} // anonymous namespace + TORRENT_TEST(upnp_parser1) { parse_state xml_s;