2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2018-04-02 15:04:42 +00:00
|
|
|
"math/big"
|
2018-03-30 16:15:06 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
func TestInteropHook(t *testing.T) {
|
|
|
|
v := New(ModeMute)
|
|
|
|
v.RegisterInteropFunc("foo", func(evm *VM) error {
|
|
|
|
evm.Estack().PushVal(1)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
EmitSyscall(buf, "foo")
|
2019-08-14 12:40:31 +00:00
|
|
|
EmitOpcode(buf, RET)
|
2018-04-10 09:45:31 +00:00
|
|
|
v.Load(buf.Bytes())
|
|
|
|
v.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, v.state.HasFlag(faultState))
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterInterop(t *testing.T) {
|
|
|
|
v := New(ModeMute)
|
|
|
|
currRegistered := len(v.interop)
|
|
|
|
v.RegisterInteropFunc("foo", func(evm *VM) error { return nil })
|
|
|
|
assert.Equal(t, currRegistered+1, len(v.interop))
|
|
|
|
_, ok := v.interop["foo"]
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestPushBytes1to75(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for i := 1; i <= 75; i++ {
|
|
|
|
b := randomBytes(i)
|
|
|
|
EmitBytes(buf, b)
|
|
|
|
vm := load(buf.Bytes())
|
|
|
|
vm.Step()
|
|
|
|
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
|
|
|
|
elem := vm.estack.Pop()
|
2018-04-02 15:04:42 +00:00
|
|
|
assert.IsType(t, &ByteArrayItem{}, elem.value)
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.IsType(t, elem.Bytes(), b)
|
|
|
|
assert.Equal(t, 0, vm.estack.Len())
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
vm.execute(nil, RET)
|
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-09-09 09:07:03 +00:00
|
|
|
func TestPushBytesNoParam(t *testing.T) {
|
|
|
|
prog := make([]byte, 1)
|
|
|
|
prog[0] = byte(PUSHBYTES1)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushBytesShort(t *testing.T) {
|
|
|
|
prog := make([]byte, 10)
|
|
|
|
prog[0] = byte(PUSHBYTES10) // but only 9 left in the `prog`
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
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-08-14 12:40:31 +00:00
|
|
|
for i := int(PUSHM1); i <= int(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-08-14 12:40:31 +00:00
|
|
|
for i := int(PUSHM1); i <= int(PUSH16); i++ {
|
2018-03-30 16:15:06 +00:00
|
|
|
if i == 80 {
|
|
|
|
continue // nice opcode layout we got here.
|
|
|
|
}
|
|
|
|
vm.Step()
|
|
|
|
|
|
|
|
elem := vm.estack.Pop()
|
2018-04-02 15:04:42 +00:00
|
|
|
assert.IsType(t, &BigIntegerItem{}, elem.value)
|
2019-08-14 12:40:31 +00:00
|
|
|
val := i - int(PUSH1) + 1
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, elem.BigInt().Int64(), int64(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData1BadNoN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA1)}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData1BadN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA1), 1}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData1Good(t *testing.T) {
|
|
|
|
prog := makeProgram(PUSHDATA1, 3, 1, 2, 3)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte{1, 2, 3}, vm.estack.Pop().Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData2BadNoN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA2)}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData2ShortN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA2), 0}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData2BadN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA2), 1, 0}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData2Good(t *testing.T) {
|
|
|
|
prog := makeProgram(PUSHDATA2, 3, 0, 1, 2, 3)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte{1, 2, 3}, vm.estack.Pop().Bytes())
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData4BadNoN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA4)}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData4BadN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA4), 1, 0, 0, 0}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-11 08:52:39 +00:00
|
|
|
func TestPushData4ShortN(t *testing.T) {
|
|
|
|
prog := []byte{byte(PUSHDATA4), 0, 0, 0}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData4Good(t *testing.T) {
|
|
|
|
prog := makeProgram(PUSHDATA4, 3, 0, 0, 0, 1, 2, 3)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte{1, 2, 3}, vm.estack.Pop().Bytes())
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 11:57:39 +00:00
|
|
|
func TestNOTNoArgument(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTBool(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(false)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTNonZeroInt(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{false}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTArray(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]StackItem{})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{false}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTStruct(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.Push(NewElement(&StructItem{[]StackItem{}}))
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{false}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTByteArray0(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 0})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNOTByteArray1(t *testing.T) {
|
|
|
|
prog := makeProgram(NOT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 1})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, &BoolItem{false}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestAdd(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(ADD)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(6), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMul(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(MUL)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(8), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiv(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(DIV)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSub(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(SUB)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-12 09:02:38 +00:00
|
|
|
func TestSHRGood(t *testing.T) {
|
|
|
|
prog := makeProgram(SHR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSHRZero(t *testing.T) {
|
|
|
|
prog := makeProgram(SHR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 1})
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSHLGood(t *testing.T) {
|
|
|
|
prog := makeProgram(SHL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(16), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSHLZero(t *testing.T) {
|
|
|
|
prog := makeProgram(SHL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 1})
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestLT(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(LT)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, false, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLTE(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(LTE)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGT(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(GT)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(9)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGTE(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(GTE)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDepth(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(DEPTH)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-12 07:50:43 +00:00
|
|
|
func TestEQUALNoArguments(t *testing.T) {
|
|
|
|
prog := makeProgram(EQUAL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEQUALBad1Argument(t *testing.T) {
|
|
|
|
prog := makeProgram(EQUAL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEQUALGoodInteger(t *testing.T) {
|
|
|
|
prog := makeProgram(EQUAL)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(5)
|
|
|
|
vm.estack.PushVal(5)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestNumEqual(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(NUMEQUAL)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, false, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNumNotEqual(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(NUMNOTEQUAL)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, false, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func TestINC(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := makeProgram(INC)
|
2018-04-02 15:04:42 +00:00
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-04-02 15:04:42 +00:00
|
|
|
assert.Equal(t, big.NewInt(2), vm.estack.Pop().BigInt())
|
|
|
|
}
|
|
|
|
|
2019-09-06 16:00:04 +00:00
|
|
|
func TestNEWARRAYInteger(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWARRAY)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
2019-09-12 08:24:10 +00:00
|
|
|
assert.Equal(t, &ArrayItem{[]StackItem{makeStackItem(false)}}, vm.estack.Pop().value)
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWARRAYStruct(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWARRAY)
|
|
|
|
vm := load(prog)
|
|
|
|
arr := []StackItem{makeStackItem(42)}
|
|
|
|
vm.estack.Push(&Element{value: &StructItem{arr}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ArrayItem{arr}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWARRAYArray(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWARRAY)
|
|
|
|
vm := load(prog)
|
|
|
|
arr := []StackItem{makeStackItem(42)}
|
|
|
|
vm.estack.Push(&Element{value: &ArrayItem{arr}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ArrayItem{arr}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:48:39 +00:00
|
|
|
func TestNEWARRAYByteArray(t *testing.T) {
|
2019-09-06 16:00:04 +00:00
|
|
|
prog := makeProgram(NEWARRAY)
|
|
|
|
vm := load(prog)
|
2019-09-12 08:48:39 +00:00
|
|
|
vm.estack.PushVal([]byte{})
|
2019-09-06 16:00:04 +00:00
|
|
|
vm.Run()
|
2019-09-12 08:48:39 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ArrayItem{[]StackItem{}}, vm.estack.Pop().value)
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWSTRUCTInteger(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWSTRUCT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
2019-09-12 08:24:10 +00:00
|
|
|
assert.Equal(t, &StructItem{[]StackItem{makeStackItem(false)}}, vm.estack.Pop().value)
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWSTRUCTArray(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWSTRUCT)
|
|
|
|
vm := load(prog)
|
|
|
|
arr := []StackItem{makeStackItem(42)}
|
|
|
|
vm.estack.Push(&Element{value: &ArrayItem{arr}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &StructItem{arr}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEWSTRUCTStruct(t *testing.T) {
|
|
|
|
prog := makeProgram(NEWSTRUCT)
|
|
|
|
vm := load(prog)
|
|
|
|
arr := []StackItem{makeStackItem(42)}
|
|
|
|
vm.estack.Push(&Element{value: &StructItem{arr}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &StructItem{arr}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:48:39 +00:00
|
|
|
func TestNEWSTRUCTByteArray(t *testing.T) {
|
2019-09-06 16:00:04 +00:00
|
|
|
prog := makeProgram(NEWSTRUCT)
|
|
|
|
vm := load(prog)
|
2019-09-12 08:48:39 +00:00
|
|
|
vm.estack.PushVal([]byte{})
|
2019-09-06 16:00:04 +00:00
|
|
|
vm.Run()
|
2019-09-12 08:48:39 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &StructItem{[]StackItem{}}, vm.estack.Pop().value)
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 14:05:56 +00:00
|
|
|
func TestAPPENDArray(t *testing.T) {
|
|
|
|
prog := makeProgram(DUP, PUSH5, APPEND)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.Push(&Element{value: &ArrayItem{}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ArrayItem{[]StackItem{makeStackItem(5)}}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPPENDStruct(t *testing.T) {
|
|
|
|
prog := makeProgram(DUP, PUSH5, APPEND)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.Push(&Element{value: &StructItem{}})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &StructItem{[]StackItem{makeStackItem(5)}}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPPENDBadNoArguments(t *testing.T) {
|
|
|
|
prog := makeProgram(APPEND)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPPENDBad1Argument(t *testing.T) {
|
|
|
|
prog := makeProgram(APPEND)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-11 14:08:36 +00:00
|
|
|
func TestAPPENDWrongType(t *testing.T) {
|
|
|
|
prog := makeProgram(APPEND)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{})
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:19:25 +00:00
|
|
|
func TestPICKITEMBadIndex(t *testing.T) {
|
|
|
|
prog := makeProgram(PICKITEM)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]StackItem{})
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKITEMArray(t *testing.T) {
|
|
|
|
prog := makeProgram(PICKITEM)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]StackItem{makeStackItem(1), makeStackItem(2)})
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKITEMByteArray(t *testing.T) {
|
|
|
|
prog := makeProgram(PICKITEM)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{1, 2})
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:53:11 +00:00
|
|
|
func TestSIZENoArgument(t *testing.T) {
|
|
|
|
prog := makeProgram(SIZE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIZEByteArray(t *testing.T) {
|
|
|
|
prog := makeProgram(SIZE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 1})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIZEBool(t *testing.T) {
|
|
|
|
prog := makeProgram(SIZE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(false)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:19:52 +00:00
|
|
|
func TestSIGNNoArgument(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNWrongType(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]StackItem{})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNBool(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(false)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BigIntegerItem{big.NewInt(0)}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNPositiveInt(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BigIntegerItem{big.NewInt(1)}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNNegativeInt(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BigIntegerItem{big.NewInt(-1)}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNZero(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BigIntegerItem{big.NewInt(0)}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSIGNByteArray(t *testing.T) {
|
|
|
|
prog := makeProgram(SIGN)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{0, 1})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &BigIntegerItem{big.NewInt(1)}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func TestAppCall(t *testing.T) {
|
2019-08-14 12:40:31 +00:00
|
|
|
prog := []byte{byte(APPCALL)}
|
2018-03-30 16:15:06 +00:00
|
|
|
hash := util.Uint160{}
|
|
|
|
prog = append(prog, hash.Bytes()...)
|
2019-08-14 12:40:31 +00:00
|
|
|
prog = append(prog, byte(RET))
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
vm := load(prog)
|
2019-08-14 12:40:31 +00:00
|
|
|
vm.scripts[hash] = makeProgram(DEPTH)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm.estack.PushVal(2)
|
|
|
|
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
elem := vm.estack.Pop() // depth should be 1
|
|
|
|
assert.Equal(t, int64(1), elem.BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleCall(t *testing.T) {
|
|
|
|
progStr := "52c56b525a7c616516006c766b00527ac46203006c766b00c3616c756653c56b6c766b00527ac46c766b51527ac46203006c766b00c36c766b51c393616c7566"
|
|
|
|
result := 12
|
|
|
|
|
|
|
|
prog, err := hex.DecodeString(progStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
2019-09-05 12:07:56 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
2018-03-30 16:15:06 +00:00
|
|
|
assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64()))
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:05:44 +00:00
|
|
|
func TestNZtrue(t *testing.T) {
|
|
|
|
prog := makeProgram(NZ)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNZfalse(t *testing.T) {
|
|
|
|
prog := makeProgram(NZ)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, false, vm.estack.Pop().Bool())
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:18:04 +00:00
|
|
|
func TestPICKbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(PICK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKbadNegative(t *testing.T) {
|
|
|
|
prog := makeProgram(PICK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPICKgood(t *testing.T) {
|
|
|
|
prog := makeProgram(PICK)
|
|
|
|
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)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, int64(result), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-12 07:57:55 +00:00
|
|
|
func TestROTBad(t *testing.T) {
|
|
|
|
prog := makeProgram(ROT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestROTGood(t *testing.T) {
|
|
|
|
prog := makeProgram(ROT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 3, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, makeStackItem(3), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:43:59 +00:00
|
|
|
func TestXTUCKbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(XTUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXTUCKbadNoN(t *testing.T) {
|
|
|
|
prog := makeProgram(XTUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXTUCKbadNegative(t *testing.T) {
|
|
|
|
prog := makeProgram(XTUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-12 07:39:54 +00:00
|
|
|
func TestXTUCKbadZero(t *testing.T) {
|
|
|
|
prog := makeProgram(XTUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:43:59 +00:00
|
|
|
func TestXTUCKgood(t *testing.T) {
|
|
|
|
prog := makeProgram(XTUCK)
|
|
|
|
topelement := 5
|
|
|
|
xtuckdepth := 3
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.estack.PushVal(4)
|
|
|
|
vm.estack.PushVal(topelement)
|
|
|
|
vm.estack.PushVal(xtuckdepth)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, int64(topelement), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
assert.Equal(t, int64(topelement), vm.estack.Peek(xtuckdepth).BigInt().Int64())
|
|
|
|
}
|
2019-09-05 12:18:04 +00:00
|
|
|
|
2019-09-05 13:34:35 +00:00
|
|
|
func TestTUCKbadNoitems(t *testing.T) {
|
|
|
|
prog := makeProgram(TUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTUCKbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(TUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTUCKgood(t *testing.T) {
|
|
|
|
prog := makeProgram(TUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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) {
|
|
|
|
prog := makeProgram(TUCK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(11)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:57:44 +00:00
|
|
|
func TestOVERbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(OVER)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOVERbadNoitems(t *testing.T) {
|
|
|
|
prog := makeProgram(OVER)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOVERgood(t *testing.T) {
|
|
|
|
prog := makeProgram(OVER)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(42)
|
|
|
|
vm.estack.PushVal(34)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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-09-12 08:09:23 +00:00
|
|
|
func TestNIPBadNoItem(t *testing.T) {
|
|
|
|
prog := makeProgram(NIP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNIPGood(t *testing.T) {
|
|
|
|
prog := makeProgram(NIP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, makeStackItem(2), vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:05:10 +00:00
|
|
|
func TestDROPBadNoItem(t *testing.T) {
|
|
|
|
prog := makeProgram(DROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDROPGood(t *testing.T) {
|
|
|
|
prog := makeProgram(DROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 0, vm.estack.Len())
|
|
|
|
}
|
|
|
|
|
2019-09-05 13:42:54 +00:00
|
|
|
func TestXDROPbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(XDROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXDROPbadNoN(t *testing.T) {
|
|
|
|
prog := makeProgram(XDROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXDROPbadNegative(t *testing.T) {
|
|
|
|
prog := makeProgram(XDROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXDROPgood(t *testing.T) {
|
|
|
|
prog := makeProgram(XDROP)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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
|
|
|
|
2019-09-05 14:20:53 +00:00
|
|
|
func TestINVERTbadNoitem(t *testing.T) {
|
|
|
|
prog := makeProgram(INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood1(t *testing.T) {
|
|
|
|
prog := makeProgram(INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, int64(-1), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood2(t *testing.T) {
|
|
|
|
prog := makeProgram(INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, int64(0), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestINVERTgood3(t *testing.T) {
|
|
|
|
prog := makeProgram(INVERT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0x69)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, int64(-0x6A), vm.estack.Peek(0).BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:12:19 +00:00
|
|
|
func TestCATBadNoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCATBadOneArg(t *testing.T) {
|
|
|
|
prog := makeProgram(CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abc"))
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCATGood(t *testing.T) {
|
|
|
|
prog := makeProgram(CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abc"))
|
|
|
|
vm.estack.PushVal([]byte("def"))
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte("abcdef"), vm.estack.Peek(0).Bytes())
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:28:53 +00:00
|
|
|
func TestCATInt0ByteArray(t *testing.T) {
|
|
|
|
prog := makeProgram(CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal([]byte{})
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ByteArrayItem{[]byte{}}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCATByteArrayInt1(t *testing.T) {
|
|
|
|
prog := makeProgram(CAT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte{})
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, &ByteArrayItem{[]byte{1}}, vm.estack.Pop().value)
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:12:19 +00:00
|
|
|
func TestSUBSTRBadNoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadOneArg(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadTwoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRGood(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte("bc"), vm.estack.Peek(0).Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadOffset(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(6)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadLen(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(6)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:00:11 +00:00
|
|
|
func TestSUBSTRBad387(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
b := make([]byte, 6, 20)
|
|
|
|
copy(b, "abcdef")
|
|
|
|
vm.estack.PushVal(b)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(6)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadNegativeOffset(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSUBSTRBadNegativeLen(t *testing.T) {
|
|
|
|
prog := makeProgram(SUBSTR)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(3)
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:12:19 +00:00
|
|
|
func TestLEFTBadNoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(LEFT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLEFTBadNoString(t *testing.T) {
|
|
|
|
prog := makeProgram(LEFT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:03:43 +00:00
|
|
|
func TestLEFTBadNegativeLen(t *testing.T) {
|
|
|
|
prog := makeProgram(LEFT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:12:19 +00:00
|
|
|
func TestLEFTGood(t *testing.T) {
|
|
|
|
prog := makeProgram(LEFT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte("ab"), vm.estack.Peek(0).Bytes())
|
|
|
|
}
|
|
|
|
|
2019-09-09 14:05:40 +00:00
|
|
|
func TestLEFTGoodLen(t *testing.T) {
|
2019-09-06 06:12:19 +00:00
|
|
|
prog := makeProgram(LEFT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(8)
|
|
|
|
vm.Run()
|
2019-09-09 14:05:40 +00:00
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte("abcdef"), vm.estack.Peek(0).Bytes())
|
2019-09-06 06:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRIGHTBadNoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(RIGHT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRIGHTBadNoString(t *testing.T) {
|
|
|
|
prog := makeProgram(RIGHT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:03:43 +00:00
|
|
|
func TestRIGHTBadNegativeLen(t *testing.T) {
|
|
|
|
prog := makeProgram(RIGHT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(-1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:12:19 +00:00
|
|
|
func TestRIGHTGood(t *testing.T) {
|
|
|
|
prog := makeProgram(RIGHT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(2)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []byte("ef"), vm.estack.Peek(0).Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRIGHTBadLen(t *testing.T) {
|
|
|
|
prog := makeProgram(RIGHT)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal([]byte("abcdef"))
|
|
|
|
vm.estack.PushVal(8)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:33:43 +00:00
|
|
|
func TestPACKBadLen(t *testing.T) {
|
|
|
|
prog := makeProgram(PACK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPACKGoodZeroLen(t *testing.T) {
|
|
|
|
prog := makeProgram(PACK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(0)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 1, vm.estack.Len())
|
|
|
|
assert.Equal(t, []StackItem{}, vm.estack.Peek(0).Array())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPACKGood(t *testing.T) {
|
|
|
|
prog := makeProgram(PACK)
|
|
|
|
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))
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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) {
|
|
|
|
prog := makeProgram(UNPACK)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUNPACKGood(t *testing.T) {
|
|
|
|
prog := makeProgram(UNPACK)
|
|
|
|
elements := []int{55, 34, 42}
|
|
|
|
vm := load(prog)
|
|
|
|
// canary
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSEBadNotArray(t *testing.T) {
|
|
|
|
prog := makeProgram(REVERSE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSEGoodOneElem(t *testing.T) {
|
|
|
|
prog := makeProgram(REVERSE)
|
|
|
|
elements := []int{22}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREVERSEGood(t *testing.T) {
|
|
|
|
eodd := []int{22, 34, 42, 55, 81}
|
|
|
|
even := []int{22, 34, 42, 55, 81, 99}
|
|
|
|
eall := [][]int{eodd, even}
|
|
|
|
|
|
|
|
for _, elements := range eall {
|
|
|
|
prog := makeProgram(REVERSE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(elements)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEBadNoArgs(t *testing.T) {
|
|
|
|
prog := makeProgram(REMOVE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEBadOneArg(t *testing.T) {
|
|
|
|
prog := makeProgram(REMOVE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEBadNotArray(t *testing.T) {
|
|
|
|
prog := makeProgram(REMOVE)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.estack.PushVal(1)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEBadIndex(t *testing.T) {
|
|
|
|
prog := makeProgram(REMOVE)
|
|
|
|
elements := []int{22, 34, 42, 55, 81}
|
|
|
|
vm := load(prog)
|
|
|
|
vm.estack.PushVal(elements)
|
|
|
|
vm.estack.PushVal(10)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestREMOVEGood(t *testing.T) {
|
2019-09-12 08:32:09 +00:00
|
|
|
prog := makeProgram(DUP, PUSH2, 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)
|
|
|
|
vm.Run()
|
|
|
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
|
|
|
assert.Equal(t, 2, vm.estack.Len())
|
2019-09-12 08:32:09 +00:00
|
|
|
assert.Equal(t, makeStackItem(reselements), vm.estack.Pop().value)
|
|
|
|
assert.Equal(t, makeStackItem(1), vm.estack.Pop().value)
|
2019-09-06 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func makeProgram(opcodes ...Instruction) []byte {
|
|
|
|
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-08-14 12:40:31 +00:00
|
|
|
prog[len(prog)-1] = byte(RET)
|
2018-03-30 16:15:06 +00:00
|
|
|
return prog
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(prog []byte) *VM {
|
2018-04-10 09:45:31 +00:00
|
|
|
vm := New(ModeMute)
|
2018-03-30 16:15:06 +00:00
|
|
|
vm.mute = true
|
|
|
|
vm.istack.PushVal(NewContext(prog))
|
|
|
|
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
|
|
|
|
}
|