2020-02-03 14:24:57 +00:00
|
|
|
package emit
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2020-02-05 07:13:43 +00:00
|
|
|
"errors"
|
2021-03-05 11:16:34 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2018-03-04 13:56:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2021-03-09 10:17:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-27 08:00:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmitInt(t *testing.T) {
|
2020-02-05 07:29:43 +00:00
|
|
|
t.Run("minis one", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, -1)
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.Len(t, result, 1)
|
|
|
|
assert.EqualValues(t, opcode.PUSHM1, result[0])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, 0)
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.Len(t, result, 1)
|
|
|
|
assert.EqualValues(t, opcode.PUSH0, result[0])
|
|
|
|
})
|
|
|
|
|
2020-02-03 14:50:25 +00:00
|
|
|
t.Run("1-byte int", func(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, 10)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, opcode.PUSH10, result[0])
|
|
|
|
})
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
t.Run("big 1-byte int", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, 42)
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, opcode.PUSHINT8, result[0])
|
|
|
|
assert.EqualValues(t, 42, result[1])
|
|
|
|
})
|
|
|
|
|
2020-02-03 14:50:25 +00:00
|
|
|
t.Run("2-byte int", func(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2020-04-21 13:45:48 +00:00
|
|
|
Int(buf.BinWriter, 300)
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.Equal(t, 3, len(result))
|
|
|
|
assert.EqualValues(t, opcode.PUSHINT16, result[0])
|
2020-06-04 18:11:27 +00:00
|
|
|
assert.EqualValues(t, 300, bigint.FromBytes(result[1:]).Int64())
|
2020-04-21 13:45:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("3-byte int", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, 1<<20)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
2020-04-21 13:45:48 +00:00
|
|
|
assert.Equal(t, 5, len(result))
|
|
|
|
assert.EqualValues(t, opcode.PUSHINT32, result[0])
|
2020-06-04 18:11:27 +00:00
|
|
|
assert.EqualValues(t, 1<<20, bigint.FromBytes(result[1:]).Int64())
|
2020-02-03 14:50:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("4-byte int", func(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2020-04-21 13:45:48 +00:00
|
|
|
Int(buf.BinWriter, 1<<28)
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.Equal(t, 5, len(result))
|
|
|
|
assert.EqualValues(t, opcode.PUSHINT32, result[0])
|
2020-06-04 18:11:27 +00:00
|
|
|
assert.EqualValues(t, 1<<28, bigint.FromBytes(result[1:]).Int64())
|
2020-04-21 13:45:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("negative 3-byte int with padding", func(t *testing.T) {
|
|
|
|
const num = -(1 << 23)
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Int(buf.BinWriter, num)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
2020-04-21 13:45:48 +00:00
|
|
|
assert.Equal(t, 5, len(result))
|
|
|
|
assert.EqualValues(t, opcode.PUSHINT32, result[0])
|
2020-06-04 18:11:27 +00:00
|
|
|
assert.EqualValues(t, num, bigint.FromBytes(result[1:]).Int64())
|
2020-02-03 14:50:25 +00:00
|
|
|
})
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 07:21:31 +00:00
|
|
|
func getSlice(n int) []byte {
|
|
|
|
data := make([]byte, n)
|
|
|
|
for i := range data {
|
|
|
|
data[i] = byte(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytes(t *testing.T) {
|
|
|
|
t.Run("small slice", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Bytes(buf.BinWriter, []byte{0, 1, 2, 3})
|
|
|
|
|
|
|
|
result := buf.Bytes()
|
2020-04-21 13:45:48 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSHDATA1, result[0])
|
|
|
|
assert.EqualValues(t, 4, result[1])
|
|
|
|
assert.EqualValues(t, []byte{0, 1, 2, 3}, result[2:])
|
2020-02-05 07:21:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with len <= 255", func(t *testing.T) {
|
|
|
|
const size = 200
|
|
|
|
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Bytes(buf.BinWriter, getSlice(size))
|
|
|
|
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, opcode.PUSHDATA1, result[0])
|
|
|
|
assert.EqualValues(t, size, result[1])
|
|
|
|
assert.Equal(t, getSlice(size), result[2:])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with len <= 65535", func(t *testing.T) {
|
|
|
|
const size = 60000
|
|
|
|
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Bytes(buf.BinWriter, getSlice(size))
|
|
|
|
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, opcode.PUSHDATA2, result[0])
|
|
|
|
assert.EqualValues(t, size, binary.LittleEndian.Uint16(result[1:3]))
|
|
|
|
assert.Equal(t, getSlice(size), result[3:])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with len > 65535", func(t *testing.T) {
|
|
|
|
const size = 100000
|
|
|
|
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Bytes(buf.BinWriter, getSlice(size))
|
|
|
|
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, opcode.PUSHDATA4, result[0])
|
|
|
|
assert.EqualValues(t, size, binary.LittleEndian.Uint32(result[1:5]))
|
|
|
|
assert.Equal(t, getSlice(size), result[5:])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:00:06 +00:00
|
|
|
func TestEmitArray(t *testing.T) {
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
2021-03-05 11:16:34 +00:00
|
|
|
veryBig := new(big.Int).SetUint64(math.MaxUint64)
|
|
|
|
veryBig.Add(veryBig, big.NewInt(1))
|
|
|
|
Array(buf.BinWriter, big.NewInt(0), veryBig,
|
|
|
|
[]interface{}{int64(1), int64(2)}, nil, int64(1), "str", true, []byte{0xCA, 0xFE})
|
2020-03-27 08:00:06 +00:00
|
|
|
require.NoError(t, buf.Err)
|
|
|
|
|
|
|
|
res := buf.Bytes()
|
2020-04-21 13:45:48 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSHDATA1, res[0])
|
|
|
|
assert.EqualValues(t, 2, res[1])
|
|
|
|
assert.EqualValues(t, []byte{0xCA, 0xFE}, res[2:4])
|
|
|
|
assert.EqualValues(t, opcode.PUSHT, res[4])
|
2021-03-09 10:17:21 +00:00
|
|
|
assert.EqualValues(t, opcode.CONVERT, res[5])
|
|
|
|
assert.EqualValues(t, stackitem.BooleanT, res[6])
|
|
|
|
assert.EqualValues(t, opcode.PUSHDATA1, res[7])
|
|
|
|
assert.EqualValues(t, 3, res[8])
|
|
|
|
assert.EqualValues(t, []byte("str"), res[9:12])
|
|
|
|
assert.EqualValues(t, opcode.PUSH1, res[12])
|
|
|
|
assert.EqualValues(t, opcode.PUSHNULL, res[13])
|
2020-11-24 08:08:52 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSH2, res[14])
|
2021-03-09 10:17:21 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSH1, res[15])
|
|
|
|
assert.EqualValues(t, opcode.PUSH2, res[16])
|
|
|
|
assert.EqualValues(t, opcode.PACK, res[17])
|
2021-03-05 11:16:34 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSHINT128, res[18])
|
|
|
|
assert.EqualValues(t, veryBig, bigint.FromBytes(res[19:35]))
|
|
|
|
assert.EqualValues(t, opcode.PUSH0, res[35])
|
2020-03-27 08:00:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Array(buf.BinWriter)
|
|
|
|
require.NoError(t, buf.Err)
|
2022-01-14 14:21:14 +00:00
|
|
|
assert.EqualValues(t, []byte{byte(opcode.NEWARRAY0)}, buf.Bytes())
|
2020-03-27 08:00:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid type", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Array(buf.BinWriter, struct{}{})
|
|
|
|
require.Error(t, buf.Err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
func TestEmitBool(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Bool(buf.BinWriter, true)
|
|
|
|
Bool(buf.BinWriter, false)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
2021-03-09 10:17:21 +00:00
|
|
|
assert.EqualValues(t, opcode.PUSH1, result[0])
|
|
|
|
assert.EqualValues(t, opcode.CONVERT, result[1])
|
|
|
|
assert.EqualValues(t, stackitem.BooleanT, result[2])
|
|
|
|
assert.EqualValues(t, opcode.PUSH0, result[3])
|
|
|
|
assert.EqualValues(t, opcode.CONVERT, result[4])
|
|
|
|
assert.EqualValues(t, stackitem.BooleanT, result[5])
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
func TestEmitOpcode(t *testing.T) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
Opcodes(w.BinWriter, opcode.PUSH1, opcode.NEWMAP)
|
|
|
|
result := w.Bytes()
|
|
|
|
assert.Equal(t, result, []byte{byte(opcode.PUSH1), byte(opcode.NEWMAP)})
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
func TestEmitString(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-03-04 13:56:49 +00:00
|
|
|
str := "City Of Zion"
|
2020-02-03 14:46:51 +00:00
|
|
|
String(buf.BinWriter, str)
|
2020-04-21 13:45:48 +00:00
|
|
|
assert.Equal(t, buf.Len(), len(str)+2)
|
|
|
|
assert.Equal(t, buf.Bytes()[2:], []byte(str))
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitSyscall(t *testing.T) {
|
|
|
|
syscalls := []string{
|
2020-08-14 10:50:52 +00:00
|
|
|
interopnames.SystemRuntimeLog,
|
|
|
|
interopnames.SystemRuntimeNotify,
|
2020-06-10 08:49:39 +00:00
|
|
|
"System.Runtime.Whatever",
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-03-04 13:56:49 +00:00
|
|
|
for _, syscall := range syscalls {
|
2020-02-03 14:46:51 +00:00
|
|
|
Syscall(buf.BinWriter, syscall)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
2020-04-15 14:40:05 +00:00
|
|
|
assert.Equal(t, 5, len(result))
|
2020-02-03 14:50:25 +00:00
|
|
|
assert.Equal(t, opcode.Opcode(result[0]), opcode.SYSCALL)
|
2020-08-13 07:41:33 +00:00
|
|
|
assert.Equal(t, binary.LittleEndian.Uint32(result[1:]), interopnames.ToID([]byte(syscall)))
|
2018-03-04 13:56:49 +00:00
|
|
|
buf.Reset()
|
|
|
|
}
|
2020-02-05 07:13:43 +00:00
|
|
|
|
|
|
|
t.Run("empty syscall", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Syscall(buf.BinWriter, "")
|
|
|
|
assert.Error(t, buf.Err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty syscall after error", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
err := errors.New("first error")
|
|
|
|
|
|
|
|
buf.Err = err
|
|
|
|
Syscall(buf.BinWriter, "")
|
|
|
|
assert.Equal(t, err, buf.Err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJmp(t *testing.T) {
|
|
|
|
const label = 0x23
|
|
|
|
|
|
|
|
t.Run("correct", func(t *testing.T) {
|
|
|
|
ops := []opcode.Opcode{opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT, opcode.CALL}
|
|
|
|
for i := range ops {
|
|
|
|
t.Run(ops[i].String(), func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Jmp(buf.BinWriter, ops[i], label)
|
|
|
|
assert.NoError(t, buf.Err)
|
|
|
|
|
|
|
|
result := buf.Bytes()
|
|
|
|
assert.EqualValues(t, ops[i], result[0])
|
|
|
|
assert.EqualValues(t, 0x23, binary.LittleEndian.Uint16(result[1:]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not a jump instruction", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Jmp(buf.BinWriter, opcode.ABS, label)
|
|
|
|
assert.Error(t, buf.Err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not a jump after error", func(t *testing.T) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
err := errors.New("first error")
|
|
|
|
|
|
|
|
buf.Err = err
|
|
|
|
Jmp(buf.BinWriter, opcode.ABS, label)
|
|
|
|
assert.Error(t, buf.Err)
|
|
|
|
})
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitCall(t *testing.T) {
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
Call(buf.BinWriter, opcode.JMP, 100)
|
2020-02-03 14:50:25 +00:00
|
|
|
result := buf.Bytes()
|
|
|
|
assert.Equal(t, opcode.Opcode(result[0]), opcode.JMP)
|
|
|
|
label := binary.LittleEndian.Uint16(result[1:3])
|
2018-03-04 13:56:49 +00:00
|
|
|
assert.Equal(t, label, uint16(100))
|
|
|
|
}
|