From c4d975260e8c479a171692a99b56a77dedaecdda Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 28 Jan 2016 17:55:26 +0000 Subject: [PATCH] When running tests, don't actually send http requests from version checker, but mock the http service. --- src/cli/Cli.cpp | 7 ++++--- src/cli/Cli.h | 4 +++- src/cli/VersionChecker.cpp | 4 ---- src/cli/VersionChecker.h | 1 - src/main.cpp | 4 +++- test/cli/testutils/CliTest.h | 10 +++++++++- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/cli/Cli.cpp b/src/cli/Cli.cpp index 16570619..099e5eed 100644 --- a/src/cli/Cli.cpp +++ b/src/cli/Cli.cpp @@ -48,6 +48,7 @@ using cpputils::RandomGenerator; using cpputils::unique_ref; using cpputils::SCryptSettings; using cpputils::Console; +using cpputils::HttpClient; using std::cout; using std::string; using std::endl; @@ -72,8 +73,8 @@ using boost::chrono::milliseconds; namespace cryfs { - Cli::Cli(RandomGenerator &keyGenerator, const SCryptSettings &scryptSettings, shared_ptr console): - _keyGenerator(keyGenerator), _scryptSettings(scryptSettings), _console(console) {} + Cli::Cli(RandomGenerator &keyGenerator, const SCryptSettings &scryptSettings, shared_ptr console, shared_ptr httpClient): + _keyGenerator(keyGenerator), _scryptSettings(scryptSettings), _console(console), _httpClient(httpClient) {} void Cli::_showVersion() { cout << "CryFS Version " << version::VERSION_STRING << endl; @@ -89,7 +90,7 @@ namespace cryfs { #ifndef NDEBUG cout << "WARNING! This is a debug build. Performance might be slow." << endl; #endif - VersionChecker versionChecker; + VersionChecker versionChecker(_httpClient); optional newestVersion = versionChecker.newestVersion(); if (newestVersion == none) { cout << "Could not connect to cryfs.org to check for updates." << endl; diff --git a/src/cli/Cli.h b/src/cli/Cli.h index b0f2a693..86ee8811 100644 --- a/src/cli/Cli.h +++ b/src/cli/Cli.h @@ -8,12 +8,13 @@ #include #include #include +#include #include "CallAfterTimeout.h" namespace cryfs { class Cli final { public: - Cli(cpputils::RandomGenerator &keyGenerator, const cpputils::SCryptSettings &scryptSettings, std::shared_ptr console); + Cli(cpputils::RandomGenerator &keyGenerator, const cpputils::SCryptSettings &scryptSettings, std::shared_ptr console, std::shared_ptr httpClient); int main(int argc, char *argv[]); private: @@ -38,6 +39,7 @@ namespace cryfs { cpputils::RandomGenerator &_keyGenerator; cpputils::SCryptSettings _scryptSettings; std::shared_ptr _console; + std::shared_ptr _httpClient; DISALLOW_COPY_AND_ASSIGN(Cli); }; diff --git a/src/cli/VersionChecker.cpp b/src/cli/VersionChecker.cpp index 53507974..a58cc84a 100644 --- a/src/cli/VersionChecker.cpp +++ b/src/cli/VersionChecker.cpp @@ -18,10 +18,6 @@ using namespace cpputils::logging; namespace cryfs { - VersionChecker::VersionChecker() - :VersionChecker(make_shared()) { - } - VersionChecker::VersionChecker(shared_ptr httpClient) : _versionInfo(_getVersionInfo(std::move(httpClient))) { } diff --git a/src/cli/VersionChecker.h b/src/cli/VersionChecker.h index 6e7cda5d..f94e6778 100644 --- a/src/cli/VersionChecker.h +++ b/src/cli/VersionChecker.h @@ -10,7 +10,6 @@ namespace cryfs { class VersionChecker final { public: - VersionChecker(); //TODO Write a cpputils::shared_ref and use it VersionChecker(std::shared_ptr httpClient); diff --git a/src/main.cpp b/src/main.cpp index 14bc0562..d65fd467 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,16 @@ #include "cli/Cli.h" #include #include +#include using namespace cryfs; using cpputils::Random; using cpputils::SCrypt; +using cpputils::CurlHttpClient; using std::make_shared; using cpputils::IOStreamConsole; int main(int argc, char *argv[]) { auto &keyGenerator = Random::OSRandom(); - return Cli(keyGenerator, SCrypt::DefaultSettings, make_shared()).main(argc, argv); + return Cli(keyGenerator, SCrypt::DefaultSettings, make_shared(), make_shared()).main(argc, argv); } diff --git a/test/cli/testutils/CliTest.h b/test/cli/testutils/CliTest.h index 69ac7ce2..b3ad6259 100644 --- a/test/cli/testutils/CliTest.h +++ b/test/cli/testutils/CliTest.h @@ -7,8 +7,10 @@ #include #include #include "../../../src/cli/Cli.h" +#include "../../../src/cli/VersionChecker.h" #include #include +#include #include "../../testutils/MockConsole.h" class CliTest : public ::testing::Test { @@ -23,6 +25,12 @@ public: cpputils::TempFile configfile; std::shared_ptr console; + std::shared_ptr _httpClient() { + std::shared_ptr httpClient = std::make_shared(); + httpClient->addWebsite("https://www.cryfs.org/version_info.json", "{\"version_info\":{\"current\":\"0.8.5\"}}"); + return httpClient; + } + void run(std::vector args) { std::vector _args; _args.reserve(args.size()+1); @@ -31,7 +39,7 @@ public: _args.push_back(const_cast(arg)); } auto &keyGenerator = cpputils::Random::PseudoRandom(); - cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console).main(_args.size(), _args.data()); + cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console, _httpClient()).main(_args.size(), _args.data()); } void EXPECT_EXIT_WITH_HELP_MESSAGE(std::vector args, const std::string &message = "") {