github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/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": "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": "address"}]`,
   105  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   106  		want: common.Address{1},
   107  	},
   108  	{
   109  		def:  `[{"type": "bytes32"}]`,
   110  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   111  		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},
   112  	},
   113  	{
   114  		def:  `[{"type": "bytes"}]`,
   115  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   116  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   117  	},
   118  	{
   119  		def:  `[{"type": "bytes"}]`,
   120  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   121  		want: [32]byte{},
   122  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   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:  `[{"type": "bytes32"}]`,
   132  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   133  		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},
   134  	},
   135  	{
   136  		def:  `[{"type": "function"}]`,
   137  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   138  		want: [24]byte{1},
   139  	},
   140  	// slices
   141  	{
   142  		def:  `[{"type": "uint8[]"}]`,
   143  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   144  		want: []uint8{1, 2},
   145  	},
   146  	{
   147  		def:  `[{"type": "uint8[2]"}]`,
   148  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   149  		want: [2]uint8{1, 2},
   150  	},
   151  	// multi dimensional, if these pass, all types that don't require length prefix should pass
   152  	{
   153  		def:  `[{"type": "uint8[][]"}]`,
   154  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   155  		want: [][]uint8{{1, 2}, {1, 2}},
   156  	},
   157  	{
   158  		def:  `[{"type": "uint8[2][2]"}]`,
   159  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   160  		want: [2][2]uint8{{1, 2}, {1, 2}},
   161  	},
   162  	{
   163  		def:  `[{"type": "uint8[][2]"}]`,
   164  		enc:  "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   165  		want: [2][]uint8{{1}, {1}},
   166  	},
   167  	{
   168  		def:  `[{"type": "uint8[2][]"}]`,
   169  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   170  		want: [][2]uint8{{1, 2}},
   171  	},
   172  	{
   173  		def:  `[{"type": "uint16[]"}]`,
   174  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   175  		want: []uint16{1, 2},
   176  	},
   177  	{
   178  		def:  `[{"type": "uint16[2]"}]`,
   179  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   180  		want: [2]uint16{1, 2},
   181  	},
   182  	{
   183  		def:  `[{"type": "uint32[]"}]`,
   184  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   185  		want: []uint32{1, 2},
   186  	},
   187  	{
   188  		def:  `[{"type": "uint32[2]"}]`,
   189  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   190  		want: [2]uint32{1, 2},
   191  	},
   192  	{
   193  		def:  `[{"type": "uint32[2][3][4]"}]`,
   194  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
   195  		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}}},
   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 TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   444  	// Similar to TestMultiReturnWithArray, but with a special case in mind:
   445  	//  values of nested static arrays count towards the size as well, and any element following
   446  	//  after such nested array argument should be read with the correct offset,
   447  	//  so that it does not read content from the previous array argument.
   448  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   449  	abi, err := JSON(strings.NewReader(definition))
   450  	if err != nil {
   451  		t.Fatal(err)
   452  	}
   453  	buff := new(bytes.Buffer)
   454  	// construct the test array, each 3 char element is joined with 61 '0' chars,
   455  	// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
   456  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   457  		"", //empty, to apply the 61-char separator to the first element as well.
   458  		"111", "112", "113", "121", "122", "123",
   459  		"211", "212", "213", "221", "222", "223",
   460  		"311", "312", "313", "321", "322", "323",
   461  		"411", "412", "413", "421", "422", "423",
   462  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   463  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   464  
   465  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   466  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   467  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   468  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   469  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   470  	}
   471  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   472  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   473  		t.Fatal(err)
   474  	}
   475  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   476  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   477  	}
   478  	if *ret2 != ret2Exp {
   479  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   480  	}
   481  }
   482  
   483  func TestUnmarshal(t *testing.T) {
   484  	const definition = `[
   485  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   486  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   487  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   488  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   489  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   490  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   491  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   492  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   493  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   494  
   495  	abi, err := JSON(strings.NewReader(definition))
   496  	if err != nil {
   497  		t.Fatal(err)
   498  	}
   499  	buff := new(bytes.Buffer)
   500  
   501  	// marshall mixed bytes (mixedBytes)
   502  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   503  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   504  	mixedBytes := []interface{}{&p0, &p1}
   505  
   506  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   507  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   508  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   509  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   510  
   511  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   512  	if err != nil {
   513  		t.Error(err)
   514  	} else {
   515  		if !bytes.Equal(p0, p0Exp) {
   516  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   517  		}
   518  
   519  		if !bytes.Equal(p1[:], p1Exp) {
   520  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   521  		}
   522  	}
   523  
   524  	// marshal int
   525  	var Int *big.Int
   526  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   527  	if err != nil {
   528  		t.Error(err)
   529  	}
   530  
   531  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   532  		t.Error("expected Int to be 1 got", Int)
   533  	}
   534  
   535  	// marshal bool
   536  	var Bool bool
   537  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   538  	if err != nil {
   539  		t.Error(err)
   540  	}
   541  
   542  	if !Bool {
   543  		t.Error("expected Bool to be true")
   544  	}
   545  
   546  	// marshal dynamic bytes max length 32
   547  	buff.Reset()
   548  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   549  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   550  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   551  	buff.Write(bytesOut)
   552  
   553  	var Bytes []byte
   554  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   555  	if err != nil {
   556  		t.Error(err)
   557  	}
   558  
   559  	if !bytes.Equal(Bytes, bytesOut) {
   560  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   561  	}
   562  
   563  	// marshall dynamic bytes max length 64
   564  	buff.Reset()
   565  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   566  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   567  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   568  	buff.Write(bytesOut)
   569  
   570  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   571  	if err != nil {
   572  		t.Error(err)
   573  	}
   574  
   575  	if !bytes.Equal(Bytes, bytesOut) {
   576  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   577  	}
   578  
   579  	// marshall dynamic bytes max length 64
   580  	buff.Reset()
   581  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   582  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   583  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   584  	buff.Write(bytesOut)
   585  
   586  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   587  	if err != nil {
   588  		t.Error(err)
   589  	}
   590  
   591  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   592  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   593  	}
   594  
   595  	// marshal dynamic bytes output empty
   596  	err = abi.Unpack(&Bytes, "bytes", nil)
   597  	if err == nil {
   598  		t.Error("expected error")
   599  	}
   600  
   601  	// marshal dynamic bytes length 5
   602  	buff.Reset()
   603  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   604  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   605  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   606  
   607  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   608  	if err != nil {
   609  		t.Error(err)
   610  	}
   611  
   612  	if !bytes.Equal(Bytes, []byte("hello")) {
   613  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   614  	}
   615  
   616  	// marshal dynamic bytes length 5
   617  	buff.Reset()
   618  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   619  
   620  	var hash common.Hash
   621  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   622  	if err != nil {
   623  		t.Error(err)
   624  	}
   625  
   626  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   627  	if hash != helloHash {
   628  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   629  	}
   630  
   631  	// marshal error
   632  	buff.Reset()
   633  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   634  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   635  	if err == nil {
   636  		t.Error("expected error")
   637  	}
   638  
   639  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   640  	if err == nil {
   641  		t.Error("expected error")
   642  	}
   643  
   644  	buff.Reset()
   645  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   646  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   647  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   648  	// marshal int array
   649  	var intArray [3]*big.Int
   650  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   651  	if err != nil {
   652  		t.Error(err)
   653  	}
   654  	var testAgainstIntArray [3]*big.Int
   655  	testAgainstIntArray[0] = big.NewInt(1)
   656  	testAgainstIntArray[1] = big.NewInt(2)
   657  	testAgainstIntArray[2] = big.NewInt(3)
   658  
   659  	for i, Int := range intArray {
   660  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   661  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   662  		}
   663  	}
   664  	// marshal address slice
   665  	buff.Reset()
   666  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   667  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   668  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   669  
   670  	var outAddr []common.Address
   671  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   672  	if err != nil {
   673  		t.Fatal("didn't expect error:", err)
   674  	}
   675  
   676  	if len(outAddr) != 1 {
   677  		t.Fatal("expected 1 item, got", len(outAddr))
   678  	}
   679  
   680  	if outAddr[0] != (common.Address{1}) {
   681  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   682  	}
   683  
   684  	// marshal multiple address slice
   685  	buff.Reset()
   686  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   687  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   688  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   689  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   690  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   691  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   692  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   693  
   694  	var outAddrStruct struct {
   695  		A []common.Address
   696  		B []common.Address
   697  	}
   698  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   699  	if err != nil {
   700  		t.Fatal("didn't expect error:", err)
   701  	}
   702  
   703  	if len(outAddrStruct.A) != 1 {
   704  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   705  	}
   706  
   707  	if outAddrStruct.A[0] != (common.Address{1}) {
   708  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   709  	}
   710  
   711  	if len(outAddrStruct.B) != 2 {
   712  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   713  	}
   714  
   715  	if outAddrStruct.B[0] != (common.Address{2}) {
   716  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   717  	}
   718  	if outAddrStruct.B[1] != (common.Address{3}) {
   719  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   720  	}
   721  
   722  	// marshal invalid address slice
   723  	buff.Reset()
   724  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   725  
   726  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   727  	if err == nil {
   728  		t.Fatal("expected error:", err)
   729  	}
   730  }
   731  
   732  func TestOOMMaliciousInput(t *testing.T) {
   733  	oomTests := []unpackTest{
   734  		{
   735  			def: `[{"type": "uint8[]"}]`,
   736  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   737  				"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
   738  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   739  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   740  		},
   741  		{ // Length larger than 64 bits
   742  			def: `[{"type": "uint8[]"}]`,
   743  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   744  				"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
   745  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   746  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   747  		},
   748  		{ // Offset very large (over 64 bits)
   749  			def: `[{"type": "uint8[]"}]`,
   750  			enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
   751  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   752  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   753  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   754  		},
   755  		{ // Offset very large (below 64 bits)
   756  			def: `[{"type": "uint8[]"}]`,
   757  			enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
   758  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   759  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   760  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   761  		},
   762  		{ // Offset negative (as 64 bit)
   763  			def: `[{"type": "uint8[]"}]`,
   764  			enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
   765  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   766  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   767  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   768  		},
   769  
   770  		{ // Negative length
   771  			def: `[{"type": "uint8[]"}]`,
   772  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   773  				"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
   774  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   775  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   776  		},
   777  		{ // Very large length
   778  			def: `[{"type": "uint8[]"}]`,
   779  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   780  				"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
   781  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   782  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   783  		},
   784  	}
   785  	for i, test := range oomTests {
   786  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   787  		abi, err := JSON(strings.NewReader(def))
   788  		if err != nil {
   789  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   790  		}
   791  		encb, err := hex.DecodeString(test.enc)
   792  		if err != nil {
   793  			t.Fatalf("invalid hex: %s" + test.enc)
   794  		}
   795  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   796  		if err == nil {
   797  			t.Fatalf("Expected error on malicious input, test %d", i)
   798  		}
   799  	}
   800  }