Test cases don't need user interaction anymore

This commit is contained in:
Sebastian Messmer 2015-07-26 13:09:55 +02:00
parent fa4a50b7b6
commit fd11436fb9
8 changed files with 62 additions and 35 deletions

View File

@ -1,6 +1,5 @@
#include "CryConfigLoader.h"
#include <boost/filesystem.hpp>
#include "utils/Console.h"
namespace bf = boost::filesystem;
using cpputils::unique_ref;
@ -12,6 +11,10 @@ using std::string;
namespace cryfs {
CryConfigLoader::CryConfigLoader(): CryConfigLoader(make_unique_ref<IOStreamConsole>()) {}
CryConfigLoader::CryConfigLoader(unique_ref<Console> console) : _console(std::move(console)) {}
unique_ref<CryConfig> CryConfigLoader::loadOrCreate(const bf::path &filename) {
auto config = loadExisting(filename);
if (config != none) {
@ -41,7 +44,7 @@ void CryConfigLoader::_initializeConfigWithWeakKey(CryConfig *config) {
void CryConfigLoader::_generateCipher(CryConfig *config) {
vector<string> ciphers = {"aes-256-gcm", "aes-256-cfb"};
int cipherIndex = Console().ask("Which block cipher do you want to use?", ciphers);
int cipherIndex = _console->ask("Which block cipher do you want to use?", ciphers);
config->SetCipher(ciphers[cipherIndex]);
}
@ -50,12 +53,10 @@ void CryConfigLoader::_generateTestCipher(CryConfig *config) {
}
void CryConfigLoader::_generateEncKey(CryConfig *config) {
printf("Generating secure encryption key...");
fflush(stdout);
_console->print("Generating secure encryption key...");
auto new_key = Cipher::EncryptionKey::CreateOSRandom();
config->SetEncryptionKey(new_key.ToString());
printf("done\n");
fflush(stdout);
_console->print("done");
}
void CryConfigLoader::_generateWeakEncKey(CryConfig *config) {

View File

@ -6,31 +6,38 @@
#include <boost/filesystem/path.hpp>
#include "CryConfig.h"
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
#include "utils/Console.h"
namespace cryfs {
class CryConfigLoader {
public:
//TODO Get rid of this and use dynamically configured Cipher instead
using Cipher = blockstore::encrypted::AES256_GCM;
static cpputils::unique_ref<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
CryConfigLoader();
explicit CryConfigLoader(cpputils::unique_ref<Console> console);
static cpputils::unique_ref<CryConfig> createNew(const boost::filesystem::path &filename);
static boost::optional<cpputils::unique_ref<CryConfig>> loadExisting(const boost::filesystem::path &filename);
cpputils::unique_ref<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
cpputils::unique_ref<CryConfig> createNew(const boost::filesystem::path &filename);
boost::optional<cpputils::unique_ref<CryConfig>> loadExisting(const boost::filesystem::path &filename);
//This method is only for testing purposes, because creating weak keys is much faster than creating strong keys.
static cpputils::unique_ref<CryConfig> loadOrCreateWithWeakKey(const boost::filesystem::path &filename);
static cpputils::unique_ref<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename);
cpputils::unique_ref<CryConfig> loadOrCreateWithWeakKey(const boost::filesystem::path &filename);
cpputils::unique_ref<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename);
private:
static void _initializeConfig(CryConfig *config);
static void _generateCipher(CryConfig *config);
static void _generateEncKey(CryConfig *config);
static void _generateRootBlobKey(CryConfig *config);
void _initializeConfig(CryConfig *config);
void _generateCipher(CryConfig *config);
void _generateEncKey(CryConfig *config);
void _generateRootBlobKey(CryConfig *config);
static void _initializeConfigWithWeakKey(CryConfig *config); // TODO Rename to _initializeConfigForTest
static void _generateWeakEncKey(CryConfig *config); // TODO Rename to _generateTestEncKey
static void _generateTestCipher(CryConfig *config);
void _initializeConfigWithWeakKey(CryConfig *config); // TODO Rename to _initializeConfigForTest
void _generateWeakEncKey(CryConfig *config); // TODO Rename to _generateTestEncKey
void _generateTestCipher(CryConfig *config);
cpputils::unique_ref<Console> _console;
};
}

View File

@ -20,7 +20,7 @@ using cpputils::make_unique_ref;
int main (int argc, char *argv[])
{
auto blockStore = make_unique_ref<OnDiskBlockStore>(bf::path("/home/heinzi/cryfstest/root"));
auto config = cryfs::CryConfigLoader::loadOrCreate(bf::path("/home/heinzi/cryfstest/config.json"));
auto config = cryfs::CryConfigLoader().loadOrCreate(bf::path("/home/heinzi/cryfstest/config.json"));
cryfs::CryDevice device(std::move(config), std::move(blockStore));
fspp::FilesystemImpl fsimpl(&device);
fspp::fuse::Fuse fuse(&fsimpl);

View File

@ -12,10 +12,10 @@ using std::getline;
using boost::optional;
using boost::none;
Console::Console(): Console(std::cout, std::cin) {
IOStreamConsole::IOStreamConsole(): IOStreamConsole(std::cout, std::cin) {
}
Console::Console(ostream &output, istream &input): _output(output), _input(input) {
IOStreamConsole::IOStreamConsole(ostream &output, istream &input): _output(output), _input(input) {
}
optional<int> parseInt(const string &str) {
@ -34,7 +34,7 @@ optional<int> parseInt(const string &str) {
}
}
unsigned int Console::ask(const string &question, const vector<string> &options) {
unsigned int IOStreamConsole::ask(const string &question, const vector<string> &options) {
if(options.size() == 0) {
throw std::invalid_argument("options should have at least one entry");
}
@ -51,3 +51,7 @@ unsigned int Console::ask(const string &question, const vector<string> &options)
} while(choice == none || *choice < 1 || static_cast<unsigned int>(*choice) > options.size());
return *choice-1;
}
void IOStreamConsole::print(const std::string &output) {
_output << output << std::flush;
}

View File

@ -6,13 +6,20 @@
#include <vector>
#include <iostream>
//TODO Add test cases
class Console {
public:
Console();
Console(std::ostream &output, std::istream &input);
unsigned int ask(const std::string &question, const std::vector<std::string> &options);
virtual unsigned int ask(const std::string &question, const std::vector<std::string> &options) = 0;
virtual void print(const std::string &output) = 0;
};
class IOStreamConsole: public Console {
public:
IOStreamConsole();
IOStreamConsole(std::ostream &output, std::istream &input);
unsigned int ask(const std::string &question, const std::vector<std::string> &options) override;
//TODO Test print()
void print(const std::string &output) override;
private:
std::ostream &_output;
std::istream &_input;

View File

@ -15,11 +15,19 @@ using cpputils::TempDir;
using cpputils::TempFile;
using cpputils::dynamic_pointer_move;
using cpputils::make_unique_ref;
using cpputils::unique_ref;
using blockstore::ondisk::OnDiskBlockStore;
namespace bf = boost::filesystem;
using namespace cryfs;
class MockConsole: public Console {
void print(const std::string &) override {}
unsigned int ask(const std::string &, const std::vector<std::string> &) override {
return 0;
}
};
class CryFsTest: public Test {
public:
CryFsTest(): rootdir(), config(false) {}
@ -29,27 +37,27 @@ public:
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
{
CryDevice dev(CryConfigLoader::createNewWithWeakKey(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader().createNewWithWeakKey(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
}
CryDevice dev(CryConfigLoader::loadExisting(config.path()).value(), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader().loadExisting(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, UsingStrongKey1_CreatedRootdirIsLoadableAfterClosing) {
{
CryDevice dev(CryConfigLoader::createNew(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader(make_unique_ref<MockConsole>()).createNew(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
}
CryDevice dev(CryConfigLoader::loadExisting(config.path()).value(), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader().loadExisting(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, UsingStrongKey2_CreatedRootdirIsLoadableAfterClosing) {
{
CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader(make_unique_ref<MockConsole>()).loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
}
CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(CryConfigLoader().loadOrCreate(config.path()), make_unique_ref<OnDiskBlockStore>(rootdir.path()));
auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root.get()).get()->children();
}

View File

@ -21,7 +21,7 @@ public:
unique_ref<Device> createDevice() override {
auto blockStore = cpputils::make_unique_ref<FakeBlockStore>();
auto config = CryConfigLoader::loadOrCreateWithWeakKey(configFile.path());
auto config = CryConfigLoader().loadOrCreateWithWeakKey(configFile.path());
return make_unique_ref<CryDevice>(std::move(config), std::move(blockStore));
}

View File

@ -23,7 +23,7 @@ public:
});
}
private:
Console _console;
IOStreamConsole _console;
};
class ConsoleTest: public ::testing::Test {