libcryfs/test/cryfs/filesystem/CryFsTest.cpp

74 lines
2.6 KiB
C++
Raw Normal View History

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>
#include <blockstore/implementations/ondisk/OnDiskBlockStore2.h>
2016-02-11 16:39:42 +01:00
#include <cryfs/filesystem/CryDevice.h>
#include <cryfs/filesystem/CryDir.h>
#include <cryfs/filesystem/CryFile.h>
#include <cryfs/filesystem/CryOpenFile.h>
2015-10-22 18:48:04 +02:00
#include "../testutils/MockConsole.h"
2016-02-11 16:39:42 +01:00
#include <cryfs/config/CryConfigLoader.h>
#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;
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;
using cpputils::unique_ref;
using cpputils::Random;
using cpputils::SCrypt;
using cpputils::Data;
2016-09-25 02:50:28 +02:00
using cpputils::NoninteractiveConsole;
using blockstore::ondisk::OnDiskBlockStore2;
using boost::none;
2015-04-27 16:38:09 +02:00
namespace bf = boost::filesystem;
using namespace cryfs;
class CryFsTest: public Test, public TestWithMockConsole, public TestWithFakeHomeDirectory {
2015-04-27 16:38:09 +02:00
public:
CryFsTest(): tempLocalStateDir(), localStateDir(tempLocalStateDir.path()), rootdir(), config(false) {
2015-10-22 18:48:04 +02:00
}
CryConfigFile loadOrCreateConfig() {
auto askPassword = [] {return "mypassword";};
return CryConfigLoader(make_shared<NoninteractiveConsole>(mockConsole()), Random::PseudoRandom(), localStateDir, SCrypt::TestSettings, askPassword, askPassword, none, none, none).loadOrCreate(config.path(), false, false).value().configFile;
}
unique_ref<OnDiskBlockStore2> blockStore() {
return make_unique_ref<OnDiskBlockStore2>(rootdir.path());
2015-10-22 18:48:04 +02:00
}
cpputils::TempDir tempLocalStateDir;
LocalStateDir localStateDir;
2015-04-27 16:38:09 +02:00
TempDir rootdir;
TempFile config;
};
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
2015-06-17 12:28:18 +02:00
{
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false);
2015-06-17 12:28:18 +02:00
}
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false);
auto rootDir = dev.LoadDir(bf::path("/"));
rootDir.value()->children();
2015-04-27 16:38:09 +02:00
}
TEST_F(CryFsTest, LoadingFilesystemDoesntModifyConfigFile) {
{
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false);
}
Data configAfterCreating = Data::LoadFromFile(config.path()).value();
{
CryDevice dev(loadOrCreateConfig(), blockStore(), localStateDir, 0x12345678, false, false);
}
Data configAfterLoading = Data::LoadFromFile(config.path()).value();
EXPECT_EQ(configAfterCreating, configAfterLoading);
}