github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/pack_test.go (about)

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