status-go/abi-spec/core_test.go

134 lines
10 KiB
Go

package abispec
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestMethodPattern(t *testing.T) {
results := methodPattern.FindStringSubmatch("sam(bytes,bool,uint256[])")
require.Equal(t, "sam(bytes,bool,uint256[])", results[0])
require.Equal(t, "sam", results[1])
require.Equal(t, "bytes,bool,uint256[]", results[2])
results = methodPattern.FindStringSubmatch("s(bytes,bool,uint256[])")
require.Equal(t, "s(bytes,bool,uint256[])", results[0])
require.Equal(t, "s", results[1])
require.Equal(t, "bytes,bool,uint256[]", results[2])
results = methodPattern.FindStringSubmatch("s1(bytes,bool,uint256[])")
require.Equal(t, "s1(bytes,bool,uint256[])", results[0])
require.Equal(t, "s1", results[1])
require.Equal(t, "bytes,bool,uint256[]", results[2])
results = methodPattern.FindStringSubmatch("1s(bytes,bool,uint256[])")
require.Len(t, results, 0)
}
func TestEncodeTransfer(t *testing.T) {
result, err := EncodeTransfer("0x6f5f90fb1dD8E406F233442935F689bA7D5701b2", "10000")
require.NoError(t, err)
require.Equal(t, "0xa9059cbb0000000000000000000000006f5f90fb1dd8e406f233442935f689ba7d5701b20000000000000000000000000000000000000000000000000000000000002710", result)
result, err = EncodeTransfer("0x6f5f90fb1dD8E406F233442935F689bA7D5701b2", "0x2710")
require.NoError(t, err)
require.Equal(t, "0xa9059cbb0000000000000000000000006f5f90fb1dd8e406f233442935f689ba7d5701b20000000000000000000000000000000000000000000000000000000000002710", result)
}
func TestEncode(t *testing.T) {
result, err := Encode("baz(uint32,bool)", "[69,true]")
require.NoError(t, err)
require.Equal(t, "0xcdcd77c000000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001", result)
result, err = Encode("baz(uint256,bool)", "[69,true]")
require.NoError(t, err)
require.Equal(t, "0x72ed38b600000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001", result)
result, err = Encode("bar(bytes3[2])", `[["abc","def"]]`)
require.NoError(t, err)
require.Equal(t, "0xfce353f661626300000000000000000000000000000000000000000000000000000000006465660000000000000000000000000000000000000000000000000000000000", result)
result, err = Encode("sam(bytes)", `["dave"]`)
require.NoError(t, err)
require.Equal(t, "0x05e73fb9000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000046461766500000000000000000000000000000000000000000000000000000000", result)
result, err = Encode("sam(bytes,bool,uint256[])", `["dave",true,[1,2,3]]`)
require.NoError(t, err)
require.Equal(t, "0xa5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", result)
result, err = Encode("f(bytes10)", `["1234567890"]`)
require.NoError(t, err)
require.Equal(t, "0x6350865e3132333435363738393000000000000000000000000000000000000000000000", result)
result, err = Encode("f(uint256,uint32[],bytes10,bytes)", `[291,[1110,1929],"1234567890","Hello, world!"]`)
require.NoError(t, err)
require.Equal(t, "0x8be6524600000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000080313233343536373839300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004560000000000000000000000000000000000000000000000000000000000000789000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000", result)
result, err = Encode("getExpectedRate(address,address,uint256)", `["0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",1000]`)
require.NoError(t, err)
require.Equal(t, "0x809a9e55000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000003e8", result)
result, err = Encode("f(uint256)", `["115792089237316195423570985008687907853269984665640564039457584007913129639935"]`)
require.NoError(t, err)
require.Equal(t, "0xb3de648bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", result)
//TODO following case would fail cause go-ethereum does not support type uint, should we fix this?
//result, err = Encode("g(uint[][],string[])", `[[[1,2],[3]],["one","two","three"]]`)
//require.NoError(t, err)
//require.Equal(t, "0xad6a3446000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000036f6e650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000374776f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000057468726565000000000000000000000000000000000000000000000000000000", result)
}
func TestDecode(t *testing.T) {
out, err := Decode("0x000000000000000000000000000000000000000000000000000000005bc741cd00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000013b86dbf1a83c9e6a492914a0ee39e8a5b7eb60700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e516d533152484e4a57414b356e426f6f57454d34654d644268707a35666e325764557473457357754a4b79356147000000000000000000000000000000000000",
[]string{"uint256", "bytes", "address", "uint256", "uint256"})
require.NoError(t, err)
bytes, err := json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `[1539785165,"0x516d533152484e4a57414b356e426f6f57454d34654d644268707a35666e325764557473457357754a4b79356147","0x13b86dbf1a83c9e6a492914a0ee39e8a5b7eb607",0,0]`, string(bytes))
out, err = Decode("0x0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d7621dc58210001",
[]string{"uint256", "uint256"})
require.NoError(t, err)
bytes, err = json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `["1000000000000000000","970000000000000001"]`, string(bytes))
out, err = Decode("0x000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000003e8",
[]string{"address", "address", "uint256"})
require.NoError(t, err)
bytes, err = json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `["0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",1000]`, string(bytes))
out, err = Decode("0x00000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001",
[]string{"uint32", "bool"})
require.NoError(t, err)
bytes, err = json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `[69,true]`, string(bytes))
out, err = Decode("0x61626300000000000000000000000000000000000000000000000000000000006465660000000000000000000000000000000000000000000000000000000000",
[]string{"bytes3[2]"})
require.NoError(t, err)
bytes, err = json.Marshal(out[0])
require.NoError(t, err)
require.JSONEq(t, `["616263","646566"]`, string(bytes))
out, err = Decode("0x0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
[]string{"string", "bool", "uint256[]"})
require.NoError(t, err)
bytes, err = json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `["dave",true,[1,2,3]]`, string(bytes))
out, err = Decode("0x00000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000080313233343536373839300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004560000000000000000000000000000000000000000000000000000000000000789000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000",
[]string{"uint256", "uint32[]", "bytes10", "bytes"})
require.NoError(t, err)
bytes, err = json.Marshal(out)
require.NoError(t, err)
require.JSONEq(t, `[291,[1110,1929],"31323334353637383930","0x48656c6c6f2c20776f726c6421"]`, string(bytes))
}