frostfs-api-go-pogpp/pkg/acl/eacl/target_test.go
Leonard Lyubich 9ddc4c1f48 [#168] acl: Implement binary/JSON encoders/decoders on Target
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-11-13 18:07:26 +03:00

64 lines
1.3 KiB
Go

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) {
keys := []ecdsa.PublicKey{
test.DecodeKey(1).PublicKey,
test.DecodeKey(2).PublicKey,
}
target := &Target{
role: RoleSystem,
keys: keys,
}
v2 := target.ToV2()
require.NotNil(t, v2)
require.Equal(t, v2acl.RoleSystem, v2.GetRole())
require.Len(t, v2.GetKeys(), len(keys))
for i, key := range v2.GetKeys() {
require.Equal(t, key, crypto.MarshalPublicKey(&keys[i]))
}
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))
})
}
func TestTargetEncoding(t *testing.T) {
tar := NewTarget()
tar.SetRole(RoleSystem)
tar.SetKeys(test.DecodeKey(-1).PublicKey)
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)
})
}