From 39c62ae1856842e0d0afc40f02c26d96b14d3106 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 23 Oct 2015 00:04:03 +0200 Subject: [PATCH] Added test case for CryConfigFile --- src/config/CryConfigFile.cpp | 8 ++- src/config/CryConfigFile.h | 1 + test/config/CryConfigFileTest.cpp | 90 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 test/config/CryConfigFileTest.cpp diff --git a/src/config/CryConfigFile.cpp b/src/config/CryConfigFile.cpp index 9010c1be..2bc9f571 100644 --- a/src/config/CryConfigFile.cpp +++ b/src/config/CryConfigFile.cpp @@ -14,7 +14,13 @@ CryConfigFile CryConfigFile::create(const bf::path &path, CryConfig config) { if (bf::exists(path)) { throw std::runtime_error("Config file exists already."); } - return CryConfigFile(path, std::move(config)); + auto result = CryConfigFile(path, std::move(config)); + result.save(); + return result; +} + +CryConfigFile::~CryConfigFile() { + //We do not call save() here, because we do not want the config file to be re-encrypted on each filesystem run } optional CryConfigFile::load(const bf::path &path) { diff --git a/src/config/CryConfigFile.h b/src/config/CryConfigFile.h index 92713451..4a54ddb5 100644 --- a/src/config/CryConfigFile.h +++ b/src/config/CryConfigFile.h @@ -9,6 +9,7 @@ namespace cryfs { class CryConfigFile final { public: CryConfigFile(CryConfigFile &&rhs); + ~CryConfigFile(); static CryConfigFile create(const boost::filesystem::path &path, CryConfig config); static boost::optional load(const boost::filesystem::path &path); diff --git a/test/config/CryConfigFileTest.cpp b/test/config/CryConfigFileTest.cpp new file mode 100644 index 00000000..7fb3fa1b --- /dev/null +++ b/test/config/CryConfigFileTest.cpp @@ -0,0 +1,90 @@ +#include + +#include "../../src/config/CryConfigFile.h" +#include + +using namespace cryfs; +using cpputils::TempFile; + +class CryConfigFileTest: public ::testing::Test { +public: + CryConfigFileTest(): file(false) {} + + TempFile file; + + CryConfigFile CreateAndLoadEmpty() { + Create(CryConfig()); + return Load(); + } + + void Create(CryConfig cfg) { + CryConfigFile::create(file.path(), std::move(cfg)); + } + + CryConfigFile Load() { + return CryConfigFile::load(file.path()).value(); + } +}; + +TEST_F(CryConfigFileTest, RootBlob_Init) { + CryConfigFile created = CreateAndLoadEmpty(); + EXPECT_EQ("", created.config()->RootBlob()); +} + +TEST_F(CryConfigFileTest, RootBlob_CreateAndLoad) { + CryConfig cfg; + cfg.SetRootBlob("rootblobid"); + Create(std::move(cfg)); + CryConfigFile loaded = Load(); + EXPECT_EQ("rootblobid", loaded.config()->RootBlob()); +} + +TEST_F(CryConfigFileTest, RootBlob_SaveAndLoad) { + CryConfigFile created = CreateAndLoadEmpty(); + created.config()->SetRootBlob("rootblobid"); + created.save(); + CryConfigFile loaded = Load(); + EXPECT_EQ("rootblobid", loaded.config()->RootBlob()); +} + +TEST_F(CryConfigFileTest, EncryptionKey_Init) { + CryConfigFile created = CreateAndLoadEmpty(); + EXPECT_EQ("", created.config()->EncryptionKey()); +} + +TEST_F(CryConfigFileTest, EncryptionKey_CreateAndLoad) { + CryConfig cfg; + cfg.SetEncryptionKey("encryptionkey"); + Create(std::move(cfg)); + CryConfigFile loaded = Load(); + EXPECT_EQ("encryptionkey", loaded.config()->EncryptionKey()); +} + +TEST_F(CryConfigFileTest, EncryptionKey_SaveAndLoad) { + CryConfigFile created = CreateAndLoadEmpty(); + created.config()->SetEncryptionKey("encryptionkey"); + created.save(); + CryConfigFile loaded = Load(); + EXPECT_EQ("encryptionkey", loaded.config()->EncryptionKey()); +} + +TEST_F(CryConfigFileTest, Cipher_Init) { + CryConfigFile created = CreateAndLoadEmpty(); + EXPECT_EQ("", created.config()->Cipher()); +} + +TEST_F(CryConfigFileTest, Cipher_CreateAndLoad) { + CryConfig cfg; + cfg.SetCipher("cipher"); + Create(std::move(cfg)); + CryConfigFile loaded = Load(); + EXPECT_EQ("cipher", loaded.config()->Cipher()); +} + +TEST_F(CryConfigFileTest, Cipher_SaveAndLoad) { + CryConfigFile created = CreateAndLoadEmpty(); + created.config()->SetCipher("cipher"); + created.save(); + CryConfigFile loaded = Load(); + EXPECT_EQ("cipher", loaded.config()->Cipher()); +}