2021-05-25 19:59:21 +00:00
|
|
|
package accessbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2021-05-26 16:48:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/hcs"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
func Test_tokens_encode_decode(t *testing.T) {
|
|
|
|
var (
|
|
|
|
tkn = token.NewBearerToken()
|
|
|
|
tkn2 = token.NewBearerToken()
|
|
|
|
)
|
2021-05-25 19:59:21 +00:00
|
|
|
sec, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tkn.SetEACLTable(eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.SignToken(sec))
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
data, err := encodeToken(tkn, cred.PrivateKey(), cred.PublicKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
err = decodeToken(data, tkn2, cred.PrivateKey(), cred.PublicKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
require.Equal(t, tkn, tkn2)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
func Test_bearer_token_in_access_box(t *testing.T) {
|
|
|
|
var (
|
|
|
|
box, box2 AccessBox
|
|
|
|
tkn = token.NewBearerToken()
|
|
|
|
)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
sec, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tkn.SetEACLTable(eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.SignToken(sec))
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
box.SetOwnerPublicKey(cred.PublicKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
err = box.AddBearerToken(tkn, cred.PrivateKey(), cred.PublicKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
data, err := box.Marshal()
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
err = box2.Unmarshal(data)
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
tkn2, err := box2.GetBearerToken(cred.PrivateKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
require.Equal(t, tkn, tkn2)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
func Test_accessbox_multiple_keys(t *testing.T) {
|
|
|
|
var (
|
|
|
|
box AccessBox
|
|
|
|
tkn = token.NewBearerToken()
|
|
|
|
)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
sec, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tkn.SetEACLTable(eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.SignToken(sec))
|
|
|
|
|
|
|
|
count := 10
|
|
|
|
pubs := make([]hcs.PublicKey, 0, count)
|
|
|
|
keys := make([]hcs.PrivateKey, 0, count)
|
|
|
|
{ // generate keys
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
cred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubs = append(pubs, cred.PublicKey())
|
|
|
|
keys = append(keys, cred.PrivateKey())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
box.SetOwnerPublicKey(cred.PublicKey())
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
err = box.AddBearerToken(tkn, cred.PrivateKey(), pubs...)
|
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
for i, k := range keys {
|
|
|
|
tkn2, err := box.GetBearerToken(k)
|
|
|
|
require.NoError(t, err, "key #%d: %s failed", i, k)
|
|
|
|
require.Equal(t, tkn2, tkn)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_unknown_key(t *testing.T) {
|
|
|
|
var (
|
|
|
|
box AccessBox
|
|
|
|
tkn = token.NewBearerToken()
|
|
|
|
)
|
|
|
|
|
|
|
|
sec, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
cred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wrongCred, err := hcs.Generate(rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tkn.SetEACLTable(eacl.NewTable())
|
|
|
|
require.NoError(t, tkn.SignToken(sec))
|
|
|
|
|
|
|
|
box.SetOwnerPublicKey(cred.PublicKey())
|
|
|
|
|
|
|
|
err = box.AddBearerToken(tkn, cred.PrivateKey(), cred.PublicKey())
|
|
|
|
require.NoError(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
_, err = box.GetBearerToken(wrongCred.PrivateKey())
|
|
|
|
require.Error(t, err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|