2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cpp-utils/tempfile/TempDir.h>
|
|
|
|
#include <cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <cpp-utils/pointer/cast.h>
|
2017-07-18 23:49:51 +02:00
|
|
|
#include <blockstore/implementations/ondisk/OnDiskBlockStore2.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/filesystem/CryDevice.h>
|
|
|
|
#include <cryfs/impl/filesystem/CryDir.h>
|
|
|
|
#include <cryfs/impl/filesystem/CryFile.h>
|
|
|
|
#include <cryfs/impl/filesystem/CryOpenFile.h>
|
2015-10-22 18:48:04 +02:00
|
|
|
#include "../testutils/MockConsole.h"
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/config/CryConfigLoader.h>
|
|
|
|
#include <cryfs/impl/config/CryPresetPasswordBasedKeyProvider.h>
|
2016-06-22 02:36:29 +02:00
|
|
|
#include <cpp-utils/system/homedir.h>
|
|
|
|
#include "../testutils/TestWithFakeHomeDirectory.h"
|
2016-09-25 02:50:28 +02:00
|
|
|
#include <cpp-utils/io/NoninteractiveConsole.h>
|
2015-04-27 16:38:09 +02:00
|
|
|
|
|
|
|
//TODO (whole project) Make constructors explicit when implicit construction not needed
|
|
|
|
|
|
|
|
using ::testing::Test;
|
2016-09-25 02:50:28 +02:00
|
|
|
using std::make_shared;
|
2018-12-22 00:58:30 +01:00
|
|
|
using std::shared_ptr;
|
2015-04-27 16:38:09 +02:00
|
|
|
using cpputils::TempDir;
|
|
|
|
using cpputils::TempFile;
|
2015-07-21 18:22:03 +02:00
|
|
|
using cpputils::make_unique_ref;
|
2015-07-26 13:09:55 +02:00
|
|
|
using cpputils::unique_ref;
|
2015-10-24 19:35:37 +02:00
|
|
|
using cpputils::Random;
|
2015-11-04 05:27:00 +01:00
|
|
|
using cpputils::SCrypt;
|
2015-11-11 20:22:46 +01:00
|
|
|
using cpputils::Data;
|
2016-09-25 02:50:28 +02:00
|
|
|
using cpputils::NoninteractiveConsole;
|
2017-07-18 23:49:51 +02:00
|
|
|
using blockstore::ondisk::OnDiskBlockStore2;
|
2015-10-30 19:53:15 +01:00
|
|
|
using boost::none;
|
2018-10-28 02:18:32 +01:00
|
|
|
using cryfs::CryPresetPasswordBasedKeyProvider;
|
2015-04-27 16:38:09 +02:00
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
using namespace cryfs;
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
namespace {
|
|
|
|
|
2016-06-22 02:36:29 +02:00
|
|
|
class CryFsTest: public Test, public TestWithMockConsole, public TestWithFakeHomeDirectory {
|
2015-04-27 16:38:09 +02:00
|
|
|
public:
|
2018-04-22 07:04:21 +02:00
|
|
|
CryFsTest(): tempLocalStateDir(), localStateDir(tempLocalStateDir.path()), rootdir(), config(false) {
|
2015-10-22 18:48:04 +02:00
|
|
|
}
|
2015-10-23 12:16:23 +02:00
|
|
|
|
2018-12-22 00:58:30 +01:00
|
|
|
shared_ptr<CryConfigFile> loadOrCreateConfig() {
|
2018-10-28 02:18:32 +01:00
|
|
|
auto keyProvider = make_unique_ref<CryPresetPasswordBasedKeyProvider>("mypassword", make_unique_ref<SCrypt>(SCrypt::TestSettings));
|
2020-07-19 22:26:13 +02:00
|
|
|
return CryConfigLoader(make_shared<NoninteractiveConsole>(mockConsole()), Random::PseudoRandom(), std::move(keyProvider), localStateDir, none, none, none).loadOrCreate(config.path(), false, false).right().configFile;
|
2015-10-23 12:16:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 23:49:51 +02:00
|
|
|
unique_ref<OnDiskBlockStore2> blockStore() {
|
|
|
|
return make_unique_ref<OnDiskBlockStore2>(rootdir.path());
|
2015-10-22 18:48:04 +02:00
|
|
|
}
|
2015-10-23 12:16:23 +02:00
|
|
|
|
2018-04-22 07:04:21 +02:00
|
|
|
cpputils::TempDir tempLocalStateDir;
|
|
|
|
LocalStateDir localStateDir;
|
2015-04-27 16:38:09 +02:00
|
|
|
TempDir rootdir;
|
|
|
|
TempFile config;
|
|
|
|
};
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
auto failOnIntegrityViolation() {
|
|
|
|
return [] {
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:16:23 +02:00
|
|
|
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
|
2015-06-17 12:28:18 +02:00
|
|
|
{
|
2018-12-11 06:20:18 +01:00
|
|
|
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false, failOnIntegrityViolation());
|
2019-10-13 13:29:59 +02:00
|
|
|
dev.setContext(fspp::Context {fspp::relatime()});
|
2015-06-17 12:28:18 +02:00
|
|
|
}
|
2018-12-11 06:20:18 +01:00
|
|
|
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false, failOnIntegrityViolation());
|
2019-10-13 13:29:59 +02:00
|
|
|
dev.setContext(fspp::Context {fspp::relatime()});
|
2017-01-21 20:16:35 +01:00
|
|
|
auto rootDir = dev.LoadDir(bf::path("/"));
|
|
|
|
rootDir.value()->children();
|
2015-04-27 16:38:09 +02:00
|
|
|
}
|
2015-11-11 20:22:46 +01:00
|
|
|
|
|
|
|
TEST_F(CryFsTest, LoadingFilesystemDoesntModifyConfigFile) {
|
|
|
|
{
|
2018-12-11 06:20:18 +01:00
|
|
|
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false, failOnIntegrityViolation());
|
2019-10-13 13:29:59 +02:00
|
|
|
dev.setContext(fspp::Context {fspp::relatime()});
|
2015-11-11 20:22:46 +01:00
|
|
|
}
|
2023-07-08 23:48:59 +02:00
|
|
|
const Data configAfterCreating = Data::LoadFromFile(config.path()).value();
|
2015-11-11 20:22:46 +01:00
|
|
|
{
|
2018-12-11 06:20:18 +01:00
|
|
|
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false, failOnIntegrityViolation());
|
2019-10-13 13:29:59 +02:00
|
|
|
dev.setContext(fspp::Context {fspp::relatime()});
|
2015-11-11 20:22:46 +01:00
|
|
|
}
|
2023-07-08 23:48:59 +02:00
|
|
|
const Data configAfterLoading = Data::LoadFromFile(config.path()).value();
|
2015-11-11 20:22:46 +01:00
|
|
|
EXPECT_EQ(configAfterCreating, configAfterLoading);
|
|
|
|
}
|
2018-12-11 06:20:18 +01:00
|
|
|
|
|
|
|
}
|