commit 01fc34516ef9fcff3cffc9d224c6870a8eb7eff6 Author: rakshasa Date: Sat Mar 29 07:32:25 2025 +0100 Fixed DhtController::m_router check and log unit tests. diff --git src/torrent/tracker/dht_controller.cc src/torrent/tracker/dht_controller.cc index 2d0afcf6..c43e1dd7 100644 --- src/torrent/tracker/dht_controller.cc +++ src/torrent/tracker/dht_controller.cc @@ -101,13 +101,13 @@ DhtController::set_receive_requests(bool state) { void DhtController::add_node(const sockaddr* sa, int port) { - if (!m_router) + if (m_router) m_router->contact(sa, port); } void DhtController::add_node(const std::string& host, int port) { - if (!m_router) + if (m_router) m_router->add_contact(host, port); } @@ -121,11 +121,17 @@ DhtController::store_cache(Object* container) { DhtController::statistics_type DhtController::get_statistics() const { + if (!m_router) + throw internal_error("DhtController::get_statistics called but DHT not initialized."); + return m_router->get_statistics(); } void DhtController::reset_statistics() { + if (!m_router) + throw internal_error("DhtController::reset_statistics called but DHT not initialized."); + m_router->reset_statistics(); } @@ -133,6 +139,9 @@ DhtController::reset_statistics() { void DhtController::set_upload_throttle(Throttle* t) { + if (!m_router) + throw internal_error("DhtController::set_upload_throttle() called but DHT not initialized."); + if (m_router->is_active()) throw internal_error("DhtController::set_upload_throttle() called while DHT server active."); @@ -141,6 +150,9 @@ DhtController::set_upload_throttle(Throttle* t) { void DhtController::set_download_throttle(Throttle* t) { + if (!m_router) + throw internal_error("DhtController::set_download_throttle() called but DHT not initialized."); + if (m_router->is_active()) throw internal_error("DhtController::set_download_throttle() called while DHT server active."); diff --git src/torrent/utils/log.cc src/torrent/utils/log.cc index a587cdfe..1c7a4362 100644 --- src/torrent/utils/log.cc +++ src/torrent/utils/log.cc @@ -45,12 +45,12 @@ struct log_gz_output { gzFile gz_file; }; -typedef std::vector log_cache_list; -typedef std::vector > log_child_list; -typedef std::vector log_slot_list; -typedef std::vector > log_output_list; +typedef std::vector log_cache_list; +typedef std::vector> log_child_list; +typedef std::vector log_slot_list; +typedef std::vector> log_output_list; -log_output_list log_outputs; +log_output_list log_outputs LIBTORRENT_EXPORT; log_child_list log_children; log_cache_list log_cache; log_group_list log_groups; @@ -105,10 +105,10 @@ log_rebuild_cache() { continue; } - log_cache_list::iterator cache_itr = + log_cache_list::iterator cache_itr = std::find_if(log_cache.begin(), log_cache.end(), std::bind(&log_cache_entry::equal_outputs, std::placeholders::_1, use_outputs)); - + if (cache_itr == log_cache.end()) { cache_itr = log_cache.insert(log_cache.end(), log_cache_entry()); cache_itr->outputs = use_outputs; @@ -282,13 +282,11 @@ log_add_group_output(int group, const char* name) { log_output_list::iterator itr = log_find_output_name(name); size_t index = std::distance(log_outputs.begin(), itr); - if (itr == log_outputs.end()) { - throw input_error("Log name not found."); - } + if (itr == log_outputs.end()) + throw input_error("Log name not found: '" + std::string(name) + "'"); - if (index >= log_group::max_size_outputs()) { + if (index >= log_group::max_size_outputs()) throw input_error("Cannot add more log group outputs."); - } log_groups[group].set_output_at(index, true); log_rebuild_cache(); @@ -346,7 +344,7 @@ log_gz_file_write(std::shared_ptr& outfile, const char* data, siz int buffer_length = snprintf(buffer, 64, GROUPFMT, cachedTime.seconds(), log_level_char[group % 6]); - + if (buffer_length > 0) gzwrite(outfile->gz_file, buffer, buffer_length); @@ -355,7 +353,7 @@ log_gz_file_write(std::shared_ptr& outfile, const char* data, siz } else if (group == -1) { gzwrite(outfile->gz_file, "---DUMP---\n", sizeof("---DUMP---\n") - 1); - + if (length != 0) gzwrite(outfile->gz_file, data, length); diff --git src/torrent/utils/log_buffer.h src/torrent/utils/log_buffer.h index 025192c1..41f9d5ef 100644 --- src/torrent/utils/log_buffer.h +++ src/torrent/utils/log_buffer.h @@ -8,6 +8,8 @@ #include #include +#include + namespace torrent { struct log_entry { @@ -23,7 +25,7 @@ struct log_entry { std::string message; }; -class [[gnu::visibility("default")]] log_buffer : private std::deque { +class LIBTORRENT_EXPORT log_buffer : private std::deque { public: typedef std::deque base_type; typedef std::function slot_void; @@ -47,7 +49,7 @@ public: m_max_size(200) {} unsigned int max_size() const { return m_max_size; } - + // Always lock before calling any function. void lock() { m_lock.lock(); } void unlock() { m_lock.unlock(); } @@ -66,7 +68,7 @@ private: typedef std::unique_ptr> log_buffer_ptr; -[[gnu::visibility("default")]] log_buffer_ptr log_open_log_buffer(const char* name); +log_buffer_ptr log_open_log_buffer(const char* name) LIBTORRENT_EXPORT; } diff --git test/Makefile.am test/Makefile.am index d0d98da9..87ae61d2 100644 --- test/Makefile.am +++ test/Makefile.am @@ -9,6 +9,9 @@ TESTS = \ check_PROGRAMS = $(TESTS) +# This can cause duplicate symbols, so export anything that causes issues. + +# LibTorrent_Test_LDADD = ../src/libtorrent.la LibTorrent_Test_LDADD = \ ../src/libtorrent.la \ ../src/libtorrent_other.la \ diff --git test/helpers/progress_listener.cc test/helpers/progress_listener.cc index 7a6ed047..e7f000fc 100644 --- test/helpers/progress_listener.cc +++ test/helpers/progress_listener.cc @@ -10,6 +10,8 @@ #include "torrent/utils/log.h" #include "torrent/utils/log_buffer.h" +#include + static std::string get_test_path(const test_list_type& tl) { if (tl.size() < 2) diff --git test/helpers/test_fixture.cc test/helpers/test_fixture.cc index 4d8d7214..3d766ea0 100644 --- test/helpers/test_fixture.cc +++ test/helpers/test_fixture.cc @@ -1,18 +1,20 @@ -#include "config.h" - -#include "test_fixture.h" - -#include "torrent/utils/log.h" - -void -test_fixture::setUp() { - mock_init(); - - log_add_group_output(torrent::LOG_CONNECTION_BIND, "test_output"); - log_add_group_output(torrent::LOG_CONNECTION_FD, "test_output"); -} - -void -test_fixture::tearDown() { - mock_cleanup(); -} +#include "config.h" + +#include "test_fixture.h" + +#include "torrent/utils/log.h" + +#include + +void +test_fixture::setUp() { + mock_init(); + + log_add_group_output(torrent::LOG_CONNECTION_BIND, "test_output"); + log_add_group_output(torrent::LOG_CONNECTION_FD, "test_output"); +} + +void +test_fixture::tearDown() { + mock_cleanup(); +} diff --git test/torrent/utils/test_log.cc test/torrent/utils/test_log.cc index 8ab8ed87..214866a8 100644 --- test/torrent/utils/test_log.cc +++ test/torrent/utils/test_log.cc @@ -9,8 +9,8 @@ #include #include -#include -#include +#include "torrent/exceptions.h" +#include "torrent/utils/log.h" CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_log, "torrent/utils"); @@ -97,12 +97,12 @@ test_log::test_print() { open_output("test_print_1", 0x1); open_output("test_print_2", 0x2); torrent::log_add_group_output(0, "test_print_1"); - + LTUNIT_ASSERT_OUTPUT(0, 0x1, "foo_bar", "foo_bar"); LTUNIT_ASSERT_OUTPUT(0, 0x1, "foo 123 bar", "foo %i %s", 123, "bar"); torrent::log_add_group_output(0, "test_print_2"); - + LTUNIT_ASSERT_OUTPUT(0, 0x1|0x2, "test_multiple", "test_multiple"); } @@ -144,7 +144,7 @@ test_log::test_file_output() { torrent::log_open_file_output("test_file", filename.c_str()); torrent::log_add_group_output(GROUP_PARENT_1, "test_file"); - + lt_log_print(GROUP_PARENT_1, "test_file"); torrent::log_cleanup(); // To ensure we flush the buffers. @@ -152,7 +152,7 @@ test_log::test_file_output() { std::ifstream temp_file(filename.c_str()); CPPUNIT_ASSERT(temp_file.good()); - + char buffer[256]; temp_file.getline(buffer, 256); diff --git test/torrent/utils/test_log_buffer.cc test/torrent/utils/test_log_buffer.cc index 58412750..60732273 100644 --- test/torrent/utils/test_log_buffer.cc +++ test/torrent/utils/test_log_buffer.cc @@ -3,7 +3,7 @@ #include "test_log_buffer.h" #include "globals.h" -#include +#include "torrent/utils/log_buffer.h" CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_log_buffer, "torrent/utils");