github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/unpack_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math"
    24  	"math/big"
    25  	"reflect"
    26  	"strconv"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  // TestUnpack tests the general pack/unpack tests in packing_test.go
    35  func TestUnpack(t *testing.T) {
    36  	t.Parallel()
    37  	for i, test := range packUnpackTests {
    38  		t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) {
    39  			//Unpack
    40  			def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
    41  			abi, err := JSON(strings.NewReader(def))
    42  			if err != nil {
    43  				t.Fatalf("invalid ABI definition %s: %v", def, err)
    44  			}
    45  			encb, err := hex.DecodeString(test.packed)
    46  			if err != nil {
    47  				t.Fatalf("invalid hex %s: %v", test.packed, err)
    48  			}
    49  			out, err := abi.Unpack("method", encb)
    50  			if err != nil {
    51  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
    52  				return
    53  			}
    54  			if !reflect.DeepEqual(test.unpacked, ConvertType(out[0], test.unpacked)) {
    55  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.unpacked, out[0])
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  type unpackTest struct {
    62  	def  string      // ABI definition JSON
    63  	enc  string      // evm return data
    64  	want interface{} // the expected output
    65  	err  string      // empty or error if expected
    66  }
    67  
    68  func (test unpackTest) checkError(err error) error {
    69  	if err != nil {
    70  		if len(test.err) == 0 {
    71  			return fmt.Errorf("expected no err but got: %v", err)
    72  		} else if err.Error() != test.err {
    73  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    74  		}
    75  	} else if len(test.err) > 0 {
    76  		return fmt.Errorf("expected err: %v but got none", test.err)
    77  	}
    78  	return nil
    79  }
    80  
    81  var unpackTests = []unpackTest{
    82  	// Bools
    83  	{
    84  		def:  `[{ "type": "bool" }]`,
    85  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    86  		want: false,
    87  		err:  "abi: improperly encoded boolean value",
    88  	},
    89  	{
    90  		def:  `[{ "type": "bool" }]`,
    91  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    92  		want: false,
    93  		err:  "abi: improperly encoded boolean value",
    94  	},
    95  	// Integers
    96  	{
    97  		def:  `[{"type": "uint32"}]`,
    98  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    99  		want: uint16(0),
   100  		err:  "abi: cannot unmarshal uint32 in to uint16",
   101  	},
   102  	{
   103  		def:  `[{"type": "uint17"}]`,
   104  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   105  		want: uint16(0),
   106  		err:  "abi: cannot unmarshal *big.Int in to uint16",
   107  	},
   108  	{
   109  		def:  `[{"type": "int32"}]`,
   110  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   111  		want: int16(0),
   112  		err:  "abi: cannot unmarshal int32 in to int16",
   113  	},
   114  	{
   115  		def:  `[{"type": "int17"}]`,
   116  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   117  		want: int16(0),
   118  		err:  "abi: cannot unmarshal *big.Int in to int16",
   119  	},
   120  	{
   121  		def:  `[{"type": "bytes"}]`,
   122  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   123  		want: [32]byte{1},
   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: `[{"name":"___","type":"int256"}]`,
   133  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   134  		want: struct {
   135  			IntOne *big.Int
   136  			Intone *big.Int
   137  		}{IntOne: big.NewInt(1)},
   138  	},
   139  	{
   140  		def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
   141  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   142  		want: struct {
   143  			Int1 *big.Int
   144  			Int2 *big.Int
   145  		}{},
   146  		err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
   147  	},
   148  	{
   149  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   150  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   151  		want: struct {
   152  			Int1 *big.Int
   153  			Int2 *big.Int
   154  		}{},
   155  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   156  	},
   157  	{
   158  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   159  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   160  		want: struct {
   161  			Int1 *big.Int
   162  			Int2 *big.Int
   163  		}{},
   164  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   165  	},
   166  	{
   167  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   168  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   169  		want: struct {
   170  			Int1 *big.Int
   171  			Int2 *big.Int
   172  		}{},
   173  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   174  	},
   175  	{
   176  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   177  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   178  		want: struct {
   179  			Int1 *big.Int
   180  			Int2 *big.Int
   181  		}{},
   182  		err: "abi: purely underscored output cannot unpack to struct",
   183  	},
   184  	// Make sure only the first argument is consumed
   185  	{
   186  		def: `[{"name":"int_one","type":"int256"}]`,
   187  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   188  		want: struct {
   189  			IntOne *big.Int
   190  		}{big.NewInt(1)},
   191  	},
   192  	{
   193  		def: `[{"name":"int__one","type":"int256"}]`,
   194  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   195  		want: struct {
   196  			IntOne *big.Int
   197  		}{big.NewInt(1)},
   198  	},
   199  	{
   200  		def: `[{"name":"int_one_","type":"int256"}]`,
   201  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   202  		want: struct {
   203  			IntOne *big.Int
   204  		}{big.NewInt(1)},
   205  	},
   206  	{
   207  		def:  `[{"type":"bool"}]`,
   208  		enc:  "",
   209  		want: false,
   210  		err:  "abi: attempting to unmarshal an empty string while arguments are expected",
   211  	},
   212  	{
   213  		def:  `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`,
   214  		enc:  "",
   215  		want: false,
   216  		err:  "abi: attempting to unmarshal an empty string while arguments are expected",
   217  	},
   218  	{
   219  		def:  `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`,
   220  		enc:  "",
   221  		want: false,
   222  	},
   223  }
   224  
   225  // TestLocalUnpackTests runs test specially designed only for unpacking.
   226  // All test cases that can be used to test packing and unpacking should move to packing_test.go
   227  func TestLocalUnpackTests(t *testing.T) {
   228  	t.Parallel()
   229  	for i, test := range unpackTests {
   230  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   231  			//Unpack
   232  			def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
   233  			abi, err := JSON(strings.NewReader(def))
   234  			if err != nil {
   235  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   236  			}
   237  			encb, err := hex.DecodeString(test.enc)
   238  			if err != nil {
   239  				t.Fatalf("invalid hex %s: %v", test.enc, err)
   240  			}
   241  			outptr := reflect.New(reflect.TypeOf(test.want))
   242  			err = abi.UnpackIntoInterface(outptr.Interface(), "method", encb)
   243  			if err := test.checkError(err); err != nil {
   244  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   245  				return
   246  			}
   247  			out := outptr.Elem().Interface()
   248  			if !reflect.DeepEqual(test.want, out) {
   249  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   250  			}
   251  		})
   252  	}
   253  }
   254  
   255  func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
   256  	t.Parallel()
   257  	abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
   258  	if err != nil {
   259  		t.Fatal(err)
   260  	}
   261  
   262  	var (
   263  		marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
   264  		marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
   265  
   266  		out32 [][32]byte
   267  		out15 [][15]byte
   268  	)
   269  
   270  	// test 32
   271  	err = abi.UnpackIntoInterface(&out32, "testDynamicFixedBytes32", marshalledReturn32)
   272  	if err != nil {
   273  		t.Fatal(err)
   274  	}
   275  	if len(out32) != 2 {
   276  		t.Fatalf("expected array with 2 values, got %d", len(out32))
   277  	}
   278  	expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
   279  	if !bytes.Equal(out32[0][:], expected) {
   280  		t.Errorf("expected %x, got %x\n", expected, out32[0])
   281  	}
   282  	expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
   283  	if !bytes.Equal(out32[1][:], expected) {
   284  		t.Errorf("expected %x, got %x\n", expected, out32[1])
   285  	}
   286  
   287  	// test 15
   288  	err = abi.UnpackIntoInterface(&out15, "testDynamicFixedBytes32", marshalledReturn15)
   289  	if err != nil {
   290  		t.Fatal(err)
   291  	}
   292  	if len(out15) != 2 {
   293  		t.Fatalf("expected array with 2 values, got %d", len(out15))
   294  	}
   295  	expected = common.Hex2Bytes("307830313233343500000000000000")
   296  	if !bytes.Equal(out15[0][:], expected) {
   297  		t.Errorf("expected %x, got %x\n", expected, out15[0])
   298  	}
   299  	expected = common.Hex2Bytes("307839383736353400000000000000")
   300  	if !bytes.Equal(out15[1][:], expected) {
   301  		t.Errorf("expected %x, got %x\n", expected, out15[1])
   302  	}
   303  }
   304  
   305  type methodMultiOutput struct {
   306  	Int    *big.Int
   307  	String string
   308  }
   309  
   310  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   311  	const definition = `[
   312  	{ "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   313  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   314  
   315  	abi, err := JSON(strings.NewReader(definition))
   316  	require.NoError(err)
   317  	// using buff to make the code readable
   318  	buff := new(bytes.Buffer)
   319  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   320  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   321  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   322  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   323  	return abi, buff.Bytes(), expected
   324  }
   325  
   326  func TestMethodMultiReturn(t *testing.T) {
   327  	t.Parallel()
   328  	type reversed struct {
   329  		String string
   330  		Int    *big.Int
   331  	}
   332  
   333  	newInterfaceSlice := func(len int) interface{} {
   334  		slice := make([]interface{}, len)
   335  		return &slice
   336  	}
   337  
   338  	abi, data, expected := methodMultiReturn(require.New(t))
   339  	bigint := new(big.Int)
   340  	var testCases = []struct {
   341  		dest     interface{}
   342  		expected interface{}
   343  		error    string
   344  		name     string
   345  	}{{
   346  		&methodMultiOutput{},
   347  		&expected,
   348  		"",
   349  		"Can unpack into structure",
   350  	}, {
   351  		&reversed{},
   352  		&reversed{expected.String, expected.Int},
   353  		"",
   354  		"Can unpack into reversed structure",
   355  	}, {
   356  		&[]interface{}{&bigint, new(string)},
   357  		&[]interface{}{&expected.Int, &expected.String},
   358  		"",
   359  		"Can unpack into a slice",
   360  	}, {
   361  		&[]interface{}{&bigint, ""},
   362  		&[]interface{}{&expected.Int, expected.String},
   363  		"",
   364  		"Can unpack into a slice without indirection",
   365  	}, {
   366  		&[2]interface{}{&bigint, new(string)},
   367  		&[2]interface{}{&expected.Int, &expected.String},
   368  		"",
   369  		"Can unpack into an array",
   370  	}, {
   371  		&[2]interface{}{},
   372  		&[2]interface{}{expected.Int, expected.String},
   373  		"",
   374  		"Can unpack into interface array",
   375  	}, {
   376  		newInterfaceSlice(2),
   377  		&[]interface{}{expected.Int, expected.String},
   378  		"",
   379  		"Can unpack into interface slice",
   380  	}, {
   381  		&[]interface{}{new(int), new(int)},
   382  		&[]interface{}{&expected.Int, &expected.String},
   383  		"abi: cannot unmarshal *big.Int in to int",
   384  		"Can not unpack into a slice with wrong types",
   385  	}, {
   386  		&[]interface{}{new(int)},
   387  		&[]interface{}{},
   388  		"abi: insufficient number of arguments for unpack, want 2, got 1",
   389  		"Can not unpack into a slice with wrong types",
   390  	}}
   391  	for _, tc := range testCases {
   392  		tc := tc
   393  		t.Run(tc.name, func(t *testing.T) {
   394  			require := require.New(t)
   395  			err := abi.UnpackIntoInterface(tc.dest, "multi", data)
   396  			if tc.error == "" {
   397  				require.Nil(err, "Should be able to unpack method outputs.")
   398  				require.Equal(tc.expected, tc.dest)
   399  			} else {
   400  				require.EqualError(err, tc.error)
   401  			}
   402  		})
   403  	}
   404  }
   405  
   406  func TestMultiReturnWithArray(t *testing.T) {
   407  	t.Parallel()
   408  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   409  	abi, err := JSON(strings.NewReader(definition))
   410  	if err != nil {
   411  		t.Fatal(err)
   412  	}
   413  	buff := new(bytes.Buffer)
   414  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   415  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   416  
   417  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   418  	ret2, ret2Exp := new(uint64), uint64(8)
   419  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   420  		t.Fatal(err)
   421  	}
   422  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   423  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   424  	}
   425  	if *ret2 != ret2Exp {
   426  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   427  	}
   428  }
   429  
   430  func TestMultiReturnWithStringArray(t *testing.T) {
   431  	t.Parallel()
   432  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
   433  	abi, err := JSON(strings.NewReader(definition))
   434  	if err != nil {
   435  		t.Fatal(err)
   436  	}
   437  	buff := new(bytes.Buffer)
   438  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
   439  	temp, _ := new(big.Int).SetString("30000000000000000000", 10)
   440  	ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
   441  	ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
   442  	ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
   443  	ret4, ret4Exp := new(bool), false
   444  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
   445  		t.Fatal(err)
   446  	}
   447  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   448  		t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
   449  	}
   450  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   451  		t.Error("address result", *ret2, "!= Expected", ret2Exp)
   452  	}
   453  	if !reflect.DeepEqual(*ret3, ret3Exp) {
   454  		t.Error("string array result", *ret3, "!= Expected", ret3Exp)
   455  	}
   456  	if !reflect.DeepEqual(*ret4, ret4Exp) {
   457  		t.Error("bool result", *ret4, "!= Expected", ret4Exp)
   458  	}
   459  }
   460  
   461  func TestMultiReturnWithStringSlice(t *testing.T) {
   462  	t.Parallel()
   463  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
   464  	abi, err := JSON(strings.NewReader(definition))
   465  	if err != nil {
   466  		t.Fatal(err)
   467  	}
   468  	buff := new(bytes.Buffer)
   469  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
   470  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
   471  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
   472  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
   473  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
   474  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
   475  	buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
   476  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
   477  	buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
   478  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
   479  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
   480  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
   481  	ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
   482  	ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
   483  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   484  		t.Fatal(err)
   485  	}
   486  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   487  		t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
   488  	}
   489  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   490  		t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
   491  	}
   492  }
   493  
   494  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   495  	t.Parallel()
   496  	// Similar to TestMultiReturnWithArray, but with a special case in mind:
   497  	//  values of nested static arrays count towards the size as well, and any element following
   498  	//  after such nested array argument should be read with the correct offset,
   499  	//  so that it does not read content from the previous array argument.
   500  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   501  	abi, err := JSON(strings.NewReader(definition))
   502  	if err != nil {
   503  		t.Fatal(err)
   504  	}
   505  	buff := new(bytes.Buffer)
   506  	// construct the test array, each 3 char element is joined with 61 '0' chars,
   507  	// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
   508  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   509  		"", //empty, to apply the 61-char separator to the first element as well.
   510  		"111", "112", "113", "121", "122", "123",
   511  		"211", "212", "213", "221", "222", "223",
   512  		"311", "312", "313", "321", "322", "323",
   513  		"411", "412", "413", "421", "422", "423",
   514  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   515  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   516  
   517  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   518  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   519  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   520  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   521  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   522  	}
   523  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   524  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   525  		t.Fatal(err)
   526  	}
   527  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   528  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   529  	}
   530  	if *ret2 != ret2Exp {
   531  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   532  	}
   533  }
   534  
   535  func TestUnmarshal(t *testing.T) {
   536  	t.Parallel()
   537  	const definition = `[
   538  	{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] },
   539  	{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] },
   540  	{ "name" : "bytes", "type": "function", "outputs": [ { "type": "bytes" } ] },
   541  	{ "name" : "fixed", "type": "function", "outputs": [ { "type": "bytes32" } ] },
   542  	{ "name" : "multi", "type": "function", "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   543  	{ "name" : "intArraySingle", "type": "function", "outputs": [ { "type": "uint256[3]" } ] },
   544  	{ "name" : "addressSliceSingle", "type": "function", "outputs": [ { "type": "address[]" } ] },
   545  	{ "name" : "addressSliceDouble", "type": "function", "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   546  	{ "name" : "mixedBytes", "type": "function", "stateMutability" : "view", "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   547  
   548  	abi, err := JSON(strings.NewReader(definition))
   549  	if err != nil {
   550  		t.Fatal(err)
   551  	}
   552  	buff := new(bytes.Buffer)
   553  
   554  	// marshall mixed bytes (mixedBytes)
   555  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   556  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   557  	mixedBytes := []interface{}{&p0, &p1}
   558  
   559  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   560  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   561  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   562  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   563  
   564  	err = abi.UnpackIntoInterface(&mixedBytes, "mixedBytes", buff.Bytes())
   565  	if err != nil {
   566  		t.Error(err)
   567  	} else {
   568  		if !bytes.Equal(p0, p0Exp) {
   569  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   570  		}
   571  
   572  		if !bytes.Equal(p1[:], p1Exp) {
   573  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   574  		}
   575  	}
   576  
   577  	// marshal int
   578  	var Int *big.Int
   579  	err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   580  	if err != nil {
   581  		t.Error(err)
   582  	}
   583  
   584  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   585  		t.Error("expected Int to be 1 got", Int)
   586  	}
   587  
   588  	// marshal bool
   589  	var Bool bool
   590  	err = abi.UnpackIntoInterface(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   591  	if err != nil {
   592  		t.Error(err)
   593  	}
   594  
   595  	if !Bool {
   596  		t.Error("expected Bool to be true")
   597  	}
   598  
   599  	// marshal dynamic bytes max length 32
   600  	buff.Reset()
   601  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   602  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   603  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   604  	buff.Write(bytesOut)
   605  
   606  	var Bytes []byte
   607  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   608  	if err != nil {
   609  		t.Error(err)
   610  	}
   611  
   612  	if !bytes.Equal(Bytes, bytesOut) {
   613  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   614  	}
   615  
   616  	// marshall dynamic bytes max length 64
   617  	buff.Reset()
   618  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   619  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   620  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   621  	buff.Write(bytesOut)
   622  
   623  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   624  	if err != nil {
   625  		t.Error(err)
   626  	}
   627  
   628  	if !bytes.Equal(Bytes, bytesOut) {
   629  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   630  	}
   631  
   632  	// marshall dynamic bytes max length 64
   633  	buff.Reset()
   634  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   635  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   636  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   637  	buff.Write(bytesOut)
   638  
   639  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   640  	if err != nil {
   641  		t.Error(err)
   642  	}
   643  
   644  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   645  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   646  	}
   647  
   648  	// marshal dynamic bytes output empty
   649  	err = abi.UnpackIntoInterface(&Bytes, "bytes", nil)
   650  	if err == nil {
   651  		t.Error("expected error")
   652  	}
   653  
   654  	// marshal dynamic bytes length 5
   655  	buff.Reset()
   656  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   657  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   658  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   659  
   660  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   661  	if err != nil {
   662  		t.Error(err)
   663  	}
   664  
   665  	if !bytes.Equal(Bytes, []byte("hello")) {
   666  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   667  	}
   668  
   669  	// marshal dynamic bytes length 5
   670  	buff.Reset()
   671  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   672  
   673  	var hash common.Hash
   674  	err = abi.UnpackIntoInterface(&hash, "fixed", buff.Bytes())
   675  	if err != nil {
   676  		t.Error(err)
   677  	}
   678  
   679  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   680  	if hash != helloHash {
   681  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   682  	}
   683  
   684  	// marshal error
   685  	buff.Reset()
   686  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   687  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   688  	if err == nil {
   689  		t.Error("expected error")
   690  	}
   691  
   692  	err = abi.UnpackIntoInterface(&Bytes, "multi", make([]byte, 64))
   693  	if err == nil {
   694  		t.Error("expected error")
   695  	}
   696  
   697  	buff.Reset()
   698  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   699  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   700  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   701  	// marshal int array
   702  	var intArray [3]*big.Int
   703  	err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes())
   704  	if err != nil {
   705  		t.Error(err)
   706  	}
   707  	var testAgainstIntArray [3]*big.Int
   708  	testAgainstIntArray[0] = big.NewInt(1)
   709  	testAgainstIntArray[1] = big.NewInt(2)
   710  	testAgainstIntArray[2] = big.NewInt(3)
   711  
   712  	for i, Int := range intArray {
   713  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   714  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   715  		}
   716  	}
   717  	// marshal address slice
   718  	buff.Reset()
   719  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   720  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   721  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   722  
   723  	var outAddr []common.Address
   724  	err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
   725  	if err != nil {
   726  		t.Fatal("didn't expect error:", err)
   727  	}
   728  
   729  	if len(outAddr) != 1 {
   730  		t.Fatal("expected 1 item, got", len(outAddr))
   731  	}
   732  
   733  	if outAddr[0] != (common.Address{1}) {
   734  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   735  	}
   736  
   737  	// marshal multiple address slice
   738  	buff.Reset()
   739  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   740  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   741  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   742  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   743  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   744  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   745  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   746  
   747  	var outAddrStruct struct {
   748  		A []common.Address
   749  		B []common.Address
   750  	}
   751  	err = abi.UnpackIntoInterface(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   752  	if err != nil {
   753  		t.Fatal("didn't expect error:", err)
   754  	}
   755  
   756  	if len(outAddrStruct.A) != 1 {
   757  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   758  	}
   759  
   760  	if outAddrStruct.A[0] != (common.Address{1}) {
   761  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   762  	}
   763  
   764  	if len(outAddrStruct.B) != 2 {
   765  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   766  	}
   767  
   768  	if outAddrStruct.B[0] != (common.Address{2}) {
   769  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   770  	}
   771  	if outAddrStruct.B[1] != (common.Address{3}) {
   772  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   773  	}
   774  
   775  	// marshal invalid address slice
   776  	buff.Reset()
   777  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   778  
   779  	err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
   780  	if err == nil {
   781  		t.Fatal("expected error:", err)
   782  	}
   783  }
   784  
   785  func TestUnpackTuple(t *testing.T) {
   786  	t.Parallel()
   787  	const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
   788  	abi, err := JSON(strings.NewReader(simpleTuple))
   789  	if err != nil {
   790  		t.Fatal(err)
   791  	}
   792  	buff := new(bytes.Buffer)
   793  
   794  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
   795  	buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
   796  
   797  	// If the result is single tuple, use struct as return value container directly.
   798  	type v struct {
   799  		A *big.Int
   800  		B *big.Int
   801  	}
   802  	type r struct {
   803  		Result v
   804  	}
   805  	var ret0 = new(r)
   806  	err = abi.UnpackIntoInterface(ret0, "tuple", buff.Bytes())
   807  
   808  	if err != nil {
   809  		t.Error(err)
   810  	} else {
   811  		if ret0.Result.A.Cmp(big.NewInt(1)) != 0 {
   812  			t.Errorf("unexpected value unpacked: want %x, got %x", 1, ret0.Result.A)
   813  		}
   814  		if ret0.Result.B.Cmp(big.NewInt(-1)) != 0 {
   815  			t.Errorf("unexpected value unpacked: want %x, got %x", -1, ret0.Result.B)
   816  		}
   817  	}
   818  
   819  	// Test nested tuple
   820  	const nestedTuple = `[{"name":"tuple","type":"function","outputs":[
   821  		{"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]},
   822  		{"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]},
   823  		{"type":"uint256","name":"a"}
   824  	]}]`
   825  
   826  	abi, err = JSON(strings.NewReader(nestedTuple))
   827  	if err != nil {
   828  		t.Fatal(err)
   829  	}
   830  	buff.Reset()
   831  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
   832  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
   833  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
   834  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
   835  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
   836  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
   837  	buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
   838  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
   839  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
   840  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
   841  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
   842  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
   843  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
   844  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
   845  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
   846  
   847  	type T struct {
   848  		X *big.Int `abi:"x"`
   849  		Z *big.Int `abi:"y"` // Test whether the abi tag works.
   850  	}
   851  
   852  	type S struct {
   853  		A *big.Int
   854  		B []*big.Int
   855  		C []T
   856  	}
   857  
   858  	type Ret struct {
   859  		FieldS S `abi:"s"`
   860  		FieldT T `abi:"t"`
   861  		A      *big.Int
   862  	}
   863  	var ret Ret
   864  	var expected = Ret{
   865  		FieldS: S{
   866  			A: big.NewInt(1),
   867  			B: []*big.Int{big.NewInt(1), big.NewInt(2)},
   868  			C: []T{
   869  				{big.NewInt(1), big.NewInt(2)},
   870  				{big.NewInt(2), big.NewInt(1)},
   871  			},
   872  		},
   873  		FieldT: T{
   874  			big.NewInt(0), big.NewInt(1),
   875  		},
   876  		A: big.NewInt(1),
   877  	}
   878  
   879  	err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes())
   880  	if err != nil {
   881  		t.Error(err)
   882  	}
   883  	if reflect.DeepEqual(ret, expected) {
   884  		t.Error("unexpected unpack value")
   885  	}
   886  }
   887  
   888  func TestOOMMaliciousInput(t *testing.T) {
   889  	t.Parallel()
   890  	oomTests := []unpackTest{
   891  		{
   892  			def: `[{"type": "uint8[]"}]`,
   893  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   894  				"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
   895  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   896  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   897  		},
   898  		{ // Length larger than 64 bits
   899  			def: `[{"type": "uint8[]"}]`,
   900  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   901  				"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
   902  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   903  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   904  		},
   905  		{ // Offset very large (over 64 bits)
   906  			def: `[{"type": "uint8[]"}]`,
   907  			enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
   908  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   909  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   910  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   911  		},
   912  		{ // Offset very large (below 64 bits)
   913  			def: `[{"type": "uint8[]"}]`,
   914  			enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
   915  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   916  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   917  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   918  		},
   919  		{ // Offset negative (as 64 bit)
   920  			def: `[{"type": "uint8[]"}]`,
   921  			enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
   922  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   923  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   924  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   925  		},
   926  
   927  		{ // Negative length
   928  			def: `[{"type": "uint8[]"}]`,
   929  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   930  				"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
   931  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   932  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   933  		},
   934  		{ // Very large length
   935  			def: `[{"type": "uint8[]"}]`,
   936  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   937  				"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
   938  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   939  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   940  		},
   941  	}
   942  	for i, test := range oomTests {
   943  		def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
   944  		abi, err := JSON(strings.NewReader(def))
   945  		if err != nil {
   946  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   947  		}
   948  		encb, err := hex.DecodeString(test.enc)
   949  		if err != nil {
   950  			t.Fatalf("invalid hex: %s" + test.enc)
   951  		}
   952  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   953  		if err == nil {
   954  			t.Fatalf("Expected error on malicious input, test %d", i)
   955  		}
   956  	}
   957  }
   958  
   959  func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
   960  	t.Parallel()
   961  	var encodeABI Arguments
   962  	uint256Ty, err := NewType("uint256", "", nil)
   963  	if err != nil {
   964  		panic(err)
   965  	}
   966  	encodeABI = Arguments{
   967  		{Type: uint256Ty},
   968  	}
   969  
   970  	maxU64, ok := new(big.Int).SetString(strconv.FormatUint(math.MaxUint64, 10), 10)
   971  	if !ok {
   972  		panic("bug")
   973  	}
   974  	maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1))
   975  	cases := []struct {
   976  		decodeType  string
   977  		inputValue  *big.Int
   978  		err         error
   979  		expectValue interface{}
   980  	}{
   981  		{
   982  			decodeType: "uint8",
   983  			inputValue: big.NewInt(math.MaxUint8 + 1),
   984  			err:        errBadUint8,
   985  		},
   986  		{
   987  			decodeType:  "uint8",
   988  			inputValue:  big.NewInt(math.MaxUint8),
   989  			err:         nil,
   990  			expectValue: uint8(math.MaxUint8),
   991  		},
   992  		{
   993  			decodeType: "uint16",
   994  			inputValue: big.NewInt(math.MaxUint16 + 1),
   995  			err:        errBadUint16,
   996  		},
   997  		{
   998  			decodeType:  "uint16",
   999  			inputValue:  big.NewInt(math.MaxUint16),
  1000  			err:         nil,
  1001  			expectValue: uint16(math.MaxUint16),
  1002  		},
  1003  		{
  1004  			decodeType: "uint32",
  1005  			inputValue: big.NewInt(math.MaxUint32 + 1),
  1006  			err:        errBadUint32,
  1007  		},
  1008  		{
  1009  			decodeType:  "uint32",
  1010  			inputValue:  big.NewInt(math.MaxUint32),
  1011  			err:         nil,
  1012  			expectValue: uint32(math.MaxUint32),
  1013  		},
  1014  		{
  1015  			decodeType: "uint64",
  1016  			inputValue: maxU64Plus1,
  1017  			err:        errBadUint64,
  1018  		},
  1019  		{
  1020  			decodeType:  "uint64",
  1021  			inputValue:  maxU64,
  1022  			err:         nil,
  1023  			expectValue: uint64(math.MaxUint64),
  1024  		},
  1025  		{
  1026  			decodeType:  "uint256",
  1027  			inputValue:  maxU64Plus1,
  1028  			err:         nil,
  1029  			expectValue: maxU64Plus1,
  1030  		},
  1031  		{
  1032  			decodeType: "int8",
  1033  			inputValue: big.NewInt(math.MaxInt8 + 1),
  1034  			err:        errBadInt8,
  1035  		},
  1036  		{
  1037  			decodeType: "int8",
  1038  			inputValue: big.NewInt(math.MinInt8 - 1),
  1039  			err:        errBadInt8,
  1040  		},
  1041  		{
  1042  			decodeType:  "int8",
  1043  			inputValue:  big.NewInt(math.MaxInt8),
  1044  			err:         nil,
  1045  			expectValue: int8(math.MaxInt8),
  1046  		},
  1047  		{
  1048  			decodeType: "int16",
  1049  			inputValue: big.NewInt(math.MaxInt16 + 1),
  1050  			err:        errBadInt16,
  1051  		},
  1052  		{
  1053  			decodeType: "int16",
  1054  			inputValue: big.NewInt(math.MinInt16 - 1),
  1055  			err:        errBadInt16,
  1056  		},
  1057  		{
  1058  			decodeType:  "int16",
  1059  			inputValue:  big.NewInt(math.MaxInt16),
  1060  			err:         nil,
  1061  			expectValue: int16(math.MaxInt16),
  1062  		},
  1063  		{
  1064  			decodeType: "int32",
  1065  			inputValue: big.NewInt(math.MaxInt32 + 1),
  1066  			err:        errBadInt32,
  1067  		},
  1068  		{
  1069  			decodeType: "int32",
  1070  			inputValue: big.NewInt(math.MinInt32 - 1),
  1071  			err:        errBadInt32,
  1072  		},
  1073  		{
  1074  			decodeType:  "int32",
  1075  			inputValue:  big.NewInt(math.MaxInt32),
  1076  			err:         nil,
  1077  			expectValue: int32(math.MaxInt32),
  1078  		},
  1079  		{
  1080  			decodeType: "int64",
  1081  			inputValue: new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)),
  1082  			err:        errBadInt64,
  1083  		},
  1084  		{
  1085  			decodeType: "int64",
  1086  			inputValue: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)),
  1087  			err:        errBadInt64,
  1088  		},
  1089  		{
  1090  			decodeType:  "int64",
  1091  			inputValue:  big.NewInt(math.MaxInt64),
  1092  			err:         nil,
  1093  			expectValue: int64(math.MaxInt64),
  1094  		},
  1095  	}
  1096  	for i, testCase := range cases {
  1097  		packed, err := encodeABI.Pack(testCase.inputValue)
  1098  		if err != nil {
  1099  			panic(err)
  1100  		}
  1101  		ty, err := NewType(testCase.decodeType, "", nil)
  1102  		if err != nil {
  1103  			panic(err)
  1104  		}
  1105  		decodeABI := Arguments{
  1106  			{Type: ty},
  1107  		}
  1108  		decoded, err := decodeABI.Unpack(packed)
  1109  		if err != testCase.err {
  1110  			t.Fatalf("Expected error %v, actual error %v. case %d", testCase.err, err, i)
  1111  		}
  1112  		if err != nil {
  1113  			continue
  1114  		}
  1115  		if !reflect.DeepEqual(decoded[0], testCase.expectValue) {
  1116  			t.Fatalf("Expected value %v, actual value %v", testCase.expectValue, decoded[0])
  1117  		}
  1118  	}
  1119  }