2021-11-18 11:31:58 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2021-11-18 12:21:14 +00:00
|
|
|
"encoding/binary"
|
2021-11-18 11:31:58 +00:00
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/subnet"
|
2021-11-18 11:31:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-11-18 12:21:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-18 11:31:58 +00:00
|
|
|
)
|
|
|
|
|
2021-11-24 14:22:27 +00:00
|
|
|
const subnetPath = "../subnet"
|
2021-11-18 11:31:58 +00:00
|
|
|
|
|
|
|
func deploySubnetContract(t *testing.T, e *neotest.Executor) util.Uint160 {
|
|
|
|
c := neotest.CompileFile(t, e.CommitteeHash, subnetPath, path.Join(subnetPath, "config.yml"))
|
2021-11-18 12:21:14 +00:00
|
|
|
args := []interface{}{false}
|
2021-11-18 11:31:58 +00:00
|
|
|
e.DeployContract(t, c, args)
|
|
|
|
return c.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubnetInvoker(t *testing.T) *neotest.ContractInvoker {
|
|
|
|
e := newExecutor(t)
|
|
|
|
h := deploySubnetContract(t, e)
|
|
|
|
return e.CommitteeInvoker(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubnet_Version(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
e.Invoke(t, common.Version, "version")
|
|
|
|
}
|
2021-11-18 12:21:14 +00:00
|
|
|
|
|
|
|
func TestSubnet_Put(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
acc := e.NewAccount(t)
|
|
|
|
pub, ok := vm.ParseSignatureContract(acc.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-11-24 10:03:02 +00:00
|
|
|
id := make([]byte, 5)
|
2021-11-18 12:21:14 +00:00
|
|
|
binary.LittleEndian.PutUint32(id, 123)
|
|
|
|
info := randomBytes(10)
|
|
|
|
|
2021-11-29 12:21:40 +00:00
|
|
|
e.InvokeFail(t, common.ErrWitnessFailed, "put", id, pub, info)
|
2021-11-18 12:21:14 +00:00
|
|
|
|
|
|
|
cAcc := e.WithSigners(acc)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "put", id, pub, info)
|
2021-11-18 12:21:14 +00:00
|
|
|
|
|
|
|
cBoth := e.WithSigners(e.Committee, acc)
|
|
|
|
cBoth.InvokeFail(t, subnet.ErrInvalidSubnetID, "put", []byte{1, 2, 3}, pub, info)
|
|
|
|
cBoth.InvokeFail(t, subnet.ErrInvalidOwner, "put", id, pub[10:], info)
|
|
|
|
cBoth.Invoke(t, stackitem.Null{}, "put", id, pub, info)
|
|
|
|
cAcc.Invoke(t, stackitem.NewBuffer(info), "get", id)
|
|
|
|
cBoth.InvokeFail(t, subnet.ErrAlreadyExists, "put", id, pub, info)
|
|
|
|
}
|
2021-11-18 12:24:32 +00:00
|
|
|
|
|
|
|
func TestSubnet_Delete(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
id, owner := createSubnet(t, e)
|
2021-11-18 12:24:32 +00:00
|
|
|
|
2021-11-29 12:21:40 +00:00
|
|
|
e.InvokeFail(t, common.ErrWitnessFailed, "delete", id)
|
2021-11-18 12:24:32 +00:00
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
cAcc := e.WithSigners(owner)
|
2021-11-24 10:03:02 +00:00
|
|
|
cAcc.InvokeFail(t, subnet.ErrInvalidSubnetID, "delete", []byte{1, 1, 1, 1})
|
2021-11-24 18:09:13 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "delete", []byte{1, 1, 1, 1, 1})
|
2021-11-18 12:24:32 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "delete", id)
|
2021-11-23 15:36:45 +00:00
|
|
|
cAcc.InvokeFail(t, subnet.ErrNotExist, "get", id)
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
2021-11-21 19:55:39 +00:00
|
|
|
|
|
|
|
func TestSubnet_AddNodeAdmin(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
2021-11-21 19:55:39 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
const method = "addNodeAdmin"
|
2021-11-21 19:55:39 +00:00
|
|
|
|
2021-11-24 14:22:27 +00:00
|
|
|
e.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, admPub)
|
|
|
|
e.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, admPub[1:])
|
|
|
|
e.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, admPub)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
|
|
|
cAdm := e.WithSigners(adm)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAdm.InvokeFail(t, common.ErrOwnerWitnessFailed, method, id, admPub)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
|
|
|
cOwner := e.WithSigners(owner)
|
|
|
|
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
|
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubnet_RemoveNodeAdmin(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
2021-11-21 19:55:39 +00:00
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
const method = "removeNodeAdmin"
|
2021-11-21 19:55:39 +00:00
|
|
|
|
2021-11-24 14:22:27 +00:00
|
|
|
e.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, admPub)
|
|
|
|
e.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, admPub[1:])
|
|
|
|
e.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, admPub)
|
2021-11-21 19:55:39 +00:00
|
|
|
|
|
|
|
cAdm := e.WithSigners(adm)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAdm.InvokeFail(t, common.ErrOwnerWitnessFailed, method, id, admPub)
|
2021-11-21 19:55:39 +00:00
|
|
|
|
|
|
|
cOwner := e.WithSigners(owner)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
|
2021-11-22 14:20:00 +00:00
|
|
|
cOwner.Invoke(t, stackitem.Null{}, "addNodeAdmin", id, admPub)
|
2021-11-21 19:55:39 +00:00
|
|
|
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
2021-11-21 19:55:39 +00:00
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
func TestSubnet_AddNode(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
node := e.NewAccount(t)
|
|
|
|
nodePub, ok := vm.ParseSignatureContract(node.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "addNode"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidNode, method, id, nodePub[1:])
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
|
2021-11-22 16:55:07 +00:00
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:35:07 +00:00
|
|
|
func TestSubnet_RemoveNode(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
node := e.NewAccount(t)
|
|
|
|
nodePub, ok := vm.ParseSignatureContract(node.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "removeNode"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidNode, method, id, nodePub[1:])
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
|
2021-11-22 18:35:07 +00:00
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub)
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
|
|
|
|
|
|
|
|
cAdm := cOwn.WithSigners(adm)
|
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addNodeAdmin", id, admPub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cAdm.Invoke(t, stackitem.Null{}, method, id, nodePub)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:59:49 +00:00
|
|
|
func TestSubnet_NodeAllowed(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
node := e.NewAccount(t)
|
|
|
|
nodePub, ok := vm.ParseSignatureContract(node.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "nodeAllowed"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidNode, method, id, nodePub[1:])
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
|
2021-11-22 18:59:49 +00:00
|
|
|
cOwn.Invoke(t, stackitem.NewBool(false), method, id, nodePub)
|
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub)
|
|
|
|
cOwn.Invoke(t, stackitem.NewBool(true), method, id, nodePub)
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
func TestSubnet_AddClientAdmin(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "addClientAdmin"
|
|
|
|
|
2021-11-24 10:33:24 +00:00
|
|
|
groupId := randomBytes(5)
|
2021-11-23 09:54:32 +00:00
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, admPub)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
|
2021-11-23 09:54:32 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
|
2021-11-23 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:20:07 +00:00
|
|
|
func TestSubnet_RemoveClientAdmin(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "removeClientAdmin"
|
|
|
|
|
2021-11-24 10:33:24 +00:00
|
|
|
groupId := randomBytes(5)
|
2021-11-23 10:20:07 +00:00
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, admPub)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
|
2021-11-23 10:20:07 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
func TestSubnet_AddUser(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
user := randomBytes(27)
|
|
|
|
|
2021-11-24 10:33:24 +00:00
|
|
|
groupId := randomBytes(5)
|
2021-11-23 10:56:56 +00:00
|
|
|
|
|
|
|
const method = "addUser"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, user)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, user)
|
2021-11-23 10:56:56 +00:00
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
|
|
|
|
|
|
|
|
cAdm := e.WithSigners(adm)
|
|
|
|
cAdm.Invoke(t, stackitem.Null{}, method, id, groupId, user)
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
func TestSubnet_RemoveUser(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
2021-11-24 10:33:24 +00:00
|
|
|
groupId := randomBytes(5)
|
2021-11-23 11:15:16 +00:00
|
|
|
user := randomBytes(27)
|
|
|
|
|
|
|
|
adm := e.NewAccount(t)
|
|
|
|
admPub, ok := vm.ParseSignatureContract(adm.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
const method = "removeUser"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, user)
|
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, user)
|
2021-11-23 11:15:16 +00:00
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
|
2021-11-23 11:15:16 +00:00
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
|
|
|
|
|
|
|
|
cAdm := cOwn.WithSigners(adm)
|
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
|
2021-11-24 18:09:13 +00:00
|
|
|
cAdm.Invoke(t, stackitem.Null{}, method, id, groupId, user)
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 13:19:58 +00:00
|
|
|
func TestSubnet_UserAllowed(t *testing.T) {
|
|
|
|
e := newSubnetInvoker(t)
|
|
|
|
|
|
|
|
id, owner := createSubnet(t, e)
|
|
|
|
|
2021-11-24 10:03:02 +00:00
|
|
|
groupId := randomBytes(5)
|
2021-11-23 13:19:58 +00:00
|
|
|
|
|
|
|
user := randomBytes(27)
|
|
|
|
|
|
|
|
const method = "userAllowed"
|
|
|
|
|
|
|
|
cOwn := e.WithSigners(owner)
|
2021-11-24 14:22:27 +00:00
|
|
|
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, user)
|
2021-11-23 13:19:58 +00:00
|
|
|
|
|
|
|
cOwn.Invoke(t, stackitem.NewBool(false), method, id, user)
|
|
|
|
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
|
|
|
|
cOwn.Invoke(t, stackitem.NewBool(true), method, id, user)
|
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
func createSubnet(t *testing.T, e *neotest.ContractInvoker) (id []byte, owner neotest.Signer) {
|
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
pub []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
owner = e.NewAccount(t)
|
|
|
|
pub, ok = vm.ParseSignatureContract(owner.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-11-24 10:03:02 +00:00
|
|
|
id = make([]byte, 5)
|
2021-11-22 14:20:00 +00:00
|
|
|
binary.LittleEndian.PutUint32(id, 123)
|
|
|
|
info := randomBytes(10)
|
|
|
|
|
|
|
|
cBoth := e.WithSigners(e.Committee, owner)
|
|
|
|
cBoth.Invoke(t, stackitem.Null{}, "put", id, pub, info)
|
|
|
|
|
|
|
|
return
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|