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;
|
namespace bf = boost::filesystem;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
using cpputils::unique_ref;
|
||||||
|
using cpputils::make_unique_ref;
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
namespace system {
|
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());
|
struct passwd* pwd = getpwuid(getuid());
|
||||||
string homedir;
|
string homedir;
|
||||||
if (pwd) {
|
if (pwd) {
|
||||||
@ -21,5 +37,15 @@ namespace cpputils {
|
|||||||
}
|
}
|
||||||
return homedir;
|
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
|
#define MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H
|
||||||
|
|
||||||
#include <boost/filesystem/path.hpp>
|
#include <boost/filesystem/path.hpp>
|
||||||
|
#include "../macros.h"
|
||||||
|
#include <cpp-utils/pointer/unique_ref.h>
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
namespace system {
|
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 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);
|
_createDirIfNotExists(app_dir);
|
||||||
bf::path filesystems_dir = app_dir / "filesystems";
|
bf::path filesystems_dir = app_dir / "filesystems";
|
||||||
_createDirIfNotExists(filesystems_dir);
|
_createDirIfNotExists(filesystems_dir);
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <cryfs/filesystem/CryOpenFile.h>
|
#include <cryfs/filesystem/CryOpenFile.h>
|
||||||
#include "../testutils/MockConsole.h"
|
#include "../testutils/MockConsole.h"
|
||||||
#include <cryfs/config/CryConfigLoader.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
|
//TODO (whole project) Make constructors explicit when implicit construction not needed
|
||||||
|
|
||||||
@ -24,13 +26,14 @@ using cpputils::Console;
|
|||||||
using cpputils::Random;
|
using cpputils::Random;
|
||||||
using cpputils::SCrypt;
|
using cpputils::SCrypt;
|
||||||
using cpputils::Data;
|
using cpputils::Data;
|
||||||
|
using cpputils::system::FakeHomeDirectoryRAII;
|
||||||
using blockstore::ondisk::OnDiskBlockStore;
|
using blockstore::ondisk::OnDiskBlockStore;
|
||||||
using boost::none;
|
using boost::none;
|
||||||
|
|
||||||
namespace bf = boost::filesystem;
|
namespace bf = boost::filesystem;
|
||||||
using namespace cryfs;
|
using namespace cryfs;
|
||||||
|
|
||||||
class CryFsTest: public Test, public TestWithMockConsole {
|
class CryFsTest: public Test, public TestWithMockConsole, public TestWithFakeHomeDirectory {
|
||||||
public:
|
public:
|
||||||
CryFsTest(): rootdir(), config(false) {
|
CryFsTest(): rootdir(), config(false) {
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <cryfs/filesystem/CryDevice.h>
|
#include <cryfs/filesystem/CryDevice.h>
|
||||||
#include <cryfs/config/CryConfigLoader.h>
|
#include <cryfs/config/CryConfigLoader.h>
|
||||||
#include "../testutils/MockConsole.h"
|
#include "../testutils/MockConsole.h"
|
||||||
|
#include "../testutils/TestWithFakeHomeDirectory.h"
|
||||||
|
|
||||||
using cpputils::unique_ref;
|
using cpputils::unique_ref;
|
||||||
using cpputils::make_unique_ref;
|
using cpputils::make_unique_ref;
|
||||||
@ -19,7 +20,7 @@ using blockstore::testfake::FakeBlockStore;
|
|||||||
|
|
||||||
using namespace cryfs;
|
using namespace cryfs;
|
||||||
|
|
||||||
class CryFsTestFixture: public FileSystemTestFixture, public TestWithMockConsole {
|
class CryFsTestFixture: public FileSystemTestFixture, public TestWithMockConsole, public TestWithFakeHomeDirectory {
|
||||||
public:
|
public:
|
||||||
CryFsTestFixture()
|
CryFsTestFixture()
|
||||||
// Don't create config tempfile yet
|
// Don't create config tempfile yet
|
||||||
|
@ -5,8 +5,9 @@
|
|||||||
#include <blockstore/implementations/testfake/FakeBlockStore.h>
|
#include <blockstore/implementations/testfake/FakeBlockStore.h>
|
||||||
#include <cpp-utils/tempfile/TempFile.h>
|
#include <cpp-utils/tempfile/TempFile.h>
|
||||||
#include <cpp-utils/crypto/kdf/Scrypt.h>
|
#include <cpp-utils/crypto/kdf/Scrypt.h>
|
||||||
|
#include "../../testutils/TestWithFakeHomeDirectory.h"
|
||||||
|
|
||||||
class CryTestBase {
|
class CryTestBase : public TestWithFakeHomeDirectory {
|
||||||
public:
|
public:
|
||||||
CryTestBase(): _configFile(false), _device(nullptr) {
|
CryTestBase(): _configFile(false), _device(nullptr) {
|
||||||
auto fakeBlockStore = cpputils::make_unique_ref<blockstore::testfake::FakeBlockStore>();
|
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…
x
Reference in New Issue
Block a user