github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/accounts/abi/unpack_test.go (about)

     1  // Copyright 2015 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  	"strings"
    26  	"testing"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  )
    30  
    31  type unpackTest struct {
    32  	def  string      // ABI definition JSON
    33  	enc  string      // evm return data
    34  	want interface{} // the expected output
    35  	err  string      // empty or error if expected
    36  }
    37  
    38  func (test unpackTest) checkError(err error) error {
    39  	if err != nil {
    40  		if len(test.err) == 0 {
    41  			return fmt.Errorf("expected no err but got: %v", err)
    42  		} else if err.Error() != test.err {
    43  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    44  		}
    45  	} else if len(test.err) > 0 {
    46  		return fmt.Errorf("expected err: %v but got none", test.err)
    47  	}
    48  	return nil
    49  }
    50  
    51  var unpackTests = []unpackTest{
    52  	{
    53  		def:  `[{ "type": "bool" }]`,
    54  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    55  		want: true,
    56  	},
    57  	{
    58  		def:  `[{"type": "uint32"}]`,
    59  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    60  		want: uint32(1),
    61  	},
    62  	{
    63  		def:  `[{"type": "uint32"}]`,
    64  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    65  		want: uint16(0),
    66  		err:  "abi: cannot unmarshal uint32 in to uint16",
    67  	},
    68  	{
    69  		def:  `[{"type": "uint17"}]`,
    70  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    71  		want: uint16(0),
    72  		err:  "abi: cannot unmarshal *big.Int in to uint16",
    73  	},
    74  	{
    75  		def:  `[{"type": "uint17"}]`,
    76  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    77  		want: big.NewInt(1),
    78  	},
    79  	{
    80  		def:  `[{"type": "int32"}]`,
    81  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    82  		want: int32(1),
    83  	},
    84  	{
    85  		def:  `[{"type": "int32"}]`,
    86  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    87  		want: int16(0),
    88  		err:  "abi: cannot unmarshal int32 in to int16",
    89  	},
    90  	{
    91  		def:  `[{"type": "int17"}]`,
    92  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    93  		want: int16(0),
    94  		err:  "abi: cannot unmarshal *big.Int in to int16",
    95  	},
    96  	{
    97  		def:  `[{"type": "int17"}]`,
    98  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    99  		want: big.NewInt(1),
   100  	},
   101  	{
   102  		def:  `[{"type": "address"}]`,
   103  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   104  		want: common.Address{1},
   105  	},
   106  	{
   107  		def:  `[{"type": "bytes32"}]`,
   108  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   109  		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},
   110  	},
   111  	{
   112  		def:  `[{"type": "bytes"}]`,
   113  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   114  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   115  	},
   116  	{
   117  		def:  `[{"type": "bytes"}]`,
   118  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   119  		want: [32]byte{},
   120  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   121  	},
   122  	{
   123  		def:  `[{"type": "bytes32"}]`,
   124  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   125  		want: []byte(nil),
   126  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   127  	},
   128  	{
   129  		def:  `[{"type": "bytes32"}]`,
   130  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   131  		want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
   132  	},
   133  	{
   134  		def:  `[{"type": "function"}]`,
   135  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   136  		want: [24]byte{1},
   137  	},
   138  	// slices
   139  	{
   140  		def:  `[{"type": "uint8[]"}]`,
   141  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   142  		want: []uint8{1, 2},
   143  	},
   144  	{
   145  		def:  `[{"type": "uint8[2]"}]`,
   146  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   147  		want: [2]uint8{1, 2},
   148  	},
   149  	// multi dimensional, if these pass, all types that don't require length prefix should pass
   150  	{
   151  		def:  `[{"type": "uint8[][]"}]`,
   152  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   153  		want: [][]uint8{{1, 2}, {1, 2}},
   154  	},
   155  	{
   156  		def:  `[{"type": "uint8[2][2]"}]`,
   157  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   158  		want: [2][2]uint8{{1, 2}, {1, 2}},
   159  	},
   160  	{
   161  		def:  `[{"type": "uint8[][2]"}]`,
   162  		enc:  "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   163  		want: [2][]uint8{{1}, {1}},
   164  	},
   165  	{
   166  		def:  `[{"type": "uint8[2][]"}]`,
   167  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   168  		want: [][2]uint8{{1, 2}},
   169  	},
   170  	{
   171  		def:  `[{"type": "uint16[]"}]`,
   172  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   173  		want: []uint16{1, 2},
   174  	},
   175  	{
   176  		def:  `[{"type": "uint16[2]"}]`,
   177  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   178  		want: [2]uint16{1, 2},
   179  	},
   180  	{
   181  		def:  `[{"type": "uint32[]"}]`,
   182  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   183  		want: []uint32{1, 2},
   184  	},
   185  	{
   186  		def:  `[{"type": "uint32[2]"}]`,
   187  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   188  		want: [2]uint32{1, 2},
   189  	},
   190  	{
   191  		def:  `[{"type": "uint64[]"}]`,
   192  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   193  		want: []uint64{1, 2},
   194  	},
   195  	{
   196  		def:  `[{"type": "uint64[2]"}]`,
   197  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   198  		want: [2]uint64{1, 2},
   199  	},
   200  	{
   201  		def:  `[{"type": "uint256[]"}]`,
   202  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   203  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   204  	},
   205  	{
   206  		def:  `[{"type": "uint256[3]"}]`,
   207  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   208  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   209  	},
   210  	{
   211  		def:  `[{"type": "int8[]"}]`,
   212  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   213  		want: []int8{1, 2},
   214  	},
   215  	{
   216  		def:  `[{"type": "int8[2]"}]`,
   217  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   218  		want: [2]int8{1, 2},
   219  	},
   220  	{
   221  		def:  `[{"type": "int16[]"}]`,
   222  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   223  		want: []int16{1, 2},
   224  	},
   225  	{
   226  		def:  `[{"type": "int16[2]"}]`,
   227  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   228  		want: [2]int16{1, 2},
   229  	},
   230  	{
   231  		def:  `[{"type": "int32[]"}]`,
   232  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   233  		want: []int32{1, 2},
   234  	},
   235  	{
   236  		def:  `[{"type": "int32[2]"}]`,
   237  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   238  		want: [2]int32{1, 2},
   239  	},
   240  	{
   241  		def:  `[{"type": "int64[]"}]`,
   242  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   243  		want: []int64{1, 2},
   244  	},
   245  	{
   246  		def:  `[{"type": "int64[2]"}]`,
   247  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   248  		want: [2]int64{1, 2},
   249  	},
   250  	{
   251  		def:  `[{"type": "int256[]"}]`,
   252  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   253  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   254  	},
   255  	{
   256  		def:  `[{"type": "int256[3]"}]`,
   257  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   258  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   259  	},
   260  }
   261  
   262  func TestUnpack(t *testing.T) {
   263  	for i, test := range unpackTests {
   264  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   265  		abi, err := JSON(strings.NewReader(def))
   266  		if err != nil {
   267  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   268  		}
   269  		encb, err := hex.DecodeString(test.enc)
   270  		if err != nil {
   271  			t.Fatalf("invalid hex: %s" + test.enc)
   272  		}
   273  		outptr := reflect.New(reflect.TypeOf(test.want))
   274  		err = abi.Unpack(outptr.Interface(), "method", encb)
   275  		if err := test.checkError(err); err != nil {
   276  			t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   277  			continue
   278  		}
   279  		out := outptr.Elem().Interface()
   280  		if !reflect.DeepEqual(test.want, out) {
   281  			t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   282  		}
   283  	}
   284  }
   285  
   286  func TestMultiReturnWithStruct(t *testing.T) {
   287  	const definition = `[
   288  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   289  
   290  	abi, err := JSON(strings.NewReader(definition))
   291  	if err != nil {
   292  		t.Fatal(err)
   293  	}
   294  
   295  	// using buff to make the code readable
   296  	buff := new(bytes.Buffer)
   297  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   298  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   299  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   300  	stringOut := "hello"
   301  	buff.Write(common.RightPadBytes([]byte(stringOut), 32))
   302  
   303  	var inter struct {
   304  		Int    *big.Int
   305  		String string
   306  	}
   307  	err = abi.Unpack(&inter, "multi", buff.Bytes())
   308  	if err != nil {
   309  		t.Error(err)
   310  	}
   311  
   312  	if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
   313  		t.Error("expected Int to be 1 got", inter.Int)
   314  	}
   315  
   316  	if inter.String != stringOut {
   317  		t.Error("expected String to be", stringOut, "got", inter.String)
   318  	}
   319  
   320  	var reversed struct {
   321  		String string
   322  		Int    *big.Int
   323  	}
   324  
   325  	err = abi.Unpack(&reversed, "multi", buff.Bytes())
   326  	if err != nil {
   327  		t.Error(err)
   328  	}
   329  
   330  	if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
   331  		t.Error("expected Int to be 1 got", reversed.Int)
   332  	}
   333  
   334  	if reversed.String != stringOut {
   335  		t.Error("expected String to be", stringOut, "got", reversed.String)
   336  	}
   337  }
   338  
   339  func TestUnmarshal(t *testing.T) {
   340  	const definition = `[
   341  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   342  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   343  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   344  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   345  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   346  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   347  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   348  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   349  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   350  
   351  	abi, err := JSON(strings.NewReader(definition))
   352  	if err != nil {
   353  		t.Fatal(err)
   354  	}
   355  	buff := new(bytes.Buffer)
   356  
   357  	// marshall mixed bytes (mixedBytes)
   358  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   359  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   360  	mixedBytes := []interface{}{&p0, &p1}
   361  
   362  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   363  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   364  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   365  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   366  
   367  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   368  	if err != nil {
   369  		t.Error(err)
   370  	} else {
   371  		if bytes.Compare(p0, p0Exp) != 0 {
   372  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   373  		}
   374  
   375  		if bytes.Compare(p1[:], p1Exp) != 0 {
   376  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   377  		}
   378  	}
   379  
   380  	// marshal int
   381  	var Int *big.Int
   382  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   383  	if err != nil {
   384  		t.Error(err)
   385  	}
   386  
   387  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   388  		t.Error("expected Int to be 1 got", Int)
   389  	}
   390  
   391  	// marshal bool
   392  	var Bool bool
   393  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   394  	if err != nil {
   395  		t.Error(err)
   396  	}
   397  
   398  	if !Bool {
   399  		t.Error("expected Bool to be true")
   400  	}
   401  
   402  	// marshal dynamic bytes max length 32
   403  	buff.Reset()
   404  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   405  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   406  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   407  	buff.Write(bytesOut)
   408  
   409  	var Bytes []byte
   410  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   411  	if err != nil {
   412  		t.Error(err)
   413  	}
   414  
   415  	if !bytes.Equal(Bytes, bytesOut) {
   416  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   417  	}
   418  
   419  	// marshall dynamic bytes max length 64
   420  	buff.Reset()
   421  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   422  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   423  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   424  	buff.Write(bytesOut)
   425  
   426  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   427  	if err != nil {
   428  		t.Error(err)
   429  	}
   430  
   431  	if !bytes.Equal(Bytes, bytesOut) {
   432  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   433  	}
   434  
   435  	// marshall dynamic bytes max length 64
   436  	buff.Reset()
   437  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   438  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   439  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   440  	buff.Write(bytesOut)
   441  
   442  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   443  	if err != nil {
   444  		t.Error(err)
   445  	}
   446  
   447  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   448  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   449  	}
   450  
   451  	// marshal dynamic bytes output empty
   452  	err = abi.Unpack(&Bytes, "bytes", nil)
   453  	if err == nil {
   454  		t.Error("expected error")
   455  	}
   456  
   457  	// marshal dynamic bytes length 5
   458  	buff.Reset()
   459  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   460  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   461  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   462  
   463  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   464  	if err != nil {
   465  		t.Error(err)
   466  	}
   467  
   468  	if !bytes.Equal(Bytes, []byte("hello")) {
   469  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   470  	}
   471  
   472  	// marshal dynamic bytes length 5
   473  	buff.Reset()
   474  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   475  
   476  	var hash common.Hash
   477  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   478  	if err != nil {
   479  		t.Error(err)
   480  	}
   481  
   482  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   483  	if hash != helloHash {
   484  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   485  	}
   486  
   487  	// marshal error
   488  	buff.Reset()
   489  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   490  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   491  	if err == nil {
   492  		t.Error("expected error")
   493  	}
   494  
   495  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   496  	if err == nil {
   497  		t.Error("expected error")
   498  	}
   499  
   500  	buff.Reset()
   501  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   502  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   503  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   504  	// marshal int array
   505  	var intArray [3]*big.Int
   506  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   507  	if err != nil {
   508  		t.Error(err)
   509  	}
   510  	var testAgainstIntArray [3]*big.Int
   511  	testAgainstIntArray[0] = big.NewInt(1)
   512  	testAgainstIntArray[1] = big.NewInt(2)
   513  	testAgainstIntArray[2] = big.NewInt(3)
   514  
   515  	for i, Int := range intArray {
   516  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   517  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   518  		}
   519  	}
   520  	// marshal address slice
   521  	buff.Reset()
   522  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   523  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   524  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   525  
   526  	var outAddr []common.Address
   527  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   528  	if err != nil {
   529  		t.Fatal("didn't expect error:", err)
   530  	}
   531  
   532  	if len(outAddr) != 1 {
   533  		t.Fatal("expected 1 item, got", len(outAddr))
   534  	}
   535  
   536  	if outAddr[0] != (common.Address{1}) {
   537  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   538  	}
   539  
   540  	// marshal multiple address slice
   541  	buff.Reset()
   542  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   543  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   544  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   545  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   546  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   547  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   548  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   549  
   550  	var outAddrStruct struct {
   551  		A []common.Address
   552  		B []common.Address
   553  	}
   554  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   555  	if err != nil {
   556  		t.Fatal("didn't expect error:", err)
   557  	}
   558  
   559  	if len(outAddrStruct.A) != 1 {
   560  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   561  	}
   562  
   563  	if outAddrStruct.A[0] != (common.Address{1}) {
   564  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   565  	}
   566  
   567  	if len(outAddrStruct.B) != 2 {
   568  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   569  	}
   570  
   571  	if outAddrStruct.B[0] != (common.Address{2}) {
   572  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   573  	}
   574  	if outAddrStruct.B[1] != (common.Address{3}) {
   575  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   576  	}
   577  
   578  	// marshal invalid address slice
   579  	buff.Reset()
   580  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   581  
   582  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   583  	if err == nil {
   584  		t.Fatal("expected error:", err)
   585  	}
   586  }