github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/accounts/abi/abi_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  	"fmt"
    22  	"log"
    23  	"math/big"
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/atheioschain/go-atheios/common"
    29  	"github.com/atheioschain/go-atheios/crypto"
    30  )
    31  
    32  // formatSilceOutput add padding to the value and adds a size
    33  func formatSliceOutput(v ...[]byte) []byte {
    34  	off := common.LeftPadBytes(big.NewInt(int64(len(v))).Bytes(), 32)
    35  	output := append(off, make([]byte, 0, len(v)*32)...)
    36  
    37  	for _, value := range v {
    38  		output = append(output, common.LeftPadBytes(value, 32)...)
    39  	}
    40  	return output
    41  }
    42  
    43  // quick helper padding
    44  func pad(input []byte, size int, left bool) []byte {
    45  	if left {
    46  		return common.LeftPadBytes(input, size)
    47  	}
    48  	return common.RightPadBytes(input, size)
    49  }
    50  
    51  func TestTypeCheck(t *testing.T) {
    52  	for i, test := range []struct {
    53  		typ   string
    54  		input interface{}
    55  		err   string
    56  	}{
    57  		{"uint", big.NewInt(1), ""},
    58  		{"int", big.NewInt(1), ""},
    59  		{"uint30", big.NewInt(1), ""},
    60  		{"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"},
    61  		{"uint16", uint16(1), ""},
    62  		{"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
    63  		{"uint16[]", []uint16{1, 2, 3}, ""},
    64  		{"uint16[]", [3]uint16{1, 2, 3}, ""},
    65  		{"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"},
    66  		{"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
    67  		{"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
    68  		{"uint16[3]", []uint16{1, 2, 3}, ""},
    69  		{"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
    70  		{"address[]", []common.Address{{1}}, ""},
    71  		{"address[1]", []common.Address{{1}}, ""},
    72  		{"address[1]", [1]common.Address{{1}}, ""},
    73  		{"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"},
    74  		{"bytes32", [32]byte{}, ""},
    75  		{"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
    76  		{"bytes32", common.Hash{1}, ""},
    77  		{"bytes31", [31]byte{}, ""},
    78  		{"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
    79  		{"bytes", []byte{0, 1}, ""},
    80  		{"bytes", [2]byte{0, 1}, ""},
    81  		{"bytes", common.Hash{1}, ""},
    82  		{"string", "hello world", ""},
    83  		{"bytes32[]", [][32]byte{{}}, ""},
    84  		{"function", [24]byte{}, ""},
    85  	} {
    86  		typ, err := NewType(test.typ)
    87  		if err != nil {
    88  			t.Fatal("unexpected parse error:", err)
    89  		}
    90  
    91  		err = typeCheck(typ, reflect.ValueOf(test.input))
    92  		if err != nil && len(test.err) == 0 {
    93  			t.Errorf("%d failed. Expected no err but got: %v", i, err)
    94  			continue
    95  		}
    96  		if err == nil && len(test.err) != 0 {
    97  			t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
    98  			continue
    99  		}
   100  
   101  		if err != nil && len(test.err) != 0 && err.Error() != test.err {
   102  			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
   103  		}
   104  	}
   105  }
   106  
   107  func TestSimpleMethodUnpack(t *testing.T) {
   108  	for i, test := range []struct {
   109  		def              string      // definition of the **output** ABI params
   110  		marshalledOutput []byte      // evm return data
   111  		expectedOut      interface{} // the expected output
   112  		outVar           string      // the output variable (e.g. uint32, *big.Int, etc)
   113  		err              string      // empty or error if expected
   114  	}{
   115  		{
   116  			`[ { "type": "uint32" } ]`,
   117  			pad([]byte{1}, 32, true),
   118  			uint32(1),
   119  			"uint32",
   120  			"",
   121  		},
   122  		{
   123  			`[ { "type": "uint32" } ]`,
   124  			pad([]byte{1}, 32, true),
   125  			nil,
   126  			"uint16",
   127  			"abi: cannot unmarshal uint32 in to uint16",
   128  		},
   129  		{
   130  			`[ { "type": "uint17" } ]`,
   131  			pad([]byte{1}, 32, true),
   132  			nil,
   133  			"uint16",
   134  			"abi: cannot unmarshal *big.Int in to uint16",
   135  		},
   136  		{
   137  			`[ { "type": "uint17" } ]`,
   138  			pad([]byte{1}, 32, true),
   139  			big.NewInt(1),
   140  			"*big.Int",
   141  			"",
   142  		},
   143  
   144  		{
   145  			`[ { "type": "int32" } ]`,
   146  			pad([]byte{1}, 32, true),
   147  			int32(1),
   148  			"int32",
   149  			"",
   150  		},
   151  		{
   152  			`[ { "type": "int32" } ]`,
   153  			pad([]byte{1}, 32, true),
   154  			nil,
   155  			"int16",
   156  			"abi: cannot unmarshal int32 in to int16",
   157  		},
   158  		{
   159  			`[ { "type": "int17" } ]`,
   160  			pad([]byte{1}, 32, true),
   161  			nil,
   162  			"int16",
   163  			"abi: cannot unmarshal *big.Int in to int16",
   164  		},
   165  		{
   166  			`[ { "type": "int17" } ]`,
   167  			pad([]byte{1}, 32, true),
   168  			big.NewInt(1),
   169  			"*big.Int",
   170  			"",
   171  		},
   172  
   173  		{
   174  			`[ { "type": "address" } ]`,
   175  			pad(pad([]byte{1}, 20, false), 32, true),
   176  			common.Address{1},
   177  			"address",
   178  			"",
   179  		},
   180  		{
   181  			`[ { "type": "bytes32" } ]`,
   182  			pad([]byte{1}, 32, false),
   183  			pad([]byte{1}, 32, false),
   184  			"bytes",
   185  			"",
   186  		},
   187  		{
   188  			`[ { "type": "bytes32" } ]`,
   189  			pad([]byte{1}, 32, false),
   190  			pad([]byte{1}, 32, false),
   191  			"hash",
   192  			"",
   193  		},
   194  		{
   195  			`[ { "type": "bytes32" } ]`,
   196  			pad([]byte{1}, 32, false),
   197  			pad([]byte{1}, 32, false),
   198  			"interface",
   199  			"",
   200  		},
   201  		{
   202  			`[ { "type": "function" } ]`,
   203  			pad([]byte{1}, 32, false),
   204  			[24]byte{1},
   205  			"function",
   206  			"",
   207  		},
   208  	} {
   209  		abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   210  		abi, err := JSON(strings.NewReader(abiDefinition))
   211  		if err != nil {
   212  			t.Errorf("%d failed. %v", i, err)
   213  			continue
   214  		}
   215  
   216  		var outvar interface{}
   217  		switch test.outVar {
   218  		case "uint8":
   219  			var v uint8
   220  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   221  			outvar = v
   222  		case "uint16":
   223  			var v uint16
   224  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   225  			outvar = v
   226  		case "uint32":
   227  			var v uint32
   228  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   229  			outvar = v
   230  		case "uint64":
   231  			var v uint64
   232  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   233  			outvar = v
   234  		case "int8":
   235  			var v int8
   236  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   237  			outvar = v
   238  		case "int16":
   239  			var v int16
   240  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   241  			outvar = v
   242  		case "int32":
   243  			var v int32
   244  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   245  			outvar = v
   246  		case "int64":
   247  			var v int64
   248  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   249  			outvar = v
   250  		case "*big.Int":
   251  			var v *big.Int
   252  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   253  			outvar = v
   254  		case "address":
   255  			var v common.Address
   256  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   257  			outvar = v
   258  		case "bytes":
   259  			var v []byte
   260  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   261  			outvar = v
   262  		case "hash":
   263  			var v common.Hash
   264  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   265  			outvar = v
   266  		case "function":
   267  			var v [24]byte
   268  			err = abi.Unpack(&v, "method", test.marshalledOutput)
   269  			outvar = v
   270  		case "interface":
   271  			err = abi.Unpack(&outvar, "method", test.marshalledOutput)
   272  		default:
   273  			t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar)
   274  			continue
   275  		}
   276  
   277  		if err != nil && len(test.err) == 0 {
   278  			t.Errorf("%d failed. Expected no err but got: %v", i, err)
   279  			continue
   280  		}
   281  		if err == nil && len(test.err) != 0 {
   282  			t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
   283  			continue
   284  		}
   285  		if err != nil && len(test.err) != 0 && err.Error() != test.err {
   286  			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
   287  			continue
   288  		}
   289  
   290  		if err == nil {
   291  			// bit of an ugly hack for hash type but I don't feel like finding a proper solution
   292  			if test.outVar == "hash" {
   293  				tmp := outvar.(common.Hash) // without assignment it's unaddressable
   294  				outvar = tmp[:]
   295  			}
   296  
   297  			if !reflect.DeepEqual(test.expectedOut, outvar) {
   298  				t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar)
   299  			}
   300  		}
   301  	}
   302  }
   303  
   304  func TestUnpackSetInterfaceSlice(t *testing.T) {
   305  	var (
   306  		var1 = new(uint8)
   307  		var2 = new(uint8)
   308  	)
   309  	out := []interface{}{var1, var2}
   310  	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`))
   311  	if err != nil {
   312  		t.Fatal(err)
   313  	}
   314  	marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
   315  	err = abi.Unpack(&out, "ints", marshalledReturn)
   316  	if err != nil {
   317  		t.Fatal(err)
   318  	}
   319  	if *var1 != 1 {
   320  		t.Error("expected var1 to be 1, got", *var1)
   321  	}
   322  	if *var2 != 2 {
   323  		t.Error("expected var2 to be 2, got", *var2)
   324  	}
   325  
   326  	out = []interface{}{var1}
   327  	err = abi.Unpack(&out, "ints", marshalledReturn)
   328  
   329  	expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)"
   330  	if err == nil || err.Error() != expErr {
   331  		t.Error("expected err:", expErr, "Got:", err)
   332  	}
   333  }
   334  
   335  func TestUnpackSetInterfaceArrayOutput(t *testing.T) {
   336  	var (
   337  		var1 = new([1]uint32)
   338  		var2 = new([1]uint32)
   339  	)
   340  	out := []interface{}{var1, var2}
   341  	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint32[1]"}, {"type":"uint32[1]"}]}]`))
   342  	if err != nil {
   343  		t.Fatal(err)
   344  	}
   345  	marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
   346  	err = abi.Unpack(&out, "ints", marshalledReturn)
   347  	if err != nil {
   348  		t.Fatal(err)
   349  	}
   350  
   351  	if *var1 != [1]uint32{1} {
   352  		t.Error("expected var1 to be [1], got", *var1)
   353  	}
   354  	if *var2 != [1]uint32{2} {
   355  		t.Error("expected var2 to be [2], got", *var2)
   356  	}
   357  }
   358  
   359  func TestPack(t *testing.T) {
   360  	for i, test := range []struct {
   361  		typ string
   362  
   363  		input  interface{}
   364  		output []byte
   365  	}{
   366  		{"uint16", uint16(2), pad([]byte{2}, 32, true)},
   367  		{"uint16[]", []uint16{1, 2}, formatSliceOutput([]byte{1}, []byte{2})},
   368  		{"bytes20", [20]byte{1}, pad([]byte{1}, 32, false)},
   369  		{"uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, formatSliceOutput([]byte{1}, []byte{2})},
   370  		{"address[]", []common.Address{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))},
   371  		{"bytes32[]", []common.Hash{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))},
   372  		{"function", [24]byte{1}, pad([]byte{1}, 32, false)},
   373  	} {
   374  		typ, err := NewType(test.typ)
   375  		if err != nil {
   376  			t.Fatal("unexpected parse error:", err)
   377  		}
   378  
   379  		output, err := typ.pack(reflect.ValueOf(test.input))
   380  		if err != nil {
   381  			t.Fatal("unexpected pack error:", err)
   382  		}
   383  
   384  		if !bytes.Equal(output, test.output) {
   385  			t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
   386  		}
   387  	}
   388  }
   389  
   390  func TestMethodPack(t *testing.T) {
   391  	abi, err := JSON(strings.NewReader(jsondata2))
   392  	if err != nil {
   393  		t.Fatal(err)
   394  	}
   395  
   396  	sig := abi.Methods["slice"].Id()
   397  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   398  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   399  
   400  	packed, err := abi.Pack("slice", []uint32{1, 2})
   401  	if err != nil {
   402  		t.Error(err)
   403  	}
   404  
   405  	if !bytes.Equal(packed, sig) {
   406  		t.Errorf("expected %x got %x", sig, packed)
   407  	}
   408  
   409  	var addrA, addrB = common.Address{1}, common.Address{2}
   410  	sig = abi.Methods["sliceAddress"].Id()
   411  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
   412  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   413  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   414  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   415  
   416  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
   417  	if err != nil {
   418  		t.Fatal(err)
   419  	}
   420  	if !bytes.Equal(packed, sig) {
   421  		t.Errorf("expected %x got %x", sig, packed)
   422  	}
   423  
   424  	var addrC, addrD = common.Address{3}, common.Address{4}
   425  	sig = abi.Methods["sliceMultiAddress"].Id()
   426  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
   427  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
   428  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   429  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   430  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   431  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   432  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   433  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   434  
   435  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   436  	if err != nil {
   437  		t.Fatal(err)
   438  	}
   439  	if !bytes.Equal(packed, sig) {
   440  		t.Errorf("expected %x got %x", sig, packed)
   441  	}
   442  
   443  	sig = abi.Methods["slice256"].Id()
   444  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   445  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   446  
   447  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   448  	if err != nil {
   449  		t.Error(err)
   450  	}
   451  
   452  	if !bytes.Equal(packed, sig) {
   453  		t.Errorf("expected %x got %x", sig, packed)
   454  	}
   455  }
   456  
   457  const jsondata = `
   458  [
   459  	{ "type" : "function", "name" : "balance", "constant" : true },
   460  	{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
   461  ]`
   462  
   463  const jsondata2 = `
   464  [
   465  	{ "type" : "function", "name" : "balance", "constant" : true },
   466  	{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
   467  	{ "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
   468  	{ "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
   469  	{ "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
   470  	{ "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
   471  	{ "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
   472  	{ "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
   473  	{ "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
   474  	{ "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
   475  	{ "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
   476  	{ "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
   477  	{ "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
   478  	{ "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
   479  ]`
   480  
   481  func TestReader(t *testing.T) {
   482  	Uint256, _ := NewType("uint256")
   483  	exp := ABI{
   484  		Methods: map[string]Method{
   485  			"balance": {
   486  				"balance", true, nil, nil,
   487  			},
   488  			"send": {
   489  				"send", false, []Argument{
   490  					{"amount", Uint256, false},
   491  				}, nil,
   492  			},
   493  		},
   494  	}
   495  
   496  	abi, err := JSON(strings.NewReader(jsondata))
   497  	if err != nil {
   498  		t.Error(err)
   499  	}
   500  
   501  	// deep equal fails for some reason
   502  	t.Skip()
   503  	if !reflect.DeepEqual(abi, exp) {
   504  		t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
   505  	}
   506  }
   507  
   508  func TestTestNumbers(t *testing.T) {
   509  	abi, err := JSON(strings.NewReader(jsondata2))
   510  	if err != nil {
   511  		t.Error(err)
   512  		t.FailNow()
   513  	}
   514  
   515  	if _, err := abi.Pack("balance"); err != nil {
   516  		t.Error(err)
   517  	}
   518  
   519  	if _, err := abi.Pack("balance", 1); err == nil {
   520  		t.Error("expected error for balance(1)")
   521  	}
   522  
   523  	if _, err := abi.Pack("doesntexist", nil); err == nil {
   524  		t.Errorf("doesntexist shouldn't exist")
   525  	}
   526  
   527  	if _, err := abi.Pack("doesntexist", 1); err == nil {
   528  		t.Errorf("doesntexist(1) shouldn't exist")
   529  	}
   530  
   531  	if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
   532  		t.Error(err)
   533  	}
   534  
   535  	i := new(int)
   536  	*i = 1000
   537  	if _, err := abi.Pack("send", i); err == nil {
   538  		t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
   539  	}
   540  
   541  	if _, err := abi.Pack("test", uint32(1000)); err != nil {
   542  		t.Error(err)
   543  	}
   544  }
   545  
   546  func TestTestString(t *testing.T) {
   547  	abi, err := JSON(strings.NewReader(jsondata2))
   548  	if err != nil {
   549  		t.Error(err)
   550  		t.FailNow()
   551  	}
   552  
   553  	if _, err := abi.Pack("string", "hello world"); err != nil {
   554  		t.Error(err)
   555  	}
   556  }
   557  
   558  func TestTestBool(t *testing.T) {
   559  	abi, err := JSON(strings.NewReader(jsondata2))
   560  	if err != nil {
   561  		t.Error(err)
   562  		t.FailNow()
   563  	}
   564  
   565  	if _, err := abi.Pack("bool", true); err != nil {
   566  		t.Error(err)
   567  	}
   568  }
   569  
   570  func TestTestSlice(t *testing.T) {
   571  	abi, err := JSON(strings.NewReader(jsondata2))
   572  	if err != nil {
   573  		t.Error(err)
   574  		t.FailNow()
   575  	}
   576  
   577  	slice := make([]uint64, 2)
   578  	if _, err := abi.Pack("uint64[2]", slice); err != nil {
   579  		t.Error(err)
   580  	}
   581  
   582  	if _, err := abi.Pack("uint64[]", slice); err != nil {
   583  		t.Error(err)
   584  	}
   585  }
   586  
   587  func TestMethodSignature(t *testing.T) {
   588  	String, _ := NewType("string")
   589  	m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
   590  	exp := "foo(string,string)"
   591  	if m.Sig() != exp {
   592  		t.Error("signature mismatch", exp, "!=", m.Sig())
   593  	}
   594  
   595  	idexp := crypto.Keccak256([]byte(exp))[:4]
   596  	if !bytes.Equal(m.Id(), idexp) {
   597  		t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
   598  	}
   599  
   600  	uintt, _ := NewType("uint")
   601  	m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
   602  	exp = "foo(uint256)"
   603  	if m.Sig() != exp {
   604  		t.Error("signature mismatch", exp, "!=", m.Sig())
   605  	}
   606  }
   607  
   608  func TestMultiPack(t *testing.T) {
   609  	abi, err := JSON(strings.NewReader(jsondata2))
   610  	if err != nil {
   611  		t.Error(err)
   612  		t.FailNow()
   613  	}
   614  
   615  	sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
   616  	sig = append(sig, make([]byte, 64)...)
   617  	sig[35] = 10
   618  	sig[67] = 11
   619  
   620  	packed, err := abi.Pack("bar", uint32(10), uint16(11))
   621  	if err != nil {
   622  		t.Error(err)
   623  		t.FailNow()
   624  	}
   625  
   626  	if !bytes.Equal(packed, sig) {
   627  		t.Errorf("expected %x got %x", sig, packed)
   628  	}
   629  }
   630  
   631  func ExampleJSON() {
   632  	const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
   633  
   634  	abi, err := JSON(strings.NewReader(definition))
   635  	if err != nil {
   636  		log.Fatalln(err)
   637  	}
   638  	out, err := abi.Pack("isBar", common.HexToAddress("01"))
   639  	if err != nil {
   640  		log.Fatalln(err)
   641  	}
   642  
   643  	fmt.Printf("%x\n", out)
   644  	// Output:
   645  	// 1f2c40920000000000000000000000000000000000000000000000000000000000000001
   646  }
   647  
   648  func TestInputVariableInputLength(t *testing.T) {
   649  	const definition = `[
   650  	{ "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
   651  	{ "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
   652  	{ "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
   653  	]`
   654  
   655  	abi, err := JSON(strings.NewReader(definition))
   656  	if err != nil {
   657  		t.Fatal(err)
   658  	}
   659  
   660  	// test one string
   661  	strin := "hello world"
   662  	strpack, err := abi.Pack("strOne", strin)
   663  	if err != nil {
   664  		t.Error(err)
   665  	}
   666  
   667  	offset := make([]byte, 32)
   668  	offset[31] = 32
   669  	length := make([]byte, 32)
   670  	length[31] = byte(len(strin))
   671  	value := common.RightPadBytes([]byte(strin), 32)
   672  	exp := append(offset, append(length, value...)...)
   673  
   674  	// ignore first 4 bytes of the output. This is the function identifier
   675  	strpack = strpack[4:]
   676  	if !bytes.Equal(strpack, exp) {
   677  		t.Errorf("expected %x, got %x\n", exp, strpack)
   678  	}
   679  
   680  	// test one bytes
   681  	btspack, err := abi.Pack("bytesOne", []byte(strin))
   682  	if err != nil {
   683  		t.Error(err)
   684  	}
   685  	// ignore first 4 bytes of the output. This is the function identifier
   686  	btspack = btspack[4:]
   687  	if !bytes.Equal(btspack, exp) {
   688  		t.Errorf("expected %x, got %x\n", exp, btspack)
   689  	}
   690  
   691  	//  test two strings
   692  	str1 := "hello"
   693  	str2 := "world"
   694  	str2pack, err := abi.Pack("strTwo", str1, str2)
   695  	if err != nil {
   696  		t.Error(err)
   697  	}
   698  
   699  	offset1 := make([]byte, 32)
   700  	offset1[31] = 64
   701  	length1 := make([]byte, 32)
   702  	length1[31] = byte(len(str1))
   703  	value1 := common.RightPadBytes([]byte(str1), 32)
   704  
   705  	offset2 := make([]byte, 32)
   706  	offset2[31] = 128
   707  	length2 := make([]byte, 32)
   708  	length2[31] = byte(len(str2))
   709  	value2 := common.RightPadBytes([]byte(str2), 32)
   710  
   711  	exp2 := append(offset1, offset2...)
   712  	exp2 = append(exp2, append(length1, value1...)...)
   713  	exp2 = append(exp2, append(length2, value2...)...)
   714  
   715  	// ignore first 4 bytes of the output. This is the function identifier
   716  	str2pack = str2pack[4:]
   717  	if !bytes.Equal(str2pack, exp2) {
   718  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   719  	}
   720  
   721  	// test two strings, first > 32, second < 32
   722  	str1 = strings.Repeat("a", 33)
   723  	str2pack, err = abi.Pack("strTwo", str1, str2)
   724  	if err != nil {
   725  		t.Error(err)
   726  	}
   727  
   728  	offset1 = make([]byte, 32)
   729  	offset1[31] = 64
   730  	length1 = make([]byte, 32)
   731  	length1[31] = byte(len(str1))
   732  	value1 = common.RightPadBytes([]byte(str1), 64)
   733  	offset2[31] = 160
   734  
   735  	exp2 = append(offset1, offset2...)
   736  	exp2 = append(exp2, append(length1, value1...)...)
   737  	exp2 = append(exp2, append(length2, value2...)...)
   738  
   739  	// ignore first 4 bytes of the output. This is the function identifier
   740  	str2pack = str2pack[4:]
   741  	if !bytes.Equal(str2pack, exp2) {
   742  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   743  	}
   744  
   745  	// test two strings, first > 32, second >32
   746  	str1 = strings.Repeat("a", 33)
   747  	str2 = strings.Repeat("a", 33)
   748  	str2pack, err = abi.Pack("strTwo", str1, str2)
   749  	if err != nil {
   750  		t.Error(err)
   751  	}
   752  
   753  	offset1 = make([]byte, 32)
   754  	offset1[31] = 64
   755  	length1 = make([]byte, 32)
   756  	length1[31] = byte(len(str1))
   757  	value1 = common.RightPadBytes([]byte(str1), 64)
   758  
   759  	offset2 = make([]byte, 32)
   760  	offset2[31] = 160
   761  	length2 = make([]byte, 32)
   762  	length2[31] = byte(len(str2))
   763  	value2 = common.RightPadBytes([]byte(str2), 64)
   764  
   765  	exp2 = append(offset1, offset2...)
   766  	exp2 = append(exp2, append(length1, value1...)...)
   767  	exp2 = append(exp2, append(length2, value2...)...)
   768  
   769  	// ignore first 4 bytes of the output. This is the function identifier
   770  	str2pack = str2pack[4:]
   771  	if !bytes.Equal(str2pack, exp2) {
   772  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   773  	}
   774  }
   775  
   776  func TestDefaultFunctionParsing(t *testing.T) {
   777  	const definition = `[{ "name" : "balance" }]`
   778  
   779  	abi, err := JSON(strings.NewReader(definition))
   780  	if err != nil {
   781  		t.Fatal(err)
   782  	}
   783  
   784  	if _, ok := abi.Methods["balance"]; !ok {
   785  		t.Error("expected 'balance' to be present")
   786  	}
   787  }
   788  
   789  func TestBareEvents(t *testing.T) {
   790  	const definition = `[
   791  	{ "type" : "event", "name" : "balance" },
   792  	{ "type" : "event", "name" : "anon", "anonymous" : true},
   793  	{ "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
   794  	]`
   795  
   796  	arg0, _ := NewType("uint256")
   797  	arg1, _ := NewType("address")
   798  
   799  	expectedEvents := map[string]struct {
   800  		Anonymous bool
   801  		Args      []Argument
   802  	}{
   803  		"balance": {false, nil},
   804  		"anon":    {true, nil},
   805  		"args": {false, []Argument{
   806  			{Name: "arg0", Type: arg0, Indexed: false},
   807  			{Name: "arg1", Type: arg1, Indexed: true},
   808  		}},
   809  	}
   810  
   811  	abi, err := JSON(strings.NewReader(definition))
   812  	if err != nil {
   813  		t.Fatal(err)
   814  	}
   815  
   816  	if len(abi.Events) != len(expectedEvents) {
   817  		t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
   818  	}
   819  
   820  	for name, exp := range expectedEvents {
   821  		got, ok := abi.Events[name]
   822  		if !ok {
   823  			t.Errorf("could not found event %s", name)
   824  			continue
   825  		}
   826  		if got.Anonymous != exp.Anonymous {
   827  			t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
   828  		}
   829  		if len(got.Inputs) != len(exp.Args) {
   830  			t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
   831  			continue
   832  		}
   833  		for i, arg := range exp.Args {
   834  			if arg.Name != got.Inputs[i].Name {
   835  				t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
   836  			}
   837  			if arg.Indexed != got.Inputs[i].Indexed {
   838  				t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
   839  			}
   840  			if arg.Type.T != got.Inputs[i].Type.T {
   841  				t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
   842  			}
   843  		}
   844  	}
   845  }
   846  
   847  func TestMultiReturnWithStruct(t *testing.T) {
   848  	const definition = `[
   849  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   850  
   851  	abi, err := JSON(strings.NewReader(definition))
   852  	if err != nil {
   853  		t.Fatal(err)
   854  	}
   855  
   856  	// using buff to make the code readable
   857  	buff := new(bytes.Buffer)
   858  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   859  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   860  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   861  	stringOut := "hello"
   862  	buff.Write(common.RightPadBytes([]byte(stringOut), 32))
   863  
   864  	var inter struct {
   865  		Int    *big.Int
   866  		String string
   867  	}
   868  	err = abi.Unpack(&inter, "multi", buff.Bytes())
   869  	if err != nil {
   870  		t.Error(err)
   871  	}
   872  
   873  	if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
   874  		t.Error("expected Int to be 1 got", inter.Int)
   875  	}
   876  
   877  	if inter.String != stringOut {
   878  		t.Error("expected String to be", stringOut, "got", inter.String)
   879  	}
   880  
   881  	var reversed struct {
   882  		String string
   883  		Int    *big.Int
   884  	}
   885  
   886  	err = abi.Unpack(&reversed, "multi", buff.Bytes())
   887  	if err != nil {
   888  		t.Error(err)
   889  	}
   890  
   891  	if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
   892  		t.Error("expected Int to be 1 got", reversed.Int)
   893  	}
   894  
   895  	if reversed.String != stringOut {
   896  		t.Error("expected String to be", stringOut, "got", reversed.String)
   897  	}
   898  }
   899  
   900  func TestMultiReturnWithSlice(t *testing.T) {
   901  	const definition = `[
   902  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   903  
   904  	abi, err := JSON(strings.NewReader(definition))
   905  	if err != nil {
   906  		t.Fatal(err)
   907  	}
   908  
   909  	// using buff to make the code readable
   910  	buff := new(bytes.Buffer)
   911  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   912  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   913  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   914  	stringOut := "hello"
   915  	buff.Write(common.RightPadBytes([]byte(stringOut), 32))
   916  
   917  	var inter []interface{}
   918  	err = abi.Unpack(&inter, "multi", buff.Bytes())
   919  	if err != nil {
   920  		t.Error(err)
   921  	}
   922  
   923  	if len(inter) != 2 {
   924  		t.Fatal("expected 2 results got", len(inter))
   925  	}
   926  
   927  	if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 {
   928  		t.Error("expected index 0 to be 1 got", num)
   929  	}
   930  
   931  	if str, ok := inter[1].(string); !ok || str != stringOut {
   932  		t.Error("expected index 1 to be", stringOut, "got", str)
   933  	}
   934  }
   935  
   936  func TestMarshalArrays(t *testing.T) {
   937  	const definition = `[
   938  	{ "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   939  	{ "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] }
   940  	]`
   941  
   942  	abi, err := JSON(strings.NewReader(definition))
   943  	if err != nil {
   944  		t.Fatal(err)
   945  	}
   946  
   947  	output := common.LeftPadBytes([]byte{1}, 32)
   948  
   949  	var bytes10 [10]byte
   950  	err = abi.Unpack(&bytes10, "bytes32", output)
   951  	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
   952  		t.Error("expected error or bytes32 not be assignable to bytes10:", err)
   953  	}
   954  
   955  	var bytes32 [32]byte
   956  	err = abi.Unpack(&bytes32, "bytes32", output)
   957  	if err != nil {
   958  		t.Error("didn't expect error:", err)
   959  	}
   960  	if !bytes.Equal(bytes32[:], output) {
   961  		t.Error("expected bytes32[31] to be 1 got", bytes32[31])
   962  	}
   963  
   964  	type (
   965  		B10 [10]byte
   966  		B32 [32]byte
   967  	)
   968  
   969  	var b10 B10
   970  	err = abi.Unpack(&b10, "bytes32", output)
   971  	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
   972  		t.Error("expected error or bytes32 not be assignable to bytes10:", err)
   973  	}
   974  
   975  	var b32 B32
   976  	err = abi.Unpack(&b32, "bytes32", output)
   977  	if err != nil {
   978  		t.Error("didn't expect error:", err)
   979  	}
   980  	if !bytes.Equal(b32[:], output) {
   981  		t.Error("expected bytes32[31] to be 1 got", bytes32[31])
   982  	}
   983  
   984  	output[10] = 1
   985  	var shortAssignLong [32]byte
   986  	err = abi.Unpack(&shortAssignLong, "bytes10", output)
   987  	if err != nil {
   988  		t.Error("didn't expect error:", err)
   989  	}
   990  	if !bytes.Equal(output, shortAssignLong[:]) {
   991  		t.Errorf("expected %x to be %x", shortAssignLong, output)
   992  	}
   993  }
   994  
   995  func TestUnmarshal(t *testing.T) {
   996  	const definition = `[
   997  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   998  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   999  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  1000  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  1001  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  1002  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  1003  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  1004  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  1005  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  1006  
  1007  	abi, err := JSON(strings.NewReader(definition))
  1008  	if err != nil {
  1009  		t.Fatal(err)
  1010  	}
  1011  	buff := new(bytes.Buffer)
  1012  
  1013  	// marshal int
  1014  	var Int *big.Int
  1015  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  1016  	if err != nil {
  1017  		t.Error(err)
  1018  	}
  1019  
  1020  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  1021  		t.Error("expected Int to be 1 got", Int)
  1022  	}
  1023  
  1024  	// marshal bool
  1025  	var Bool bool
  1026  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  1027  	if err != nil {
  1028  		t.Error(err)
  1029  	}
  1030  
  1031  	if !Bool {
  1032  		t.Error("expected Bool to be true")
  1033  	}
  1034  
  1035  	// marshal dynamic bytes max length 32
  1036  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1037  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1038  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
  1039  	buff.Write(bytesOut)
  1040  
  1041  	var Bytes []byte
  1042  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  1043  	if err != nil {
  1044  		t.Error(err)
  1045  	}
  1046  
  1047  	if !bytes.Equal(Bytes, bytesOut) {
  1048  		t.Errorf("expected %x got %x", bytesOut, Bytes)
  1049  	}
  1050  
  1051  	// marshall dynamic bytes max length 64
  1052  	buff.Reset()
  1053  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1054  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  1055  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
  1056  	buff.Write(bytesOut)
  1057  
  1058  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  1059  	if err != nil {
  1060  		t.Error(err)
  1061  	}
  1062  
  1063  	if !bytes.Equal(Bytes, bytesOut) {
  1064  		t.Errorf("expected %x got %x", bytesOut, Bytes)
  1065  	}
  1066  
  1067  	// marshall dynamic bytes max length 63
  1068  	buff.Reset()
  1069  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1070  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  1071  	bytesOut = common.RightPadBytes([]byte("hello"), 63)
  1072  	buff.Write(bytesOut)
  1073  
  1074  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  1075  	if err != nil {
  1076  		t.Error(err)
  1077  	}
  1078  
  1079  	if !bytes.Equal(Bytes, bytesOut) {
  1080  		t.Errorf("expected %x got %x", bytesOut, Bytes)
  1081  	}
  1082  
  1083  	// marshal dynamic bytes output empty
  1084  	err = abi.Unpack(&Bytes, "bytes", nil)
  1085  	if err == nil {
  1086  		t.Error("expected error")
  1087  	}
  1088  
  1089  	// marshal dynamic bytes length 5
  1090  	buff.Reset()
  1091  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1092  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  1093  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
  1094  
  1095  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  1096  	if err != nil {
  1097  		t.Error(err)
  1098  	}
  1099  
  1100  	if !bytes.Equal(Bytes, []byte("hello")) {
  1101  		t.Errorf("expected %x got %x", bytesOut, Bytes)
  1102  	}
  1103  
  1104  	// marshal dynamic bytes length 5
  1105  	buff.Reset()
  1106  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
  1107  
  1108  	var hash common.Hash
  1109  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
  1110  	if err != nil {
  1111  		t.Error(err)
  1112  	}
  1113  
  1114  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  1115  	if hash != helloHash {
  1116  		t.Errorf("Expected %x to equal %x", hash, helloHash)
  1117  	}
  1118  
  1119  	// marshal error
  1120  	buff.Reset()
  1121  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1122  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  1123  	if err == nil {
  1124  		t.Error("expected error")
  1125  	}
  1126  
  1127  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  1128  	if err == nil {
  1129  		t.Error("expected error")
  1130  	}
  1131  
  1132  	// marshal mixed bytes
  1133  	buff.Reset()
  1134  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  1135  	fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")
  1136  	buff.Write(fixed)
  1137  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  1138  	bytesOut = common.RightPadBytes([]byte("hello"), 32)
  1139  	buff.Write(bytesOut)
  1140  
  1141  	var out []interface{}
  1142  	err = abi.Unpack(&out, "mixedBytes", buff.Bytes())
  1143  	if err != nil {
  1144  		t.Fatal("didn't expect error:", err)
  1145  	}
  1146  
  1147  	if !bytes.Equal(bytesOut, out[0].([]byte)) {
  1148  		t.Errorf("expected %x, got %x", bytesOut, out[0])
  1149  	}
  1150  
  1151  	if !bytes.Equal(fixed, out[1].([]byte)) {
  1152  		t.Errorf("expected %x, got %x", fixed, out[1])
  1153  	}
  1154  
  1155  	buff.Reset()
  1156  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  1157  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  1158  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  1159  	// marshal int array
  1160  	var intArray [3]*big.Int
  1161  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  1162  	if err != nil {
  1163  		t.Error(err)
  1164  	}
  1165  	var testAgainstIntArray [3]*big.Int
  1166  	testAgainstIntArray[0] = big.NewInt(1)
  1167  	testAgainstIntArray[1] = big.NewInt(2)
  1168  	testAgainstIntArray[2] = big.NewInt(3)
  1169  
  1170  	for i, Int := range intArray {
  1171  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
  1172  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  1173  		}
  1174  	}
  1175  	// marshal address slice
  1176  	buff.Reset()
  1177  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  1178  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  1179  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  1180  
  1181  	var outAddr []common.Address
  1182  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  1183  	if err != nil {
  1184  		t.Fatal("didn't expect error:", err)
  1185  	}
  1186  
  1187  	if len(outAddr) != 1 {
  1188  		t.Fatal("expected 1 item, got", len(outAddr))
  1189  	}
  1190  
  1191  	if outAddr[0] != (common.Address{1}) {
  1192  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  1193  	}
  1194  
  1195  	// marshal multiple address slice
  1196  	buff.Reset()
  1197  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  1198  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  1199  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  1200  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  1201  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  1202  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  1203  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  1204  
  1205  	var outAddrStruct struct {
  1206  		A []common.Address
  1207  		B []common.Address
  1208  	}
  1209  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  1210  	if err != nil {
  1211  		t.Fatal("didn't expect error:", err)
  1212  	}
  1213  
  1214  	if len(outAddrStruct.A) != 1 {
  1215  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  1216  	}
  1217  
  1218  	if outAddrStruct.A[0] != (common.Address{1}) {
  1219  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  1220  	}
  1221  
  1222  	if len(outAddrStruct.B) != 2 {
  1223  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  1224  	}
  1225  
  1226  	if outAddrStruct.B[0] != (common.Address{2}) {
  1227  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  1228  	}
  1229  	if outAddrStruct.B[1] != (common.Address{3}) {
  1230  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  1231  	}
  1232  
  1233  	// marshal invalid address slice
  1234  	buff.Reset()
  1235  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  1236  
  1237  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  1238  	if err == nil {
  1239  		t.Fatal("expected error:", err)
  1240  	}
  1241  }