github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/abi/pack_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package abi
    13  
    14  import (
    15  	"bytes"
    16  	"math"
    17  	"math/big"
    18  	"reflect"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/Sberex/go-sberex/common"
    23  )
    24  
    25  func TestPack(t *testing.T) {
    26  	for i, test := range []struct {
    27  		typ string
    28  
    29  		input  interface{}
    30  		output []byte
    31  	}{
    32  		{
    33  			"uint8",
    34  			uint8(2),
    35  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    36  		},
    37  		{
    38  			"uint8[]",
    39  			[]uint8{1, 2},
    40  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    41  		},
    42  		{
    43  			"uint16",
    44  			uint16(2),
    45  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    46  		},
    47  		{
    48  			"uint16[]",
    49  			[]uint16{1, 2},
    50  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    51  		},
    52  		{
    53  			"uint32",
    54  			uint32(2),
    55  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    56  		},
    57  		{
    58  			"uint32[]",
    59  			[]uint32{1, 2},
    60  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    61  		},
    62  		{
    63  			"uint64",
    64  			uint64(2),
    65  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    66  		},
    67  		{
    68  			"uint64[]",
    69  			[]uint64{1, 2},
    70  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    71  		},
    72  		{
    73  			"uint256",
    74  			big.NewInt(2),
    75  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    76  		},
    77  		{
    78  			"uint256[]",
    79  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
    80  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    81  		},
    82  		{
    83  			"int8",
    84  			int8(2),
    85  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    86  		},
    87  		{
    88  			"int8[]",
    89  			[]int8{1, 2},
    90  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    91  		},
    92  		{
    93  			"int16",
    94  			int16(2),
    95  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    96  		},
    97  		{
    98  			"int16[]",
    99  			[]int16{1, 2},
   100  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   101  		},
   102  		{
   103  			"int32",
   104  			int32(2),
   105  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   106  		},
   107  		{
   108  			"int32[]",
   109  			[]int32{1, 2},
   110  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   111  		},
   112  		{
   113  			"int64",
   114  			int64(2),
   115  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   116  		},
   117  		{
   118  			"int64[]",
   119  			[]int64{1, 2},
   120  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   121  		},
   122  		{
   123  			"int256",
   124  			big.NewInt(2),
   125  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   126  		},
   127  		{
   128  			"int256[]",
   129  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
   130  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   131  		},
   132  		{
   133  			"bytes1",
   134  			[1]byte{1},
   135  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   136  		},
   137  		{
   138  			"bytes2",
   139  			[2]byte{1},
   140  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   141  		},
   142  		{
   143  			"bytes3",
   144  			[3]byte{1},
   145  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   146  		},
   147  		{
   148  			"bytes4",
   149  			[4]byte{1},
   150  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   151  		},
   152  		{
   153  			"bytes5",
   154  			[5]byte{1},
   155  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   156  		},
   157  		{
   158  			"bytes6",
   159  			[6]byte{1},
   160  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   161  		},
   162  		{
   163  			"bytes7",
   164  			[7]byte{1},
   165  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   166  		},
   167  		{
   168  			"bytes8",
   169  			[8]byte{1},
   170  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   171  		},
   172  		{
   173  			"bytes9",
   174  			[9]byte{1},
   175  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   176  		},
   177  		{
   178  			"bytes10",
   179  			[10]byte{1},
   180  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   181  		},
   182  		{
   183  			"bytes11",
   184  			[11]byte{1},
   185  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   186  		},
   187  		{
   188  			"bytes12",
   189  			[12]byte{1},
   190  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   191  		},
   192  		{
   193  			"bytes13",
   194  			[13]byte{1},
   195  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   196  		},
   197  		{
   198  			"bytes14",
   199  			[14]byte{1},
   200  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   201  		},
   202  		{
   203  			"bytes15",
   204  			[15]byte{1},
   205  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   206  		},
   207  		{
   208  			"bytes16",
   209  			[16]byte{1},
   210  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   211  		},
   212  		{
   213  			"bytes17",
   214  			[17]byte{1},
   215  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   216  		},
   217  		{
   218  			"bytes18",
   219  			[18]byte{1},
   220  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   221  		},
   222  		{
   223  			"bytes19",
   224  			[19]byte{1},
   225  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   226  		},
   227  		{
   228  			"bytes20",
   229  			[20]byte{1},
   230  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   231  		},
   232  		{
   233  			"bytes21",
   234  			[21]byte{1},
   235  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   236  		},
   237  		{
   238  			"bytes22",
   239  			[22]byte{1},
   240  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   241  		},
   242  		{
   243  			"bytes23",
   244  			[23]byte{1},
   245  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   246  		},
   247  		{
   248  			"bytes24",
   249  			[24]byte{1},
   250  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   251  		},
   252  		{
   253  			"bytes24",
   254  			[24]byte{1},
   255  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   256  		},
   257  		{
   258  			"bytes25",
   259  			[25]byte{1},
   260  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   261  		},
   262  		{
   263  			"bytes26",
   264  			[26]byte{1},
   265  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   266  		},
   267  		{
   268  			"bytes27",
   269  			[27]byte{1},
   270  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   271  		},
   272  		{
   273  			"bytes28",
   274  			[28]byte{1},
   275  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   276  		},
   277  		{
   278  			"bytes29",
   279  			[29]byte{1},
   280  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   281  		},
   282  		{
   283  			"bytes30",
   284  			[30]byte{1},
   285  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   286  		},
   287  		{
   288  			"bytes31",
   289  			[31]byte{1},
   290  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   291  		},
   292  		{
   293  			"bytes32",
   294  			[32]byte{1},
   295  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   296  		},
   297  		{
   298  			"address[]",
   299  			[]common.Address{{1}, {2}},
   300  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"),
   301  		},
   302  		{
   303  			"bytes32[]",
   304  			[]common.Hash{{1}, {2}},
   305  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"),
   306  		},
   307  		{
   308  			"function",
   309  			[24]byte{1},
   310  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   311  		},
   312  		{
   313  			"string",
   314  			"foobar",
   315  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"),
   316  		},
   317  	} {
   318  		typ, err := NewType(test.typ)
   319  		if err != nil {
   320  			t.Fatalf("%v failed. Unexpected parse error: %v", i, err)
   321  		}
   322  
   323  		output, err := typ.pack(reflect.ValueOf(test.input))
   324  		if err != nil {
   325  			t.Fatalf("%v failed. Unexpected pack error: %v", i, err)
   326  		}
   327  
   328  		if !bytes.Equal(output, test.output) {
   329  			t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
   330  		}
   331  	}
   332  }
   333  
   334  func TestMethodPack(t *testing.T) {
   335  	abi, err := JSON(strings.NewReader(jsondata2))
   336  	if err != nil {
   337  		t.Fatal(err)
   338  	}
   339  
   340  	sig := abi.Methods["slice"].Id()
   341  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   342  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   343  
   344  	packed, err := abi.Pack("slice", []uint32{1, 2})
   345  	if err != nil {
   346  		t.Error(err)
   347  	}
   348  
   349  	if !bytes.Equal(packed, sig) {
   350  		t.Errorf("expected %x got %x", sig, packed)
   351  	}
   352  
   353  	var addrA, addrB = common.Address{1}, common.Address{2}
   354  	sig = abi.Methods["sliceAddress"].Id()
   355  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
   356  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   357  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   358  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   359  
   360  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
   361  	if err != nil {
   362  		t.Fatal(err)
   363  	}
   364  	if !bytes.Equal(packed, sig) {
   365  		t.Errorf("expected %x got %x", sig, packed)
   366  	}
   367  
   368  	var addrC, addrD = common.Address{3}, common.Address{4}
   369  	sig = abi.Methods["sliceMultiAddress"].Id()
   370  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
   371  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
   372  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   373  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   374  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   375  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   376  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   377  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   378  
   379  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   380  	if err != nil {
   381  		t.Fatal(err)
   382  	}
   383  	if !bytes.Equal(packed, sig) {
   384  		t.Errorf("expected %x got %x", sig, packed)
   385  	}
   386  
   387  	sig = abi.Methods["slice256"].Id()
   388  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   389  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   390  
   391  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   392  	if err != nil {
   393  		t.Error(err)
   394  	}
   395  
   396  	if !bytes.Equal(packed, sig) {
   397  		t.Errorf("expected %x got %x", sig, packed)
   398  	}
   399  }
   400  
   401  func TestPackNumber(t *testing.T) {
   402  	tests := []struct {
   403  		value  reflect.Value
   404  		packed []byte
   405  	}{
   406  		// Protocol limits
   407  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   408  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   409  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   410  
   411  		// Type corner cases
   412  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   413  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   414  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   415  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   416  
   417  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   418  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   419  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   420  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   421  
   422  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   423  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   424  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   425  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   426  	}
   427  	for i, tt := range tests {
   428  		packed := packNum(tt.value)
   429  		if !bytes.Equal(packed, tt.packed) {
   430  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   431  		}
   432  	}
   433  }