]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4232] Removed more test files in host cache
authorFrancis Dupont <fdupont@isc.org>
Wed, 17 Dec 2025 20:32:31 +0000 (21:32 +0100)
committerFrancis Dupont <fdupont@isc.org>
Wed, 17 Dec 2025 21:39:18 +0000 (22:39 +0100)
src/hooks/dhcp/host_cache/tests/command_unittests.cc

index dfa08029af4c0bfffe389894a1457f802eb267ad..c84efb92d3791eeedb4a84f1c59d0cf4f9305f7e 100644 (file)
@@ -19,6 +19,7 @@
 #include <fstream>
 #include <functional>
 #include <iostream>
+#include <list>
 
 using namespace isc;
 using namespace isc::asiolink;
@@ -48,12 +49,16 @@ public:
 
         hcptr_.reset(new HostCache());
         MultiThreadingMgr::instance().setMode(false);
+        remove_on_exit_.push_back("dump.json");
+        remove_on_exit_.push_back("source.json");
     }
 
     /// @brief Destructor
     virtual ~CommandTest() {
-        ::remove("dump.json");
-        ::remove("source.json");
+        // Remove test files.
+        for (auto const& file : remove_on_exit_) {
+            ::remove(file.c_str());
+        }
 
         // Revert to original data directory.
         CfgMgr::instance().getDataDir(true, original_datadir_);
@@ -310,6 +315,9 @@ public:
 
     /// @brief Stores the pre-test DHCP data directory.
     std::string original_datadir_;
+
+    /// @brief List of test files to remove in the destructor.
+    std::list<std::string> remove_on_exit_;
 };
 
 /// @brief Size handler function
@@ -707,6 +715,7 @@ CommandTest::testWriteCommand() {
     handlerType write_handler = std::bind(&writeHandler, hcptr_, ph::_1);
     string file_txt = "dump.json";
     string full_file_txt = CfgMgr::instance().getDataDir() + "/" + file_txt;
+    remove_on_exit_.push_back(full_file_txt);
     string write_cmd =
         "{ \"command\": \"cache-write\", \"arguments\": \"" + file_txt + "\" }";
 
@@ -763,7 +772,6 @@ CommandTest::testWriteCommand() {
     exp_error += CfgMgr::instance().getDataDir();
     exp_error += "'";
     checkCommand(write_handler, badpath_cmd, 1, 1, exp_error);
-    ::remove(full_file_txt.c_str());
 }
 
 // Verifies that cache-write really writes the expected file.
@@ -773,6 +781,7 @@ CommandTest::testWriteCommandSecurityWarning() {
     handlerType write_handler = std::bind(&writeHandler, hcptr_, ph::_1);
     file::PathChecker::enableEnforcement(false);
     string badpath = "/tmp/kea-host-cache-test.txt";
+    remove_on_exit_.push_back(badpath);
     string write_cmd =
         "{ \"command\": \"cache-write\", \"arguments\": \"" + badpath + "\" }";
 
@@ -789,7 +798,6 @@ CommandTest::testWriteCommandSecurityWarning() {
         << " is NOT SECURE: invalid path specified: '/tmp', supported path is '"
         << CfgMgr::instance().getDataDir() << "'";
     EXPECT_EQ(1, countFile(oss.str()));
-    ::remove(badpath.c_str());
 }
 
 // Verifies that cache-load can load a dump file.