diff --git a/src/config/CryConfigConsole.h b/src/config/CryConfigConsole.h index 0b8fea92..e807c8de 100644 --- a/src/config/CryConfigConsole.h +++ b/src/config/CryConfigConsole.h @@ -10,10 +10,6 @@ namespace cryfs { //TODO Test class CryConfigConsole final { public: - struct Config { - std::string cipher; - }; - CryConfigConsole(std::shared_ptr console); CryConfigConsole(CryConfigConsole &&rhs) = default; diff --git a/test/config/CryConfigConsoleTest.cpp b/test/config/CryConfigConsoleTest.cpp new file mode 100644 index 00000000..7247da4b --- /dev/null +++ b/test/config/CryConfigConsoleTest.cpp @@ -0,0 +1,73 @@ +#include +#include +#include "../../src/config/CryConfigConsole.h" +#include "../../src/config/CryCipher.h" +#include +#include "../testutils/MockConsole.h" + +using namespace cryfs; + +using boost::optional; +using boost::none; +using cpputils::Console; +using cpputils::unique_ref; +using cpputils::make_unique_ref; +using std::string; +using std::vector; +using std::shared_ptr; +using std::make_shared; +using ::testing::_; +using ::testing::Return; +using ::testing::Invoke; +using ::testing::ValuesIn; +using ::testing::HasSubstr; +using ::testing::UnorderedElementsAreArray; +using ::testing::WithParamInterface; + +class CryConfigConsoleTest: public ::testing::Test { +public: + CryConfigConsoleTest() + : console(make_shared()), + cryconsole(console) { + } + shared_ptr console; + CryConfigConsole cryconsole; +}; + +class CryConfigConsoleTest_Cipher: public CryConfigConsoleTest {}; + +#define EXPECT_ASK_FOR_CIPHER() EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))) + +TEST_F(CryConfigConsoleTest_Cipher, AsksForCipher) { + EXPECT_ASK_FOR_CIPHER().Times(1).WillOnce(ChooseAnyCipher()); + cryconsole.askCipher(); +} + +class CryConfigConsoleTest_Cipher_Choose: public CryConfigConsoleTest_Cipher, public ::testing::WithParamInterface { +public: + string cipherName = GetParam(); + optional cipherWarning = CryCiphers::find(cipherName).warning(); + + void EXPECT_DONT_SHOW_WARNING() { + EXPECT_CALL(*console, askYesNo(_)).Times(0); + } + + void EXPECT_SHOW_WARNING(const string &warning) { + EXPECT_CALL(*console, askYesNo(HasSubstr(warning))).WillOnce(Return(true)); + } +}; + +TEST_P(CryConfigConsoleTest_Cipher_Choose, ChoosesCipherCorrectly) { + if (cipherWarning == none) { + EXPECT_DONT_SHOW_WARNING(); + } else { + EXPECT_SHOW_WARNING(*cipherWarning); + } + + EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher(cipherName)); + + string chosenCipher = cryconsole.askCipher(); + EXPECT_EQ(cipherName, chosenCipher); +} + +INSTANTIATE_TEST_CASE_P(CryConfigConsoleTest_Cipher_Choose, CryConfigConsoleTest_Cipher_Choose, ValuesIn(CryCiphers::supportedCipherNames())); diff --git a/test/config/CryConfigCreatorTest.cpp b/test/config/CryConfigCreatorTest.cpp index 2ff9b173..4aa0ceb2 100644 --- a/test/config/CryConfigCreatorTest.cpp +++ b/test/config/CryConfigCreatorTest.cpp @@ -69,32 +69,3 @@ TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) { CryConfig config = creator.create(none); cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid } - -class CryConfigCreatorTest_ChooseCipher: public CryConfigCreatorTest, public ::testing::WithParamInterface { -public: - string cipherName = GetParam(); - optional cipherWarning = CryCiphers::find(cipherName).warning(); - - void EXPECT_DONT_SHOW_WARNING() { - EXPECT_CALL(*console, askYesNo(_)).Times(0); - } - - void EXPECT_SHOW_WARNING(const string &warning) { - EXPECT_CALL(*console, askYesNo(HasSubstr(warning))).WillOnce(Return(true)); - } -}; - -TEST_P(CryConfigCreatorTest_ChooseCipher, ChoosesCipherCorrectly) { - if (cipherWarning == none) { - EXPECT_DONT_SHOW_WARNING(); - } else { - EXPECT_SHOW_WARNING(*cipherWarning); - } - - EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher(cipherName)); - - CryConfig config = creator.create(none); - EXPECT_EQ(cipherName, config.Cipher()); -} - -INSTANTIATE_TEST_CASE_P(CryConfigCreatorTest_ChooseCipher, CryConfigCreatorTest_ChooseCipher, ValuesIn(CryCiphers::supportedCipherNames()));