test: Always create unique tmpdirs

This ensures our tests always have clear temporary directories to store
their files.

Change-Id: Ib755c4a9407b1e1925b34ae1537eb234542133b0
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/subprojects/stdplus.wrap b/subprojects/stdplus.wrap
index 765ba71..d49e9a1 100644
--- a/subprojects/stdplus.wrap
+++ b/subprojects/stdplus.wrap
@@ -4,3 +4,5 @@
 
 [provide]
 stdplus = stdplus_dep
+stdplus-gtest = stdplus_gtest_dep
+program_names = run_with_tmp
diff --git a/test/meson.build b/test/meson.build
index 45f4072..b644bfa 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -27,6 +27,7 @@
   networkd_dep,
   gtest,
   gmock,
+  dependency('stdplus-gtest'),
 ]
 
 test_lib = static_library(
@@ -53,14 +54,18 @@
   'vlan_interface',
 ]
 
+run_with_tmp = find_program('run_with_tmp', native: true)
+
 foreach t : tests
   test(
     t,
-    executable(
+    run_with_tmp,
+    args: executable(
       t.underscorify(),
       'test_' + t + '.cpp',
       implicit_include_directories: false,
-      dependencies: test_dep))
+      dependencies: test_dep),
+    env: {'TMPTMPL': 'phosphor-networkd-test.XXXXXXXXXX'})
 endforeach
 
 if (get_option('hyp-nw-config') == true)
diff --git a/test/test_config_parser.cpp b/test/test_config_parser.cpp
index 61432c2..7866f87 100644
--- a/test/test_config_parser.cpp
+++ b/test/test_config_parser.cpp
@@ -2,10 +2,13 @@
 
 #include "config_parser.hpp"
 
+#include <fmt/format.h>
+
 #include <exception>
 #include <fstream>
 #include <phosphor-logging/elog-errors.hpp>
 #include <stdexcept>
+#include <stdplus/gtest/tmp.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 #include <gtest/gtest.h>
@@ -15,19 +18,19 @@
 namespace network
 {
 
-class TestConfigParser : public testing::Test
+class TestConfigParser : public stdplus::gtest::TestWithTmp
 {
   public:
     config::Parser parser;
     TestConfigParser()
     {
-        remove("/tmp/eth0.network");
-        std::ofstream filestream("/tmp/eth0.network");
+        auto filename = fmt::format("{}/eth0.network", CaseTmpDir());
+        std::ofstream filestream(filename);
 
         filestream << "[Match]\nName=eth0\n"
                    << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n";
         filestream.close();
-        parser.setFile("/tmp/eth0.network");
+        parser.setFile(filename);
     }
 
     bool isValueFound(const std::vector<std::string>& values,
@@ -79,7 +82,6 @@
     config::ValueList values;
     std::tie(rc, values) = parser.getValues("Network", "abc");
     EXPECT_EQ(config::ReturnCode::KEY_NOT_FOUND, rc);
-    remove("/tmp/eth0.network");
 }
 
 } // namespace network
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index 913b6e1..0846f7e 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -12,6 +12,7 @@
 #include <exception>
 #include <fstream>
 #include <sdbusplus/bus.hpp>
+#include <stdplus/gtest/tmp.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 #include <gtest/gtest.h>
@@ -21,35 +22,19 @@
 namespace network
 {
 
-class TestEthernetInterface : public testing::Test
+class TestEthernetInterface : public stdplus::gtest::TestWithTmp
 {
   public:
     sdbusplus::bus::bus bus;
+    std::string confDir;
     MockManager manager;
     MockEthernetInterface interface;
-    std::string confDir;
     TestEthernetInterface() :
-        bus(sdbusplus::bus::new_default()),
-        manager(bus, "/xyz/openbmc_test/network", "/tmp/"),
+        bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
+        manager(bus, "/xyz/openbmc_test/network", confDir),
         interface(makeInterface(bus, manager))
 
     {
-        setConfDir();
-    }
-
-    void setConfDir()
-    {
-        char tmp[] = "/tmp/EthernetInterface.XXXXXX";
-        confDir = mkdtemp(tmp);
-        manager.setConfDir(confDir);
-    }
-
-    ~TestEthernetInterface()
-    {
-        if (confDir != "")
-        {
-            fs::remove_all(confDir);
-        }
     }
 
     static constexpr ether_addr mac{0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index 48da2f2..76983ed 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -9,6 +9,7 @@
 #include <exception>
 #include <filesystem>
 #include <sdbusplus/bus.hpp>
+#include <stdplus/gtest/tmp.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 #include <gtest/gtest.h>
@@ -20,32 +21,15 @@
 
 namespace fs = std::filesystem;
 
-class TestNetworkManager : public testing::Test
+class TestNetworkManager : public stdplus::gtest::TestWithTmp
 {
   public:
     sdbusplus::bus::bus bus;
     MockManager manager;
-    std::string confDir;
     TestNetworkManager() :
         bus(sdbusplus::bus::new_default()),
-        manager(bus, "/xyz/openbmc_test/abc", "/tmp")
+        manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
     {
-        setConfDir();
-    }
-
-    ~TestNetworkManager()
-    {
-        if (confDir != "")
-        {
-            fs::remove_all(confDir);
-        }
-    }
-
-    void setConfDir()
-    {
-        char tmp[] = "/tmp/NetworkManager.XXXXXX";
-        confDir = mkdtemp(tmp);
-        manager.setConfDir(confDir);
     }
 
     void createInterfaces()
diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp
index c6a5f8f..51e7649 100644
--- a/test/test_rtnetlink.cpp
+++ b/test/test_rtnetlink.cpp
@@ -10,6 +10,7 @@
 #include <functional>
 #include <sdbusplus/bus.hpp>
 #include <sdeventplus/event.hpp>
+#include <testutil.hpp>
 
 #include <gtest/gtest.h>
 
@@ -23,43 +24,25 @@
 extern std::unique_ptr<Timer> refreshObjectTimer;
 EventPtr eventPtr = nullptr;
 
-class TestRtNetlink : public testing::Test
+class TestRtNetlink : public TestWithTmp
 {
 
   public:
-    std::string confDir;
     phosphor::Descriptor smartSock;
 
     TestRtNetlink()
     {
-        manager =
-            std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp");
+        manager = std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd",
+                                                CaseTmpDir());
         sd_event* events;
         sd_event_default(&events);
         eventPtr.reset(events);
         events = nullptr;
-        setConfDir();
         initializeTimers();
         createNetLinkSocket();
         bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
         rtnetlink::Server svr(eventPtr, smartSock);
     }
-
-    ~TestRtNetlink()
-    {
-        if (confDir.empty())
-        {
-            fs::remove_all(confDir);
-        }
-    }
-
-    void setConfDir()
-    {
-        char tmp[] = "/tmp/NetworkManager.XXXXXX";
-        confDir = mkdtemp(tmp);
-        manager->setConfDir(confDir);
-    }
-
     void createNetLinkSocket()
     {
         // RtnetLink socket
diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp
index f7e6f2b..2dd78b8 100644
--- a/test/test_vlan_interface.cpp
+++ b/test/test_vlan_interface.cpp
@@ -11,6 +11,7 @@
 #include <exception>
 #include <filesystem>
 #include <sdbusplus/bus.hpp>
+#include <stdplus/gtest/tmp.hpp>
 
 #include <gtest/gtest.h>
 
@@ -21,28 +22,19 @@
 
 namespace fs = std::filesystem;
 
-class TestVlanInterface : public testing::Test
+class TestVlanInterface : public stdplus::gtest::TestWithTmp
 {
   public:
     sdbusplus::bus::bus bus;
+    std::string confDir;
     MockManager manager;
     EthernetInterface interface;
-    std::string confDir;
     TestVlanInterface() :
-        bus(sdbusplus::bus::new_default()),
-        manager(bus, "/xyz/openbmc_test/network", "/tmp"),
+        bus(sdbusplus::bus::new_default()), confDir(CaseTmpDir()),
+        manager(bus, "/xyz/openbmc_test/network", confDir),
         interface(makeInterface(bus, manager))
 
     {
-        setConfDir();
-    }
-
-    ~TestVlanInterface()
-    {
-        if (confDir != "")
-        {
-            fs::remove_all(confDir);
-        }
     }
 
     static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
@@ -58,13 +50,6 @@
                 true};
     }
 
-    void setConfDir()
-    {
-        char tmp[] = "/tmp/VlanInterface.XXXXXX";
-        confDir = mkdtemp(tmp);
-        manager.setConfDir(confDir);
-    }
-
     void createVlan(VlanId id)
     {
         std::string ifname = "test0.";