2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/config/CryConfigCreator.h>
|
|
|
|
#include <cryfs/impl/config/CryCipher.h>
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <cpp-utils/crypto/symmetric/ciphers.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include "../../impl/testutils/MockConsole.h"
|
|
|
|
#include "../../impl/testutils/TestWithFakeHomeDirectory.h"
|
2016-09-25 02:50:28 +02:00
|
|
|
#include <cpp-utils/io/NoninteractiveConsole.h>
|
2016-03-26 17:09:07 +01:00
|
|
|
#include <gitversion/gitversion.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/localstate/LocalStateDir.h>
|
2015-10-22 18:48:14 +02:00
|
|
|
|
|
|
|
using namespace cryfs;
|
|
|
|
|
|
|
|
using boost::none;
|
2016-09-25 02:50:28 +02:00
|
|
|
using cpputils::NoninteractiveConsole;
|
2015-10-22 18:48:14 +02:00
|
|
|
using std::string;
|
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::Return;
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using ::testing::UnorderedElementsAreArray;
|
2018-10-19 20:34:51 +02:00
|
|
|
using ::testing::NiceMock;
|
2015-10-22 18:48:14 +02:00
|
|
|
|
2016-03-01 17:45:48 +01:00
|
|
|
#define EXPECT_ASK_TO_USE_DEFAULT_SETTINGS() \
|
2016-09-25 02:50:28 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo("Use default settings?", true)).Times(1)
|
2016-03-01 17:45:48 +01:00
|
|
|
#define EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS() \
|
2016-09-25 02:50:28 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo("Use default settings?", true)).Times(0)
|
2016-03-01 17:45:48 +01:00
|
|
|
#define EXPECT_ASK_FOR_CIPHER() \
|
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block cipher"), UnorderedElementsAreArray(CryCiphers::supportedCipherNames()))).Times(1)
|
|
|
|
#define EXPECT_DOES_NOT_ASK_FOR_CIPHER() \
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block cipher"), testing::_)).Times(0)
|
2016-03-01 17:45:48 +01:00
|
|
|
#define EXPECT_ASK_FOR_BLOCKSIZE() \
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block size"), testing::_)).Times(1)
|
2016-03-01 17:45:48 +01:00
|
|
|
#define EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE() \
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block size"), testing::_)).Times(0)
|
2016-06-27 08:24:32 +02:00
|
|
|
#define EXPECT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION() \
|
2016-09-25 20:05:38 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("missing block"), false)).Times(1)
|
2016-06-27 08:24:32 +02:00
|
|
|
#define EXPECT_DOES_NOT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION() \
|
2016-09-25 20:05:38 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("missing block"), false)).Times(0)
|
2016-06-27 08:24:32 +02:00
|
|
|
#define IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION() \
|
2016-09-25 20:05:38 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("missing block"), false))
|
2016-03-01 17:45:48 +01:00
|
|
|
|
2017-09-30 10:03:19 +02:00
|
|
|
class CryConfigCreatorTest: public ::testing::Test, TestWithFakeHomeDirectory {
|
2015-10-22 18:48:14 +02:00
|
|
|
public:
|
|
|
|
CryConfigCreatorTest()
|
2018-10-19 20:34:51 +02:00
|
|
|
: console(make_shared<NiceMock<MockConsole>>()),
|
2018-04-22 07:04:21 +02:00
|
|
|
tempLocalStateDir(), localStateDir(tempLocalStateDir.path()),
|
|
|
|
creator(console, cpputils::Random::PseudoRandom(), localStateDir),
|
|
|
|
noninteractiveCreator(make_shared<NoninteractiveConsole>(console), cpputils::Random::PseudoRandom(), localStateDir) {
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block cipher"), testing::_)).WillRepeatedly(ChooseAnyCipher());
|
|
|
|
EXPECT_CALL(*console, ask(HasSubstr("block size"), testing::_)).WillRepeatedly(Return(0));
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
2018-10-19 20:34:51 +02:00
|
|
|
shared_ptr<NiceMock<MockConsole>> console;
|
2018-04-22 07:04:21 +02:00
|
|
|
cpputils::TempDir tempLocalStateDir;
|
|
|
|
LocalStateDir localStateDir;
|
2015-10-22 18:48:14 +02:00
|
|
|
CryConfigCreator creator;
|
2016-02-21 01:34:21 +01:00
|
|
|
CryConfigCreator noninteractiveCreator;
|
2015-10-22 18:48:14 +02:00
|
|
|
|
2016-03-01 17:45:48 +01:00
|
|
|
void AnswerNoToDefaultSettings() {
|
|
|
|
EXPECT_ASK_TO_USE_DEFAULT_SETTINGS().WillOnce(Return(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnswerYesToDefaultSettings() {
|
|
|
|
EXPECT_ASK_TO_USE_DEFAULT_SETTINGS().WillOnce(Return(true));
|
|
|
|
}
|
|
|
|
};
|
2015-10-22 18:48:14 +02:00
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2016-01-25 14:33:40 +01:00
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher());
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2015-10-30 19:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2016-01-25 14:33:40 +01:00
|
|
|
EXPECT_DOES_NOT_ASK_FOR_CIPHER();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(string("aes-256-gcm"), none, none, false).config;
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:45:48 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfUsingDefaultSettings) {
|
|
|
|
AnswerYesToDefaultSettings();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_CIPHER();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2016-03-01 17:45:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 01:34:21 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfNoninteractive) {
|
2016-03-01 17:45:48 +01:00
|
|
|
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
|
2016-02-21 01:34:21 +01:00
|
|
|
EXPECT_DOES_NOT_ASK_FOR_CIPHER();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2016-02-21 01:34:21 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:45:48 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesAskForBlocksizeIfNotSpecified) {
|
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2016-03-01 17:45:48 +01:00
|
|
|
EXPECT_ASK_FOR_BLOCKSIZE().WillOnce(Return(1));
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2016-03-01 17:45:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:12:41 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfSpecified) {
|
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2016-03-04 23:12:41 +01:00
|
|
|
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, 10*1024u, none, false).config;
|
2016-03-04 23:12:41 +01:00
|
|
|
}
|
2016-03-01 17:45:48 +01:00
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfNoninteractive) {
|
|
|
|
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2016-03-01 17:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfUsingDefaultSettings) {
|
|
|
|
AnswerYesToDefaultSettings();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2016-06-27 08:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesAskWhetherMissingBlocksAreIntegrityViolationsIfNotSpecified) {
|
|
|
|
AnswerNoToDefaultSettings();
|
|
|
|
EXPECT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION().WillOnce(Return(true));
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2016-06-27 08:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskWhetherMissingBlocksAreIntegrityViolationsIfSpecified_True) {
|
|
|
|
AnswerNoToDefaultSettings();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, true, false).config;
|
2016-06-27 08:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskWhetherMissingBlocksAreIntegrityViolationsIfSpecified_False) {
|
|
|
|
AnswerNoToDefaultSettings();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, false, false).config;
|
2016-06-27 08:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskWhetherMissingBlocksAreIntegrityViolationsIfNoninteractive) {
|
|
|
|
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2016-06-27 08:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskWhetherMissingBlocksAreIntegrityViolationsIfUsingDefaultSettings) {
|
|
|
|
AnswerYesToDefaultSettings();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2016-03-01 17:45:48 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 18:48:14 +02:00
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesEmptyRootBlobId) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
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) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2015-10-22 18:48:14 +02:00
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("mars-448-gcm"));
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
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) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2015-10-22 18:48:14 +02:00
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-256-gcm"));
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2015-10-30 19:53:15 +01:00
|
|
|
cpputils::AES256_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) {
|
2016-03-01 17:45:48 +01:00
|
|
|
AnswerNoToDefaultSettings();
|
2016-06-27 08:24:32 +02:00
|
|
|
IGNORE_ASK_FOR_MISSINGBLOCKISINTEGRITYVIOLATION();
|
2015-10-22 18:48:14 +02:00
|
|
|
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-128-gcm"));
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = creator.create(none, none, none, false).config;
|
2015-10-30 19:53:15 +01:00
|
|
|
cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
|
2015-10-22 18:48:14 +02:00
|
|
|
}
|
2016-03-01 17:45:48 +01:00
|
|
|
|
2016-03-04 23:12:41 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, DoesNotAskForAnythingIfEverythingIsSpecified) {
|
|
|
|
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
|
|
|
|
EXPECT_DOES_NOT_ASK_FOR_CIPHER();
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(string("aes-256-gcm"), 10*1024u, none, false).config;
|
2016-03-04 23:12:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 17:09:07 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, SetsCorrectCreatedWithVersion) {
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2016-03-26 17:09:07 +01:00
|
|
|
EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion());
|
|
|
|
}
|
|
|
|
|
2018-02-03 18:08:03 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, SetsCorrectLastOpenedWithVersion) {
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2018-02-03 18:08:03 +01:00
|
|
|
EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion());
|
|
|
|
}
|
|
|
|
|
2016-03-26 17:09:07 +01:00
|
|
|
TEST_F(CryConfigCreatorTest, SetsCorrectVersion) {
|
2018-02-08 08:08:01 +01:00
|
|
|
CryConfig config = noninteractiveCreator.create(none, none, none, false).config;
|
2018-02-03 18:08:03 +01:00
|
|
|
EXPECT_EQ(CryConfig::FilesystemFormatVersion, config.Version());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:45:48 +01:00
|
|
|
//TODO Add test cases ensuring that the values entered are correctly taken
|