Test cases use a fake home directory for their configuration, so they don't clutter the actual home directory.
This commit is contained in:
parent
9da30e3c17
commit
389273a24f
@ -4,10 +4,26 @@
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
using std::string;
|
||||
using cpputils::unique_ref;
|
||||
using cpputils::make_unique_ref;
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
bf::path home_directory() {
|
||||
|
||||
HomeDirectory::HomeDirectory()
|
||||
:_home_directory(_get_home_directory()) {
|
||||
}
|
||||
|
||||
HomeDirectory &HomeDirectory::singleton() {
|
||||
static HomeDirectory _singleton;
|
||||
return _singleton;
|
||||
}
|
||||
|
||||
const bf::path &HomeDirectory::get() {
|
||||
return singleton()._home_directory;
|
||||
}
|
||||
|
||||
bf::path HomeDirectory::_get_home_directory() {
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
string homedir;
|
||||
if (pwd) {
|
||||
@ -21,5 +37,15 @@ namespace cpputils {
|
||||
}
|
||||
return homedir;
|
||||
}
|
||||
|
||||
FakeHomeDirectoryRAII::FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory)
|
||||
:_oldHomeDirectory(HomeDirectory::singleton()._home_directory) {
|
||||
HomeDirectory::singleton()._home_directory = fakeHomeDirectory;
|
||||
}
|
||||
|
||||
FakeHomeDirectoryRAII::~FakeHomeDirectoryRAII() {
|
||||
// Reset to old (non-fake) value
|
||||
HomeDirectory::singleton()._home_directory = _oldHomeDirectory;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,41 @@
|
||||
#define MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include "../macros.h"
|
||||
#include <cpp-utils/pointer/unique_ref.h>
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
|
||||
boost::filesystem::path home_directory();
|
||||
class FakeHomeDirectoryRAII;
|
||||
|
||||
class HomeDirectory final {
|
||||
public:
|
||||
static const boost::filesystem::path &get();
|
||||
|
||||
private:
|
||||
boost::filesystem::path _home_directory;
|
||||
|
||||
HomeDirectory();
|
||||
static HomeDirectory &singleton();
|
||||
boost::filesystem::path _get_home_directory();
|
||||
|
||||
friend class FakeHomeDirectoryRAII;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HomeDirectory);
|
||||
};
|
||||
|
||||
|
||||
class FakeHomeDirectoryRAII final {
|
||||
public:
|
||||
FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory);
|
||||
~FakeHomeDirectoryRAII();
|
||||
|
||||
private:
|
||||
boost::filesystem::path _oldHomeDirectory;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FakeHomeDirectoryRAII);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ Key CryDevice::CreateRootBlobAndReturnKey() {
|
||||
}
|
||||
|
||||
bf::path CryDevice::_integrityFilePath(const CryConfig::FilesystemID &filesystemId) {
|
||||
bf::path app_dir = cpputils::system::home_directory() / ".cryfs";
|
||||
bf::path app_dir = cpputils::system::HomeDirectory::get() / ".cryfs";
|
||||
_createDirIfNotExists(app_dir);
|
||||
bf::path filesystems_dir = app_dir / "filesystems";
|
||||
_createDirIfNotExists(filesystems_dir);
|
||||
|
@ -9,6 +9,8 @@
|
||||
#include <cryfs/filesystem/CryOpenFile.h>
|
||||
#include "../testutils/MockConsole.h"
|
||||
#include <cryfs/config/CryConfigLoader.h>
|
||||
#include <cpp-utils/system/homedir.h>
|
||||
#include "../testutils/TestWithFakeHomeDirectory.h"
|
||||
|
||||
//TODO (whole project) Make constructors explicit when implicit construction not needed
|
||||
|
||||
@ -24,13 +26,14 @@ using cpputils::Console;
|
||||
using cpputils::Random;
|
||||
using cpputils::SCrypt;
|
||||
using cpputils::Data;
|
||||
using cpputils::system::FakeHomeDirectoryRAII;
|
||||
using blockstore::ondisk::OnDiskBlockStore;
|
||||
using boost::none;
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
using namespace cryfs;
|
||||
|
||||
class CryFsTest: public Test, public TestWithMockConsole {
|
||||
class CryFsTest: public Test, public TestWithMockConsole, public TestWithFakeHomeDirectory {
|
||||
public:
|
||||
CryFsTest(): rootdir(), config(false) {
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <cryfs/filesystem/CryDevice.h>
|
||||
#include <cryfs/config/CryConfigLoader.h>
|
||||
#include "../testutils/MockConsole.h"
|
||||
#include "../testutils/TestWithFakeHomeDirectory.h"
|
||||
|
||||
using cpputils::unique_ref;
|
||||
using cpputils::make_unique_ref;
|
||||
@ -19,7 +20,7 @@ using blockstore::testfake::FakeBlockStore;
|
||||
|
||||
using namespace cryfs;
|
||||
|
||||
class CryFsTestFixture: public FileSystemTestFixture, public TestWithMockConsole {
|
||||
class CryFsTestFixture: public FileSystemTestFixture, public TestWithMockConsole, public TestWithFakeHomeDirectory {
|
||||
public:
|
||||
CryFsTestFixture()
|
||||
// Don't create config tempfile yet
|
||||
|
@ -5,8 +5,9 @@
|
||||
#include <blockstore/implementations/testfake/FakeBlockStore.h>
|
||||
#include <cpp-utils/tempfile/TempFile.h>
|
||||
#include <cpp-utils/crypto/kdf/Scrypt.h>
|
||||
#include "../../testutils/TestWithFakeHomeDirectory.h"
|
||||
|
||||
class CryTestBase {
|
||||
class CryTestBase : public TestWithFakeHomeDirectory {
|
||||
public:
|
||||
CryTestBase(): _configFile(false), _device(nullptr) {
|
||||
auto fakeBlockStore = cpputils::make_unique_ref<blockstore::testfake::FakeBlockStore>();
|
||||
|
19
test/cryfs/testutils/TestWithFakeHomeDirectory.h
Normal file
19
test/cryfs/testutils/TestWithFakeHomeDirectory.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#ifndef MESSMER_CRYFS_TEST_TESTUTILS_TESTWITHFAKEHOMEDIRECTORY_H
|
||||
#define MESSMER_CRYFS_TEST_TESTUTILS_TESTWITHFAKEHOMEDIRECTORY_H
|
||||
|
||||
#include <cpp-utils/tempfile/TempDir.h>
|
||||
#include <cpp-utils/system/homedir.h>
|
||||
|
||||
class TestWithFakeHomeDirectory {
|
||||
public:
|
||||
TestWithFakeHomeDirectory()
|
||||
:homedir(), fakeHomeDirRAII(homedir.path()) {
|
||||
}
|
||||
|
||||
private:
|
||||
cpputils::TempDir homedir;
|
||||
cpputils::system::FakeHomeDirectoryRAII fakeHomeDirRAII;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user