Added test case for CryConfigConsole

This commit is contained in:
Sebastian Messmer 2016-01-25 14:03:32 +01:00
parent 16bdbcc2ca
commit e5a2cec5ea
3 changed files with 73 additions and 33 deletions

View File

@ -10,10 +10,6 @@ namespace cryfs {
//TODO Test //TODO Test
class CryConfigConsole final { class CryConfigConsole final {
public: public:
struct Config {
std::string cipher;
};
CryConfigConsole(std::shared_ptr<cpputils::Console> console); CryConfigConsole(std::shared_ptr<cpputils::Console> console);
CryConfigConsole(CryConfigConsole &&rhs) = default; CryConfigConsole(CryConfigConsole &&rhs) = default;

View File

@ -0,0 +1,73 @@
#include <google/gtest/gtest.h>
#include <google/gmock/gmock.h>
#include "../../src/config/CryConfigConsole.h"
#include "../../src/config/CryCipher.h"
#include <messmer/cpp-utils/crypto/symmetric/ciphers.h>
#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<MockConsole>()),
cryconsole(console) {
}
shared_ptr<MockConsole> 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<string> {
public:
string cipherName = GetParam();
optional<string> 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()));

View File

@ -69,32 +69,3 @@ TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) {
CryConfig config = creator.create(none); CryConfig config = creator.create(none);
cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
} }
class CryConfigCreatorTest_ChooseCipher: public CryConfigCreatorTest, public ::testing::WithParamInterface<string> {
public:
string cipherName = GetParam();
optional<string> 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()));