github.com/p202io/bor@v0.1.4/accounts/abi/unpack_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/big" 24 "reflect" 25 "strconv" 26 "strings" 27 "testing" 28 29 "github.com/maticnetwork/bor/common" 30 "github.com/stretchr/testify/require" 31 ) 32 33 type unpackTest struct { 34 def string // ABI definition JSON 35 enc string // evm return data 36 want interface{} // the expected output 37 err string // empty or error if expected 38 } 39 40 func (test unpackTest) checkError(err error) error { 41 if err != nil { 42 if len(test.err) == 0 { 43 return fmt.Errorf("expected no err but got: %v", err) 44 } else if err.Error() != test.err { 45 return fmt.Errorf("expected err: '%v' got err: %q", test.err, err) 46 } 47 } else if len(test.err) > 0 { 48 return fmt.Errorf("expected err: %v but got none", test.err) 49 } 50 return nil 51 } 52 53 var unpackTests = []unpackTest{ 54 { 55 def: `[{ "type": "bool" }]`, 56 enc: "0000000000000000000000000000000000000000000000000000000000000001", 57 want: true, 58 }, 59 { 60 def: `[{ "type": "bool" }]`, 61 enc: "0000000000000000000000000000000000000000000000000000000000000000", 62 want: false, 63 }, 64 { 65 def: `[{ "type": "bool" }]`, 66 enc: "0000000000000000000000000000000000000000000000000001000000000001", 67 want: false, 68 err: "abi: improperly encoded boolean value", 69 }, 70 { 71 def: `[{ "type": "bool" }]`, 72 enc: "0000000000000000000000000000000000000000000000000000000000000003", 73 want: false, 74 err: "abi: improperly encoded boolean value", 75 }, 76 { 77 def: `[{"type": "uint32"}]`, 78 enc: "0000000000000000000000000000000000000000000000000000000000000001", 79 want: uint32(1), 80 }, 81 { 82 def: `[{"type": "uint32"}]`, 83 enc: "0000000000000000000000000000000000000000000000000000000000000001", 84 want: uint16(0), 85 err: "abi: cannot unmarshal uint32 in to uint16", 86 }, 87 { 88 def: `[{"type": "uint17"}]`, 89 enc: "0000000000000000000000000000000000000000000000000000000000000001", 90 want: uint16(0), 91 err: "abi: cannot unmarshal *big.Int in to uint16", 92 }, 93 { 94 def: `[{"type": "uint17"}]`, 95 enc: "0000000000000000000000000000000000000000000000000000000000000001", 96 want: big.NewInt(1), 97 }, 98 { 99 def: `[{"type": "int32"}]`, 100 enc: "0000000000000000000000000000000000000000000000000000000000000001", 101 want: int32(1), 102 }, 103 { 104 def: `[{"type": "int32"}]`, 105 enc: "0000000000000000000000000000000000000000000000000000000000000001", 106 want: int16(0), 107 err: "abi: cannot unmarshal int32 in to int16", 108 }, 109 { 110 def: `[{"type": "int17"}]`, 111 enc: "0000000000000000000000000000000000000000000000000000000000000001", 112 want: int16(0), 113 err: "abi: cannot unmarshal *big.Int in to int16", 114 }, 115 { 116 def: `[{"type": "int17"}]`, 117 enc: "0000000000000000000000000000000000000000000000000000000000000001", 118 want: big.NewInt(1), 119 }, 120 { 121 def: `[{"type": "int256"}]`, 122 enc: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 123 want: big.NewInt(-1), 124 }, 125 { 126 def: `[{"type": "address"}]`, 127 enc: "0000000000000000000000000100000000000000000000000000000000000000", 128 want: common.Address{1}, 129 }, 130 { 131 def: `[{"type": "bytes32"}]`, 132 enc: "0100000000000000000000000000000000000000000000000000000000000000", 133 want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 134 }, 135 { 136 def: `[{"type": "bytes"}]`, 137 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 138 want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 139 }, 140 { 141 def: `[{"type": "bytes"}]`, 142 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 143 want: [32]byte{}, 144 err: "abi: cannot unmarshal []uint8 in to [32]uint8", 145 }, 146 { 147 def: `[{"type": "bytes32"}]`, 148 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 149 want: []byte(nil), 150 err: "abi: cannot unmarshal [32]uint8 in to []uint8", 151 }, 152 { 153 def: `[{"type": "bytes32"}]`, 154 enc: "0100000000000000000000000000000000000000000000000000000000000000", 155 want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 156 }, 157 { 158 def: `[{"type": "function"}]`, 159 enc: "0100000000000000000000000000000000000000000000000000000000000000", 160 want: [24]byte{1}, 161 }, 162 // slices 163 { 164 def: `[{"type": "uint8[]"}]`, 165 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 166 want: []uint8{1, 2}, 167 }, 168 { 169 def: `[{"type": "uint8[2]"}]`, 170 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 171 want: [2]uint8{1, 2}, 172 }, 173 // multi dimensional, if these pass, all types that don't require length prefix should pass 174 { 175 def: `[{"type": "uint8[][]"}]`, 176 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 177 want: [][]uint8{{1, 2}, {1, 2}}, 178 }, 179 { 180 def: `[{"type": "uint8[][]"}]`, 181 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 182 want: [][]uint8{{1, 2}, {1, 2, 3}}, 183 }, 184 { 185 def: `[{"type": "uint8[2][2]"}]`, 186 enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 187 want: [2][2]uint8{{1, 2}, {1, 2}}, 188 }, 189 { 190 def: `[{"type": "uint8[][2]"}]`, 191 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", 192 want: [2][]uint8{{1}, {1}}, 193 }, 194 { 195 def: `[{"type": "uint8[2][]"}]`, 196 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 197 want: [][2]uint8{{1, 2}}, 198 }, 199 { 200 def: `[{"type": "uint8[2][]"}]`, 201 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 202 want: [][2]uint8{{1, 2}, {1, 2}}, 203 }, 204 { 205 def: `[{"type": "uint16[]"}]`, 206 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 207 want: []uint16{1, 2}, 208 }, 209 { 210 def: `[{"type": "uint16[2]"}]`, 211 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 212 want: [2]uint16{1, 2}, 213 }, 214 { 215 def: `[{"type": "uint32[]"}]`, 216 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 217 want: []uint32{1, 2}, 218 }, 219 { 220 def: `[{"type": "uint32[2]"}]`, 221 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 222 want: [2]uint32{1, 2}, 223 }, 224 { 225 def: `[{"type": "uint32[2][3][4]"}]`, 226 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018", 227 want: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, 228 }, 229 { 230 def: `[{"type": "uint64[]"}]`, 231 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 232 want: []uint64{1, 2}, 233 }, 234 { 235 def: `[{"type": "uint64[2]"}]`, 236 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 237 want: [2]uint64{1, 2}, 238 }, 239 { 240 def: `[{"type": "uint256[]"}]`, 241 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 242 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 243 }, 244 { 245 def: `[{"type": "uint256[3]"}]`, 246 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 247 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 248 }, 249 { 250 def: `[{"type": "string[4]"}]`, 251 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b476f2d657468657265756d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000", 252 want: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"}, 253 }, 254 { 255 def: `[{"type": "string[]"}]`, 256 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000", 257 want: []string{"Ethereum", "go-ethereum"}, 258 }, 259 { 260 def: `[{"type": "bytes[]"}]`, 261 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003f0f0f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003f0f0f00000000000000000000000000000000000000000000000000000000000", 262 want: [][]byte{{0xf0, 0xf0, 0xf0}, {0xf0, 0xf0, 0xf0}}, 263 }, 264 { 265 def: `[{"type": "uint256[2][][]"}]`, 266 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8", 267 want: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}}, 268 }, 269 { 270 def: `[{"type": "int8[]"}]`, 271 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 272 want: []int8{1, 2}, 273 }, 274 { 275 def: `[{"type": "int8[2]"}]`, 276 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 277 want: [2]int8{1, 2}, 278 }, 279 { 280 def: `[{"type": "int16[]"}]`, 281 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 282 want: []int16{1, 2}, 283 }, 284 { 285 def: `[{"type": "int16[2]"}]`, 286 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 287 want: [2]int16{1, 2}, 288 }, 289 { 290 def: `[{"type": "int32[]"}]`, 291 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 292 want: []int32{1, 2}, 293 }, 294 { 295 def: `[{"type": "int32[2]"}]`, 296 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 297 want: [2]int32{1, 2}, 298 }, 299 { 300 def: `[{"type": "int64[]"}]`, 301 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 302 want: []int64{1, 2}, 303 }, 304 { 305 def: `[{"type": "int64[2]"}]`, 306 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 307 want: [2]int64{1, 2}, 308 }, 309 { 310 def: `[{"type": "int256[]"}]`, 311 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 312 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 313 }, 314 { 315 def: `[{"type": "int256[3]"}]`, 316 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 317 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 318 }, 319 // struct outputs 320 { 321 def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`, 322 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 323 want: struct { 324 Int1 *big.Int 325 Int2 *big.Int 326 }{big.NewInt(1), big.NewInt(2)}, 327 }, 328 { 329 def: `[{"name":"int_one","type":"int256"}]`, 330 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 331 want: struct { 332 IntOne *big.Int 333 }{big.NewInt(1)}, 334 }, 335 { 336 def: `[{"name":"int__one","type":"int256"}]`, 337 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 338 want: struct { 339 IntOne *big.Int 340 }{big.NewInt(1)}, 341 }, 342 { 343 def: `[{"name":"int_one_","type":"int256"}]`, 344 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 345 want: struct { 346 IntOne *big.Int 347 }{big.NewInt(1)}, 348 }, 349 { 350 def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`, 351 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 352 want: struct { 353 IntOne *big.Int 354 Intone *big.Int 355 }{big.NewInt(1), big.NewInt(2)}, 356 }, 357 { 358 def: `[{"name":"___","type":"int256"}]`, 359 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 360 want: struct { 361 IntOne *big.Int 362 Intone *big.Int 363 }{}, 364 err: "abi: purely underscored output cannot unpack to struct", 365 }, 366 { 367 def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`, 368 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 369 want: struct { 370 Int1 *big.Int 371 Int2 *big.Int 372 }{}, 373 err: "abi: multiple outputs mapping to the same struct field 'IntOne'", 374 }, 375 { 376 def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, 377 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 378 want: struct { 379 Int1 *big.Int 380 Int2 *big.Int 381 }{}, 382 err: "abi: multiple outputs mapping to the same struct field 'Int'", 383 }, 384 { 385 def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`, 386 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 387 want: struct { 388 Int1 *big.Int 389 Int2 *big.Int 390 }{}, 391 err: "abi: multiple outputs mapping to the same struct field 'Int'", 392 }, 393 { 394 def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`, 395 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 396 want: struct { 397 Int1 *big.Int 398 Int2 *big.Int 399 }{}, 400 err: "abi: multiple outputs mapping to the same struct field 'Int'", 401 }, 402 { 403 def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`, 404 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 405 want: struct { 406 Int1 *big.Int 407 Int2 *big.Int 408 }{}, 409 err: "abi: purely underscored output cannot unpack to struct", 410 }, 411 } 412 413 func TestUnpack(t *testing.T) { 414 for i, test := range unpackTests { 415 t.Run(strconv.Itoa(i), func(t *testing.T) { 416 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 417 abi, err := JSON(strings.NewReader(def)) 418 if err != nil { 419 t.Fatalf("invalid ABI definition %s: %v", def, err) 420 } 421 encb, err := hex.DecodeString(test.enc) 422 if err != nil { 423 t.Fatalf("invalid hex: %s" + test.enc) 424 } 425 outptr := reflect.New(reflect.TypeOf(test.want)) 426 err = abi.Unpack(outptr.Interface(), "method", encb) 427 if err := test.checkError(err); err != nil { 428 t.Errorf("test %d (%v) failed: %v", i, test.def, err) 429 return 430 } 431 out := outptr.Elem().Interface() 432 if !reflect.DeepEqual(test.want, out) { 433 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) 434 } 435 }) 436 } 437 } 438 439 func TestUnpackSetDynamicArrayOutput(t *testing.T) { 440 abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`)) 441 if err != nil { 442 t.Fatal(err) 443 } 444 445 var ( 446 marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000") 447 marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000") 448 449 out32 [][32]byte 450 out15 [][15]byte 451 ) 452 453 // test 32 454 err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32) 455 if err != nil { 456 t.Fatal(err) 457 } 458 if len(out32) != 2 { 459 t.Fatalf("expected array with 2 values, got %d", len(out32)) 460 } 461 expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000") 462 if !bytes.Equal(out32[0][:], expected) { 463 t.Errorf("expected %x, got %x\n", expected, out32[0]) 464 } 465 expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000") 466 if !bytes.Equal(out32[1][:], expected) { 467 t.Errorf("expected %x, got %x\n", expected, out32[1]) 468 } 469 470 // test 15 471 err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15) 472 if err != nil { 473 t.Fatal(err) 474 } 475 if len(out15) != 2 { 476 t.Fatalf("expected array with 2 values, got %d", len(out15)) 477 } 478 expected = common.Hex2Bytes("307830313233343500000000000000") 479 if !bytes.Equal(out15[0][:], expected) { 480 t.Errorf("expected %x, got %x\n", expected, out15[0]) 481 } 482 expected = common.Hex2Bytes("307839383736353400000000000000") 483 if !bytes.Equal(out15[1][:], expected) { 484 t.Errorf("expected %x, got %x\n", expected, out15[1]) 485 } 486 } 487 488 type methodMultiOutput struct { 489 Int *big.Int 490 String string 491 } 492 493 func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { 494 const definition = `[ 495 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 496 var expected = methodMultiOutput{big.NewInt(1), "hello"} 497 498 abi, err := JSON(strings.NewReader(definition)) 499 require.NoError(err) 500 // using buff to make the code readable 501 buff := new(bytes.Buffer) 502 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 503 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 504 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 505 buff.Write(common.RightPadBytes([]byte(expected.String), 32)) 506 return abi, buff.Bytes(), expected 507 } 508 509 func TestMethodMultiReturn(t *testing.T) { 510 type reversed struct { 511 String string 512 Int *big.Int 513 } 514 515 newInterfaceSlice := func(len int) interface{} { 516 slice := make([]interface{}, len) 517 return &slice 518 } 519 520 abi, data, expected := methodMultiReturn(require.New(t)) 521 bigint := new(big.Int) 522 var testCases = []struct { 523 dest interface{} 524 expected interface{} 525 error string 526 name string 527 }{{ 528 &methodMultiOutput{}, 529 &expected, 530 "", 531 "Can unpack into structure", 532 }, { 533 &reversed{}, 534 &reversed{expected.String, expected.Int}, 535 "", 536 "Can unpack into reversed structure", 537 }, { 538 &[]interface{}{&bigint, new(string)}, 539 &[]interface{}{&expected.Int, &expected.String}, 540 "", 541 "Can unpack into a slice", 542 }, { 543 &[2]interface{}{&bigint, new(string)}, 544 &[2]interface{}{&expected.Int, &expected.String}, 545 "", 546 "Can unpack into an array", 547 }, { 548 &[2]interface{}{}, 549 &[2]interface{}{expected.Int, expected.String}, 550 "", 551 "Can unpack into interface array", 552 }, { 553 newInterfaceSlice(2), 554 &[]interface{}{expected.Int, expected.String}, 555 "", 556 "Can unpack into interface slice", 557 }, { 558 &[]interface{}{new(int), new(int)}, 559 &[]interface{}{&expected.Int, &expected.String}, 560 "abi: cannot unmarshal *big.Int in to int", 561 "Can not unpack into a slice with wrong types", 562 }, { 563 &[]interface{}{new(int)}, 564 &[]interface{}{}, 565 "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", 566 "Can not unpack into a slice with wrong types", 567 }} 568 for _, tc := range testCases { 569 tc := tc 570 t.Run(tc.name, func(t *testing.T) { 571 require := require.New(t) 572 err := abi.Unpack(tc.dest, "multi", data) 573 if tc.error == "" { 574 require.Nil(err, "Should be able to unpack method outputs.") 575 require.Equal(tc.expected, tc.dest) 576 } else { 577 require.EqualError(err, tc.error) 578 } 579 }) 580 } 581 } 582 583 func TestMultiReturnWithArray(t *testing.T) { 584 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` 585 abi, err := JSON(strings.NewReader(definition)) 586 if err != nil { 587 t.Fatal(err) 588 } 589 buff := new(bytes.Buffer) 590 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) 591 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 592 593 ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} 594 ret2, ret2Exp := new(uint64), uint64(8) 595 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 596 t.Fatal(err) 597 } 598 if !reflect.DeepEqual(*ret1, ret1Exp) { 599 t.Error("array result", *ret1, "!= Expected", ret1Exp) 600 } 601 if *ret2 != ret2Exp { 602 t.Error("int result", *ret2, "!= Expected", ret2Exp) 603 } 604 } 605 606 func TestMultiReturnWithStringArray(t *testing.T) { 607 const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]` 608 abi, err := JSON(strings.NewReader(definition)) 609 if err != nil { 610 t.Fatal(err) 611 } 612 buff := new(bytes.Buffer) 613 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) 614 temp, _ := big.NewInt(0).SetString("30000000000000000000", 10) 615 ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} 616 ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") 617 ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} 618 ret4, ret4Exp := new(bool), false 619 if err := abi.Unpack(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil { 620 t.Fatal(err) 621 } 622 if !reflect.DeepEqual(*ret1, ret1Exp) { 623 t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp) 624 } 625 if !reflect.DeepEqual(*ret2, ret2Exp) { 626 t.Error("address result", *ret2, "!= Expected", ret2Exp) 627 } 628 if !reflect.DeepEqual(*ret3, ret3Exp) { 629 t.Error("string array result", *ret3, "!= Expected", ret3Exp) 630 } 631 if !reflect.DeepEqual(*ret4, ret4Exp) { 632 t.Error("bool result", *ret4, "!= Expected", ret4Exp) 633 } 634 } 635 636 func TestMultiReturnWithStringSlice(t *testing.T) { 637 const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]` 638 abi, err := JSON(strings.NewReader(definition)) 639 if err != nil { 640 t.Fatal(err) 641 } 642 buff := new(bytes.Buffer) 643 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset 644 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset 645 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length 646 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset 647 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset 648 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length 649 buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value 650 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length 651 buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value 652 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length 653 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value 654 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value 655 ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"} 656 ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)} 657 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 658 t.Fatal(err) 659 } 660 if !reflect.DeepEqual(*ret1, ret1Exp) { 661 t.Error("string slice result", *ret1, "!= Expected", ret1Exp) 662 } 663 if !reflect.DeepEqual(*ret2, ret2Exp) { 664 t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp) 665 } 666 } 667 668 func TestMultiReturnWithDeeplyNestedArray(t *testing.T) { 669 // Similar to TestMultiReturnWithArray, but with a special case in mind: 670 // values of nested static arrays count towards the size as well, and any element following 671 // after such nested array argument should be read with the correct offset, 672 // so that it does not read content from the previous array argument. 673 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]` 674 abi, err := JSON(strings.NewReader(definition)) 675 if err != nil { 676 t.Fatal(err) 677 } 678 buff := new(bytes.Buffer) 679 // construct the test array, each 3 char element is joined with 61 '0' chars, 680 // to from the ((3 + 61) * 0.5) = 32 byte elements in the array. 681 buff.Write(common.Hex2Bytes(strings.Join([]string{ 682 "", //empty, to apply the 61-char separator to the first element as well. 683 "111", "112", "113", "121", "122", "123", 684 "211", "212", "213", "221", "222", "223", 685 "311", "312", "313", "321", "322", "323", 686 "411", "412", "413", "421", "422", "423", 687 }, "0000000000000000000000000000000000000000000000000000000000000"))) 688 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876")) 689 690 ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{ 691 {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}}, 692 {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}}, 693 {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}}, 694 {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}}, 695 } 696 ret2, ret2Exp := new(uint64), uint64(0x9876) 697 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 698 t.Fatal(err) 699 } 700 if !reflect.DeepEqual(*ret1, ret1Exp) { 701 t.Error("array result", *ret1, "!= Expected", ret1Exp) 702 } 703 if *ret2 != ret2Exp { 704 t.Error("int result", *ret2, "!= Expected", ret2Exp) 705 } 706 } 707 708 func TestUnmarshal(t *testing.T) { 709 const definition = `[ 710 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 711 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 712 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 713 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 714 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 715 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 716 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 717 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 718 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 719 720 abi, err := JSON(strings.NewReader(definition)) 721 if err != nil { 722 t.Fatal(err) 723 } 724 buff := new(bytes.Buffer) 725 726 // marshall mixed bytes (mixedBytes) 727 p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") 728 p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") 729 mixedBytes := []interface{}{&p0, &p1} 730 731 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 732 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) 733 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) 734 buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) 735 736 err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes()) 737 if err != nil { 738 t.Error(err) 739 } else { 740 if !bytes.Equal(p0, p0Exp) { 741 t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) 742 } 743 744 if !bytes.Equal(p1[:], p1Exp) { 745 t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) 746 } 747 } 748 749 // marshal int 750 var Int *big.Int 751 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 752 if err != nil { 753 t.Error(err) 754 } 755 756 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 757 t.Error("expected Int to be 1 got", Int) 758 } 759 760 // marshal bool 761 var Bool bool 762 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 763 if err != nil { 764 t.Error(err) 765 } 766 767 if !Bool { 768 t.Error("expected Bool to be true") 769 } 770 771 // marshal dynamic bytes max length 32 772 buff.Reset() 773 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 774 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 775 bytesOut := common.RightPadBytes([]byte("hello"), 32) 776 buff.Write(bytesOut) 777 778 var Bytes []byte 779 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 780 if err != nil { 781 t.Error(err) 782 } 783 784 if !bytes.Equal(Bytes, bytesOut) { 785 t.Errorf("expected %x got %x", bytesOut, Bytes) 786 } 787 788 // marshall dynamic bytes max length 64 789 buff.Reset() 790 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 791 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 792 bytesOut = common.RightPadBytes([]byte("hello"), 64) 793 buff.Write(bytesOut) 794 795 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 796 if err != nil { 797 t.Error(err) 798 } 799 800 if !bytes.Equal(Bytes, bytesOut) { 801 t.Errorf("expected %x got %x", bytesOut, Bytes) 802 } 803 804 // marshall dynamic bytes max length 64 805 buff.Reset() 806 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 807 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 808 bytesOut = common.RightPadBytes([]byte("hello"), 64) 809 buff.Write(bytesOut) 810 811 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 812 if err != nil { 813 t.Error(err) 814 } 815 816 if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { 817 t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) 818 } 819 820 // marshal dynamic bytes output empty 821 err = abi.Unpack(&Bytes, "bytes", nil) 822 if err == nil { 823 t.Error("expected error") 824 } 825 826 // marshal dynamic bytes length 5 827 buff.Reset() 828 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 829 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 830 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 831 832 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 833 if err != nil { 834 t.Error(err) 835 } 836 837 if !bytes.Equal(Bytes, []byte("hello")) { 838 t.Errorf("expected %x got %x", bytesOut, Bytes) 839 } 840 841 // marshal dynamic bytes length 5 842 buff.Reset() 843 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 844 845 var hash common.Hash 846 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 847 if err != nil { 848 t.Error(err) 849 } 850 851 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 852 if hash != helloHash { 853 t.Errorf("Expected %x to equal %x", hash, helloHash) 854 } 855 856 // marshal error 857 buff.Reset() 858 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 859 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 860 if err == nil { 861 t.Error("expected error") 862 } 863 864 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 865 if err == nil { 866 t.Error("expected error") 867 } 868 869 buff.Reset() 870 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 871 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 872 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 873 // marshal int array 874 var intArray [3]*big.Int 875 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 876 if err != nil { 877 t.Error(err) 878 } 879 var testAgainstIntArray [3]*big.Int 880 testAgainstIntArray[0] = big.NewInt(1) 881 testAgainstIntArray[1] = big.NewInt(2) 882 testAgainstIntArray[2] = big.NewInt(3) 883 884 for i, Int := range intArray { 885 if Int.Cmp(testAgainstIntArray[i]) != 0 { 886 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 887 } 888 } 889 // marshal address slice 890 buff.Reset() 891 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset 892 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 893 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 894 895 var outAddr []common.Address 896 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 897 if err != nil { 898 t.Fatal("didn't expect error:", err) 899 } 900 901 if len(outAddr) != 1 { 902 t.Fatal("expected 1 item, got", len(outAddr)) 903 } 904 905 if outAddr[0] != (common.Address{1}) { 906 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 907 } 908 909 // marshal multiple address slice 910 buff.Reset() 911 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset 912 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset 913 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 914 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 915 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size 916 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 917 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 918 919 var outAddrStruct struct { 920 A []common.Address 921 B []common.Address 922 } 923 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 924 if err != nil { 925 t.Fatal("didn't expect error:", err) 926 } 927 928 if len(outAddrStruct.A) != 1 { 929 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 930 } 931 932 if outAddrStruct.A[0] != (common.Address{1}) { 933 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 934 } 935 936 if len(outAddrStruct.B) != 2 { 937 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 938 } 939 940 if outAddrStruct.B[0] != (common.Address{2}) { 941 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 942 } 943 if outAddrStruct.B[1] != (common.Address{3}) { 944 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 945 } 946 947 // marshal invalid address slice 948 buff.Reset() 949 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 950 951 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 952 if err == nil { 953 t.Fatal("expected error:", err) 954 } 955 } 956 957 func TestUnpackTuple(t *testing.T) { 958 const simpleTuple = `[{"name":"tuple","constant":false,"outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]` 959 abi, err := JSON(strings.NewReader(simpleTuple)) 960 if err != nil { 961 t.Fatal(err) 962 } 963 buff := new(bytes.Buffer) 964 965 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1 966 buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1 967 968 // If the result is single tuple, use struct as return value container directly. 969 v := struct { 970 A *big.Int 971 B *big.Int 972 }{new(big.Int), new(big.Int)} 973 974 err = abi.Unpack(&v, "tuple", buff.Bytes()) 975 if err != nil { 976 t.Error(err) 977 } else { 978 if v.A.Cmp(big.NewInt(1)) != 0 { 979 t.Errorf("unexpected value unpacked: want %x, got %x", 1, v.A) 980 } 981 if v.B.Cmp(big.NewInt(-1)) != 0 { 982 t.Errorf("unexpected value unpacked: want %x, got %x", v.B, -1) 983 } 984 } 985 986 // Test nested tuple 987 const nestedTuple = `[{"name":"tuple","constant":false,"outputs":[ 988 {"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]}, 989 {"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}, 990 {"type":"uint256","name":"a"} 991 ]}]` 992 993 abi, err = JSON(strings.NewReader(nestedTuple)) 994 if err != nil { 995 t.Fatal(err) 996 } 997 buff.Reset() 998 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset 999 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0 1000 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1 1001 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1 1002 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1 1003 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset 1004 buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset 1005 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length 1006 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1 1007 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2 1008 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length 1009 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X 1010 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y 1011 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X 1012 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y 1013 1014 type T struct { 1015 X *big.Int `abi:"x"` 1016 Z *big.Int `abi:"y"` // Test whether the abi tag works. 1017 } 1018 1019 type S struct { 1020 A *big.Int 1021 B []*big.Int 1022 C []T 1023 } 1024 1025 type Ret struct { 1026 FieldS S `abi:"s"` 1027 FieldT T `abi:"t"` 1028 A *big.Int 1029 } 1030 var ret Ret 1031 var expected = Ret{ 1032 FieldS: S{ 1033 A: big.NewInt(1), 1034 B: []*big.Int{big.NewInt(1), big.NewInt(2)}, 1035 C: []T{ 1036 {big.NewInt(1), big.NewInt(2)}, 1037 {big.NewInt(2), big.NewInt(1)}, 1038 }, 1039 }, 1040 FieldT: T{ 1041 big.NewInt(0), big.NewInt(1), 1042 }, 1043 A: big.NewInt(1), 1044 } 1045 1046 err = abi.Unpack(&ret, "tuple", buff.Bytes()) 1047 if err != nil { 1048 t.Error(err) 1049 } 1050 if reflect.DeepEqual(ret, expected) { 1051 t.Error("unexpected unpack value") 1052 } 1053 } 1054 1055 func TestOOMMaliciousInput(t *testing.T) { 1056 oomTests := []unpackTest{ 1057 { 1058 def: `[{"type": "uint8[]"}]`, 1059 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 1060 "0000000000000000000000000000000000000000000000000000000000000003" + // num elems 1061 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1062 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1063 }, 1064 { // Length larger than 64 bits 1065 def: `[{"type": "uint8[]"}]`, 1066 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 1067 "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems 1068 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1069 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1070 }, 1071 { // Offset very large (over 64 bits) 1072 def: `[{"type": "uint8[]"}]`, 1073 enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset 1074 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 1075 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1076 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1077 }, 1078 { // Offset very large (below 64 bits) 1079 def: `[{"type": "uint8[]"}]`, 1080 enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset 1081 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 1082 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1083 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1084 }, 1085 { // Offset negative (as 64 bit) 1086 def: `[{"type": "uint8[]"}]`, 1087 enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset 1088 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 1089 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1090 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1091 }, 1092 1093 { // Negative length 1094 def: `[{"type": "uint8[]"}]`, 1095 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 1096 "000000000000000000000000000000000000000000000000f000000000000002" + // num elems 1097 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1098 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1099 }, 1100 { // Very large length 1101 def: `[{"type": "uint8[]"}]`, 1102 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 1103 "0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems 1104 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 1105 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 1106 }, 1107 } 1108 for i, test := range oomTests { 1109 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 1110 abi, err := JSON(strings.NewReader(def)) 1111 if err != nil { 1112 t.Fatalf("invalid ABI definition %s: %v", def, err) 1113 } 1114 encb, err := hex.DecodeString(test.enc) 1115 if err != nil { 1116 t.Fatalf("invalid hex: %s" + test.enc) 1117 } 1118 _, err = abi.Methods["method"].Outputs.UnpackValues(encb) 1119 if err == nil { 1120 t.Fatalf("Expected error on malicious input, test %d", i) 1121 } 1122 } 1123 }