1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

added test utilities

This commit is contained in:
Falk Werner 2022-12-30 19:43:45 +01:00
parent 60594067b1
commit 9036aba41b
5 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,23 @@
#include "webfuse/test/tempdir.hpp"
#include <unistd.h>
namespace webfuse
{
tempdir::tempdir()
{
char path_template[] = "/tmp/webfuse_test_XXXXXX";
path = mkdtemp(path_template);
}
tempdir::~tempdir()
{
unlink(path.c_str());
}
std::string const tempdir::name() const
{
return path;
}
}

View File

@ -0,0 +1,22 @@
#ifndef WEBFUSE_TEMPDIR_HPP
#define WEBFUSE_TEMPDIR_HPP
#include <string>
namespace webfuse
{
class tempdir
{
public:
tempdir();
~tempdir();
std::string const name() const;
private:
std::string path;
};
}
#endif

View File

@ -0,0 +1,39 @@
#include "webfuse/test/thread.hpp"
#include <csignal>
namespace
{
extern "C" void * webfuse_thread_main(void * args)
{
auto * run = reinterpret_cast<std::function<void(void)> *>(args);
(*run)();
return nullptr;
}
}
namespace webfuse
{
thread::thread(std::function<void(void)> run)
{
pthread_create(&real_thread, nullptr,
&webfuse_thread_main,
reinterpret_cast<void*>(&run));
}
thread::~thread()
{
pthread_join(real_thread, nullptr);
}
void thread::kill(int signal_id)
{
pthread_kill(real_thread, signal_id);
}
}

View File

@ -0,0 +1,22 @@
#ifndef WEBFUSE_THREAD_HPP
#define WEBFUSE_THREAD_HPP
#include <pthread.h>
#include <functional>
namespace webfuse
{
class thread
{
public:
explicit thread(std::function<void(void)> run);
~thread();
void kill(int signal_id);
private:
pthread_t real_thread;
};
}
#endif

View File

@ -1,7 +1,38 @@
#include "webfuse/webfuse.hpp"
#include "webfuse/test/thread.hpp"
#include "webfuse/test/tempdir.hpp"
#include <gtest/gtest.h>
#include <csignal>
#include <cstring>
#include <cstdlib>
#include <chrono>
#include <thread>
extern "C" void * run(void * args)
{
webfuse::app * app = reinterpret_cast<webfuse::app*>(args);
return nullptr;
}
TEST(app, init)
{
webfuse::app app;
}
TEST(app, run)
{
webfuse::tempdir dir;
webfuse::thread thread([&dir](){
webfuse::app app;
char arg0[] = "webfuse";
char arg1[] = "-f";
char* arg2 = strdup(dir.name().c_str());
char* argv[] = { arg0, arg1, arg2, nullptr};
int rc = app.run(3, argv);
free(arg2);
});
std::this_thread::sleep_for(std::chrono::seconds(1));
thread.kill(SIGINT);
}