2021-12-16 15:26:13 +00:00
|
|
|
package container_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
containerCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
containerSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/container"
|
|
|
|
containerSvcMorph "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/container/morph"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
containertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/test"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
sessiontest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session/test"
|
2022-07-22 14:04:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-12-16 15:26:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
type mock struct {
|
|
|
|
containerSvcMorph.Reader
|
|
|
|
}
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (m mock) Put(_ containerCore.Container) (*cid.ID, error) {
|
2021-12-16 15:26:13 +00:00
|
|
|
return new(cid.ID), nil
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (m mock) Delete(_ containerCore.RemovalWitness) error {
|
2021-12-16 15:26:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidToken(t *testing.T) {
|
|
|
|
m := mock{}
|
|
|
|
e := containerSvcMorph.NewExecutor(m, m)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
|
2022-07-22 14:04:37 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sign := func(reqBody interface {
|
|
|
|
StableMarshal([]byte) []byte
|
|
|
|
SetSignature(signature *refs.Signature)
|
2023-10-31 11:56:55 +00:00
|
|
|
},
|
|
|
|
) {
|
2022-12-23 17:35:35 +00:00
|
|
|
signer := frostfsecdsa.Signer(priv.PrivateKey)
|
|
|
|
var sig frostfscrypto.Signature
|
2022-07-22 14:04:37 +00:00
|
|
|
require.NoError(t, sig.Calculate(signer, reqBody.StableMarshal(nil)))
|
|
|
|
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sig.WriteToV2(&sigV2)
|
|
|
|
reqBody.SetSignature(&sigV2)
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var tokV2 session.Token
|
|
|
|
sessiontest.ContainerSigned().WriteToV2(&tokV2)
|
|
|
|
|
2021-12-16 15:26:13 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2022-03-30 14:19:14 +00:00
|
|
|
op func(e containerSvc.ServiceExecutor, tokV2 *session.Token) error
|
2021-12-16 15:26:13 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "put",
|
2022-03-30 14:19:14 +00:00
|
|
|
op: func(e containerSvc.ServiceExecutor, tokV2 *session.Token) (err error) {
|
2022-05-16 13:15:31 +00:00
|
|
|
var reqBody container.PutRequestBody
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
cnr := containertest.Container()
|
|
|
|
|
|
|
|
var cnrV2 container.Container
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
|
|
|
|
reqBody.SetContainer(&cnrV2)
|
2022-07-22 14:04:37 +00:00
|
|
|
sign(&reqBody)
|
2022-06-28 07:01:05 +00:00
|
|
|
|
2022-03-30 14:19:14 +00:00
|
|
|
_, err = e.Put(context.TODO(), tokV2, &reqBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete",
|
2022-03-30 14:19:14 +00:00
|
|
|
op: func(e containerSvc.ServiceExecutor, tokV2 *session.Token) (err error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
var reqBody container.DeleteRequestBody
|
|
|
|
reqBody.SetContainerID(&cnrV2)
|
|
|
|
|
2022-03-30 14:19:14 +00:00
|
|
|
_, err = e.Delete(context.TODO(), tokV2, &reqBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-03-30 14:19:14 +00:00
|
|
|
tok := generateToken(new(session.ObjectSessionContext))
|
|
|
|
require.Error(t, test.op(e, tok))
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2022-03-30 14:19:14 +00:00
|
|
|
require.NoError(t, test.op(e, &tokV2))
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2022-03-30 14:19:14 +00:00
|
|
|
require.NoError(t, test.op(e, nil))
|
2021-12-16 15:26:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func generateToken(ctx session.TokenContext) *session.Token {
|
|
|
|
body := new(session.TokenBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
body.SetContext(ctx)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
tok := new(session.Token)
|
2021-12-16 15:26:13 +00:00
|
|
|
tok.SetBody(body)
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|