2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-17 14:10:00 +00:00
|
|
|
"encoding/binary"
|
2018-03-30 16:15:06 +00:00
|
|
|
"encoding/hex"
|
2020-04-28 08:24:02 +00:00
|
|
|
"fmt"
|
2018-04-02 15:04:42 +00:00
|
|
|
"math/big"
|
2018-03-30 16:15:06 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-04-21 13:45:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-06-10 12:51:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-10 14:57:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"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"
|
2018-03-30 16:15:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-22 10:44:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-30 16:15:06 +00:00
|
|
|
)
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
func fooInteropGetter(id uint32) *InteropFuncPrice {
|
2020-04-15 14:13:50 +00:00
|
|
|
if id == emit.InteropNameToID([]byte("foo")) {
|
2020-06-10 12:51:28 +00:00
|
|
|
return &InteropFuncPrice{
|
|
|
|
Func: func(evm *VM) error {
|
|
|
|
evm.Estack().PushVal(1)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Price: 1,
|
|
|
|
}
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
func TestInteropHook(t *testing.T) {
|
2019-10-22 10:44:14 +00:00
|
|
|
v := New()
|
2019-12-18 16:49:56 +00:00
|
|
|
v.RegisterInteropGetter(fooInteropGetter)
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Syscall(buf.BinWriter, "foo")
|
|
|
|
emit.Opcode(buf.BinWriter, opcode.RET)
|
2018-04-10 09:45:31 +00:00
|
|
|
v.Load(buf.Bytes())
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, v)
|
2018-04-10 09:45:31 +00:00
|
|
|
assert.Equal(t, 1, v.estack.Len())
|
|
|
|
assert.Equal(t, big.NewInt(1), v.estack.Pop().value.Value())
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
func TestRegisterInteropGetter(t *testing.T) {
|
2019-10-22 10:44:14 +00:00
|
|
|
v := New()
|
2019-12-18 16:49:56 +00:00
|
|
|
currRegistered := len(v.getInterop)
|
|
|
|
v.RegisterInteropGetter(fooInteropGetter)
|
|
|
|
assert.Equal(t, currRegistered+1, len(v.getInterop))
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 11:42:36 +00:00
|
|
|
func TestVM_SetPriceGetter(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
prog := []byte{
|
|
|
|
byte(opcode.PUSH4), byte(opcode.PUSH2),
|
|
|
|
byte(opcode.PUSHDATA1), 0x01, 0x01,
|
|
|
|
byte(opcode.PUSHDATA1), 0x02, 0xCA, 0xFE,
|
|
|
|
byte(opcode.PUSH4), byte(opcode.RET),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no price getter", func(t *testing.T) {
|
|
|
|
v.Load(prog)
|
|
|
|
runVM(t, v)
|
|
|
|
|
|
|
|
require.EqualValues(t, 0, v.GasConsumed())
|
|
|
|
})
|
|
|
|
|
2020-06-23 14:15:35 +00:00
|
|
|
v.SetPriceGetter(func(_ *VM, op opcode.Opcode, p []byte) int64 {
|
2020-01-20 11:42:36 +00:00
|
|
|
if op == opcode.PUSH4 {
|
|
|
|
return 1
|
|
|
|
} else if op == opcode.PUSHDATA1 && bytes.Equal(p, []byte{0xCA, 0xFE}) {
|
|
|
|
return 7
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with price getter", func(t *testing.T) {
|
|
|
|
v.Load(prog)
|
|
|
|
runVM(t, v)
|
|
|
|
|
2020-01-20 11:49:07 +00:00
|
|
|
require.EqualValues(t, 9, v.GasConsumed())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with sufficient gas limit", func(t *testing.T) {
|
|
|
|
v.Load(prog)
|
2020-06-16 12:13:18 +00:00
|
|
|
v.GasLimit = 9
|
2020-01-20 11:49:07 +00:00
|
|
|
runVM(t, v)
|
|
|
|
|
|
|
|
require.EqualValues(t, 9, v.GasConsumed())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with small gas limit", func(t *testing.T) {
|
|
|
|
v.Load(prog)
|
2020-06-16 12:13:18 +00:00
|
|
|
v.GasLimit = 8
|
2020-01-20 11:49:07 +00:00
|
|
|
checkVMFailed(t, v)
|
2020-01-20 11:42:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:23:14 +00:00
|
|
|
func TestAddGas(t *testing.T) {
|
|
|
|
v := New()
|
2020-06-16 12:13:18 +00:00
|
|
|
v.GasLimit = 10
|
2020-06-09 09:23:14 +00:00
|
|
|
require.True(t, v.AddGas(5))
|
|
|
|
require.True(t, v.AddGas(5))
|
|
|
|
require.False(t, v.AddGas(5))
|
|
|
|
}
|
|
|
|
|
2019-12-10 16:13:29 +00:00
|
|
|
func TestBytesToPublicKey(t *testing.T) {
|
|
|
|
v := New()
|
|
|
|
cache := v.GetPublicKeys()
|
|
|
|
assert.Equal(t, 0, len(cache))
|
|
|
|
keyHex := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
|
|
|
keyBytes, _ := hex.DecodeString(keyHex)
|
|
|
|
key := v.bytesToPublicKey(keyBytes)
|
|
|
|
assert.NotNil(t, key)
|
|
|
|
key2 := v.bytesToPublicKey(keyBytes)
|
|
|
|
assert.Equal(t, key, key2)
|
|
|
|
|
|
|
|
cache = v.GetPublicKeys()
|
|
|
|
assert.Equal(t, 1, len(cache))
|
|
|
|
assert.NotNil(t, cache[string(keyBytes)])
|
|
|
|
|
|
|
|
keyBytes[0] = 0xff
|
|
|
|
require.Panics(t, func() { v.bytesToPublicKey(keyBytes) })
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestPushBytes1to75(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-03-30 16:15:06 +00:00
|
|
|
for i := 1; i <= 75; i++ {
|
|
|
|
b := randomBytes(i)
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(buf.BinWriter, b)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(buf.Bytes())
|
2019-10-22 10:44:14 +00:00
|
|
|
err := vm.Step()
|
|
|
|
require.NoError(t, err)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
|
|
|
|
elem := vm.estack.Pop()
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.IsType(t, &stackitem.ByteArray{}, elem.value)
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.IsType(t, elem.Bytes(), b)
|
|
|
|
assert.Equal(t, 0, vm.estack.Len())
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
errExec := vm.execute(nil, opcode.RET, nil)
|
2019-10-22 10:44:14 +00:00
|
|
|
require.NoError(t, errExec)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, vm.astack.Len())
|
|
|
|
assert.Equal(t, 0, vm.istack.Len())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
func runVM(t *testing.T, vm *VM) {
|
|
|
|
err := vm.Run()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, vm.HasFailed())
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkVMFailed(t *testing.T, vm *VM) {
|
|
|
|
err := vm.Run()
|
|
|
|
require.Error(t, err)
|
2019-09-23 15:46:47 +00:00
|
|
|
assert.Equal(t, true, vm.HasFailed())
|
2019-09-09 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
func TestStackLimitPUSH1Good(t *testing.T) {
|
|
|
|
prog := make([]byte, MaxStackSize*2)
|
|
|
|
for i := 0; i < MaxStackSize; i++ {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[i] = byte(opcode.PUSH1)
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
for i := MaxStackSize; i < MaxStackSize*2; i++ {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[i] = byte(opcode.DROP)
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v := load(prog)
|
|
|
|
runVM(t, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackLimitPUSH1Bad(t *testing.T) {
|
|
|
|
prog := make([]byte, MaxStackSize+1)
|
|
|
|
for i := range prog {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[i] = byte(opcode.PUSH1)
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
v := load(prog)
|
|
|
|
checkVMFailed(t, v)
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
func TestPUSHINT(t *testing.T) {
|
|
|
|
for i := byte(0); i < 5; i++ {
|
|
|
|
op := opcode.PUSHINT8 + opcode.Opcode(i)
|
|
|
|
t.Run(op.String(), func(t *testing.T) {
|
|
|
|
buf := random.Bytes((8 << i) / 8)
|
2020-04-30 08:08:03 +00:00
|
|
|
prog := append([]byte{byte(op)}, buf...)
|
2020-06-04 18:11:27 +00:00
|
|
|
runWithArgs(t, prog, bigint.FromBytes(buf))
|
2020-04-21 13:45:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 08:58:41 +00:00
|
|
|
func TestPUSHNULL(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSHNULL, opcode.PUSHNULL, opcode.EQUAL)
|
|
|
|
v := load(prog)
|
|
|
|
require.NoError(t, v.Step())
|
|
|
|
require.Equal(t, 1, v.estack.Len())
|
|
|
|
runVM(t, v)
|
|
|
|
require.True(t, v.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2020-03-23 09:26:26 +00:00
|
|
|
func TestISNULL(t *testing.T) {
|
2020-04-30 08:08:03 +00:00
|
|
|
prog := makeProgram(opcode.ISNULL)
|
|
|
|
t.Run("Integer", getTestFuncForVM(prog, false, 1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Null", getTestFuncForVM(prog, true, stackitem.Null{}))
|
2020-03-23 09:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func testISTYPE(t *testing.T, result bool, typ stackitem.Type, item stackitem.Item) {
|
2020-04-24 10:46:46 +00:00
|
|
|
prog := []byte{byte(opcode.ISTYPE), byte(typ)}
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, result, item)
|
2020-04-24 10:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestISTYPE(t *testing.T) {
|
|
|
|
t.Run("Integer", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.IntegerT, stackitem.NewBigInteger(big.NewInt(42)))
|
|
|
|
testISTYPE(t, false, stackitem.IntegerT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("Boolean", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.BooleanT, stackitem.NewBool(true))
|
|
|
|
testISTYPE(t, false, stackitem.BooleanT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("ByteArray", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.ByteArrayT, stackitem.NewByteArray([]byte{}))
|
|
|
|
testISTYPE(t, false, stackitem.ByteArrayT, stackitem.NewBigInteger(big.NewInt(42)))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("Array", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.ArrayT, stackitem.NewArray([]stackitem.Item{}))
|
|
|
|
testISTYPE(t, false, stackitem.ArrayT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("Struct", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.StructT, stackitem.NewStruct([]stackitem.Item{}))
|
|
|
|
testISTYPE(t, false, stackitem.StructT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("Map", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.MapT, stackitem.NewMap())
|
|
|
|
testISTYPE(t, false, stackitem.MapT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
t.Run("Interop", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testISTYPE(t, true, stackitem.InteropT, stackitem.NewInterop(42))
|
|
|
|
testISTYPE(t, false, stackitem.InteropT, stackitem.NewByteArray([]byte{}))
|
2020-04-24 10:46:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func testCONVERT(to stackitem.Type, item, res stackitem.Item) func(t *testing.T) {
|
2020-04-28 08:24:02 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
prog := []byte{byte(opcode.CONVERT), byte(to)}
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, res, item)
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCONVERT(t *testing.T) {
|
|
|
|
type convertTC struct {
|
2020-06-03 12:55:06 +00:00
|
|
|
item, res stackitem.Item
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(7)),
|
|
|
|
stackitem.NewByteArray([]byte{4, 8, 15}),
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.NewByteArray([]byte{1}), stackitem.NewByteArray([]byte{2}))
|
2020-04-28 08:24:02 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
getName := func(item stackitem.Item, typ stackitem.Type) string {
|
|
|
|
return fmt.Sprintf("%s->%s", item, typ)
|
|
|
|
}
|
2020-04-28 08:24:02 +00:00
|
|
|
|
|
|
|
t.Run("->Bool", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testBool := func(a, b stackitem.Item) func(t *testing.T) {
|
|
|
|
return testCONVERT(stackitem.BooleanT, a, b)
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
trueCases := []stackitem.Item{
|
|
|
|
stackitem.NewBool(true), stackitem.NewBigInteger(big.NewInt(11)), stackitem.NewByteArray([]byte{1, 2, 3}),
|
|
|
|
stackitem.NewArray(arr), stackitem.NewArray(nil),
|
|
|
|
stackitem.NewStruct(arr), stackitem.NewStruct(nil),
|
|
|
|
stackitem.NewMap(), m, stackitem.NewInterop(struct{}{}),
|
|
|
|
stackitem.NewPointer(0, []byte{}),
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
for i := range trueCases {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run(getName(trueCases[i], stackitem.BooleanT), testBool(trueCases[i], stackitem.NewBool(true)))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
falseCases := []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewByteArray([]byte{0, 0}), stackitem.NewBool(false),
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
for i := range falseCases {
|
2020-06-03 12:55:06 +00:00
|
|
|
testBool(falseCases[i], stackitem.NewBool(false))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("compound/interop -> basic", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
types := []stackitem.Type{stackitem.IntegerT, stackitem.ByteArrayT}
|
|
|
|
items := []stackitem.Item{stackitem.NewArray(nil), stackitem.NewStruct(nil), stackitem.NewMap(), stackitem.NewInterop(struct{}{})}
|
2020-04-28 08:24:02 +00:00
|
|
|
for _, typ := range types {
|
|
|
|
for j := range items {
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run(getName(items[j], typ), testCONVERT(typ, items[j], nil))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("primitive -> Integer/ByteArray", func(t *testing.T) {
|
|
|
|
n := big.NewInt(42)
|
2020-06-04 18:11:27 +00:00
|
|
|
b := bigint.ToBytes(n)
|
2020-04-28 08:24:02 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
itemInt := stackitem.NewBigInteger(n)
|
|
|
|
itemBytes := stackitem.NewByteArray(b)
|
2020-04-28 08:24:02 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
trueCases := map[stackitem.Type][]convertTC{
|
|
|
|
stackitem.IntegerT: {
|
2020-04-28 08:24:02 +00:00
|
|
|
{itemInt, itemInt},
|
|
|
|
{itemBytes, itemInt},
|
2020-06-03 12:55:06 +00:00
|
|
|
{stackitem.NewBool(true), stackitem.NewBigInteger(big.NewInt(1))},
|
|
|
|
{stackitem.NewBool(false), stackitem.NewBigInteger(big.NewInt(0))},
|
2020-04-28 08:24:02 +00:00
|
|
|
},
|
2020-06-03 12:55:06 +00:00
|
|
|
stackitem.ByteArrayT: {
|
2020-04-28 08:24:02 +00:00
|
|
|
{itemInt, itemBytes},
|
|
|
|
{itemBytes, itemBytes},
|
2020-06-03 12:55:06 +00:00
|
|
|
{stackitem.NewBool(true), stackitem.NewByteArray([]byte{1})},
|
|
|
|
{stackitem.NewBool(false), stackitem.NewByteArray([]byte{0})},
|
2020-04-28 08:24:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for typ := range trueCases {
|
|
|
|
for _, tc := range trueCases[typ] {
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run(getName(tc.item, typ), testCONVERT(typ, tc.item, tc.res))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Struct<->Array", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
arrayItem := stackitem.NewArray(arr)
|
|
|
|
structItem := stackitem.NewStruct(arr)
|
|
|
|
t.Run("Array->Array", testCONVERT(stackitem.ArrayT, arrayItem, arrayItem))
|
|
|
|
t.Run("Array->Struct", testCONVERT(stackitem.StructT, arrayItem, structItem))
|
|
|
|
t.Run("Struct->Array", testCONVERT(stackitem.ArrayT, structItem, arrayItem))
|
|
|
|
t.Run("Struct->Struct", testCONVERT(stackitem.StructT, structItem, structItem))
|
2020-04-28 08:24:02 +00:00
|
|
|
})
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Map->Map", testCONVERT(stackitem.MapT, m, m))
|
2020-04-28 08:24:02 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
ptr := stackitem.NewPointer(1, []byte{1})
|
|
|
|
t.Run("Pointer->Pointer", testCONVERT(stackitem.PointerT, ptr, ptr))
|
2020-05-06 13:31:52 +00:00
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
t.Run("Null->", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
types := []stackitem.Type{
|
|
|
|
stackitem.BooleanT, stackitem.ByteArrayT, stackitem.IntegerT, stackitem.ArrayT, stackitem.StructT, stackitem.MapT, stackitem.InteropT, stackitem.PointerT,
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
for i := range types {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run(types[i].String(), testCONVERT(types[i], stackitem.Null{}, stackitem.Null{}))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("->Any", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
items := []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewByteArray([]byte{1}), stackitem.NewBool(true),
|
|
|
|
stackitem.NewArray(arr), stackitem.NewStruct(arr), m, stackitem.NewInterop(struct{}{}),
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range items {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run(items[i].String(), testCONVERT(stackitem.AnyT, items[i], nil))
|
2020-04-28 08:24:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
// appendBigStruct returns a program which:
|
|
|
|
// 1. pushes size Structs on stack
|
|
|
|
// 2. packs them into a new struct
|
|
|
|
// 3. appends them to a zero-length array
|
|
|
|
// Resulting stack size consists of:
|
|
|
|
// - struct (size+1)
|
|
|
|
// - array (1) of struct (size+1)
|
|
|
|
// which equals to size*2+3 elements in total.
|
2019-12-03 14:05:06 +00:00
|
|
|
func appendBigStruct(size uint16) []opcode.Opcode {
|
|
|
|
prog := make([]opcode.Opcode, size*2)
|
2019-10-29 10:26:34 +00:00
|
|
|
for i := uint16(0); i < size; i++ {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[i*2] = opcode.PUSH0
|
|
|
|
prog[i*2+1] = opcode.NEWSTRUCT
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(prog,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.INITSSLOT, 1,
|
2020-04-21 13:45:48 +00:00
|
|
|
opcode.PUSHINT16, opcode.Opcode(size), opcode.Opcode(size>>8), // LE
|
2020-06-03 12:55:06 +00:00
|
|
|
opcode.PACK, opcode.CONVERT, opcode.Opcode(stackitem.StructT),
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.STSFLD0, opcode.LDSFLD0,
|
2019-12-03 14:05:06 +00:00
|
|
|
opcode.DUP,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.PUSH0, opcode.NEWARRAY,
|
2019-12-03 14:05:06 +00:00
|
|
|
opcode.SWAP,
|
|
|
|
opcode.APPEND, opcode.RET)
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackLimitAPPENDStructGood(t *testing.T) {
|
|
|
|
prog := makeProgram(appendBigStruct(MaxStackSize/2 - 2)...)
|
|
|
|
v := load(prog)
|
|
|
|
runVM(t, v) // size = 2047 = (Max/2-2)*2+3 = Max-1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackLimitAPPENDStructBad(t *testing.T) {
|
|
|
|
prog := makeProgram(appendBigStruct(MaxStackSize/2 - 1)...)
|
|
|
|
v := load(prog)
|
|
|
|
checkVMFailed(t, v) // size = 2049 = (Max/2-1)*2+3 = Max+1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackLimit(t *testing.T) {
|
|
|
|
expected := []struct {
|
2019-12-03 14:05:06 +00:00
|
|
|
inst opcode.Opcode
|
2019-10-29 10:26:34 +00:00
|
|
|
size int
|
|
|
|
}{
|
2019-12-03 14:05:06 +00:00
|
|
|
{opcode.PUSH2, 1},
|
|
|
|
{opcode.NEWARRAY, 3}, // array + 2 items
|
2020-05-07 08:54:35 +00:00
|
|
|
{opcode.STSFLD0, 3},
|
|
|
|
{opcode.LDSFLD0, 4},
|
2020-05-20 14:09:57 +00:00
|
|
|
{opcode.NEWMAP, 5},
|
|
|
|
{opcode.DUP, 6},
|
|
|
|
{opcode.PUSH2, 7},
|
|
|
|
{opcode.LDSFLD0, 8},
|
|
|
|
{opcode.SETITEM, 6}, // -3 items and 1 new element in map
|
|
|
|
{opcode.DUP, 7},
|
|
|
|
{opcode.PUSH2, 8},
|
|
|
|
{opcode.LDSFLD0, 9},
|
|
|
|
{opcode.SETITEM, 6}, // -3 items and no new elements in map
|
|
|
|
{opcode.DUP, 7},
|
|
|
|
{opcode.PUSH2, 8},
|
|
|
|
{opcode.REMOVE, 5}, // as we have right after NEWMAP
|
|
|
|
{opcode.DROP, 4}, // DROP map with no elements
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
prog := make([]opcode.Opcode, len(expected)+2)
|
|
|
|
prog[0] = opcode.INITSSLOT
|
|
|
|
prog[1] = 1
|
2019-10-29 10:26:34 +00:00
|
|
|
for i := range expected {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog[i+2] = expected[i].inst
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vm := load(makeProgram(prog...))
|
2020-05-07 08:54:35 +00:00
|
|
|
require.NoError(t, vm.Step(), "failed to initialize static slot")
|
2019-10-29 10:26:34 +00:00
|
|
|
for i := range expected {
|
|
|
|
require.NoError(t, vm.Step())
|
2020-05-07 08:54:35 +00:00
|
|
|
require.Equal(t, expected[i].size, vm.refs.size, "i: %d", i)
|
2019-10-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestPushm1to16(t *testing.T) {
|
2019-01-25 11:20:35 +00:00
|
|
|
var prog []byte
|
2019-12-03 14:05:06 +00:00
|
|
|
for i := int(opcode.PUSHM1); i <= int(opcode.PUSH16); i++ {
|
2018-03-30 16:15:06 +00:00
|
|
|
if i == 80 {
|
|
|
|
continue // opcode layout we got here.
|
|
|
|
}
|
|
|
|
prog = append(prog, byte(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := load(prog)
|
2019-12-03 14:05:06 +00:00
|
|
|
for i := int(opcode.PUSHM1); i <= int(opcode.PUSH16); i++ {
|
2019-10-22 10:44:14 +00:00
|
|
|
err := vm.Step()
|
|
|
|
require.NoError(t, err)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
elem := vm.estack.Pop()
|
2019-12-03 14:05:06 +00:00
|
|
|
val := i - int(opcode.PUSH1) + 1
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, elem.BigInt().Int64(), int64(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPUSHDATA1(t *testing.T) {
|
|
|
|
t.Run("Good", getTestFuncForVM([]byte{byte(opcode.PUSHDATA1), 3, 1, 2, 3}, []byte{1, 2, 3}))
|
|
|
|
t.Run("NoN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA1)}, nil))
|
|
|
|
t.Run("BadN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA1), 1}, nil))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPUSHDATA2(t *testing.T) {
|
|
|
|
t.Run("Good", getTestFuncForVM([]byte{byte(opcode.PUSHDATA2), 3, 0, 1, 2, 3}, []byte{1, 2, 3}))
|
|
|
|
t.Run("NoN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA2)}, nil))
|
|
|
|
t.Run("ShortN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA2), 0}, nil))
|
|
|
|
t.Run("BadN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA2), 1, 0}, nil))
|
2019-09-11 08:52:39 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPUSHDATA4(t *testing.T) {
|
|
|
|
t.Run("Good", getTestFuncForVM([]byte{byte(opcode.PUSHDATA4), 3, 0, 0, 0, 1, 2, 3}, []byte{1, 2, 3}))
|
|
|
|
t.Run("NoN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA4)}, nil))
|
|
|
|
t.Run("BadN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA4), 1, 0, 0, 0}, nil))
|
|
|
|
t.Run("ShortN", getTestFuncForVM([]byte{byte(opcode.PUSHDATA4), 0, 0, 0}, nil))
|
2019-09-11 08:52:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:10:00 +00:00
|
|
|
func TestPushData4BigN(t *testing.T) {
|
2020-06-11 13:31:31 +00:00
|
|
|
prog := make([]byte, 1+4+stackitem.MaxSize+1)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[0] = byte(opcode.PUSHDATA4)
|
2020-06-11 13:31:31 +00:00
|
|
|
binary.LittleEndian.PutUint32(prog[1:], stackitem.MaxSize+1)
|
2019-10-17 14:10:00 +00:00
|
|
|
|
|
|
|
vm := load(prog)
|
2020-04-30 08:08:03 +00:00
|
|
|
checkVMFailed(t, vm)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 12:29:27 +00:00
|
|
|
func getEnumeratorProg(n int, isIter bool) (prog []byte) {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog = []byte{byte(opcode.INITSSLOT), 1, byte(opcode.STSFLD0)}
|
2019-11-13 11:34:03 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog = append(prog, byte(opcode.LDSFLD0))
|
2020-06-10 08:49:39 +00:00
|
|
|
prog = append(prog, getSyscallProg("System.Enumerator.Next")...)
|
2020-05-07 08:54:35 +00:00
|
|
|
prog = append(prog, byte(opcode.LDSFLD0))
|
2020-06-10 08:49:39 +00:00
|
|
|
prog = append(prog, getSyscallProg("System.Enumerator.Value")...)
|
2019-11-13 12:29:27 +00:00
|
|
|
if isIter {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog = append(prog, byte(opcode.LDSFLD0))
|
2020-06-10 08:49:39 +00:00
|
|
|
prog = append(prog, getSyscallProg("System.Iterator.Key")...)
|
2019-11-13 12:29:27 +00:00
|
|
|
}
|
2019-11-13 11:34:03 +00:00
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
prog = append(prog, byte(opcode.LDSFLD0))
|
2020-06-10 08:49:39 +00:00
|
|
|
prog = append(prog, getSyscallProg("System.Enumerator.Next")...)
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func checkEnumeratorStack(t *testing.T, vm *VM, arr []stackitem.Item) {
|
2019-11-13 11:34:03 +00:00
|
|
|
require.Equal(t, len(arr)+1, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, stackitem.NewBool(false), vm.estack.Peek(0).value)
|
2019-11-13 11:34:03 +00:00
|
|
|
for i := 0; i < len(arr); i++ {
|
|
|
|
require.Equal(t, arr[i], vm.estack.Peek(i+1).value, "pos: %d", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIterableCreate(t *testing.T, typ string) {
|
2019-11-13 12:29:27 +00:00
|
|
|
isIter := typ == "Iterator"
|
2020-06-10 08:49:39 +00:00
|
|
|
prog := getSyscallProg("System." + typ + ".Create")
|
2019-11-13 12:29:27 +00:00
|
|
|
prog = append(prog, getEnumeratorProg(2, isIter)...)
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
vm := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(42)),
|
|
|
|
stackitem.NewByteArray([]byte{3, 2, 1}),
|
2019-11-13 11:34:03 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
vm.estack.Push(&Element{value: stackitem.NewArray(arr)})
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
runVM(t, vm)
|
2019-11-13 12:29:27 +00:00
|
|
|
if isIter {
|
2020-06-03 12:55:06 +00:00
|
|
|
checkEnumeratorStack(t, vm, []stackitem.Item{
|
|
|
|
stackitem.Make(1), arr[1], stackitem.NewBool(true),
|
|
|
|
stackitem.Make(0), arr[0], stackitem.NewBool(true),
|
2019-11-13 12:29:27 +00:00
|
|
|
})
|
|
|
|
} else {
|
2020-06-03 12:55:06 +00:00
|
|
|
checkEnumeratorStack(t, vm, []stackitem.Item{
|
|
|
|
arr[1], stackitem.NewBool(true),
|
|
|
|
arr[0], stackitem.NewBool(true),
|
2019-11-13 12:29:27 +00:00
|
|
|
})
|
|
|
|
}
|
2019-11-13 11:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnumeratorCreate(t *testing.T) {
|
|
|
|
testIterableCreate(t, "Enumerator")
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:29:27 +00:00
|
|
|
func TestIteratorCreate(t *testing.T) {
|
|
|
|
testIterableCreate(t, "Iterator")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIterableConcat(t *testing.T, typ string) {
|
|
|
|
isIter := typ == "Iterator"
|
2020-06-10 08:49:39 +00:00
|
|
|
prog := getSyscallProg("System." + typ + ".Create")
|
2019-11-13 11:34:03 +00:00
|
|
|
prog = append(prog, byte(opcode.SWAP))
|
2020-06-10 08:49:39 +00:00
|
|
|
prog = append(prog, getSyscallProg("System."+typ+".Create")...)
|
|
|
|
prog = append(prog, getSyscallProg("System."+typ+".Concat")...)
|
2019-11-13 12:29:27 +00:00
|
|
|
prog = append(prog, getEnumeratorProg(3, isIter)...)
|
2019-11-13 11:34:03 +00:00
|
|
|
vm := load(prog)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{
|
|
|
|
stackitem.NewBool(false),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(123)),
|
|
|
|
stackitem.NewMap(),
|
2019-11-13 11:34:03 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
vm.estack.Push(&Element{value: stackitem.NewArray(arr[:1])})
|
|
|
|
vm.estack.Push(&Element{value: stackitem.NewArray(arr[1:])})
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
runVM(t, vm)
|
2019-11-13 12:29:27 +00:00
|
|
|
|
|
|
|
if isIter {
|
|
|
|
// Yes, this is how iterators are concatenated in reference VM
|
|
|
|
// https://github.com/neo-project/neo/blob/master-2.x/neo.UnitTests/UT_ConcatenatedIterator.cs#L54
|
2020-06-03 12:55:06 +00:00
|
|
|
checkEnumeratorStack(t, vm, []stackitem.Item{
|
|
|
|
stackitem.Make(1), arr[2], stackitem.NewBool(true),
|
|
|
|
stackitem.Make(0), arr[1], stackitem.NewBool(true),
|
|
|
|
stackitem.Make(0), arr[0], stackitem.NewBool(true),
|
2019-11-13 12:29:27 +00:00
|
|
|
})
|
|
|
|
} else {
|
2020-06-03 12:55:06 +00:00
|
|
|
checkEnumeratorStack(t, vm, []stackitem.Item{
|
|
|
|
arr[2], stackitem.NewBool(true),
|
|
|
|
arr[1], stackitem.NewBool(true),
|
|
|
|
arr[0], stackitem.NewBool(true),
|
2019-11-13 12:29:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnumeratorConcat(t *testing.T) {
|
|
|
|
testIterableConcat(t, "Enumerator")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIteratorConcat(t *testing.T) {
|
|
|
|
testIterableConcat(t, "Iterator")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIteratorKeys(t *testing.T) {
|
2020-06-10 08:49:39 +00:00
|
|
|
prog := getSyscallProg("System.Iterator.Create")
|
|
|
|
prog = append(prog, getSyscallProg("System.Iterator.Keys")...)
|
2019-11-13 12:29:27 +00:00
|
|
|
prog = append(prog, getEnumeratorProg(2, false)...)
|
|
|
|
|
|
|
|
v := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewBool(false),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(42)),
|
2019-11-13 11:34:03 +00:00
|
|
|
})
|
2019-11-13 12:29:27 +00:00
|
|
|
v.estack.PushVal(arr)
|
|
|
|
|
|
|
|
runVM(t, v)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
checkEnumeratorStack(t, v, []stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewBool(true),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewBool(true),
|
2019-11-13 12:29:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIteratorValues(t *testing.T) {
|
2020-06-10 08:49:39 +00:00
|
|
|
prog := getSyscallProg("System.Iterator.Create")
|
|
|
|
prog = append(prog, getSyscallProg("System.Iterator.Values")...)
|
2019-11-13 12:29:27 +00:00
|
|
|
prog = append(prog, getEnumeratorProg(2, false)...)
|
|
|
|
|
|
|
|
v := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewBool(false))
|
|
|
|
m.Add(stackitem.NewByteArray([]byte{32}), stackitem.NewByteArray([]byte{7}))
|
2019-11-13 12:29:27 +00:00
|
|
|
v.estack.PushVal(m)
|
|
|
|
|
|
|
|
runVM(t, v)
|
|
|
|
require.Equal(t, 5, v.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, stackitem.NewBool(false), v.estack.Peek(0).value)
|
2019-11-13 12:29:27 +00:00
|
|
|
|
|
|
|
// Map values can be enumerated in any order.
|
|
|
|
i1, i2 := 1, 3
|
2020-06-03 12:55:06 +00:00
|
|
|
if _, ok := v.estack.Peek(i1).value.(*stackitem.Bool); !ok {
|
2019-11-13 12:29:27 +00:00
|
|
|
i1, i2 = i2, i1
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, stackitem.NewBool(false), v.estack.Peek(i1).value)
|
|
|
|
require.Equal(t, stackitem.NewByteArray([]byte{7}), v.estack.Peek(i2).value)
|
2019-11-13 12:29:27 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, stackitem.NewBool(true), v.estack.Peek(2).value)
|
|
|
|
require.Equal(t, stackitem.NewBool(true), v.estack.Peek(4).value)
|
2019-11-13 11:34:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 08:36:13 +00:00
|
|
|
func getSyscallProg(name string) (prog []byte) {
|
2020-04-15 14:40:05 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Syscall(buf.BinWriter, name)
|
|
|
|
return buf.Bytes()
|
2019-11-05 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSerializeProg() (prog []byte) {
|
2020-06-16 07:56:06 +00:00
|
|
|
prog = append(prog, getSyscallProg("System.Binary.Serialize")...)
|
|
|
|
prog = append(prog, getSyscallProg("System.Binary.Deserialize")...)
|
2019-12-03 14:05:06 +00:00
|
|
|
prog = append(prog, byte(opcode.RET))
|
2019-11-05 08:36:13 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSerialize(t *testing.T, vm *VM) {
|
|
|
|
err := vm.Step()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.ByteArray)(nil), vm.estack.Top().value)
|
2019-11-05 08:36:13 +00:00
|
|
|
|
|
|
|
err = vm.Step()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, vm.estack.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeBool(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
|
|
|
vm.estack.PushVal(true)
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.Bool)(nil), vm.estack.Top().value)
|
2019-11-05 08:36:13 +00:00
|
|
|
require.Equal(t, true, vm.estack.Top().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeByteArray(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
|
|
|
value := []byte{1, 2, 3}
|
|
|
|
vm.estack.PushVal(value)
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.ByteArray)(nil), vm.estack.Top().value)
|
2019-11-05 08:36:13 +00:00
|
|
|
require.Equal(t, value, vm.estack.Top().Bytes())
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:07:08 +00:00
|
|
|
func TestSerializeInteger(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
|
|
|
value := int64(123)
|
|
|
|
vm.estack.PushVal(value)
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.BigInteger)(nil), vm.estack.Top().value)
|
2019-11-05 09:07:08 +00:00
|
|
|
require.Equal(t, value, vm.estack.Top().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:25:15 +00:00
|
|
|
func TestSerializeArray(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
2020-06-03 12:55:06 +00:00
|
|
|
item := stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.Make(true),
|
|
|
|
stackitem.Make(123),
|
|
|
|
stackitem.NewMap(),
|
2019-11-05 09:25:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vm.estack.Push(&Element{value: item})
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.Array)(nil), vm.estack.Top().value)
|
|
|
|
require.Equal(t, item.Value().([]stackitem.Item), vm.estack.Top().Array())
|
2019-11-05 09:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeArrayBad(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
2020-06-03 12:55:06 +00:00
|
|
|
item := stackitem.NewArray(makeArrayOfType(2, stackitem.BooleanT))
|
|
|
|
item.Value().([]stackitem.Item)[1] = item
|
2019-11-05 09:25:15 +00:00
|
|
|
|
|
|
|
vm.estack.Push(&Element{value: item})
|
|
|
|
|
|
|
|
err := vm.Step()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, vm.HasFailed())
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:47:58 +00:00
|
|
|
func TestSerializeDupInteger(t *testing.T) {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog := makeProgram(
|
|
|
|
opcode.PUSH0, opcode.NEWARRAY, opcode.INITSSLOT, 1,
|
|
|
|
opcode.DUP, opcode.PUSH2, opcode.DUP, opcode.STSFLD0, opcode.APPEND,
|
|
|
|
opcode.DUP, opcode.LDSFLD0, opcode.APPEND,
|
|
|
|
)
|
2019-11-15 09:47:58 +00:00
|
|
|
vm := load(append(prog, getSerializeProg()...))
|
|
|
|
|
|
|
|
runVM(t, vm)
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:25:15 +00:00
|
|
|
func TestSerializeStruct(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
2020-06-03 12:55:06 +00:00
|
|
|
item := stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make(true),
|
|
|
|
stackitem.Make(123),
|
|
|
|
stackitem.NewMap(),
|
2019-11-05 09:25:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vm.estack.Push(&Element{value: item})
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.Struct)(nil), vm.estack.Top().value)
|
|
|
|
require.Equal(t, item.Value().([]stackitem.Item), vm.estack.Top().Array())
|
2019-11-05 09:25:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:04:14 +00:00
|
|
|
func TestDeserializeUnknown(t *testing.T) {
|
2020-06-16 07:56:06 +00:00
|
|
|
prog := append(getSyscallProg("System.Binary.Deserialize"), byte(opcode.RET))
|
2019-11-05 14:04:14 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
data, err := stackitem.SerializeItem(stackitem.NewBigInteger(big.NewInt(123)))
|
2019-11-05 14:04:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data[0] = 0xFF
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, data)
|
2019-11-05 14:04:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 08:57:55 +00:00
|
|
|
func TestSerializeMap(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
2020-06-03 12:55:06 +00:00
|
|
|
item := stackitem.NewMap()
|
|
|
|
item.Add(stackitem.Make(true), stackitem.Make([]byte{1, 2, 3}))
|
|
|
|
item.Add(stackitem.Make([]byte{0}), stackitem.Make(false))
|
2019-11-05 08:57:55 +00:00
|
|
|
|
|
|
|
vm.estack.Push(&Element{value: item})
|
|
|
|
|
|
|
|
testSerialize(t, vm)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.Map)(nil), vm.estack.Top().value)
|
|
|
|
require.Equal(t, item.Value(), vm.estack.Top().value.(*stackitem.Map).Value())
|
2019-11-05 08:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:25:42 +00:00
|
|
|
func TestSerializeMapCompat(t *testing.T) {
|
2020-04-24 10:46:46 +00:00
|
|
|
resHex := "480128036b6579280576616c7565"
|
2020-03-28 14:25:42 +00:00
|
|
|
res, err := hex.DecodeString(resHex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-15 14:40:05 +00:00
|
|
|
// Create a map, push key and value, add KV to map, serialize.
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Opcode(buf.BinWriter, opcode.NEWMAP)
|
|
|
|
emit.Opcode(buf.BinWriter, opcode.DUP)
|
|
|
|
emit.Bytes(buf.BinWriter, []byte("key"))
|
|
|
|
emit.Bytes(buf.BinWriter, []byte("value"))
|
|
|
|
emit.Opcode(buf.BinWriter, opcode.SETITEM)
|
2020-06-16 07:56:06 +00:00
|
|
|
emit.Syscall(buf.BinWriter, "System.Binary.Serialize")
|
2020-04-15 14:40:05 +00:00
|
|
|
require.NoError(t, buf.Err)
|
|
|
|
|
|
|
|
vm := load(buf.Bytes())
|
2020-03-28 14:25:42 +00:00
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, res, vm.estack.Pop().Bytes())
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:04:14 +00:00
|
|
|
func TestSerializeInterop(t *testing.T) {
|
|
|
|
vm := load(getSerializeProg())
|
2020-06-03 12:55:06 +00:00
|
|
|
item := stackitem.NewInterop("kek")
|
2019-11-05 14:04:14 +00:00
|
|
|
|
|
|
|
vm.estack.Push(&Element{value: item})
|
|
|
|
|
|
|
|
err := vm.Step()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, vm.HasFailed())
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:51:28 +00:00
|
|
|
func getTestCallFlagsFunc(syscall []byte, flags smartcontract.CallFlag, result interface{}) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
script := append([]byte{byte(opcode.SYSCALL)}, syscall...)
|
|
|
|
v := New()
|
|
|
|
v.RegisterInteropGetter(getTestingInterop)
|
|
|
|
v.LoadScriptWithFlags(script, flags)
|
|
|
|
if result == nil {
|
|
|
|
checkVMFailed(t, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runVM(t, v)
|
|
|
|
require.Equal(t, result, v.PopResult())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCallFlags(t *testing.T) {
|
|
|
|
noFlags := []byte{0x77, 0x77, 0x77, 0x77}
|
|
|
|
readOnly := []byte{0x66, 0x66, 0x66, 0x66}
|
|
|
|
t.Run("NoFlagsNoRequired", getTestCallFlagsFunc(noFlags, smartcontract.NoneFlag, new(int)))
|
|
|
|
t.Run("ProvideFlagsNoRequired", getTestCallFlagsFunc(noFlags, smartcontract.AllowCall, new(int)))
|
|
|
|
t.Run("NoFlagsSomeRequired", getTestCallFlagsFunc(readOnly, smartcontract.NoneFlag, nil))
|
|
|
|
t.Run("OnlyOneProvided", getTestCallFlagsFunc(readOnly, smartcontract.AllowCall, nil))
|
|
|
|
t.Run("AllFlagsProvided", getTestCallFlagsFunc(readOnly, smartcontract.ReadOnly, new(int)))
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:57:10 +00:00
|
|
|
func getTestTriggerFunc(syscall []byte, tr trigger.Type, result interface{}) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
script := append([]byte{byte(opcode.SYSCALL)}, syscall...)
|
|
|
|
v := NewWithTrigger(tr)
|
|
|
|
v.RegisterInteropGetter(getTestingInterop)
|
|
|
|
v.LoadScript(script)
|
|
|
|
if result == nil {
|
|
|
|
checkVMFailed(t, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runVM(t, v)
|
|
|
|
require.Equal(t, result, v.PopResult())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllowedTriggers(t *testing.T) {
|
|
|
|
noFlags := []byte{0x77, 0x77, 0x77, 0x77}
|
|
|
|
appOnly := []byte{0x55, 0x55, 0x55, 0x55}
|
|
|
|
t.Run("Application/NeedNothing", getTestTriggerFunc(noFlags, trigger.Application, new(int)))
|
|
|
|
t.Run("Application/NeedApplication", getTestTriggerFunc(appOnly, trigger.Application, new(int)))
|
|
|
|
t.Run("System/NeedApplication", getTestTriggerFunc(appOnly, trigger.System, nil))
|
|
|
|
}
|
|
|
|
|
2019-10-29 08:01:06 +00:00
|
|
|
func callNTimes(n uint16) []byte {
|
|
|
|
return makeProgram(
|
2020-04-21 13:45:48 +00:00
|
|
|
opcode.PUSHINT16, opcode.Opcode(n), opcode.Opcode(n>>8), // little-endian
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.INITSSLOT, 1,
|
|
|
|
opcode.STSFLD0, opcode.LDSFLD0,
|
2020-04-23 08:56:36 +00:00
|
|
|
opcode.JMPIF, 0x3, opcode.RET,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.LDSFLD0, opcode.DEC,
|
2020-04-23 08:56:36 +00:00
|
|
|
opcode.CALL, 0xF9) // -7 -> JMP to TOALTSTACK)
|
2019-10-29 08:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvocationLimitGood(t *testing.T) {
|
|
|
|
prog := callNTimes(MaxInvocationStackSize - 1)
|
|
|
|
v := load(prog)
|
|
|
|
runVM(t, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvocationLimitBad(t *testing.T) {
|
|
|
|
prog := callNTimes(MaxInvocationStackSize)
|
|
|
|
v := load(prog)
|
|
|
|
checkVMFailed(t, v)
|
|
|
|
}
|
|
|
|
|
2020-04-23 08:56:36 +00:00
|
|
|
func isLongJMP(op opcode.Opcode) bool {
|
|
|
|
return op == opcode.JMPL || op == opcode.JMPIFL || op == opcode.JMPIFNOTL ||
|
|
|
|
op == opcode.JMPEQL || op == opcode.JMPNEL ||
|
|
|
|
op == opcode.JMPGEL || op == opcode.JMPGTL ||
|
|
|
|
op == opcode.JMPLEL || op == opcode.JMPLTL
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJMPProgram(op opcode.Opcode) []byte {
|
|
|
|
prog := []byte{byte(op)}
|
|
|
|
if isLongJMP(op) {
|
|
|
|
prog = append(prog, 0x07, 0x00, 0x00, 0x00)
|
|
|
|
} else {
|
|
|
|
prog = append(prog, 0x04)
|
|
|
|
}
|
|
|
|
return append(prog, byte(opcode.PUSH1), byte(opcode.RET), byte(opcode.PUSH2), byte(opcode.RET))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testJMP(t *testing.T, op opcode.Opcode, res interface{}, items ...interface{}) {
|
|
|
|
prog := getJMPProgram(op)
|
|
|
|
v := load(prog)
|
|
|
|
for i := range items {
|
|
|
|
v.estack.PushVal(items[i])
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
checkVMFailed(t, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runVM(t, v)
|
|
|
|
require.EqualValues(t, res, v.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJMPs(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
items []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no condition",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single item (true)",
|
|
|
|
items: []interface{}{true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single item (false)",
|
|
|
|
items: []interface{}{false},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "24 and 42",
|
|
|
|
items: []interface{}{24, 42},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "42 and 24",
|
|
|
|
items: []interface{}{42, 24},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "42 and 42",
|
|
|
|
items: []interface{}{42, 42},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 is true, 1 is false
|
|
|
|
results := map[opcode.Opcode][]interface{}{
|
|
|
|
opcode.JMP: {2, 2, 2, 2, 2, 2},
|
|
|
|
opcode.JMPIF: {nil, 2, 1, 2, 2, 2},
|
|
|
|
opcode.JMPIFNOT: {nil, 1, 2, 1, 1, 1},
|
|
|
|
opcode.JMPEQ: {nil, nil, nil, 1, 1, 2},
|
|
|
|
opcode.JMPNE: {nil, nil, nil, 2, 2, 1},
|
|
|
|
opcode.JMPGE: {nil, nil, nil, 1, 2, 2},
|
|
|
|
opcode.JMPGT: {nil, nil, nil, 1, 2, 1},
|
|
|
|
opcode.JMPLE: {nil, nil, nil, 2, 1, 2},
|
|
|
|
opcode.JMPLT: {nil, nil, nil, 2, 1, 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
i := i
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
for op := opcode.JMP; op < opcode.JMPLEL; op++ {
|
|
|
|
resOp := op
|
|
|
|
if isLongJMP(op) {
|
|
|
|
resOp--
|
|
|
|
}
|
|
|
|
t.Run(op.String(), func(t *testing.T) {
|
|
|
|
testJMP(t, op, results[resOp][i], tc.items...)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:55:30 +00:00
|
|
|
func TestPUSHA(t *testing.T) {
|
|
|
|
t.Run("Negative", getTestFuncForVM(makeProgram(opcode.PUSHA, 0xFF, 0xFF, 0xFF, 0xFF), nil))
|
|
|
|
t.Run("TooBig", getTestFuncForVM(makeProgram(opcode.PUSHA, 10, 0, 0, 0), nil))
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSHA, 2, 0, 0, 0)
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, stackitem.NewPointer(2, prog))
|
2020-05-06 13:55:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCALLA(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.CALLA, opcode.PUSH2, opcode.ADD, opcode.RET, opcode.PUSH3, opcode.RET)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("InvalidScript", getTestFuncForVM(prog, nil, stackitem.NewPointer(4, []byte{1})))
|
|
|
|
t.Run("Good", getTestFuncForVM(prog, 5, stackitem.NewPointer(4, prog)))
|
2020-05-06 13:55:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNOT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NOT)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Bool", getTestFuncForVM(prog, true, false))
|
|
|
|
t.Run("NonZeroInt", getTestFuncForVM(prog, false, 3))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Array", getTestFuncForVM(prog, false, []stackitem.Item{}))
|
|
|
|
t.Run("Struct", getTestFuncForVM(prog, false, stackitem.NewStruct([]stackitem.Item{})))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("ByteArray0", getTestFuncForVM(prog, true, []byte{0, 0}))
|
|
|
|
t.Run("ByteArray1", getTestFuncForVM(prog, false, []byte{0, 1}))
|
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Buffer0", getTestFuncForVM(prog, false, stackitem.NewBuffer([]byte{})))
|
|
|
|
t.Run("Buffer1", getTestFuncForVM(prog, false, stackitem.NewBuffer([]byte{1})))
|
2019-09-10 11:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
// getBigInt returns 2^a+b
|
|
|
|
func getBigInt(a, b int64) *big.Int {
|
|
|
|
p := new(big.Int).Exp(big.NewInt(2), big.NewInt(a), nil)
|
|
|
|
p.Add(p, big.NewInt(b))
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestArith(t *testing.T) {
|
|
|
|
// results for 5 and 2
|
|
|
|
testCases := map[opcode.Opcode]int{
|
|
|
|
opcode.ADD: 7,
|
|
|
|
opcode.MUL: 10,
|
|
|
|
opcode.DIV: 2,
|
|
|
|
opcode.MOD: 1,
|
|
|
|
opcode.SUB: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
for op, res := range testCases {
|
|
|
|
t.Run(op.String(), getTestFuncForVM(makeProgram(op), res, 5, 2))
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
func TestADDBigResult(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.ADD)
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, nil, getBigInt(stackitem.MaxBigIntegerSizeBits, -1), 1)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
func TestMULBigResult(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.MUL)
|
2020-06-03 12:55:06 +00:00
|
|
|
bi := getBigInt(stackitem.MaxBigIntegerSizeBits/2+1, 0)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, bi, bi)
|
2019-11-07 09:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 07:14:26 +00:00
|
|
|
func TestArithNegativeArguments(t *testing.T) {
|
2020-04-05 14:34:14 +00:00
|
|
|
runCase := func(op opcode.Opcode, p, q, result int64) func(t *testing.T) {
|
2020-04-30 08:08:03 +00:00
|
|
|
return getTestFuncForVM(makeProgram(op), result, p, q)
|
2020-03-17 12:45:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 14:34:14 +00:00
|
|
|
t.Run("DIV", func(t *testing.T) {
|
|
|
|
t.Run("positive/positive", runCase(opcode.DIV, 5, 2, 2))
|
|
|
|
t.Run("positive/negative", runCase(opcode.DIV, 5, -2, -2))
|
|
|
|
t.Run("negative/positive", runCase(opcode.DIV, -5, 2, -2))
|
|
|
|
t.Run("negative/negative", runCase(opcode.DIV, -5, -2, 2))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MOD", func(t *testing.T) {
|
|
|
|
t.Run("positive/positive", runCase(opcode.MOD, 5, 2, 1))
|
|
|
|
t.Run("positive/negative", runCase(opcode.MOD, 5, -2, 1))
|
|
|
|
t.Run("negative/positive", runCase(opcode.MOD, -5, 2, -1))
|
|
|
|
t.Run("negative/negative", runCase(opcode.MOD, -5, -2, -1))
|
|
|
|
})
|
2020-03-17 12:45:53 +00:00
|
|
|
|
2020-04-06 07:14:26 +00:00
|
|
|
t.Run("SHR", func(t *testing.T) {
|
|
|
|
t.Run("positive/positive", runCase(opcode.SHR, 5, 2, 1))
|
|
|
|
t.Run("negative/positive", runCase(opcode.SHR, -5, 2, -2))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SHL", func(t *testing.T) {
|
|
|
|
t.Run("positive/positive", runCase(opcode.SHL, 5, 2, 20))
|
|
|
|
t.Run("negative/positive", runCase(opcode.SHL, -5, 2, -20))
|
|
|
|
})
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
func TestSUBBigResult(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SUB)
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, nil, getBigInt(stackitem.MaxBigIntegerSizeBits, -1), -1)
|
2019-09-12 09:02:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSHR(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SHR)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, 1, 4, 2))
|
|
|
|
t.Run("Zero", getTestFuncForVM(prog, []byte{0, 1}, []byte{0, 1}, 0))
|
|
|
|
t.Run("Negative", getTestFuncForVM(prog, nil, 5, -1))
|
2019-09-18 11:35:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSHL(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SHL)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, 16, 4, 2))
|
|
|
|
t.Run("Zero", getTestFuncForVM(prog, []byte{0, 1}, []byte{0, 1}, 0))
|
|
|
|
t.Run("BigShift", getTestFuncForVM(prog, nil, 5, maxSHLArg+1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("BigResult", getTestFuncForVM(prog, nil, getBigInt(stackitem.MaxBigIntegerSizeBits/2, 0), stackitem.MaxBigIntegerSizeBits/2))
|
2019-11-07 09:14:36 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestLT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.LT)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, false, 4, 3)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLTE(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.LTE)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, true, 2, 3)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.GT)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, true, 9, 3)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGTE(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.GTE)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, true, 3, 3)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDepth(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DEPTH)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestEQUALTrue(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.EQUAL)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Array", getTestFuncForVM(prog, true, []stackitem.Item{}))
|
|
|
|
t.Run("Map", getTestFuncForVM(prog, true, stackitem.NewMap()))
|
|
|
|
t.Run("Buffer", getTestFuncForVM(prog, true, stackitem.NewBuffer([]byte{1, 2})))
|
2019-09-18 11:10:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestEQUAL(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.EQUAL)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgs", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("OneArgument", getTestFuncForVM(prog, nil, 1))
|
|
|
|
t.Run("Integer", getTestFuncForVM(prog, true, 5, 5))
|
|
|
|
t.Run("IntegerByteArray", getTestFuncForVM(prog, true, []byte{16}, 16))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Map", getTestFuncForVM(prog, false, stackitem.NewMap(), stackitem.NewMap()))
|
|
|
|
t.Run("Array", getTestFuncForVM(prog, false, []stackitem.Item{}, []stackitem.Item{}))
|
|
|
|
t.Run("Buffer", getTestFuncForVM(prog, false, stackitem.NewBuffer([]byte{42}), stackitem.NewBuffer([]byte{42})))
|
2019-09-18 11:10:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func runWithArgs(t *testing.T, prog []byte, result interface{}, args ...interface{}) {
|
|
|
|
getTestFuncForVM(prog, result, args...)(t)
|
2019-09-24 13:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 09:12:29 +00:00
|
|
|
func getCustomTestFuncForVM(prog []byte, check func(t *testing.T, v *VM), args ...interface{}) func(t *testing.T) {
|
2020-04-30 07:52:29 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
v := load(prog)
|
|
|
|
for i := range args {
|
|
|
|
v.estack.PushVal(args[i])
|
|
|
|
}
|
2020-05-06 09:12:29 +00:00
|
|
|
if check == nil {
|
2020-04-30 08:08:03 +00:00
|
|
|
checkVMFailed(t, v)
|
|
|
|
return
|
|
|
|
}
|
2020-04-30 07:52:29 +00:00
|
|
|
runVM(t, v)
|
2020-05-06 09:12:29 +00:00
|
|
|
check(t, v)
|
2020-04-30 07:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 09:12:29 +00:00
|
|
|
func getTestFuncForVM(prog []byte, result interface{}, args ...interface{}) func(t *testing.T) {
|
|
|
|
var f func(t *testing.T, v *VM)
|
|
|
|
if result != nil {
|
|
|
|
f = func(t *testing.T, v *VM) {
|
|
|
|
require.Equal(t, 1, v.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, stackitem.Make(result), v.estack.Pop().value)
|
2020-05-06 09:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return getCustomTestFuncForVM(prog, f, args...)
|
|
|
|
}
|
|
|
|
|
2020-04-30 07:52:29 +00:00
|
|
|
func TestNOTEQUALByteArray(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.NOTEQUAL)
|
|
|
|
t.Run("True", getTestFuncForVM(prog, true, []byte{1, 2}, []byte{0, 1, 2}))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, []byte{1, 2}, []byte{1, 2}))
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNUMEQUAL(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NUMEQUAL)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, 2, 2))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, 1, 2))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNUMNOTEQUAL(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NUMNOTEQUAL)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, 1, 2))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, 2, 2))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func TestINC(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INC)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, 2, 1)
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
func TestINCBigResult(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INC, opcode.INC)
|
2019-11-07 09:14:36 +00:00
|
|
|
vm := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
x := getBigInt(stackitem.MaxBigIntegerSizeBits, -2)
|
2019-11-07 09:14:36 +00:00
|
|
|
vm.estack.PushVal(x)
|
|
|
|
|
|
|
|
require.NoError(t, vm.Step())
|
|
|
|
require.False(t, vm.HasFailed())
|
|
|
|
require.Equal(t, 1, vm.estack.Len())
|
|
|
|
require.Equal(t, new(big.Int).Add(x, big.NewInt(1)), vm.estack.Top().BigInt())
|
|
|
|
|
|
|
|
checkVMFailed(t, vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDECBigResult(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DEC, opcode.DEC)
|
2019-11-07 09:14:36 +00:00
|
|
|
vm := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
x := getBigInt(stackitem.MaxBigIntegerSizeBits, -2)
|
2019-11-07 09:14:36 +00:00
|
|
|
x.Neg(x)
|
|
|
|
vm.estack.PushVal(x)
|
|
|
|
|
|
|
|
require.NoError(t, vm.Step())
|
|
|
|
require.False(t, vm.HasFailed())
|
|
|
|
require.Equal(t, 1, vm.estack.Len())
|
|
|
|
require.Equal(t, new(big.Int).Sub(x, big.NewInt(1)), vm.estack.Top().BigInt())
|
|
|
|
|
|
|
|
checkVMFailed(t, vm)
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:53:17 +00:00
|
|
|
func TestNEWBUFFER(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.NEWBUFFER)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte{0, 0, 0}), 3))
|
2020-05-12 11:53:17 +00:00
|
|
|
t.Run("Negative", getTestFuncForVM(prog, nil, -1))
|
2020-06-11 13:31:31 +00:00
|
|
|
t.Run("TooBig", getTestFuncForVM(prog, nil, stackitem.MaxSize+1))
|
2020-05-12 11:53:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 11:55:31 +00:00
|
|
|
func TestMEMCPY(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.MEMCPY)
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
buf := stackitem.NewBuffer([]byte{0, 1, 2, 3})
|
|
|
|
runWithArgs(t, prog, stackitem.NewBuffer([]byte{0, 6, 7, 3}), buf, buf, 1, []byte{4, 5, 6, 7}, 2, 2)
|
2020-05-12 11:55:31 +00:00
|
|
|
})
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("NegativeSize", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{0, 1}), 0, []byte{2}, 0, -1))
|
|
|
|
t.Run("NegativeSrcIndex", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{0, 1}), 0, []byte{2}, -1, 1))
|
|
|
|
t.Run("NegativeDstIndex", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{0, 1}), -1, []byte{2}, 0, 1))
|
|
|
|
t.Run("BigSizeSrc", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{0, 1}), 0, []byte{2}, 0, 2))
|
|
|
|
t.Run("BigSizeDst", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{0, 1}), 0, []byte{2, 3, 4}, 0, 3))
|
2020-05-12 11:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:31:59 +00:00
|
|
|
func TestNEWARRAY0(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.NEWARRAY0)
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, []stackitem.Item{})
|
2020-04-24 09:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWSTRUCT0(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.NEWSTRUCT0)
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, stackitem.NewStruct([]stackitem.Item{}))
|
2020-04-24 09:31:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNEWARRAYArray(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NEWARRAY)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("ByteArray", getTestFuncForVM(prog, stackitem.NewArray([]stackitem.Item{}), []byte{}))
|
2020-06-11 13:31:31 +00:00
|
|
|
t.Run("BadSize", getTestFuncForVM(prog, nil, stackitem.MaxArraySize+1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Integer", getTestFuncForVM(prog, []stackitem.Item{stackitem.Null{}}, 1))
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func testNEWARRAYIssue437(t *testing.T, i1 opcode.Opcode, t2 stackitem.Type, appended bool) {
|
2019-10-18 10:01:51 +00:00
|
|
|
prog := makeProgram(
|
2019-12-03 14:05:06 +00:00
|
|
|
opcode.PUSH2, i1,
|
|
|
|
opcode.DUP, opcode.PUSH3, opcode.APPEND,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.INITSSLOT, 1,
|
2020-05-20 14:28:23 +00:00
|
|
|
opcode.STSFLD0, opcode.LDSFLD0, opcode.CONVERT, opcode.Opcode(t2),
|
2019-12-03 14:05:06 +00:00
|
|
|
opcode.DUP, opcode.PUSH4, opcode.APPEND,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.LDSFLD0, opcode.PUSH5, opcode.APPEND)
|
2019-10-18 10:01:51 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := makeArrayOfType(4, stackitem.AnyT)
|
|
|
|
arr[2] = stackitem.Make(3)
|
|
|
|
arr[3] = stackitem.Make(4)
|
2019-10-18 10:01:51 +00:00
|
|
|
if appended {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr = append(arr, stackitem.Make(5))
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
if t2 == stackitem.ArrayT {
|
|
|
|
runWithArgs(t, prog, stackitem.NewArray(arr))
|
2019-10-18 10:01:51 +00:00
|
|
|
} else {
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, stackitem.NewStruct(arr))
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWARRAYIssue437(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Array+Array", func(t *testing.T) { testNEWARRAYIssue437(t, opcode.NEWARRAY, stackitem.ArrayT, true) })
|
|
|
|
t.Run("Struct+Struct", func(t *testing.T) { testNEWARRAYIssue437(t, opcode.NEWSTRUCT, stackitem.StructT, true) })
|
|
|
|
t.Run("Array+Struct", func(t *testing.T) { testNEWARRAYIssue437(t, opcode.NEWARRAY, stackitem.StructT, false) })
|
|
|
|
t.Run("Struct+Array", func(t *testing.T) { testNEWARRAYIssue437(t, opcode.NEWSTRUCT, stackitem.ArrayT, false) })
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:48:19 +00:00
|
|
|
func TestNEWARRAYT(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
testCases := map[stackitem.Type]stackitem.Item{
|
|
|
|
stackitem.BooleanT: stackitem.NewBool(false),
|
|
|
|
stackitem.IntegerT: stackitem.NewBigInteger(big.NewInt(0)),
|
|
|
|
stackitem.ByteArrayT: stackitem.NewByteArray([]byte{}),
|
|
|
|
stackitem.ArrayT: stackitem.Null{},
|
|
|
|
0xFF: nil,
|
2020-04-24 10:48:19 +00:00
|
|
|
}
|
|
|
|
for typ, item := range testCases {
|
2020-04-30 08:08:03 +00:00
|
|
|
prog := makeProgram(opcode.NEWARRAYT, opcode.Opcode(typ), opcode.PUSH0, opcode.PICKITEM)
|
|
|
|
t.Run(typ.String(), getTestFuncForVM(prog, item, 1))
|
2020-04-24 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNEWSTRUCT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NEWSTRUCT)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("ByteArray", getTestFuncForVM(prog, stackitem.NewStruct([]stackitem.Item{}), []byte{}))
|
2020-06-11 13:31:31 +00:00
|
|
|
t.Run("BadSize", getTestFuncForVM(prog, nil, stackitem.MaxArraySize+1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Integer", getTestFuncForVM(prog, stackitem.NewStruct([]stackitem.Item{stackitem.Null{}}), 1))
|
2019-09-11 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestAPPEND(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.PUSH5, opcode.APPEND)
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{stackitem.Make(5)}
|
|
|
|
t.Run("Array", getTestFuncForVM(prog, stackitem.NewArray(arr), stackitem.NewArray(nil)))
|
|
|
|
t.Run("Struct", getTestFuncForVM(prog, stackitem.NewStruct(arr), stackitem.NewStruct(nil)))
|
2019-09-11 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:03:15 +00:00
|
|
|
func TestAPPENDCloneStruct(t *testing.T) {
|
2020-05-07 08:54:35 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.NEWSTRUCT, opcode.INITSSLOT, 1, opcode.STSFLD0,
|
|
|
|
opcode.LDSFLD0, opcode.APPEND, opcode.LDSFLD0, opcode.PUSH1, opcode.APPEND)
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{stackitem.NewStruct([]stackitem.Item{})}
|
|
|
|
runWithArgs(t, prog, stackitem.NewArray(arr), stackitem.NewArray(nil))
|
2019-09-11 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestAPPENDBad(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.APPEND)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("no arguments", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("one argument", getTestFuncForVM(prog, nil, 1))
|
|
|
|
t.Run("wrong type", getTestFuncForVM(prog, nil, []byte{}, 1))
|
2019-09-11 14:08:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 08:03:35 +00:00
|
|
|
func TestAPPENDGoodSizeLimit(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NEWARRAY, opcode.DUP, opcode.PUSH0, opcode.APPEND)
|
2019-10-17 08:03:35 +00:00
|
|
|
vm := load(prog)
|
2020-06-11 13:31:31 +00:00
|
|
|
vm.estack.PushVal(stackitem.MaxArraySize - 1)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-10-17 08:03:35 +00:00
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
2020-06-11 13:31:31 +00:00
|
|
|
assert.Equal(t, stackitem.MaxArraySize, len(vm.estack.Pop().Array()))
|
2019-10-17 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPPENDBadSizeLimit(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NEWARRAY, opcode.DUP, opcode.PUSH0, opcode.APPEND)
|
2020-06-11 13:31:31 +00:00
|
|
|
runWithArgs(t, prog, nil, stackitem.MaxArraySize)
|
2019-09-12 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPICKITEM(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PICKITEM)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("bad index", getTestFuncForVM(prog, nil, []stackitem.Item{}, 0))
|
|
|
|
t.Run("Array", getTestFuncForVM(prog, 2, []stackitem.Item{stackitem.Make(1), stackitem.Make(2)}, 1))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("ByteArray", getTestFuncForVM(prog, 2, []byte{1, 2}, 1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Buffer", getTestFuncForVM(prog, 2, stackitem.NewBuffer([]byte{1, 2}), 1))
|
2019-09-12 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:20:44 +00:00
|
|
|
func TestPICKITEMDupArray(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.PICKITEM, opcode.ABS)
|
|
|
|
vm := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
vm.estack.PushVal([]stackitem.Item{stackitem.Make(-1)})
|
2019-12-17 14:20:44 +00:00
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
2020-06-03 12:55:06 +00:00
|
|
|
items := vm.estack.Pop().Value().([]stackitem.Item)
|
2019-12-17 14:20:44 +00:00
|
|
|
assert.Equal(t, big.NewInt(-1), items[0].Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKITEMDupMap(t *testing.T) {
|
2020-04-21 13:45:48 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.PUSHINT8, 42, opcode.PICKITEM, opcode.ABS)
|
2019-12-17 14:20:44 +00:00
|
|
|
vm := load(prog)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make([]byte{42}), stackitem.Make(-1))
|
2019-12-17 14:20:44 +00:00
|
|
|
vm.estack.Push(&Element{value: m})
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
2020-06-03 12:55:06 +00:00
|
|
|
items := vm.estack.Pop().Value().([]stackitem.MapElement)
|
2020-03-31 10:30:38 +00:00
|
|
|
assert.Equal(t, 1, len(items))
|
|
|
|
assert.Equal(t, []byte{42}, items[0].Key.Value())
|
|
|
|
assert.Equal(t, big.NewInt(-1), items[0].Value.Value())
|
2019-12-17 14:20:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:58:31 +00:00
|
|
|
func TestPICKITEMMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PICKITEM)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(3))
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, 3, m, 5)
|
2019-09-24 12:58:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 11:47:33 +00:00
|
|
|
func TestSETITEMBuffer(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.DUP, opcode.REVERSE4, opcode.SETITEM)
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte{0, 42, 2}), 42, 1, stackitem.NewBuffer([]byte{0, 1, 2})))
|
|
|
|
t.Run("BadIndex", getTestFuncForVM(prog, nil, 42, -1, stackitem.NewBuffer([]byte{0, 1, 2})))
|
|
|
|
t.Run("BadValue", getTestFuncForVM(prog, nil, 256, 1, stackitem.NewBuffer([]byte{0, 1, 2})))
|
2020-05-12 11:47:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:58:31 +00:00
|
|
|
func TestSETITEMMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SETITEM, opcode.PICKITEM)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(3))
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, []byte{0, 1}, m, 5, m, 5, []byte{0, 1})
|
2019-09-24 12:58:31 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 08:18:15 +00:00
|
|
|
func TestSETITEMBigMapBad(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SETITEM)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
2020-06-11 13:31:31 +00:00
|
|
|
for i := 0; i < stackitem.MaxArraySize; i++ {
|
2020-06-03 12:55:06 +00:00
|
|
|
m.Add(stackitem.Make(i), stackitem.Make(i))
|
2019-10-17 08:18:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 13:31:31 +00:00
|
|
|
runWithArgs(t, prog, nil, m, stackitem.MaxArraySize, 0)
|
2019-10-17 08:18:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 11:57:16 +00:00
|
|
|
// This test checks is SETITEM properly updates reference counter.
|
|
|
|
// 1. Create 2 arrays of size MaxArraySize - 3. (MaxStackSize = 2 * MaxArraySize)
|
|
|
|
// 2. SETITEM each of them to a map.
|
|
|
|
// 3. Replace each of them with a scalar value.
|
|
|
|
func TestSETITEMMapStackLimit(t *testing.T) {
|
2020-06-11 13:31:31 +00:00
|
|
|
size := stackitem.MaxArraySize - 3
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewArray(makeArrayOfType(size, stackitem.BooleanT)))
|
|
|
|
m.Add(stackitem.NewBigInteger(big.NewInt(2)), stackitem.NewArray(makeArrayOfType(size, stackitem.BooleanT)))
|
2020-05-21 11:57:16 +00:00
|
|
|
|
|
|
|
prog := makeProgram(
|
|
|
|
opcode.DUP, opcode.PUSH1, opcode.PUSH1, opcode.SETITEM,
|
|
|
|
opcode.DUP, opcode.PUSH2, opcode.PUSH2, opcode.SETITEM,
|
|
|
|
opcode.DUP, opcode.PUSH3, opcode.PUSH3, opcode.SETITEM,
|
|
|
|
opcode.DUP, opcode.PUSH4, opcode.PUSH4, opcode.SETITEM)
|
|
|
|
v := load(prog)
|
|
|
|
v.estack.PushVal(m)
|
|
|
|
runVM(t, v)
|
|
|
|
}
|
|
|
|
|
2019-10-17 08:18:15 +00:00
|
|
|
func TestSETITEMBigMapGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SETITEM)
|
2019-10-17 08:18:15 +00:00
|
|
|
vm := load(prog)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
2020-06-11 13:31:31 +00:00
|
|
|
for i := 0; i < stackitem.MaxArraySize; i++ {
|
2020-06-03 12:55:06 +00:00
|
|
|
m.Add(stackitem.Make(i), stackitem.Make(i))
|
2019-10-17 08:18:15 +00:00
|
|
|
}
|
|
|
|
vm.estack.Push(&Element{value: m})
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-10-17 08:18:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSIZE(t *testing.T) {
|
2020-04-24 09:24:08 +00:00
|
|
|
prog := makeProgram(opcode.SIZE)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("ByteArray", getTestFuncForVM(prog, 2, []byte{0, 1}))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Buffer", getTestFuncForVM(prog, 2, stackitem.NewBuffer([]byte{0, 1})))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Bool", getTestFuncForVM(prog, 1, false))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Array", getTestFuncForVM(prog, 2, []stackitem.Item{stackitem.Make(1), stackitem.Make([]byte{})}))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Map", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(6))
|
|
|
|
m.Add(stackitem.Make([]byte{0, 1}), stackitem.Make(6))
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, 2, m)
|
2019-09-24 13:50:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:41:39 +00:00
|
|
|
func TestKEYSMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.KEYS)
|
2019-09-24 12:41:39 +00:00
|
|
|
vm := load(prog)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(6))
|
|
|
|
m.Add(stackitem.Make([]byte{0, 1}), stackitem.Make(6))
|
2019-09-24 12:41:39 +00:00
|
|
|
vm.estack.Push(&Element{value: m})
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-24 12:41:39 +00:00
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
top := vm.estack.Pop().value.(*stackitem.Array)
|
|
|
|
assert.Equal(t, 2, len(top.Value().([]stackitem.Item)))
|
|
|
|
assert.Contains(t, top.Value().([]stackitem.Item), stackitem.Make(5))
|
|
|
|
assert.Contains(t, top.Value().([]stackitem.Item), stackitem.Make([]byte{0, 1}))
|
2019-09-24 12:41:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestKEYS(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.KEYS)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("WrongType", getTestFuncForVM(prog, nil, []stackitem.Item{}))
|
2019-09-24 12:41:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:53:55 +00:00
|
|
|
func TestVALUESMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.VALUES)
|
2019-09-24 12:53:55 +00:00
|
|
|
vm := load(prog)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make([]byte{2, 3}))
|
|
|
|
m.Add(stackitem.Make([]byte{0, 1}), stackitem.Make([]stackitem.Item{}))
|
2019-09-24 12:53:55 +00:00
|
|
|
vm.estack.Push(&Element{value: m})
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-24 12:53:55 +00:00
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
top := vm.estack.Pop().value.(*stackitem.Array)
|
|
|
|
assert.Equal(t, 2, len(top.Value().([]stackitem.Item)))
|
|
|
|
assert.Contains(t, top.Value().([]stackitem.Item), stackitem.Make([]byte{2, 3}))
|
|
|
|
assert.Contains(t, top.Value().([]stackitem.Item), stackitem.Make([]stackitem.Item{}))
|
2019-09-24 12:53:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestVALUES(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.VALUES)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("WrongType", getTestFuncForVM(prog, nil, 5))
|
|
|
|
t.Run("Array", getTestFuncForVM(prog, []int{4}, []int{4}))
|
2019-09-24 12:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestHASKEY(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.HASKEY)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("OneArgument", getTestFuncForVM(prog, nil, 1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("WrongKeyType", getTestFuncForVM(prog, nil, []stackitem.Item{}, []stackitem.Item{}))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("WrongCollectionType", getTestFuncForVM(prog, nil, 1, 2))
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := makeArrayOfType(5, stackitem.BooleanT)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Array", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, stackitem.NewArray(arr), 4))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, stackitem.NewArray(arr), 5))
|
2020-04-30 08:08:03 +00:00
|
|
|
})
|
|
|
|
t.Run("Struct", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, stackitem.NewStruct(arr), 4))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, stackitem.NewStruct(arr), 5))
|
2020-04-30 08:08:03 +00:00
|
|
|
})
|
2020-05-12 11:47:33 +00:00
|
|
|
|
|
|
|
t.Run("Buffer", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, stackitem.NewBuffer([]byte{5, 5, 5}), 2))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, stackitem.NewBuffer([]byte{5, 5, 5}), 3))
|
|
|
|
t.Run("Negative", getTestFuncForVM(prog, nil, stackitem.NewBuffer([]byte{5, 5, 5}), -1))
|
2020-05-12 11:47:33 +00:00
|
|
|
})
|
2019-09-24 12:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestHASKEYMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.HASKEY)
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(6))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, m, 5))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, m, 6))
|
2019-09-24 12:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSIGN(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SIGN)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("WrongType", getTestFuncForVM(prog, nil, []stackitem.Item{}))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("Bool", getTestFuncForVM(prog, 0, false))
|
|
|
|
t.Run("PositiveInt", getTestFuncForVM(prog, 1, 2))
|
|
|
|
t.Run("NegativeInt", getTestFuncForVM(prog, -1, -1))
|
|
|
|
t.Run("Zero", getTestFuncForVM(prog, 0, 0))
|
|
|
|
t.Run("ByteArray", getTestFuncForVM(prog, 1, []byte{0, 1}))
|
2019-09-10 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestSimpleCall(t *testing.T) {
|
2020-04-21 13:56:58 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
w := buf.BinWriter
|
|
|
|
emit.Opcode(w, opcode.PUSH2)
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Instruction(w, opcode.CALL, []byte{03})
|
2020-04-21 13:56:58 +00:00
|
|
|
emit.Opcode(w, opcode.RET)
|
|
|
|
emit.Opcode(w, opcode.PUSH10)
|
|
|
|
emit.Opcode(w, opcode.ADD)
|
|
|
|
emit.Opcode(w, opcode.RET)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-04-21 13:56:58 +00:00
|
|
|
result := 12
|
|
|
|
vm := load(buf.Bytes())
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64()))
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestNZ(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NZ)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("True", getTestFuncForVM(prog, true, 1))
|
|
|
|
t.Run("False", getTestFuncForVM(prog, false, 0))
|
2019-09-05 12:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPICK(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PICK)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoItem", getTestFuncForVM(prog, nil, 1))
|
|
|
|
t.Run("Negative", getTestFuncForVM(prog, nil, -1))
|
2019-09-05 12:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKgood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PICK)
|
2019-09-05 12:18:04 +00:00
|
|
|
result := 2
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(result)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(5)
|
|
|
|
vm.estack.PushVal(3)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 12:18:04 +00:00
|
|
|
assert.Equal(t, int64(result), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:56:58 +00:00
|
|
|
func TestPICKDup(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSHM1, opcode.PUSH0,
|
|
|
|
opcode.PUSH1,
|
|
|
|
opcode.PUSH2,
|
|
|
|
opcode.PICK,
|
|
|
|
opcode.ABS)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 4, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(0), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(-1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-12 07:57:55 +00:00
|
|
|
func TestROTBad(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.ROT)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1, 2)
|
2019-09-12 07:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestROTGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.ROT)
|
2019-09-12 07:57:55 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-12 07:57:55 +00:00
|
|
|
assert.Equal(t, 3, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.Equal(t, stackitem.Make(1), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(3), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(2), vm.estack.Pop().value)
|
2019-12-16 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestROLLBad1(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.ROLL)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1, -1)
|
2019-12-16 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestROLLBad2(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.ROLL)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1, 2, 3, 3)
|
2019-12-16 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestROLLGood(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.ROLL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(1)
|
2020-04-30 08:08:03 +00:00
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 4, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.Equal(t, stackitem.Make(3), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(4), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(2), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(1), vm.estack.Pop().value)
|
2019-09-05 12:43:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 09:12:29 +00:00
|
|
|
func getCheckEStackFunc(items ...interface{}) func(t *testing.T, v *VM) {
|
|
|
|
return func(t *testing.T, v *VM) {
|
|
|
|
require.Equal(t, len(items), v.estack.Len())
|
|
|
|
for i := 0; i < len(items); i++ {
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.Equal(t, stackitem.Make(items[i]), v.estack.Peek(i).Item())
|
2020-05-06 09:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSE3(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.REVERSE3)
|
|
|
|
t.Run("SmallStack", getTestFuncForVM(prog, nil, 1, 2))
|
|
|
|
t.Run("Good", getCustomTestFuncForVM(prog, getCheckEStackFunc(1, 2, 3), 1, 2, 3))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSE4(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.REVERSE4)
|
|
|
|
t.Run("SmallStack", getTestFuncForVM(prog, nil, 1, 2, 3))
|
|
|
|
t.Run("Good", getCustomTestFuncForVM(prog, getCheckEStackFunc(1, 2, 3, 4), 1, 2, 3, 4))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSEN(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.REVERSEN)
|
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("SmallStack", getTestFuncForVM(prog, nil, 1, 2, 3))
|
|
|
|
t.Run("NegativeArgument", getTestFuncForVM(prog, nil, 1, 2, -1))
|
|
|
|
t.Run("Zero", getCustomTestFuncForVM(prog, getCheckEStackFunc(3, 2, 1), 1, 2, 3, 0))
|
|
|
|
t.Run("OneItem", getCustomTestFuncForVM(prog, getCheckEStackFunc(42), 42, 1))
|
|
|
|
t.Run("Good", getCustomTestFuncForVM(prog, getCheckEStackFunc(1, 2, 3, 4, 5), 1, 2, 3, 4, 5, 5))
|
|
|
|
}
|
|
|
|
|
2019-09-05 13:34:35 +00:00
|
|
|
func TestTUCKbadNoitems(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.TUCK)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("NoItem", getTestFuncForVM(prog, nil, 1))
|
2019-09-05 13:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTUCKgood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.TUCK)
|
2019-09-05 13:34:35 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 13:34:35 +00:00
|
|
|
assert.Equal(t, int64(34), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(42), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(34), vm.estack.Peek(2).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTUCKgood2(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.TUCK)
|
2019-09-05 13:34:35 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(11)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 13:34:35 +00:00
|
|
|
assert.Equal(t, int64(34), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(42), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(34), vm.estack.Peek(2).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(11), vm.estack.Peek(3).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestOVER(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.OVER)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("NoItem", getTestFuncForVM(prog, nil, 1))
|
2019-09-05 12:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOVERgood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.OVER)
|
2019-09-05 12:57:44 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 12:57:44 +00:00
|
|
|
assert.Equal(t, int64(42), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(34), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(42), vm.estack.Peek(2).BigInt().Int64())
|
|
|
|
assert.Equal(t, 3, vm.estack.Len())
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:56:58 +00:00
|
|
|
func TestOVERDup(t *testing.T) {
|
2020-04-21 13:45:48 +00:00
|
|
|
prog := makeProgram(opcode.PUSHDATA1, 2, 1, 0,
|
2019-12-17 13:56:58 +00:00
|
|
|
opcode.PUSH1,
|
|
|
|
opcode.OVER,
|
|
|
|
opcode.PUSH1,
|
|
|
|
opcode.LEFT,
|
2020-04-21 13:45:48 +00:00
|
|
|
opcode.PUSHDATA1, 1, 2,
|
2019-12-17 13:56:58 +00:00
|
|
|
opcode.CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 3, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte{0x01, 0x02}, vm.estack.Pop().Bytes())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, []byte{0x01, 0x00}, vm.estack.Pop().Bytes())
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:09:23 +00:00
|
|
|
func TestNIPBadNoItem(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NIP)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1)
|
2019-09-12 08:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNIPGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.NIP)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, 2, 1, 2)
|
2019-09-12 08:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 08:05:10 +00:00
|
|
|
func TestDROPBadNoItem(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DROP)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil)
|
2019-09-12 08:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDROPGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DROP)
|
2019-09-12 08:05:10 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-12 08:05:10 +00:00
|
|
|
assert.Equal(t, 0, vm.estack.Len())
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestXDROP(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.XDROP)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("NoN", getTestFuncForVM(prog, nil, 1, 2))
|
|
|
|
t.Run("Negative", getTestFuncForVM(prog, nil, 1, -1))
|
2019-09-05 13:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestXDROPgood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.XDROP)
|
2019-09-05 13:42:54 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(2)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 13:42:54 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(2), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
}
|
2019-09-05 12:57:44 +00:00
|
|
|
|
2020-05-06 08:32:22 +00:00
|
|
|
func TestCLEAR(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.CLEAR)
|
|
|
|
v := load(prog)
|
|
|
|
v.estack.PushVal(123)
|
|
|
|
require.Equal(t, 1, v.estack.Len())
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
require.Equal(t, 0, v.estack.Len())
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:20:53 +00:00
|
|
|
func TestINVERTbadNoitem(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INVERT)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil)
|
2019-09-05 14:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood1(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INVERT)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, -1, 0)
|
2019-09-05 14:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood2(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INVERT)
|
2019-09-05 14:20:53 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 14:20:53 +00:00
|
|
|
assert.Equal(t, int64(0), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood3(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.INVERT)
|
2019-09-05 14:20:53 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0x69)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-05 14:20:53 +00:00
|
|
|
assert.Equal(t, int64(-0x6A), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-12-17 11:01:15 +00:00
|
|
|
func TestINVERTWithConversion1(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSHDATA2, 0, 0, opcode.INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, int64(-1), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTWithConversion2(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSH0, opcode.PUSH1, opcode.NUMEQUAL, opcode.INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, int64(-1), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestCAT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.CAT)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("OneArgument", getTestFuncForVM(prog, nil, []byte("abc")))
|
|
|
|
t.Run("BigItem", func(t *testing.T) {
|
2020-06-11 13:31:31 +00:00
|
|
|
arg := make([]byte, stackitem.MaxSize/2+1)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, arg, arg)
|
|
|
|
})
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte("abcdef")), []byte("abc"), []byte("def")))
|
|
|
|
t.Run("Int0ByteArray", getTestFuncForVM(prog, stackitem.NewBuffer([]byte{}), 0, []byte{}))
|
|
|
|
t.Run("ByteArrayInt1", getTestFuncForVM(prog, stackitem.NewBuffer([]byte{1}), []byte{}, 1))
|
2019-09-06 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSUBSTR(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SUBSTR)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("OneArgument", getTestFuncForVM(prog, nil, 1))
|
|
|
|
t.Run("TwoArguments", getTestFuncForVM(prog, nil, 0, 2))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte("bc")), []byte("abcdef"), 1, 2))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("BadOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), 7, 1))
|
|
|
|
t.Run("BigLen", getTestFuncForVM(prog, nil, []byte("abcdef"), 1, 6))
|
|
|
|
t.Run("NegativeOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), -1, 3))
|
|
|
|
t.Run("NegativeLen", getTestFuncForVM(prog, nil, []byte("abcdef"), 3, -1))
|
2019-09-06 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:00:11 +00:00
|
|
|
func TestSUBSTRBad387(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.SUBSTR)
|
2019-09-11 09:00:11 +00:00
|
|
|
vm := load(prog)
|
|
|
|
b := make([]byte, 6, 20)
|
|
|
|
copy(b, "abcdef")
|
|
|
|
vm.estack.PushVal(b)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(6)
|
2020-04-15 13:16:49 +00:00
|
|
|
checkVMFailed(t, vm)
|
2019-09-11 09:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestLEFT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.LEFT)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("NoString", getTestFuncForVM(prog, nil, 2))
|
|
|
|
t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte("ab")), "abcdef", 2))
|
2020-05-21 06:43:35 +00:00
|
|
|
t.Run("BadBigLen", getTestFuncForVM(prog, nil, "abcdef", 8))
|
2019-09-06 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestRIGHT(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.RIGHT)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("NoString", getTestFuncForVM(prog, nil, 2))
|
|
|
|
t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good", getTestFuncForVM(prog, stackitem.NewBuffer([]byte("ef")), "abcdef", 2))
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("BadLen", getTestFuncForVM(prog, nil, "abcdef", 8))
|
2019-09-06 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestPACK(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PACK)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("BadLen", getTestFuncForVM(prog, nil, 1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Good0Len", getTestFuncForVM(prog, []stackitem.Item{}, 0))
|
2019-09-06 07:33:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 08:07:44 +00:00
|
|
|
func TestPACKBigLen(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PACK)
|
2019-10-17 08:07:44 +00:00
|
|
|
vm := load(prog)
|
2020-06-11 13:31:31 +00:00
|
|
|
for i := 0; i <= stackitem.MaxArraySize; i++ {
|
2019-10-17 08:07:44 +00:00
|
|
|
vm.estack.PushVal(0)
|
|
|
|
}
|
2020-06-11 13:31:31 +00:00
|
|
|
vm.estack.PushVal(stackitem.MaxArraySize + 1)
|
2019-10-22 10:44:14 +00:00
|
|
|
checkVMFailed(t, vm)
|
2019-10-17 08:07:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 07:33:43 +00:00
|
|
|
func TestPACKGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.PACK)
|
2019-09-06 07:33:43 +00:00
|
|
|
elements := []int{55, 34, 42}
|
|
|
|
vm := load(prog)
|
|
|
|
// canary
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
for i := len(elements) - 1; i >= 0; i-- {
|
|
|
|
vm.estack.PushVal(elements[i])
|
|
|
|
}
|
|
|
|
vm.estack.PushVal(len(elements))
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-06 07:33:43 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
a := vm.estack.Peek(0).Array()
|
|
|
|
assert.Equal(t, len(elements), len(a))
|
|
|
|
for i := 0; i < len(elements); i++ {
|
|
|
|
e := a[i].Value().(*big.Int)
|
|
|
|
assert.Equal(t, int64(elements[i]), e.Int64())
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:32:20 +00:00
|
|
|
func TestUNPACKBadNotArray(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.UNPACK)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1)
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUNPACKGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.UNPACK)
|
2019-09-06 08:32:20 +00:00
|
|
|
elements := []int{55, 34, 42}
|
|
|
|
vm := load(prog)
|
|
|
|
// canary
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-06 08:32:20 +00:00
|
|
|
assert.Equal(t, 5, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(len(elements)), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
for k, v := range elements {
|
2019-09-09 08:23:27 +00:00
|
|
|
assert.Equal(t, int64(v), vm.estack.Peek(k+1).BigInt().Int64())
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
2019-09-09 08:23:27 +00:00
|
|
|
assert.Equal(t, int64(1), vm.estack.Peek(len(elements)+1).BigInt().Int64())
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 11:47:33 +00:00
|
|
|
func TestREVERSEITEMS(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.DUP, opcode.REVERSEITEMS)
|
|
|
|
t.Run("InvalidItem", getTestFuncForVM(prog, nil, 1))
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Buffer", getTestFuncForVM(prog, stackitem.NewBuffer([]byte{3, 2, 1}), stackitem.NewBuffer([]byte{1, 2, 3})))
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func testREVERSEITEMSIssue437(t *testing.T, i1 opcode.Opcode, t2 stackitem.Type, reversed bool) {
|
2019-10-18 10:01:51 +00:00
|
|
|
prog := makeProgram(
|
2019-12-03 14:05:06 +00:00
|
|
|
opcode.PUSH0, i1,
|
|
|
|
opcode.DUP, opcode.PUSH1, opcode.APPEND,
|
|
|
|
opcode.DUP, opcode.PUSH2, opcode.APPEND,
|
2020-05-20 14:28:23 +00:00
|
|
|
opcode.DUP, opcode.CONVERT, opcode.Opcode(t2), opcode.REVERSEITEMS)
|
2019-10-18 10:01:51 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := make([]stackitem.Item, 2)
|
2019-10-18 10:01:51 +00:00
|
|
|
if reversed {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr[0] = stackitem.Make(2)
|
|
|
|
arr[1] = stackitem.Make(1)
|
2019-10-18 10:01:51 +00:00
|
|
|
} else {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr[0] = stackitem.Make(1)
|
|
|
|
arr[1] = stackitem.Make(2)
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
2020-05-20 14:28:23 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if i1 == opcode.NEWARRAY {
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, stackitem.NewArray(arr))
|
2019-10-18 10:01:51 +00:00
|
|
|
} else {
|
2020-06-03 12:55:06 +00:00
|
|
|
runWithArgs(t, prog, stackitem.NewStruct(arr))
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:24:08 +00:00
|
|
|
func TestREVERSEITEMSIssue437(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("Array+Array", func(t *testing.T) { testREVERSEITEMSIssue437(t, opcode.NEWARRAY, stackitem.ArrayT, true) })
|
|
|
|
t.Run("Struct+Struct", func(t *testing.T) { testREVERSEITEMSIssue437(t, opcode.NEWSTRUCT, stackitem.StructT, true) })
|
|
|
|
t.Run("Array+Struct", func(t *testing.T) { testREVERSEITEMSIssue437(t, opcode.NEWARRAY, stackitem.StructT, false) })
|
|
|
|
t.Run("Struct+Array", func(t *testing.T) { testREVERSEITEMSIssue437(t, opcode.NEWSTRUCT, stackitem.ArrayT, false) })
|
2019-10-18 10:01:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:24:08 +00:00
|
|
|
func TestREVERSEITEMSGoodOneElem(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.DUP, opcode.REVERSEITEMS)
|
2019-09-06 08:32:20 +00:00
|
|
|
elements := []int{22}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-06 08:32:20 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
a := vm.estack.Peek(0).Array()
|
|
|
|
assert.Equal(t, len(elements), len(a))
|
|
|
|
e := a[0].Value().(*big.Int)
|
|
|
|
assert.Equal(t, int64(elements[0]), e.Int64())
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:24:08 +00:00
|
|
|
func TestREVERSEITEMSGoodStruct(t *testing.T) {
|
2019-09-12 13:54:22 +00:00
|
|
|
eodd := []int{22, 34, 42, 55, 81}
|
|
|
|
even := []int{22, 34, 42, 55, 81, 99}
|
|
|
|
eall := [][]int{eodd, even}
|
|
|
|
|
|
|
|
for _, elements := range eall {
|
2020-04-24 09:24:08 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.REVERSEITEMS)
|
2019-09-12 13:54:22 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := make([]stackitem.Item, len(elements))
|
2019-09-12 13:54:22 +00:00
|
|
|
for i := range elements {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr[i] = stackitem.Make(elements[i])
|
2019-09-12 13:54:22 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
vm.estack.Push(&Element{value: stackitem.NewStruct(arr)})
|
2019-09-12 13:54:22 +00:00
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-12 13:54:22 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
a := vm.estack.Peek(0).Array()
|
|
|
|
assert.Equal(t, len(elements), len(a))
|
|
|
|
for k, v := range elements {
|
|
|
|
e := a[len(a)-1-k].Value().(*big.Int)
|
|
|
|
assert.Equal(t, int64(v), e.Int64())
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:24:08 +00:00
|
|
|
func TestREVERSEITEMSGood(t *testing.T) {
|
2019-09-06 08:32:20 +00:00
|
|
|
eodd := []int{22, 34, 42, 55, 81}
|
|
|
|
even := []int{22, 34, 42, 55, 81, 99}
|
|
|
|
eall := [][]int{eodd, even}
|
|
|
|
|
|
|
|
for _, elements := range eall {
|
2020-04-24 09:24:08 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.REVERSEITEMS)
|
2019-09-06 08:32:20 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-06 08:32:20 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
a := vm.estack.Peek(0).Array()
|
|
|
|
assert.Equal(t, len(elements), len(a))
|
|
|
|
for k, v := range elements {
|
2019-09-09 08:23:27 +00:00
|
|
|
e := a[len(a)-1-k].Value().(*big.Int)
|
2019-09-06 08:32:20 +00:00
|
|
|
assert.Equal(t, int64(v), e.Int64())
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestREMOVE(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.REMOVE)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("NoArgument", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("OneArgument", getTestFuncForVM(prog, nil, 1))
|
|
|
|
t.Run("NotArray", getTestFuncForVM(prog, nil, 1, 1))
|
|
|
|
t.Run("BadIndex", getTestFuncForVM(prog, nil, []int{22, 34, 42, 55, 81}, 10))
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEGood(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.PUSH2, opcode.REMOVE)
|
2019-09-06 08:32:20 +00:00
|
|
|
elements := []int{22, 34, 42, 55, 81}
|
|
|
|
reselements := []int{22, 34, 55, 81}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-06 08:32:20 +00:00
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.Equal(t, stackitem.Make(reselements), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, stackitem.Make(1), vm.estack.Pop().value)
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:16:24 +00:00
|
|
|
func TestREMOVEMap(t *testing.T) {
|
2019-12-03 14:05:06 +00:00
|
|
|
prog := makeProgram(opcode.REMOVE, opcode.PUSH5, opcode.HASKEY)
|
2019-09-24 13:16:24 +00:00
|
|
|
vm := load(prog)
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.Make(5), stackitem.Make(3))
|
|
|
|
m.Add(stackitem.Make([]byte{0, 1}), stackitem.Make([]byte{2, 3}))
|
2019-09-24 13:16:24 +00:00
|
|
|
vm.estack.Push(&Element{value: m})
|
|
|
|
vm.estack.Push(&Element{value: m})
|
2020-06-03 12:55:06 +00:00
|
|
|
vm.estack.PushVal(stackitem.Make(5))
|
2019-09-24 13:16:24 +00:00
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
runVM(t, vm)
|
2019-09-24 13:16:24 +00:00
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
2020-06-03 12:55:06 +00:00
|
|
|
assert.Equal(t, stackitem.Make(false), vm.estack.Pop().value)
|
2019-09-24 13:16:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func testCLEARITEMS(t *testing.T, item stackitem.Item) {
|
2020-04-24 10:02:38 +00:00
|
|
|
prog := makeProgram(opcode.DUP, opcode.DUP, opcode.CLEARITEMS, opcode.SIZE)
|
|
|
|
v := load(prog)
|
|
|
|
v.estack.PushVal(item)
|
|
|
|
runVM(t, v)
|
|
|
|
require.Equal(t, 2, v.estack.Len())
|
2020-05-12 13:05:10 +00:00
|
|
|
require.EqualValues(t, 2, v.refs.size) // empty collection + it's size
|
2020-04-24 10:02:38 +00:00
|
|
|
require.EqualValues(t, 0, v.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCLEARITEMS(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewByteArray([]byte{1})}
|
|
|
|
m := stackitem.NewMap()
|
|
|
|
m.Add(stackitem.NewBigInteger(big.NewInt(1)), stackitem.NewByteArray([]byte{}))
|
|
|
|
m.Add(stackitem.NewByteArray([]byte{42}), stackitem.NewBigInteger(big.NewInt(2)))
|
|
|
|
|
|
|
|
testCases := map[string]stackitem.Item{
|
|
|
|
"empty Array": stackitem.NewArray([]stackitem.Item{}),
|
|
|
|
"filled Array": stackitem.NewArray(arr),
|
|
|
|
"empty Struct": stackitem.NewStruct([]stackitem.Item{}),
|
|
|
|
"filled Struct": stackitem.NewStruct(arr),
|
|
|
|
"empty Map": stackitem.NewMap(),
|
2020-04-24 10:02:38 +00:00
|
|
|
"filled Map": m,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, item := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) { testCLEARITEMS(t, item) })
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Integer", func(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.CLEARITEMS)
|
2020-04-30 08:08:03 +00:00
|
|
|
runWithArgs(t, prog, nil, 1)
|
2020-04-24 10:02:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:02:40 +00:00
|
|
|
func TestSWAPGood(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.SWAP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(4), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2020-04-30 08:08:03 +00:00
|
|
|
func TestSWAP(t *testing.T) {
|
2019-12-16 16:02:40 +00:00
|
|
|
prog := makeProgram(opcode.SWAP)
|
2020-04-30 08:08:03 +00:00
|
|
|
t.Run("EmptyStack", getTestFuncForVM(prog, nil))
|
|
|
|
t.Run("SmallStack", getTestFuncForVM(prog, nil, 4))
|
2019-12-16 16:02:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
func TestDupInt(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.DUP, opcode.ABS)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(-1), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDupByteArray(t *testing.T) {
|
2020-04-21 13:45:48 +00:00
|
|
|
prog := makeProgram(opcode.PUSHDATA1, 2, 1, 0,
|
2019-12-17 13:38:42 +00:00
|
|
|
opcode.DUP,
|
|
|
|
opcode.PUSH1,
|
|
|
|
opcode.LEFT,
|
2020-04-21 13:45:48 +00:00
|
|
|
opcode.PUSHDATA1, 1, 2,
|
2019-12-17 13:38:42 +00:00
|
|
|
opcode.CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte{0x01, 0x02}, vm.estack.Pop().Bytes())
|
|
|
|
assert.Equal(t, []byte{0x01, 0x00}, vm.estack.Pop().Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDupBool(t *testing.T) {
|
|
|
|
prog := makeProgram(opcode.PUSH0, opcode.NOT,
|
|
|
|
opcode.DUP,
|
|
|
|
opcode.PUSH1, opcode.NOT,
|
|
|
|
opcode.BOOLAND)
|
|
|
|
vm := load(prog)
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
|
|
|
assert.Equal(t, false, vm.estack.Pop().Bool())
|
|
|
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:20:48 +00:00
|
|
|
var opcodesTestCases = map[opcode.Opcode][]struct {
|
|
|
|
name string
|
|
|
|
args []interface{}
|
|
|
|
expected interface{}
|
|
|
|
actual func(vm *VM) interface{}
|
|
|
|
}{
|
|
|
|
opcode.AND: {
|
|
|
|
{
|
|
|
|
name: "1_1",
|
|
|
|
args: []interface{}{1, 1},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1_0",
|
|
|
|
args: []interface{}{1, 0},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_1",
|
|
|
|
args: []interface{}{0, 1},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_0",
|
|
|
|
args: []interface{}{0, 0},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "random_values",
|
|
|
|
args: []interface{}{
|
|
|
|
[]byte{1, 0, 1, 0, 1, 0, 1, 1},
|
|
|
|
[]byte{1, 1, 0, 0, 0, 0, 0, 1},
|
|
|
|
},
|
|
|
|
expected: []byte{1, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bytes()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.OR: {
|
|
|
|
{
|
|
|
|
name: "1_1",
|
|
|
|
args: []interface{}{1, 1},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_0",
|
|
|
|
args: []interface{}{0, 0},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_1",
|
|
|
|
args: []interface{}{0, 1},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1_0",
|
|
|
|
args: []interface{}{1, 0},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "random_values",
|
|
|
|
args: []interface{}{
|
|
|
|
[]byte{1, 0, 1, 0, 1, 0, 1, 1},
|
|
|
|
[]byte{1, 1, 0, 0, 0, 0, 0, 1},
|
|
|
|
},
|
|
|
|
expected: []byte{1, 1, 1, 0, 1, 0, 1, 1},
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bytes()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.XOR: {
|
|
|
|
{
|
|
|
|
name: "1_1",
|
|
|
|
args: []interface{}{1, 1},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_0",
|
|
|
|
args: []interface{}{0, 0},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_1",
|
|
|
|
args: []interface{}{0, 1},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1_0",
|
|
|
|
args: []interface{}{1, 0},
|
|
|
|
expected: int64(1),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "random_values",
|
|
|
|
args: []interface{}{
|
|
|
|
[]byte{1, 0, 1, 0, 1, 0, 1, 1},
|
|
|
|
[]byte{1, 1, 0, 0, 0, 0, 0, 1},
|
|
|
|
},
|
|
|
|
expected: []byte{0, 1, 1, 0, 1, 0, 1},
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bytes()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.BOOLOR: {
|
|
|
|
{
|
|
|
|
name: "1_1",
|
|
|
|
args: []interface{}{true, true},
|
|
|
|
expected: true,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_0",
|
|
|
|
args: []interface{}{false, false},
|
|
|
|
expected: false,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0_1",
|
|
|
|
args: []interface{}{false, true},
|
|
|
|
expected: true,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "1_0",
|
|
|
|
args: []interface{}{true, false},
|
|
|
|
expected: true,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.MIN: {
|
|
|
|
{
|
|
|
|
name: "3_5",
|
|
|
|
args: []interface{}{3, 5},
|
|
|
|
expected: int64(3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5_3",
|
|
|
|
args: []interface{}{5, 3},
|
|
|
|
expected: int64(3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "3_3",
|
|
|
|
args: []interface{}{3, 3},
|
|
|
|
expected: int64(3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.MAX: {
|
|
|
|
{
|
|
|
|
name: "3_5",
|
|
|
|
args: []interface{}{3, 5},
|
|
|
|
expected: int64(5),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5_3",
|
|
|
|
args: []interface{}{5, 3},
|
|
|
|
expected: int64(5),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "3_3",
|
|
|
|
args: []interface{}{3, 3},
|
|
|
|
expected: int64(3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.WITHIN: {
|
|
|
|
{
|
|
|
|
name: "within",
|
|
|
|
args: []interface{}{4, 3, 5},
|
|
|
|
expected: true,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "less",
|
|
|
|
args: []interface{}{2, 3, 5},
|
|
|
|
expected: false,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "more",
|
|
|
|
args: []interface{}{6, 3, 5},
|
|
|
|
expected: false,
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().Bool()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opcode.NEGATE: {
|
|
|
|
{
|
|
|
|
name: "3",
|
|
|
|
args: []interface{}{3},
|
|
|
|
expected: int64(-3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-3",
|
|
|
|
args: []interface{}{-3},
|
|
|
|
expected: int64(3),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "0",
|
|
|
|
args: []interface{}{0},
|
|
|
|
expected: int64(0),
|
|
|
|
actual: func(vm *VM) interface{} {
|
|
|
|
return vm.estack.Pop().BigInt().Int64()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBitAndNumericOpcodes(t *testing.T) {
|
|
|
|
for code, opcodeTestCases := range opcodesTestCases {
|
|
|
|
t.Run(code.String(), func(t *testing.T) {
|
|
|
|
for _, testCase := range opcodeTestCases {
|
|
|
|
prog := makeProgram(code)
|
|
|
|
vm := load(prog)
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
for _, arg := range testCase.args {
|
|
|
|
vm.estack.PushVal(arg)
|
|
|
|
}
|
|
|
|
runVM(t, vm)
|
|
|
|
assert.Equal(t, testCase.expected, testCase.actual(vm))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
func TestSLOTOpcodes(t *testing.T) {
|
|
|
|
t.Run("Fail", func(t *testing.T) {
|
|
|
|
t.Run("EmptyStatic", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 0), nil))
|
|
|
|
t.Run("EmptyLocal", getTestFuncForVM(makeProgram(opcode.INITSLOT, 0, 0), nil))
|
|
|
|
t.Run("NotEnoughArguments", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 0, 2), nil, 1))
|
|
|
|
t.Run("DoubleStatic", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 1, opcode.INITSSLOT, 1), nil))
|
|
|
|
t.Run("DoubleLocal", getTestFuncForVM(makeProgram(opcode.INITSLOT, 1, 0, opcode.INITSLOT, 1, 0), nil))
|
|
|
|
t.Run("DoubleArgument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 0, 1, opcode.INITSLOT, 0, 1), nil, 1, 2))
|
|
|
|
t.Run("LoadBigStatic", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 2, opcode.LDSFLD2), nil))
|
|
|
|
t.Run("LoadBigLocal", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 2, opcode.LDLOC2), nil, 1, 2))
|
|
|
|
t.Run("LoadBigArgument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 2, opcode.LDARG2), nil, 1, 2))
|
|
|
|
t.Run("StoreBigStatic", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 2, opcode.STSFLD2), nil, 0))
|
|
|
|
t.Run("StoreBigLocal", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 2, opcode.STLOC2), nil, 0, 1, 2))
|
|
|
|
t.Run("StoreBigArgument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 2, opcode.STARG2), nil, 0, 1, 2))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Default", func(t *testing.T) {
|
2020-06-03 12:55:06 +00:00
|
|
|
t.Run("DefaultStatic", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 2, opcode.LDSFLD1), stackitem.Null{}))
|
|
|
|
t.Run("DefaultLocal", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 0, opcode.LDLOC1), stackitem.Null{}))
|
2020-05-07 08:54:35 +00:00
|
|
|
t.Run("DefaultArgument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 0, 2, opcode.LDARG1), 2, 2, 1))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Set/Get", func(t *testing.T) {
|
|
|
|
t.Run("FailCrossLoads", func(t *testing.T) {
|
|
|
|
t.Run("Static/Local", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 2, opcode.LDLOC1), nil))
|
|
|
|
t.Run("Static/Argument", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 2, opcode.LDARG1), nil))
|
|
|
|
t.Run("Local/Argument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 0, 2, opcode.LDLOC1), nil))
|
|
|
|
t.Run("Argument/Local", getTestFuncForVM(makeProgram(opcode.INITSLOT, 2, 0, opcode.LDARG1), nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Static", getTestFuncForVM(makeProgram(opcode.INITSSLOT, 8, opcode.STSFLD, 7, opcode.LDSFLD, 7), 42, 42))
|
|
|
|
t.Run("Local", getTestFuncForVM(makeProgram(opcode.INITSLOT, 8, 0, opcode.STLOC, 7, opcode.LDLOC, 7), 42, 42))
|
|
|
|
t.Run("Argument", getTestFuncForVM(makeProgram(opcode.INITSLOT, 0, 2, opcode.STARG, 1, opcode.LDARG, 1), 42, 42, 1, 2))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
func makeProgram(opcodes ...opcode.Opcode) []byte {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := make([]byte, len(opcodes)+1) // RET
|
2018-03-30 16:15:06 +00:00
|
|
|
for i := 0; i < len(opcodes); i++ {
|
|
|
|
prog[i] = byte(opcodes[i])
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
prog[len(prog)-1] = byte(opcode.RET)
|
2018-03-30 16:15:06 +00:00
|
|
|
return prog
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(prog []byte) *VM {
|
2019-10-22 10:44:14 +00:00
|
|
|
vm := New()
|
2020-05-21 06:28:41 +00:00
|
|
|
if len(prog) != 0 {
|
|
|
|
vm.LoadScript(prog)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
return vm
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomBytes(n int) []byte {
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = charset[rand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|