]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3830] Addressed further review comments
authorThomas Markwalder <tmark@isc.org>
Mon, 28 Apr 2025 12:44:14 +0000 (08:44 -0400)
committerAndrei Pavel <andrei@isc.org>
Fri, 16 May 2025 09:20:42 +0000 (12:20 +0300)
    Minor cleanup and fixed hooks partesr UTs
modified:   src/lib/hooks/hooks_parser.h
modified:   src/lib/hooks/tests/hooks_manager_unittest.cc

src/lib/hooks/hooks_parser.h
src/lib/hooks/tests/hooks_manager_unittest.cc

index 28b6330e87c04171c95a6b2e339ad7aa10a72f0e..fb99add71f5c6764066ccbe799fc3bee4063b3e9 100644 (file)
@@ -61,22 +61,23 @@ public:
     /// @brief Validates a library path against the supported path for hooks libraries.
     ///
     /// @param libpath library path to validate.
-    /// @param enforce_path enables validation against the supported path.  If false
-    /// verifies only that the path contains a file name.
+    /// @param enforce_path enables validation against the supported path.
+    /// If false verifies only that the path contains a file name.
     ///
     /// @return validated path
-    static std::string validatePath(const std::string libpath, bool enforce_path = true);
+    static std::string validatePath(const std::string libpath,
+                                    bool enforce_path = true);
 
     /// @brief Fetches the supported Hooks path.
     ///
-    /// The first call to this function with no arguments will set the default 
+    /// The first call to this function with no arguments will set the default
     /// hooks path to either the value of DEFAULT_HOOKS_PATH or the environment
-    /// variable KEA_HOOKS_PATH if it is defined.  Subsequent calls with no 
+    /// variable KEA_HOOKS_PATH if it is defined.  Subsequent calls with no
     /// arguments will simply return this value.
-    /// 
+    ///
     /// @param reset recalculate when true, defaults to false. This is for
     /// testing purposes only.
-    /// @param explicit_path set default hooks path to this value. This is 
+    /// @param explicit_path set default hooks path to this value. This is
     /// for testing purposes only.
     ///
     /// @return String containing the default hooks path.
index ef7883621ca8ac5677b67512bd8eddea8af0ea1b..cd1d638fa172ecd6d0a4008f23eed79737d84dc4 100644 (file)
@@ -25,7 +25,7 @@
 #include <string>
 
 #include <unistd.h>
-#include <stdlib.h>
+#include <cstdlib>
 
 using namespace isc;
 using namespace isc::hooks;
@@ -1080,13 +1080,42 @@ TEST_F(HooksManagerTest, UnloadBeforeUnpark) {
     EXPECT_FALSE(unparked);
 }
 
-TEST(HooksParser, getHooksPath) {
+/// @brief Test fixture for hooks parsing.
+class HooksParserTest : public ::testing::Test {
+public:
+    /// @brief Constructor
+    HooksParserTest() {
+        // Save current value of the environment path.
+        char* env_path = std::getenv("KEA_HOOKS_PATH");
+        if (env_path) {
+            original_path_ = std::string(env_path);
+        }
+
+        // Clear the environment path.
+        unsetenv("KEA_HOOKS_PATH");
+    }
+
+    /// @brief Destructor
+    ~HooksParserTest() {
+        // Restore the original environment path.
+        if (!original_path_.empty()) {
+            setenv("KEA_HOOKS_PATH", original_path_.c_str(), 1);
+        } else {
+            unsetenv("KEA_HOOKS_PATH");
+        }
+    }
+
+    /// @brief Retains the environment variable's original value.
+    std::string original_path_;
+};
+
+TEST_F(HooksParserTest, getHooksPath) {
     ASSERT_FALSE(std::getenv("KEA_HOOKS_PATH"));
     auto hooks_path = HooksLibrariesParser::getHooksPath(true);
     EXPECT_EQ(hooks_path, DEFAULT_HOOKS_PATH);
 }
 
-TEST(HooksParser, getHooksPathWithEnv) {
+TEST_F(HooksParserTest, getHooksPathWithEnv) {
     std::string evar("KEA_HOOKS_PATH=/tmp");
     putenv(const_cast<char*>(evar.c_str()));
     ASSERT_TRUE(std::getenv("KEA_HOOKS_PATH"));
@@ -1094,13 +1123,13 @@ TEST(HooksParser, getHooksPathWithEnv) {
     EXPECT_EQ(hooks_path, "/tmp");
 }
 
-TEST(HooksParser, getHooksPathExplicit) {
+TEST_F(HooksParserTest, getHooksPathExplicit) {
     auto hooks_path = HooksLibrariesParser::getHooksPath(true, "/explicit/path");
     EXPECT_EQ(hooks_path, "/explicit/path");
 }
 
 // Verifies HooksParser::validatePath() when enforce_path is true.
-TEST(HooksParser, validatePathEnforcePath) {
+TEST_F(HooksParserTest, validatePathEnforcePath) {
     HooksLibrariesParser::getHooksPath(true);
     std::string def_path(HooksLibrariesParser::getHooksPath());
     struct Scenario {
@@ -1159,7 +1188,7 @@ TEST(HooksParser, validatePathEnforcePath) {
 }
 
 // Verifies HooksParser::validatePath() when enforce_path is false.
-TEST(HooksParser, validatePathEnforcePathFalse) {
+TEST_F(HooksParserTest, validatePathEnforcePathFalse) {
     HooksLibrariesParser::getHooksPath(true);
     std::string def_path(HooksLibrariesParser::getHooksPath());
     struct Scenario {