From af4ef5d42568215eccd8281659735748993de507 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Mon, 25 Jan 2016 14:33:40 +0100 Subject: [PATCH] Offer a default configuration when creating new filesystems --- ChangeLog.txt | 7 ++++--- src/config/CryConfigConsole.cpp | 21 +++++++++++++++++++-- src/config/CryConfigConsole.h | 8 ++++++-- test/config/CryConfigConsoleTest.cpp | 13 +++++++++++-- test/config/CryConfigCreatorTest.cpp | 11 ++++++++--- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d5ce2e55..732a0f5f 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,7 @@ Version 0.8.5 --------------- * Fix package manager warning when installing the .deb package +* Offer a default configuration when creating new filesystems Version 0.8.4 --------------- @@ -10,15 +11,15 @@ Version 0.8.4 Version 0.8.3 --------------- -* Ask for password confirmation when creating new filesystem. +* Ask for password confirmation when creating new filesystem * Check for new CryFS versions and ask the user to update if a new version is available * Implemented a mechanism that can show warnings about security bugs to users of a certain CryFS version. Let's hope this won't be necessary ;) * Compatibility with GCC 4.8 (that allows compiling on Ubuntu 14.04 for example) Version 0.8.2 --------------- -* Mount directory, base directory, logfile and config file can be specified as relative paths. -* Improved error messages. +* Mount directory, base directory, logfile and config file can be specified as relative paths +* Improved error messages Version 0.8.1 --------------- diff --git a/src/config/CryConfigConsole.cpp b/src/config/CryConfigConsole.cpp index 200d8ccc..b69b9fdb 100644 --- a/src/config/CryConfigConsole.cpp +++ b/src/config/CryConfigConsole.cpp @@ -10,11 +10,21 @@ using std::vector; using std::shared_ptr; namespace cryfs { + constexpr const char *CryConfigConsole::DEFAULT_CIPHER; + CryConfigConsole::CryConfigConsole(shared_ptr console) - : _console(std::move(console)) { + : _console(std::move(console)), _useDefaultSettings(none) { } - string CryConfigConsole::askCipher() const { + string CryConfigConsole::askCipher() { + if (_checkUseDefaultSettings()) { + return DEFAULT_CIPHER; + } else { + return _askCipher(); + } + } + + string CryConfigConsole::_askCipher() const { vector ciphers = CryCiphers::supportedCipherNames(); string cipherName = ""; bool askAgain = true; @@ -34,4 +44,11 @@ namespace cryfs { } return _console->askYesNo(string() + (*warning) + " Do you want to take this cipher nevertheless?"); } + + bool CryConfigConsole::_checkUseDefaultSettings() { + if (_useDefaultSettings == none) { + _useDefaultSettings = _console->askYesNo("Use default settings?"); + } + return *_useDefaultSettings; + } } diff --git a/src/config/CryConfigConsole.h b/src/config/CryConfigConsole.h index 39e7f4ef..53e01266 100644 --- a/src/config/CryConfigConsole.h +++ b/src/config/CryConfigConsole.h @@ -12,14 +12,18 @@ namespace cryfs { CryConfigConsole(std::shared_ptr console); CryConfigConsole(CryConfigConsole &&rhs) = default; - std::string askCipher() const; + std::string askCipher(); - private: static constexpr const char *DEFAULT_CIPHER = "aes-256-gcm"; + private: + bool _checkUseDefaultSettings(); + + std::string _askCipher() const; bool _showWarningForCipherAndReturnIfOk(const std::string &cipherName) const; std::shared_ptr _console; + boost::optional _useDefaultSettings; DISALLOW_COPY_AND_ASSIGN(CryConfigConsole); }; diff --git a/test/config/CryConfigConsoleTest.cpp b/test/config/CryConfigConsoleTest.cpp index 7247da4b..713a8fe1 100644 --- a/test/config/CryConfigConsoleTest.cpp +++ b/test/config/CryConfigConsoleTest.cpp @@ -36,13 +36,22 @@ public: class CryConfigConsoleTest_Cipher: public CryConfigConsoleTest {}; -#define EXPECT_ASK_FOR_CIPHER() EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))) +#define EXPECT_ASK_FOR_CIPHER() \ + EXPECT_CALL(*console, askYesNo("Use default settings?")).Times(1).WillOnce(Return(false)); \ + EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))).Times(1) TEST_F(CryConfigConsoleTest_Cipher, AsksForCipher) { - EXPECT_ASK_FOR_CIPHER().Times(1).WillOnce(ChooseAnyCipher()); + EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher()); cryconsole.askCipher(); } +TEST_F(CryConfigConsoleTest_Cipher, ChooseDefaultCipher) { + EXPECT_CALL(*console, askYesNo("Use default settings?")).Times(1).WillOnce(Return(true)); + EXPECT_CALL(*console, ask(HasSubstr("block cipher"), _)).Times(0); + string cipher = cryconsole.askCipher(); + EXPECT_EQ(CryConfigConsole::DEFAULT_CIPHER, cipher); +} + class CryConfigConsoleTest_Cipher_Choose: public CryConfigConsoleTest_Cipher, public ::testing::WithParamInterface { public: string cipherName = GetParam(); diff --git a/test/config/CryConfigCreatorTest.cpp b/test/config/CryConfigCreatorTest.cpp index 4aa0ceb2..861acb90 100644 --- a/test/config/CryConfigCreatorTest.cpp +++ b/test/config/CryConfigCreatorTest.cpp @@ -34,15 +34,20 @@ public: CryConfigCreator creator; }; -#define EXPECT_ASK_FOR_CIPHER() EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))) +#define EXPECT_ASK_FOR_CIPHER() \ + EXPECT_CALL(*console, askYesNo("Use default settings?")).Times(1).WillOnce(Return(false)); \ + EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))).Times(1) +#define EXPECT_DOES_NOT_ASK_FOR_CIPHER() \ + EXPECT_CALL(*console, askYesNo("Use default settings?")).Times(0); \ + EXPECT_CALL(*console, ask(HasSubstr("block cipher"), _)).Times(0); TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) { - EXPECT_ASK_FOR_CIPHER().Times(1).WillOnce(ChooseAnyCipher()); + EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher()); CryConfig config = creator.create(none); } TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) { - EXPECT_ASK_FOR_CIPHER().Times(0); + EXPECT_DOES_NOT_ASK_FOR_CIPHER(); CryConfig config = creator.create(string("aes-256-gcm")); }