2021-10-27 10:00:15 +00:00
|
|
|
package session_test
|
|
|
|
|
|
|
|
import (
|
2024-07-12 12:42:55 +00:00
|
|
|
"bytes"
|
2023-09-08 14:15:08 +00:00
|
|
|
"crypto/rand"
|
2022-06-02 08:28:20 +00:00
|
|
|
"fmt"
|
2022-04-07 16:09:15 +00:00
|
|
|
"math"
|
2023-09-08 14:15:08 +00:00
|
|
|
mrand "math/rand"
|
2021-10-27 10:00:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2023-03-07 11:20:03 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
sessiontest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
2022-04-07 16:09:15 +00:00
|
|
|
"github.com/google/uuid"
|
2021-10-27 10:00:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestContainerProtocolV2(t *testing.T) {
|
|
|
|
var validV2 v2session.Token
|
|
|
|
|
|
|
|
var body v2session.TokenBody
|
|
|
|
validV2.SetBody(&body)
|
|
|
|
|
|
|
|
// ID
|
2022-04-07 16:09:15 +00:00
|
|
|
id := uuid.New()
|
2022-06-02 08:28:20 +00:00
|
|
|
binID, err := id.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
restoreID := func() {
|
|
|
|
body.SetID(binID)
|
|
|
|
}
|
|
|
|
restoreID()
|
|
|
|
|
|
|
|
// Owner
|
2023-11-21 08:35:10 +00:00
|
|
|
usr := usertest.ID()
|
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()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Session key
|
|
|
|
signer := randSigner()
|
2022-12-13 14:36:35 +00:00
|
|
|
authKey := frostfsecdsa.PublicKey(signer.PublicKey)
|
2022-06-02 08:28:20 +00:00
|
|
|
binAuthKey := make([]byte, authKey.MaxEncodedSize())
|
|
|
|
binAuthKey = binAuthKey[:authKey.Encode(binAuthKey)]
|
|
|
|
restoreAuthKey := func() {
|
|
|
|
body.SetSessionKey(binAuthKey)
|
|
|
|
}
|
|
|
|
restoreAuthKey()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Context
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
var cCnr v2session.ContainerSessionContext
|
|
|
|
restoreCtx := func() {
|
|
|
|
cCnr.SetContainerID(&cnrV2)
|
|
|
|
cCnr.SetWildcard(false)
|
|
|
|
body.SetContext(&cCnr)
|
|
|
|
}
|
|
|
|
restoreCtx()
|
2021-10-27 10:00:15 +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.Container)
|
|
|
|
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.Container) {
|
|
|
|
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.Container) {
|
|
|
|
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.Container) {
|
|
|
|
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.Container) {
|
|
|
|
require.True(t, val.AssertAuthKey(&authKey))
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
body := m.GetBody()
|
|
|
|
key := body.GetSessionKey()
|
2024-07-12 12:42:55 +00:00
|
|
|
cp := bytes.Clone(key)
|
2022-06-02 08:28:20 +00:00
|
|
|
cp[len(cp)-1]++
|
|
|
|
body.SetSessionKey(cp)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Context",
|
|
|
|
corrupt: []func(){
|
|
|
|
func() {
|
|
|
|
body.SetContext(nil)
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
cCnr.SetWildcard(true)
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
cCnr.SetContainerID(nil)
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
var brokenCnr refs.ContainerID
|
|
|
|
brokenCnr.SetValue(append(cnrV2.GetValue(), 1))
|
|
|
|
cCnr.SetContainerID(&brokenCnr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
restore: restoreCtx,
|
|
|
|
assert: func(val session.Container) {
|
|
|
|
require.True(t, val.AppliedTo(cnr))
|
|
|
|
require.False(t, val.AppliedTo(cidtest.ID()))
|
|
|
|
},
|
|
|
|
breakSign: func(m *v2session.Token) {
|
|
|
|
cnr := m.GetBody().GetContext().(*v2session.ContainerSessionContext).ContainerID().GetValue()
|
|
|
|
cnr[len(cnr)-1]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
var val session.Container
|
2021-10-27 10:00:15 +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))
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
testcase.restore()
|
|
|
|
require.NoError(t, val.ReadFromV2(validV2), testcase.name)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
if testcase.assert != nil {
|
|
|
|
testcase.assert(val)
|
|
|
|
}
|
2022-04-07 16:09:15 +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)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var signedV2 v2session.Token
|
|
|
|
val.WriteToV2(&signedV2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var restored session.Container
|
|
|
|
require.NoError(t, restored.ReadFromV2(signedV2), testcase.name)
|
|
|
|
require.True(t, restored.VerifySignature(), testcase.name)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
testcase.breakSign(&signedV2)
|
2021-10-27 10:00:15 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestContainer_WriteToV2(t *testing.T) {
|
|
|
|
var val session.Container
|
2021-10-27 10:00:15 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID
|
|
|
|
id := uuid.New()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
binID, err := id.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +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())
|
2021-10-27 10:00:15 +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))
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var usr user.ID
|
|
|
|
user.IDFromKey(&usr, signer.PublicKey)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var usrV2 refs.OwnerID
|
|
|
|
usr.WriteToV2(&usrV2)
|
2021-10-27 10:00: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())
|
|
|
|
})
|
2021-10-27 10:00:15 +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())
|
2021-10-27 10:00:15 +00:00
|
|
|
})
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// Context
|
|
|
|
assert(func(m v2session.Token) {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, cCnr.Wildcard())
|
|
|
|
require.Zero(t, cCnr.ContainerID())
|
2022-04-07 16:09:15 +00:00
|
|
|
})
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
cnr := cidtest.ID()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
val.ApplyOnlyTo(cnr)
|
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.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.False(t, cCnr.Wildcard())
|
|
|
|
require.Equal(t, &cnrV2, cCnr.ContainerID())
|
2021-10-27 10:00:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestContainer_ApplyOnlyTo(t *testing.T) {
|
|
|
|
var val session.Container
|
|
|
|
var m v2session.Token
|
|
|
|
filled := sessiontest.Container()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertDefaults := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, cCnr.Wildcard())
|
|
|
|
require.Zero(t, cCnr.ContainerID())
|
|
|
|
}
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertBinary := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.Unmarshal(val.Marshal()))
|
|
|
|
baseAssert()
|
|
|
|
}
|
|
|
|
|
|
|
|
assertJSON := func(baseAssert func()) {
|
|
|
|
val2 := filled
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
jd, err := val.MarshalJSON()
|
2021-10-27 10:00:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.NoError(t, val2.UnmarshalJSON(jd))
|
|
|
|
baseAssert()
|
|
|
|
}
|
2021-10-27 10:00:15 +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.ApplyOnlyTo(cnr)
|
|
|
|
|
|
|
|
val.WriteToV2(&m)
|
|
|
|
|
|
|
|
assertCnr := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.False(t, cCnr.Wildcard())
|
|
|
|
require.Equal(t, &cnrV2, cCnr.ContainerID())
|
|
|
|
}
|
|
|
|
|
|
|
|
assertCnr()
|
|
|
|
assertBinary(assertCnr)
|
|
|
|
assertJSON(assertCnr)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestContainer_AppliedTo(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Container
|
|
|
|
|
|
|
|
cnr1 := cidtest.ID()
|
|
|
|
cnr2 := cidtest.ID()
|
|
|
|
|
|
|
|
require.True(t, x.AppliedTo(cnr1))
|
|
|
|
require.True(t, x.AppliedTo(cnr2))
|
|
|
|
|
|
|
|
x.ApplyOnlyTo(cnr1)
|
|
|
|
|
|
|
|
require.True(t, x.AppliedTo(cnr1))
|
|
|
|
require.False(t, x.AppliedTo(cnr2))
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func TestContainer_InvalidAt(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Container
|
|
|
|
|
2023-09-08 14:15:08 +00:00
|
|
|
nbf := mrand.Uint64()
|
2022-04-07 16:09:15 +00:00
|
|
|
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 TestContainer_ID(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Container
|
|
|
|
|
|
|
|
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 TestContainer_AssertAuthKey(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var x session.Container
|
|
|
|
|
|
|
|
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 TestContainer_ForVerb(t *testing.T) {
|
|
|
|
var val session.Container
|
|
|
|
var m v2session.Token
|
|
|
|
filled := sessiontest.Container()
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertDefaults := func() {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Zero(t, cCnr.Verb())
|
|
|
|
}
|
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-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
assertVerb := func(verb v2session.ContainerSessionVerb) {
|
|
|
|
cCnr, ok := m.GetBody().GetContext().(*v2session.ContainerSessionContext)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, verb, cCnr.Verb())
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
for from, to := range map[session.ContainerVerb]v2session.ContainerSessionVerb{
|
|
|
|
session.VerbContainerPut: v2session.ContainerVerbPut,
|
|
|
|
session.VerbContainerDelete: v2session.ContainerVerbDelete,
|
|
|
|
session.VerbContainerSetEACL: v2session.ContainerVerbSetEACL,
|
|
|
|
} {
|
|
|
|
val.ForVerb(from)
|
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
|
|
|
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 TestContainer_AssertVerb(t *testing.T) {
|
|
|
|
var x session.Container
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
const v1, v2 = session.VerbContainerPut, session.VerbContainerDelete
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
require.False(t, x.AssertVerb(v1))
|
|
|
|
require.False(t, x.AssertVerb(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))
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
2022-05-04 14:49:55 +00:00
|
|
|
|
2022-05-23 16:15:11 +00:00
|
|
|
func TestIssuedBy(t *testing.T) {
|
2022-05-04 14:49:55 +00:00
|
|
|
var (
|
|
|
|
token session.Container
|
|
|
|
issuer user.ID
|
|
|
|
signer = randSigner()
|
|
|
|
)
|
|
|
|
|
|
|
|
user.IDFromKey(&issuer, signer.PublicKey)
|
|
|
|
|
2022-05-23 16:15:11 +00:00
|
|
|
require.False(t, session.IssuedBy(token, issuer))
|
2022-05-04 14:49:55 +00:00
|
|
|
|
|
|
|
require.NoError(t, token.Sign(signer))
|
2022-05-23 16:15:11 +00:00
|
|
|
require.True(t, session.IssuedBy(token, issuer))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainer_Issuer(t *testing.T) {
|
|
|
|
var token session.Container
|
|
|
|
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))
|
2022-05-04 14:49:55 +00:00
|
|
|
}
|
2022-06-04 13:45:25 +00:00
|
|
|
|
|
|
|
func TestContainer_Sign(t *testing.T) {
|
|
|
|
val := sessiontest.Container()
|
|
|
|
|
|
|
|
require.NoError(t, val.Sign(randSigner()))
|
|
|
|
|
|
|
|
require.True(t, val.VerifySignature())
|
|
|
|
}
|
2022-06-07 17:25:29 +00:00
|
|
|
|
|
|
|
func TestContainer_VerifyDataSignature(t *testing.T) {
|
|
|
|
signer := randSigner()
|
|
|
|
|
|
|
|
var tok session.Container
|
|
|
|
|
|
|
|
data := make([]byte, 100)
|
|
|
|
rand.Read(data)
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
|
|
|
require.NoError(t, sig.Calculate(frostfsecdsa.SignerRFC6979(signer), data))
|
2022-06-07 17:25:29 +00:00
|
|
|
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sig.WriteToV2(&sigV2)
|
|
|
|
|
|
|
|
require.False(t, tok.VerifySessionDataSignature(data, sigV2.GetSign()))
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
tok.SetAuthKey((*frostfsecdsa.PublicKeyRFC6979)(&signer.PublicKey))
|
2022-06-07 17:25:29 +00:00
|
|
|
require.True(t, tok.VerifySessionDataSignature(data, sigV2.GetSign()))
|
|
|
|
require.False(t, tok.VerifySessionDataSignature(append(data, 1), sigV2.GetSign()))
|
|
|
|
require.False(t, tok.VerifySessionDataSignature(data, append(sigV2.GetSign(), 1)))
|
|
|
|
}
|