Improve test cases
This commit is contained in:
parent
7e01e84d35
commit
be8a1efd35
@ -108,7 +108,7 @@ namespace cryfs {
|
||||
}
|
||||
|
||||
void Cli::_checkForUpdates(unique_ref<HttpClient> httpClient) {
|
||||
VersionChecker versionChecker(std::move(httpClient));
|
||||
VersionChecker versionChecker(httpClient.get());
|
||||
optional<string> newestVersion = versionChecker.newestVersion();
|
||||
if (newestVersion == none) {
|
||||
cout << "Could not check for updates." << endl;
|
||||
|
@ -17,8 +17,8 @@ using namespace cpputils::logging;
|
||||
|
||||
namespace cryfs {
|
||||
|
||||
VersionChecker::VersionChecker(unique_ref<HttpClient> httpClient)
|
||||
: _versionInfo(_getVersionInfo(std::move(httpClient))) {
|
||||
VersionChecker::VersionChecker(HttpClient* httpClient)
|
||||
: _versionInfo(_getVersionInfo(httpClient)) {
|
||||
}
|
||||
|
||||
optional<string> VersionChecker::newestVersion() const {
|
||||
@ -48,7 +48,7 @@ namespace cryfs {
|
||||
return none;
|
||||
}
|
||||
|
||||
optional<ptree> VersionChecker::_getVersionInfo(unique_ref<HttpClient> httpClient) {
|
||||
optional<ptree> VersionChecker::_getVersionInfo(HttpClient* httpClient) {
|
||||
long timeoutMsec = 2000;
|
||||
optional<string> response = httpClient->get("https://www.cryfs.org/version_info.json", timeoutMsec);
|
||||
if (response == none) {
|
||||
|
@ -12,12 +12,12 @@ namespace cryfs {
|
||||
class VersionChecker final {
|
||||
public:
|
||||
//TODO Write a cpputils::shared_ref and use it
|
||||
VersionChecker(cpputils::unique_ref<cpputils::HttpClient> httpClient);
|
||||
VersionChecker(cpputils::HttpClient* httpClient);
|
||||
|
||||
boost::optional<std::string> newestVersion() const;
|
||||
boost::optional<std::string> securityWarningFor(const std::string &version) const;
|
||||
private:
|
||||
static boost::optional<boost::property_tree::ptree> _getVersionInfo(cpputils::unique_ref<cpputils::HttpClient> httpClient);
|
||||
static boost::optional<boost::property_tree::ptree> _getVersionInfo(cpputils::HttpClient* httpClient);
|
||||
static boost::optional<boost::property_tree::ptree> _parseJson(const std::string &json);
|
||||
|
||||
boost::optional<boost::property_tree::ptree> _versionInfo;
|
||||
|
@ -11,6 +11,7 @@ set(SOURCES
|
||||
CliTest_ShowingHelp.cpp
|
||||
EnvironmentTest.cpp
|
||||
VersionCheckerTest.cpp
|
||||
IntegrityCheckTest.cpp
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
3
test/cryfs-cli/IntegrityCheckTest.cpp
Normal file
3
test/cryfs-cli/IntegrityCheckTest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
//TODO Add cryfs-cli tests for
|
||||
// - filesystem id changed
|
||||
// - filesystem id correct but encryption key changed
|
@ -15,15 +15,15 @@ using namespace cryfs;
|
||||
class VersionCheckerTest: public ::testing::Test {
|
||||
public:
|
||||
unique_ref<VersionChecker> versionChecker() {
|
||||
return make_unique_ref<VersionChecker>(http);
|
||||
return make_unique_ref<VersionChecker>(_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<FakeHttpClient> http = make_shared<FakeHttpClient>();
|
||||
unique_ref<FakeHttpClient> _http = make_unique_ref<FakeHttpClient>();
|
||||
};
|
||||
|
||||
TEST_F(VersionCheckerTest, NewestVersion_NoInternet) {
|
||||
|
@ -26,8 +26,8 @@ public:
|
||||
cpputils::TempFile configfile;
|
||||
std::shared_ptr<MockConsole> console;
|
||||
|
||||
std::shared_ptr<cpputils::HttpClient> _httpClient() {
|
||||
std::shared_ptr<cpputils::FakeHttpClient> httpClient = std::make_shared<cpputils::FakeHttpClient>();
|
||||
cpputils::unique_ref<cpputils::HttpClient> _httpClient() {
|
||||
cpputils::unique_ref<cpputils::FakeHttpClient> httpClient = cpputils::make_unique_ref<cpputils::FakeHttpClient>();
|
||||
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<const char*> args, const std::string &message = "") {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user