diff --git a/server/storage/rethink_realdb_test.go b/server/storage/rethink_realdb_test.go index 56983897b..964025fb4 100644 --- a/server/storage/rethink_realdb_test.go +++ b/server/storage/rethink_realdb_test.go @@ -112,14 +112,23 @@ func TestRethinkUpdateCurrentEmpty(t *testing.T) { } // UpdateCurrent will add a new TUF file if the version is higher than previous, but fail -// if the version is equal to or less than the current, whether or not that previous -// version exists -func TestRethinkUpdateCurrentVersionCheck(t *testing.T) { +// if the version already exists in the DB +func TestRethinkUpdateCurrentVersionCheckOldVersionExists(t *testing.T) { + dbStore, cleanup := rethinkDBSetup(t) + defer cleanup() + + testUpdateCurrentVersionCheck(t, dbStore, true) +} + +// UpdateCurrent will add a new TUF file if the version is higher than previous, but fail +// if the version is lower than an existing version in the DB, but does not actually +// correspond to one that already does exists in the DB +func TestRethinkUpdateCurrentVersionCheckOldVersionNotExist(t *testing.T) { t.Skip("Currently rethink only errors if the previous version exists - it doesn't check for strictly increasing") dbStore, cleanup := rethinkDBSetup(t) defer cleanup() - testUpdateCurrentVersionCheck(t, dbStore) + testUpdateCurrentVersionCheck(t, dbStore, false) } // UpdateMany succeeds if the updates do not conflict with each other or with what's diff --git a/server/storage/sqldb_test.go b/server/storage/sqldb_test.go index 82e030e75..fed9c712f 100644 --- a/server/storage/sqldb_test.go +++ b/server/storage/sqldb_test.go @@ -76,14 +76,27 @@ func TestSQLUpdateCurrentEmpty(t *testing.T) { dbStore.DB.Close() } -// TestSQLUpdateCurrentNewVersion asserts that UpdateCurrent will add a +// TestSQLUpdateCurrentVersionCheckOldVersionExists asserts that UpdateCurrent will add a // new (higher) version of an existing TUF file, and that an error is raised if -// trying to update to an older version of a TUF file. -func TestSQLUpdateCurrentNewVersion(t *testing.T) { +// trying to update to an older version of a TUF file that already exists. +func TestSQLUpdateCurrentVersionCheckOldVersionExists(t *testing.T) { dbStore, cleanup := sqldbSetup(t) defer cleanup() - expected := testUpdateCurrentVersionCheck(t, dbStore) + expected := testUpdateCurrentVersionCheck(t, dbStore, true) + assertExpectedGormTUFMeta(t, expected, dbStore.DB) + + dbStore.DB.Close() +} + +// TestSQLUpdateCurrentVersionCheckOldVersionNotExist asserts that UpdateCurrent will add a +// new (higher) version of an existing TUF file, and that an error is raised if +// trying to update to an older version of a TUF file that doesn't exist in the DB. +func TestSQLUpdateCurrentVersionCheckOldVersionNotExist(t *testing.T) { + dbStore, cleanup := sqldbSetup(t) + defer cleanup() + + expected := testUpdateCurrentVersionCheck(t, dbStore, false) assertExpectedGormTUFMeta(t, expected, dbStore.DB) dbStore.DB.Close() diff --git a/server/storage/storage_test.go b/server/storage/storage_test.go index 566450db3..9bc6d0cbb 100644 --- a/server/storage/storage_test.go +++ b/server/storage/storage_test.go @@ -91,8 +91,9 @@ func testUpdateCurrentEmptyStore(t *testing.T, s MetaStore) []StoredTUFMeta { } // UpdateCurrent will successfully add a new (higher) version of an existing TUF file, -// but will return an error if there is an older version of a TUF file. -func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore) []StoredTUFMeta { +// but will return an error if there is an older version of a TUF file. oldVersionExists +// specifies whether the older version should already exist in the DB or not. +func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore, oldVersionExists bool) []StoredTUFMeta { role, gun := data.CanonicalRootRole, "testGUN" expected := []StoredTUFMeta{ @@ -109,14 +110,17 @@ func testUpdateCurrentVersionCheck(t *testing.T, s MetaStore) []StoredTUFMeta { require.NoError(t, s.UpdateCurrent(gun, MakeUpdate(expected[2]))) // Inserting a version that already exists, or that is lower than the current version, will fail - for _, version := range []int{3, 4} { - tufObj := SampleCustomTUFObj(gun, role, version, nil) - err := s.UpdateCurrent(gun, MakeUpdate(tufObj)) - require.Error(t, err, "Error should not be nil") - require.IsType(t, &ErrOldVersion{}, err, - "Expected ErrOldVersion error type, got: %v", err) + version := 3 + if oldVersionExists { + version = 4 } + tufObj := SampleCustomTUFObj(gun, role, version, nil) + err := s.UpdateCurrent(gun, MakeUpdate(tufObj)) + require.Error(t, err, "Error should not be nil") + require.IsType(t, &ErrOldVersion{}, err, + "Expected ErrOldVersion error type, got: %v", err) + assertExpectedTUFMetaInStore(t, s, expected[:2], false) assertExpectedTUFMetaInStore(t, s, expected[2:], true) return expected