2015-10-22 18:48:14 +02:00
|
|
|
#include <google/gtest/gtest.h>
|
|
|
|
#include <google/gmock/gmock.h>
|
|
|
|
#include "../../src/config/CryConfigCreator.h"
|
|
|
|
#include "../../src/config/CryCipher.h"
|
2015-10-27 23:46:54 +01:00
|
|
|
#include <messmer/cpp-utils/crypto/symmetric/ciphers.h>
|
2015-10-22 18:48:14 +02:00
|
|
|
#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;
|
2016-01-17 14:57:40 +01:00
|
|
|
using std::shared_ptr;
|
|
|
|
using std::make_shared;
|
2015-10-22 18:48:14 +02:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::Invoke;
|
|
|
|
using ::testing::ValuesIn;
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using ::testing::UnorderedElementsAreArray;
|
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
|
|
|
|
class CryConfigCreatorTest: public ::testing::Test {
|
|
|
|
public:
|
|
|
|
CryConfigCreatorTest()
|
2016-01-17 14:57:40 +01:00
|
|
|
: console(make_shared<MockConsole>()),
|
|
|
|
creator(console, cpputils::Random::PseudoRandom()) {
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
2016-01-17 14:57:40 +01:00
|
|
|
shared_ptr<MockConsole> console;
|
2015-10-22 18:48:14 +02:00
|
|
|
CryConfigCreator creator;
|
|
|
|
};
|
|
|
|
|
2016-01-25 14:33:40 +01:00
|
|
|
#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);
|
2015-10-22 18:48:14 +02:00
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) {
|
2016-01-25 14:33:40 +01:00
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher());
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(none);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) {
|
2016-01-25 14:33:40 +01:00
|
|
|
EXPECT_DOES_NOT_ASK_FOR_CIPHER();
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(string("aes-256-gcm"));
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesEmptyRootBlobId) {
|
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher());
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(none);
|
2015-10-22 18:48:14 +02:00
|
|
|
EXPECT_EQ("", config.RootBlob()); // This tells CryFS to create a new root blob
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_448) {
|
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("mars-448-gcm"));
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(none);
|
2015-10-27 23:46:54 +01:00
|
|
|
cpputils::Mars448_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_256) {
|
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-256-gcm"));
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(none);
|
|
|
|
cpputils::AES256_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) {
|
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-128-gcm"));
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfig config = creator.create(none);
|
|
|
|
cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|