Only show update check warning if version on server is actually newer

This commit is contained in:
Sebastian Messmer 2016-02-20 18:23:46 +01:00
parent d00e097954
commit 346baf8e9b
6 changed files with 121 additions and 2 deletions

View File

@ -3,6 +3,7 @@ project (cryfs)
set(SOURCES set(SOURCES
cli/Cli.cpp cli/Cli.cpp
cli/VersionChecker.cpp cli/VersionChecker.cpp
cli/VersionCompare.cpp
cli/CallAfterTimeout.cpp cli/CallAfterTimeout.cpp
cli/program_options/utils.cpp cli/program_options/utils.cpp
cli/program_options/ProgramOptions.cpp cli/program_options/ProgramOptions.cpp

View File

@ -21,6 +21,7 @@
#include <cryfs/filesystem/CryDir.h> #include <cryfs/filesystem/CryDir.h>
#include "VersionChecker.h" #include "VersionChecker.h"
#include "VersionCompare.h"
//TODO Fails with gpg-homedir in filesystem: gpg --homedir gpg-homedir --gen-key //TODO Fails with gpg-homedir in filesystem: gpg --homedir gpg-homedir --gen-key
//TODO Many functions accessing the ProgramOptions object. Factor out into class that stores it as a member. //TODO Many functions accessing the ProgramOptions object. Factor out into class that stores it as a member.
@ -90,8 +91,8 @@ namespace cryfs {
VersionChecker versionChecker(_httpClient); VersionChecker versionChecker(_httpClient);
optional<string> newestVersion = versionChecker.newestVersion(); optional<string> newestVersion = versionChecker.newestVersion();
if (newestVersion == none) { if (newestVersion == none) {
cout << "Could not connect to cryfs.org to check for updates." << endl; cout << "Could not check for updates." << endl;
} else if (*newestVersion != version::VERSION_STRING) { } else if (VersionCompare::isOlderThan(version::VERSION_STRING, *newestVersion)) {
cout << "CryFS " << *newestVersion << " is released. Please update." << endl; cout << "CryFS " << *newestVersion << " is released. Please update." << endl;
} }
optional<string> securityWarning = versionChecker.securityWarningFor(version::VERSION_STRING); optional<string> securityWarning = versionChecker.securityWarningFor(version::VERSION_STRING);

View File

@ -0,0 +1,43 @@
#include "VersionCompare.h"
using std::string;
namespace cryfs {
bool VersionCompare::isOlderThan(const string &v1, const string &v2) {
return _isOlderThanStartingFrom(v1, v2, 0, 0);
}
bool VersionCompare::_isOlderThanStartingFrom(const string &v1, const string &v2, size_t startPos1, size_t startPos2) {
if (startPos1 > v1.size() && startPos2 > v2.size()) {
// All components are equal
return false;
}
string componentStr1 = _extractComponent(v1, startPos1);
string componentStr2 = _extractComponent(v2, startPos2);
uint32_t component1 = _parseComponent(componentStr1);
uint32_t component2 = _parseComponent(componentStr2);
if (component1 == component2) {
return _isOlderThanStartingFrom(v1, v2, startPos1 + componentStr1.size() + 1, startPos2 + componentStr2.size() + 1);
}
return component1 < component2;
}
string VersionCompare::_extractComponent(const string &version, size_t startPos) {
if (startPos >= version.size()) {
return "";
}
size_t found = version.find('.', startPos);
if (found == string::npos) {
return version.substr(startPos);
}
return version.substr(startPos, found-startPos);
}
uint32_t VersionCompare::_parseComponent(const string &component) {
if (component == "" || component.substr(0, 3) == "dev") {
return 0;
}
return std::stoul(component);
}
}

View File

@ -0,0 +1,19 @@
#pragma once
#ifndef MESSMER_CRYFS_CLI_VERSIONCOMPARE_H
#define MESSMER_CRYFS_CLI_VERSIONCOMPARE_H
#include <string>
namespace cryfs {
class VersionCompare {
public:
static bool isOlderThan(const std::string &v1, const std::string &v2);
private:
static bool _isOlderThanStartingFrom(const std::string &v1, const std::string &v2, size_t startPos1, size_t startPos2);
static std::string _extractComponent(const std::string &version, size_t startPos);
static uint32_t _parseComponent(const std::string &component);
};
}
#endif

View File

@ -10,6 +10,7 @@ set(SOURCES
cli/program_options/ParserTest.cpp cli/program_options/ParserTest.cpp
cli/CliTest_ShowingHelp.cpp cli/CliTest_ShowingHelp.cpp
cli/VersionCheckerTest.cpp cli/VersionCheckerTest.cpp
cli/VersionCompareTest.cpp
config/crypto/CryConfigEncryptorFactoryTest.cpp config/crypto/CryConfigEncryptorFactoryTest.cpp
config/crypto/outer/OuterConfigTest.cpp config/crypto/outer/OuterConfigTest.cpp
config/crypto/outer/OuterEncryptorTest.cpp config/crypto/outer/OuterEncryptorTest.cpp

View File

@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include <cryfs/cli/VersionCompare.h>
using namespace cryfs;
using std::string;
class VersionCompareTest : public ::testing::Test {
public:
void EXPECT_IS_OLDER_THAN(const string &v1, const string &v2) {
EXPECT_TRUE(VersionCompare::isOlderThan(v1, v2));
EXPECT_FALSE(VersionCompare::isOlderThan(v2, v1));
}
void EXPECT_IS_SAME_AGE(const string &v1, const string &v2) {
EXPECT_FALSE(VersionCompare::isOlderThan(v1, v2));
EXPECT_FALSE(VersionCompare::isOlderThan(v2, v1));
}
};
TEST_F(VersionCompareTest, IsDifferentVersion) {
EXPECT_IS_OLDER_THAN("0.8", "0.8.1");
EXPECT_IS_OLDER_THAN("0.8", "1.0");
EXPECT_IS_OLDER_THAN("0.8", "1.0.1");
EXPECT_IS_OLDER_THAN("0.8.1", "1.0");
EXPECT_IS_OLDER_THAN("0.7.9", "0.8.0");
EXPECT_IS_OLDER_THAN("1.0.0", "1.0.1");
EXPECT_IS_OLDER_THAN("1.0.0.0", "1.0.0.1");
EXPECT_IS_OLDER_THAN("1", "1.0.0.1");
EXPECT_IS_OLDER_THAN("1.0.0.0", "1.1");
}
TEST_F(VersionCompareTest, IsSameVersion) {
EXPECT_IS_SAME_AGE("0.8", "0.8");
EXPECT_IS_SAME_AGE("1.0", "1.0");
EXPECT_IS_SAME_AGE("1", "1.0");
EXPECT_IS_SAME_AGE("1.0.0", "1.0.0");
EXPECT_IS_SAME_AGE("0.8", "0.8.0");
EXPECT_IS_SAME_AGE("1", "1.0.0.0");
}
TEST_F(VersionCompareTest, ZeroPrefix) {
EXPECT_IS_OLDER_THAN("1.00.0", "1.0.01");
EXPECT_IS_SAME_AGE("1.0.01", "1.0.1");
EXPECT_IS_SAME_AGE("01.0.01", "1.0.1");
}
TEST_F(VersionCompareTest, DevVersions) {
EXPECT_IS_OLDER_THAN("0.8", "0.8.1.dev1");
EXPECT_IS_OLDER_THAN("0.8.1", "0.8.2.dev2");
EXPECT_IS_OLDER_THAN("0.8.1.dev1", "0.8.2");
EXPECT_IS_OLDER_THAN("0.8.dev1", "0.8.1");
EXPECT_IS_OLDER_THAN("0.8.dev1", "0.9");
EXPECT_IS_SAME_AGE("0.9.1.dev5", "0.9.1");
}