gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/aqua/accounts/abi/unpack_test.go (about)

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