github.com/googgoog/go-ethereum@v1.9.7/signer/fourbyte/abi_test.go (about)

     1  // Copyright 2019 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 fourbyte
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/accounts/abi"
    26  	"github.com/ethereum/go-ethereum/common"
    27  )
    28  
    29  func verify(t *testing.T, jsondata, calldata string, exp []interface{}) {
    30  	abispec, err := abi.JSON(strings.NewReader(jsondata))
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	cd := common.Hex2Bytes(calldata)
    35  	sigdata, argdata := cd[:4], cd[4:]
    36  	method, err := abispec.MethodById(sigdata)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	data, err := method.Inputs.UnpackValues(argdata)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	if len(data) != len(exp) {
    45  		t.Fatalf("Mismatched length, expected %d, got %d", len(exp), len(data))
    46  	}
    47  	for i, elem := range data {
    48  		if !reflect.DeepEqual(elem, exp[i]) {
    49  			t.Fatalf("Unpack error, arg %d, got %v, want %v", i, elem, exp[i])
    50  		}
    51  	}
    52  }
    53  
    54  func TestNewUnpacker(t *testing.T) {
    55  	type unpackTest struct {
    56  		jsondata string
    57  		calldata string
    58  		exp      []interface{}
    59  	}
    60  	testcases := []unpackTest{
    61  		{ // https://solidity.readthedocs.io/en/develop/abi-spec.html#use-of-dynamic-types
    62  			`[{"type":"function","name":"f", "inputs":[{"type":"uint256"},{"type":"uint32[]"},{"type":"bytes10"},{"type":"bytes"}]}]`,
    63  			// 0x123, [0x456, 0x789], "1234567890", "Hello, world!"
    64  			"8be65246" + "00000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000080313233343536373839300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004560000000000000000000000000000000000000000000000000000000000000789000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000",
    65  			[]interface{}{
    66  				big.NewInt(0x123),
    67  				[]uint32{0x456, 0x789},
    68  				[10]byte{49, 50, 51, 52, 53, 54, 55, 56, 57, 48},
    69  				common.Hex2Bytes("48656c6c6f2c20776f726c6421"),
    70  			},
    71  		}, { // https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples
    72  			`[{"type":"function","name":"sam","inputs":[{"type":"bytes"},{"type":"bool"},{"type":"uint256[]"}]}]`,
    73  			//  "dave", true and [1,2,3]
    74  			"a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
    75  			[]interface{}{
    76  				[]byte{0x64, 0x61, 0x76, 0x65},
    77  				true,
    78  				[]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
    79  			},
    80  		}, {
    81  			`[{"type":"function","name":"send","inputs":[{"type":"uint256"}]}]`,
    82  			"a52c101e0000000000000000000000000000000000000000000000000000000000000012",
    83  			[]interface{}{big.NewInt(0x12)},
    84  		}, {
    85  			`[{"type":"function","name":"compareAndApprove","inputs":[{"type":"address"},{"type":"uint256"},{"type":"uint256"}]}]`,
    86  			"751e107900000000000000000000000000000133700000deadbeef00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
    87  			[]interface{}{
    88  				common.HexToAddress("0x00000133700000deadbeef000000000000000000"),
    89  				new(big.Int).SetBytes([]byte{0x00}),
    90  				big.NewInt(0x1),
    91  			},
    92  		},
    93  	}
    94  	for _, c := range testcases {
    95  		verify(t, c.jsondata, c.calldata, c.exp)
    96  	}
    97  }
    98  
    99  func TestCalldataDecoding(t *testing.T) {
   100  	// send(uint256)                              : a52c101e
   101  	// compareAndApprove(address,uint256,uint256) : 751e1079
   102  	// issue(address[],uint256)                   : 42958b54
   103  	jsondata := `
   104  [
   105  	{"type":"function","name":"send","inputs":[{"name":"a","type":"uint256"}]},
   106  	{"type":"function","name":"compareAndApprove","inputs":[{"name":"a","type":"address"},{"name":"a","type":"uint256"},{"name":"a","type":"uint256"}]},
   107  	{"type":"function","name":"issue","inputs":[{"name":"a","type":"address[]"},{"name":"a","type":"uint256"}]},
   108  	{"type":"function","name":"sam","inputs":[{"name":"a","type":"bytes"},{"name":"a","type":"bool"},{"name":"a","type":"uint256[]"}]}
   109  ]`
   110  	// Expected failures
   111  	for i, hexdata := range []string{
   112  		"a52c101e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
   113  		"a52c101e000000000000000000000000000000000000000000000000000000000000001200",
   114  		"a52c101e00000000000000000000000000000000000000000000000000000000000000",
   115  		"a52c101e",
   116  		"a52c10",
   117  		"",
   118  		// Too short
   119  		"751e10790000000000000000000000000000000000000000000000000000000000000012",
   120  		"751e1079FFffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   121  		// Not valid multiple of 32
   122  		"deadbeef00000000000000000000000000000000000000000000000000000000000000",
   123  		// Too short 'issue'
   124  		"42958b5400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
   125  		// Too short compareAndApprove
   126  		"a52c101e00ff0000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
   127  		// From https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
   128  		// contains a bool with illegal values
   129  		"a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   130  	} {
   131  		_, err := parseCallData(common.Hex2Bytes(hexdata), jsondata)
   132  		if err == nil {
   133  			t.Errorf("test %d: expected decoding to fail: %s", i, hexdata)
   134  		}
   135  	}
   136  	// Expected success
   137  	for i, hexdata := range []string{
   138  		// From https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
   139  		"a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   140  		"a52c101e0000000000000000000000000000000000000000000000000000000000000012",
   141  		"a52c101eFFffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   142  		"751e1079000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   143  		"42958b54" +
   144  			// start of dynamic type
   145  			"0000000000000000000000000000000000000000000000000000000000000040" +
   146  			// uint256
   147  			"0000000000000000000000000000000000000000000000000000000000000001" +
   148  			// length of  array
   149  			"0000000000000000000000000000000000000000000000000000000000000002" +
   150  			// array values
   151  			"000000000000000000000000000000000000000000000000000000000000dead" +
   152  			"000000000000000000000000000000000000000000000000000000000000beef",
   153  	} {
   154  		_, err := parseCallData(common.Hex2Bytes(hexdata), jsondata)
   155  		if err != nil {
   156  			t.Errorf("test %d: unexpected failure on input %s:\n %v (%d bytes) ", i, hexdata, err, len(common.Hex2Bytes(hexdata)))
   157  		}
   158  	}
   159  }
   160  
   161  func TestMaliciousABIStrings(t *testing.T) {
   162  	tests := []string{
   163  		"func(uint256,uint256,[]uint256)",
   164  		"func(uint256,uint256,uint256,)",
   165  		"func(,uint256,uint256,uint256)",
   166  	}
   167  	data := common.Hex2Bytes("4401a6e40000000000000000000000000000000000000000000000000000000000000012")
   168  	for i, tt := range tests {
   169  		_, err := verifySelector(tt, data)
   170  		if err == nil {
   171  			t.Errorf("test %d: expected error for selector '%v'", i, tt)
   172  		}
   173  	}
   174  }