github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/pack_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math"
    24  	"math/big"
    25  	"reflect"
    26  	"strconv"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  )
    32  
    33  // TestPack tests the general pack/unpack tests in packing_test.go
    34  func TestPack(t *testing.T) {
    35  	t.Parallel()
    36  	for i, test := range packUnpackTests {
    37  		i, test := i, test
    38  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    39  			t.Parallel()
    40  			encb, err := hex.DecodeString(test.packed)
    41  			if err != nil {
    42  				t.Fatalf("invalid hex %s: %v", test.packed, err)
    43  			}
    44  			inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def)
    45  			inAbi, err := JSON(strings.NewReader(inDef))
    46  			if err != nil {
    47  				t.Fatalf("invalid ABI definition %s, %v", inDef, err)
    48  			}
    49  			var packed []byte
    50  			packed, err = inAbi.Pack("method", test.unpacked)
    51  
    52  			if err != nil {
    53  				t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
    54  			}
    55  			if !reflect.DeepEqual(packed[4:], encb) {
    56  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestMethodPack(t *testing.T) {
    63  	t.Parallel()
    64  	abi, err := JSON(strings.NewReader(jsondata))
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	sig := abi.Methods["slice"].ID
    70  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
    71  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    72  
    73  	packed, err := abi.Pack("slice", []uint32{1, 2})
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	if !bytes.Equal(packed, sig) {
    79  		t.Errorf("expected %x got %x", sig, packed)
    80  	}
    81  
    82  	var addrA, addrB = common.Address{1}, common.Address{2}
    83  	sig = abi.Methods["sliceAddress"].ID
    84  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
    85  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    86  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
    87  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
    88  
    89  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if !bytes.Equal(packed, sig) {
    94  		t.Errorf("expected %x got %x", sig, packed)
    95  	}
    96  
    97  	var addrC, addrD = common.Address{3}, common.Address{4}
    98  	sig = abi.Methods["sliceMultiAddress"].ID
    99  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
   100  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
   101  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   102  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   103  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   104  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   105  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   106  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   107  
   108  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if !bytes.Equal(packed, sig) {
   113  		t.Errorf("expected %x got %x", sig, packed)
   114  	}
   115  
   116  	sig = abi.Methods["slice256"].ID
   117  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   118  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   119  
   120  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   121  	if err != nil {
   122  		t.Error(err)
   123  	}
   124  
   125  	if !bytes.Equal(packed, sig) {
   126  		t.Errorf("expected %x got %x", sig, packed)
   127  	}
   128  
   129  	a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
   130  	sig = abi.Methods["nestedArray"].ID
   131  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   132  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   133  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   134  	sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
   135  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   136  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   137  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   138  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   139  	packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  	if !bytes.Equal(packed, sig) {
   144  		t.Errorf("expected %x got %x", sig, packed)
   145  	}
   146  
   147  	sig = abi.Methods["nestedArray2"].ID
   148  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   149  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   150  	sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
   151  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   152  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   153  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   154  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   155  	packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	if !bytes.Equal(packed, sig) {
   160  		t.Errorf("expected %x got %x", sig, packed)
   161  	}
   162  
   163  	sig = abi.Methods["nestedSlice"].ID
   164  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   165  	sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
   166  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   167  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   168  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   169  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   170  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   171  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   172  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   173  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   174  	packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	if !bytes.Equal(packed, sig) {
   179  		t.Errorf("expected %x got %x", sig, packed)
   180  	}
   181  }
   182  
   183  func TestPackNumber(t *testing.T) {
   184  	t.Parallel()
   185  	tests := []struct {
   186  		value  reflect.Value
   187  		packed []byte
   188  	}{
   189  		// Protocol limits
   190  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   191  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   192  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   193  
   194  		// Type corner cases
   195  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   196  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   197  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   198  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   199  
   200  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   201  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   202  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   203  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   204  
   205  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   206  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   207  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   208  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   209  	}
   210  	for i, tt := range tests {
   211  		packed := packNum(tt.value)
   212  		if !bytes.Equal(packed, tt.packed) {
   213  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   214  		}
   215  	}
   216  }