From fd11436fb967a45affb24461d082aba3cc203073 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 26 Jul 2015 13:09:55 +0200 Subject: [PATCH] Test cases don't need user interaction anymore --- src/CryConfigLoader.cpp | 13 +++++++------ src/CryConfigLoader.h | 31 +++++++++++++++++++------------ src/main.cpp | 2 +- src/utils/Console.cpp | 10 +++++++--- src/utils/Console.h | 17 ++++++++++++----- test/CryFsTest.cpp | 20 ++++++++++++++------ test/FileSystemTest.cpp | 2 +- test/utils/ConsoleTest.cpp | 2 +- 8 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/CryConfigLoader.cpp b/src/CryConfigLoader.cpp index 8c0bd71a..4fa43352 100644 --- a/src/CryConfigLoader.cpp +++ b/src/CryConfigLoader.cpp @@ -1,6 +1,5 @@ #include "CryConfigLoader.h" #include -#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()) {} + +CryConfigLoader::CryConfigLoader(unique_ref console) : _console(std::move(console)) {} + unique_ref 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 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) { diff --git a/src/CryConfigLoader.h b/src/CryConfigLoader.h index 90e94275..ded78dd5 100644 --- a/src/CryConfigLoader.h +++ b/src/CryConfigLoader.h @@ -6,31 +6,38 @@ #include #include "CryConfig.h" #include +#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 loadOrCreate(const boost::filesystem::path &filename); + CryConfigLoader(); + explicit CryConfigLoader(cpputils::unique_ref console); - static cpputils::unique_ref createNew(const boost::filesystem::path &filename); - static boost::optional> loadExisting(const boost::filesystem::path &filename); + cpputils::unique_ref loadOrCreate(const boost::filesystem::path &filename); + + cpputils::unique_ref createNew(const boost::filesystem::path &filename); + boost::optional> 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 loadOrCreateWithWeakKey(const boost::filesystem::path &filename); - static cpputils::unique_ref createNewWithWeakKey(const boost::filesystem::path &filename); + cpputils::unique_ref loadOrCreateWithWeakKey(const boost::filesystem::path &filename); + cpputils::unique_ref 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; }; } diff --git a/src/main.cpp b/src/main.cpp index e290b755..a520c5a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ using cpputils::make_unique_ref; int main (int argc, char *argv[]) { auto blockStore = make_unique_ref(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); diff --git a/src/utils/Console.cpp b/src/utils/Console.cpp index 3753296a..a3f65220 100644 --- a/src/utils/Console.cpp +++ b/src/utils/Console.cpp @@ -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 parseInt(const string &str) { @@ -34,7 +34,7 @@ optional parseInt(const string &str) { } } -unsigned int Console::ask(const string &question, const vector &options) { +unsigned int IOStreamConsole::ask(const string &question, const vector &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 &options) } while(choice == none || *choice < 1 || static_cast(*choice) > options.size()); return *choice-1; } + +void IOStreamConsole::print(const std::string &output) { + _output << output << std::flush; +} diff --git a/src/utils/Console.h b/src/utils/Console.h index 6d56710b..7412f812 100644 --- a/src/utils/Console.h +++ b/src/utils/Console.h @@ -6,13 +6,20 @@ #include #include -//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 &options); + virtual unsigned int ask(const std::string &question, const std::vector &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 &options) override; + + //TODO Test print() + void print(const std::string &output) override; private: std::ostream &_output; std::istream &_input; diff --git a/test/CryFsTest.cpp b/test/CryFsTest.cpp index 26033417..ebff606b 100644 --- a/test/CryFsTest.cpp +++ b/test/CryFsTest.cpp @@ -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 &) 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(rootdir.path())); + CryDevice dev(CryConfigLoader().createNewWithWeakKey(config.path()), make_unique_ref(rootdir.path())); } - CryDevice dev(CryConfigLoader::loadExisting(config.path()).value(), make_unique_ref(rootdir.path())); + CryDevice dev(CryConfigLoader().loadExisting(config.path()).value(), make_unique_ref(rootdir.path())); auto root = dev.Load(bf::path("/")); dynamic_pointer_move(root.get()).get()->children(); } TEST_F(CryFsTest, UsingStrongKey1_CreatedRootdirIsLoadableAfterClosing) { { - CryDevice dev(CryConfigLoader::createNew(config.path()), make_unique_ref(rootdir.path())); + CryDevice dev(CryConfigLoader(make_unique_ref()).createNew(config.path()), make_unique_ref(rootdir.path())); } - CryDevice dev(CryConfigLoader::loadExisting(config.path()).value(), make_unique_ref(rootdir.path())); + CryDevice dev(CryConfigLoader().loadExisting(config.path()).value(), make_unique_ref(rootdir.path())); auto root = dev.Load(bf::path("/")); dynamic_pointer_move(root.get()).get()->children(); } TEST_F(CryFsTest, UsingStrongKey2_CreatedRootdirIsLoadableAfterClosing) { { - CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique_ref(rootdir.path())); + CryDevice dev(CryConfigLoader(make_unique_ref()).loadOrCreate(config.path()), make_unique_ref(rootdir.path())); } - CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique_ref(rootdir.path())); + CryDevice dev(CryConfigLoader().loadOrCreate(config.path()), make_unique_ref(rootdir.path())); auto root = dev.Load(bf::path("/")); dynamic_pointer_move(root.get()).get()->children(); } diff --git a/test/FileSystemTest.cpp b/test/FileSystemTest.cpp index a2b704c7..9c7db546 100644 --- a/test/FileSystemTest.cpp +++ b/test/FileSystemTest.cpp @@ -21,7 +21,7 @@ public: unique_ref createDevice() override { auto blockStore = cpputils::make_unique_ref(); - auto config = CryConfigLoader::loadOrCreateWithWeakKey(configFile.path()); + auto config = CryConfigLoader().loadOrCreateWithWeakKey(configFile.path()); return make_unique_ref(std::move(config), std::move(blockStore)); } diff --git a/test/utils/ConsoleTest.cpp b/test/utils/ConsoleTest.cpp index 0369ea77..b3811dd9 100644 --- a/test/utils/ConsoleTest.cpp +++ b/test/utils/ConsoleTest.cpp @@ -23,7 +23,7 @@ public: }); } private: - Console _console; + IOStreamConsole _console; }; class ConsoleTest: public ::testing::Test {