added test utilities

pull/105/head
Falk Werner 1 year ago
parent 60594067b1
commit 9036aba41b

@ -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;
}
}

@ -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

@ -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);
}
}

@ -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

@ -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);
}
Loading…
Cancel
Save