2022-01-25 16:21:35 +00:00
|
|
|
package session_test
|
|
|
|
|
|
|
|
import (
|
2022-04-07 16:09:15 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
2022-01-25 16:21:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
"github.com/google/uuid"
|
2022-06-02 08:28:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2022-05-25 09:03:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2022-01-25 16:21:35 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-05-25 09:03:22 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2023-04-25 09:11:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/crypto/test"
|
2022-05-25 09:03:22 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2022-01-25 16:21:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test"
|
2022-05-25 16:04:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-06-02 08:28:20 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2022-01-25 16:21:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObjectProtocolV2(t *testing.T) {
|
|
|
|
var validV2 v2session.Token
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var body v2session.TokenBody
|
|
|
|
validV2.SetBody(&body)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// ID
|
|
|
|
id := uuid.New()
|
|
|
|
binID, err := id.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
restoreID := func() {
|
|
|
|
body.SetID(binID)
|
|
|
|
}
|
|
|
|
restoreID()
|
|
|
|
|
|
|
|
// Owner
|
2023-04-25 09:11:42 +00:00
|
|
|
usr := *usertest.ID(t)
|
2022-06-02 08:28:20 +00:00
|
|
|
var usrV2 refs.OwnerID
|
|
|
|
usr.WriteToV2(&usrV2)
|
|
|
|
restoreUser := func() {
|
|
|
|
body.SetOwnerID(&usrV2)
|
|
|
|
}
|
|
|
|
restoreUser()
|
|
|
|
|
|
|
|
// Lifetime
|
|
|
|
var lifetime v2session.TokenLifetime
|
|
|
|
lifetime.SetIat(1)
|
|
|
|
lifetime.SetNbf(2)
|
|
|
|
lifetime.SetExp(3)
|
|
|
|
restoreLifetime := func() {
|
|
|
|
body.SetLifetime(&lifetime)
|
|
|
|
}
|
|
|
|
restoreLifetime()
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Session key
|
2023-04-25 09:11:42 +00:00
|
|
|
signer := test.RandomSignerRFC6979(t)
|
2023-04-25 08:31:27 +00:00
|
|
|
authKey := signer.Public()
|
2022-06-02 08:28:20 +00:00
|
|
|
binAuthKey := make([]byte, authKey.MaxEncodedSize())
|
|
|
|
binAuthKey = binAuthKey[:authKey.Encode(binAuthKey)]
|
|
|
|
restoreAuthKey := func() {
|
|
|
|
body.SetSessionKey(binAuthKey)
|
|
|
|
}
|
|
|
|
restoreAuthKey()
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Context
|
|
|
|
cnr := cidtest.ID()
|
2022-09-16 14:13:57 +00:00
|
|
|
obj1 := oidtest.ID()
|
|
|
|
obj2 := oidtest.ID()
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
var obj1V2 refs.ObjectID
|
|
|
|
obj1.WriteToV2(&obj1V2)
|
|
|
|
var obj2V2 refs.ObjectID
|
|
|
|
obj2.WriteToV2(&obj2V2)
|
2022-06-02 08:28:20 +00:00
|
|
|
var cObj v2session.ObjectSessionContext
|
|
|
|
restoreCtx := func() {
|
2022-09-16 14:13:57 +00:00
|
|
|
cObj.SetTarget(&cnrV2, obj1V2, obj2V2)
|
2022-06-02 08:28:20 +00:00
|
|
|
body.SetContext(&cObj)
|
|
|
|
}
|
|
|
|
restoreCtx()
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Signature
|
|
|
|
var sig refs.Signature
|
|
|
|
restoreSig := func() {
|
|
|
|
validV2.SetSignature(&sig)
|
|
|
|
}
|
|
|
|
restoreSig()
|
|
|
|
|
|
|
|
// TODO(@cthulhu-rider): #260 use functionality for message corruption
|
|
|
|
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
corrupt []func()
|
|
|
|
restore func()
|
|
|
|
assert func(session.Object)
|
|
|
|
breakSign func(*v2session.Token)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Signature",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
validV2.SetSignature(nil)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreSig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ID",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
body.SetID([]byte{1, 2, 3})
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
id, err := uuid.NewDCEPerson()
|
|
|
|
require.NoError(t, err)
|
|
|
|
bindID, err := id.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
body.SetID(bindID)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreID,
|
|
|
|
assert: func(val session.Object) {
|
|
|
|
require.Equal(t, id, val.ID())
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
id := m.GetBody().GetID()
|
|
|
|
id[len(id)-1]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "User",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
var brokenUsrV2 refs.OwnerID
|
|
|
|
brokenUsrV2.SetValue(append(usrV2.GetValue(), 1))
|
|
|
|
body.SetOwnerID(&brokenUsrV2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreUser,
|
|
|
|
assert: func(val session.Object) {
|
|
|
|
require.Equal(t, usr, val.Issuer())
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
id := m.GetBody().GetOwnerID().GetValue()
|
2023-04-25 09:11:42 +00:00
|
|
|
copy(id, usertest.ID(t).WalletBytes())
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Lifetime",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
body.SetLifetime(nil)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreLifetime,
|
|
|
|
assert: func(val session.Object) {
|
|
|
|
require.True(t, val.InvalidAt(1))
|
|
|
|
require.False(t, val.InvalidAt(2))
|
2022-12-23 11:58:18 +00:00
|
|
|
require.False(t, val.InvalidAt(3))
|
|
|
|
require.True(t, val.InvalidAt(4))
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
lt := m.GetBody().GetLifetime()
|
|
|
|
lt.SetIat(lt.GetIat() + 1)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Auth key",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
body.SetSessionKey(nil)
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
body.SetSessionKey([]byte{})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreAuthKey,
|
|
|
|
assert: func(val session.Object) {
|
2023-04-25 08:31:27 +00:00
|
|
|
require.True(t, val.AssertAuthKey(authKey))
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
body := m.GetBody()
|
|
|
|
key := body.GetSessionKey()
|
|
|
|
cp := slice.Copy(key)
|
|
|
|
cp[len(cp)-1]++
|
|
|
|
body.SetSessionKey(cp)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Context",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
body.SetContext(nil)
|
|
|
|
},
|
|
|
|
func() {
|
2022-09-16 14:13:57 +00:00
|
|
|
cObj.SetTarget(nil)
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
func() {
|
|
|
|
var brokenCnr refs.ContainerID
|
2022-09-16 14:13:57 +00:00
|
|
|
brokenCnr.SetValue(append(cnrV2.GetValue(), 1))
|
|
|
|
cObj.SetTarget(&brokenCnr)
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
func() {
|
|
|
|
var brokenObj refs.ObjectID
|
2022-09-16 14:13:57 +00:00
|
|
|
brokenObj.SetValue(append(obj1V2.GetValue(), 1))
|
|
|
|
cObj.SetTarget(&cnrV2, brokenObj)
|
2022-06-02 08:28:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreCtx,
|
|
|
|
assert: func(val session.Object) {
|
|
|
|
require.True(t, val.AssertContainer(cnr))
|
|
|
|
require.False(t, val.AssertContainer(cidtest.ID()))
|
2022-09-16 14:13:57 +00:00
|
|
|
require.True(t, val.AssertObject(obj1))
|
|
|
|
require.True(t, val.AssertObject(obj2))
|
2022-06-02 08:28:20 +00:00
|
|
|
require.False(t, val.AssertObject(oidtest.ID()))
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
2022-09-16 14:13:57 +00:00
|
|
|
cnr := m.GetBody().GetContext().(*v2session.ObjectSessionContext).GetContainer().GetValue()
|
2022-06-02 08:28:20 +00:00
|
|
|
cnr[len(cnr)-1]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
var val session.Object
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
for i, corrupt := range testcase.corrupt {
|
|
|
|
corrupt()
|
|
|
|
require.Error(t, val.ReadFromV2(validV2), testcase.name, fmt.Sprintf("corrupt #%d", i))
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
testcase.restore()
|
|
|
|
require.NoError(t, val.ReadFromV2(validV2), testcase.name, fmt.Sprintf("corrupt #%d", i))
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
if testcase.assert != nil {
|
|
|
|
testcase.assert(val)
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
if testcase.breakSign != nil {
|
|
|
|
require.NoError(t, val.Sign(signer), testcase.name)
|
|
|
|
require.True(t, val.VerifySignature(), testcase.name)
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var signedV2 v2session.Token
|
|
|
|
val.WriteToV2(&signedV2)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var restored session.Object
|
|
|
|
require.NoError(t, restored.ReadFromV2(signedV2), testcase.name)
|
|
|
|
require.True(t, restored.VerifySignature(), testcase.name)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
testcase.breakSign(&signedV2)
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, restored.ReadFromV2(signedV2), testcase.name)
|
|
|
|
require.False(t, restored.VerifySignature(), testcase.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_WriteToV2(t *testing.T) {
|
|
|
|
var val session.Object
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assert := func(baseAssert func(v2session.Token)) {
|
|
|
|
var m v2session.Token
|
|
|
|
val.WriteToV2(&m)
|
|
|
|
baseAssert(m)
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// ID
|
|
|
|
id := uuid.New()
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
binID, err := id.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.SetID(id)
|
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
require.Equal(t, binID, m.GetBody().GetID())
|
2022-01-25 16:21:35 +00:00
|
|
|
})
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Owner/Signature
|
2023-04-25 09:11:42 +00:00
|
|
|
signer := test.RandomSignerRFC6979(t)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val.Sign(signer))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var usr user.ID
|
2023-04-25 08:31:27 +00:00
|
|
|
require.NoError(t, user.IDFromSigner(&usr, signer))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var usrV2 refs.OwnerID
|
|
|
|
usr.WriteToV2(&usrV2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
require.Equal(t, &usrV2, m.GetBody().GetOwnerID())
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
sig := m.GetSignature()
|
|
|
|
require.NotZero(t, sig.GetKey())
|
|
|
|
require.NotZero(t, sig.GetSign())
|
|
|
|
})
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Lifetime
|
|
|
|
const iat, nbf, exp = 1, 2, 3
|
|
|
|
val.SetIat(iat)
|
|
|
|
val.SetNbf(nbf)
|
|
|
|
val.SetExp(exp)
|
|
|
|
|
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
lt := m.GetBody().GetLifetime()
|
|
|
|
require.EqualValues(t, iat, lt.GetIat())
|
|
|
|
require.EqualValues(t, nbf, lt.GetNbf())
|
|
|
|
require.EqualValues(t, exp, lt.GetExp())
|
2022-01-25 16:21:35 +00:00
|
|
|
})
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Context
|
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Zero(t, cCnr.GetContainer())
|
|
|
|
require.Zero(t, cCnr.GetObjects())
|
2022-04-07 16:09:15 +00:00
|
|
|
})
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
obj1 := oidtest.ID()
|
|
|
|
obj2 := oidtest.ID()
|
2022-06-02 08:28:20 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
var obj1V2 refs.ObjectID
|
|
|
|
obj1.WriteToV2(&obj1V2)
|
|
|
|
var obj2V2 refs.ObjectID
|
|
|
|
obj2.WriteToV2(&obj2V2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.BindContainer(cnr)
|
2022-09-16 14:13:57 +00:00
|
|
|
val.LimitByObjects(obj1, obj2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Equal(t, &cnrV2, cCnr.GetContainer())
|
|
|
|
require.Equal(t, []refs.ObjectID{obj1V2, obj2V2}, cCnr.GetObjects())
|
2022-01-25 16:21:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_BindContainer(t *testing.T) {
|
|
|
|
var val session.Object
|
|
|
|
var m v2session.Token
|
|
|
|
filled := sessiontest.Object()
|
|
|
|
|
|
|
|
assertDefaults := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Zero(t, cCnr.GetContainer())
|
|
|
|
require.Zero(t, cCnr.GetObjects())
|
2022-06-02 08:28:20 +00:00
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertBinary := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.Unmarshal(val.Marshal()))
|
|
|
|
baseAssert()
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertJSON := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
jd, err := val.MarshalJSON()
|
2022-01-25 16:21:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.UnmarshalJSON(jd))
|
|
|
|
baseAssert()
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertDefaults()
|
|
|
|
assertBinary(assertDefaults)
|
|
|
|
assertJSON(assertDefaults)
|
|
|
|
|
|
|
|
// set value
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
|
|
|
|
val.BindContainer(cnr)
|
|
|
|
|
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertCnr := func() {
|
|
|
|
cObj, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Equal(t, &cnrV2, cObj.GetContainer())
|
2022-06-02 08:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertCnr()
|
|
|
|
assertBinary(assertCnr)
|
|
|
|
assertJSON(assertCnr)
|
2022-01-25 16:21:35 +00:00
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_AssertContainer(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Object
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
cnr := cidtest.ID()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
require.False(t, x.AssertContainer(cnr))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
x.BindContainer(cnr)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
require.True(t, x.AssertContainer(cnr))
|
|
|
|
}
|
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
func TestObject_LimitByObjects(t *testing.T) {
|
2022-06-02 08:28:20 +00:00
|
|
|
var val session.Object
|
|
|
|
var m v2session.Token
|
|
|
|
filled := sessiontest.Object()
|
|
|
|
|
|
|
|
assertDefaults := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Zero(t, cCnr.GetContainer())
|
|
|
|
require.Zero(t, cCnr.GetObjects())
|
2022-06-02 08:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertBinary := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
|
|
|
|
|
|
|
require.NoError(t, val2.Unmarshal(val.Marshal()))
|
|
|
|
baseAssert()
|
|
|
|
}
|
|
|
|
|
|
|
|
assertJSON := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
|
|
|
|
|
|
|
jd, err := val.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, val2.UnmarshalJSON(jd))
|
|
|
|
baseAssert()
|
|
|
|
}
|
|
|
|
|
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertDefaults()
|
|
|
|
assertBinary(assertDefaults)
|
|
|
|
assertJSON(assertDefaults)
|
|
|
|
|
|
|
|
// set value
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
obj1 := oidtest.ID()
|
|
|
|
obj2 := oidtest.ID()
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
var obj1V2 refs.ObjectID
|
|
|
|
obj1.WriteToV2(&obj1V2)
|
|
|
|
var obj2V2 refs.ObjectID
|
|
|
|
obj2.WriteToV2(&obj2V2)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
val.LimitByObjects(obj1, obj2)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertObj := func() {
|
|
|
|
cObj, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
2022-09-16 14:13:57 +00:00
|
|
|
require.Equal(t, []refs.ObjectID{obj1V2, obj2V2}, cObj.GetObjects())
|
2022-06-02 08:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertObj()
|
|
|
|
assertBinary(assertObj)
|
|
|
|
assertJSON(assertObj)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_AssertObject(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Object
|
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
obj1 := oidtest.ID()
|
2022-06-02 08:28:20 +00:00
|
|
|
obj2 := oidtest.ID()
|
2022-09-16 14:13:57 +00:00
|
|
|
objOther := oidtest.ID()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
require.True(t, x.AssertObject(obj1))
|
2022-06-02 08:28:20 +00:00
|
|
|
require.True(t, x.AssertObject(obj2))
|
2022-09-16 14:13:57 +00:00
|
|
|
require.True(t, x.AssertObject(objOther))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
x.LimitByObjects(obj1, obj2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-09-16 14:13:57 +00:00
|
|
|
require.True(t, x.AssertObject(obj1))
|
|
|
|
require.True(t, x.AssertObject(obj2))
|
|
|
|
require.False(t, x.AssertObject(objOther))
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_InvalidAt(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Object
|
|
|
|
|
|
|
|
nbf := rand.Uint64()
|
|
|
|
if nbf == math.MaxUint64 {
|
|
|
|
nbf--
|
|
|
|
}
|
|
|
|
|
|
|
|
iat := nbf
|
|
|
|
exp := iat + 1
|
|
|
|
|
|
|
|
x.SetNbf(nbf)
|
|
|
|
x.SetIat(iat)
|
|
|
|
x.SetExp(exp)
|
|
|
|
|
|
|
|
require.True(t, x.InvalidAt(nbf-1))
|
|
|
|
require.True(t, x.InvalidAt(iat-1))
|
|
|
|
require.False(t, x.InvalidAt(iat))
|
2022-12-23 11:58:18 +00:00
|
|
|
require.False(t, x.InvalidAt(exp))
|
|
|
|
require.True(t, x.InvalidAt(exp+1))
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_ID(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Object
|
|
|
|
|
|
|
|
require.Zero(t, x.ID())
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
|
|
|
x.SetID(id)
|
|
|
|
|
|
|
|
require.Equal(t, id, x.ID())
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_AssertAuthKey(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Object
|
|
|
|
|
2023-04-25 09:11:42 +00:00
|
|
|
key := test.RandomSignerRFC6979(t).Public()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
|
|
|
require.False(t, x.AssertAuthKey(key))
|
|
|
|
|
|
|
|
x.SetAuthKey(key)
|
|
|
|
|
|
|
|
require.True(t, x.AssertAuthKey(key))
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_ForVerb(t *testing.T) {
|
|
|
|
var val session.Object
|
|
|
|
var m v2session.Token
|
|
|
|
filled := sessiontest.Object()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertDefaults := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Zero(t, cCnr.GetVerb())
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertBinary := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.Unmarshal(val.Marshal()))
|
|
|
|
baseAssert()
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertJSON := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
jd, err := val.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.UnmarshalJSON(jd))
|
|
|
|
baseAssert()
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.WriteToV2(&m)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertDefaults()
|
|
|
|
assertBinary(assertDefaults)
|
|
|
|
assertJSON(assertDefaults)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// set value
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertVerb := func(verb v2session.ObjectSessionVerb) {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ObjectSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, verb, cCnr.GetVerb())
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
for from, to := range map[session.ObjectVerb]v2session.ObjectSessionVerb{
|
|
|
|
session.VerbObjectPut: v2session.ObjectVerbPut,
|
|
|
|
session.VerbObjectGet: v2session.ObjectVerbGet,
|
|
|
|
session.VerbObjectHead: v2session.ObjectVerbHead,
|
|
|
|
session.VerbObjectSearch: v2session.ObjectVerbSearch,
|
|
|
|
session.VerbObjectRangeHash: v2session.ObjectVerbRangeHash,
|
|
|
|
session.VerbObjectRange: v2session.ObjectVerbRange,
|
|
|
|
session.VerbObjectDelete: v2session.ObjectVerbDelete,
|
|
|
|
} {
|
|
|
|
val.ForVerb(from)
|
|
|
|
|
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertVerb(to)
|
|
|
|
assertBinary(func() { assertVerb(to) })
|
|
|
|
assertJSON(func() { assertVerb(to) })
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
2022-06-02 08:28:20 +00:00
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestObject_AssertVerb(t *testing.T) {
|
|
|
|
var x session.Object
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
const v1, v2 = session.VerbObjectGet, session.VerbObjectPut
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.False(t, x.AssertVerb(v1, v2))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
x.ForVerb(v1)
|
|
|
|
require.True(t, x.AssertVerb(v1))
|
|
|
|
require.False(t, x.AssertVerb(v2))
|
|
|
|
require.True(t, x.AssertVerb(v1, v2))
|
|
|
|
require.True(t, x.AssertVerb(v2, v1))
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
2022-05-25 16:04:09 +00:00
|
|
|
|
|
|
|
func TestObject_Issuer(t *testing.T) {
|
|
|
|
var token session.Object
|
2023-04-25 09:11:42 +00:00
|
|
|
signer := test.RandomSignerRFC6979(t)
|
2022-05-25 16:04:09 +00:00
|
|
|
|
|
|
|
require.Zero(t, token.Issuer())
|
|
|
|
|
|
|
|
require.NoError(t, token.Sign(signer))
|
|
|
|
|
|
|
|
var issuer user.ID
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
require.NoError(t, user.IDFromSigner(&issuer, signer))
|
2022-05-25 16:04:09 +00:00
|
|
|
|
|
|
|
require.True(t, token.Issuer().Equals(issuer))
|
|
|
|
}
|
2022-06-04 13:45:25 +00:00
|
|
|
|
|
|
|
func TestObject_Sign(t *testing.T) {
|
|
|
|
val := sessiontest.Object()
|
|
|
|
|
2023-04-25 09:11:42 +00:00
|
|
|
require.NoError(t, val.Sign(test.RandomSignerRFC6979(t)))
|
2022-06-04 13:45:25 +00:00
|
|
|
|
|
|
|
require.True(t, val.VerifySignature())
|
|
|
|
}
|