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