2019-11-06 09:15:30 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2019-11-06 09:15:30 +00:00
|
|
|
"fmt"
|
2022-02-22 16:27:32 +00:00
|
|
|
"io"
|
2019-11-06 09:15:30 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2019-11-06 09:15:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
vmUT struct {
|
|
|
|
Category string `json:"category"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tests []vmUTEntry `json:"tests"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTActionType string
|
|
|
|
|
|
|
|
vmUTEntry struct {
|
|
|
|
Name string
|
|
|
|
Script vmUTScript
|
|
|
|
Steps []vmUTStep
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTExecutionContextState struct {
|
|
|
|
Instruction string `json:"nextInstruction"`
|
|
|
|
InstructionPointer int `json:"instructionPointer"`
|
|
|
|
EStack []vmUTStackItem `json:"evaluationStack"`
|
2020-05-21 06:38:12 +00:00
|
|
|
StaticFields []vmUTStackItem `json:"staticFields"`
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vmUTExecutionEngineState struct {
|
2022-07-08 14:28:29 +00:00
|
|
|
State vmstate.State `json:"state"`
|
2019-11-06 09:15:30 +00:00
|
|
|
ResultStack []vmUTStackItem `json:"resultStack"`
|
|
|
|
InvocationStack []vmUTExecutionContextState `json:"invocationStack"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTScript []byte
|
|
|
|
|
|
|
|
vmUTStackItem struct {
|
|
|
|
Type vmUTStackItemType
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTStep struct {
|
|
|
|
Actions []vmUTActionType `json:"actions"`
|
|
|
|
Result vmUTExecutionEngineState `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTStackItemType string
|
|
|
|
)
|
|
|
|
|
|
|
|
// stackItemAUX is used as an intermediate structure
|
|
|
|
// to conditionally unmarshal vmUTStackItem based
|
|
|
|
// on the value of Type field.
|
|
|
|
type stackItemAUX struct {
|
|
|
|
Type vmUTStackItemType `json:"type"`
|
|
|
|
Value json.RawMessage `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-05-20 13:07:37 +00:00
|
|
|
vmExecute vmUTActionType = "execute"
|
|
|
|
vmStepInto vmUTActionType = "stepinto"
|
|
|
|
vmStepOut vmUTActionType = "stepout"
|
|
|
|
vmStepOver vmUTActionType = "stepover"
|
2019-11-06 09:15:30 +00:00
|
|
|
|
2020-05-20 12:48:19 +00:00
|
|
|
typeArray vmUTStackItemType = "array"
|
|
|
|
typeBoolean vmUTStackItemType = "boolean"
|
|
|
|
typeBuffer vmUTStackItemType = "buffer"
|
|
|
|
typeByteString vmUTStackItemType = "bytestring"
|
|
|
|
typeInteger vmUTStackItemType = "integer"
|
|
|
|
typeInterop vmUTStackItemType = "interop"
|
|
|
|
typeMap vmUTStackItemType = "map"
|
|
|
|
typeNull vmUTStackItemType = "null"
|
|
|
|
typePointer vmUTStackItemType = "pointer"
|
|
|
|
typeString vmUTStackItemType = "string"
|
|
|
|
typeStruct vmUTStackItemType = "struct"
|
2019-11-06 09:15:30 +00:00
|
|
|
|
2019-12-04 17:30:27 +00:00
|
|
|
testsDir = "testdata/neo-vm/tests/neo-vm.Tests/Tests/"
|
2019-11-06 09:15:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUT(t *testing.T) {
|
2019-12-04 17:30:27 +00:00
|
|
|
testsRan := false
|
2019-11-06 09:15:30 +00:00
|
|
|
err := filepath.Walk(testsDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if !strings.HasSuffix(path, ".json") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testFile(t, path)
|
2019-12-04 17:30:27 +00:00
|
|
|
testsRan = true
|
2019-11-06 09:15:30 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2019-12-04 17:30:27 +00:00
|
|
|
require.Equal(t, true, testsRan, "neo-vm tests should be available (check submodules)")
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
func testSyscallHandler(v *VM, id uint32) error {
|
2020-06-10 12:51:28 +00:00
|
|
|
switch id {
|
2020-07-23 09:31:57 +00:00
|
|
|
case 0x77777777:
|
2020-07-28 13:38:00 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(new(int)))
|
2020-07-23 09:31:57 +00:00
|
|
|
case 0x66666666:
|
2020-12-29 10:45:49 +00:00
|
|
|
if !v.Context().callFlag.Has(callflag.ReadOnly) {
|
2020-07-28 13:38:00 +00:00
|
|
|
return errors.New("invalid call flags")
|
2020-06-10 12:51:28 +00:00
|
|
|
}
|
2020-07-28 13:38:00 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(new(int)))
|
2020-07-23 09:31:57 +00:00
|
|
|
case 0x55555555:
|
2020-07-28 13:38:00 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(new(int)))
|
2020-07-23 09:30:30 +00:00
|
|
|
case 0xADDEADDE:
|
2020-07-28 13:38:00 +00:00
|
|
|
v.throw(stackitem.Make("error"))
|
|
|
|
default:
|
|
|
|
return errors.New("syscall not found")
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
func testFile(t *testing.T, filename string) {
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-06 09:15:30 +00:00
|
|
|
|
2020-05-20 12:51:50 +00:00
|
|
|
// get rid of possible BOM
|
|
|
|
if len(data) > 2 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf {
|
|
|
|
data = data[3:]
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(filename, "MEMCPY.json") {
|
|
|
|
return // FIXME not a valid JSON https://github.com/neo-project/neo-vm/issues/322
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
ut := new(vmUT)
|
2020-05-20 12:51:50 +00:00
|
|
|
require.NoErrorf(t, json.Unmarshal(data, ut), "file: %s", filename)
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
t.Run(ut.Category+":"+ut.Name, func(t *testing.T) {
|
|
|
|
for i := range ut.Tests {
|
|
|
|
test := ut.Tests[i]
|
2020-07-23 09:30:30 +00:00
|
|
|
if test.Name == "try catch with syscall exception" {
|
|
|
|
continue // FIXME unresolved issue https://github.com/neo-project/neo-vm/issues/343
|
|
|
|
}
|
2019-11-06 09:15:30 +00:00
|
|
|
t.Run(ut.Tests[i].Name, func(t *testing.T) {
|
|
|
|
prog := []byte(test.Script)
|
|
|
|
vm := load(prog)
|
2022-07-08 14:28:29 +00:00
|
|
|
vm.state = vmstate.Break
|
2020-07-28 13:38:00 +00:00
|
|
|
vm.SyscallHandler = testSyscallHandler
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
for i := range test.Steps {
|
|
|
|
execStep(t, vm, test.Steps[i])
|
|
|
|
result := test.Steps[i].Result
|
2020-05-20 12:02:01 +00:00
|
|
|
require.Equal(t, result.State, vm.state)
|
2022-07-08 14:28:29 +00:00
|
|
|
if result.State == vmstate.Fault { // do not compare stacks on fault
|
2019-11-06 09:15:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.InvocationStack) > 0 {
|
|
|
|
for i, s := range result.InvocationStack {
|
|
|
|
ctx := vm.istack.Peek(i).Value().(*Context)
|
|
|
|
if ctx.nextip < len(ctx.prog) {
|
|
|
|
require.Equal(t, s.InstructionPointer, ctx.nextip)
|
2020-05-20 12:01:06 +00:00
|
|
|
op, err := opcode.FromString(s.Instruction)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, op, opcode.Opcode(ctx.prog[ctx.nextip]))
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
compareStacks(t, s.EStack, vm.estack)
|
2020-07-09 12:00:49 +00:00
|
|
|
compareSlots(t, s.StaticFields, ctx.static)
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.ResultStack) != 0 {
|
|
|
|
compareStacks(t, result.ResultStack, vm.estack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func compareItems(t *testing.T, a, b stackitem.Item) {
|
2019-11-06 09:15:30 +00:00
|
|
|
switch si := a.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.BigInteger:
|
|
|
|
val := si.Value().(*big.Int).Int64()
|
2019-11-06 09:15:30 +00:00
|
|
|
switch ac := b.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.BigInteger:
|
|
|
|
require.Equal(t, val, ac.Value().(*big.Int).Int64())
|
|
|
|
case *stackitem.ByteArray:
|
2020-06-04 18:11:27 +00:00
|
|
|
require.Equal(t, val, bigint.FromBytes(ac.Value().([]byte)).Int64())
|
2021-08-03 16:37:06 +00:00
|
|
|
case stackitem.Bool:
|
2020-06-03 12:55:06 +00:00
|
|
|
if ac.Value().(bool) {
|
2019-11-06 09:15:30 +00:00
|
|
|
require.Equal(t, val, int64(1))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, val, int64(0))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
require.Fail(t, "wrong type")
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Pointer:
|
|
|
|
p, ok := b.(*stackitem.Pointer)
|
2020-05-20 12:48:19 +00:00
|
|
|
require.True(t, ok)
|
2020-06-03 12:55:06 +00:00
|
|
|
require.Equal(t, si.Position(), p.Position()) // there no script in test files
|
2021-08-11 12:16:29 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
require.Equal(t, a.Type(), b.Type())
|
|
|
|
|
|
|
|
as := a.Value().([]stackitem.Item)
|
|
|
|
bs := a.Value().([]stackitem.Item)
|
|
|
|
require.Equal(t, len(as), len(bs))
|
|
|
|
|
|
|
|
for i := range as {
|
|
|
|
compareItems(t, as[i], bs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
case *stackitem.Map:
|
|
|
|
require.Equal(t, a.Type(), b.Type())
|
|
|
|
|
|
|
|
as := a.Value().([]stackitem.MapElement)
|
|
|
|
bs := a.Value().([]stackitem.MapElement)
|
|
|
|
require.Equal(t, len(as), len(bs))
|
|
|
|
|
|
|
|
for i := range as {
|
|
|
|
compareItems(t, as[i].Key, bs[i].Key)
|
|
|
|
compareItems(t, as[i].Value, bs[i].Value)
|
|
|
|
}
|
2019-11-06 09:15:30 +00:00
|
|
|
default:
|
|
|
|
require.Equal(t, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareStacks(t *testing.T, expected []vmUTStackItem, actual *Stack) {
|
2020-06-03 12:55:06 +00:00
|
|
|
compareItemArrays(t, expected, actual.Len(), func(i int) stackitem.Item { return actual.Peek(i).Item() })
|
2020-05-21 06:38:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 12:03:01 +00:00
|
|
|
func compareSlots(t *testing.T, expected []vmUTStackItem, actual *slot) {
|
|
|
|
if (actual == nil || *actual == nil) && len(expected) == 0 {
|
2020-05-21 06:38:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NotNil(t, actual)
|
|
|
|
compareItemArrays(t, expected, actual.Size(), actual.Get)
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func compareItemArrays(t *testing.T, expected []vmUTStackItem, n int, getItem func(i int) stackitem.Item) {
|
2019-11-06 09:15:30 +00:00
|
|
|
if expected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-21 06:38:12 +00:00
|
|
|
require.Equal(t, len(expected), n)
|
2019-11-06 09:15:30 +00:00
|
|
|
for i, item := range expected {
|
2020-05-21 06:38:12 +00:00
|
|
|
it := getItem(i)
|
|
|
|
require.NotNil(t, it)
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
if item.Type == typeInterop {
|
2020-06-03 12:55:06 +00:00
|
|
|
require.IsType(t, (*stackitem.Interop)(nil), it)
|
2019-11-06 09:15:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-05-21 06:38:12 +00:00
|
|
|
compareItems(t, item.toStackItem(), it)
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func (v *vmUTStackItem) toStackItem() stackitem.Item {
|
2020-05-20 12:48:19 +00:00
|
|
|
switch v.Type.toLower() {
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeArray:
|
|
|
|
items := v.Value.([]vmUTStackItem)
|
2020-06-03 12:55:06 +00:00
|
|
|
result := make([]stackitem.Item, len(items))
|
2019-11-06 09:15:30 +00:00
|
|
|
for i := range items {
|
|
|
|
result[i] = items[i].toStackItem()
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewArray(result)
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeString:
|
|
|
|
panic("not implemented")
|
|
|
|
case typeMap:
|
2020-06-03 12:55:06 +00:00
|
|
|
return v.Value.(*stackitem.Map)
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeInterop:
|
|
|
|
panic("not implemented")
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeByteString:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewByteArray(v.Value.([]byte))
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeBuffer:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBuffer(v.Value.([]byte))
|
2020-05-20 12:48:19 +00:00
|
|
|
case typePointer:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewPointer(v.Value.(int), nil)
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeNull:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.Null{}
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeBoolean:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBool(v.Value.(bool))
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeInteger:
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBigInteger(v.Value.(*big.Int))
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeStruct:
|
|
|
|
items := v.Value.([]vmUTStackItem)
|
2020-06-03 12:55:06 +00:00
|
|
|
result := make([]stackitem.Item, len(items))
|
2019-11-06 09:15:30 +00:00
|
|
|
for i := range items {
|
|
|
|
result[i] = items[i].toStackItem()
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewStruct(result)
|
2019-11-06 09:15:30 +00:00
|
|
|
default:
|
2020-05-20 12:48:19 +00:00
|
|
|
panic(fmt.Sprintf("invalid type: %s", v.Type))
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execStep(t *testing.T, v *VM, step vmUTStep) {
|
|
|
|
for i, a := range step.Actions {
|
|
|
|
var err error
|
2020-05-20 13:07:37 +00:00
|
|
|
switch a.toLower() {
|
2019-11-06 09:15:30 +00:00
|
|
|
case vmExecute:
|
|
|
|
err = v.Run()
|
|
|
|
case vmStepInto:
|
|
|
|
err = v.StepInto()
|
|
|
|
case vmStepOut:
|
|
|
|
err = v.StepOut()
|
|
|
|
case vmStepOver:
|
|
|
|
err = v.StepOver()
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid action: %s", a))
|
|
|
|
}
|
|
|
|
|
|
|
|
// only the last action is allowed to fail
|
|
|
|
if i+1 < len(step.Actions) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func jsonStringToInteger(s string) stackitem.Item {
|
2020-05-20 14:34:32 +00:00
|
|
|
b, err := decodeHex(s)
|
|
|
|
if err == nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBigInteger(new(big.Int).SetBytes(b))
|
2020-05-20 14:34:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:48:19 +00:00
|
|
|
func (v vmUTStackItemType) toLower() vmUTStackItemType {
|
|
|
|
return vmUTStackItemType(strings.ToLower(string(v)))
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
func (v *vmUTScript) UnmarshalJSON(data []byte) error {
|
2020-05-20 12:01:06 +00:00
|
|
|
var ops []string
|
|
|
|
if err := json.Unmarshal(data, &ops); err != nil {
|
2019-11-06 09:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
var script []byte
|
|
|
|
for i := range ops {
|
|
|
|
if b, ok := decodeSingle(ops[i]); ok {
|
|
|
|
script = append(script, b...)
|
|
|
|
} else {
|
2020-08-22 19:36:10 +00:00
|
|
|
return fmt.Errorf("invalid script part: %s", ops[i])
|
2020-05-20 12:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = script
|
2019-11-06 09:15:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
func decodeSingle(s string) ([]byte, bool) {
|
|
|
|
if op, err := opcode.FromString(s); err == nil {
|
|
|
|
return []byte{byte(op)}, true
|
|
|
|
}
|
|
|
|
b, err := decodeHex(s)
|
|
|
|
return b, err == nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 13:07:37 +00:00
|
|
|
func (v vmUTActionType) toLower() vmUTActionType {
|
|
|
|
return vmUTActionType(strings.ToLower(string(v)))
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
func (v *vmUTActionType) UnmarshalJSON(data []byte) error {
|
|
|
|
return json.Unmarshal(data, (*string)(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vmUTStackItem) UnmarshalJSON(data []byte) error {
|
|
|
|
var si stackItemAUX
|
|
|
|
if err := json.Unmarshal(data, &si); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Type = si.Type
|
|
|
|
|
2020-05-20 12:48:19 +00:00
|
|
|
switch typ := si.Type.toLower(); typ {
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeArray, typeStruct:
|
|
|
|
var a []vmUTStackItem
|
|
|
|
if err := json.Unmarshal(si.Value, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = a
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeInteger, typePointer:
|
2019-11-06 09:15:30 +00:00
|
|
|
num := new(big.Int)
|
|
|
|
var a int64
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(si.Value, &a); err == nil {
|
|
|
|
num.SetInt64(a)
|
|
|
|
} else if err := json.Unmarshal(si.Value, &s); err == nil {
|
|
|
|
num.SetString(s, 10)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("invalid integer: %v", si.Value))
|
|
|
|
}
|
2020-05-20 12:48:19 +00:00
|
|
|
if typ == typePointer {
|
|
|
|
v.Value = int(num.Int64())
|
|
|
|
} else {
|
|
|
|
v.Value = num
|
|
|
|
}
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeBoolean:
|
|
|
|
var b bool
|
|
|
|
if err := json.Unmarshal(si.Value, &b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = b
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeByteString, typeBuffer:
|
2019-11-06 09:15:30 +00:00
|
|
|
b, err := decodeBytes(si.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = b
|
2020-05-20 12:48:19 +00:00
|
|
|
case typeInterop, typeNull:
|
2019-11-06 09:15:30 +00:00
|
|
|
v.Value = nil
|
|
|
|
case typeMap:
|
2020-05-21 06:52:24 +00:00
|
|
|
// we want to have the same order as in test file, so a custom decoder is used
|
|
|
|
d := json.NewDecoder(bytes.NewReader(si.Value))
|
|
|
|
if tok, err := d.Token(); err != nil || tok != json.Delim('{') {
|
|
|
|
return fmt.Errorf("invalid map start")
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
result := stackitem.NewMap()
|
2020-05-21 06:52:24 +00:00
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if tok == json.Delim('}') {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
key, ok := tok.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("string expected in map key")
|
|
|
|
}
|
|
|
|
|
|
|
|
var it vmUTStackItem
|
|
|
|
if err := d.Decode(&it); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("can't decode map value: %w", err)
|
2020-05-21 06:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item := jsonStringToInteger(key)
|
|
|
|
if item == nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
return fmt.Errorf("can't unmarshal Item %s", key)
|
2020-05-21 06:52:24 +00:00
|
|
|
}
|
|
|
|
result.Add(item, it.toStackItem())
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
2020-05-21 06:52:24 +00:00
|
|
|
v.Value = result
|
2019-11-06 09:15:30 +00:00
|
|
|
case typeString:
|
|
|
|
panic("not implemented")
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown type: %s", si.Type))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeBytes tries to decode bytes from string.
|
|
|
|
// It tries hex and base64 encodings.
|
|
|
|
func decodeBytes(data []byte) ([]byte, error) {
|
|
|
|
if len(data) == 2 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
data = data[1 : len(data)-1] // strip quotes
|
|
|
|
if b, err := decodeHex(string(data)); err == nil {
|
2019-11-06 09:15:30 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(data))
|
2022-02-22 16:27:32 +00:00
|
|
|
return io.ReadAll(r)
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
2020-05-20 12:01:06 +00:00
|
|
|
|
|
|
|
func decodeHex(s string) ([]byte, error) {
|
2021-05-12 15:44:35 +00:00
|
|
|
s = strings.TrimPrefix(s, "0x")
|
2020-05-20 12:01:06 +00:00
|
|
|
return hex.DecodeString(s)
|
|
|
|
}
|