2015-10-23 12:16:23 +02:00
|
|
|
#include <google/gtest/gtest.h>
|
|
|
|
#include "../../src/config/CryConfigLoader.h"
|
|
|
|
#include "../testutils/MockConsole.h"
|
|
|
|
#include <messmer/cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <messmer/cpp-utils/random/Random.h>
|
2015-10-27 23:46:54 +01:00
|
|
|
#include <messmer/cpp-utils/crypto/symmetric/ciphers.h>
|
2015-10-23 12:16:23 +02:00
|
|
|
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using cpputils::TempFile;
|
2015-11-04 05:27:00 +01:00
|
|
|
using cpputils::SCrypt;
|
2015-10-24 19:35:37 +02:00
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2015-10-23 12:16:23 +02:00
|
|
|
using std::string;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::_;
|
|
|
|
|
|
|
|
using namespace cryfs;
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
//TODO Test loading with same/different --cipher argument
|
|
|
|
|
2015-10-23 12:16:23 +02:00
|
|
|
class CryConfigLoaderTest: public ::testing::Test, public TestWithMockConsole {
|
|
|
|
public:
|
2015-10-24 19:35:37 +02:00
|
|
|
CryConfigLoaderTest(): file(false) {}
|
2015-10-23 12:16:23 +02:00
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfigLoader loader(const string &password, const optional<string> &cipher = none) {
|
2015-11-04 05:27:00 +01:00
|
|
|
return CryConfigLoader(mockConsole(), cpputils::Random::PseudoRandom(), SCrypt::TestSettings, [password] {return password;}, cipher);
|
2015-10-24 19:35:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
CryConfigFile Create(const string &password = "mypassword", const optional<string> &cipher = none) {
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_FALSE(file.exists());
|
2015-11-04 05:27:00 +01:00
|
|
|
return loader(password, cipher).loadOrCreate(file.path()).value();
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
optional<CryConfigFile> Load(const string &password = "mypassword", const optional<string> &cipher = none) {
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_TRUE(file.exists());
|
2015-11-04 05:27:00 +01:00
|
|
|
return loader(password, cipher).loadOrCreate(file.path());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithRootBlob(const string &rootBlob, const string &password = "mypassword") {
|
2015-11-04 05:27:00 +01:00
|
|
|
auto cfg = loader(password).loadOrCreate(file.path()).value();
|
2015-10-23 12:16:23 +02:00
|
|
|
cfg.config()->SetRootBlob(rootBlob);
|
|
|
|
cfg.save();
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithCipher(const string &cipher, const string &password = "mypassword") {
|
2015-11-04 05:27:00 +01:00
|
|
|
auto cfg = loader(password).loadOrCreate(file.path()).value();
|
2015-10-23 12:16:23 +02:00
|
|
|
cfg.config()->SetCipher(cipher);
|
|
|
|
cfg.save();
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithEncryptionKey(const string &encKey, const string &password = "mypassword") {
|
2015-11-04 05:27:00 +01:00
|
|
|
auto cfg = loader(password).loadOrCreate(file.path()).value();
|
2015-10-23 12:16:23 +02:00
|
|
|
cfg.config()->SetEncryptionKey(encKey);
|
|
|
|
cfg.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
TempFile file;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, CreatesNewIfNotExisting) {
|
|
|
|
EXPECT_FALSE(file.exists());
|
|
|
|
Create();
|
|
|
|
EXPECT_TRUE(file.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, DoesntCrashIfExisting) {
|
|
|
|
Create();
|
|
|
|
Load();
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:36:57 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesntLoadIfWrongPassword) {
|
2015-10-24 19:35:37 +02:00
|
|
|
Create("mypassword");
|
2015-10-26 16:36:57 +01:00
|
|
|
auto loaded = Load("mypassword2");
|
|
|
|
EXPECT_EQ(none, loaded);
|
2015-10-24 19:35:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesntLoadIfDifferentCipher) {
|
|
|
|
Create("mypassword", string("aes-256-gcm"));
|
|
|
|
try {
|
|
|
|
Load("mypassword", string("aes-256-cfb"));
|
|
|
|
EXPECT_TRUE(false); // Should throw exception
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
EXPECT_EQ(string("Filesystem uses aes-256-gcm cipher and not aes-256-cfb as specified."), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, DoesLoadIfSameCipher) {
|
|
|
|
Create("mypassword", string("aes-256-gcm"));
|
|
|
|
Load("mypassword", string("aes-256-gcm"));
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:16:23 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, RootBlob_Load) {
|
|
|
|
CreateWithRootBlob("rootblobid");
|
2015-10-26 16:36:57 +01:00
|
|
|
auto loaded = Load().value();
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_EQ("rootblobid", loaded.config()->RootBlob());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, RootBlob_Create) {
|
|
|
|
auto created = Create();
|
|
|
|
EXPECT_EQ("", created.config()->RootBlob());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, EncryptionKey_Load) {
|
|
|
|
CreateWithEncryptionKey("encryptionkey");
|
2015-10-26 16:36:57 +01:00
|
|
|
auto loaded = Load().value();
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_EQ("encryptionkey", loaded.config()->EncryptionKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, EncryptionKey_Create) {
|
|
|
|
auto created = Create();
|
|
|
|
//aes-256-gcm is the default cipher chosen by mockConsole()
|
2015-10-27 23:46:54 +01:00
|
|
|
cpputils::AES256_GCM::EncryptionKey::FromString(created.config()->EncryptionKey()); // This crashes if key is invalid
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Cipher_Load) {
|
|
|
|
CreateWithCipher("ciphername");
|
2015-10-26 16:36:57 +01:00
|
|
|
auto loaded = Load().value();
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_EQ("ciphername", loaded.config()->Cipher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Cipher_Create) {
|
|
|
|
auto created = Create();
|
|
|
|
//aes-256-gcm is the default cipher chosen by mockConsole()
|
|
|
|
EXPECT_EQ("aes-256-gcm", created.config()->Cipher());
|
|
|
|
}
|