package tests import ( "path" "testing" "git.frostfs.info/TrueCloudLab/frostfs-contract/policy" "github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" ) const policyPath = "../policy" func deployPolicyContract(t *testing.T, e *neotest.Executor) util.Uint160 { cfgPath := path.Join(policyPath, "config.yml") c := neotest.CompileFile(t, e.CommitteeHash, policyPath, cfgPath) e.DeployContract(t, c, nil) return c.Hash } func newPolicyInvoker(t *testing.T) *neotest.ContractInvoker { e := newExecutor(t) h := deployPolicyContract(t, e) return e.CommitteeInvoker(h) } func TestPolicy(t *testing.T) { e := newPolicyInvoker(t) // Policies are opaque to the contract and are just raw bytes to store. p1 := []byte("chain1") p2 := []byte("chain2") p3 := []byte("chain3") p33 := []byte("chain33") e.Invoke(t, stackitem.Null{}, "addChain", policy.Namespace, "mynamespace", "ingress:123", p1) checkChains(t, e, "mynamespace", "", "ingress", [][]byte{p1}) checkChains(t, e, "mynamespace", "", "all", nil) e.Invoke(t, stackitem.Null{}, "addChain", policy.Container, "cnr1", "ingress:myrule2", p2) checkChains(t, e, "mynamespace", "", "ingress", [][]byte{p1}) // Only namespace chains. checkChains(t, e, "mynamespace", "cnr1", "ingress", [][]byte{p1, p2}) checkChains(t, e, "mynamespace", "cnr1", "all", nil) // No chains attached to 'all'. checkChains(t, e, "mynamespace", "cnr2", "ingress", [][]byte{p1}) // Only namespace, no chains for the container. e.Invoke(t, stackitem.Null{}, "addChain", policy.Container, "cnr1", "ingress:myrule3", p3) checkChains(t, e, "mynamespace", "cnr1", "ingress", [][]byte{p1, p2, p3}) e.Invoke(t, stackitem.Null{}, "addChain", policy.Container, "cnr1", "ingress:myrule3", p33) checkChains(t, e, "mynamespace", "cnr1", "ingress", [][]byte{p1, p2, p33}) // Override chain. t.Run("removal", func(t *testing.T) { t.Run("wrong name", func(t *testing.T) { e.Invoke(t, stackitem.Null{}, "removeChain", policy.Namespace, "mynamespace", "ingress") checkChains(t, e, "mynamespace", "", "ingress", [][]byte{p1}) }) e.Invoke(t, stackitem.Null{}, "removeChain", policy.Namespace, "mynamespace", "ingress:123") checkChains(t, e, "mynamespace", "", "ingress", nil) checkChains(t, e, "mynamespace", "cnr1", "ingress", [][]byte{p2, p33}) // Container chains still exist. // Remove by prefix. e.Invoke(t, stackitem.Null{}, "removeChainsByPrefix", policy.Container, "cnr1", "ingress") checkChains(t, e, "mynamespace", "cnr1", "ingress", nil) }) } func checkChains(t *testing.T, e *neotest.ContractInvoker, namespace, container, name string, expected [][]byte) { s, err := e.TestInvoke(t, "listChains", namespace, container, name) require.NoError(t, err) require.Equal(t, 1, s.Len()) if len(expected) == 0 { _, ok := s.Pop().Item().(stackitem.Null) require.True(t, ok) return } var actual [][]byte arr := s.Pop().Array() for i := range arr { bs, err := arr[i].TryBytes() require.NoError(t, err) actual = append(actual, bs) } require.ElementsMatch(t, expected, actual) }