2019-11-18 16:31:27 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-07-15 08:49:24 +00:00
|
|
|
"encoding/base64"
|
2020-03-18 09:41:09 +00:00
|
|
|
"fmt"
|
2019-11-18 16:31:27 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-04-07 09:41:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-03-18 09:41:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
|
2020-04-08 11:08:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-11-18 16:31:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Missing tests:
|
|
|
|
* TestTxGetWitnesses
|
|
|
|
* TestAccountIsStandard
|
|
|
|
* TestCreateContractStateFromVM
|
|
|
|
* TestContractCreate
|
|
|
|
* TestContractMigrate
|
|
|
|
* TestRuntimeSerialize
|
|
|
|
* TestRuntimeDeserialize
|
|
|
|
*/
|
|
|
|
|
2020-03-03 13:13:19 +00:00
|
|
|
func TestGetTrigger(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
_, _, context, chain := createVMAndPushBlock(t)
|
2020-03-03 13:13:19 +00:00
|
|
|
defer chain.Close()
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, runtimeGetTrigger(context))
|
2020-03-03 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
func TestStorageFind(t *testing.T) {
|
2020-01-10 08:47:55 +00:00
|
|
|
v, contractState, context, chain := createVMAndContractState(t)
|
|
|
|
defer chain.Close()
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-05-29 08:02:26 +00:00
|
|
|
skeys := [][]byte{{0x01, 0x02}, {0x02, 0x01}, {0x01, 0x01}}
|
2019-12-26 11:34:38 +00:00
|
|
|
items := []*state.StorageItem{
|
|
|
|
{
|
|
|
|
Value: []byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: []byte{0x04, 0x03, 0x02, 0x01},
|
|
|
|
},
|
2020-05-29 08:02:26 +00:00
|
|
|
{
|
|
|
|
Value: []byte{0x03, 0x04, 0x05, 0x06},
|
|
|
|
},
|
2019-12-26 11:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
require.NoError(t, context.DAO.PutContractState(contractState))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
id := contractState.ID
|
2019-12-26 11:34:38 +00:00
|
|
|
|
|
|
|
for i := range skeys {
|
2020-06-18 10:50:30 +00:00
|
|
|
err := context.DAO.PutStorageItem(id, skeys[i], items[i])
|
2019-12-26 11:34:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("normal invocation", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal([]byte{0x01})
|
2020-06-18 10:50:30 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: id}))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
err := storageFind(context)
|
2019-12-26 11:34:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
var iter *stackitem.Interop
|
2019-12-26 11:34:38 +00:00
|
|
|
require.NotPanics(t, func() { iter = v.Estack().Top().Interop() })
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Next(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
require.True(t, v.Estack().Pop().Bool())
|
|
|
|
|
2020-05-29 08:02:26 +00:00
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, iterator.Key(context))
|
2020-05-29 08:02:26 +00:00
|
|
|
require.Equal(t, []byte{0x01, 0x01}, v.Estack().Pop().Bytes())
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Value(context))
|
2020-05-29 08:02:26 +00:00
|
|
|
require.Equal(t, []byte{0x03, 0x04, 0x05, 0x06}, v.Estack().Pop().Bytes())
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Next(context))
|
2020-05-29 08:02:26 +00:00
|
|
|
require.True(t, v.Estack().Pop().Bool())
|
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, iterator.Key(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
require.Equal(t, []byte{0x01, 0x02}, v.Estack().Pop().Bytes())
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Value(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, v.Estack().Pop().Bytes())
|
|
|
|
|
|
|
|
v.Estack().PushVal(iter)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Next(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
|
|
|
|
2020-05-29 08:02:26 +00:00
|
|
|
t.Run("normal invocation, empty result", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal([]byte{0x03})
|
2020-06-18 10:50:30 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: id}))
|
2020-05-29 08:02:26 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
err := storageFind(context)
|
2020-05-29 08:02:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, enumerator.Next(context))
|
2020-05-29 08:02:26 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
t.Run("invalid type for StorageContext", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal([]byte{0x01})
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(nil))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, storageFind(context))
|
2019-12-26 11:34:38 +00:00
|
|
|
})
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
t.Run("invalid id", func(t *testing.T) {
|
|
|
|
invalidID := id + 1
|
2019-12-26 11:34:38 +00:00
|
|
|
|
|
|
|
v.Estack().PushVal([]byte{0x01})
|
2020-06-18 10:50:30 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(&StorageContext{ID: invalidID}))
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, storageFind(context))
|
|
|
|
require.NoError(t, enumerator.Next(context))
|
2020-06-18 10:50:30 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
2019-12-26 11:34:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-18 09:41:09 +00:00
|
|
|
func TestECDSAVerify(t *testing.T) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
chain := newTestChain(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
ic := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil)
|
2020-03-18 09:41:09 +00:00
|
|
|
runCase := func(t *testing.T, isErr bool, result interface{}, args ...interface{}) {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.SpawnVM()
|
2020-03-18 09:41:09 +00:00
|
|
|
for i := range args {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.Estack().PushVal(args[i])
|
2020-03-18 09:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("panic: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2020-08-07 11:37:49 +00:00
|
|
|
err = crypto.ECDSASecp256r1Verify(ic)
|
2020-03-18 09:41:09 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if isErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2020-10-29 16:10:28 +00:00
|
|
|
require.Equal(t, 1, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, result, ic.VM.Estack().Pop().Value().(bool))
|
2020-03-18 09:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := []byte("test message")
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
runCase(t, false, true, sign, priv.PublicKey().Bytes(), msg)
|
|
|
|
})
|
|
|
|
|
2020-04-13 10:43:36 +00:00
|
|
|
t.Run("signed interop item", func(t *testing.T) {
|
2020-06-18 09:00:51 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{0, 1, 2}, 1)
|
2020-04-13 10:43:36 +00:00
|
|
|
msg := tx.GetSignedPart()
|
|
|
|
sign := priv.Sign(msg)
|
2020-06-03 12:55:06 +00:00
|
|
|
runCase(t, false, true, sign, priv.PublicKey().Bytes(), stackitem.NewInterop(tx))
|
2020-04-13 10:43:36 +00:00
|
|
|
})
|
|
|
|
|
2020-04-13 13:18:28 +00:00
|
|
|
t.Run("signed script container", func(t *testing.T) {
|
2020-06-18 09:00:51 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{0, 1, 2}, 1)
|
2020-04-13 13:18:28 +00:00
|
|
|
msg := tx.GetSignedPart()
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
ic.Container = tx
|
2020-06-03 12:55:06 +00:00
|
|
|
runCase(t, false, true, sign, priv.PublicKey().Bytes(), stackitem.Null{})
|
2020-04-13 13:18:28 +00:00
|
|
|
})
|
|
|
|
|
2020-03-18 09:41:09 +00:00
|
|
|
t.Run("missing arguments", func(t *testing.T) {
|
|
|
|
runCase(t, true, false)
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
runCase(t, true, false, sign)
|
|
|
|
runCase(t, true, false, sign, priv.PublicKey().Bytes())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid signature", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
2020-04-14 13:15:49 +00:00
|
|
|
sign[0] = ^sign[0]
|
2020-03-18 09:41:09 +00:00
|
|
|
runCase(t, false, false, sign, priv.PublicKey().Bytes(), msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid public key", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
|
|
|
pub := priv.PublicKey().Bytes()
|
2020-07-25 08:50:03 +00:00
|
|
|
pub[0] = 0xFF // invalid prefix
|
2020-03-18 09:41:09 +00:00
|
|
|
runCase(t, true, false, sign, pub, msg)
|
|
|
|
})
|
2020-07-25 09:16:56 +00:00
|
|
|
|
|
|
|
t.Run("invalid message", func(t *testing.T) {
|
|
|
|
sign := priv.Sign(msg)
|
2020-08-29 15:44:45 +00:00
|
|
|
runCase(t, true, false, sign, priv.PublicKey().Bytes(),
|
2020-07-25 09:16:56 +00:00
|
|
|
stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(msg)}))
|
|
|
|
})
|
2020-03-18 09:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
func TestRuntimeEncodeDecode(t *testing.T) {
|
|
|
|
original := []byte("my pretty string")
|
|
|
|
encoded64 := base64.StdEncoding.EncodeToString(original)
|
|
|
|
encoded58 := base58.Encode(original)
|
2020-07-15 08:49:24 +00:00
|
|
|
v, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
t.Run("Encode64", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(original)
|
|
|
|
require.NoError(t, runtimeEncodeBase64(ic))
|
2020-07-15 08:49:24 +00:00
|
|
|
actual := v.Estack().Pop().Bytes()
|
2020-08-23 14:19:56 +00:00
|
|
|
require.Equal(t, []byte(encoded64), actual)
|
2020-07-15 08:49:24 +00:00
|
|
|
})
|
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
t.Run("Encode58", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(original)
|
|
|
|
require.NoError(t, runtimeEncodeBase58(ic))
|
|
|
|
actual := v.Estack().Pop().Bytes()
|
|
|
|
require.Equal(t, []byte(encoded58), actual)
|
|
|
|
})
|
|
|
|
t.Run("Decode64/positive", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(encoded64)
|
|
|
|
require.NoError(t, runtimeDecodeBase64(ic))
|
|
|
|
actual := v.Estack().Pop().Bytes()
|
|
|
|
require.Equal(t, original, actual)
|
|
|
|
})
|
|
|
|
t.Run("Decode64/error", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(encoded64 + "%")
|
|
|
|
require.Error(t, runtimeDecodeBase64(ic))
|
|
|
|
})
|
|
|
|
t.Run("Decode58/positive", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(encoded58)
|
|
|
|
require.NoError(t, runtimeDecodeBase58(ic))
|
|
|
|
actual := v.Estack().Pop().Bytes()
|
|
|
|
require.Equal(t, original, actual)
|
|
|
|
})
|
|
|
|
t.Run("Decode58/error", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(encoded58 + "%")
|
|
|
|
require.Error(t, runtimeDecodeBase58(ic))
|
2020-07-15 08:49:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:42:58 +00:00
|
|
|
// Helper functions to create VM, InteropContext, TX, Account, Contract.
|
2019-11-18 16:31:27 +00:00
|
|
|
|
2020-06-09 10:18:32 +00:00
|
|
|
func createVM(t *testing.T) (*vm.VM, *interop.Context, *Blockchain) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
context := chain.newInteropContext(trigger.Application,
|
2020-06-18 09:00:51 +00:00
|
|
|
dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-06-09 10:18:32 +00:00
|
|
|
return v, context, chain
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interop.Context, *Blockchain) {
|
2020-06-09 09:18:08 +00:00
|
|
|
v, block, context, chain := createVMAndBlock(t)
|
|
|
|
v.Estack().PushVal(stackitem.NewInterop(block))
|
|
|
|
return v, block, context, chain
|
|
|
|
}
|
|
|
|
|
|
|
|
func createVMAndBlock(t *testing.T) (*vm.VM, *block.Block, *interop.Context, *Blockchain) {
|
2019-11-18 16:31:27 +00:00
|
|
|
block := newDumbBlock()
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-06-18 09:00:51 +00:00
|
|
|
context := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), block, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, block, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
|
2020-01-10 08:47:55 +00:00
|
|
|
v, tx, context, chain := createVMAndTX(t)
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(tx))
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, tx, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.Context, *Blockchain) {
|
2020-06-09 09:12:56 +00:00
|
|
|
script := []byte("testscript")
|
|
|
|
m := manifest.NewManifest(hash.Hash160(script))
|
|
|
|
m.Features = smartcontract.HasStorage
|
2019-11-28 16:06:09 +00:00
|
|
|
contractState := &state.Contract{
|
2020-06-09 09:12:56 +00:00
|
|
|
Script: script,
|
|
|
|
Manifest: *m,
|
2020-06-18 10:50:30 +00:00
|
|
|
ID: 123,
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-06-18 09:00:51 +00:00
|
|
|
context := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, contractState, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
|
2019-12-23 12:04:23 +00:00
|
|
|
script := []byte{byte(opcode.PUSH1), byte(opcode.RET)}
|
2020-06-18 09:00:51 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, script, 0)
|
2019-11-18 16:31:27 +00:00
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3, 4}}}
|
2020-01-10 08:47:55 +00:00
|
|
|
chain := newTestChain(t)
|
2020-06-18 09:00:51 +00:00
|
|
|
context := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, tx)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := context.SpawnVM()
|
2020-01-10 08:47:55 +00:00
|
|
|
return v, tx, context, chain
|
2019-11-18 16:31:27 +00:00
|
|
|
}
|