2021-10-25 12:10:13 +00:00
|
|
|
package util_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-10-25 12:10:13 +00:00
|
|
|
sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2022-03-18 08:46:07 +00:00
|
|
|
tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage/temporary"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2022-05-17 13:59:46 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2021-10-25 12:10:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewKeyStorage(t *testing.T) {
|
|
|
|
nodeKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
tokenStor := tokenStorage.NewTokenStore()
|
2021-10-25 12:19:50 +00:00
|
|
|
stor := util.NewKeyStorage(&nodeKey.PrivateKey, tokenStor, mockedNetworkState{42})
|
2021-10-25 12:10:13 +00:00
|
|
|
|
|
|
|
t.Run("node key", func(t *testing.T) {
|
|
|
|
key, err := stor.GetKey(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodeKey.PrivateKey, *key)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown token", func(t *testing.T) {
|
|
|
|
tok := generateToken(t)
|
|
|
|
_, err = stor.GetKey(tok)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("known token", func(t *testing.T) {
|
|
|
|
tok := createToken(t, tokenStor, 100)
|
|
|
|
pubKey, err := keys.NewPublicKeyFromBytes(tok.SessionKey(), elliptic.P256())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
key, err := stor.GetKey(tok)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, pubKey.X, key.PublicKey.X)
|
|
|
|
require.Equal(t, pubKey.Y, key.PublicKey.Y)
|
|
|
|
})
|
2021-10-25 12:19:50 +00:00
|
|
|
|
|
|
|
t.Run("expired token", func(t *testing.T) {
|
|
|
|
tok := createToken(t, tokenStor, 30)
|
|
|
|
_, err := stor.GetKey(tok)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2021-10-25 12:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateToken(t *testing.T) *session.Token {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubKey := key.PublicKey().Bytes()
|
|
|
|
id, err := uuid.New().MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tok := session.NewToken()
|
|
|
|
tok.SetSessionKey(pubKey)
|
|
|
|
tok.SetID(id)
|
2022-05-17 13:59:46 +00:00
|
|
|
tok.SetOwnerID(usertest.ID())
|
2021-10-25 12:10:13 +00:00
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
|
|
|
|
func createToken(t *testing.T, store *tokenStorage.TokenStore, exp uint64) *session.Token {
|
2022-05-17 13:59:46 +00:00
|
|
|
owner := usertest.ID()
|
|
|
|
|
|
|
|
var ownerV2 refs.OwnerID
|
|
|
|
owner.WriteToV2(&ownerV2)
|
|
|
|
|
2021-10-25 12:10:13 +00:00
|
|
|
req := new(sessionV2.CreateRequestBody)
|
2022-05-17 13:59:46 +00:00
|
|
|
req.SetOwnerID(&ownerV2)
|
2021-10-25 12:10:13 +00:00
|
|
|
req.SetExpiration(exp)
|
|
|
|
|
|
|
|
resp, err := store.Create(context.Background(), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tok := session.NewToken()
|
|
|
|
tok.SetSessionKey(resp.GetSessionKey())
|
|
|
|
tok.SetID(resp.GetID())
|
2022-05-17 13:59:46 +00:00
|
|
|
tok.SetOwnerID(owner)
|
2021-10-25 12:10:13 +00:00
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|
2021-10-25 12:19:50 +00:00
|
|
|
|
|
|
|
type mockedNetworkState struct {
|
|
|
|
value uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockedNetworkState) CurrentEpoch() uint64 {
|
|
|
|
return m.value
|
|
|
|
}
|