libcryfs/test/fspp/testutils/FuseTest.h

99 lines
4.9 KiB
C
Raw Normal View History

2014-11-19 00:25:57 +01:00
#pragma once
2015-10-15 13:04:57 +02:00
#ifndef MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_
#define MESSMER_FSPP_TEST_TESTUTILS_FUSETEST_H_
2014-11-19 00:25:57 +01:00
2016-02-11 12:53:42 +01:00
#include <gtest/gtest.h>
#include <gmock/gmock.h>
2014-11-19 00:25:57 +01:00
2016-02-11 12:53:42 +01:00
#include "fspp/fuse/Filesystem.h"
#include "fspp/fs_interface/FuseErrnoException.h"
2016-02-11 12:53:42 +01:00
#include "fspp/fuse/Fuse.h"
#include "fspp/fs_interface/Dir.h"
2014-11-19 00:25:57 +01:00
#include <boost/filesystem.hpp>
2016-02-11 12:53:42 +01:00
#include <cpp-utils/tempfile/TempDir.h>
2014-11-19 00:40:46 +01:00
#include "FuseThread.h"
2014-11-19 00:25:57 +01:00
class MockFilesystem: public fspp::fuse::Filesystem {
2014-11-19 00:25:57 +01:00
public:
2014-11-25 15:01:33 +01:00
MockFilesystem();
virtual ~MockFilesystem();
MOCK_METHOD(void, setContext, (fspp::Context&&), (override));
MOCK_METHOD(int, openFile, (const boost::filesystem::path&, int), (override));
MOCK_METHOD(void, closeFile, (int), (override));
MOCK_METHOD(void, lstat, (const boost::filesystem::path&, fspp::fuse::STAT*), (override));
MOCK_METHOD(void, fstat, (int, fspp::fuse::STAT*), (override));
MOCK_METHOD(void, truncate, (const boost::filesystem::path&, fspp::num_bytes_t), (override));
MOCK_METHOD(void, ftruncate, (int, fspp::num_bytes_t), (override));
MOCK_METHOD(fspp::num_bytes_t, read, (int, void*, fspp::num_bytes_t, fspp::num_bytes_t), (override));
MOCK_METHOD(void, write, (int, const void*, fspp::num_bytes_t, fspp::num_bytes_t), (override));
MOCK_METHOD(void, flush, (int), (override));
MOCK_METHOD(void, fsync, (int), (override));
MOCK_METHOD(void, fdatasync, (int), (override));
MOCK_METHOD(void, access, (const boost::filesystem::path&, int), (override));
MOCK_METHOD(int, createAndOpenFile, (const boost::filesystem::path&, mode_t, uid_t, gid_t), (override));
MOCK_METHOD(void, mkdir, (const boost::filesystem::path&, mode_t, uid_t, gid_t), (override));
MOCK_METHOD(void, rmdir, (const boost::filesystem::path&), (override));
MOCK_METHOD(void, unlink, (const boost::filesystem::path&), (override));
MOCK_METHOD(void, rename, (const boost::filesystem::path&, const boost::filesystem::path&), (override));
MOCK_METHOD(std::vector<fspp::Dir::Entry>, readDir, (const boost::filesystem::path &path), (override));
MOCK_METHOD(void, utimens, (const boost::filesystem::path&, timespec, timespec), (override));
MOCK_METHOD(void, statfs, (struct statvfs*), (override));
MOCK_METHOD(void, chmod, (const boost::filesystem::path&, mode_t), (override));
MOCK_METHOD(void, chown, (const boost::filesystem::path&, uid_t, gid_t), (override));
MOCK_METHOD(void, createSymlink, (const boost::filesystem::path&, const boost::filesystem::path&, uid_t, gid_t), (override));
MOCK_METHOD(void, readSymlink, (const boost::filesystem::path&, char*, fspp::num_bytes_t), (override));
2014-11-19 00:25:57 +01:00
};
class FuseTest: public ::testing::Test {
public:
2015-10-17 20:35:17 +02:00
static constexpr const char* FILENAME = "/myfile";
2014-11-25 14:29:44 +01:00
FuseTest();
2014-11-19 00:25:57 +01:00
class TempTestFS {
public:
TempTestFS(std::shared_ptr<MockFilesystem> fsimpl, const std::vector<std::string>& fuseOptions = {});
virtual ~TempTestFS();
2014-11-19 00:25:57 +01:00
public:
const boost::filesystem::path &mountDir() const;
2014-11-19 00:25:57 +01:00
private:
2015-04-25 03:47:30 +02:00
cpputils::TempDir _mountDir;
2014-11-19 00:25:57 +01:00
fspp::fuse::Fuse _fuse;
FuseThread _fuse_thread;
};
cpputils::unique_ref<TempTestFS> TestFS(const std::vector<std::string>& fuseOptions = {});
2014-11-19 00:25:57 +01:00
std::shared_ptr<MockFilesystem> fsimpl;
2014-11-19 00:38:41 +01:00
const fspp::Context& context() const {
ASSERT(_context != boost::none, "Context wasn't correctly initialized");
return *_context;
}
private:
boost::optional<fspp::Context> _context;
public:
2014-11-25 14:29:44 +01:00
//TODO Combine ReturnIsFile and ReturnIsFileFstat. This should be possible in gmock by either (a) using ::testing::Undefined as parameter type or (b) using action macros
2021-04-21 08:09:00 +02:00
static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsFile; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsFileWithSize(fspp::num_bytes_t size); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static ::testing::Action<void(int, fspp::fuse::STAT*)> ReturnIsFileFstat; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static ::testing::Action<void(int, fspp::fuse::STAT*)> ReturnIsFileFstatWithSize(fspp::num_bytes_t size); // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnIsDir; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static ::testing::Action<void(const boost::filesystem::path&, fspp::fuse::STAT*)> ReturnDoesntExist; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
2014-11-25 14:29:44 +01:00
2015-02-17 00:48:49 +01:00
void ReturnIsFileOnLstat(const boost::filesystem::path &path);
2018-09-15 23:32:58 +02:00
void ReturnIsFileOnLstatWithSize(const boost::filesystem::path &path, fspp::num_bytes_t size);
2015-02-17 00:48:49 +01:00
void ReturnIsDirOnLstat(const boost::filesystem::path &path);
void ReturnDoesntExistOnLstat(const boost::filesystem::path &path);
2014-11-25 18:43:50 +01:00
void OnOpenReturnFileDescriptor(const char *filename, int descriptor);
void ReturnIsFileOnFstat(int descriptor);
2018-09-15 23:32:58 +02:00
void ReturnIsFileOnFstatWithSize(int descriptor, fspp::num_bytes_t size);
2014-11-19 00:25:57 +01:00
};
#endif