From be8dffb24d34d20092333ecfed27728ee0ec74dd Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 26 Jun 2016 16:53:10 -0700 Subject: [PATCH] MyClientId is generated outside of CryDevice to give the CryConfigCreator access to it. --- src/cryfs-cli/Cli.cpp | 6 ++-- src/cryfs-cli/Cli.h | 5 +-- src/cryfs/CMakeLists.txt | 3 +- src/cryfs/config/CryConfigCreator.cpp | 7 ++-- src/cryfs/config/CryConfigCreator.h | 7 +++- src/cryfs/config/CryConfigLoader.cpp | 17 ++++++---- src/cryfs/config/CryConfigLoader.h | 11 +++++-- src/cryfs/filesystem/CryDevice.cpp | 32 +++++-------------- src/cryfs/filesystem/CryDevice.h | 9 ++---- src/cryfs/localstate/LocalStateDir.cpp | 23 +++++++++++++ src/cryfs/localstate/LocalStateDir.h | 23 +++++++++++++ .../{filesystem => localstate}/MyClientId.cpp | 0 .../{filesystem => localstate}/MyClientId.h | 4 +-- test/cryfs/CMakeLists.txt | 2 +- test/cryfs/config/CryConfigCreatorTest.cpp | 30 ++++++++--------- test/cryfs/config/CryConfigLoaderTest.cpp | 18 +++++++---- test/cryfs/filesystem/CryFsTest.cpp | 10 +++--- test/cryfs/filesystem/FileSystemTest.cpp | 2 +- test/cryfs/filesystem/testutils/CryTestBase.h | 2 +- .../MyClientIdTest.cpp | 2 +- 20 files changed, 132 insertions(+), 81 deletions(-) create mode 100644 src/cryfs/localstate/LocalStateDir.cpp create mode 100644 src/cryfs/localstate/LocalStateDir.h rename src/cryfs/{filesystem => localstate}/MyClientId.cpp (100%) rename src/cryfs/{filesystem => localstate}/MyClientId.h (85%) rename test/cryfs/{filesystem => localstate}/MyClientIdTest.cpp (91%) diff --git a/src/cryfs-cli/Cli.cpp b/src/cryfs-cli/Cli.cpp index 1a027f9c..98e7e158 100644 --- a/src/cryfs-cli/Cli.cpp +++ b/src/cryfs-cli/Cli.cpp @@ -191,7 +191,7 @@ namespace cryfs { return *configFile; } - CryConfigFile Cli::_loadOrCreateConfig(const ProgramOptions &options) { + CryConfigLoader::ConfigLoadResult Cli::_loadOrCreateConfig(const ProgramOptions &options) { try { auto configFile = _determineConfigFile(options); auto config = _loadOrCreateConfigFile(configFile, options.cipher(), options.blocksizeBytes()); @@ -206,7 +206,7 @@ namespace cryfs { } } - optional Cli::_loadOrCreateConfigFile(const bf::path &configFilePath, const optional &cipher, const optional &blocksizeBytes) { + optional Cli::_loadOrCreateConfigFile(const bf::path &configFilePath, const optional &cipher, const optional &blocksizeBytes) { if (_noninteractive) { return CryConfigLoader(_console, _keyGenerator, _scryptSettings, &Cli::_askPasswordNoninteractive, @@ -224,7 +224,7 @@ namespace cryfs { try { auto blockStore = make_unique_ref(options.baseDir()); auto config = _loadOrCreateConfig(options); - CryDevice device(std::move(config), std::move(blockStore)); + CryDevice device(std::move(config.configFile), std::move(blockStore), config.myClientId); _sanityCheckFilesystem(&device); fspp::FilesystemImpl fsimpl(&device); fspp::fuse::Fuse fuse(&fsimpl, "cryfs", "cryfs@"+options.baseDir().native()); diff --git a/src/cryfs-cli/Cli.h b/src/cryfs-cli/Cli.h index 9f53216f..ba8b607d 100644 --- a/src/cryfs-cli/Cli.h +++ b/src/cryfs-cli/Cli.h @@ -11,6 +11,7 @@ #include #include #include "CallAfterTimeout.h" +#include namespace cryfs { class Cli final { @@ -21,8 +22,8 @@ namespace cryfs { private: void _checkForUpdates(); void _runFilesystem(const program_options::ProgramOptions &options); - CryConfigFile _loadOrCreateConfig(const program_options::ProgramOptions &options); - boost::optional _loadOrCreateConfigFile(const boost::filesystem::path &configFilePath, const boost::optional &cipher, const boost::optional &blocksizeBytes); + CryConfigLoader::ConfigLoadResult _loadOrCreateConfig(const program_options::ProgramOptions &options); + boost::optional _loadOrCreateConfigFile(const boost::filesystem::path &configFilePath, const boost::optional &cipher, const boost::optional &blocksizeBytes); boost::filesystem::path _determineConfigFile(const program_options::ProgramOptions &options); static std::string _askPasswordForExistingFilesystem(); static std::string _askPasswordForNewFilesystem(); diff --git a/src/cryfs/CMakeLists.txt b/src/cryfs/CMakeLists.txt index 31a2b133..6c40b491 100644 --- a/src/cryfs/CMakeLists.txt +++ b/src/cryfs/CMakeLists.txt @@ -40,7 +40,8 @@ set(LIB_SOURCES filesystem/cachingfsblobstore/SymlinkBlobRef.cpp filesystem/CryFile.cpp filesystem/CryDevice.cpp - filesystem/MyClientId.cpp + localstate/MyClientId.cpp + localstate/LocalStateDir.cpp ) add_library(${PROJECT_NAME} STATIC ${LIB_SOURCES}) diff --git a/src/cryfs/config/CryConfigCreator.cpp b/src/cryfs/config/CryConfigCreator.cpp index 20b1b3c9..00ba0f91 100644 --- a/src/cryfs/config/CryConfigCreator.cpp +++ b/src/cryfs/config/CryConfigCreator.cpp @@ -2,6 +2,8 @@ #include "CryCipher.h" #include #include +#include +#include using cpputils::Console; using cpputils::unique_ref; @@ -19,7 +21,7 @@ namespace cryfs { :_console(console), _configConsole(console, noninteractive), _encryptionKeyGenerator(encryptionKeyGenerator) { } - CryConfig CryConfigCreator::create(const optional &cipherFromCommandLine, const optional &blocksizeBytesFromCommandLine) { + CryConfigCreator::ConfigCreateResult CryConfigCreator::create(const optional &cipherFromCommandLine, const optional &blocksizeBytesFromCommandLine) { CryConfig config; config.SetCipher(_generateCipher(cipherFromCommandLine)); config.SetVersion(gitversion::VersionString()); @@ -31,7 +33,8 @@ namespace cryfs { #ifndef CRYFS_NO_COMPATIBILITY config.SetHasVersionNumbers(true); #endif - return config; + uint32_t myClientId = MyClientId(LocalStateDir::forFilesystemId(config.FilesystemId())).loadOrGenerate(); + return ConfigCreateResult {std::move(config), myClientId}; } uint32_t CryConfigCreator::_generateBlocksizeBytes(const optional &blocksizeBytesFromCommandLine) { diff --git a/src/cryfs/config/CryConfigCreator.h b/src/cryfs/config/CryConfigCreator.h index f089851c..b02eea11 100644 --- a/src/cryfs/config/CryConfigCreator.h +++ b/src/cryfs/config/CryConfigCreator.h @@ -14,7 +14,12 @@ namespace cryfs { CryConfigCreator(std::shared_ptr console, cpputils::RandomGenerator &encryptionKeyGenerator, bool noninteractive); CryConfigCreator(CryConfigCreator &&rhs) = default; - CryConfig create(const boost::optional &cipherFromCommandLine, const boost::optional &blocksizeBytesFromCommandLine); + struct ConfigCreateResult { + CryConfig config; + uint32_t myClientId; + }; + + ConfigCreateResult create(const boost::optional &cipherFromCommandLine, const boost::optional &blocksizeBytesFromCommandLine); private: std::string _generateCipher(const boost::optional &cipherFromCommandLine); std::string _generateEncKey(const std::string &cipher); diff --git a/src/cryfs/config/CryConfigLoader.cpp b/src/cryfs/config/CryConfigLoader.cpp index e483bf6a..db50a00d 100644 --- a/src/cryfs/config/CryConfigLoader.cpp +++ b/src/cryfs/config/CryConfigLoader.cpp @@ -6,6 +6,8 @@ #include #include #include +#include "../localstate/LocalStateDir.h" +#include "../localstate/MyClientId.h" namespace bf = boost::filesystem; using cpputils::unique_ref; @@ -22,6 +24,8 @@ using std::vector; using std::string; using std::function; using std::shared_ptr; +using std::unique_ptr; +using std::make_unique; using gitversion::VersionCompare; using namespace cpputils::logging; @@ -33,7 +37,7 @@ CryConfigLoader::CryConfigLoader(shared_ptr console, RandomGenerator &k _cipherFromCommandLine(cipherFromCommandLine), _blocksizeBytesFromCommandLine(blocksizeBytesFromCommandLine) { } -optional CryConfigLoader::_loadConfig(const bf::path &filename) { +optional CryConfigLoader::_loadConfig(const bf::path &filename) { string password = _askPasswordForExistingFilesystem(); std::cout << "Loading config file (this can take some time)..." << std::flush; auto config = CryConfigFile::load(filename, password); @@ -53,7 +57,8 @@ optional CryConfigLoader::_loadConfig(const bf::path &filename) { config->save(); } _checkCipher(*config->config()); - return std::move(*config); + uint32_t myClientId = MyClientId(LocalStateDir::forFilesystemId(config->config()->FilesystemId())).loadOrGenerate(); + return ConfigLoadResult {std::move(*config), myClientId}; } void CryConfigLoader::_checkVersion(const CryConfig &config) { @@ -75,7 +80,7 @@ void CryConfigLoader::_checkCipher(const CryConfig &config) const { } } -optional CryConfigLoader::loadOrCreate(const bf::path &filename) { +optional CryConfigLoader::loadOrCreate(const bf::path &filename) { if (bf::exists(filename)) { return _loadConfig(filename); } else { @@ -83,14 +88,14 @@ optional CryConfigLoader::loadOrCreate(const bf::path &filename) } } -CryConfigFile CryConfigLoader::_createConfig(const bf::path &filename) { +CryConfigLoader::ConfigLoadResult CryConfigLoader::_createConfig(const bf::path &filename) { auto config = _creator.create(_cipherFromCommandLine, _blocksizeBytesFromCommandLine); //TODO Ask confirmation if using insecure password (<8 characters) string password = _askPasswordForNewFilesystem(); std::cout << "Creating config file (this can take some time)..." << std::flush; - auto result = CryConfigFile::create(filename, std::move(config), password, _scryptSettings); + auto result = CryConfigFile::create(filename, std::move(config.config), password, _scryptSettings); std::cout << "done" << std::endl; - return result; + return ConfigLoadResult {std::move(result), config.myClientId}; } diff --git a/src/cryfs/config/CryConfigLoader.h b/src/cryfs/config/CryConfigLoader.h index 2f9e2b17..5515e625 100644 --- a/src/cryfs/config/CryConfigLoader.h +++ b/src/cryfs/config/CryConfigLoader.h @@ -16,11 +16,16 @@ public: CryConfigLoader(std::shared_ptr console, cpputils::RandomGenerator &keyGenerator, const cpputils::SCryptSettings &scryptSettings, std::function askPasswordForExistingFilesystem, std::function askPasswordForNewFilesystem, const boost::optional &cipherFromCommandLine, const boost::optional &blocksizeBytesFromCommandLine, bool noninteractive); CryConfigLoader(CryConfigLoader &&rhs) = default; - boost::optional loadOrCreate(const boost::filesystem::path &filename); + struct ConfigLoadResult { + CryConfigFile configFile; + uint32_t myClientId; + }; + + boost::optional loadOrCreate(const boost::filesystem::path &filename); private: - boost::optional _loadConfig(const boost::filesystem::path &filename); - CryConfigFile _createConfig(const boost::filesystem::path &filename); + boost::optional _loadConfig(const boost::filesystem::path &filename); + ConfigLoadResult _createConfig(const boost::filesystem::path &filename); void _checkVersion(const CryConfig &config); void _checkCipher(const CryConfig &config) const; diff --git a/src/cryfs/filesystem/CryDevice.cpp b/src/cryfs/filesystem/CryDevice.cpp index c275a816..74326f1b 100644 --- a/src/cryfs/filesystem/CryDevice.cpp +++ b/src/cryfs/filesystem/CryDevice.cpp @@ -17,7 +17,8 @@ #include "../config/CryCipher.h" #include #include -#include "MyClientId.h" +#include "cryfs/localstate/MyClientId.h" +#include "cryfs/localstate/LocalStateDir.h" using std::string; @@ -52,12 +53,12 @@ namespace bf = boost::filesystem; namespace cryfs { -CryDevice::CryDevice(CryConfigFile configFile, unique_ref blockStore) +CryDevice::CryDevice(CryConfigFile configFile, unique_ref blockStore, uint32_t myClientId) : _fsBlobStore( make_unique_ref( make_unique_ref( make_unique_ref( - CreateBlobStore(std::move(blockStore), &configFile) + CreateBlobStore(std::move(blockStore), &configFile, myClientId) ) ) ) @@ -66,8 +67,8 @@ CryDevice::CryDevice(CryConfigFile configFile, unique_ref blockStore _onFsAction() { } -unique_ref CryDevice::CreateBlobStore(unique_ref blockStore, CryConfigFile *configFile) { - auto versionCountingEncryptedBlockStore = CreateVersionCountingEncryptedBlockStore(std::move(blockStore), configFile); +unique_ref CryDevice::CreateBlobStore(unique_ref blockStore, CryConfigFile *configFile, uint32_t myClientId) { + auto versionCountingEncryptedBlockStore = CreateVersionCountingEncryptedBlockStore(std::move(blockStore), configFile, myClientId); // Create versionCountingEncryptedBlockStore not in the same line as BlobStoreOnBlocks, because it can modify BlocksizeBytes // in the configFile and therefore has to be run before the second parameter to the BlobStoreOnBlocks parameter is evaluated. return make_unique_ref( @@ -77,11 +78,10 @@ unique_ref CryDevice::CreateBlobStore(unique_refconfig()->BlocksizeBytes()); } -unique_ref CryDevice::CreateVersionCountingEncryptedBlockStore(unique_ref blockStore, CryConfigFile *configFile) { +unique_ref CryDevice::CreateVersionCountingEncryptedBlockStore(unique_ref blockStore, CryConfigFile *configFile, uint32_t myClientId) { auto encryptedBlockStore = CreateEncryptedBlockStore(*configFile->config(), std::move(blockStore)); - auto statePath = _statePath(configFile->config()->FilesystemId()); + auto statePath = LocalStateDir::forFilesystemId(configFile->config()->FilesystemId()); auto integrityFilePath = statePath / "integritydata"; - auto myClientId = MyClientId(statePath).loadOrGenerate(); #ifndef CRYFS_NO_COMPATIBILITY if (!configFile->config()->HasVersionNumbers()) { @@ -101,22 +101,6 @@ Key CryDevice::CreateRootBlobAndReturnKey() { return rootBlob->key(); } -bf::path CryDevice::_statePath(const CryConfig::FilesystemID &filesystemId) { - bf::path app_dir = cpputils::system::HomeDirectory::get() / ".cryfs"; - _createDirIfNotExists(app_dir); - bf::path filesystems_dir = app_dir / "filesystems"; - _createDirIfNotExists(filesystems_dir); - bf::path this_filesystem_dir = filesystems_dir / filesystemId.ToString(); - _createDirIfNotExists(this_filesystem_dir); - return this_filesystem_dir; -} - -void CryDevice::_createDirIfNotExists(const bf::path &path) { - if (!bf::exists(path)) { - bf::create_directory(path); - } -} - optional> CryDevice::Load(const bf::path &path) { // TODO Split into smaller functions ASSERT(path.is_absolute(), "Non absolute path given"); diff --git a/src/cryfs/filesystem/CryDevice.h b/src/cryfs/filesystem/CryDevice.h index 57df6168..c4f2089c 100644 --- a/src/cryfs/filesystem/CryDevice.h +++ b/src/cryfs/filesystem/CryDevice.h @@ -17,7 +17,7 @@ namespace cryfs { class CryDevice final: public fspp::Device { public: - CryDevice(CryConfigFile config, cpputils::unique_ref blockStore); + CryDevice(CryConfigFile config, cpputils::unique_ref blockStore, uint32_t myClientId); void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; @@ -47,13 +47,10 @@ private: blockstore::Key _rootKey; std::vector> _onFsAction; - static boost::filesystem::path _statePath(const CryConfig::FilesystemID &filesystemId); - static void _createDirIfNotExists(const boost::filesystem::path &path); - blockstore::Key GetOrCreateRootKey(CryConfigFile *config); blockstore::Key CreateRootBlobAndReturnKey(); - static cpputils::unique_ref CreateBlobStore(cpputils::unique_ref blockStore, CryConfigFile *configFile); - static cpputils::unique_ref CreateVersionCountingEncryptedBlockStore(cpputils::unique_ref blockStore, CryConfigFile *configFile); + static cpputils::unique_ref CreateBlobStore(cpputils::unique_ref blockStore, CryConfigFile *configFile, uint32_t myClientId); + static cpputils::unique_ref CreateVersionCountingEncryptedBlockStore(cpputils::unique_ref blockStore, CryConfigFile *configFile, uint32_t myClientId); static cpputils::unique_ref CreateEncryptedBlockStore(const CryConfig &config, cpputils::unique_ref baseBlockStore); struct BlobWithParent { diff --git a/src/cryfs/localstate/LocalStateDir.cpp b/src/cryfs/localstate/LocalStateDir.cpp new file mode 100644 index 00000000..7bee2b97 --- /dev/null +++ b/src/cryfs/localstate/LocalStateDir.cpp @@ -0,0 +1,23 @@ +#include "LocalStateDir.h" +#include +#include + +namespace bf = boost::filesystem; + +namespace cryfs { + bf::path LocalStateDir::forFilesystemId(const CryConfig::FilesystemID &filesystemId) { + bf::path app_dir = cpputils::system::HomeDirectory::get() / ".cryfs"; + _createDirIfNotExists(app_dir); + bf::path filesystems_dir = app_dir / "filesystems"; + _createDirIfNotExists(filesystems_dir); + bf::path this_filesystem_dir = filesystems_dir / filesystemId.ToString(); + _createDirIfNotExists(this_filesystem_dir); + return this_filesystem_dir; + } + + void LocalStateDir::_createDirIfNotExists(const bf::path &path) { + if (!bf::exists(path)) { + bf::create_directory(path); + } + } +} diff --git a/src/cryfs/localstate/LocalStateDir.h b/src/cryfs/localstate/LocalStateDir.h new file mode 100644 index 00000000..481c1d9a --- /dev/null +++ b/src/cryfs/localstate/LocalStateDir.h @@ -0,0 +1,23 @@ +#pragma once +#ifndef MESSMER_CRYFS_LOCALSTATE_LOCALSTATEDIR_H_ +#define MESSMER_CRYFS_LOCALSTATE_LOCALSTATEDIR_H_ + +#include +#include +#include "../config/CryConfig.h" + +namespace cryfs { + + class LocalStateDir final { + public: + static boost::filesystem::path forFilesystemId(const CryConfig::FilesystemID &filesystemId); + + private: + LocalStateDir(); // static functions only + + static void _createDirIfNotExists(const boost::filesystem::path &path); + }; +} + + +#endif diff --git a/src/cryfs/filesystem/MyClientId.cpp b/src/cryfs/localstate/MyClientId.cpp similarity index 100% rename from src/cryfs/filesystem/MyClientId.cpp rename to src/cryfs/localstate/MyClientId.cpp diff --git a/src/cryfs/filesystem/MyClientId.h b/src/cryfs/localstate/MyClientId.h similarity index 85% rename from src/cryfs/filesystem/MyClientId.h rename to src/cryfs/localstate/MyClientId.h index 47dce099..5667e4af 100644 --- a/src/cryfs/filesystem/MyClientId.h +++ b/src/cryfs/localstate/MyClientId.h @@ -1,6 +1,6 @@ #pragma once -#ifndef MESSMER_CRYFS_FILESYSTEM_MYCLIENTID_H_ -#define MESSMER_CRYFS_FILESYSTEM_MYCLIENTID_H_ +#ifndef MESSMER_CRYFS_LOCALSTATE_MYCLIENTID_H_ +#define MESSMER_CRYFS_LOCALSTATE_MYCLIENTID_H_ #include #include diff --git a/test/cryfs/CMakeLists.txt b/test/cryfs/CMakeLists.txt index dbca7a20..e440aae6 100644 --- a/test/cryfs/CMakeLists.txt +++ b/test/cryfs/CMakeLists.txt @@ -17,7 +17,7 @@ set(SOURCES filesystem/CryFsTest.cpp filesystem/CryNodeTest.cpp filesystem/FileSystemTest.cpp - filesystem/MyClientIdTest.cpp + localstate/MyClientIdTest.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/test/cryfs/config/CryConfigCreatorTest.cpp b/test/cryfs/config/CryConfigCreatorTest.cpp index 5c45e004..214a6251 100644 --- a/test/cryfs/config/CryConfigCreatorTest.cpp +++ b/test/cryfs/config/CryConfigCreatorTest.cpp @@ -63,91 +63,91 @@ public: TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) { AnswerNoToDefaultSettings(); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher()); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) { AnswerNoToDefaultSettings(); EXPECT_DOES_NOT_ASK_FOR_CIPHER(); - CryConfig config = creator.create(string("aes-256-gcm"), none); + CryConfig config = creator.create(string("aes-256-gcm"), none).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfUsingDefaultSettings) { AnswerYesToDefaultSettings(); EXPECT_DOES_NOT_ASK_FOR_CIPHER(); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfNoninteractive) { EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_FOR_CIPHER(); - CryConfig config = noninteractiveCreator.create(none, none); + CryConfig config = noninteractiveCreator.create(none, none).config; } TEST_F(CryConfigCreatorTest, DoesAskForBlocksizeIfNotSpecified) { AnswerNoToDefaultSettings(); EXPECT_ASK_FOR_BLOCKSIZE().WillOnce(Return(1)); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfSpecified) { AnswerNoToDefaultSettings(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); - CryConfig config = creator.create(none, 10*1024u); + CryConfig config = creator.create(none, 10*1024u).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfNoninteractive) { EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); - CryConfig config = noninteractiveCreator.create(none, none); + CryConfig config = noninteractiveCreator.create(none, none).config; } TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfUsingDefaultSettings) { AnswerYesToDefaultSettings(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; } TEST_F(CryConfigCreatorTest, ChoosesEmptyRootBlobId) { AnswerNoToDefaultSettings(); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; EXPECT_EQ("", config.RootBlob()); // This tells CryFS to create a new root blob } TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_448) { AnswerNoToDefaultSettings(); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("mars-448-gcm")); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; cpputils::Mars448_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid } TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_256) { AnswerNoToDefaultSettings(); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-256-gcm")); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; cpputils::AES256_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid } TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) { AnswerNoToDefaultSettings(); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-128-gcm")); - CryConfig config = creator.create(none, none); + CryConfig config = creator.create(none, none).config; cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid } TEST_F(CryConfigCreatorTest, DoesNotAskForAnythingIfEverythingIsSpecified) { EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_FOR_CIPHER(); - CryConfig config = noninteractiveCreator.create(string("aes-256-gcm"), 10*1024u); + CryConfig config = noninteractiveCreator.create(string("aes-256-gcm"), 10*1024u).config; } TEST_F(CryConfigCreatorTest, SetsCorrectCreatedWithVersion) { - CryConfig config = noninteractiveCreator.create(none, none); + CryConfig config = noninteractiveCreator.create(none, none).config; EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion()); } TEST_F(CryConfigCreatorTest, SetsCorrectVersion) { - CryConfig config = noninteractiveCreator.create(none, none); + CryConfig config = noninteractiveCreator.create(none, none).config; EXPECT_EQ(gitversion::VersionString(), config.Version()); } diff --git a/test/cryfs/config/CryConfigLoaderTest.cpp b/test/cryfs/config/CryConfigLoaderTest.cpp index bde8147e..f12f7a23 100644 --- a/test/cryfs/config/CryConfigLoaderTest.cpp +++ b/test/cryfs/config/CryConfigLoaderTest.cpp @@ -44,41 +44,45 @@ public: CryConfigFile Create(const string &password = "mypassword", const optional &cipher = none, bool noninteractive = false) { EXPECT_FALSE(file.exists()); - return loader(password, noninteractive, cipher).loadOrCreate(file.path()).value(); + return loader(password, noninteractive, cipher).loadOrCreate(file.path()).value().configFile; } optional Load(const string &password = "mypassword", const optional &cipher = none, bool noninteractive = false) { EXPECT_TRUE(file.exists()); - return loader(password, noninteractive, cipher).loadOrCreate(file.path()); + auto loadResult = loader(password, noninteractive, cipher).loadOrCreate(file.path()); + if (loadResult == none) { + return none; + } + return std::move(loadResult->configFile); } void CreateWithRootBlob(const string &rootBlob, const string &password = "mypassword") { - auto cfg = loader(password, false).loadOrCreate(file.path()).value(); + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetRootBlob(rootBlob); cfg.save(); } void CreateWithCipher(const string &cipher, const string &password = "mypassword") { - auto cfg = loader(password, false).loadOrCreate(file.path()).value(); + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetCipher(cipher); cfg.save(); } void CreateWithEncryptionKey(const string &encKey, const string &password = "mypassword") { - auto cfg = loader(password, false).loadOrCreate(file.path()).value(); + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetEncryptionKey(encKey); cfg.save(); } void CreateWithVersion(const string &version, const string &password = "mypassword") { - auto cfg = loader(password, false).loadOrCreate(file.path()).value(); + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetVersion(version); cfg.config()->SetCreatedWithVersion(version); cfg.save(); } void CreateWithFilesystemID(const CryConfig::FilesystemID &filesystemId, const string &password = "mypassword") { - auto cfg = loader(password, false).loadOrCreate(file.path()).value(); + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetFilesystemId(filesystemId); cfg.save(); } diff --git a/test/cryfs/filesystem/CryFsTest.cpp b/test/cryfs/filesystem/CryFsTest.cpp index d12585e0..ef71705f 100644 --- a/test/cryfs/filesystem/CryFsTest.cpp +++ b/test/cryfs/filesystem/CryFsTest.cpp @@ -40,7 +40,7 @@ public: CryConfigFile loadOrCreateConfig() { auto askPassword = [] {return "mypassword";}; - return CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true).loadOrCreate(config.path()).value(); + return CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true).loadOrCreate(config.path()).value().configFile; } unique_ref blockStore() { @@ -53,20 +53,20 @@ public: TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) { { - CryDevice dev(loadOrCreateConfig(), blockStore()); + CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678); } - CryDevice dev(loadOrCreateConfig(), blockStore()); + CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678); auto root = dev.Load(bf::path("/")); dynamic_pointer_move(root.get()).get()->children(); } TEST_F(CryFsTest, LoadingFilesystemDoesntModifyConfigFile) { { - CryDevice dev(loadOrCreateConfig(), blockStore()); + CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678); } Data configAfterCreating = Data::LoadFromFile(config.path()).value(); { - CryDevice dev(loadOrCreateConfig(), blockStore()); + CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678); } Data configAfterLoading = Data::LoadFromFile(config.path()).value(); EXPECT_EQ(configAfterCreating, configAfterLoading); diff --git a/test/cryfs/filesystem/FileSystemTest.cpp b/test/cryfs/filesystem/FileSystemTest.cpp index da10ef23..358308d6 100644 --- a/test/cryfs/filesystem/FileSystemTest.cpp +++ b/test/cryfs/filesystem/FileSystemTest.cpp @@ -31,7 +31,7 @@ public: auto askPassword = [] {return "mypassword";}; auto config = CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true) .loadOrCreate(configFile.path()).value(); - return make_unique_ref(std::move(config), std::move(blockStore)); + return make_unique_ref(std::move(config.configFile), std::move(blockStore), config.myClientId); } cpputils::TempFile configFile; diff --git a/test/cryfs/filesystem/testutils/CryTestBase.h b/test/cryfs/filesystem/testutils/CryTestBase.h index 3dd12588..ac7bc33c 100644 --- a/test/cryfs/filesystem/testutils/CryTestBase.h +++ b/test/cryfs/filesystem/testutils/CryTestBase.h @@ -11,7 +11,7 @@ class CryTestBase : public TestWithFakeHomeDirectory { public: CryTestBase(): _configFile(false), _device(nullptr) { auto fakeBlockStore = cpputils::make_unique_ref(); - _device = std::make_unique(configFile(), std::move(fakeBlockStore)); + _device = std::make_unique(configFile(), std::move(fakeBlockStore), 0x12345678); } cryfs::CryConfigFile configFile() { diff --git a/test/cryfs/filesystem/MyClientIdTest.cpp b/test/cryfs/localstate/MyClientIdTest.cpp similarity index 91% rename from test/cryfs/filesystem/MyClientIdTest.cpp rename to test/cryfs/localstate/MyClientIdTest.cpp index f6fe13cf..89a680d6 100644 --- a/test/cryfs/filesystem/MyClientIdTest.cpp +++ b/test/cryfs/localstate/MyClientIdTest.cpp @@ -1,6 +1,6 @@ #include -#include "../../../src/cryfs/filesystem/MyClientId.h" +#include "cryfs/localstate/MyClientId.h" #include using cpputils::TempDir;