2022-03-24 08:05:41 +00:00
|
|
|
package bearer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
|
|
|
tokentest "github.com/nspcc-dev/neofs-sdk-go/bearer/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBearerToken_Issuer(t *testing.T) {
|
|
|
|
var bearerToken bearer.Token
|
|
|
|
|
|
|
|
t.Run("non signed token", func(t *testing.T) {
|
2022-04-12 15:19:00 +00:00
|
|
|
_, ok := bearerToken.Issuer()
|
|
|
|
require.False(t, ok)
|
2022-03-24 08:05:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("signed token", func(t *testing.T) {
|
|
|
|
p, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ownerID := owner.NewIDFromPublicKey((*ecdsa.PublicKey)(p.PublicKey()))
|
|
|
|
|
|
|
|
bearerToken.SetEACLTable(*eacl.NewTable())
|
|
|
|
require.NoError(t, bearerToken.Sign(p.PrivateKey))
|
2022-04-12 15:19:00 +00:00
|
|
|
issuer, ok := bearerToken.Issuer()
|
|
|
|
require.True(t, ok)
|
2022-03-24 08:05:41 +00:00
|
|
|
require.True(t, ownerID.Equal(&issuer))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterEncoding(t *testing.T) {
|
|
|
|
f := tokentest.Token()
|
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data := f.Marshal()
|
|
|
|
|
|
|
|
var f2 bearer.Token
|
|
|
|
require.NoError(t, f2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, f, f2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := f.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var d2 bearer.Token
|
|
|
|
require.NoError(t, d2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, f, d2)
|
|
|
|
})
|
|
|
|
}
|