File 0002-Fixed-DhtController-m_router-check-and-log-unit-test.patch of Package libtorrent

From 01fc34516ef9fcff3cffc9d224c6870a8eb7eff6 Mon Sep 17 00:00:00 2001
From: rakshasa <sundell.software@gmail.com>
Date: Sat, 29 Mar 2025 07:32:25 +0100
Subject: [PATCH 2/2] Fixed DhtController::m_router check and log unit tests.

---
 src/torrent/tracker/dht_controller.cc | 16 +++++++++--
 src/torrent/utils/log.cc              | 26 +++++++++---------
 src/torrent/utils/log_buffer.h        |  8 +++---
 test/Makefile.am                      |  3 +++
 test/helpers/progress_listener.cc     |  2 ++
 test/helpers/test_fixture.cc          | 38 ++++++++++++++-------------
 test/torrent/utils/test_log.cc        | 12 ++++-----
 test/torrent/utils/test_log_buffer.cc |  2 +-
 8 files changed, 63 insertions(+), 44 deletions(-)

diff --git a/src/torrent/tracker/dht_controller.cc b/src/torrent/tracker/dht_controller.cc
index 2d0afcf6..c43e1dd7 100644
--- a/src/torrent/tracker/dht_controller.cc
+++ b/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 a/src/torrent/utils/log.cc b/src/torrent/utils/log.cc
index a587cdfe..1c7a4362 100644
--- a/src/torrent/utils/log.cc
+++ b/src/torrent/utils/log.cc
@@ -45,12 +45,12 @@ struct log_gz_output {
   gzFile gz_file;
 };
 
-typedef std::vector<log_cache_entry>                   log_cache_list;
-typedef std::vector<std::pair<int, int> >              log_child_list;
-typedef std::vector<log_slot>                          log_slot_list;
-typedef std::vector<std::pair<std::string, log_slot> > log_output_list;
+typedef std::vector<log_cache_entry>                  log_cache_list;
+typedef std::vector<std::pair<int, int>>              log_child_list;
+typedef std::vector<log_slot>                         log_slot_list;
+typedef std::vector<std::pair<std::string, log_slot>> 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<log_gz_output>& 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<log_gz_output>& 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 a/src/torrent/utils/log_buffer.h b/src/torrent/utils/log_buffer.h
index 025192c1..41f9d5ef 100644
--- a/src/torrent/utils/log_buffer.h
+++ b/src/torrent/utils/log_buffer.h
@@ -8,6 +8,8 @@
 #include <string>
 #include <utility>
 
+#include <torrent/common.h>
+
 namespace torrent {
 
 struct log_entry {
@@ -23,7 +25,7 @@ struct log_entry {
   std::string message;
 };
 
-class [[gnu::visibility("default")]] log_buffer : private std::deque<log_entry> {
+class LIBTORRENT_EXPORT log_buffer : private std::deque<log_entry> {
 public:
   typedef std::deque<log_entry>  base_type;
   typedef std::function<void ()> 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, std::function<void (log_buffer*)>> 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 a/test/Makefile.am b/test/Makefile.am
index d0d98da9..87ae61d2 100644
--- a/test/Makefile.am
+++ b/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 a/test/helpers/progress_listener.cc b/test/helpers/progress_listener.cc
index 7a6ed047..e7f000fc 100644
--- a/test/helpers/progress_listener.cc
+++ b/test/helpers/progress_listener.cc
@@ -10,6 +10,8 @@
 #include "torrent/utils/log.h"
 #include "torrent/utils/log_buffer.h"
 
+#include <iostream>
+
 static std::string
 get_test_path(const test_list_type& tl) {
   if (tl.size() < 2)
diff --git a/test/helpers/test_fixture.cc b/test/helpers/test_fixture.cc
index 4d8d7214..3d766ea0 100644
--- a/test/helpers/test_fixture.cc
+++ b/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 <iostream>
+
+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 a/test/torrent/utils/test_log.cc b/test/torrent/utils/test_log.cc
index 8ab8ed87..214866a8 100644
--- a/test/torrent/utils/test_log.cc
+++ b/test/torrent/utils/test_log.cc
@@ -9,8 +9,8 @@
 #include <functional>
 #include <iostream>
 
-#include <torrent/exceptions.h>
-#include <torrent/utils/log.h>
+#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 a/test/torrent/utils/test_log_buffer.cc b/test/torrent/utils/test_log_buffer.cc
index 58412750..60732273 100644
--- a/test/torrent/utils/test_log_buffer.cc
+++ b/test/torrent/utils/test_log_buffer.cc
@@ -3,7 +3,7 @@
 #include "test_log_buffer.h"
 
 #include "globals.h"
-#include <torrent/utils/log_buffer.h>
+#include "torrent/utils/log_buffer.h"
 
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_log_buffer, "torrent/utils");
 
-- 
2.49.0

openSUSE Build Service is sponsored by