Skip to content

Commit

Permalink
metabase/test: create fixed checksums for test objects
Browse files Browse the repository at this point in the history
SDK somehow generates a random checksum (with a random type too) every time now,
so the old tests should be ready for it (do not use test SDK package in fact).
Closes #2917, and improves test granularity a little.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell committed Aug 20, 2024
1 parent 0d9c67f commit 5eeff3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
15 changes: 8 additions & 7 deletions pkg/local_object_storage/metabase/db_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package meta_test

import (
"crypto/rand"
"os"
"strconv"
"testing"

meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
"github.com/nspcc-dev/neofs-sdk-go/checksum"
checksumtest "github.com/nspcc-dev/neofs-sdk-go/checksum/test"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/nspcc-dev/neofs-sdk-go/object"
Expand All @@ -16,7 +16,6 @@ import (
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
"github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/nspcc-dev/tzhash/tz"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -76,12 +75,14 @@ func generateObjectWithCID(t testing.TB, cnr cid.ID) *object.Object {
ver.SetMajor(2)
ver.SetMinor(1)

csum := checksumtest.Checksum()

var csumTZ checksum.Checksum
csumTZ.SetTillichZemor(tz.Sum(csum.Value()))
payload := make([]byte, 10)
_, err := rand.Read(payload)
require.NoError(t, err)

payload := []byte{1, 2, 3, 4, 5}
csum, err := checksum.NewFromData(checksum.SHA256, payload)
require.NoError(t, err)
csumTZ, err := checksum.NewFromData(checksum.TillichZemor, payload)
require.NoError(t, err)

obj := object.New()
obj.SetID(oidtest.ID())
Expand Down
42 changes: 25 additions & 17 deletions pkg/local_object_storage/metabase/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,32 +376,40 @@ func TestDB_SelectPayloadHash(t *testing.T) {
cs, _ := raw1.PayloadChecksum()
payloadHash := [sha256.Size]byte(cs.Value())

fs := objectSDK.SearchFilters{}
fs.AddPayloadHashFilter(objectSDK.MatchStringEqual, payloadHash)
t.Run("equal filter", func(t *testing.T) {
fs := objectSDK.SearchFilters{}
fs.AddPayloadHashFilter(objectSDK.MatchStringEqual, payloadHash)

testSelect(t, db, cnr, fs, object.AddressOf(raw1))
testSelect(t, db, cnr, fs, object.AddressOf(raw1))
})

fs = objectSDK.SearchFilters{}
fs.AddFilter(objectSDK.FilterPayloadChecksum,
hex.EncodeToString(payloadHash[:len(payloadHash)-1]),
objectSDK.MatchCommonPrefix)
t.Run("common prefix filter", func(t *testing.T) {
fs := objectSDK.SearchFilters{}
fs.AddFilter(objectSDK.FilterPayloadChecksum,
hex.EncodeToString(payloadHash[:len(payloadHash)-1]),
objectSDK.MatchCommonPrefix)

testSelect(t, db, cnr, fs, object.AddressOf(raw1))
testSelect(t, db, cnr, fs, object.AddressOf(raw1))
})

fs = objectSDK.SearchFilters{}
fs.AddPayloadHashFilter(objectSDK.MatchStringNotEqual, payloadHash)
t.Run("not equal filter", func(t *testing.T) {
fs := objectSDK.SearchFilters{}
fs.AddPayloadHashFilter(objectSDK.MatchStringNotEqual, payloadHash)

testSelect(t, db, cnr, fs, object.AddressOf(raw2))
testSelect(t, db, cnr, fs, object.AddressOf(raw2))
})

fs = objectSDK.SearchFilters{}
fs.AddFilter(objectSDK.FilterPayloadChecksum,
"",
objectSDK.MatchNotPresent)
t.Run("not present filter", func(t *testing.T) {
fs := objectSDK.SearchFilters{}
fs.AddFilter(objectSDK.FilterPayloadChecksum,
"",
objectSDK.MatchNotPresent)

testSelect(t, db, cnr, fs)
testSelect(t, db, cnr, fs)
})

t.Run("invalid hashes", func(t *testing.T) {
fs = objectSDK.SearchFilters{}
fs := objectSDK.SearchFilters{}
otherHash := payloadHash
otherHash[0]++
fs.AddPayloadHashFilter(objectSDK.MatchStringNotEqual, otherHash)
Expand Down

0 comments on commit 5eeff3f

Please sign in to comment.