github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/abi/unpack_test.go (about)

     1  package abi
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"math/big"
     8  	"reflect"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/quickchainproject/quickchain/common"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  type unpackTest struct {
    18  	def  string      // ABI definition JSON
    19  	enc  string      // evm return data
    20  	want interface{} // the expected output
    21  	err  string      // empty or error if expected
    22  }
    23  
    24  func (test unpackTest) checkError(err error) error {
    25  	if err != nil {
    26  		if len(test.err) == 0 {
    27  			return fmt.Errorf("expected no err but got: %v", err)
    28  		} else if err.Error() != test.err {
    29  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    30  		}
    31  	} else if len(test.err) > 0 {
    32  		return fmt.Errorf("expected err: %v but got none", test.err)
    33  	}
    34  	return nil
    35  }
    36  
    37  var unpackTests = []unpackTest{
    38  	{
    39  		def:  `[{ "type": "bool" }]`,
    40  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    41  		want: true,
    42  	},
    43  	{
    44  		def:  `[{ "type": "bool" }]`,
    45  		enc:  "0000000000000000000000000000000000000000000000000000000000000000",
    46  		want: false,
    47  	},
    48  	{
    49  		def:  `[{ "type": "bool" }]`,
    50  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    51  		want: false,
    52  		err:  "abi: improperly encoded boolean value",
    53  	},
    54  	{
    55  		def:  `[{ "type": "bool" }]`,
    56  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    57  		want: false,
    58  		err:  "abi: improperly encoded boolean value",
    59  	},
    60  	{
    61  		def:  `[{"type": "uint32"}]`,
    62  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    63  		want: uint32(1),
    64  	},
    65  	{
    66  		def:  `[{"type": "uint32"}]`,
    67  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    68  		want: uint16(0),
    69  		err:  "abi: cannot unmarshal uint32 in to uint16",
    70  	},
    71  	{
    72  		def:  `[{"type": "uint17"}]`,
    73  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    74  		want: uint16(0),
    75  		err:  "abi: cannot unmarshal *big.Int in to uint16",
    76  	},
    77  	{
    78  		def:  `[{"type": "uint17"}]`,
    79  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    80  		want: big.NewInt(1),
    81  	},
    82  	{
    83  		def:  `[{"type": "int32"}]`,
    84  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    85  		want: int32(1),
    86  	},
    87  	{
    88  		def:  `[{"type": "int32"}]`,
    89  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    90  		want: int16(0),
    91  		err:  "abi: cannot unmarshal int32 in to int16",
    92  	},
    93  	{
    94  		def:  `[{"type": "int17"}]`,
    95  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    96  		want: int16(0),
    97  		err:  "abi: cannot unmarshal *big.Int in to int16",
    98  	},
    99  	{
   100  		def:  `[{"type": "int17"}]`,
   101  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   102  		want: big.NewInt(1),
   103  	},
   104  	{
   105  		def:  `[{"type": "address"}]`,
   106  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   107  		want: common.Address{1},
   108  	},
   109  	{
   110  		def:  `[{"type": "bytes32"}]`,
   111  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   112  		want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   113  	},
   114  	{
   115  		def:  `[{"type": "bytes"}]`,
   116  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   117  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   118  	},
   119  	{
   120  		def:  `[{"type": "bytes"}]`,
   121  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   122  		want: [32]byte{},
   123  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   124  	},
   125  	{
   126  		def:  `[{"type": "bytes32"}]`,
   127  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   128  		want: []byte(nil),
   129  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   130  	},
   131  	{
   132  		def:  `[{"type": "bytes32"}]`,
   133  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   134  		want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   135  	},
   136  	{
   137  		def:  `[{"type": "function"}]`,
   138  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   139  		want: [24]byte{1},
   140  	},
   141  	// slices
   142  	{
   143  		def:  `[{"type": "uint8[]"}]`,
   144  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   145  		want: []uint8{1, 2},
   146  	},
   147  	{
   148  		def:  `[{"type": "uint8[2]"}]`,
   149  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   150  		want: [2]uint8{1, 2},
   151  	},
   152  	// multi dimensional, if these pass, all types that don't require length prefix should pass
   153  	{
   154  		def:  `[{"type": "uint8[][]"}]`,
   155  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   156  		want: [][]uint8{{1, 2}, {1, 2}},
   157  	},
   158  	{
   159  		def:  `[{"type": "uint8[2][2]"}]`,
   160  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   161  		want: [2][2]uint8{{1, 2}, {1, 2}},
   162  	},
   163  	{
   164  		def:  `[{"type": "uint8[][2]"}]`,
   165  		enc:  "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   166  		want: [2][]uint8{{1}, {1}},
   167  	},
   168  	{
   169  		def:  `[{"type": "uint8[2][]"}]`,
   170  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   171  		want: [][2]uint8{{1, 2}},
   172  	},
   173  	{
   174  		def:  `[{"type": "uint16[]"}]`,
   175  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   176  		want: []uint16{1, 2},
   177  	},
   178  	{
   179  		def:  `[{"type": "uint16[2]"}]`,
   180  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   181  		want: [2]uint16{1, 2},
   182  	},
   183  	{
   184  		def:  `[{"type": "uint32[]"}]`,
   185  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   186  		want: []uint32{1, 2},
   187  	},
   188  	{
   189  		def:  `[{"type": "uint32[2]"}]`,
   190  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   191  		want: [2]uint32{1, 2},
   192  	},
   193  	{
   194  		def:  `[{"type": "uint32[2][3][4]"}]`,
   195  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
   196  		want: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}},
   197  	},
   198  	{
   199  		def:  `[{"type": "uint64[]"}]`,
   200  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   201  		want: []uint64{1, 2},
   202  	},
   203  	{
   204  		def:  `[{"type": "uint64[2]"}]`,
   205  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   206  		want: [2]uint64{1, 2},
   207  	},
   208  	{
   209  		def:  `[{"type": "uint256[]"}]`,
   210  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   211  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   212  	},
   213  	{
   214  		def:  `[{"type": "uint256[3]"}]`,
   215  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   216  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   217  	},
   218  	{
   219  		def:  `[{"type": "int8[]"}]`,
   220  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   221  		want: []int8{1, 2},
   222  	},
   223  	{
   224  		def:  `[{"type": "int8[2]"}]`,
   225  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   226  		want: [2]int8{1, 2},
   227  	},
   228  	{
   229  		def:  `[{"type": "int16[]"}]`,
   230  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   231  		want: []int16{1, 2},
   232  	},
   233  	{
   234  		def:  `[{"type": "int16[2]"}]`,
   235  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   236  		want: [2]int16{1, 2},
   237  	},
   238  	{
   239  		def:  `[{"type": "int32[]"}]`,
   240  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   241  		want: []int32{1, 2},
   242  	},
   243  	{
   244  		def:  `[{"type": "int32[2]"}]`,
   245  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   246  		want: [2]int32{1, 2},
   247  	},
   248  	{
   249  		def:  `[{"type": "int64[]"}]`,
   250  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   251  		want: []int64{1, 2},
   252  	},
   253  	{
   254  		def:  `[{"type": "int64[2]"}]`,
   255  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   256  		want: [2]int64{1, 2},
   257  	},
   258  	{
   259  		def:  `[{"type": "int256[]"}]`,
   260  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   261  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   262  	},
   263  	{
   264  		def:  `[{"type": "int256[3]"}]`,
   265  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   266  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   267  	},
   268  	// struct outputs
   269  	{
   270  		def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
   271  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   272  		want: struct {
   273  			Int1 *big.Int
   274  			Int2 *big.Int
   275  		}{big.NewInt(1), big.NewInt(2)},
   276  	},
   277  	{
   278  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   279  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   280  		want: struct {
   281  			Int1 *big.Int
   282  			Int2 *big.Int
   283  		}{},
   284  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   285  	},
   286  	{
   287  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   288  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   289  		want: struct {
   290  			Int1 *big.Int
   291  			Int2 *big.Int
   292  		}{},
   293  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   294  	},
   295  	{
   296  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   297  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   298  		want: struct {
   299  			Int1 *big.Int
   300  			Int2 *big.Int
   301  		}{},
   302  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   303  	},
   304  	{
   305  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   306  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   307  		want: struct {
   308  			Int1 *big.Int
   309  			Int2 *big.Int
   310  		}{},
   311  		err: "abi: purely underscored output cannot unpack to struct",
   312  	},
   313  }
   314  
   315  func TestUnpack(t *testing.T) {
   316  	for i, test := range unpackTests {
   317  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   318  			def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   319  			abi, err := JSON(strings.NewReader(def))
   320  			if err != nil {
   321  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   322  			}
   323  			encb, err := hex.DecodeString(test.enc)
   324  			if err != nil {
   325  				t.Fatalf("invalid hex: %s" + test.enc)
   326  			}
   327  			outptr := reflect.New(reflect.TypeOf(test.want))
   328  			err = abi.Unpack(outptr.Interface(), "method", encb)
   329  			if err := test.checkError(err); err != nil {
   330  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   331  				return
   332  			}
   333  			out := outptr.Elem().Interface()
   334  			if !reflect.DeepEqual(test.want, out) {
   335  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   336  			}
   337  		})
   338  	}
   339  }
   340  
   341  type methodMultiOutput struct {
   342  	Int    *big.Int
   343  	String string
   344  }
   345  
   346  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   347  	const definition = `[
   348  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   349  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   350  
   351  	abi, err := JSON(strings.NewReader(definition))
   352  	require.NoError(err)
   353  	// using buff to make the code readable
   354  	buff := new(bytes.Buffer)
   355  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   356  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   357  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   358  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   359  	return abi, buff.Bytes(), expected
   360  }
   361  
   362  func TestMethodMultiReturn(t *testing.T) {
   363  	type reversed struct {
   364  		String string
   365  		Int    *big.Int
   366  	}
   367  
   368  	abi, data, expected := methodMultiReturn(require.New(t))
   369  	bigint := new(big.Int)
   370  	var testCases = []struct {
   371  		dest     interface{}
   372  		expected interface{}
   373  		error    string
   374  		name     string
   375  	}{{
   376  		&methodMultiOutput{},
   377  		&expected,
   378  		"",
   379  		"Can unpack into structure",
   380  	}, {
   381  		&reversed{},
   382  		&reversed{expected.String, expected.Int},
   383  		"",
   384  		"Can unpack into reversed structure",
   385  	}, {
   386  		&[]interface{}{&bigint, new(string)},
   387  		&[]interface{}{&expected.Int, &expected.String},
   388  		"",
   389  		"Can unpack into a slice",
   390  	}, {
   391  		&[2]interface{}{&bigint, new(string)},
   392  		&[2]interface{}{&expected.Int, &expected.String},
   393  		"",
   394  		"Can unpack into an array",
   395  	}, {
   396  		&[]interface{}{new(int), new(int)},
   397  		&[]interface{}{&expected.Int, &expected.String},
   398  		"abi: cannot unmarshal *big.Int in to int",
   399  		"Can not unpack into a slice with wrong types",
   400  	}, {
   401  		&[]interface{}{new(int)},
   402  		&[]interface{}{},
   403  		"abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
   404  		"Can not unpack into a slice with wrong types",
   405  	}}
   406  	for _, tc := range testCases {
   407  		tc := tc
   408  		t.Run(tc.name, func(t *testing.T) {
   409  			require := require.New(t)
   410  			err := abi.Unpack(tc.dest, "multi", data)
   411  			if tc.error == "" {
   412  				require.Nil(err, "Should be able to unpack method outputs.")
   413  				require.Equal(tc.expected, tc.dest)
   414  			} else {
   415  				require.EqualError(err, tc.error)
   416  			}
   417  		})
   418  	}
   419  }
   420  
   421  func TestMultiReturnWithArray(t *testing.T) {
   422  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   423  	abi, err := JSON(strings.NewReader(definition))
   424  	if err != nil {
   425  		t.Fatal(err)
   426  	}
   427  	buff := new(bytes.Buffer)
   428  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   429  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   430  
   431  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   432  	ret2, ret2Exp := new(uint64), uint64(8)
   433  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   434  		t.Fatal(err)
   435  	}
   436  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   437  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   438  	}
   439  	if *ret2 != ret2Exp {
   440  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   441  	}
   442  }
   443  
   444  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   445  	// Similar to TestMultiReturnWithArray, but with a special case in mind:
   446  	//  values of nested static arrays count towards the size as well, and any element following
   447  	//  after such nested array argument should be read with the correct offset,
   448  	//  so that it does not read content from the previous array argument.
   449  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   450  	abi, err := JSON(strings.NewReader(definition))
   451  	if err != nil {
   452  		t.Fatal(err)
   453  	}
   454  	buff := new(bytes.Buffer)
   455  	// construct the test array, each 3 char element is joined with 61 '0' chars,
   456  	// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
   457  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   458  		"", //empty, to apply the 61-char separator to the first element as well.
   459  		"111", "112", "113", "121", "122", "123",
   460  		"211", "212", "213", "221", "222", "223",
   461  		"311", "312", "313", "321", "322", "323",
   462  		"411", "412", "413", "421", "422", "423",
   463  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   464  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   465  
   466  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   467  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   468  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   469  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   470  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   471  	}
   472  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   473  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   474  		t.Fatal(err)
   475  	}
   476  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   477  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   478  	}
   479  	if *ret2 != ret2Exp {
   480  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   481  	}
   482  }
   483  
   484  func TestUnmarshal(t *testing.T) {
   485  	const definition = `[
   486  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   487  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   488  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   489  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   490  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   491  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   492  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   493  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   494  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   495  
   496  	abi, err := JSON(strings.NewReader(definition))
   497  	if err != nil {
   498  		t.Fatal(err)
   499  	}
   500  	buff := new(bytes.Buffer)
   501  
   502  	// marshall mixed bytes (mixedBytes)
   503  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   504  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   505  	mixedBytes := []interface{}{&p0, &p1}
   506  
   507  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   508  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   509  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   510  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   511  
   512  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   513  	if err != nil {
   514  		t.Error(err)
   515  	} else {
   516  		if !bytes.Equal(p0, p0Exp) {
   517  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   518  		}
   519  
   520  		if !bytes.Equal(p1[:], p1Exp) {
   521  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   522  		}
   523  	}
   524  
   525  	// marshal int
   526  	var Int *big.Int
   527  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   528  	if err != nil {
   529  		t.Error(err)
   530  	}
   531  
   532  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   533  		t.Error("expected Int to be 1 got", Int)
   534  	}
   535  
   536  	// marshal bool
   537  	var Bool bool
   538  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   539  	if err != nil {
   540  		t.Error(err)
   541  	}
   542  
   543  	if !Bool {
   544  		t.Error("expected Bool to be true")
   545  	}
   546  
   547  	// marshal dynamic bytes max length 32
   548  	buff.Reset()
   549  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   550  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   551  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   552  	buff.Write(bytesOut)
   553  
   554  	var Bytes []byte
   555  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   556  	if err != nil {
   557  		t.Error(err)
   558  	}
   559  
   560  	if !bytes.Equal(Bytes, bytesOut) {
   561  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   562  	}
   563  
   564  	// marshall dynamic bytes max length 64
   565  	buff.Reset()
   566  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   567  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   568  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   569  	buff.Write(bytesOut)
   570  
   571  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   572  	if err != nil {
   573  		t.Error(err)
   574  	}
   575  
   576  	if !bytes.Equal(Bytes, bytesOut) {
   577  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   578  	}
   579  
   580  	// marshall dynamic bytes max length 64
   581  	buff.Reset()
   582  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   583  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   584  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   585  	buff.Write(bytesOut)
   586  
   587  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   588  	if err != nil {
   589  		t.Error(err)
   590  	}
   591  
   592  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   593  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   594  	}
   595  
   596  	// marshal dynamic bytes output empty
   597  	err = abi.Unpack(&Bytes, "bytes", nil)
   598  	if err == nil {
   599  		t.Error("expected error")
   600  	}
   601  
   602  	// marshal dynamic bytes length 5
   603  	buff.Reset()
   604  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   605  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   606  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   607  
   608  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   609  	if err != nil {
   610  		t.Error(err)
   611  	}
   612  
   613  	if !bytes.Equal(Bytes, []byte("hello")) {
   614  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   615  	}
   616  
   617  	// marshal dynamic bytes length 5
   618  	buff.Reset()
   619  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   620  
   621  	var hash common.Hash
   622  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   623  	if err != nil {
   624  		t.Error(err)
   625  	}
   626  
   627  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   628  	if hash != helloHash {
   629  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   630  	}
   631  
   632  	// marshal error
   633  	buff.Reset()
   634  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   635  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   636  	if err == nil {
   637  		t.Error("expected error")
   638  	}
   639  
   640  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   641  	if err == nil {
   642  		t.Error("expected error")
   643  	}
   644  
   645  	buff.Reset()
   646  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   647  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   648  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   649  	// marshal int array
   650  	var intArray [3]*big.Int
   651  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   652  	if err != nil {
   653  		t.Error(err)
   654  	}
   655  	var testAgainstIntArray [3]*big.Int
   656  	testAgainstIntArray[0] = big.NewInt(1)
   657  	testAgainstIntArray[1] = big.NewInt(2)
   658  	testAgainstIntArray[2] = big.NewInt(3)
   659  
   660  	for i, Int := range intArray {
   661  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   662  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   663  		}
   664  	}
   665  	// marshal address slice
   666  	buff.Reset()
   667  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   668  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   669  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   670  
   671  	var outAddr []common.Address
   672  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   673  	if err != nil {
   674  		t.Fatal("didn't expect error:", err)
   675  	}
   676  
   677  	if len(outAddr) != 1 {
   678  		t.Fatal("expected 1 item, got", len(outAddr))
   679  	}
   680  
   681  	if outAddr[0] != (common.Address{1}) {
   682  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   683  	}
   684  
   685  	// marshal multiple address slice
   686  	buff.Reset()
   687  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   688  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   689  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   690  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   691  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   692  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   693  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   694  
   695  	var outAddrStruct struct {
   696  		A []common.Address
   697  		B []common.Address
   698  	}
   699  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   700  	if err != nil {
   701  		t.Fatal("didn't expect error:", err)
   702  	}
   703  
   704  	if len(outAddrStruct.A) != 1 {
   705  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   706  	}
   707  
   708  	if outAddrStruct.A[0] != (common.Address{1}) {
   709  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   710  	}
   711  
   712  	if len(outAddrStruct.B) != 2 {
   713  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   714  	}
   715  
   716  	if outAddrStruct.B[0] != (common.Address{2}) {
   717  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   718  	}
   719  	if outAddrStruct.B[1] != (common.Address{3}) {
   720  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   721  	}
   722  
   723  	// marshal invalid address slice
   724  	buff.Reset()
   725  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   726  
   727  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   728  	if err == nil {
   729  		t.Fatal("expected error:", err)
   730  	}
   731  }
   732  
   733  func TestOOMMaliciousInput(t *testing.T) {
   734  	oomTests := []unpackTest{
   735  		{
   736  			def: `[{"type": "uint8[]"}]`,
   737  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   738  				"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
   739  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   740  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   741  		},
   742  		{ // Length larger than 64 bits
   743  			def: `[{"type": "uint8[]"}]`,
   744  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   745  				"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
   746  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   747  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   748  		},
   749  		{ // Offset very large (over 64 bits)
   750  			def: `[{"type": "uint8[]"}]`,
   751  			enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
   752  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   753  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   754  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   755  		},
   756  		{ // Offset very large (below 64 bits)
   757  			def: `[{"type": "uint8[]"}]`,
   758  			enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
   759  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   760  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   761  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   762  		},
   763  		{ // Offset negative (as 64 bit)
   764  			def: `[{"type": "uint8[]"}]`,
   765  			enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
   766  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   767  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   768  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   769  		},
   770  
   771  		{ // Negative length
   772  			def: `[{"type": "uint8[]"}]`,
   773  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   774  				"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
   775  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   776  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   777  		},
   778  		{ // Very large length
   779  			def: `[{"type": "uint8[]"}]`,
   780  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   781  				"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
   782  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   783  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   784  		},
   785  	}
   786  	for i, test := range oomTests {
   787  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   788  		abi, err := JSON(strings.NewReader(def))
   789  		if err != nil {
   790  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   791  		}
   792  		encb, err := hex.DecodeString(test.enc)
   793  		if err != nil {
   794  			t.Fatalf("invalid hex: %s" + test.enc)
   795  		}
   796  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   797  		if err == nil {
   798  			t.Fatalf("Expected error on malicious input, test %d", i)
   799  		}
   800  	}
   801  }