Skip to content

Commit

Permalink
Fix hardcoded paths in testcases
Browse files Browse the repository at this point in the history
I share my tmpdir with Portage that runs testcases with its own user
account. If there are hardcoded paths I run into permissions issues when
I want to run the testcases.
  • Loading branch information
quinox authored and halfgaar committed Jul 10, 2024
1 parent 13a44a1 commit 30983b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
7 changes: 4 additions & 3 deletions FlashMQTests/flashmqtempdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

#include <sys/types.h>
#include <unistd.h>
#include <filesystem>

#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<char> nameBuf(templateName.size() + 1, 0);
std::copy(templateName.begin(), templateName.end(), nameBuf.begin());
this->path = std::string(mkdtemp(nameBuf.data()));
}

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...
Expand All @@ -26,7 +27,7 @@ FlashMQTempDir::~FlashMQTempDir()
}
}

const std::string &FlashMQTempDir::getPath() const
const std::filesystem::path &FlashMQTempDir::getPath() const
{
return this->path;
}
5 changes: 3 additions & 2 deletions FlashMQTests/flashmqtempdir.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#ifndef FLASHMQTEMPDIR_H
#define FLASHMQTEMPDIR_H

#include <filesystem>
#include <stdlib.h>
#include <string>
#include <vector>

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
26 changes: 18 additions & 8 deletions FlashMQTests/tst_maintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<RetainedMessage> messagesLoaded = db2.readData();
db2.closeFile();
Expand Down Expand Up @@ -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<RetainedMessage> messagesLoaded = db2.readData();
db2.closeFile();
Expand All @@ -898,13 +903,15 @@ void MainTests::testRetainedMessageDBEmptyList()
try
{
std::vector<RetainedMessage> 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<RetainedMessage> messagesLoaded = db2.readData();
db2.closeFile();
Expand Down Expand Up @@ -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<SubscriptionStore> store2(new SubscriptionStore());
store2->loadSessionsAndSubscriptions("/tmp/flashmqtests_sessions.db");
store2->loadSessionsAndSubscriptions(dbpath);

MYCASTCOMPARE(store->sessionsById.size(), 2);
MYCASTCOMPARE(store2->sessionsById.size(), 2);
Expand Down

0 comments on commit 30983b5

Please sign in to comment.