2019-11-27 09:52:15 +00:00
|
|
|
package smartcontract
|
|
|
|
|
|
|
|
import (
|
2020-06-05 12:45:15 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2020-02-21 14:34:18 +00:00
|
|
|
"encoding/json"
|
2020-02-27 14:38:17 +00:00
|
|
|
"math"
|
2020-02-21 14:34:18 +00:00
|
|
|
"reflect"
|
2019-11-27 09:52:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-11-27 09:52:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-23 11:53:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-27 09:52:15 +00:00
|
|
|
)
|
|
|
|
|
2020-02-27 14:38:17 +00:00
|
|
|
var marshalJSONTestCases = []struct {
|
|
|
|
input Parameter
|
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: Parameter{Type: IntegerType, Value: int64(12345)},
|
|
|
|
result: `{"type":"Integer","value":12345}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{Type: StringType, Value: "Some string"},
|
|
|
|
result: `{"type":"String","value":"Some string"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{Type: BoolType, Value: true},
|
|
|
|
result: `{"type":"Boolean","value":true}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{Type: ByteArrayType, Value: []byte{0x01, 0x02, 0x03}},
|
2020-07-29 09:13:07 +00:00
|
|
|
result: `{"type":"ByteString","value":"` + hexToBase64("010203") + `"}`,
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
2020-03-04 16:47:10 +00:00
|
|
|
{
|
|
|
|
input: Parameter{Type: ByteArrayType},
|
2020-07-29 09:13:07 +00:00
|
|
|
result: `{"type":"ByteString","value":null}`,
|
2020-03-04 16:47:10 +00:00
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: PublicKeyType,
|
|
|
|
Value: []byte{0x03, 0xb3, 0xbf, 0x15, 0x02, 0xfb, 0xdc, 0x05, 0x44, 0x9b, 0x50, 0x6a, 0xaf, 0x04, 0x57, 0x97, 0x24, 0x02, 0x4b, 0x06, 0x54, 0x2e, 0x49, 0x26, 0x2b, 0xfa, 0xa3, 0xf7, 0x0e, 0x20, 0x00, 0x40, 0xa9},
|
|
|
|
},
|
|
|
|
result: `{"type":"PublicKey","value":"03b3bf1502fbdc05449b506aaf04579724024b06542e49262bfaa3f70e200040a9"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: ArrayType,
|
|
|
|
Value: []Parameter{
|
|
|
|
{Type: StringType, Value: "str 1"},
|
|
|
|
{Type: IntegerType, Value: int64(2)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: `{"type":"Array","value":[{"type":"String","value":"str 1"},{"type":"Integer","value":2}]}`,
|
|
|
|
},
|
2020-03-05 10:34:01 +00:00
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: ArrayType,
|
|
|
|
Value: []Parameter{
|
|
|
|
{Type: ByteArrayType, Value: []byte{1, 2}},
|
|
|
|
{
|
|
|
|
Type: ArrayType,
|
|
|
|
Value: []Parameter{
|
|
|
|
{Type: ByteArrayType, Value: []byte{3, 2, 1}},
|
|
|
|
{Type: ByteArrayType, Value: []byte{7, 8, 9}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
2020-07-29 09:13:07 +00:00
|
|
|
result: `{"type":"Array","value":[{"type":"ByteString","value":"` + hexToBase64("0102") + `"},{"type":"Array","value":[` +
|
|
|
|
`{"type":"ByteString","value":"` + hexToBase64("030201") + `"},{"type":"ByteString","value":"` + hexToBase64("070809") + `"}]}]}`,
|
2020-03-05 10:34:01 +00:00
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: MapType,
|
2020-03-30 15:24:06 +00:00
|
|
|
Value: []ParameterPair{
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key1"},
|
|
|
|
Value: Parameter{Type: IntegerType, Value: int64(1)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key2"},
|
|
|
|
Value: Parameter{Type: StringType, Value: "two"},
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
result: `{"type":"Map","value":[{"key":{"type":"String","value":"key1"},"value":{"type":"Integer","value":1}},{"key":{"type":"String","value":"key2"},"value":{"type":"String","value":"two"}}]}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: MapType,
|
2020-03-30 15:24:06 +00:00
|
|
|
Value: []ParameterPair{
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key1"},
|
|
|
|
Value: Parameter{Type: ArrayType, Value: []Parameter{
|
|
|
|
{Type: StringType, Value: "str 1"},
|
|
|
|
{Type: IntegerType, Value: int64(2)},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
result: `{"type":"Map","value":[{"key":{"type":"String","value":"key1"},"value":{"type":"Array","value":[{"type":"String","value":"str 1"},{"type":"Integer","value":2}]}}]}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: Hash160Type,
|
|
|
|
Value: util.Uint160{
|
2020-04-01 17:31:29 +00:00
|
|
|
0xd6, 0x24, 0x87, 0x12, 0xff, 0x97, 0x22, 0x80, 0xa0, 0xae,
|
|
|
|
0xf5, 0x24, 0x1c, 0x96, 0x4d, 0x63, 0x78, 0x29, 0xcd, 0xb,
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
result: `{"type":"Hash160","value":"0x0bcd2978634d961c24f5aea0802297ff128724d6"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: Hash256Type,
|
|
|
|
Value: util.Uint256{
|
|
|
|
0x2d, 0xf3, 0x45, 0xf2, 0x45, 0xc5, 0x98, 0x9e,
|
|
|
|
0x69, 0x95, 0x45, 0x06, 0xa5, 0x9e, 0x40, 0x12,
|
|
|
|
0xc1, 0x68, 0x54, 0x48, 0x08, 0xfc, 0xcc, 0x5b,
|
|
|
|
0x15, 0x18, 0xab, 0xa0, 0x8f, 0x30, 0x37, 0xf0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: `{"type":"Hash256","value":"0xf037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"}`,
|
|
|
|
},
|
2020-05-10 21:55:15 +00:00
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: InteropInterfaceType,
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
result: `{"type":"InteropInterface","value":null}`,
|
|
|
|
},
|
2020-06-23 13:11:49 +00:00
|
|
|
{
|
|
|
|
input: Parameter{
|
|
|
|
Type: ArrayType,
|
|
|
|
Value: []Parameter{},
|
|
|
|
},
|
|
|
|
result: `{"type":"Array","value":[]}`,
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var marshalJSONErrorCases = []Parameter{
|
|
|
|
{
|
|
|
|
Type: UnknownType,
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: IntegerType,
|
|
|
|
Value: math.Inf(1),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParam_MarshalJSON(t *testing.T) {
|
|
|
|
for _, tc := range marshalJSONTestCases {
|
2020-06-23 13:11:49 +00:00
|
|
|
res, err := json.Marshal(tc.input)
|
2020-02-27 14:38:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
var actual, expected Parameter
|
|
|
|
assert.NoError(t, json.Unmarshal(res, &actual))
|
|
|
|
assert.NoError(t, json.Unmarshal([]byte(tc.result), &expected))
|
|
|
|
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range marshalJSONErrorCases {
|
|
|
|
_, err := json.Marshal(&input)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalJSONTestCases = []struct {
|
2020-02-21 14:34:18 +00:00
|
|
|
input string
|
|
|
|
result Parameter
|
|
|
|
}{
|
2020-02-27 14:38:17 +00:00
|
|
|
{
|
|
|
|
input: `{"type":"Bool","value":true}`,
|
|
|
|
result: Parameter{Type: BoolType, Value: true},
|
|
|
|
},
|
2020-02-21 14:34:18 +00:00
|
|
|
{
|
|
|
|
input: `{"type":"Integer","value":12345}`,
|
|
|
|
result: Parameter{Type: IntegerType, Value: int64(12345)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"Integer","value":"12345"}`,
|
|
|
|
result: Parameter{Type: IntegerType, Value: int64(12345)},
|
|
|
|
},
|
|
|
|
{
|
2020-07-29 09:13:07 +00:00
|
|
|
input: `{"type":"ByteString","value":"` + hexToBase64("010203") + `"}`,
|
2020-02-21 14:34:18 +00:00
|
|
|
result: Parameter{Type: ByteArrayType, Value: []byte{0x01, 0x02, 0x03}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"String","value":"Some string"}`,
|
|
|
|
result: Parameter{Type: StringType, Value: "Some string"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"Array","value":[
|
|
|
|
{"type": "String", "value": "str 1"},
|
|
|
|
{"type": "Integer", "value": 2}]}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: ArrayType,
|
|
|
|
Value: []Parameter{
|
|
|
|
{Type: StringType, Value: "str 1"},
|
|
|
|
{Type: IntegerType, Value: int64(2)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type": "Hash160", "value": "0bcd2978634d961c24f5aea0802297ff128724d6"}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: Hash160Type,
|
|
|
|
Value: util.Uint160{
|
2020-04-01 17:31:29 +00:00
|
|
|
0xd6, 0x24, 0x87, 0x12, 0xff, 0x97, 0x22, 0x80, 0xa0, 0xae,
|
|
|
|
0xf5, 0x24, 0x1c, 0x96, 0x4d, 0x63, 0x78, 0x29, 0xcd, 0xb,
|
2020-02-21 14:34:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type": "Hash256", "value": "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: Hash256Type,
|
|
|
|
Value: util.Uint256{
|
|
|
|
0x2d, 0xf3, 0x45, 0xf2, 0x45, 0xc5, 0x98, 0x9e,
|
|
|
|
0x69, 0x95, 0x45, 0x06, 0xa5, 0x9e, 0x40, 0x12,
|
|
|
|
0xc1, 0x68, 0x54, 0x48, 0x08, 0xfc, 0xcc, 0x5b,
|
|
|
|
0x15, 0x18, 0xab, 0xa0, 0x8f, 0x30, 0x37, 0xf0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
{
|
|
|
|
input: `{"type":"Map","value":[{"key":{"type":"String","value":"key1"},"value":{"type":"Integer","value":1}},{"key":{"type":"String","value":"key2"},"value":{"type":"String","value":"two"}}]}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: MapType,
|
2020-03-30 15:24:06 +00:00
|
|
|
Value: []ParameterPair{
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key1"},
|
|
|
|
Value: Parameter{Type: IntegerType, Value: int64(1)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key2"},
|
|
|
|
Value: Parameter{Type: StringType, Value: "two"},
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"Map","value":[{"key":{"type":"String","value":"key1"},"value":{"type":"Array","value":[{"type":"String","value":"str 1"},{"type":"Integer","value":2}]}}]}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: MapType,
|
2020-03-30 15:24:06 +00:00
|
|
|
Value: []ParameterPair{
|
|
|
|
{
|
|
|
|
Key: Parameter{Type: StringType, Value: "key1"},
|
|
|
|
Value: Parameter{Type: ArrayType, Value: []Parameter{
|
|
|
|
{Type: StringType, Value: "str 1"},
|
|
|
|
{Type: IntegerType, Value: int64(2)},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-27 14:38:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
result: Parameter{
|
|
|
|
Type: PublicKeyType,
|
|
|
|
Value: []byte{0x03, 0xb3, 0xbf, 0x15, 0x02, 0xfb, 0xdc, 0x05, 0x44, 0x9b, 0x50, 0x6a, 0xaf, 0x04, 0x57, 0x97, 0x24, 0x02, 0x4b, 0x06, 0x54, 0x2e, 0x49, 0x26, 0x2b, 0xfa, 0xa3, 0xf7, 0x0e, 0x20, 0x00, 0x40, 0xa9},
|
|
|
|
},
|
|
|
|
input: `{"type":"PublicKey","value":"03b3bf1502fbdc05449b506aaf04579724024b06542e49262bfaa3f70e200040a9"}`,
|
|
|
|
},
|
2020-05-10 21:55:15 +00:00
|
|
|
{
|
|
|
|
input: `{"type":"InteropInterface","value":null}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: InteropInterfaceType,
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"InteropInterface","value":""}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: InteropInterfaceType,
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"type":"InteropInterface","value":"Hundertwasser"}`,
|
|
|
|
result: Parameter{
|
|
|
|
Type: InteropInterfaceType,
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
},
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 14:38:17 +00:00
|
|
|
var unmarshalJSONErrorCases = []string{
|
2020-07-29 09:13:07 +00:00
|
|
|
`{"type": "ByteString","value":`, // incorrect JSON
|
|
|
|
`{"type": "ByteString","value":1}`, // incorrect Value
|
|
|
|
`{"type": "ByteString","value":"12^"}`, // incorrect ByteArray value
|
2020-02-21 14:34:18 +00:00
|
|
|
`{"type": "String","value":`, // incorrect JSON
|
|
|
|
`{"type": "String","value":1}`, // incorrect Value
|
|
|
|
`{"type": "Integer","value": "nn"}`, // incorrect Integer value
|
|
|
|
`{"type": "Integer","value": []}`, // incorrect Integer value
|
|
|
|
`{"type": "Array","value": 123}`, // incorrect Array value
|
|
|
|
`{"type": "Hash160","value": "0bcd"}`, // incorrect Uint160 value
|
|
|
|
`{"type": "Hash256","value": "0bcd"}`, // incorrect Uint256 value
|
|
|
|
`{"type": "Stringg","value": ""}`, // incorrect type
|
|
|
|
`{"type": {},"value": ""}`, // incorrect value
|
2020-02-27 14:38:17 +00:00
|
|
|
`{"type": "Boolean","value": qwerty}`, // incorrect Bool value
|
|
|
|
`{"type": "Boolean","value": ""}`, // incorrect Bool value
|
|
|
|
`{"type": "Map","value": ["key": {}]}`, // incorrect Map value
|
|
|
|
`{"type": "Map","value": ["key": {"type":"String", "value":"qwer"}, "value": {"type":"Boolean"}]}`, // incorrect Map Value value
|
|
|
|
`{"type": "Map","value": ["key": {"type":"String"}, "value": {"type":"Boolean", "value":true}]}`, // incorrect Map Key value
|
2020-02-21 14:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParam_UnmarshalJSON(t *testing.T) {
|
|
|
|
var s Parameter
|
2020-02-27 14:38:17 +00:00
|
|
|
for _, tc := range unmarshalJSONTestCases {
|
2020-02-21 14:34:18 +00:00
|
|
|
assert.NoError(t, json.Unmarshal([]byte(tc.input), &s))
|
|
|
|
assert.Equal(t, s, tc.result)
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:38:17 +00:00
|
|
|
for _, input := range unmarshalJSONErrorCases {
|
2020-02-21 14:34:18 +00:00
|
|
|
assert.Error(t, json.Unmarshal([]byte(input), &s))
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:34:18 +00:00
|
|
|
var tryParseTestCases = []struct {
|
|
|
|
input interface{}
|
|
|
|
expected interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: []byte{
|
|
|
|
0x0b, 0xcd, 0x29, 0x78, 0x63, 0x4d, 0x96, 0x1c, 0x24, 0xf5,
|
|
|
|
0xae, 0xa0, 0x80, 0x22, 0x97, 0xff, 0x12, 0x87, 0x24, 0xd6,
|
|
|
|
},
|
|
|
|
expected: util.Uint160{
|
|
|
|
0x0b, 0xcd, 0x29, 0x78, 0x63, 0x4d, 0x96, 0x1c, 0x24, 0xf5,
|
|
|
|
0xae, 0xa0, 0x80, 0x22, 0x97, 0xff, 0x12, 0x87, 0x24, 0xd6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []byte{
|
|
|
|
0xf0, 0x37, 0x30, 0x8f, 0xa0, 0xab, 0x18, 0x15,
|
|
|
|
0x5b, 0xcc, 0xfc, 0x08, 0x48, 0x54, 0x68, 0xc1,
|
|
|
|
0x12, 0x40, 0x9e, 0xa5, 0x06, 0x45, 0x95, 0x69,
|
|
|
|
0x9e, 0x98, 0xc5, 0x45, 0xf2, 0x45, 0xf3, 0x2d,
|
|
|
|
},
|
|
|
|
expected: util.Uint256{
|
|
|
|
0x2d, 0xf3, 0x45, 0xf2, 0x45, 0xc5, 0x98, 0x9e,
|
|
|
|
0x69, 0x95, 0x45, 0x06, 0xa5, 0x9e, 0x40, 0x12,
|
|
|
|
0xc1, 0x68, 0x54, 0x48, 0x08, 0xfc, 0xcc, 0x5b,
|
|
|
|
0x15, 0x18, 0xab, 0xa0, 0x8f, 0x30, 0x37, 0xf0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []byte{0, 1, 2, 3, 4, 9, 8, 6},
|
|
|
|
expected: []byte{0, 1, 2, 3, 4, 9, 8, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []byte{0x63, 0x78, 0x29, 0xcd, 0x0b},
|
|
|
|
expected: int64(50686687331),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []byte("this is a test string"),
|
|
|
|
expected: "this is a test string",
|
|
|
|
},
|
|
|
|
}
|
2019-11-27 09:52:15 +00:00
|
|
|
|
2020-02-21 14:34:18 +00:00
|
|
|
func TestParam_TryParse(t *testing.T) {
|
|
|
|
for _, tc := range tryParseTestCases {
|
|
|
|
t.Run(reflect.TypeOf(tc.expected).String(), func(t *testing.T) {
|
|
|
|
input := Parameter{
|
|
|
|
Type: ByteArrayType,
|
|
|
|
Value: tc.input,
|
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.New(reflect.TypeOf(tc.expected))
|
|
|
|
assert.NoError(t, input.TryParse(val.Interface()))
|
|
|
|
assert.Equal(t, tc.expected, val.Elem().Interface())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("[]Uint160", func(t *testing.T) {
|
|
|
|
exp1 := util.Uint160{1, 2, 3, 4, 5}
|
|
|
|
exp2 := util.Uint160{9, 8, 7, 6, 5}
|
|
|
|
|
|
|
|
params := Params{
|
|
|
|
{
|
|
|
|
Type: ByteArrayType,
|
|
|
|
Value: exp1.BytesBE(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ByteArrayType,
|
|
|
|
Value: exp2.BytesBE(),
|
|
|
|
},
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
|
|
|
|
var out1, out2 util.Uint160
|
|
|
|
|
|
|
|
assert.NoError(t, params.TryParseArray(&out1, &out2))
|
|
|
|
assert.Equal(t, exp1, out1)
|
|
|
|
assert.Equal(t, exp2, out2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamType_String(t *testing.T) {
|
|
|
|
types := []ParamType{
|
|
|
|
SignatureType,
|
|
|
|
BoolType,
|
|
|
|
IntegerType,
|
|
|
|
Hash160Type,
|
|
|
|
Hash256Type,
|
|
|
|
ByteArrayType,
|
|
|
|
PublicKeyType,
|
|
|
|
StringType,
|
|
|
|
ArrayType,
|
|
|
|
InteropInterfaceType,
|
|
|
|
MapType,
|
|
|
|
VoidType,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, exp := range types {
|
|
|
|
actual, err := ParseParamType(exp.String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, exp, actual)
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
|
|
|
|
actual, err := ParseParamType(UnknownType.String())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, UnknownType, actual)
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParameterFromString(t *testing.T) {
|
|
|
|
var inouts = []struct {
|
|
|
|
in string
|
|
|
|
out Parameter
|
|
|
|
err bool
|
|
|
|
}{{
|
|
|
|
in: "qwerty",
|
|
|
|
out: Parameter{StringType, "qwerty"},
|
|
|
|
}, {
|
|
|
|
in: "42",
|
2020-03-18 08:57:33 +00:00
|
|
|
out: Parameter{IntegerType, int64(42)},
|
2019-11-27 09:52:15 +00:00
|
|
|
}, {
|
|
|
|
in: "Hello, 世界",
|
|
|
|
out: Parameter{StringType, "Hello, 世界"},
|
|
|
|
}, {
|
|
|
|
in: `\4\2`,
|
2020-03-18 08:57:33 +00:00
|
|
|
out: Parameter{IntegerType, int64(42)},
|
2019-11-27 09:52:15 +00:00
|
|
|
}, {
|
|
|
|
in: `\\4\2`,
|
|
|
|
out: Parameter{StringType, `\42`},
|
|
|
|
}, {
|
|
|
|
in: `\\\4\2`,
|
|
|
|
out: Parameter{StringType, `\42`},
|
|
|
|
}, {
|
|
|
|
in: "int:42",
|
2020-03-18 08:57:33 +00:00
|
|
|
out: Parameter{IntegerType, int64(42)},
|
2019-11-27 09:52:15 +00:00
|
|
|
}, {
|
|
|
|
in: "true",
|
|
|
|
out: Parameter{BoolType, true},
|
|
|
|
}, {
|
|
|
|
in: "string:true",
|
|
|
|
out: Parameter{StringType, "true"},
|
|
|
|
}, {
|
|
|
|
in: "\xfe\xff",
|
|
|
|
err: true,
|
|
|
|
}, {
|
|
|
|
in: `string\:true`,
|
|
|
|
out: Parameter{StringType, "string:true"},
|
|
|
|
}, {
|
|
|
|
in: "string:true:true",
|
|
|
|
out: Parameter{StringType, "true:true"},
|
|
|
|
}, {
|
|
|
|
in: `string\\:true`,
|
|
|
|
err: true,
|
|
|
|
}, {
|
|
|
|
in: `qwerty:asdf`,
|
|
|
|
err: true,
|
|
|
|
}, {
|
|
|
|
in: `bool:asdf`,
|
|
|
|
err: true,
|
2020-02-21 14:34:18 +00:00
|
|
|
}, {
|
|
|
|
in: `InteropInterface:123`,
|
|
|
|
err: true,
|
|
|
|
}, {
|
|
|
|
in: `Map:[]`,
|
|
|
|
err: true,
|
2019-11-27 09:52:15 +00:00
|
|
|
}}
|
|
|
|
for _, inout := range inouts {
|
|
|
|
out, err := NewParameterFromString(inout.in)
|
|
|
|
if inout.err {
|
|
|
|
assert.NotNil(t, err, "should error on '%s' input", inout.in)
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err, "shouldn't error on '%s' input", inout.in)
|
|
|
|
assert.Equal(t, inout.out, *out, "bad output for '%s' input", inout.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 11:53:17 +00:00
|
|
|
|
|
|
|
func TestEncodeDecodeBinary(t *testing.T) {
|
|
|
|
for _, tc := range marshalJSONTestCases {
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, &tc.input, new(Parameter))
|
2020-03-23 11:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("unknown", func(t *testing.T) {
|
|
|
|
p := Parameter{Type: UnknownType}
|
2020-03-26 14:43:24 +00:00
|
|
|
_, err := testserdes.EncodeBinary(&p)
|
|
|
|
require.Error(t, err)
|
2020-03-23 11:53:17 +00:00
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary([]byte{0xAA}, &p))
|
2020-03-23 11:53:17 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-05 12:45:15 +00:00
|
|
|
|
|
|
|
func hexToBase64(s string) string {
|
|
|
|
b, _ := hex.DecodeString(s)
|
|
|
|
return base64.StdEncoding.EncodeToString(b)
|
|
|
|
}
|