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

View File

@ -6,31 +6,38 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include "CryConfig.h" #include "CryConfig.h"
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h> #include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
#include "utils/Console.h"
namespace cryfs { namespace cryfs {
class CryConfigLoader { class CryConfigLoader {
public: public:
//TODO Get rid of this and use dynamically configured Cipher instead
using Cipher = blockstore::encrypted::AES256_GCM; 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); cpputils::unique_ref<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
static boost::optional<cpputils::unique_ref<CryConfig>> loadExisting(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. //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); 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> createNewWithWeakKey(const boost::filesystem::path &filename);
private: private:
static void _initializeConfig(CryConfig *config); void _initializeConfig(CryConfig *config);
static void _generateCipher(CryConfig *config); void _generateCipher(CryConfig *config);
static void _generateEncKey(CryConfig *config); void _generateEncKey(CryConfig *config);
static void _generateRootBlobKey(CryConfig *config); void _generateRootBlobKey(CryConfig *config);
static void _initializeConfigWithWeakKey(CryConfig *config); // TODO Rename to _initializeConfigForTest void _initializeConfigWithWeakKey(CryConfig *config); // TODO Rename to _initializeConfigForTest
static void _generateWeakEncKey(CryConfig *config); // TODO Rename to _generateTestEncKey void _generateWeakEncKey(CryConfig *config); // TODO Rename to _generateTestEncKey
static void _generateTestCipher(CryConfig *config); 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[]) int main (int argc, char *argv[])
{ {
auto blockStore = make_unique_ref<OnDiskBlockStore>(bf::path("/home/heinzi/cryfstest/root")); 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)); cryfs::CryDevice device(std::move(config), std::move(blockStore));
fspp::FilesystemImpl fsimpl(&device); fspp::FilesystemImpl fsimpl(&device);
fspp::fuse::Fuse fuse(&fsimpl); fspp::fuse::Fuse fuse(&fsimpl);

View File

@ -12,10 +12,10 @@ using std::getline;
using boost::optional; using boost::optional;
using boost::none; 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) { 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) { if(options.size() == 0) {
throw std::invalid_argument("options should have at least one entry"); 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()); } while(choice == none || *choice < 1 || static_cast<unsigned int>(*choice) > options.size());
return *choice-1; return *choice-1;
} }
void IOStreamConsole::print(const std::string &output) {
_output << output << std::flush;
}

View File

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

View File

@ -15,11 +15,19 @@ using cpputils::TempDir;
using cpputils::TempFile; using cpputils::TempFile;
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
using cpputils::make_unique_ref; using cpputils::make_unique_ref;
using cpputils::unique_ref;
using blockstore::ondisk::OnDiskBlockStore; using blockstore::ondisk::OnDiskBlockStore;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using namespace cryfs; 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 { class CryFsTest: public Test {
public: public:
CryFsTest(): rootdir(), config(false) {} CryFsTest(): rootdir(), config(false) {}
@ -29,27 +37,27 @@ public:
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) { 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("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root.get()).get()->children(); dynamic_pointer_move<CryDir>(root.get()).get()->children();
} }
TEST_F(CryFsTest, UsingStrongKey1_CreatedRootdirIsLoadableAfterClosing) { 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("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root.get()).get()->children(); dynamic_pointer_move<CryDir>(root.get()).get()->children();
} }
TEST_F(CryFsTest, UsingStrongKey2_CreatedRootdirIsLoadableAfterClosing) { 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("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root.get()).get()->children(); dynamic_pointer_move<CryDir>(root.get()).get()->children();
} }

View File

@ -21,7 +21,7 @@ public:
unique_ref<Device> createDevice() override { unique_ref<Device> createDevice() override {
auto blockStore = cpputils::make_unique_ref<FakeBlockStore>(); 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)); return make_unique_ref<CryDevice>(std::move(config), std::move(blockStore));
} }

View File

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