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