[#150] tests: add container type in tests

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
enable-notary-in-public-chains
Evgenii Stratonikov 2021-10-11 10:39:49 +03:00 committed by Alex Vanin
parent a7601334f7
commit 16f3281198
2 changed files with 36 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package tests
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"math/rand"
"strings" "strings"
"testing" "testing"
@ -264,3 +265,9 @@ func TestInvoke(bc *core.Blockchain, tx *transaction.Transaction) (*vm.VM, error
err = v.Run() err = v.Run()
return v, err return v, err
} }
func randomBytes(n int) []byte {
a := make([]byte, n)
rand.Read(a)
return a
}

View File

@ -49,19 +49,33 @@ func setContainerOwner(c []byte, owner *wallet.Account) {
copy(c[7:], owner.Contract.ScriptHash().BytesBE()) copy(c[7:], owner.Contract.ScriptHash().BytesBE())
} }
type testContainer struct {
id [32]byte
value, sig, pub, token []byte
}
func dummyContainer(owner *wallet.Account) testContainer {
value := randomBytes(100)
value[1] = 0 // zero offset
setContainerOwner(value, owner)
return testContainer{
id: sha256.Sum256(value),
value: value,
sig: randomBytes(64),
pub: randomBytes(33),
token: randomBytes(42),
}
}
func TestContainerPut(t *testing.T) { func TestContainerPut(t *testing.T) {
bc := NewChain(t) bc := NewChain(t)
h, balanceHash := prepareContainerContract(t, bc) h, balanceHash := prepareContainerContract(t, bc)
acc := NewAccount(t, bc) acc := NewAccount(t, bc)
dummySig := make([]byte, 64) c := dummyContainer(acc)
dummyPub := make([]byte, 33)
dummyToken := make([]byte, 100)
container := make([]byte, 100)
setContainerOwner(container, acc)
containerID := sha256.Sum256(container)
putArgs := []interface{}{container, dummySig, dummyPub, dummyToken} putArgs := []interface{}{c.value, c.sig, c.pub, c.token}
tx := PrepareInvoke(t, bc, CommitteeAcc, h, "put", putArgs...) tx := PrepareInvoke(t, bc, CommitteeAcc, h, "put", putArgs...)
AddBlock(t, bc, tx) AddBlock(t, bc, tx)
CheckFault(t, bc, tx.Hash(), "insufficient balance to create container") CheckFault(t, bc, tx.Hash(), "insufficient balance to create container")
@ -80,24 +94,24 @@ func TestContainerPut(t *testing.T) {
balanceMint(t, bc, acc, balanceHash, containerFee*1, []byte{}) balanceMint(t, bc, acc, balanceHash, containerFee*1, []byte{})
putArgs := []interface{}{container, dummySig, dummyPub, dummyToken, "mycnt", ""} putArgs := []interface{}{c.value, c.sig, c.pub, c.token, "mycnt", ""}
tx = PrepareInvoke(t, bc, CommitteeAcc, h, "putNamed", putArgs...) tx = PrepareInvoke(t, bc, CommitteeAcc, h, "putNamed", putArgs...)
AddBlockCheckHalt(t, bc, tx) AddBlockCheckHalt(t, bc, tx)
tx = PrepareInvoke(t, bc, acc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT)) tx = PrepareInvoke(t, bc, acc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT))
CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{ CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray([]byte(base58.Encode(containerID[:]))), stackitem.NewByteArray([]byte(base58.Encode(c.id[:]))),
})) }))
tx = PrepareInvoke(t, bc, CommitteeAcc, h, "delete", containerID[:], dummySig, dummyToken) tx = PrepareInvoke(t, bc, CommitteeAcc, h, "delete", c.id[:], c.sig, c.token)
AddBlockCheckHalt(t, bc, tx) AddBlockCheckHalt(t, bc, tx)
tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT)) tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT))
CheckTestInvoke(t, bc, tx, stackitem.Null{}) CheckTestInvoke(t, bc, tx, stackitem.Null{})
t.Run("register in advance", func(t *testing.T) { t.Run("register in advance", func(t *testing.T) {
container[len(container)-1] = 10 c.value[len(c.value)-1] = 10
containerID = sha256.Sum256(container) c.id = sha256.Sum256(c.value)
tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "register", tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "register",
"second.neofs", CommitteeAcc.Contract.ScriptHash(), "second.neofs", CommitteeAcc.Contract.ScriptHash(),
@ -106,13 +120,13 @@ func TestContainerPut(t *testing.T) {
balanceMint(t, bc, acc, balanceHash, containerFee*1, []byte{}) balanceMint(t, bc, acc, balanceHash, containerFee*1, []byte{})
putArgs := []interface{}{container, dummySig, dummyPub, dummyToken, "second", "neofs"} putArgs := []interface{}{c.value, c.sig, c.pub, c.token, "second", "neofs"}
tx = PrepareInvoke(t, bc, CommitteeAcc, h, "putNamed", putArgs...) tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h, "putNamed", putArgs...)
AddBlockCheckHalt(t, bc, tx) AddBlockCheckHalt(t, bc, tx)
tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "second.neofs", int64(nns.TXT)) tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "second.neofs", int64(nns.TXT))
CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{ CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray([]byte(base58.Encode(containerID[:]))), stackitem.NewByteArray([]byte(base58.Encode(c.id[:]))),
})) }))
}) })
}) })