diff --git a/FlashMQTests/flashmqtempdir.cpp b/FlashMQTests/flashmqtempdir.cpp index 0638f79..a723c40 100644 --- a/FlashMQTests/flashmqtempdir.cpp +++ b/FlashMQTests/flashmqtempdir.cpp @@ -2,12 +2,13 @@ #include #include +#include #include "utils.h" FlashMQTempDir::FlashMQTempDir() { - const std::string templateName("/tmp/flashmq_storage_test_XXXXXX"); + const std::string templateName(std::filesystem::temp_directory_path() / "flashmq_test_XXXXXX"); std::vector nameBuf(templateName.size() + 1, 0); std::copy(templateName.begin(), templateName.end(), nameBuf.begin()); this->path = std::string(mkdtemp(nameBuf.data())); @@ -15,7 +16,7 @@ FlashMQTempDir::FlashMQTempDir() FlashMQTempDir::~FlashMQTempDir() { - if (this->path.empty() || !strContains(this->path, "flashmq_storage_test")) + if (this->path.empty() || !strContains(this->path, "flashmq_test_")) return; // Not pretty, but whatever works... @@ -26,7 +27,7 @@ FlashMQTempDir::~FlashMQTempDir() } } -const std::string &FlashMQTempDir::getPath() const +const std::filesystem::path &FlashMQTempDir::getPath() const { return this->path; } diff --git a/FlashMQTests/flashmqtempdir.h b/FlashMQTests/flashmqtempdir.h index 09cabd9..c5b8714 100644 --- a/FlashMQTests/flashmqtempdir.h +++ b/FlashMQTests/flashmqtempdir.h @@ -1,18 +1,19 @@ #ifndef FLASHMQTEMPDIR_H #define FLASHMQTEMPDIR_H +#include #include #include #include class FlashMQTempDir { - std::string path; + std::filesystem::path path; public: FlashMQTempDir(); ~FlashMQTempDir(); - const std::string &getPath() const; + const std::filesystem::path &getPath() const; }; #endif // FLASHMQTEMPDIR_H diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 202906d..377d9e0 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -25,6 +25,7 @@ See LICENSE for license details. #include "retainedmessagesdb.h" #include "utils.h" #include "exceptions.h" +#include "flashmqtempdir.h" void MainTests::test_circbuf() { @@ -761,7 +762,8 @@ void MainTests::test_utf8_compare_implementation() // Just something to look at. It prefixes lines with a red cross when the checker returns false. Note that this means you // don't see the difference between invalid UTF8 and valid but invalid for MQTT. - std::ofstream outfile("/tmp/flashmq_utf8_test_result.txt", std::ios::binary); + FlashMQTempDir tmpdir; + std::ofstream outfile(tmpdir.getPath() / "flashmq_utf8_test_result.txt", std::ios::binary); int line_count = 0; std::ifstream infile("UTF-8-test.txt", std::ios::binary); @@ -831,12 +833,14 @@ void MainTests::testRetainedMessageDB() rm.publish.username = formatString("Username__%d", usernameCount++); } - RetainedMessagesDB db("/tmp/flashmqtests_retained.db"); + FlashMQTempDir tmpdir; + auto dbpath = tmpdir.getPath() / "flashmqtests_retained.db"; + RetainedMessagesDB db(dbpath); db.openWrite(); db.saveData(messages); db.closeFile(); - RetainedMessagesDB db2("/tmp/flashmqtests_retained.db"); + RetainedMessagesDB db2(dbpath); db2.openRead(); std::list messagesLoaded = db2.readData(); db2.closeFile(); @@ -874,7 +878,8 @@ void MainTests::testRetainedMessageDBNotPresent() { try { - RetainedMessagesDB db2("/tmp/flashmqtests_asdfasdfasdf.db"); + FlashMQTempDir tmpdir; + RetainedMessagesDB db2(tmpdir.getPath() / "flashmqtests_asdfasdfasdf.db"); db2.openRead(); std::list messagesLoaded = db2.readData(); db2.closeFile(); @@ -898,13 +903,15 @@ void MainTests::testRetainedMessageDBEmptyList() try { std::vector messages; + FlashMQTempDir tmpdir; + std::string dbpath = tmpdir.getPath() / "flashmqtests_retained.db"; - RetainedMessagesDB db("/tmp/flashmqtests_retained.db"); + RetainedMessagesDB db(dbpath); db.openWrite(); db.saveData(messages); db.closeFile(); - RetainedMessagesDB db2("/tmp/flashmqtests_retained.db"); + RetainedMessagesDB db2(dbpath); db2.openRead(); std::list messagesLoaded = db2.readData(); db2.closeFile(); @@ -975,12 +982,15 @@ void MainTests::testSavingSessions() PublishCopyFactory fac(&publishPacket); c1ses->writePacket(fac, 1, false); - store->saveSessionsAndSubscriptions("/tmp/flashmqtests_sessions.db"); + FlashMQTempDir tmpdir; + auto dbpath = tmpdir.getPath() / "flashmqtests_sessions.db"; + + store->saveSessionsAndSubscriptions(dbpath); usleep(1000000); std::shared_ptr store2(new SubscriptionStore()); - store2->loadSessionsAndSubscriptions("/tmp/flashmqtests_sessions.db"); + store2->loadSessionsAndSubscriptions(dbpath); MYCASTCOMPARE(store->sessionsById.size(), 2); MYCASTCOMPARE(store2->sessionsById.size(), 2);