From 9036aba41b753b1aa741a27e9ea0e6b381f51795 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 30 Dec 2022 19:43:45 +0100 Subject: [PATCH] added test utilities --- test-src/webfuse/test/tempdir.cpp | 23 ++++++++++++++++++ test-src/webfuse/test/tempdir.hpp | 22 +++++++++++++++++ test-src/webfuse/test/thread.cpp | 39 +++++++++++++++++++++++++++++++ test-src/webfuse/test/thread.hpp | 22 +++++++++++++++++ test-src/webfuse/test_app.cpp | 31 ++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 test-src/webfuse/test/tempdir.cpp create mode 100644 test-src/webfuse/test/tempdir.hpp create mode 100644 test-src/webfuse/test/thread.cpp create mode 100644 test-src/webfuse/test/thread.hpp diff --git a/test-src/webfuse/test/tempdir.cpp b/test-src/webfuse/test/tempdir.cpp new file mode 100644 index 0000000..9474ba5 --- /dev/null +++ b/test-src/webfuse/test/tempdir.cpp @@ -0,0 +1,23 @@ +#include "webfuse/test/tempdir.hpp" +#include + +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; +} + +} \ No newline at end of file diff --git a/test-src/webfuse/test/tempdir.hpp b/test-src/webfuse/test/tempdir.hpp new file mode 100644 index 0000000..216341b --- /dev/null +++ b/test-src/webfuse/test/tempdir.hpp @@ -0,0 +1,22 @@ +#ifndef WEBFUSE_TEMPDIR_HPP +#define WEBFUSE_TEMPDIR_HPP + +#include + +namespace webfuse +{ + +class tempdir +{ +public: + tempdir(); + ~tempdir(); + std::string const name() const; +private: + std::string path; + +}; + +} + +#endif diff --git a/test-src/webfuse/test/thread.cpp b/test-src/webfuse/test/thread.cpp new file mode 100644 index 0000000..2ebef00 --- /dev/null +++ b/test-src/webfuse/test/thread.cpp @@ -0,0 +1,39 @@ +#include "webfuse/test/thread.hpp" +#include + +namespace +{ + +extern "C" void * webfuse_thread_main(void * args) +{ + auto * run = reinterpret_cast *>(args); + (*run)(); + return nullptr; +} + +} + + +namespace webfuse +{ + +thread::thread(std::function run) +{ + pthread_create(&real_thread, nullptr, + &webfuse_thread_main, + reinterpret_cast(&run)); + +} + +thread::~thread() +{ + pthread_join(real_thread, nullptr); +} + +void thread::kill(int signal_id) +{ + pthread_kill(real_thread, signal_id); +} + + +} \ No newline at end of file diff --git a/test-src/webfuse/test/thread.hpp b/test-src/webfuse/test/thread.hpp new file mode 100644 index 0000000..8dfbd67 --- /dev/null +++ b/test-src/webfuse/test/thread.hpp @@ -0,0 +1,22 @@ +#ifndef WEBFUSE_THREAD_HPP +#define WEBFUSE_THREAD_HPP + +#include +#include + +namespace webfuse +{ + +class thread +{ +public: + explicit thread(std::function run); + ~thread(); + void kill(int signal_id); +private: + pthread_t real_thread; +}; + +} + +#endif diff --git a/test-src/webfuse/test_app.cpp b/test-src/webfuse/test_app.cpp index d15cfde..0f546c6 100644 --- a/test-src/webfuse/test_app.cpp +++ b/test-src/webfuse/test_app.cpp @@ -1,7 +1,38 @@ #include "webfuse/webfuse.hpp" +#include "webfuse/test/thread.hpp" +#include "webfuse/test/tempdir.hpp" #include +#include +#include +#include +#include +#include + +extern "C" void * run(void * args) +{ + webfuse::app * app = reinterpret_cast(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); } \ No newline at end of file