1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

organized unit tests

This commit is contained in:
Falk Werner
2020-02-20 17:15:13 +01:00
parent abd6efe477
commit a27e68f5a6
52 changed files with 69 additions and 69 deletions

View File

@@ -0,0 +1,15 @@
#include "webfuse/utils/die_if.hpp"
#include <cstdlib>
namespace webfuse_test
{
void die_if(bool expression)
{
if (expression)
{
exit(EXIT_FAILURE);
}
}
}

View File

@@ -0,0 +1,11 @@
#ifndef WF_TEST_DIE_IF_HPP
#define WF_TEST_DIE_IF_HPP
namespace webfuse_test
{
extern void die_if(bool expression);
}
#endif

View File

@@ -0,0 +1,38 @@
#include "webfuse/utils/file_utils.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace webfuse_test
{
bool is_dir(std::string const & path)
{
struct stat info;
int rc = stat(path.c_str(), &info);
return (0 == rc) && (S_ISDIR(info.st_mode));
}
bool is_symlink(std::string const & path)
{
struct stat info;
int rc = lstat(path.c_str(), &info);
return (0 == rc) && (S_ISLNK(info.st_mode));
}
bool is_same_path(std::string const & path, std::string const & other)
{
struct stat info;
int rc = stat(path.c_str(), &info);
struct stat info_other;
int rc_other = stat(other.c_str(), &info_other);
return (0 == rc) && (0 == rc_other) && (info.st_ino == info_other.st_ino);
}
}

View File

@@ -0,0 +1,17 @@
#ifndef WF_TEST_FILE_UTILS_HPP
#define WF_TEST_FILE_UTILS_HPP
#include <string>
namespace webfuse_test
{
bool is_dir(std::string const & path);
bool is_symlink(std::string const & path);
bool is_same_path(std::string const & path, std::string const & other);
}
#endif

View File

@@ -0,0 +1,19 @@
#include "webfuse/utils/msleep.hpp"
#include <ctime>
namespace webfuse_test
{
void msleep(long millis)
{
long const secs_per_msec = 1000;
long const msecs_per_nsec = (1000 * 1000);
long const seconds = millis / secs_per_msec;
long const nanos = (millis % secs_per_msec) * msecs_per_nsec;
struct timespec timeout = { seconds, nanos };
while (0 != nanosleep(&timeout, &timeout));
}
}

View File

@@ -0,0 +1,12 @@
#ifndef WF_TEST_MSLEEP_HPP
#define WF_TEST_MSLEEP_HPP
namespace webfuse_test
{
extern void msleep(long millis);
}
#endif

View File

@@ -0,0 +1,33 @@
#include "webfuse/core/string.h"
#include "webfuse/utils/tempdir.hpp"
#include <unistd.h>
#include <cstdlib>
#include <stdexcept>
namespace webfuse_test
{
TempDir::TempDir(char const * prefix)
: path_(wf_create_string("/tmp/%s_XXXXXX", prefix))
{
char * result = mkdtemp(path_);
if (NULL == result)
{
throw std::runtime_error("unable to create temp dir");
}
}
TempDir::~TempDir()
{
rmdir(path_);
free(path_);
}
char const * TempDir::path()
{
return path_;
}
}

View File

@@ -0,0 +1,21 @@
#ifndef WF_TEST_TEMPDIR_HPP
#define WF_TEST_TEMPDIR_HPP
namespace webfuse_test
{
class TempDir
{
TempDir(TempDir const &) = delete;
TempDir & operator=(TempDir const &) = delete;
public:
explicit TempDir(char const * prefix);
~TempDir();
char const * path();
private:
char * path_;
};
}
#endif

View File

@@ -0,0 +1,44 @@
#include "webfuse/utils/timeout_watcher.hpp"
#include <stdexcept>
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::chrono::steady_clock;
namespace
{
milliseconds now()
{
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
}
}
namespace webfuse_test
{
TimeoutWatcher::TimeoutWatcher(milliseconds timeout)
: startedAt(now())
, timeout_(timeout)
{
}
TimeoutWatcher::~TimeoutWatcher()
{
}
bool TimeoutWatcher::isTimeout()
{
return (now() - startedAt) > timeout_;
}
void TimeoutWatcher::check()
{
if (isTimeout())
{
throw std::runtime_error("timeout");
}
}
}

View File

@@ -0,0 +1,26 @@
#ifndef WF_TEST_TIMEOUT_WATCHER_HPP
#define WF_TEST_TIMEOUT_WATCHER_HPP
#include <chrono>
namespace webfuse_test
{
class TimeoutWatcher final
{
TimeoutWatcher(TimeoutWatcher const & other) = delete;
TimeoutWatcher& operator=(TimeoutWatcher const & other) = delete;
public:
explicit TimeoutWatcher(std::chrono::milliseconds timeout);
~TimeoutWatcher();
bool isTimeout();
void check();
private:
std::chrono::milliseconds startedAt;
std::chrono::milliseconds timeout_;
};
}
#endif