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:
15
test/webfuse/utils/die_if.cc
Normal file
15
test/webfuse/utils/die_if.cc
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
test/webfuse/utils/die_if.hpp
Normal file
11
test/webfuse/utils/die_if.hpp
Normal 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
|
||||
38
test/webfuse/utils/file_utils.cc
Normal file
38
test/webfuse/utils/file_utils.cc
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
17
test/webfuse/utils/file_utils.hpp
Normal file
17
test/webfuse/utils/file_utils.hpp
Normal 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
|
||||
19
test/webfuse/utils/msleep.cc
Normal file
19
test/webfuse/utils/msleep.cc
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
12
test/webfuse/utils/msleep.hpp
Normal file
12
test/webfuse/utils/msleep.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef WF_TEST_MSLEEP_HPP
|
||||
#define WF_TEST_MSLEEP_HPP
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
extern void msleep(long millis);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
33
test/webfuse/utils/tempdir.cc
Normal file
33
test/webfuse/utils/tempdir.cc
Normal 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_;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
21
test/webfuse/utils/tempdir.hpp
Normal file
21
test/webfuse/utils/tempdir.hpp
Normal 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
|
||||
44
test/webfuse/utils/timeout_watcher.cc
Normal file
44
test/webfuse/utils/timeout_watcher.cc
Normal 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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
26
test/webfuse/utils/timeout_watcher.hpp
Normal file
26
test/webfuse/utils/timeout_watcher.hpp
Normal 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
|
||||
Reference in New Issue
Block a user