github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/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  	for i, test := range packUnpackTests {
    36  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    37  			encb, err := hex.DecodeString(test.packed)
    38  			if err != nil {
    39  				t.Fatalf("invalid hex %s: %v", test.packed, err)
    40  			}
    41  			inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def)
    42  			inAbi, err := JSON(strings.NewReader(inDef))
    43  			if err != nil {
    44  				t.Fatalf("invalid ABI definition %s, %v", inDef, err)
    45  			}
    46  			var packed []byte
    47  			packed, err = inAbi.Pack("method", test.unpacked)
    48  
    49  			if err != nil {
    50  				t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
    51  			}
    52  			if !reflect.DeepEqual(packed[4:], encb) {
    53  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestMethodPack(t *testing.T) {
    60  	abi, err := JSON(strings.NewReader(jsondata))
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	sig := abi.Methods["slice"].ID
    66  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
    67  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    68  
    69  	packed, err := abi.Pack("slice", []uint32{1, 2})
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  
    74  	if !bytes.Equal(packed, sig) {
    75  		t.Errorf("expected %x got %x", sig, packed)
    76  	}
    77  
    78  	var addrA, addrB = common.Address{1}, common.Address{2}
    79  	sig = abi.Methods["sliceAddress"].ID
    80  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
    81  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    82  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
    83  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
    84  
    85  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	if !bytes.Equal(packed, sig) {
    90  		t.Errorf("expected %x got %x", sig, packed)
    91  	}
    92  
    93  	var addrC, addrD = common.Address{3}, common.Address{4}
    94  	sig = abi.Methods["sliceMultiAddress"].ID
    95  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
    96  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
    97  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    98  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
    99  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   100  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   101  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   102  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   103  
   104  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	if !bytes.Equal(packed, sig) {
   109  		t.Errorf("expected %x got %x", sig, packed)
   110  	}
   111  
   112  	sig = abi.Methods["slice256"].ID
   113  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   114  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   115  
   116  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   117  	if err != nil {
   118  		t.Error(err)
   119  	}
   120  
   121  	if !bytes.Equal(packed, sig) {
   122  		t.Errorf("expected %x got %x", sig, packed)
   123  	}
   124  
   125  	a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
   126  	sig = abi.Methods["nestedArray"].ID
   127  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   128  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   129  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   130  	sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
   131  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   132  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   133  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   134  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   135  	packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	if !bytes.Equal(packed, sig) {
   140  		t.Errorf("expected %x got %x", sig, packed)
   141  	}
   142  
   143  	sig = abi.Methods["nestedArray2"].ID
   144  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   145  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   146  	sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
   147  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   148  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   149  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   150  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   151  	packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
   152  	if err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	if !bytes.Equal(packed, sig) {
   156  		t.Errorf("expected %x got %x", sig, packed)
   157  	}
   158  
   159  	sig = abi.Methods["nestedSlice"].ID
   160  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   161  	sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
   162  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   163  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   164  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   165  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   166  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   167  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   168  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   169  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   170  	packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	if !bytes.Equal(packed, sig) {
   175  		t.Errorf("expected %x got %x", sig, packed)
   176  	}
   177  }
   178  
   179  func TestPackNumber(t *testing.T) {
   180  	tests := []struct {
   181  		value  reflect.Value
   182  		packed []byte
   183  	}{
   184  		// Protocol limits
   185  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   186  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   187  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   188  
   189  		// Type corner cases
   190  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   191  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   192  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   193  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   194  
   195  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   196  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   197  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   198  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   199  
   200  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   201  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   202  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   203  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   204  	}
   205  	for i, tt := range tests {
   206  		packed := packNum(tt.value)
   207  		if !bytes.Equal(packed, tt.packed) {
   208  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   209  		}
   210  	}
   211  }