2020-09-14 15:21:11 +00:00
|
|
|
package eacl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
"github.com/nspcc-dev/neofs-crypto/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTarget(t *testing.T) {
|
2020-12-01 06:39:35 +00:00
|
|
|
keys := []*ecdsa.PublicKey{
|
|
|
|
&test.DecodeKey(1).PublicKey,
|
|
|
|
&test.DecodeKey(2).PublicKey,
|
2020-09-14 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:39:35 +00:00
|
|
|
target := NewTarget()
|
|
|
|
target.SetRole(RoleSystem)
|
|
|
|
SetTargetECDSAKeys(target, keys...)
|
2020-09-14 15:21:11 +00:00
|
|
|
|
|
|
|
v2 := target.ToV2()
|
|
|
|
require.NotNil(t, v2)
|
|
|
|
require.Equal(t, v2acl.RoleSystem, v2.GetRole())
|
2020-10-19 12:39:13 +00:00
|
|
|
require.Len(t, v2.GetKeys(), len(keys))
|
|
|
|
for i, key := range v2.GetKeys() {
|
2020-12-01 06:39:35 +00:00
|
|
|
require.Equal(t, key, crypto.MarshalPublicKey(keys[i]))
|
2020-09-14 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newTarget := NewTargetFromV2(v2)
|
|
|
|
require.Equal(t, target, newTarget)
|
|
|
|
|
|
|
|
t.Run("from nil v2 target", func(t *testing.T) {
|
|
|
|
require.Equal(t, new(Target), NewTargetFromV2(nil))
|
|
|
|
})
|
|
|
|
}
|
2020-11-13 11:40:57 +00:00
|
|
|
|
|
|
|
func TestTargetEncoding(t *testing.T) {
|
|
|
|
tar := NewTarget()
|
|
|
|
tar.SetRole(RoleSystem)
|
2020-12-01 06:39:35 +00:00
|
|
|
SetTargetECDSAKeys(tar, &test.DecodeKey(-1).PublicKey)
|
2020-11-13 11:40:57 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := tar.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tar2 := NewTarget()
|
|
|
|
require.NoError(t, tar2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, tar, tar2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := tar.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tar2 := NewTarget()
|
|
|
|
require.NoError(t, tar2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, tar, tar2)
|
|
|
|
})
|
|
|
|
}
|
2021-05-27 11:31:30 +00:00
|
|
|
|
|
|
|
func TestTarget_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *Target
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
|
|
|
}
|