2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/config/CryPasswordBasedKeyProvider.h>
|
2018-10-22 04:31:08 +02:00
|
|
|
#include <gmock/gmock.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include "../../impl/testutils/MockConsole.h"
|
2018-10-22 04:31:08 +02:00
|
|
|
#include <cpp-utils/data/DataFixture.h>
|
|
|
|
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using cpputils::EncryptionKey;
|
|
|
|
using cpputils::PasswordBasedKDF;
|
|
|
|
using cpputils::Data;
|
|
|
|
using cpputils::DataFixture;
|
|
|
|
using std::shared_ptr;
|
|
|
|
using std::make_shared;
|
|
|
|
using std::string;
|
|
|
|
using cryfs::CryPasswordBasedKeyProvider;
|
|
|
|
using testing::Return;
|
|
|
|
using testing::Invoke;
|
|
|
|
using testing::Eq;
|
|
|
|
using testing::StrEq;
|
|
|
|
using testing::NiceMock;
|
|
|
|
|
2021-01-13 04:48:27 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-10-22 04:31:08 +02:00
|
|
|
class MockCallable {
|
|
|
|
public:
|
2019-10-13 17:49:57 +02:00
|
|
|
MOCK_METHOD(std::string, call, ());
|
2018-10-22 04:31:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockKDF : public PasswordBasedKDF {
|
|
|
|
public:
|
2019-10-13 17:49:57 +02:00
|
|
|
MOCK_METHOD(EncryptionKey, deriveExistingKey, (size_t keySize, const string& password, const Data& kdfParameters), (override));
|
|
|
|
MOCK_METHOD(KeyResult, deriveNewKey, (size_t keySize, const string& password), (override));
|
2018-10-22 04:31:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class CryPasswordBasedKeyProviderTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
CryPasswordBasedKeyProviderTest()
|
|
|
|
: mockConsole(make_shared<NiceMock<MockConsole>>())
|
|
|
|
, askPasswordForNewFilesystem()
|
|
|
|
, askPasswordForExistingFilesystem()
|
|
|
|
, kdf_(make_unique_ref<MockKDF>())
|
|
|
|
, kdf(kdf_.get())
|
|
|
|
, keyProvider(mockConsole, [this] () {return askPasswordForExistingFilesystem.call();}, [this] () {return askPasswordForNewFilesystem.call(); }, std::move(kdf_)) {}
|
|
|
|
|
|
|
|
shared_ptr<NiceMock<MockConsole>> mockConsole;
|
|
|
|
MockCallable askPasswordForNewFilesystem;
|
|
|
|
MockCallable askPasswordForExistingFilesystem;
|
|
|
|
unique_ref<MockKDF> kdf_;
|
|
|
|
MockKDF* kdf;
|
|
|
|
|
|
|
|
CryPasswordBasedKeyProvider keyProvider;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CryPasswordBasedKeyProviderTest, requestKeyForNewFilesystem) {
|
|
|
|
constexpr size_t keySize = 512;
|
|
|
|
constexpr const char* password = "mypassword";
|
|
|
|
const EncryptionKey key = EncryptionKey::FromString(DataFixture::generate(keySize).ToString());
|
|
|
|
const Data kdfParameters = DataFixture::generate(100);
|
|
|
|
|
|
|
|
EXPECT_CALL(askPasswordForNewFilesystem, call()).Times(1).WillOnce(Return(password));
|
|
|
|
EXPECT_CALL(askPasswordForExistingFilesystem, call()).Times(0);
|
|
|
|
EXPECT_CALL(*kdf, deriveNewKey(Eq(keySize), StrEq(password))).Times(1).WillOnce(Invoke([&] (auto, auto) {return PasswordBasedKDF::KeyResult{key, kdfParameters.copy()};}));
|
|
|
|
|
|
|
|
auto returned_key = keyProvider.requestKeyForNewFilesystem(keySize);
|
|
|
|
|
|
|
|
EXPECT_EQ(key.ToString(), returned_key.key.ToString());
|
|
|
|
EXPECT_EQ(kdfParameters, returned_key.kdfParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryPasswordBasedKeyProviderTest, requestKeyForExistingFilesystem) {
|
|
|
|
constexpr size_t keySize = 512;
|
|
|
|
constexpr const char* password = "mypassword";
|
|
|
|
const EncryptionKey key = EncryptionKey::FromString(DataFixture::generate(keySize).ToString());
|
|
|
|
const Data kdfParameters = DataFixture::generate(100);
|
|
|
|
|
|
|
|
EXPECT_CALL(askPasswordForNewFilesystem, call()).Times(0);
|
|
|
|
EXPECT_CALL(askPasswordForExistingFilesystem, call()).Times(1).WillOnce(Return(password));
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*kdf, deriveExistingKey(Eq(keySize), StrEq(password), testing::_)).Times(1).WillOnce(Invoke([&] (auto, auto, const auto& kdfParams) {
|
2018-10-22 04:31:08 +02:00
|
|
|
EXPECT_EQ(kdfParameters, kdfParams);
|
|
|
|
return key;
|
|
|
|
}));
|
|
|
|
|
2023-07-08 23:48:59 +02:00
|
|
|
const EncryptionKey returned_key = keyProvider.requestKeyForExistingFilesystem(keySize, kdfParameters);
|
2018-10-22 04:31:08 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(key.ToString(), returned_key.ToString());
|
|
|
|
}
|
2021-01-13 04:48:27 +01:00
|
|
|
|
|
|
|
}
|