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