github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/accounts/abi/unpack_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:26</date>
    10  //</624342584260562944>
    11  
    12  
    13  package abi
    14  
    15  import (
    16  	"bytes"
    17  	"encoding/hex"
    18  	"fmt"
    19  	"math/big"
    20  	"reflect"
    21  	"strconv"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  type unpackTest struct {
    30  def  string      //ABI定义JSON
    31  enc  string      //返回数据
    32  want interface{} //预期产量
    33  err  string      //如果需要,则为空或错误
    34  }
    35  
    36  func (test unpackTest) checkError(err error) error {
    37  	if err != nil {
    38  		if len(test.err) == 0 {
    39  			return fmt.Errorf("expected no err but got: %v", err)
    40  		} else if err.Error() != test.err {
    41  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    42  		}
    43  	} else if len(test.err) > 0 {
    44  		return fmt.Errorf("expected err: %v but got none", test.err)
    45  	}
    46  	return nil
    47  }
    48  
    49  var unpackTests = []unpackTest{
    50  	{
    51  		def:  `[{ "type": "bool" }]`,
    52  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    53  		want: true,
    54  	},
    55  	{
    56  		def:  `[{ "type": "bool" }]`,
    57  		enc:  "0000000000000000000000000000000000000000000000000000000000000000",
    58  		want: false,
    59  	},
    60  	{
    61  		def:  `[{ "type": "bool" }]`,
    62  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    63  		want: false,
    64  		err:  "abi: improperly encoded boolean value",
    65  	},
    66  	{
    67  		def:  `[{ "type": "bool" }]`,
    68  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    69  		want: false,
    70  		err:  "abi: improperly encoded boolean value",
    71  	},
    72  	{
    73  		def:  `[{"type": "uint32"}]`,
    74  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    75  		want: uint32(1),
    76  	},
    77  	{
    78  		def:  `[{"type": "uint32"}]`,
    79  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    80  		want: uint16(0),
    81  		err:  "abi: cannot unmarshal uint32 in to uint16",
    82  	},
    83  	{
    84  		def:  `[{"type": "uint17"}]`,
    85  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    86  		want: uint16(0),
    87  		err:  "abi: cannot unmarshal *big.Int in to uint16",
    88  	},
    89  	{
    90  		def:  `[{"type": "uint17"}]`,
    91  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    92  		want: big.NewInt(1),
    93  	},
    94  	{
    95  		def:  `[{"type": "int32"}]`,
    96  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    97  		want: int32(1),
    98  	},
    99  	{
   100  		def:  `[{"type": "int32"}]`,
   101  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   102  		want: int16(0),
   103  		err:  "abi: cannot unmarshal int32 in to int16",
   104  	},
   105  	{
   106  		def:  `[{"type": "int17"}]`,
   107  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   108  		want: int16(0),
   109  		err:  "abi: cannot unmarshal *big.Int in to int16",
   110  	},
   111  	{
   112  		def:  `[{"type": "int17"}]`,
   113  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   114  		want: big.NewInt(1),
   115  	},
   116  	{
   117  		def:  `[{"type": "address"}]`,
   118  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   119  		want: common.Address{1},
   120  	},
   121  	{
   122  		def:  `[{"type": "bytes32"}]`,
   123  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   124  		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},
   125  	},
   126  	{
   127  		def:  `[{"type": "bytes"}]`,
   128  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   129  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   130  	},
   131  	{
   132  		def:  `[{"type": "bytes"}]`,
   133  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   134  		want: [32]byte{},
   135  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   136  	},
   137  	{
   138  		def:  `[{"type": "bytes32"}]`,
   139  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   140  		want: []byte(nil),
   141  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   142  	},
   143  	{
   144  		def:  `[{"type": "bytes32"}]`,
   145  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   146  		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},
   147  	},
   148  	{
   149  		def:  `[{"type": "function"}]`,
   150  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   151  		want: [24]byte{1},
   152  	},
   153  //片
   154  	{
   155  		def:  `[{"type": "uint8[]"}]`,
   156  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   157  		want: []uint8{1, 2},
   158  	},
   159  	{
   160  		def:  `[{"type": "uint8[2]"}]`,
   161  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   162  		want: [2]uint8{1, 2},
   163  	},
   164  //多维,如果这些通过,则不需要长度前缀的所有类型都应通过
   165  	{
   166  		def:  `[{"type": "uint8[][]"}]`,
   167  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   168  		want: [][]uint8{{1, 2}, {1, 2}},
   169  	},
   170  	{
   171  		def:  `[{"type": "uint8[2][2]"}]`,
   172  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   173  		want: [2][2]uint8{{1, 2}, {1, 2}},
   174  	},
   175  	{
   176  		def:  `[{"type": "uint8[][2]"}]`,
   177  		enc:  "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   178  		want: [2][]uint8{{1}, {1}},
   179  	},
   180  	{
   181  		def:  `[{"type": "uint8[2][]"}]`,
   182  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   183  		want: [][2]uint8{{1, 2}},
   184  	},
   185  	{
   186  		def:  `[{"type": "uint16[]"}]`,
   187  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   188  		want: []uint16{1, 2},
   189  	},
   190  	{
   191  		def:  `[{"type": "uint16[2]"}]`,
   192  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   193  		want: [2]uint16{1, 2},
   194  	},
   195  	{
   196  		def:  `[{"type": "uint32[]"}]`,
   197  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   198  		want: []uint32{1, 2},
   199  	},
   200  	{
   201  		def:  `[{"type": "uint32[2]"}]`,
   202  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   203  		want: [2]uint32{1, 2},
   204  	},
   205  	{
   206  		def:  `[{"type": "uint32[2][3][4]"}]`,
   207  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
   208  		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}}},
   209  	},
   210  	{
   211  		def:  `[{"type": "uint64[]"}]`,
   212  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   213  		want: []uint64{1, 2},
   214  	},
   215  	{
   216  		def:  `[{"type": "uint64[2]"}]`,
   217  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   218  		want: [2]uint64{1, 2},
   219  	},
   220  	{
   221  		def:  `[{"type": "uint256[]"}]`,
   222  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   223  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   224  	},
   225  	{
   226  		def:  `[{"type": "uint256[3]"}]`,
   227  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   228  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   229  	},
   230  	{
   231  		def:  `[{"type": "int8[]"}]`,
   232  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   233  		want: []int8{1, 2},
   234  	},
   235  	{
   236  		def:  `[{"type": "int8[2]"}]`,
   237  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   238  		want: [2]int8{1, 2},
   239  	},
   240  	{
   241  		def:  `[{"type": "int16[]"}]`,
   242  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   243  		want: []int16{1, 2},
   244  	},
   245  	{
   246  		def:  `[{"type": "int16[2]"}]`,
   247  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   248  		want: [2]int16{1, 2},
   249  	},
   250  	{
   251  		def:  `[{"type": "int32[]"}]`,
   252  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   253  		want: []int32{1, 2},
   254  	},
   255  	{
   256  		def:  `[{"type": "int32[2]"}]`,
   257  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   258  		want: [2]int32{1, 2},
   259  	},
   260  	{
   261  		def:  `[{"type": "int64[]"}]`,
   262  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   263  		want: []int64{1, 2},
   264  	},
   265  	{
   266  		def:  `[{"type": "int64[2]"}]`,
   267  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   268  		want: [2]int64{1, 2},
   269  	},
   270  	{
   271  		def:  `[{"type": "int256[]"}]`,
   272  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   273  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   274  	},
   275  	{
   276  		def:  `[{"type": "int256[3]"}]`,
   277  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   278  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   279  	},
   280  //结构输出
   281  	{
   282  		def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
   283  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   284  		want: struct {
   285  			Int1 *big.Int
   286  			Int2 *big.Int
   287  		}{big.NewInt(1), big.NewInt(2)},
   288  	},
   289  	{
   290  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   291  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   292  		want: struct {
   293  			Int1 *big.Int
   294  			Int2 *big.Int
   295  		}{},
   296  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   297  	},
   298  	{
   299  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   300  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   301  		want: struct {
   302  			Int1 *big.Int
   303  			Int2 *big.Int
   304  		}{},
   305  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   306  	},
   307  	{
   308  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   309  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   310  		want: struct {
   311  			Int1 *big.Int
   312  			Int2 *big.Int
   313  		}{},
   314  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   315  	},
   316  	{
   317  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   318  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   319  		want: struct {
   320  			Int1 *big.Int
   321  			Int2 *big.Int
   322  		}{},
   323  		err: "abi: purely underscored output cannot unpack to struct",
   324  	},
   325  }
   326  
   327  func TestUnpack(t *testing.T) {
   328  	for i, test := range unpackTests {
   329  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   330  			def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   331  			abi, err := JSON(strings.NewReader(def))
   332  			if err != nil {
   333  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   334  			}
   335  			encb, err := hex.DecodeString(test.enc)
   336  			if err != nil {
   337  				t.Fatalf("invalid hex: %s" + test.enc)
   338  			}
   339  			outptr := reflect.New(reflect.TypeOf(test.want))
   340  			err = abi.Unpack(outptr.Interface(), "method", encb)
   341  			if err := test.checkError(err); err != nil {
   342  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   343  				return
   344  			}
   345  			out := outptr.Elem().Interface()
   346  			if !reflect.DeepEqual(test.want, out) {
   347  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   348  			}
   349  		})
   350  	}
   351  }
   352  
   353  type methodMultiOutput struct {
   354  	Int    *big.Int
   355  	String string
   356  }
   357  
   358  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   359  	const definition = `[
   360  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   361  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   362  
   363  	abi, err := JSON(strings.NewReader(definition))
   364  	require.NoError(err)
   365  //使用buff使代码可读
   366  	buff := new(bytes.Buffer)
   367  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   368  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   369  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   370  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   371  	return abi, buff.Bytes(), expected
   372  }
   373  
   374  func TestMethodMultiReturn(t *testing.T) {
   375  	type reversed struct {
   376  		String string
   377  		Int    *big.Int
   378  	}
   379  
   380  	abi, data, expected := methodMultiReturn(require.New(t))
   381  	bigint := new(big.Int)
   382  	var testCases = []struct {
   383  		dest     interface{}
   384  		expected interface{}
   385  		error    string
   386  		name     string
   387  	}{{
   388  		&methodMultiOutput{},
   389  		&expected,
   390  		"",
   391  		"Can unpack into structure",
   392  	}, {
   393  		&reversed{},
   394  		&reversed{expected.String, expected.Int},
   395  		"",
   396  		"Can unpack into reversed structure",
   397  	}, {
   398  		&[]interface{}{&bigint, new(string)},
   399  		&[]interface{}{&expected.Int, &expected.String},
   400  		"",
   401  		"Can unpack into a slice",
   402  	}, {
   403  		&[2]interface{}{&bigint, new(string)},
   404  		&[2]interface{}{&expected.Int, &expected.String},
   405  		"",
   406  		"Can unpack into an array",
   407  	}, {
   408  		&[]interface{}{new(int), new(int)},
   409  		&[]interface{}{&expected.Int, &expected.String},
   410  		"abi: cannot unmarshal *big.Int in to int",
   411  		"Can not unpack into a slice with wrong types",
   412  	}, {
   413  		&[]interface{}{new(int)},
   414  		&[]interface{}{},
   415  		"abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
   416  		"Can not unpack into a slice with wrong types",
   417  	}}
   418  	for _, tc := range testCases {
   419  		tc := tc
   420  		t.Run(tc.name, func(t *testing.T) {
   421  			require := require.New(t)
   422  			err := abi.Unpack(tc.dest, "multi", data)
   423  			if tc.error == "" {
   424  				require.Nil(err, "Should be able to unpack method outputs.")
   425  				require.Equal(tc.expected, tc.dest)
   426  			} else {
   427  				require.EqualError(err, tc.error)
   428  			}
   429  		})
   430  	}
   431  }
   432  
   433  func TestMultiReturnWithArray(t *testing.T) {
   434  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   435  	abi, err := JSON(strings.NewReader(definition))
   436  	if err != nil {
   437  		t.Fatal(err)
   438  	}
   439  	buff := new(bytes.Buffer)
   440  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   441  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   442  
   443  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   444  	ret2, ret2Exp := new(uint64), uint64(8)
   445  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   446  		t.Fatal(err)
   447  	}
   448  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   449  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   450  	}
   451  	if *ret2 != ret2Exp {
   452  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   453  	}
   454  }
   455  
   456  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   457  //类似于TestMultiReturnWithArray,但考虑到特殊情况:
   458  //嵌套静态数组的值也将计入大小,以及后面的任何元素
   459  //在这种嵌套数组参数之后,应该用正确的偏移量读取,
   460  //这样它就不会从前面的数组参数中读取内容。
   461  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   462  	abi, err := JSON(strings.NewReader(definition))
   463  	if err != nil {
   464  		t.Fatal(err)
   465  	}
   466  	buff := new(bytes.Buffer)
   467  //构造测试数组,每个3个char元素与61个“0”chars连接,
   468  //从((3+61)*0.5)=32字节的数组元素。
   469  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   470  "", //空,以便将61个字符分隔符也应用于第一个元素。
   471  		"111", "112", "113", "121", "122", "123",
   472  		"211", "212", "213", "221", "222", "223",
   473  		"311", "312", "313", "321", "322", "323",
   474  		"411", "412", "413", "421", "422", "423",
   475  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   476  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   477  
   478  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   479  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   480  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   481  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   482  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   483  	}
   484  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   485  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   486  		t.Fatal(err)
   487  	}
   488  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   489  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   490  	}
   491  	if *ret2 != ret2Exp {
   492  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   493  	}
   494  }
   495  
   496  func TestUnmarshal(t *testing.T) {
   497  	const definition = `[
   498  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   499  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   500  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   501  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   502  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   503  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   504  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   505  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   506  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   507  
   508  	abi, err := JSON(strings.NewReader(definition))
   509  	if err != nil {
   510  		t.Fatal(err)
   511  	}
   512  	buff := new(bytes.Buffer)
   513  
   514  //Marshall混合字节(MixedBytes)
   515  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   516  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   517  	mixedBytes := []interface{}{&p0, &p1}
   518  
   519  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   520  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   521  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   522  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   523  
   524  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   525  	if err != nil {
   526  		t.Error(err)
   527  	} else {
   528  		if !bytes.Equal(p0, p0Exp) {
   529  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   530  		}
   531  
   532  		if !bytes.Equal(p1[:], p1Exp) {
   533  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   534  		}
   535  	}
   536  
   537  //元帅INT
   538  	var Int *big.Int
   539  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   540  	if err != nil {
   541  		t.Error(err)
   542  	}
   543  
   544  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   545  		t.Error("expected Int to be 1 got", Int)
   546  	}
   547  
   548  //元帅
   549  	var Bool bool
   550  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   551  	if err != nil {
   552  		t.Error(err)
   553  	}
   554  
   555  	if !Bool {
   556  		t.Error("expected Bool to be true")
   557  	}
   558  
   559  //封送动态字节最大长度32
   560  	buff.Reset()
   561  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   562  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   563  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   564  	buff.Write(bytesOut)
   565  
   566  	var Bytes []byte
   567  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   568  	if err != nil {
   569  		t.Error(err)
   570  	}
   571  
   572  	if !bytes.Equal(Bytes, bytesOut) {
   573  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   574  	}
   575  
   576  //马歇尔动态字节最大长度64
   577  	buff.Reset()
   578  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   579  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   580  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   581  	buff.Write(bytesOut)
   582  
   583  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   584  	if err != nil {
   585  		t.Error(err)
   586  	}
   587  
   588  	if !bytes.Equal(Bytes, bytesOut) {
   589  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   590  	}
   591  
   592  //马歇尔动态字节最大长度64
   593  	buff.Reset()
   594  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   595  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   596  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   597  	buff.Write(bytesOut)
   598  
   599  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   600  	if err != nil {
   601  		t.Error(err)
   602  	}
   603  
   604  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   605  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   606  	}
   607  
   608  //封送动态字节输出为空
   609  	err = abi.Unpack(&Bytes, "bytes", nil)
   610  	if err == nil {
   611  		t.Error("expected error")
   612  	}
   613  
   614  //封送动态字节长度5
   615  	buff.Reset()
   616  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   617  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   618  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   619  
   620  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   621  	if err != nil {
   622  		t.Error(err)
   623  	}
   624  
   625  	if !bytes.Equal(Bytes, []byte("hello")) {
   626  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   627  	}
   628  
   629  //封送动态字节长度5
   630  	buff.Reset()
   631  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   632  
   633  	var hash common.Hash
   634  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   635  	if err != nil {
   636  		t.Error(err)
   637  	}
   638  
   639  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   640  	if hash != helloHash {
   641  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   642  	}
   643  
   644  //元帅错误
   645  	buff.Reset()
   646  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   647  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   648  	if err == nil {
   649  		t.Error("expected error")
   650  	}
   651  
   652  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   653  	if err == nil {
   654  		t.Error("expected error")
   655  	}
   656  
   657  	buff.Reset()
   658  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   659  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   660  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   661  //marshal int数组
   662  	var intArray [3]*big.Int
   663  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   664  	if err != nil {
   665  		t.Error(err)
   666  	}
   667  	var testAgainstIntArray [3]*big.Int
   668  	testAgainstIntArray[0] = big.NewInt(1)
   669  	testAgainstIntArray[1] = big.NewInt(2)
   670  	testAgainstIntArray[2] = big.NewInt(3)
   671  
   672  	for i, Int := range intArray {
   673  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   674  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   675  		}
   676  	}
   677  //封送地址切片
   678  	buff.Reset()
   679  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) //抵消
   680  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) //大小
   681  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   682  
   683  	var outAddr []common.Address
   684  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   685  	if err != nil {
   686  		t.Fatal("didn't expect error:", err)
   687  	}
   688  
   689  	if len(outAddr) != 1 {
   690  		t.Fatal("expected 1 item, got", len(outAddr))
   691  	}
   692  
   693  	if outAddr[0] != (common.Address{1}) {
   694  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   695  	}
   696  
   697  //封送多个地址片
   698  	buff.Reset()
   699  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) //抵消
   700  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) //抵消
   701  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) //大小
   702  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   703  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) //大小
   704  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   705  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   706  
   707  	var outAddrStruct struct {
   708  		A []common.Address
   709  		B []common.Address
   710  	}
   711  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   712  	if err != nil {
   713  		t.Fatal("didn't expect error:", err)
   714  	}
   715  
   716  	if len(outAddrStruct.A) != 1 {
   717  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   718  	}
   719  
   720  	if outAddrStruct.A[0] != (common.Address{1}) {
   721  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   722  	}
   723  
   724  	if len(outAddrStruct.B) != 2 {
   725  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   726  	}
   727  
   728  	if outAddrStruct.B[0] != (common.Address{2}) {
   729  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   730  	}
   731  	if outAddrStruct.B[1] != (common.Address{3}) {
   732  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   733  	}
   734  
   735  //封送无效的地址切片
   736  	buff.Reset()
   737  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   738  
   739  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   740  	if err == nil {
   741  		t.Fatal("expected error:", err)
   742  	}
   743  }
   744  
   745  func TestOOMMaliciousInput(t *testing.T) {
   746  	oomTests := []unpackTest{
   747  		{
   748  			def: `[{"type": "uint8[]"}]`,
   749  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   750  "0000000000000000000000000000000000000000000000000000000000000003" + //努姆埃勒姆
   751  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   752  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   753  		},
   754  { //长度大于64位
   755  			def: `[{"type": "uint8[]"}]`,
   756  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   757  "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + //努姆埃勒姆
   758  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   759  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   760  		},
   761  { //偏移量非常大(超过64位)
   762  			def: `[{"type": "uint8[]"}]`,
   763  enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + //抵消
   764  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   765  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   766  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   767  		},
   768  { //偏移量非常大(低于64位)
   769  			def: `[{"type": "uint8[]"}]`,
   770  enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + //抵消
   771  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   772  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   773  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   774  		},
   775  { //负偏移量(64位)
   776  			def: `[{"type": "uint8[]"}]`,
   777  enc: "000000000000000000000000000000000000000000000000f000000000000020" + //抵消
   778  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   779  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   780  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   781  		},
   782  
   783  { //负长度
   784  			def: `[{"type": "uint8[]"}]`,
   785  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   786  "000000000000000000000000000000000000000000000000f000000000000002" + //努姆埃勒姆
   787  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   788  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   789  		},
   790  { //非常大的长度
   791  			def: `[{"type": "uint8[]"}]`,
   792  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   793  "0000000000000000000000000000000000000000000000007fffffffff000002" + //努姆埃勒姆
   794  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   795  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   796  		},
   797  	}
   798  	for i, test := range oomTests {
   799  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   800  		abi, err := JSON(strings.NewReader(def))
   801  		if err != nil {
   802  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   803  		}
   804  		encb, err := hex.DecodeString(test.enc)
   805  		if err != nil {
   806  			t.Fatalf("invalid hex: %s" + test.enc)
   807  		}
   808  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   809  		if err == nil {
   810  			t.Fatalf("Expected error on malicious input, test %d", i)
   811  		}
   812  	}
   813  }
   814