cmd/syncthing, lib/db: Abort execution if db version is too high (fixes #4994) (#5022)

This commit is contained in:
Simon Frei
2018-06-26 11:40:34 +02:00
committed by Jakob Borg
parent ef5ca0c218
commit 881e923105
4 changed files with 83 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ package db
import (
"bytes"
"os"
"testing"
"github.com/syncthing/syncthing/lib/fs"
@@ -159,7 +160,7 @@ func TestIgnoredFiles(t *testing.T) {
if err != nil {
t.Fatal(err)
}
db := newDBInstance(ldb, "<memory>")
db, _ := newDBInstance(ldb, "<memory>")
fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
// The contents of the database are like this:
@@ -280,7 +281,7 @@ func TestUpdate0to3(t *testing.T) {
if err != nil {
t.Fatal(err)
}
db := newDBInstance(ldb, "<memory>")
db, _ := newDBInstance(ldb, "<memory>")
folder := []byte(update0to3Folder)
@@ -338,3 +339,27 @@ func TestUpdate0to3(t *testing.T) {
t.Errorf(`Missing needed file "%v"`, n)
}
}
func TestDowngrade(t *testing.T) {
loc := "testdata/downgrade.db"
db, err := Open(loc)
if err != nil {
t.Fatal(err)
}
defer func() {
db.Close()
os.RemoveAll(loc)
}()
miscDB := NewNamespacedKV(db, string(KeyTypeMiscData))
miscDB.PutInt64("dbVersion", dbVersion+1)
l.Infoln(dbVersion)
db.Close()
db, err = Open(loc)
if err, ok := err.(databaseDowngradeError); !ok {
t.Fatal("Expected error due to database downgrade, got", err)
} else if err.minSyncthingVersion != dbMinSyncthingVersion {
t.Fatalf("Error has %v as min Syncthing version, expected %v", err.minSyncthingVersion, dbMinSyncthingVersion)
}
}