2020-09-29 12:37:19 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-09-30 11:07:28 +00:00
|
|
|
"crypto/ecdsa"
|
2020-09-29 12:37:19 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-02-15 08:28:42 +00:00
|
|
|
"strconv"
|
2020-09-29 12:37:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-15 08:28:42 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-11-10 07:08:33 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2020-09-29 12:37:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testSHA(t *testing.T) [sha256.Size]byte {
|
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
|
|
|
func testObjectID(t *testing.T) *object.ID {
|
|
|
|
id := object.NewID()
|
|
|
|
id.SetSHA256(testSHA(t))
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:07:28 +00:00
|
|
|
func blankValidObject(t *testing.T, key *ecdsa.PrivateKey) *RawObject {
|
|
|
|
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ownerID := owner.NewID()
|
|
|
|
ownerID.SetNeo3Wallet(wallet)
|
|
|
|
|
|
|
|
obj := NewRaw()
|
2021-11-10 07:08:33 +00:00
|
|
|
obj.SetContainerID(cidtest.GenerateID())
|
2020-09-30 11:07:28 +00:00
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2021-02-15 08:28:42 +00:00
|
|
|
type testNetState struct {
|
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s testNetState) CurrentEpoch() uint64 {
|
|
|
|
return s.epoch
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:37:19 +00:00
|
|
|
func TestFormatValidator_Validate(t *testing.T) {
|
2021-02-15 08:28:42 +00:00
|
|
|
const curEpoch = 13
|
|
|
|
|
|
|
|
v := NewFormatValidator(
|
|
|
|
WithNetState(testNetState{
|
|
|
|
epoch: curEpoch,
|
|
|
|
}),
|
|
|
|
)
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
ownerKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2020-09-29 12:37:19 +00:00
|
|
|
t.Run("nil input", func(t *testing.T) {
|
|
|
|
require.Error(t, v.Validate(nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil identifier", func(t *testing.T) {
|
|
|
|
obj := NewRaw()
|
|
|
|
|
|
|
|
require.True(t, errors.Is(v.Validate(obj.Object()), errNilID))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil container identifier", func(t *testing.T) {
|
|
|
|
obj := NewRaw()
|
|
|
|
obj.SetID(testObjectID(t))
|
|
|
|
|
|
|
|
require.True(t, errors.Is(v.Validate(obj.Object()), errNilCID))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unsigned object", func(t *testing.T) {
|
|
|
|
obj := NewRaw()
|
2021-11-10 07:08:33 +00:00
|
|
|
obj.SetContainerID(cidtest.GenerateID())
|
2020-09-29 12:37:19 +00:00
|
|
|
obj.SetID(testObjectID(t))
|
|
|
|
|
|
|
|
require.Error(t, v.Validate(obj.Object()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("correct w/ session token", func(t *testing.T) {
|
2021-05-31 08:55:40 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(ownerKey.PublicKey()))
|
2021-05-31 11:34:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tok := sessiontest.Generate()
|
|
|
|
tok.SetOwnerID(owner.NewIDFromNeo3Wallet(w))
|
2020-09-29 12:37:19 +00:00
|
|
|
|
|
|
|
obj := NewRaw()
|
2021-11-10 07:08:33 +00:00
|
|
|
obj.SetContainerID(cidtest.GenerateID())
|
2021-05-31 11:34:39 +00:00
|
|
|
obj.SetSessionToken(sessiontest.Generate())
|
|
|
|
obj.SetOwnerID(tok.OwnerID())
|
2020-09-29 12:37:19 +00:00
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
2020-09-29 12:37:19 +00:00
|
|
|
|
|
|
|
require.NoError(t, v.Validate(obj.Object()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("correct w/o session token", func(t *testing.T) {
|
2021-05-31 08:55:40 +00:00
|
|
|
obj := blankValidObject(t, &ownerKey.PrivateKey)
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
2020-09-30 11:07:28 +00:00
|
|
|
|
|
|
|
require.NoError(t, v.Validate(obj.Object()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("tombstone content", func(t *testing.T) {
|
2020-12-01 11:23:28 +00:00
|
|
|
obj := NewRaw()
|
|
|
|
obj.SetType(object.TypeTombstone)
|
|
|
|
|
2021-02-19 11:09:57 +00:00
|
|
|
require.Error(t, v.ValidateContent(obj.Object())) // no tombstone content
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
content := object.NewTombstone()
|
|
|
|
content.SetMembers([]*object.ID{nil})
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
data, err := content.Marshal()
|
2020-09-29 12:37:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-01 11:23:28 +00:00
|
|
|
obj.SetPayload(data)
|
|
|
|
|
2021-02-19 11:09:57 +00:00
|
|
|
require.Error(t, v.ValidateContent(obj.Object())) // no members in tombstone
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
content.SetMembers([]*object.ID{testObjectID(t)})
|
2020-09-30 11:07:28 +00:00
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
data, err = content.Marshal()
|
2020-09-30 11:07:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-01 11:23:28 +00:00
|
|
|
obj.SetPayload(data)
|
|
|
|
|
2021-02-19 11:09:57 +00:00
|
|
|
require.Error(t, v.ValidateContent(obj.Object())) // no expiration epoch in tombstone
|
|
|
|
|
|
|
|
expirationAttribute := object.NewAttribute()
|
|
|
|
expirationAttribute.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
expirationAttribute.SetValue(strconv.Itoa(10))
|
|
|
|
|
|
|
|
obj.SetAttributes(expirationAttribute)
|
|
|
|
|
|
|
|
require.Error(t, v.ValidateContent(obj.Object())) // different expiration values
|
|
|
|
|
|
|
|
content.SetExpirationEpoch(10)
|
|
|
|
data, err = content.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
obj.SetPayload(data)
|
|
|
|
|
|
|
|
require.NoError(t, v.ValidateContent(obj.Object())) // all good
|
2020-09-29 12:37:19 +00:00
|
|
|
})
|
2020-12-17 16:54:38 +00:00
|
|
|
|
|
|
|
t.Run("storage group content", func(t *testing.T) {
|
|
|
|
obj := NewRaw()
|
|
|
|
obj.SetType(object.TypeStorageGroup)
|
|
|
|
|
|
|
|
require.Error(t, v.ValidateContent(obj.Object()))
|
|
|
|
|
|
|
|
content := storagegroup.New()
|
|
|
|
content.SetMembers([]*object.ID{nil})
|
|
|
|
|
|
|
|
data, err := content.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
obj.SetPayload(data)
|
|
|
|
|
|
|
|
require.Error(t, v.ValidateContent(obj.Object()))
|
|
|
|
|
|
|
|
content.SetMembers([]*object.ID{testObjectID(t)})
|
|
|
|
|
|
|
|
data, err = content.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
obj.SetPayload(data)
|
|
|
|
|
|
|
|
require.NoError(t, v.ValidateContent(obj.Object()))
|
|
|
|
})
|
2021-02-15 08:28:42 +00:00
|
|
|
|
|
|
|
t.Run("expiration", func(t *testing.T) {
|
|
|
|
fn := func(val string) *Object {
|
2021-05-31 08:55:40 +00:00
|
|
|
obj := blankValidObject(t, &ownerKey.PrivateKey)
|
2021-02-15 08:28:42 +00:00
|
|
|
|
|
|
|
a := object.NewAttribute()
|
|
|
|
a.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
a.SetValue(val)
|
|
|
|
|
|
|
|
obj.SetAttributes(a)
|
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
2021-02-15 08:28:42 +00:00
|
|
|
|
|
|
|
return obj.Object()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("invalid attribute value", func(t *testing.T) {
|
|
|
|
val := "text"
|
|
|
|
err := v.Validate(fn(val))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("expired object", func(t *testing.T) {
|
|
|
|
val := strconv.FormatUint(curEpoch-1, 10)
|
|
|
|
err := v.Validate(fn(val))
|
|
|
|
require.True(t, errors.Is(err, errExpired))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("alive object", func(t *testing.T) {
|
|
|
|
val := strconv.FormatUint(curEpoch, 10)
|
|
|
|
err := v.Validate(fn(val))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
2021-06-23 13:30:14 +00:00
|
|
|
|
|
|
|
t.Run("attributes", func(t *testing.T) {
|
|
|
|
t.Run("duplication", func(t *testing.T) {
|
2021-06-23 14:20:13 +00:00
|
|
|
obj := blankValidObject(t, &ownerKey.PrivateKey)
|
2021-06-23 13:30:14 +00:00
|
|
|
|
|
|
|
a1 := object.NewAttribute()
|
|
|
|
a1.SetKey("key1")
|
|
|
|
a1.SetValue("val1")
|
|
|
|
|
|
|
|
a2 := object.NewAttribute()
|
|
|
|
a2.SetKey("key2")
|
|
|
|
a2.SetValue("val2")
|
|
|
|
|
|
|
|
obj.SetAttributes(a1, a2)
|
|
|
|
|
|
|
|
err := v.checkAttributes(obj.Object())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a2.SetKey(a1.Key())
|
|
|
|
|
|
|
|
err = v.checkAttributes(obj.Object())
|
|
|
|
require.Equal(t, errDuplAttr, err)
|
|
|
|
})
|
2021-06-23 14:20:13 +00:00
|
|
|
|
|
|
|
t.Run("empty value", func(t *testing.T) {
|
|
|
|
obj := blankValidObject(t, &ownerKey.PrivateKey)
|
|
|
|
|
|
|
|
a := object.NewAttribute()
|
|
|
|
a.SetKey("key")
|
|
|
|
|
|
|
|
obj.SetAttributes(a)
|
|
|
|
|
|
|
|
err := v.checkAttributes(obj.Object())
|
|
|
|
require.Equal(t, errEmptyAttrVal, err)
|
|
|
|
})
|
2021-06-23 13:30:14 +00:00
|
|
|
})
|
2020-09-29 12:37:19 +00:00
|
|
|
}
|