2022-01-25 16:21:35 +00:00
|
|
|
package session_test
|
|
|
|
|
|
|
|
import (
|
2022-04-07 16:09:15 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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"
|
2022-04-07 16:09:15 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2022-06-02 08:28:20 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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-04-07 16:09:15 +00:00
|
|
|
func randSigner() ecdsa.PrivateKey {
|
|
|
|
k, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("generate private key: %v", err))
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
return k.PrivateKey
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func randPublicKey() neofscrypto.PublicKey {
|
|
|
|
k := randSigner().PublicKey
|
|
|
|
return (*neofsecdsa.PublicKey)(&k)
|
|
|
|
}
|
2022-01-25 16:21:35 +00:00
|
|
|
|
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
|
|
|
|
usr := *usertest.ID()
|
|
|
|
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
|
|
|
|
signer := randSigner()
|
|
|
|
authKey := neofsecdsa.PublicKey(signer.PublicKey)
|
|
|
|
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()
|
|
|
|
obj := oidtest.ID()
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(obj)
|
|
|
|
var addrV2 refs.Address
|
|
|
|
addr.WriteToV2(&addrV2)
|
|
|
|
var cObj v2session.ObjectSessionContext
|
|
|
|
restoreCtx := func() {
|
|
|
|
cObj.SetAddress(&addrV2)
|
|
|
|
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()
|
|
|
|
copy(id, usertest.ID().WalletBytes())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
require.True(t, val.InvalidAt(3))
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
require.True(t, val.AssertAuthKey(&authKey))
|
|
|
|
},
|
|
|
|
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() {
|
|
|
|
cObj.SetAddress(new(refs.Address))
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
var brokenCnr refs.ContainerID
|
|
|
|
brokenCnr.SetValue(append(addrV2.GetContainerID().GetValue(), 1))
|
|
|
|
var addr refs.Address
|
|
|
|
addr.SetContainerID(&brokenCnr)
|
|
|
|
cObj.SetAddress(&addr)
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
var brokenObj refs.ObjectID
|
|
|
|
brokenObj.SetValue(append(addrV2.GetContainerID().GetValue(), 1))
|
|
|
|
var addr refs.Address
|
|
|
|
addr.SetObjectID(&brokenObj)
|
|
|
|
cObj.SetAddress(&addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreCtx,
|
|
|
|
assert: func(val session.Object) {
|
|
|
|
require.True(t, val.AssertContainer(cnr))
|
|
|
|
require.False(t, val.AssertContainer(cidtest.ID()))
|
|
|
|
require.True(t, val.AssertObject(obj))
|
|
|
|
require.False(t, val.AssertObject(oidtest.ID()))
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
cnr := m.GetBody().GetContext().(*v2session.ObjectSessionContext).GetAddress().GetContainerID().GetValue()
|
|
|
|
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
|
|
|
|
signer := randSigner()
|
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
|
|
|
|
user.IDFromKey(&usr, signer.PublicKey)
|
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)
|
|
|
|
require.Zero(t, cCnr.GetAddress())
|
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-06-02 08:28:20 +00:00
|
|
|
obj := oidtest.ID()
|
|
|
|
|
|
|
|
var objV2 refs.ObjectID
|
|
|
|
obj.WriteToV2(&objV2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.BindContainer(cnr)
|
|
|
|
val.LimitByObject(obj)
|
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)
|
|
|
|
require.Equal(t, &cnrV2, cCnr.GetAddress().GetContainerID())
|
|
|
|
require.Equal(t, &objV2, cCnr.GetAddress().GetObjectID())
|
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)
|
|
|
|
require.Zero(t, cCnr.GetAddress())
|
|
|
|
}
|
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)
|
|
|
|
require.Equal(t, &cnrV2, cObj.GetAddress().GetContainerID())
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObject_LimitByObject(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)
|
|
|
|
require.Zero(t, cCnr.GetAddress())
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
obj := oidtest.ID()
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var objV2 refs.ObjectID
|
|
|
|
obj.WriteToV2(&objV2)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.LimitByObject(obj)
|
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)
|
|
|
|
require.Equal(t, &objV2, cObj.GetAddress().GetObjectID())
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-02 08:28:20 +00:00
|
|
|
obj := oidtest.ID()
|
|
|
|
obj2 := oidtest.ID()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.True(t, x.AssertObject(obj))
|
|
|
|
require.True(t, x.AssertObject(obj2))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
x.LimitByObject(obj)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.True(t, x.AssertObject(obj))
|
|
|
|
require.False(t, x.AssertObject(obj2))
|
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))
|
|
|
|
require.True(t, x.InvalidAt(exp))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
key := randPublicKey()
|
|
|
|
|
|
|
|
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
|
|
|
|
signer := randSigner()
|
|
|
|
|
|
|
|
require.Zero(t, token.Issuer())
|
|
|
|
|
|
|
|
require.NoError(t, token.Sign(signer))
|
|
|
|
|
|
|
|
var issuer user.ID
|
|
|
|
|
|
|
|
user.IDFromKey(&issuer, signer.PublicKey)
|
|
|
|
|
|
|
|
require.True(t, token.Issuer().Equals(issuer))
|
|
|
|
}
|