2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/config/CryConfigFile.h>
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <cpp-utils/tempfile/TempFile.h>
|
2018-12-22 00:58:30 +01:00
|
|
|
#include <cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include "../../impl/testutils/FakeCryKeyProvider.h"
|
2015-10-23 00:04:03 +02:00
|
|
|
|
|
|
|
using namespace cryfs;
|
|
|
|
using cpputils::TempFile;
|
2018-12-22 00:58:30 +01:00
|
|
|
using cpputils::unique_ref;
|
2015-10-24 19:35:37 +02:00
|
|
|
using std::string;
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2015-11-12 19:59:38 +01:00
|
|
|
using cpputils::Data;
|
2015-10-24 19:35:37 +02:00
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
//gtest/boost::optional workaround for working with optional<CryConfigFile>
|
|
|
|
namespace boost {
|
|
|
|
inline std::ostream &operator<<(std::ostream &out, const CryConfigFile &file) {
|
|
|
|
UNUSED(file);
|
2016-02-13 20:42:28 +01:00
|
|
|
out << "ConfigFile()";
|
2015-10-24 19:35:37 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 20:42:28 +01:00
|
|
|
#include <boost/optional/optional_io.hpp>
|
2015-10-23 00:04:03 +02:00
|
|
|
|
|
|
|
class CryConfigFileTest: public ::testing::Test {
|
|
|
|
public:
|
|
|
|
CryConfigFileTest(): file(false) {}
|
|
|
|
|
|
|
|
TempFile file;
|
|
|
|
|
2015-11-11 10:19:47 +01:00
|
|
|
CryConfig Config() {
|
|
|
|
CryConfig result;
|
|
|
|
result.SetCipher("aes-256-gcm");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> CreateAndLoadEmpty(unsigned char keySeed = 0) {
|
2018-10-22 04:31:08 +02:00
|
|
|
Create(Config(), keySeed);
|
2015-10-24 19:35:37 +02:00
|
|
|
return Load().value();
|
|
|
|
}
|
|
|
|
|
2018-10-22 04:31:08 +02:00
|
|
|
void Create(CryConfig cfg, unsigned int keySeed = 0) {
|
|
|
|
FakeCryKeyProvider keyProvider(keySeed);
|
|
|
|
CryConfigFile::create(file.path(), std::move(cfg), &keyProvider);
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 00:58:30 +01:00
|
|
|
optional<unique_ref<CryConfigFile>> Load(unsigned char keySeed = 0) {
|
2018-10-22 04:31:08 +02:00
|
|
|
FakeCryKeyProvider keyProvider(keySeed);
|
2020-07-19 22:26:13 +02:00
|
|
|
return CryConfigFile::load(file.path(), &keyProvider, CryConfigFile::Access::ReadWrite).right_opt();
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
2015-11-11 10:19:47 +01:00
|
|
|
void CreateWithCipher(const string &cipher) {
|
|
|
|
return CreateWithCipher(cipher, file);
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithCipher(const string &cipher, const TempFile &tempFile) {
|
|
|
|
CryConfig cfg;
|
|
|
|
cfg.SetCipher(cipher);
|
2018-10-22 04:31:08 +02:00
|
|
|
FakeCryKeyProvider keyProvider(0);
|
|
|
|
CryConfigFile::create(tempFile.path(), std::move(cfg), &keyProvider);
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
TEST_F(CryConfigFileTest, DoesntLoadIfWrongPassword) {
|
2018-10-22 04:31:08 +02:00
|
|
|
const unsigned char pw1 = 0;
|
|
|
|
const unsigned char pw2 = 1;
|
|
|
|
Create(Config(), pw1);
|
|
|
|
auto loaded = Load(pw2);
|
2015-10-24 19:35:37 +02:00
|
|
|
EXPECT_EQ(none, loaded);
|
|
|
|
}
|
|
|
|
|
2015-10-23 00:04:03 +02:00
|
|
|
TEST_F(CryConfigFileTest, RootBlob_Init) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
EXPECT_EQ("", created->config()->RootBlob());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, RootBlob_CreateAndLoad) {
|
2015-11-11 10:19:47 +01:00
|
|
|
CryConfig cfg = Config();
|
2015-10-23 00:04:03 +02:00
|
|
|
cfg.SetRootBlob("rootblobid");
|
|
|
|
Create(std::move(cfg));
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("rootblobid", loaded->config()->RootBlob());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, RootBlob_SaveAndLoad) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
created->config()->SetRootBlob("rootblobid");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("rootblobid", loaded->config()->RootBlob());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, EncryptionKey_Init) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
EXPECT_EQ("", created->config()->EncryptionKey());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, EncryptionKey_CreateAndLoad) {
|
2015-11-11 10:19:47 +01:00
|
|
|
CryConfig cfg = Config();
|
2015-10-23 00:04:03 +02:00
|
|
|
cfg.SetEncryptionKey("encryptionkey");
|
|
|
|
Create(std::move(cfg));
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("encryptionkey", loaded->config()->EncryptionKey());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, EncryptionKey_SaveAndLoad) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
created->config()->SetEncryptionKey("encryptionkey");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("encryptionkey", loaded->config()->EncryptionKey());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, Cipher_Init) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
EXPECT_EQ("aes-256-gcm", created->config()->Cipher());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, Cipher_CreateAndLoad) {
|
2015-11-11 10:19:47 +01:00
|
|
|
CryConfig cfg = Config();
|
|
|
|
cfg.SetCipher("twofish-128-cfb");
|
2015-10-23 00:04:03 +02:00
|
|
|
Create(std::move(cfg));
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("twofish-128-cfb", loaded->config()->Cipher());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, Cipher_SaveAndLoad) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
created->config()->SetCipher("twofish-128-cfb");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("twofish-128-cfb", loaded->config()->Cipher());
|
2015-10-23 00:04:03 +02:00
|
|
|
}
|
2015-10-24 19:35:37 +02:00
|
|
|
|
2016-03-26 17:09:07 +01:00
|
|
|
TEST_F(CryConfigFileTest, Version_Init) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
EXPECT_EQ("", created->config()->Version());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, Version_CreateAndLoad) {
|
|
|
|
CryConfig cfg = Config();
|
|
|
|
cfg.SetVersion("0.9.2");
|
|
|
|
Create(std::move(cfg));
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("0.9.2", loaded->config()->Version());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, Version_SaveAndLoad) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
created->config()->SetVersion("0.9.2");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("0.9.2", loaded->config()->Version());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, CreatedWithVersion_Init) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
EXPECT_EQ("", created->config()->Version());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, CreatedWithVersion_CreateAndLoad) {
|
|
|
|
CryConfig cfg = Config();
|
|
|
|
cfg.SetCreatedWithVersion("0.9.2");
|
|
|
|
Create(std::move(cfg));
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("0.9.2", loaded->config()->CreatedWithVersion());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, CreatedWithVersion_SaveAndLoad) {
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = CreateAndLoadEmpty();
|
|
|
|
created->config()->SetCreatedWithVersion("0.9.2");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("0.9.2", loaded->config()->CreatedWithVersion());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
//Test that the encrypted config file has the same size, no matter how big the plaintext config data.
|
|
|
|
TEST_F(CryConfigFileTest, ConfigFileHasFixedSize) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const TempFile file1(false);
|
|
|
|
const TempFile file2(false);
|
2015-11-11 10:19:47 +01:00
|
|
|
//It is important to have different cipher name lengths here, because they're on the outer encryption level.
|
|
|
|
//So this ensures that there also is a padding happening on the outer encryption level.
|
|
|
|
CreateWithCipher("aes-128-gcm", file1); // Short cipher name and short key
|
|
|
|
CreateWithCipher("twofish-256-cfb", file2); // Long cipher name and long key
|
2015-10-24 19:35:37 +02:00
|
|
|
EXPECT_EQ(bf::file_size(file1.path()), bf::file_size(file2.path()));
|
|
|
|
}
|
2015-11-11 10:19:47 +01:00
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, CanSaveAndLoadModififedCipher) {
|
|
|
|
CreateWithCipher("aes-256-gcm");
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> created = Load().value();
|
|
|
|
EXPECT_EQ("aes-256-gcm", created->config()->Cipher());
|
|
|
|
created->config()->SetCipher("twofish-128-cfb");
|
|
|
|
created->save();
|
|
|
|
unique_ref<CryConfigFile> loaded = std::move(Load().value());
|
|
|
|
EXPECT_EQ("twofish-128-cfb", loaded->config()->Cipher());
|
2015-11-11 10:19:47 +01:00
|
|
|
}
|
2015-11-12 19:59:38 +01:00
|
|
|
|
|
|
|
TEST_F(CryConfigFileTest, FailsIfConfigFileIsEncryptedWithACipherDifferentToTheOneSpecifiedByTheUser) {
|
2018-10-22 04:31:08 +02:00
|
|
|
constexpr unsigned char keySeed = 0;
|
|
|
|
FakeCryKeyProvider keyProvider(keySeed);
|
|
|
|
auto encryptor = CryConfigEncryptorFactory::deriveNewKey(&keyProvider);
|
2015-11-12 19:59:38 +01:00
|
|
|
auto config = Config();
|
|
|
|
config.SetCipher("aes-256-gcm");
|
2023-07-08 23:48:59 +02:00
|
|
|
const Data encrypted = encryptor->encrypt(config.save(), "aes-256-cfb");
|
2015-11-12 19:59:38 +01:00
|
|
|
encrypted.StoreToFile(file.path());
|
2018-10-22 04:31:08 +02:00
|
|
|
auto loaded = Load(keySeed);
|
2015-11-12 19:59:38 +01:00
|
|
|
EXPECT_EQ(none, loaded);
|
|
|
|
}
|