2020-06-24 13:47:57 +00:00
|
|
|
package nef
|
|
|
|
|
|
|
|
import (
|
2021-01-13 12:26:35 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2021-01-19 07:55:34 +00:00
|
|
|
"errors"
|
2021-01-13 12:26:35 +00:00
|
|
|
"strconv"
|
2020-06-24 13:47:57 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-01-18 14:05:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2021-01-19 07:55:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-01-18 14:05:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-24 13:47:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeBinary(t *testing.T) {
|
|
|
|
script := []byte{12, 32, 84, 35, 14}
|
|
|
|
expected := &File{
|
|
|
|
Header: Header{
|
|
|
|
Magic: Magic,
|
2021-01-19 08:08:34 +00:00
|
|
|
Compiler: "best compiler version 1",
|
2020-06-24 13:47:57 +00:00
|
|
|
},
|
2021-01-18 14:05:11 +00:00
|
|
|
Tokens: []MethodToken{{
|
|
|
|
Hash: random.Uint160(),
|
|
|
|
Method: "method",
|
|
|
|
ParamCount: 3,
|
|
|
|
HasReturn: true,
|
|
|
|
CallFlag: callflag.WriteStates,
|
|
|
|
}},
|
2020-06-24 13:47:57 +00:00
|
|
|
Script: script,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("invalid Magic", func(t *testing.T) {
|
|
|
|
expected.Header.Magic = 123
|
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid checksum", func(t *testing.T) {
|
|
|
|
expected.Header.Magic = Magic
|
|
|
|
expected.Checksum = 123
|
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
2020-11-18 09:57:44 +00:00
|
|
|
t.Run("zero-length script", func(t *testing.T) {
|
|
|
|
expected.Script = make([]byte, 0)
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-11-18 09:57:44 +00:00
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
2020-06-24 13:47:57 +00:00
|
|
|
t.Run("invalid script length", func(t *testing.T) {
|
|
|
|
newScript := make([]byte, MaxScriptLength+1)
|
|
|
|
expected.Script = newScript
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
2021-01-18 14:05:11 +00:00
|
|
|
t.Run("invalid tokens list", func(t *testing.T) {
|
|
|
|
expected.Script = script
|
|
|
|
expected.Tokens[0].Method = "_reserved"
|
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
2020-06-24 13:47:57 +00:00
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
expected.Script = script
|
2021-01-18 14:05:11 +00:00
|
|
|
expected.Tokens[0].Method = "method"
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
expected.Header.Magic = Magic
|
|
|
|
testserdes.EncodeDecodeBinary(t, expected, &File{})
|
|
|
|
})
|
2021-09-09 14:03:11 +00:00
|
|
|
t.Run("positive with empty tokens", func(t *testing.T) {
|
|
|
|
expected.Script = script
|
|
|
|
// Check `Tokens` won't be deserialized to `nil`. We expect them to be non-nil slice in the related code.
|
|
|
|
expected.Tokens = []MethodToken{}
|
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
|
|
|
expected.Header.Magic = Magic
|
|
|
|
testserdes.EncodeDecodeBinary(t, expected, &File{})
|
|
|
|
})
|
2021-01-19 07:55:34 +00:00
|
|
|
t.Run("invalid reserved bytes", func(t *testing.T) {
|
|
|
|
expected.Script = script
|
|
|
|
expected.Tokens = expected.Tokens[:0]
|
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
|
|
|
bytes, err := testserdes.EncodeBinary(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sz := io.GetVarSize(&expected.Header)
|
|
|
|
bytes[sz] = 1
|
|
|
|
err = testserdes.DecodeBinary(bytes, new(File))
|
|
|
|
require.True(t, errors.Is(err, errInvalidReserved), "got: %v", err)
|
|
|
|
|
|
|
|
bytes[sz] = 0
|
|
|
|
bytes[sz+3] = 1
|
|
|
|
err = testserdes.DecodeBinary(bytes, new(File))
|
|
|
|
require.True(t, errors.Is(err, errInvalidReserved), "got: %v", err)
|
|
|
|
})
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkDecodeError(t *testing.T, expected *File) {
|
|
|
|
bytes, err := testserdes.EncodeBinary(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Error(t, testserdes.DecodeBinary(bytes, &File{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesFromBytes(t *testing.T) {
|
|
|
|
script := []byte{12, 32, 84, 35, 14}
|
|
|
|
expected := File{
|
|
|
|
Header: Header{
|
|
|
|
Magic: Magic,
|
2021-01-19 08:08:34 +00:00
|
|
|
Compiler: "best compiler version 1",
|
2020-06-24 13:47:57 +00:00
|
|
|
},
|
2021-01-18 14:05:11 +00:00
|
|
|
Tokens: []MethodToken{{
|
|
|
|
Hash: random.Uint160(),
|
|
|
|
Method: "someMethod",
|
|
|
|
ParamCount: 3,
|
|
|
|
HasReturn: true,
|
|
|
|
CallFlag: callflag.WriteStates,
|
|
|
|
}},
|
2020-06-24 13:47:57 +00:00
|
|
|
Script: script,
|
|
|
|
}
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
|
|
|
|
bytes, err := expected.Bytes()
|
|
|
|
require.NoError(t, err)
|
|
|
|
actual, err := FileFromBytes(bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
2021-01-13 12:26:35 +00:00
|
|
|
|
|
|
|
func TestMarshalUnmarshalJSON(t *testing.T) {
|
|
|
|
expected := &File{
|
|
|
|
Header: Header{
|
|
|
|
Magic: Magic,
|
2021-01-19 08:08:34 +00:00
|
|
|
Compiler: "test.compiler-test.ver",
|
2021-01-13 12:26:35 +00:00
|
|
|
},
|
2021-01-18 14:05:11 +00:00
|
|
|
Tokens: []MethodToken{{
|
|
|
|
Hash: util.Uint160{0x12, 0x34, 0x56, 0x78, 0x91, 0x00},
|
|
|
|
Method: "someMethod",
|
|
|
|
ParamCount: 3,
|
|
|
|
HasReturn: true,
|
|
|
|
CallFlag: callflag.WriteStates,
|
|
|
|
}},
|
2021-01-13 12:26:35 +00:00
|
|
|
Script: []byte{1, 2, 3, 4},
|
|
|
|
}
|
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
|
|
|
|
|
|
|
data, err := json.Marshal(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, `{
|
|
|
|
"magic":`+strconv.FormatUint(uint64(Magic), 10)+`,
|
2021-01-19 08:08:34 +00:00
|
|
|
"compiler": "test.compiler-test.ver",
|
2021-09-23 21:19:37 +00:00
|
|
|
"source": "",
|
2021-01-18 14:05:11 +00:00
|
|
|
"tokens": [
|
|
|
|
{
|
|
|
|
"hash": "0x`+expected.Tokens[0].Hash.StringLE()+`",
|
|
|
|
"method": "someMethod",
|
|
|
|
"paramcount": 3,
|
|
|
|
"hasreturnvalue": true,
|
2021-07-05 15:23:16 +00:00
|
|
|
"callflags": "`+expected.Tokens[0].CallFlag.String()+`"
|
2021-01-18 14:05:11 +00:00
|
|
|
}
|
|
|
|
],
|
2021-01-13 12:26:35 +00:00
|
|
|
"script": "`+base64.StdEncoding.EncodeToString(expected.Script)+`",
|
|
|
|
"checksum":`+strconv.FormatUint(uint64(expected.Checksum), 10)+`}`, string(data))
|
|
|
|
|
|
|
|
actual := new(File)
|
|
|
|
require.NoError(t, json.Unmarshal(data, actual))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|