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()
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData1(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData2(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushData4(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
assert.Equal(t, big.NewInt(2), vm.estack.Pop().BigInt())
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64()))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|