From be8a1efd3561bad2d79d77c9fa8cbde0d5fff3cf Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 30 Sep 2017 22:24:33 +0100 Subject: [PATCH] Improve test cases --- src/cryfs-cli/Cli.cpp | 2 +- src/cryfs-cli/VersionChecker.cpp | 6 +++--- src/cryfs-cli/VersionChecker.h | 4 ++-- test/cryfs-cli/CMakeLists.txt | 1 + test/cryfs-cli/IntegrityCheckTest.cpp | 3 +++ test/cryfs-cli/VersionCheckerTest.cpp | 6 +++--- test/cryfs-cli/testutils/CliTest.h | 6 +++--- test/cryfs/config/CryConfigLoaderTest.cpp | 26 +++++++++++++++++------ 8 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 test/cryfs-cli/IntegrityCheckTest.cpp diff --git a/src/cryfs-cli/Cli.cpp b/src/cryfs-cli/Cli.cpp index bf398830..f0e474bf 100644 --- a/src/cryfs-cli/Cli.cpp +++ b/src/cryfs-cli/Cli.cpp @@ -108,7 +108,7 @@ namespace cryfs { } void Cli::_checkForUpdates(unique_ref httpClient) { - VersionChecker versionChecker(std::move(httpClient)); + VersionChecker versionChecker(httpClient.get()); optional newestVersion = versionChecker.newestVersion(); if (newestVersion == none) { cout << "Could not check for updates." << endl; diff --git a/src/cryfs-cli/VersionChecker.cpp b/src/cryfs-cli/VersionChecker.cpp index 7d17b75c..89a1b94b 100644 --- a/src/cryfs-cli/VersionChecker.cpp +++ b/src/cryfs-cli/VersionChecker.cpp @@ -17,8 +17,8 @@ using namespace cpputils::logging; namespace cryfs { - VersionChecker::VersionChecker(unique_ref httpClient) - : _versionInfo(_getVersionInfo(std::move(httpClient))) { + VersionChecker::VersionChecker(HttpClient* httpClient) + : _versionInfo(_getVersionInfo(httpClient)) { } optional VersionChecker::newestVersion() const { @@ -48,7 +48,7 @@ namespace cryfs { return none; } - optional VersionChecker::_getVersionInfo(unique_ref httpClient) { + optional VersionChecker::_getVersionInfo(HttpClient* httpClient) { long timeoutMsec = 2000; optional response = httpClient->get("https://www.cryfs.org/version_info.json", timeoutMsec); if (response == none) { diff --git a/src/cryfs-cli/VersionChecker.h b/src/cryfs-cli/VersionChecker.h index 220570c1..bc1e54a9 100644 --- a/src/cryfs-cli/VersionChecker.h +++ b/src/cryfs-cli/VersionChecker.h @@ -12,12 +12,12 @@ namespace cryfs { class VersionChecker final { public: //TODO Write a cpputils::shared_ref and use it - VersionChecker(cpputils::unique_ref httpClient); + VersionChecker(cpputils::HttpClient* httpClient); boost::optional newestVersion() const; boost::optional securityWarningFor(const std::string &version) const; private: - static boost::optional _getVersionInfo(cpputils::unique_ref httpClient); + static boost::optional _getVersionInfo(cpputils::HttpClient* httpClient); static boost::optional _parseJson(const std::string &json); boost::optional _versionInfo; diff --git a/test/cryfs-cli/CMakeLists.txt b/test/cryfs-cli/CMakeLists.txt index e9e62ee6..decbbfa4 100644 --- a/test/cryfs-cli/CMakeLists.txt +++ b/test/cryfs-cli/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES CliTest_ShowingHelp.cpp EnvironmentTest.cpp VersionCheckerTest.cpp + IntegrityCheckTest.cpp ) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/test/cryfs-cli/IntegrityCheckTest.cpp b/test/cryfs-cli/IntegrityCheckTest.cpp new file mode 100644 index 00000000..45a16cb0 --- /dev/null +++ b/test/cryfs-cli/IntegrityCheckTest.cpp @@ -0,0 +1,3 @@ +//TODO Add cryfs-cli tests for +// - filesystem id changed +// - filesystem id correct but encryption key changed diff --git a/test/cryfs-cli/VersionCheckerTest.cpp b/test/cryfs-cli/VersionCheckerTest.cpp index 66950038..8f2bdea6 100644 --- a/test/cryfs-cli/VersionCheckerTest.cpp +++ b/test/cryfs-cli/VersionCheckerTest.cpp @@ -15,15 +15,15 @@ using namespace cryfs; class VersionCheckerTest: public ::testing::Test { public: unique_ref versionChecker() { - return make_unique_ref(http); + return make_unique_ref(_http.get()); } void setVersionInfo(const string &versionInfo) { - http->addWebsite("https://www.cryfs.org/version_info.json", versionInfo); + _http->addWebsite("https://www.cryfs.org/version_info.json", versionInfo); } private: - shared_ptr http = make_shared(); + unique_ref _http = make_unique_ref(); }; TEST_F(VersionCheckerTest, NewestVersion_NoInternet) { diff --git a/test/cryfs-cli/testutils/CliTest.h b/test/cryfs-cli/testutils/CliTest.h index 207c014e..4a65b440 100644 --- a/test/cryfs-cli/testutils/CliTest.h +++ b/test/cryfs-cli/testutils/CliTest.h @@ -26,8 +26,8 @@ public: cpputils::TempFile configfile; std::shared_ptr console; - std::shared_ptr _httpClient() { - std::shared_ptr httpClient = std::make_shared(); + cpputils::unique_ref _httpClient() { + cpputils::unique_ref httpClient = cpputils::make_unique_ref(); httpClient->addWebsite("https://www.cryfs.org/version_info.json", "{\"version_info\":{\"current\":\"0.8.5\"}}"); return httpClient; } @@ -44,7 +44,7 @@ public: std::cin.putback('\n'); std::cin.putback('s'); std::cin.putback('s'); std::cin.putback('a'); std::cin.putback('p'); std::cin.putback('\n'); std::cin.putback('s'); std::cin.putback('s'); std::cin.putback('a'); std::cin.putback('p'); // Run Cryfs - cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console, _httpClient()).main(_args.size(), _args.data()); + cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console).main(_args.size(), _args.data(), _httpClient()); } void EXPECT_EXIT_WITH_HELP_MESSAGE(std::vector args, const std::string &message = "") { diff --git a/test/cryfs/config/CryConfigLoaderTest.cpp b/test/cryfs/config/CryConfigLoaderTest.cpp index 71a57ce1..9eb373d1 100644 --- a/test/cryfs/config/CryConfigLoaderTest.cpp +++ b/test/cryfs/config/CryConfigLoaderTest.cpp @@ -98,7 +98,11 @@ public: FakeRandomGenerator generator(Data::FromString(encKey)); auto loader = CryConfigLoader(console, generator, SCrypt::TestSettings, askPassword, askPassword, none, none, none); - auto cfg = loader.loadOrCreate(file.path()).value().configFile; + loader.loadOrCreate(file.path()).value().configFile; + } + + void ChangeEncryptionKey(const string &encKey, const string& password = "mypassword") { + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; cfg.config()->SetEncryptionKey(encKey); cfg.save(); } @@ -116,6 +120,12 @@ public: cfg.save(); } + void ChangeFilesystemID(const CryConfig::FilesystemID &filesystemId, const string& password = "mypassword") { + auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile; + cfg.config()->SetFilesystemId(filesystemId); + cfg.save(); + } + string olderVersion() { string olderVersion; if (std::stol(gitversion::MinorVersion()) > 0) { @@ -201,6 +211,15 @@ TEST_F(CryConfigLoaderTest, EncryptionKey_Load) { EXPECT_EQ("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E3", loaded.config()->EncryptionKey()); } +TEST_F(CryConfigLoaderTest, EncryptionKey_Load_whenKeyChanged_thenFails) { + CreateWithEncryptionKey("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E3"); + ChangeEncryptionKey("3B4682CF22F3CA199E385729B9F3CA19D325229E385729B9443CA19D325229E4"); + EXPECT_THROW( + Load(), + std::runtime_error + ); +} + TEST_F(CryConfigLoaderTest, EncryptionKey_Create) { auto created = Create(); //aes-256-gcm is the default cipher chosen by mockConsole() @@ -313,8 +332,3 @@ TEST_F(CryConfigLoaderTest, MyClientIdIsLoadedCorrectly) { uint32_t myClientId = loader("mypassword", true).loadOrCreate(file.path()).value().myClientId; EXPECT_EQ(myClientId, loader("mypassword", true).loadOrCreate(file.path()).value().myClientId); } - -// TODO Test behavior if -// - filesystem id changed -// - filesystem id correct but encryption key changed -// TODO Add cryfs-cli tests for the same thing