frostfs-sdk-go/eacl/target_test.go
Pavel Pogodaev 6ce73790ea
All checks were successful
DCO / DCO (pull_request) Successful in 38s
Tests and linters / Tests (pull_request) Successful in 1m13s
Tests and linters / Lint (pull_request) Successful in 2m36s
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
2024-10-22 14:05:12 +00:00

84 lines
1.8 KiB
Go

package eacl
import (
"crypto/ecdsa"
"testing"
v2acl "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/acl"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/stretchr/testify/require"
)
func TestTarget(t *testing.T) {
pubs := []*ecdsa.PublicKey{
randomPublicKey(t),
randomPublicKey(t),
}
target := NewTarget()
target.SetRole(RoleSystem)
SetTargetECDSAKeys(target, pubs...)
v2 := target.ToV2()
require.NotNil(t, v2)
require.Equal(t, v2acl.RoleSystem, v2.GetRole())
require.Len(t, v2.GetKeys(), len(pubs))
for i, key := range v2.GetKeys() {
require.Equal(t, key, (*keys.PublicKey)(pubs[i]).Bytes())
}
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)
SetTargetECDSAKeys(tar, randomPublicKey(t))
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)
})
}
func TestTarget_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Target
require.Nil(t, x.ToV2())
})
t.Run("default values", func(t *testing.T) {
target := NewTarget()
// check initial values
require.Equal(t, RoleUnknown, target.Role())
require.Nil(t, target.BinaryKeys())
// convert to v2 message
targetV2 := target.ToV2()
require.Equal(t, v2acl.RoleUnknown, targetV2.GetRole())
require.Nil(t, targetV2.GetKeys())
})
}