- Refactor main()
- Added CryConfigLoaderTest
This commit is contained in:
parent
39c62ae185
commit
20b0034ab1
@ -8,7 +8,6 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace cryfs {
|
||||
//TODO Add test case for the whole config folder
|
||||
|
||||
class CryConfig final {
|
||||
public:
|
||||
|
@ -1,5 +1,6 @@
|
||||
#ifndef CRYFS_SRC_CONFIG_CRYCONFIGCREATOR_H
|
||||
#define CRYFS_SRC_CONFIG_CRYCONFIGCREATOR_H
|
||||
#pragma once
|
||||
#ifndef MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGCREATOR_H
|
||||
#define MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGCREATOR_H
|
||||
|
||||
#include <messmer/cpp-utils/pointer/unique_ref.h>
|
||||
#include <messmer/cpp-utils/random/RandomGenerator.h>
|
||||
|
@ -1,5 +1,6 @@
|
||||
#ifndef CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
|
||||
#define CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
|
||||
#pragma once
|
||||
#ifndef MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
|
||||
#define MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
@ -15,8 +15,6 @@ using std::string;
|
||||
|
||||
namespace cryfs {
|
||||
|
||||
CryConfigLoader::CryConfigLoader(): CryConfigLoader(make_unique_ref<IOStreamConsole>(), cpputils::Random::OSRandom()) {}
|
||||
|
||||
CryConfigLoader::CryConfigLoader(unique_ref<Console> console, cpputils::RandomGenerator &keyGenerator)
|
||||
: _creator(std::move(console), keyGenerator) {}
|
||||
|
||||
@ -25,14 +23,7 @@ CryConfigFile CryConfigLoader::loadOrCreate(const bf::path &filename) {
|
||||
if (config != none) {
|
||||
return std::move(*config);
|
||||
}
|
||||
return createNew(filename);
|
||||
}
|
||||
|
||||
CryConfigFile CryConfigLoader::createNew(const bf::path &filename) {
|
||||
auto config = _creator.create();
|
||||
auto configFile = CryConfigFile::create(filename, std::move(config));
|
||||
configFile.save();
|
||||
return configFile;
|
||||
return CryConfigFile::create(filename, _creator.create());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,11 +12,9 @@ namespace cryfs {
|
||||
|
||||
class CryConfigLoader {
|
||||
public:
|
||||
CryConfigLoader();
|
||||
explicit CryConfigLoader(cpputils::unique_ref<cpputils::Console> console, cpputils::RandomGenerator &keyGenerator);
|
||||
CryConfigLoader(cpputils::unique_ref<cpputils::Console> console, cpputils::RandomGenerator &keyGenerator);
|
||||
|
||||
CryConfigFile loadOrCreate(const boost::filesystem::path &filename);
|
||||
CryConfigFile createNew(const boost::filesystem::path &filename);
|
||||
|
||||
private:
|
||||
CryConfigCreator _creator;
|
||||
|
10
src/main.cpp
10
src/main.cpp
@ -23,6 +23,8 @@ using blockstore::inmemory::InMemoryBlockStore;
|
||||
using program_options::ProgramOptions;
|
||||
|
||||
using cpputils::make_unique_ref;
|
||||
using cpputils::Random;
|
||||
using cpputils::IOStreamConsole;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::vector;
|
||||
@ -50,8 +52,14 @@ void showVersion() {
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
CryConfigFile loadOrCreateConfig(const ProgramOptions &options) {
|
||||
auto console = make_unique_ref<IOStreamConsole>();
|
||||
auto &keyGenerator = Random::OSRandom();
|
||||
return CryConfigLoader(std::move(console), keyGenerator).loadOrCreate(bf::path(options.configFile()));
|
||||
}
|
||||
|
||||
void runFilesystem(const ProgramOptions &options) {
|
||||
auto config = CryConfigLoader().loadOrCreate(bf::path(options.configFile()));
|
||||
auto config = loadOrCreateConfig(options);
|
||||
//TODO This daemonize causes error messages when initializing CryDevice to get lost.
|
||||
// However, initializing CryDevice might (?) already spawn threads and we have to do daemonization before that
|
||||
// because it doesn't fork threads. What to do?
|
||||
|
@ -36,14 +36,6 @@ public:
|
||||
|
||||
#define EXPECT_ASK_FOR_CIPHER() EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames())))
|
||||
|
||||
ACTION(ChooseAnyCipher) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ACTION_P(ChooseCipher, cipherName) {
|
||||
return std::find(arg1.begin(), arg1.end(), cipherName) - arg1.begin();
|
||||
}
|
||||
|
||||
TEST_F(CryConfigCreatorTest, DoesNotCrash) {
|
||||
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher());
|
||||
CryConfig config = creator.create();
|
||||
|
97
test/config/CryConfigLoaderTest.cpp
Normal file
97
test/config/CryConfigLoaderTest.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include <google/gtest/gtest.h>
|
||||
#include "../../src/config/CryConfigLoader.h"
|
||||
#include "../testutils/MockConsole.h"
|
||||
#include <messmer/cpp-utils/tempfile/TempFile.h>
|
||||
#include <messmer/cpp-utils/random/Random.h>
|
||||
#include <messmer/blockstore/implementations/encrypted/ciphers/ciphers.h>
|
||||
|
||||
using cpputils::unique_ref;
|
||||
using cpputils::make_unique_ref;
|
||||
using cpputils::TempFile;
|
||||
using std::string;
|
||||
using ::testing::Return;
|
||||
using ::testing::_;
|
||||
|
||||
using namespace cryfs;
|
||||
|
||||
class CryConfigLoaderTest: public ::testing::Test, public TestWithMockConsole {
|
||||
public:
|
||||
CryConfigLoaderTest(): loader(mockConsole(), cpputils::Random::PseudoRandom()), file(false) {}
|
||||
|
||||
CryConfigFile Create() {
|
||||
EXPECT_FALSE(file.exists());
|
||||
return loader.loadOrCreate(file.path());
|
||||
}
|
||||
|
||||
CryConfigFile Load() {
|
||||
EXPECT_TRUE(file.exists());
|
||||
return loader.loadOrCreate(file.path());
|
||||
}
|
||||
|
||||
void CreateWithRootBlob(const string &rootBlob) {
|
||||
auto cfg = loader.loadOrCreate(file.path());
|
||||
cfg.config()->SetRootBlob(rootBlob);
|
||||
cfg.save();
|
||||
}
|
||||
|
||||
void CreateWithCipher(const string &cipher) {
|
||||
auto cfg = loader.loadOrCreate(file.path());
|
||||
cfg.config()->SetCipher(cipher);
|
||||
cfg.save();
|
||||
}
|
||||
|
||||
void CreateWithEncryptionKey(const string &encKey) {
|
||||
auto cfg = loader.loadOrCreate(file.path());
|
||||
cfg.config()->SetEncryptionKey(encKey);
|
||||
cfg.save();
|
||||
}
|
||||
|
||||
CryConfigLoader loader;
|
||||
TempFile file;
|
||||
};
|
||||
|
||||
TEST_F(CryConfigLoaderTest, CreatesNewIfNotExisting) {
|
||||
EXPECT_FALSE(file.exists());
|
||||
Create();
|
||||
EXPECT_TRUE(file.exists());
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, DoesntCrashIfExisting) {
|
||||
Create();
|
||||
Load();
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, RootBlob_Load) {
|
||||
CreateWithRootBlob("rootblobid");
|
||||
auto loaded = Load();
|
||||
EXPECT_EQ("rootblobid", loaded.config()->RootBlob());
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, RootBlob_Create) {
|
||||
auto created = Create();
|
||||
EXPECT_EQ("", created.config()->RootBlob());
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, EncryptionKey_Load) {
|
||||
CreateWithEncryptionKey("encryptionkey");
|
||||
auto loaded = Load();
|
||||
EXPECT_EQ("encryptionkey", loaded.config()->EncryptionKey());
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, EncryptionKey_Create) {
|
||||
auto created = Create();
|
||||
//aes-256-gcm is the default cipher chosen by mockConsole()
|
||||
blockstore::encrypted::AES256_GCM::EncryptionKey::FromString(created.config()->EncryptionKey()); // This crashes if key is invalid
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, Cipher_Load) {
|
||||
CreateWithCipher("ciphername");
|
||||
auto loaded = Load();
|
||||
EXPECT_EQ("ciphername", loaded.config()->Cipher());
|
||||
}
|
||||
|
||||
TEST_F(CryConfigLoaderTest, Cipher_Create) {
|
||||
auto created = Create();
|
||||
//aes-256-gcm is the default cipher chosen by mockConsole()
|
||||
EXPECT_EQ("aes-256-gcm", created.config()->Cipher());
|
||||
}
|
@ -25,40 +25,28 @@ using blockstore::ondisk::OnDiskBlockStore;
|
||||
namespace bf = boost::filesystem;
|
||||
using namespace cryfs;
|
||||
|
||||
class CryFsTest: public Test {
|
||||
class CryFsTest: public Test, public TestWithMockConsole {
|
||||
public:
|
||||
CryFsTest(): rootdir(), config(false) {
|
||||
}
|
||||
unique_ref<MockConsole> mockConsole() {
|
||||
auto console = make_unique_ref<MockConsole>();
|
||||
EXPECT_CALL(*console, ask(_, _)).WillRepeatedly(Return(0));
|
||||
EXPECT_CALL(*console, askYesNo(_)).WillRepeatedly(Return(true));
|
||||
return console;
|
||||
|
||||
CryConfigFile loadOrCreateConfig() {
|
||||
return CryConfigLoader(mockConsole(), cpputils::Random::PseudoRandom()).loadOrCreate(config.path());
|
||||
}
|
||||
|
||||
unique_ref<OnDiskBlockStore> blockStore() {
|
||||
return make_unique_ref<OnDiskBlockStore>(rootdir.path());
|
||||
}
|
||||
|
||||
TempDir rootdir;
|
||||
TempFile config;
|
||||
};
|
||||
|
||||
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing_1) {
|
||||
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
|
||||
{
|
||||
CryDevice dev(
|
||||
CryConfigLoader(mockConsole(), cpputils::Random::PseudoRandom())
|
||||
.createNew(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path())
|
||||
);
|
||||
CryDevice dev(loadOrCreateConfig(), blockStore());
|
||||
}
|
||||
CryDevice dev(CryConfigFile::load(config.path()).value(), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
|
||||
auto root = dev.Load(bf::path("/"));
|
||||
dynamic_pointer_move<CryDir>(root.get()).get()->children();
|
||||
}
|
||||
|
||||
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing_2) {
|
||||
{
|
||||
CryDevice dev(
|
||||
CryConfigLoader(mockConsole(), cpputils::Random::PseudoRandom())
|
||||
.loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path())
|
||||
);
|
||||
}
|
||||
CryDevice dev(CryConfigLoader().loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
|
||||
CryDevice dev(loadOrCreateConfig(), blockStore());
|
||||
auto root = dev.Load(bf::path("/"));
|
||||
dynamic_pointer_move<CryDir>(root.get()).get()->children();
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ using blockstore::testfake::FakeBlockStore;
|
||||
|
||||
using namespace cryfs;
|
||||
|
||||
class CryFsTestFixture: public FileSystemTestFixture {
|
||||
class CryFsTestFixture: public FileSystemTestFixture, public TestWithMockConsole {
|
||||
public:
|
||||
CryFsTestFixture()
|
||||
// Don't create config tempfile yet
|
||||
@ -29,13 +29,6 @@ public:
|
||||
return make_unique_ref<CryDevice>(std::move(config), std::move(blockStore));
|
||||
}
|
||||
|
||||
unique_ref<MockConsole> mockConsole() {
|
||||
auto console = make_unique_ref<MockConsole>();
|
||||
EXPECT_CALL(*console, ask(_, _)).WillRepeatedly(Return(0));
|
||||
EXPECT_CALL(*console, askYesNo(_)).WillRepeatedly(Return(true));
|
||||
return console;
|
||||
}
|
||||
|
||||
cpputils::TempFile configFile;
|
||||
};
|
||||
|
||||
|
@ -12,4 +12,21 @@ public:
|
||||
MOCK_METHOD1(askYesNo, bool(const std::string&));
|
||||
};
|
||||
|
||||
ACTION_P(ChooseCipher, cipherName) {
|
||||
return std::find(arg1.begin(), arg1.end(), cipherName) - arg1.begin();
|
||||
}
|
||||
|
||||
#define ChooseAnyCipher() ChooseCipher("aes-256-gcm")
|
||||
|
||||
class TestWithMockConsole {
|
||||
public:
|
||||
// Return a console that chooses a valid cryfs setting
|
||||
static cpputils::unique_ref<MockConsole> mockConsole() {
|
||||
auto console = cpputils::make_unique_ref<MockConsole>();
|
||||
EXPECT_CALL(*console, ask(::testing::_, ::testing::_)).WillRepeatedly(ChooseCipher("aes-256-gcm"));
|
||||
EXPECT_CALL(*console, askYesNo(::testing::_)).WillRepeatedly(::testing::Return(true));
|
||||
return console;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user