Skip to content

Commit

Permalink
Old archives should be compatible with newer version (#43)
Browse files Browse the repository at this point in the history
Instead of checking archive version number to exactly match the version of Serialbox, we assume backwards compatibility, i.e. archive version <= serialbox version is allowed.
  • Loading branch information
havogt authored Oct 11, 2017
1 parent 9670bf7 commit d983e5b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/serialbox/core/SerializerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void SerializerImpl::constructMetaDataFromJson() {

int serialboxVersion = jsonNode["serialbox_version"];

if(!Version::match(serialboxVersion))
if(!Version::isCompatible(serialboxVersion))
throw Exception(
"serialbox version of MetaData (%s) does not match the version of the library (%s)",
Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING);
Expand Down
23 changes: 14 additions & 9 deletions src/serialbox/core/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace serialbox {

/// \brief Utility to deal with Serialbox versions
struct Version {
private:
static int getVersion() {
return SERIALBOX_VERSION_MAJOR * 100 + SERIALBOX_VERSION_MINOR * 10 + SERIALBOX_VERSION_PATCH;
}

public:
Version() = delete;

/// \brief Convert to string
Expand All @@ -44,16 +50,15 @@ struct Version {
///
/// \return Return true if the versions match
/// @{
static bool match(int version) noexcept {
int major = version / 100;
int minor = (version - major * 100) / 10;
return Version::match(major, minor, version - 100 * major - 10 * minor);
}
static bool match(int version) noexcept { return version == getVersion(); }
/// @}

static bool match(int major, int minor, int patch) noexcept {
return ((major == SERIALBOX_VERSION_MAJOR) && (minor == SERIALBOX_VERSION_MINOR) &&
(patch == SERIALBOX_VERSION_PATCH));
}
/// \brief Check if the given version is compatible with the current library version (i.e. is
/// older)
///
/// \return Return true if the versions is compatible
/// @{
static bool isCompatible(int version) noexcept { return version <= getVersion(); }
/// @}
};

Expand Down
2 changes: 1 addition & 1 deletion src/serialbox/core/archive/BinaryArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void BinaryArchive::readMetaDataFromJson() {
std::string hashAlgorithm = json_["hash_algorithm"];

// Check consistency
if(!Version::match(serialboxVersion))
if(!Version::isCompatible(serialboxVersion))
throw Exception("serialbox version of binary archive (%s) does not match the version "
"of the library (%s)",
Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING);
Expand Down
5 changes: 3 additions & 2 deletions test/serialbox/core/UnittestVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ TEST(VersionTest, Comparison) {
int minor = SERIALBOX_VERSION_MINOR;
int patch = SERIALBOX_VERSION_PATCH;

EXPECT_TRUE(Version::match(major, minor, patch));
EXPECT_FALSE(Version::match(major - 1, minor, patch));
EXPECT_TRUE(Version::match(major * 100 + minor * 10 + patch));
EXPECT_FALSE(Version::match(0));

EXPECT_TRUE(Version::isCompatible(major * 100 + minor * 10 + (patch - 1)));
EXPECT_FALSE(Version::isCompatible(major * 100 + minor * 10 + (patch + 1)));
}

TEST(VersionTest, ToString) {
Expand Down
4 changes: 2 additions & 2 deletions test/serialbox/core/archive/UnittestBinaryArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ TEST_F(BinaryArchiveUtilityTest, MetaData) {
};

// -----------------------------------------------------------------------------------------------
// Invlaid serialbox version
// Invalid serialbox version
// -----------------------------------------------------------------------------------------------
{
json::json corrupted = j;
corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR - 1) +
corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR + 1) +
10 * SERIALBOX_VERSION_MINOR + SERIALBOX_VERSION_PATCH;
toFile(corrupted);

Expand Down

0 comments on commit d983e5b

Please sign in to comment.