github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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/ethereum/go-ethereum/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: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 177 want: [][]uint8{{1, 2}, {1, 2}}, 178 }, 179 { 180 def: `[{"type": "uint8[2][2]"}]`, 181 enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 182 want: [2][2]uint8{{1, 2}, {1, 2}}, 183 }, 184 { 185 def: `[{"type": "uint8[][2]"}]`, 186 enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", 187 want: [2][]uint8{{1}, {1}}, 188 }, 189 { 190 def: `[{"type": "uint8[2][]"}]`, 191 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 192 want: [][2]uint8{{1, 2}}, 193 }, 194 { 195 def: `[{"type": "uint16[]"}]`, 196 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 197 want: []uint16{1, 2}, 198 }, 199 { 200 def: `[{"type": "uint16[2]"}]`, 201 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 202 want: [2]uint16{1, 2}, 203 }, 204 { 205 def: `[{"type": "uint32[]"}]`, 206 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 207 want: []uint32{1, 2}, 208 }, 209 { 210 def: `[{"type": "uint32[2]"}]`, 211 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 212 want: [2]uint32{1, 2}, 213 }, 214 { 215 def: `[{"type": "uint32[2][3][4]"}]`, 216 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018", 217 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}}}, 218 }, 219 { 220 def: `[{"type": "uint64[]"}]`, 221 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 222 want: []uint64{1, 2}, 223 }, 224 { 225 def: `[{"type": "uint64[2]"}]`, 226 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 227 want: [2]uint64{1, 2}, 228 }, 229 { 230 def: `[{"type": "uint256[]"}]`, 231 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 232 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 233 }, 234 { 235 def: `[{"type": "uint256[3]"}]`, 236 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 237 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 238 }, 239 { 240 def: `[{"type": "int8[]"}]`, 241 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 242 want: []int8{1, 2}, 243 }, 244 { 245 def: `[{"type": "int8[2]"}]`, 246 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 247 want: [2]int8{1, 2}, 248 }, 249 { 250 def: `[{"type": "int16[]"}]`, 251 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 252 want: []int16{1, 2}, 253 }, 254 { 255 def: `[{"type": "int16[2]"}]`, 256 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 257 want: [2]int16{1, 2}, 258 }, 259 { 260 def: `[{"type": "int32[]"}]`, 261 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 262 want: []int32{1, 2}, 263 }, 264 { 265 def: `[{"type": "int32[2]"}]`, 266 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 267 want: [2]int32{1, 2}, 268 }, 269 { 270 def: `[{"type": "int64[]"}]`, 271 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 272 want: []int64{1, 2}, 273 }, 274 { 275 def: `[{"type": "int64[2]"}]`, 276 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 277 want: [2]int64{1, 2}, 278 }, 279 { 280 def: `[{"type": "int256[]"}]`, 281 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 282 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 283 }, 284 { 285 def: `[{"type": "int256[3]"}]`, 286 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 287 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 288 }, 289 // struct outputs 290 { 291 def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`, 292 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 293 want: struct { 294 Int1 *big.Int 295 Int2 *big.Int 296 }{big.NewInt(1), big.NewInt(2)}, 297 }, 298 { 299 def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, 300 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 301 want: struct { 302 Int1 *big.Int 303 Int2 *big.Int 304 }{}, 305 err: "abi: multiple outputs mapping to the same struct field 'Int'", 306 }, 307 { 308 def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`, 309 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 310 want: struct { 311 Int1 *big.Int 312 Int2 *big.Int 313 }{}, 314 err: "abi: multiple outputs mapping to the same struct field 'Int'", 315 }, 316 { 317 def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`, 318 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 319 want: struct { 320 Int1 *big.Int 321 Int2 *big.Int 322 }{}, 323 err: "abi: multiple outputs mapping to the same struct field 'Int'", 324 }, 325 { 326 def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`, 327 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 328 want: struct { 329 Int1 *big.Int 330 Int2 *big.Int 331 }{}, 332 err: "abi: purely underscored output cannot unpack to struct", 333 }, 334 } 335 336 func TestUnpack(t *testing.T) { 337 for i, test := range unpackTests { 338 t.Run(strconv.Itoa(i), func(t *testing.T) { 339 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 340 abi, err := JSON(strings.NewReader(def)) 341 if err != nil { 342 t.Fatalf("invalid ABI definition %s: %v", def, err) 343 } 344 encb, err := hex.DecodeString(test.enc) 345 if err != nil { 346 t.Fatalf("invalid hex: %s" + test.enc) 347 } 348 outptr := reflect.New(reflect.TypeOf(test.want)) 349 err = abi.Unpack(outptr.Interface(), "method", encb) 350 if err := test.checkError(err); err != nil { 351 t.Errorf("test %d (%v) failed: %v", i, test.def, err) 352 return 353 } 354 out := outptr.Elem().Interface() 355 if !reflect.DeepEqual(test.want, out) { 356 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) 357 } 358 }) 359 } 360 } 361 362 type methodMultiOutput struct { 363 Int *big.Int 364 String string 365 } 366 367 func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { 368 const definition = `[ 369 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 370 var expected = methodMultiOutput{big.NewInt(1), "hello"} 371 372 abi, err := JSON(strings.NewReader(definition)) 373 require.NoError(err) 374 // using buff to make the code readable 375 buff := new(bytes.Buffer) 376 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 377 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 378 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 379 buff.Write(common.RightPadBytes([]byte(expected.String), 32)) 380 return abi, buff.Bytes(), expected 381 } 382 383 func TestMethodMultiReturn(t *testing.T) { 384 type reversed struct { 385 String string 386 Int *big.Int 387 } 388 389 abi, data, expected := methodMultiReturn(require.New(t)) 390 bigint := new(big.Int) 391 var testCases = []struct { 392 dest interface{} 393 expected interface{} 394 error string 395 name string 396 }{{ 397 &methodMultiOutput{}, 398 &expected, 399 "", 400 "Can unpack into structure", 401 }, { 402 &reversed{}, 403 &reversed{expected.String, expected.Int}, 404 "", 405 "Can unpack into reversed structure", 406 }, { 407 &[]interface{}{&bigint, new(string)}, 408 &[]interface{}{&expected.Int, &expected.String}, 409 "", 410 "Can unpack into a slice", 411 }, { 412 &[2]interface{}{&bigint, new(string)}, 413 &[2]interface{}{&expected.Int, &expected.String}, 414 "", 415 "Can unpack into an array", 416 }, { 417 &[]interface{}{new(int), new(int)}, 418 &[]interface{}{&expected.Int, &expected.String}, 419 "abi: cannot unmarshal *big.Int in to int", 420 "Can not unpack into a slice with wrong types", 421 }, { 422 &[]interface{}{new(int)}, 423 &[]interface{}{}, 424 "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", 425 "Can not unpack into a slice with wrong types", 426 }} 427 for _, tc := range testCases { 428 tc := tc 429 t.Run(tc.name, func(t *testing.T) { 430 require := require.New(t) 431 err := abi.Unpack(tc.dest, "multi", data) 432 if tc.error == "" { 433 require.Nil(err, "Should be able to unpack method outputs.") 434 require.Equal(tc.expected, tc.dest) 435 } else { 436 require.EqualError(err, tc.error) 437 } 438 }) 439 } 440 } 441 442 func TestMultiReturnWithArray(t *testing.T) { 443 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` 444 abi, err := JSON(strings.NewReader(definition)) 445 if err != nil { 446 t.Fatal(err) 447 } 448 buff := new(bytes.Buffer) 449 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) 450 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 451 452 ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} 453 ret2, ret2Exp := new(uint64), uint64(8) 454 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 455 t.Fatal(err) 456 } 457 if !reflect.DeepEqual(*ret1, ret1Exp) { 458 t.Error("array result", *ret1, "!= Expected", ret1Exp) 459 } 460 if *ret2 != ret2Exp { 461 t.Error("int result", *ret2, "!= Expected", ret2Exp) 462 } 463 } 464 465 func TestMultiReturnWithDeeplyNestedArray(t *testing.T) { 466 // Similar to TestMultiReturnWithArray, but with a special case in mind: 467 // values of nested static arrays count towards the size as well, and any element following 468 // after such nested array argument should be read with the correct offset, 469 // so that it does not read content from the previous array argument. 470 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]` 471 abi, err := JSON(strings.NewReader(definition)) 472 if err != nil { 473 t.Fatal(err) 474 } 475 buff := new(bytes.Buffer) 476 // construct the test array, each 3 char element is joined with 61 '0' chars, 477 // to from the ((3 + 61) * 0.5) = 32 byte elements in the array. 478 buff.Write(common.Hex2Bytes(strings.Join([]string{ 479 "", //empty, to apply the 61-char separator to the first element as well. 480 "111", "112", "113", "121", "122", "123", 481 "211", "212", "213", "221", "222", "223", 482 "311", "312", "313", "321", "322", "323", 483 "411", "412", "413", "421", "422", "423", 484 }, "0000000000000000000000000000000000000000000000000000000000000"))) 485 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876")) 486 487 ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{ 488 {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}}, 489 {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}}, 490 {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}}, 491 {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}}, 492 } 493 ret2, ret2Exp := new(uint64), uint64(0x9876) 494 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 495 t.Fatal(err) 496 } 497 if !reflect.DeepEqual(*ret1, ret1Exp) { 498 t.Error("array result", *ret1, "!= Expected", ret1Exp) 499 } 500 if *ret2 != ret2Exp { 501 t.Error("int result", *ret2, "!= Expected", ret2Exp) 502 } 503 } 504 505 func TestUnmarshal(t *testing.T) { 506 const definition = `[ 507 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 508 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 509 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 510 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 511 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 512 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 513 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 514 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 515 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 516 517 abi, err := JSON(strings.NewReader(definition)) 518 if err != nil { 519 t.Fatal(err) 520 } 521 buff := new(bytes.Buffer) 522 523 // marshall mixed bytes (mixedBytes) 524 p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") 525 p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") 526 mixedBytes := []interface{}{&p0, &p1} 527 528 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 529 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) 530 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) 531 buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) 532 533 err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes()) 534 if err != nil { 535 t.Error(err) 536 } else { 537 if !bytes.Equal(p0, p0Exp) { 538 t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) 539 } 540 541 if !bytes.Equal(p1[:], p1Exp) { 542 t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) 543 } 544 } 545 546 // marshal int 547 var Int *big.Int 548 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 549 if err != nil { 550 t.Error(err) 551 } 552 553 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 554 t.Error("expected Int to be 1 got", Int) 555 } 556 557 // marshal bool 558 var Bool bool 559 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 560 if err != nil { 561 t.Error(err) 562 } 563 564 if !Bool { 565 t.Error("expected Bool to be true") 566 } 567 568 // marshal dynamic bytes max length 32 569 buff.Reset() 570 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 571 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 572 bytesOut := common.RightPadBytes([]byte("hello"), 32) 573 buff.Write(bytesOut) 574 575 var Bytes []byte 576 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 577 if err != nil { 578 t.Error(err) 579 } 580 581 if !bytes.Equal(Bytes, bytesOut) { 582 t.Errorf("expected %x got %x", bytesOut, Bytes) 583 } 584 585 // marshall dynamic bytes max length 64 586 buff.Reset() 587 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 588 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 589 bytesOut = common.RightPadBytes([]byte("hello"), 64) 590 buff.Write(bytesOut) 591 592 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 593 if err != nil { 594 t.Error(err) 595 } 596 597 if !bytes.Equal(Bytes, bytesOut) { 598 t.Errorf("expected %x got %x", bytesOut, Bytes) 599 } 600 601 // marshall dynamic bytes max length 64 602 buff.Reset() 603 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 604 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 605 bytesOut = common.RightPadBytes([]byte("hello"), 64) 606 buff.Write(bytesOut) 607 608 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 609 if err != nil { 610 t.Error(err) 611 } 612 613 if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { 614 t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) 615 } 616 617 // marshal dynamic bytes output empty 618 err = abi.Unpack(&Bytes, "bytes", nil) 619 if err == nil { 620 t.Error("expected error") 621 } 622 623 // marshal dynamic bytes length 5 624 buff.Reset() 625 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 626 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 627 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 628 629 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 630 if err != nil { 631 t.Error(err) 632 } 633 634 if !bytes.Equal(Bytes, []byte("hello")) { 635 t.Errorf("expected %x got %x", bytesOut, Bytes) 636 } 637 638 // marshal dynamic bytes length 5 639 buff.Reset() 640 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 641 642 var hash common.Hash 643 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 644 if err != nil { 645 t.Error(err) 646 } 647 648 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 649 if hash != helloHash { 650 t.Errorf("Expected %x to equal %x", hash, helloHash) 651 } 652 653 // marshal error 654 buff.Reset() 655 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 656 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 657 if err == nil { 658 t.Error("expected error") 659 } 660 661 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 662 if err == nil { 663 t.Error("expected error") 664 } 665 666 buff.Reset() 667 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 668 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 669 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 670 // marshal int array 671 var intArray [3]*big.Int 672 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 673 if err != nil { 674 t.Error(err) 675 } 676 var testAgainstIntArray [3]*big.Int 677 testAgainstIntArray[0] = big.NewInt(1) 678 testAgainstIntArray[1] = big.NewInt(2) 679 testAgainstIntArray[2] = big.NewInt(3) 680 681 for i, Int := range intArray { 682 if Int.Cmp(testAgainstIntArray[i]) != 0 { 683 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 684 } 685 } 686 // marshal address slice 687 buff.Reset() 688 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset 689 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 690 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 691 692 var outAddr []common.Address 693 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 694 if err != nil { 695 t.Fatal("didn't expect error:", err) 696 } 697 698 if len(outAddr) != 1 { 699 t.Fatal("expected 1 item, got", len(outAddr)) 700 } 701 702 if outAddr[0] != (common.Address{1}) { 703 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 704 } 705 706 // marshal multiple address slice 707 buff.Reset() 708 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset 709 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset 710 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 711 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 712 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size 713 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 714 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 715 716 var outAddrStruct struct { 717 A []common.Address 718 B []common.Address 719 } 720 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 721 if err != nil { 722 t.Fatal("didn't expect error:", err) 723 } 724 725 if len(outAddrStruct.A) != 1 { 726 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 727 } 728 729 if outAddrStruct.A[0] != (common.Address{1}) { 730 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 731 } 732 733 if len(outAddrStruct.B) != 2 { 734 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 735 } 736 737 if outAddrStruct.B[0] != (common.Address{2}) { 738 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 739 } 740 if outAddrStruct.B[1] != (common.Address{3}) { 741 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 742 } 743 744 // marshal invalid address slice 745 buff.Reset() 746 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 747 748 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 749 if err == nil { 750 t.Fatal("expected error:", err) 751 } 752 } 753 754 func TestOOMMaliciousInput(t *testing.T) { 755 oomTests := []unpackTest{ 756 { 757 def: `[{"type": "uint8[]"}]`, 758 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 759 "0000000000000000000000000000000000000000000000000000000000000003" + // num elems 760 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 761 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 762 }, 763 { // Length larger than 64 bits 764 def: `[{"type": "uint8[]"}]`, 765 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 766 "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems 767 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 768 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 769 }, 770 { // Offset very large (over 64 bits) 771 def: `[{"type": "uint8[]"}]`, 772 enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset 773 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 774 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 775 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 776 }, 777 { // Offset very large (below 64 bits) 778 def: `[{"type": "uint8[]"}]`, 779 enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset 780 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 781 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 782 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 783 }, 784 { // Offset negative (as 64 bit) 785 def: `[{"type": "uint8[]"}]`, 786 enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset 787 "0000000000000000000000000000000000000000000000000000000000000002" + // num elems 788 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 789 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 790 }, 791 792 { // Negative length 793 def: `[{"type": "uint8[]"}]`, 794 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 795 "000000000000000000000000000000000000000000000000f000000000000002" + // num elems 796 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 797 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 798 }, 799 { // Very large length 800 def: `[{"type": "uint8[]"}]`, 801 enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset 802 "0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems 803 "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1 804 "0000000000000000000000000000000000000000000000000000000000000002", // elem 2 805 }, 806 } 807 for i, test := range oomTests { 808 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 809 abi, err := JSON(strings.NewReader(def)) 810 if err != nil { 811 t.Fatalf("invalid ABI definition %s: %v", def, err) 812 } 813 encb, err := hex.DecodeString(test.enc) 814 if err != nil { 815 t.Fatalf("invalid hex: %s" + test.enc) 816 } 817 _, err = abi.Methods["method"].Outputs.UnpackValues(encb) 818 if err == nil { 819 t.Fatalf("Expected error on malicious input, test %d", i) 820 } 821 } 822 }