From 152874af8c226ec0e3a0f286a4c04b7b52d5cf0d Mon Sep 17 00:00:00 2001
From: Davide Pesavento <davidepesa@gmail.com>
Date: Tue, 20 Feb 2024 22:07:07 -0500
Subject: [PATCH] tests: remove temporary directory on setup and teardown

Change-Id: I012b11a9d6a46769dc5f6e388ab6bd3f2644607e
---
 daemon/face/unix-stream-channel.cpp           |  4 ++-
 daemon/face/unix-stream-factory.cpp           |  2 +-
 daemon/mgmt/command-authenticator.cpp         |  5 ++--
 tests/daemon/face/unix-stream-channel.t.cpp   | 11 ++-----
 .../face/unix-stream-transport-fixture.hpp    |  2 +-
 tests/global-configuration.cpp                | 29 +++++++++++--------
 tests/key-chain-fixture.cpp                   |  4 +--
 tests/wscript                                 |  2 +-
 8 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index a1abe9c4..2b09ed43 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -29,7 +29,9 @@
 #include "unix-stream-transport.hpp"
 #include "common/global.hpp"
 
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/exception.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
 
 namespace nfd::face {
 
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index 27719eb4..a1cc2fe1 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -25,7 +25,7 @@
 
 #include "unix-stream-factory.hpp"
 
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
 
 namespace nfd::face {
 
diff --git a/daemon/mgmt/command-authenticator.cpp b/daemon/mgmt/command-authenticator.cpp
index 9d327a0c..1f1b0362 100644
--- a/daemon/mgmt/command-authenticator.cpp
+++ b/daemon/mgmt/command-authenticator.cpp
@@ -26,15 +26,16 @@
 #include "command-authenticator.hpp"
 #include "common/logger.hpp"
 
-#include <ndn-cxx/tag.hpp>
 #include <ndn-cxx/security/certificate-fetcher-offline.hpp>
 #include <ndn-cxx/security/certificate-request.hpp>
 #include <ndn-cxx/security/validation-policy.hpp>
 #include <ndn-cxx/security/validation-policy-accept-all.hpp>
 #include <ndn-cxx/security/validation-policy-command-interest.hpp>
+#include <ndn-cxx/tag.hpp>
 #include <ndn-cxx/util/io.hpp>
 
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
 
 namespace security = ndn::security;
 
diff --git a/tests/daemon/face/unix-stream-channel.t.cpp b/tests/daemon/face/unix-stream-channel.t.cpp
index a767d231..605597d4 100644
--- a/tests/daemon/face/unix-stream-channel.t.cpp
+++ b/tests/daemon/face/unix-stream-channel.t.cpp
@@ -42,17 +42,12 @@ class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_s
   UnixStreamChannelFixture()
   {
     listenerEp = unix_stream::Endpoint(socketPath.string());
-
-    // in case an earlier test run crashed without a chance to run the destructor
-    boost::system::error_code ec;
-    fs::remove_all(testDir, ec);
   }
 
   ~UnixStreamChannelFixture() override
   {
-    // cleanup
     boost::system::error_code ec;
-    fs::remove_all(testDir, ec);
+    fs::remove_all(testDir, ec); // ignore error
   }
 
   shared_ptr<UnixStreamChannel>
@@ -86,8 +81,8 @@ class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_s
   }
 
 protected:
-  fs::path testDir = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel";
-  fs::path socketPath = testDir / "test" / "foo.sock";
+  static inline const fs::path testDir = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel";
+  static inline const fs::path socketPath = testDir / "test" / "foo.sock";
 };
 
 BOOST_AUTO_TEST_SUITE(Face)
diff --git a/tests/daemon/face/unix-stream-transport-fixture.hpp b/tests/daemon/face/unix-stream-transport-fixture.hpp
index 253e05c2..8aca3dab 100644
--- a/tests/daemon/face/unix-stream-transport-fixture.hpp
+++ b/tests/daemon/face/unix-stream-transport-fixture.hpp
@@ -34,7 +34,7 @@
 #include "tests/daemon/face/dummy-link-service.hpp"
 #include "tests/daemon/face/transport-test-common.hpp"
 
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
 
 namespace nfd::tests {
 
diff --git a/tests/global-configuration.cpp b/tests/global-configuration.cpp
index dfae750e..402dcf8f 100644
--- a/tests/global-configuration.cpp
+++ b/tests/global-configuration.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -23,12 +23,14 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "core/common.hpp"
-
 #include "tests/boost-test.hpp"
 
-#include <boost/filesystem.hpp>
-#include <fstream>
+#include <ndn-cxx/util/exception.hpp>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
+#include <stdexcept>
 #include <stdlib.h>
 
 namespace nfd::tests {
@@ -40,17 +42,16 @@ class GlobalConfiguration
   {
     const char* envHome = ::getenv("HOME");
     if (envHome)
-      m_home = envHome;
+      m_home.assign(envHome);
 
-    auto testHome = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-home";
-    if (::setenv("HOME", testHome.c_str(), 1) != 0)
-      NDN_THROW(std::runtime_error("setenv() failed"));
+    // in case an earlier test run crashed without a chance to run the destructor
+    boost::filesystem::remove_all(TESTDIR);
 
+    auto testHome = TESTDIR / "test-home";
     boost::filesystem::create_directories(testHome);
 
-    std::ofstream clientConf((testHome / ".ndn" / "client.conf").c_str());
-    clientConf << "pib=pib-sqlite3" << std::endl
-               << "tpm=tpm-file" << std::endl;
+    if (::setenv("HOME", testHome.c_str(), 1) != 0)
+      NDN_THROW_NO_STACK(std::runtime_error("setenv() failed"));
   }
 
   ~GlobalConfiguration() noexcept
@@ -59,9 +60,13 @@ class GlobalConfiguration
       ::unsetenv("HOME");
     else
       ::setenv("HOME", m_home.data(), 1);
+
+    boost::system::error_code ec;
+    boost::filesystem::remove_all(TESTDIR, ec); // ignore error
   }
 
 private:
+  static inline const boost::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR};
   std::string m_home;
 };
 
diff --git a/tests/key-chain-fixture.cpp b/tests/key-chain-fixture.cpp
index 21a6b9f7..57bb0577 100644
--- a/tests/key-chain-fixture.cpp
+++ b/tests/key-chain-fixture.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2022,  Regents of the University of California,
+ * Copyright (c) 2014-2024,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -27,7 +27,7 @@
 
 #include <ndn-cxx/util/io.hpp>
 
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/operations.hpp>
 
 namespace nfd::tests {
 
diff --git a/tests/wscript b/tests/wscript
index 956fd0d4..4092db1f 100644
--- a/tests/wscript
+++ b/tests/wscript
@@ -28,7 +28,7 @@ top = '..'
 def build(bld):
     # Unit tests
     if bld.env.WITH_TESTS:
-        tmpdir = 'UNIT_TESTS_TMPDIR="%s"' % bld.bldnode.make_node('tmp-files')
+        tmpdir = 'UNIT_TESTS_TMPDIR="%s"' % bld.bldnode.make_node('tests-tmp')
 
         # common test objects
         bld.objects(