2020-09-10 14:51:58 +00:00
|
|
|
package eacl_test
|
|
|
|
|
|
|
|
import (
|
2020-09-14 15:21:11 +00:00
|
|
|
"crypto/sha256"
|
2020-09-10 14:51:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-14 15:21:11 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-09-10 14:51:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
2021-06-08 08:26:23 +00:00
|
|
|
eacltest "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl/test"
|
2021-05-31 12:09:04 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
|
2021-05-24 17:35:56 +00:00
|
|
|
sessiontest "github.com/nspcc-dev/neofs-api-go/pkg/session/test"
|
2020-09-14 15:21:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-10 14:51:58 +00:00
|
|
|
)
|
|
|
|
|
2020-09-14 15:21:11 +00:00
|
|
|
func TestTable(t *testing.T) {
|
|
|
|
var (
|
2021-05-31 12:09:04 +00:00
|
|
|
v pkg.Version
|
2020-09-14 15:21:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
sha := sha256.Sum256([]byte("container id"))
|
2021-05-31 12:09:04 +00:00
|
|
|
id := cidtest.GenerateWithChecksum(sha)
|
2020-09-14 15:21:11 +00:00
|
|
|
|
|
|
|
v.SetMajor(3)
|
|
|
|
v.SetMinor(2)
|
|
|
|
|
|
|
|
table := eacl.NewTable()
|
|
|
|
table.SetVersion(v)
|
2021-05-31 12:09:04 +00:00
|
|
|
table.SetCID(id)
|
2020-09-14 15:21:11 +00:00
|
|
|
table.AddRecord(eacl.CreateRecord(eacl.ActionAllow, eacl.OperationPut))
|
|
|
|
|
|
|
|
v2 := table.ToV2()
|
|
|
|
require.NotNil(t, v2)
|
|
|
|
require.Equal(t, uint32(3), v2.GetVersion().GetMajor())
|
|
|
|
require.Equal(t, uint32(2), v2.GetVersion().GetMinor())
|
|
|
|
require.Equal(t, sha[:], v2.GetContainerID().GetValue())
|
|
|
|
require.Len(t, v2.GetRecords(), 1)
|
|
|
|
|
|
|
|
newTable := eacl.NewTableFromV2(v2)
|
|
|
|
require.Equal(t, table, newTable)
|
|
|
|
|
|
|
|
t.Run("new from nil v2 table", func(t *testing.T) {
|
|
|
|
require.Equal(t, new(eacl.Table), eacl.NewTableFromV2(nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create table", func(t *testing.T) {
|
2021-05-31 12:09:04 +00:00
|
|
|
id := cidtest.Generate()
|
2020-09-14 15:21:11 +00:00
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
table := eacl.CreateTable(*id)
|
|
|
|
require.Equal(t, id, table.CID())
|
2020-09-14 15:21:11 +00:00
|
|
|
require.Equal(t, *pkg.SDKVersion(), table.Version())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTable_AddRecord(t *testing.T) {
|
2020-11-16 14:24:06 +00:00
|
|
|
records := []*eacl.Record{
|
|
|
|
eacl.CreateRecord(eacl.ActionDeny, eacl.OperationDelete),
|
|
|
|
eacl.CreateRecord(eacl.ActionAllow, eacl.OperationPut),
|
2020-09-14 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table := eacl.NewTable()
|
|
|
|
for _, record := range records {
|
2020-11-16 14:24:06 +00:00
|
|
|
table.AddRecord(record)
|
2020-09-14 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, records, table.Records())
|
|
|
|
}
|
2020-11-13 11:55:45 +00:00
|
|
|
|
|
|
|
func TestRecordEncoding(t *testing.T) {
|
2021-06-08 08:26:23 +00:00
|
|
|
tab := eacltest.Table()
|
2020-11-13 11:55:45 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := tab.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tab2 := eacl.NewTable()
|
|
|
|
require.NoError(t, tab2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, tab, tab2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := tab.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r2 := eacl.NewTable()
|
|
|
|
require.NoError(t, r2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, tab, r2)
|
|
|
|
})
|
|
|
|
}
|
2021-05-24 17:35:56 +00:00
|
|
|
|
|
|
|
func TestTable_SessionToken(t *testing.T) {
|
|
|
|
tok := sessiontest.Generate()
|
|
|
|
|
|
|
|
table := eacl.NewTable()
|
|
|
|
table.SetSessionToken(tok)
|
|
|
|
|
|
|
|
require.Equal(t, tok, table.SessionToken())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTable_Signature(t *testing.T) {
|
|
|
|
sig := pkg.NewSignature()
|
|
|
|
sig.SetKey([]byte{1, 2, 3})
|
|
|
|
sig.SetSign([]byte{4, 5, 6})
|
|
|
|
|
|
|
|
table := eacl.NewTable()
|
|
|
|
table.SetSignature(sig)
|
|
|
|
|
|
|
|
require.Equal(t, sig, table.Signature())
|
|
|
|
}
|
2021-05-27 11:43:21 +00:00
|
|
|
|
|
|
|
func TestTable_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *eacl.Table
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
2021-05-27 11:48:20 +00:00
|
|
|
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
|
|
|
table := eacl.NewTable()
|
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Equal(t, *pkg.SDKVersion(), table.Version())
|
|
|
|
require.Nil(t, table.Records())
|
|
|
|
require.Nil(t, table.CID())
|
|
|
|
require.Nil(t, table.SessionToken())
|
|
|
|
require.Nil(t, table.Signature())
|
|
|
|
|
|
|
|
// convert to v2 message
|
|
|
|
tableV2 := table.ToV2()
|
|
|
|
|
|
|
|
require.Equal(t, pkg.SDKVersion().ToV2(), tableV2.GetVersion())
|
|
|
|
require.Nil(t, tableV2.GetRecords())
|
|
|
|
require.Nil(t, tableV2.GetContainerID())
|
|
|
|
})
|
2021-05-27 11:43:21 +00:00
|
|
|
}
|