2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
2018-12-22 00:58:30 +01:00
|
|
|
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/config/CryConfigLoader.h>
|
|
|
|
#include <cryfs/impl/config/CryPresetPasswordBasedKeyProvider.h>
|
|
|
|
#include "../../impl/testutils/MockConsole.h"
|
|
|
|
#include "../../impl/testutils/TestWithFakeHomeDirectory.h"
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <cpp-utils/random/Random.h>
|
|
|
|
#include <cpp-utils/crypto/symmetric/ciphers.h>
|
2016-06-21 01:14:07 +02:00
|
|
|
#include <cpp-utils/data/DataFixture.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>
|
2018-02-03 18:08:03 +01:00
|
|
|
#include <gitversion/parser.h>
|
2016-05-04 05:34:30 +02:00
|
|
|
#include <gitversion/VersionCompare.h>
|
2018-12-22 00:58:30 +01:00
|
|
|
#include <cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
|
2015-10-23 12:16:23 +02:00
|
|
|
|
|
|
|
using cpputils::TempFile;
|
2015-11-04 05:27:00 +01:00
|
|
|
using cpputils::SCrypt;
|
2016-06-21 01:14:07 +02:00
|
|
|
using cpputils::DataFixture;
|
2017-09-30 10:30:31 +02:00
|
|
|
using cpputils::Data;
|
2016-09-25 02:50:28 +02:00
|
|
|
using cpputils::NoninteractiveConsole;
|
2018-12-22 00:58:30 +01:00
|
|
|
using cpputils::unique_ref;
|
2020-07-19 22:26:13 +02:00
|
|
|
using cpputils::either;
|
2018-10-22 04:31:08 +02:00
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using cpputils::Console;
|
|
|
|
using cpputils::unique_ref;
|
2018-10-28 02:18:32 +01:00
|
|
|
using cryfs::CryPresetPasswordBasedKeyProvider;
|
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;
|
2015-11-24 14:42:20 +01:00
|
|
|
using std::ostream;
|
2018-10-22 04:31:08 +02:00
|
|
|
using std::shared_ptr;
|
2016-09-25 02:50:28 +02:00
|
|
|
using std::make_shared;
|
2015-10-23 12:16:23 +02:00
|
|
|
using ::testing::Return;
|
2016-05-04 05:34:30 +02:00
|
|
|
using ::testing::HasSubstr;
|
2015-10-23 12:16:23 +02:00
|
|
|
|
|
|
|
using namespace cryfs;
|
|
|
|
|
2015-11-24 14:42:20 +01:00
|
|
|
// This is needed for google test
|
|
|
|
namespace boost {
|
|
|
|
inline ostream &operator<<(ostream &stream, const CryConfigFile &) {
|
|
|
|
return stream << "CryConfigFile()";
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 09:01:38 +02:00
|
|
|
namespace cryfs {
|
|
|
|
inline ostream &operator<<(ostream &stream, const CryConfigLoader::ConfigLoadResult &) {
|
|
|
|
return stream << "ConfigLoadResult()";
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 20:42:28 +01:00
|
|
|
#include <boost/optional/optional_io.hpp>
|
2015-11-24 14:42:20 +01:00
|
|
|
|
2017-09-30 10:30:31 +02:00
|
|
|
class FakeRandomGenerator final : public cpputils::RandomGenerator {
|
|
|
|
public:
|
|
|
|
FakeRandomGenerator(Data output)
|
|
|
|
: _output(std::move(output)) {}
|
|
|
|
|
|
|
|
void _get(void *target, size_t bytes) override {
|
|
|
|
ASSERT_EQ(_output.size(), bytes);
|
|
|
|
std::memcpy(target, _output.data(), bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Data _output;
|
|
|
|
};
|
|
|
|
|
2017-09-30 10:03:19 +02:00
|
|
|
class CryConfigLoaderTest: public ::testing::Test, public TestWithMockConsole, TestWithFakeHomeDirectory {
|
2015-10-23 12:16:23 +02:00
|
|
|
public:
|
2018-10-22 04:31:08 +02:00
|
|
|
unique_ref<CryKeyProvider> keyProvider(const string& password) {
|
2018-10-28 02:18:32 +01:00
|
|
|
return make_unique_ref<CryPresetPasswordBasedKeyProvider>(password, make_unique_ref<SCrypt>(SCrypt::TestSettings));
|
2018-10-22 04:31:08 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 07:04:21 +02:00
|
|
|
CryConfigLoaderTest(): file(false), tempLocalStateDir(), localStateDir(tempLocalStateDir.path()) {
|
2016-05-04 05:34:30 +02:00
|
|
|
console = mockConsole();
|
|
|
|
}
|
2015-10-23 12:16:23 +02:00
|
|
|
|
2016-02-21 01:34:21 +01:00
|
|
|
CryConfigLoader loader(const string &password, bool noninteractive, const optional<string> &cipher = none) {
|
2018-10-22 04:31:08 +02:00
|
|
|
auto _console = noninteractive ? shared_ptr<Console>(make_shared<NoninteractiveConsole>(console)) : shared_ptr<Console>(console);
|
|
|
|
return CryConfigLoader(_console, cpputils::Random::PseudoRandom(), keyProvider(password), localStateDir, cipher, none, none);
|
2015-10-24 19:35:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 00:58:30 +01:00
|
|
|
unique_ref<CryConfigFile> Create(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false) {
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_FALSE(file.exists());
|
2020-07-19 22:26:13 +02:00
|
|
|
return loader(password, noninteractive, cipher).loadOrCreate(file.path(), false, false).right().configFile;
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:26:13 +02:00
|
|
|
either<CryConfigFile::LoadError, unique_ref<CryConfigFile>> LoadOrCreate(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false, bool allowFilesystemUpgrade = false) {
|
2015-10-23 12:16:23 +02:00
|
|
|
EXPECT_TRUE(file.exists());
|
2018-02-08 08:08:01 +01:00
|
|
|
auto loadResult = loader(password, noninteractive, cipher).loadOrCreate(file.path(), allowFilesystemUpgrade, false);
|
2020-07-19 22:26:13 +02:00
|
|
|
if (loadResult.is_left()) {
|
|
|
|
return loadResult.left();
|
2016-06-27 01:53:10 +02:00
|
|
|
}
|
2020-07-19 22:26:13 +02:00
|
|
|
return std::move(loadResult.right().configFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
either<CryConfigFile::LoadError, unique_ref<CryConfigFile>> Load(CryConfigFile::Access access = CryConfigFile::Access::ReadWrite) {
|
|
|
|
EXPECT_TRUE(file.exists());
|
|
|
|
auto loadResult = loader("mypassword", false, none).load(file.path(), false, false, access);
|
|
|
|
if (loadResult.is_left()) {
|
|
|
|
return loadResult.left();
|
|
|
|
}
|
|
|
|
return std::move(loadResult.right().configFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectLoadingModifiesFile(CryConfigFile::Access access) {
|
|
|
|
Data contents_before_loading = Data::LoadFromFile(file.path()).value();
|
|
|
|
EXPECT_TRUE(Load(access).is_right());
|
|
|
|
Data contents_after_loading = Data::LoadFromFile(file.path()).value();
|
|
|
|
ASSERT_EQ(contents_before_loading.size(), contents_after_loading.size());
|
|
|
|
EXPECT_NE(0, std::memcmp(contents_before_loading.data(), contents_after_loading.data(), contents_before_loading.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectLoadingDoesntModifyFile(CryConfigFile::Access access) {
|
|
|
|
Data contents_before_loading = Data::LoadFromFile(file.path()).value();
|
|
|
|
EXPECT_TRUE(Load(access).is_right());
|
|
|
|
Data contents_after_loading = Data::LoadFromFile(file.path()).value();
|
|
|
|
ASSERT_EQ(contents_before_loading.size(), contents_after_loading.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(contents_before_loading.data(), contents_after_loading.data(), contents_before_loading.size()));
|
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") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = loader(password, false).loadOrCreate(file.path(), false, false).right().configFile;
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetRootBlob(rootBlob);
|
|
|
|
cfg->save();
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithCipher(const string &cipher, const string &password = "mypassword") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = loader(password, false).loadOrCreate(file.path(), false, false).right().configFile;
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetCipher(cipher);
|
|
|
|
cfg->save();
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:35:37 +02:00
|
|
|
void CreateWithEncryptionKey(const string &encKey, const string &password = "mypassword") {
|
2017-09-30 10:30:31 +02:00
|
|
|
FakeRandomGenerator generator(Data::FromString(encKey));
|
2018-10-22 04:31:08 +02:00
|
|
|
auto loader = CryConfigLoader(console, generator, keyProvider(password), localStateDir, none, none, none);
|
2020-07-19 22:26:13 +02:00
|
|
|
ASSERT_TRUE(loader.loadOrCreate(file.path(), false, false).is_right());
|
2017-09-30 23:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeEncryptionKey(const string &encKey, const string& password = "mypassword") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = CryConfigFile::load(file.path(), keyProvider(password).get(), CryConfigFile::Access::ReadWrite).right();
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetEncryptionKey(encKey);
|
|
|
|
cfg->save();
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 08:08:01 +01:00
|
|
|
void CreateWithVersion(const string &version, const string& formatVersion, const string &password = "mypassword") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = loader(password, false).loadOrCreate(file.path(), false, false).right().configFile;
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetVersion(formatVersion);
|
|
|
|
cfg->config()->SetLastOpenedWithVersion(version);
|
|
|
|
cfg->config()->SetCreatedWithVersion(version);
|
|
|
|
cfg->save();
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
2018-02-08 08:08:01 +01:00
|
|
|
|
2016-06-21 01:14:07 +02:00
|
|
|
void CreateWithFilesystemID(const CryConfig::FilesystemID &filesystemId, const string &password = "mypassword") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = loader(password, false).loadOrCreate(file.path(), false, false).right().configFile;
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetFilesystemId(filesystemId);
|
|
|
|
cfg->save();
|
2016-06-21 01:14:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:24:33 +02:00
|
|
|
void ChangeFilesystemID(const CryConfig::FilesystemID &filesystemId, const string& password = "mypassword") {
|
2020-07-19 22:26:13 +02:00
|
|
|
auto cfg = CryConfigFile::load(file.path(), keyProvider(password).get(), CryConfigFile::Access::ReadWrite).right();
|
2018-12-22 00:58:30 +01:00
|
|
|
cfg->config()->SetFilesystemId(filesystemId);
|
|
|
|
cfg->save();
|
2017-09-30 23:24:33 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 05:34:30 +02:00
|
|
|
string olderVersion() {
|
2018-02-03 18:08:03 +01:00
|
|
|
auto versionInfo = gitversion::Parser::parse(CryConfig::FilesystemFormatVersion);
|
2016-05-04 05:34:30 +02:00
|
|
|
string olderVersion;
|
2018-02-03 18:08:03 +01:00
|
|
|
if (std::stol(versionInfo.minorVersion) > 0) {
|
2019-02-16 09:27:15 +01:00
|
|
|
olderVersion = versionInfo.majorVersion + "." + std::to_string(std::stol(versionInfo.minorVersion) - 1) + ".9";
|
2016-05-04 05:34:30 +02:00
|
|
|
} else {
|
2018-02-03 18:08:03 +01:00
|
|
|
olderVersion = std::to_string(std::stol(versionInfo.majorVersion) - 1) + "." + versionInfo.minorVersion;
|
2016-05-04 05:34:30 +02:00
|
|
|
}
|
2018-02-03 18:08:03 +01:00
|
|
|
assert(gitversion::VersionCompare::isOlderThan(olderVersion, CryConfig::FilesystemFormatVersion));
|
2016-05-04 05:34:30 +02:00
|
|
|
return olderVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
string newerVersion() {
|
2018-09-10 22:11:00 +02:00
|
|
|
string newerVersion = gitversion::MajorVersion()+"."+std::to_string(std::stol(gitversion::MinorVersion())+2);
|
|
|
|
EXPECT_TRUE(gitversion::VersionCompare::isOlderThan(CryConfig::FilesystemFormatVersion, newerVersion))
|
|
|
|
<< "Format Version " << CryConfig::FilesystemFormatVersion << " should be older than Git Version " << newerVersion;
|
2016-05-04 05:34:30 +02:00
|
|
|
return newerVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MockConsole> console;
|
2015-10-23 12:16:23 +02:00
|
|
|
TempFile file;
|
2018-04-22 07:04:21 +02:00
|
|
|
cpputils::TempDir tempLocalStateDir;
|
|
|
|
LocalStateDir localStateDir;
|
2015-10-23 12:16:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, CreatesNewIfNotExisting) {
|
|
|
|
EXPECT_FALSE(file.exists());
|
|
|
|
Create();
|
2020-07-19 22:26:13 +02:00
|
|
|
ASSERT_TRUE(file.exists());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, DoesntCrashIfExisting) {
|
|
|
|
Create();
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate();
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-26 16:36:57 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesntLoadIfWrongPassword) {
|
2015-10-24 19:35:37 +02:00
|
|
|
Create("mypassword");
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = LoadOrCreate("mypassword2");
|
|
|
|
EXPECT_TRUE(loaded.is_left());
|
2015-10-24 19:35:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesntLoadIfDifferentCipher) {
|
2016-02-21 01:34:21 +01:00
|
|
|
Create("mypassword", string("aes-256-gcm"), false);
|
2015-10-30 19:53:15 +01:00
|
|
|
try {
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate("mypassword", string("aes-256-cfb"), false);
|
2016-02-21 01:34:21 +01:00
|
|
|
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, DoesntLoadIfDifferentCipher_Noninteractive) {
|
|
|
|
Create("mypassword", string("aes-256-gcm"), true);
|
|
|
|
try {
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate("mypassword", string("aes-256-cfb"), true);
|
2015-10-30 19:53:15 +01:00
|
|
|
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"));
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate("mypassword", string("aes-256-gcm"));
|
2015-10-30 19:53:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 01:34:21 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesLoadIfSameCipher_Noninteractive) {
|
|
|
|
Create("mypassword", string("aes-128-gcm"), true);
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate("mypassword", string("aes-128-gcm"), true);
|
2016-02-21 01:34:21 +01:00
|
|
|
}
|
|
|
|
|
2015-10-23 12:16:23 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, RootBlob_Load) {
|
|
|
|
CreateWithRootBlob("rootblobid");
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = LoadOrCreate().right();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ("rootblobid", loaded->config()->RootBlob());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, RootBlob_Create) {
|
|
|
|
auto created = Create();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ("", created->config()->RootBlob());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, EncryptionKey_Load) {
|
2017-09-30 10:30:31 +02:00
|
|
|
CreateWithEncryptionKey("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E3");
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = LoadOrCreate().right();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E3", loaded->config()->EncryptionKey());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:24:33 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, EncryptionKey_Load_whenKeyChanged_thenFails) {
|
|
|
|
CreateWithEncryptionKey("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E3");
|
|
|
|
ChangeEncryptionKey("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E4");
|
|
|
|
EXPECT_THROW(
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate(),
|
2017-09-30 23:24:33 +02:00
|
|
|
std::runtime_error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:16:23 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, EncryptionKey_Create) {
|
|
|
|
auto created = Create();
|
2018-12-22 00:58:30 +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) {
|
2015-11-11 10:19:47 +01:00
|
|
|
CreateWithCipher("twofish-128-cfb");
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = LoadOrCreate().right();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ("twofish-128-cfb", loaded->config()->Cipher());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Cipher_Create) {
|
|
|
|
auto created = Create();
|
2021-01-15 05:02:06 +01:00
|
|
|
//xchacha20-poly1305 is the default cipher chosen by mockConsole()
|
|
|
|
EXPECT_EQ("xchacha20-poly1305", created->config()->Cipher());
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
2016-03-26 17:09:07 +01:00
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Version_Load) {
|
2019-02-16 09:27:15 +01:00
|
|
|
CreateWithVersion("0.9.4", "0.9.4");
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = std::move(LoadOrCreate().right());
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ(CryConfig::FilesystemFormatVersion, loaded->config()->Version());
|
|
|
|
EXPECT_EQ(gitversion::VersionString(), loaded->config()->LastOpenedWithVersion());
|
2019-02-16 09:38:24 +01:00
|
|
|
EXPECT_EQ("0.9.4", loaded->config()->CreatedWithVersion());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Version_Load_IsStoredAndNotOnlyOverwrittenInMemoryOnLoad) {
|
2019-02-16 09:27:15 +01:00
|
|
|
CreateWithVersion("0.9.4", "0.9.4", "mypassword");
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate().right();
|
|
|
|
auto configFile = CryConfigFile::load(file.path(), keyProvider("mypassword").get(), CryConfigFile::Access::ReadWrite).right();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ(CryConfig::FilesystemFormatVersion, configFile->config()->Version());
|
|
|
|
EXPECT_EQ(gitversion::VersionString(), configFile->config()->LastOpenedWithVersion());
|
2019-02-16 09:38:24 +01:00
|
|
|
EXPECT_EQ("0.9.4", configFile->config()->CreatedWithVersion());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, Version_Create) {
|
|
|
|
auto created = Create();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ(CryConfig::FilesystemFormatVersion, created->config()->Version());
|
|
|
|
EXPECT_EQ(gitversion::VersionString(), created->config()->LastOpenedWithVersion());
|
|
|
|
EXPECT_EQ(gitversion::VersionString(), created->config()->CreatedWithVersion());
|
2016-03-26 17:09:07 +01:00
|
|
|
}
|
2016-05-04 05:34:30 +02:00
|
|
|
|
2016-06-21 01:14:07 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, FilesystemID_Load) {
|
|
|
|
auto fixture = DataFixture::generateFixedSize<CryConfig::FilesystemID::BINARY_LENGTH>();
|
|
|
|
CreateWithFilesystemID(fixture);
|
2020-07-19 22:26:13 +02:00
|
|
|
auto loaded = LoadOrCreate().right();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_EQ(fixture, loaded->config()->FilesystemId());
|
2016-06-21 01:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, FilesystemID_Create) {
|
|
|
|
auto created = Create();
|
2018-12-22 00:58:30 +01:00
|
|
|
EXPECT_NE(CryConfig::FilesystemID::Null(), created->config()->FilesystemId());
|
2016-06-21 01:14:07 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:52:06 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, AsksWhenLoadingNewerFilesystem_AnswerYes) {
|
2016-09-25 02:50:28 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("should not be opened with older versions"), false)).Times(1).WillOnce(Return(true));
|
2016-06-07 21:52:06 +02:00
|
|
|
|
|
|
|
string version = newerVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2020-07-19 22:26:13 +02:00
|
|
|
EXPECT_TRUE(LoadOrCreate().is_right());
|
2016-06-07 21:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, AsksWhenLoadingNewerFilesystem_AnswerNo) {
|
2016-09-25 02:50:28 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("should not be opened with older versions"), false)).Times(1).WillOnce(Return(false));
|
2016-06-07 21:52:06 +02:00
|
|
|
|
2016-05-04 05:34:30 +02:00
|
|
|
string version = newerVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2016-05-04 05:34:30 +02:00
|
|
|
try {
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate();
|
2016-05-04 05:34:30 +02:00
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const std::runtime_error &e) {
|
2016-09-25 11:41:21 +02:00
|
|
|
EXPECT_THAT(e.what(), HasSubstr("Please update your CryFS version."));
|
2016-05-04 05:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, AsksWhenMigratingOlderFilesystem) {
|
2019-01-21 09:12:07 +01:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("Do you want to attempt a migration now?"), false)).Times(1).WillOnce(Return(true));
|
2016-05-04 05:34:30 +02:00
|
|
|
|
|
|
|
string version = olderVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2020-07-19 22:26:13 +02:00
|
|
|
EXPECT_TRUE(LoadOrCreate().is_right());
|
2016-05-04 05:34:30 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 22:35:02 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesNotAskForMigrationWhenCorrectVersion) {
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("Do you want to attempt a migration now?"), testing::_)).Times(0);
|
2017-09-30 22:35:02 +02:00
|
|
|
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(gitversion::VersionString(), CryConfig::FilesystemFormatVersion);
|
2020-07-19 22:26:13 +02:00
|
|
|
EXPECT_TRUE(LoadOrCreate().is_right());
|
2017-09-30 22:35:02 +02:00
|
|
|
}
|
2016-05-04 05:34:30 +02:00
|
|
|
|
2017-09-30 22:35:02 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, DontMigrateWhenAnsweredNo) {
|
2019-01-21 09:12:07 +01:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("Do you want to attempt a migration now?"), false)).Times(1).WillOnce(Return(false));
|
2017-09-30 22:35:02 +02:00
|
|
|
|
|
|
|
string version = olderVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2016-05-04 05:34:30 +02:00
|
|
|
try {
|
2020-07-19 22:26:13 +02:00
|
|
|
LoadOrCreate();
|
2016-05-04 05:34:30 +02:00
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const std::runtime_error &e) {
|
2016-09-25 11:41:21 +02:00
|
|
|
EXPECT_THAT(e.what(), HasSubstr("It has to be migrated."));
|
2016-05-04 05:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-27 02:02:51 +02:00
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, MyClientIdIsIndeterministic) {
|
|
|
|
TempFile file1(false);
|
|
|
|
TempFile file2(false);
|
2020-07-19 22:26:13 +02:00
|
|
|
uint32_t myClientId = loader("mypassword", true).loadOrCreate(file1.path(), false, false).right().myClientId;
|
|
|
|
EXPECT_NE(myClientId, loader("mypassword", true).loadOrCreate(file2.path(), false, false).right().myClientId);
|
2016-06-27 02:02:51 +02:00
|
|
|
}
|
2017-09-30 19:53:03 +02:00
|
|
|
|
2016-06-27 02:02:51 +02:00
|
|
|
TEST_F(CryConfigLoaderTest, MyClientIdIsLoadedCorrectly) {
|
|
|
|
TempFile file(false);
|
2020-07-19 22:26:13 +02:00
|
|
|
uint32_t myClientId = loader("mypassword", true).loadOrCreate(file.path(), false, false).right().myClientId;
|
|
|
|
EXPECT_EQ(myClientId, loader("mypassword", true).loadOrCreate(file.path(), false, false).right().myClientId);
|
2018-02-01 20:21:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 18:45:31 +01:00
|
|
|
TEST_F(CryConfigLoaderTest, DoesNotAskForMigrationWhenUpgradesAllowedByProgramArguments_NoninteractiveMode) {
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("migrate"), testing::_)).Times(0);
|
2018-02-01 18:45:31 +01:00
|
|
|
|
|
|
|
string version = olderVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2020-07-19 22:26:13 +02:00
|
|
|
EXPECT_TRUE(LoadOrCreate("mypassword", none, true, true).is_right());
|
2018-02-01 18:45:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, DoesNotAskForMigrationWhenUpgradesAllowedByProgramArguments_InteractiveMode) {
|
2021-04-21 08:09:00 +02:00
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("migrate"), testing::_)).Times(0);
|
2018-02-01 18:45:31 +01:00
|
|
|
|
|
|
|
string version = olderVersion();
|
2018-02-08 08:08:01 +01:00
|
|
|
CreateWithVersion(version, version);
|
2020-07-19 22:26:13 +02:00
|
|
|
EXPECT_TRUE(LoadOrCreate("mypassword", none, false, true).is_right());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, UpdatesConfigFileWithNewVersionWhenMigrated) {
|
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("Do you want to attempt a migration now?"), false)).Times(1).WillOnce(Return(true));
|
|
|
|
|
|
|
|
string version = olderVersion(); // this triggers a migration which should cause it to modify the config file on load
|
|
|
|
CreateWithVersion(version, version);
|
|
|
|
|
|
|
|
expectLoadingModifiesFile(CryConfigFile::Access::ReadWrite);
|
|
|
|
|
|
|
|
// If we load it again, it shouldn't modify again because it's already updated
|
|
|
|
expectLoadingDoesntModifyFile(CryConfigFile::Access::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CryConfigLoaderTest, DoesntUpdatesConfigFileWithNewVersionWhenLoadingReadOnly) {
|
|
|
|
EXPECT_CALL(*console, askYesNo(HasSubstr("Do you want to attempt a migration now?"), false)).Times(1).WillOnce(Return(true));
|
|
|
|
|
|
|
|
string version = olderVersion(); // this triggers a migration which usually would cause it to modify the config file on load
|
|
|
|
CreateWithVersion(version, version);
|
|
|
|
|
|
|
|
expectLoadingDoesntModifyFile(CryConfigFile::Access::ReadOnly);
|
2016-06-27 02:02:51 +02:00
|
|
|
}
|