github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/accounts/abi/unpack_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-wtc 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-wtc 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 "fmt" 22 "math/big" 23 "reflect" 24 "strings" 25 "testing" 26 27 "github.com/wtc/go-wtc/common" 28 ) 29 30 func TestSimpleMethodUnpack(t *testing.T) { 31 for i, test := range []struct { 32 def string // definition of the **output** ABI params 33 marshalledOutput []byte // evm return data 34 expectedOut interface{} // the expected output 35 outVar string // the output variable (e.g. uint32, *big.Int, etc) 36 err string // empty or error if expected 37 }{ 38 { 39 `[ { "type": "bool" } ]`, 40 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 41 bool(true), 42 "bool", 43 "", 44 }, 45 { 46 `[ { "type": "uint32" } ]`, 47 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 48 uint32(1), 49 "uint32", 50 "", 51 }, 52 { 53 `[ { "type": "uint32" } ]`, 54 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 55 nil, 56 "uint16", 57 "abi: cannot unmarshal uint32 in to uint16", 58 }, 59 { 60 `[ { "type": "uint17" } ]`, 61 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 62 nil, 63 "uint16", 64 "abi: cannot unmarshal *big.Int in to uint16", 65 }, 66 { 67 `[ { "type": "uint17" } ]`, 68 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 69 big.NewInt(1), 70 "*big.Int", 71 "", 72 }, 73 74 { 75 `[ { "type": "int32" } ]`, 76 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 77 int32(1), 78 "int32", 79 "", 80 }, 81 { 82 `[ { "type": "int32" } ]`, 83 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 84 nil, 85 "int16", 86 "abi: cannot unmarshal int32 in to int16", 87 }, 88 { 89 `[ { "type": "int17" } ]`, 90 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 91 nil, 92 "int16", 93 "abi: cannot unmarshal *big.Int in to int16", 94 }, 95 { 96 `[ { "type": "int17" } ]`, 97 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 98 big.NewInt(1), 99 "*big.Int", 100 "", 101 }, 102 103 { 104 `[ { "type": "address" } ]`, 105 common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"), 106 common.Address{1}, 107 "address", 108 "", 109 }, 110 { 111 `[ { "type": "bytes32" } ]`, 112 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 113 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 114 "bytes", 115 "", 116 }, 117 { 118 `[ { "type": "bytes32" } ]`, 119 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 120 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 121 "hash", 122 "", 123 }, 124 { 125 `[ { "type": "bytes32" } ]`, 126 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 127 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 128 "interface", 129 "", 130 }, 131 { 132 `[ { "type": "function" } ]`, 133 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 134 [24]byte{1}, 135 "function", 136 "", 137 }, 138 } { 139 abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 140 abi, err := JSON(strings.NewReader(abiDefinition)) 141 if err != nil { 142 t.Errorf("%d failed. %v", i, err) 143 continue 144 } 145 146 var outvar interface{} 147 switch test.outVar { 148 case "bool": 149 var v bool 150 err = abi.Unpack(&v, "method", test.marshalledOutput) 151 outvar = v 152 case "uint8": 153 var v uint8 154 err = abi.Unpack(&v, "method", test.marshalledOutput) 155 outvar = v 156 case "uint16": 157 var v uint16 158 err = abi.Unpack(&v, "method", test.marshalledOutput) 159 outvar = v 160 case "uint32": 161 var v uint32 162 err = abi.Unpack(&v, "method", test.marshalledOutput) 163 outvar = v 164 case "uint64": 165 var v uint64 166 err = abi.Unpack(&v, "method", test.marshalledOutput) 167 outvar = v 168 case "int8": 169 var v int8 170 err = abi.Unpack(&v, "method", test.marshalledOutput) 171 outvar = v 172 case "int16": 173 var v int16 174 err = abi.Unpack(&v, "method", test.marshalledOutput) 175 outvar = v 176 case "int32": 177 var v int32 178 err = abi.Unpack(&v, "method", test.marshalledOutput) 179 outvar = v 180 case "int64": 181 var v int64 182 err = abi.Unpack(&v, "method", test.marshalledOutput) 183 outvar = v 184 case "*big.Int": 185 var v *big.Int 186 err = abi.Unpack(&v, "method", test.marshalledOutput) 187 outvar = v 188 case "address": 189 var v common.Address 190 err = abi.Unpack(&v, "method", test.marshalledOutput) 191 outvar = v 192 case "bytes": 193 var v []byte 194 err = abi.Unpack(&v, "method", test.marshalledOutput) 195 outvar = v 196 case "hash": 197 var v common.Hash 198 err = abi.Unpack(&v, "method", test.marshalledOutput) 199 outvar = v.Bytes()[:] 200 case "function": 201 var v [24]byte 202 err = abi.Unpack(&v, "method", test.marshalledOutput) 203 outvar = v 204 case "interface": 205 err = abi.Unpack(&outvar, "method", test.marshalledOutput) 206 default: 207 t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar) 208 continue 209 } 210 211 if err != nil && len(test.err) == 0 { 212 t.Errorf("%d failed. Expected no err but got: %v", i, err) 213 continue 214 } 215 if err == nil && len(test.err) != 0 { 216 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 217 continue 218 } 219 if err != nil && len(test.err) != 0 && err.Error() != test.err { 220 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 221 continue 222 } 223 224 if err == nil { 225 if !reflect.DeepEqual(test.expectedOut, outvar) { 226 t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar) 227 } 228 } 229 } 230 } 231 232 func TestUnpackSetInterfaceSlice(t *testing.T) { 233 var ( 234 var1 = new(uint8) 235 var2 = new(uint8) 236 ) 237 out := []interface{}{var1, var2} 238 abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`)) 239 if err != nil { 240 t.Fatal(err) 241 } 242 marshalledReturn := append(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")...) 243 err = abi.Unpack(&out, "ints", marshalledReturn) 244 if err != nil { 245 t.Fatal(err) 246 } 247 if *var1 != 1 { 248 t.Error("expected var1 to be 1, got", *var1) 249 } 250 if *var2 != 2 { 251 t.Error("expected var2 to be 2, got", *var2) 252 } 253 254 out = []interface{}{var1} 255 err = abi.Unpack(&out, "ints", marshalledReturn) 256 257 expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)" 258 if err == nil || err.Error() != expErr { 259 t.Error("expected err:", expErr, "Got:", err) 260 } 261 } 262 263 func TestUnpackSetInterfaceArrayOutput(t *testing.T) { 264 var ( 265 var1 = new([1]uint32) 266 var2 = new([1]uint32) 267 ) 268 out := []interface{}{var1, var2} 269 abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint32[1]"}, {"type":"uint32[1]"}]}]`)) 270 if err != nil { 271 t.Fatal(err) 272 } 273 marshalledReturn := append(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")...) 274 err = abi.Unpack(&out, "ints", marshalledReturn) 275 if err != nil { 276 t.Fatal(err) 277 } 278 279 if *var1 != [1]uint32{1} { 280 t.Error("expected var1 to be [1], got", *var1) 281 } 282 if *var2 != [1]uint32{2} { 283 t.Error("expected var2 to be [2], got", *var2) 284 } 285 } 286 287 func TestMultiReturnWithStruct(t *testing.T) { 288 const definition = `[ 289 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 290 291 abi, err := JSON(strings.NewReader(definition)) 292 if err != nil { 293 t.Fatal(err) 294 } 295 296 // using buff to make the code readable 297 buff := new(bytes.Buffer) 298 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 299 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 300 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 301 stringOut := "hello" 302 buff.Write(common.RightPadBytes([]byte(stringOut), 32)) 303 304 var inter struct { 305 Int *big.Int 306 String string 307 } 308 err = abi.Unpack(&inter, "multi", buff.Bytes()) 309 if err != nil { 310 t.Error(err) 311 } 312 313 if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 { 314 t.Error("expected Int to be 1 got", inter.Int) 315 } 316 317 if inter.String != stringOut { 318 t.Error("expected String to be", stringOut, "got", inter.String) 319 } 320 321 var reversed struct { 322 String string 323 Int *big.Int 324 } 325 326 err = abi.Unpack(&reversed, "multi", buff.Bytes()) 327 if err != nil { 328 t.Error(err) 329 } 330 331 if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 { 332 t.Error("expected Int to be 1 got", reversed.Int) 333 } 334 335 if reversed.String != stringOut { 336 t.Error("expected String to be", stringOut, "got", reversed.String) 337 } 338 } 339 340 func TestMultiReturnWithSlice(t *testing.T) { 341 const definition = `[ 342 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 343 344 abi, err := JSON(strings.NewReader(definition)) 345 if err != nil { 346 t.Fatal(err) 347 } 348 349 // using buff to make the code readable 350 buff := new(bytes.Buffer) 351 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 352 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 353 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 354 stringOut := "hello" 355 buff.Write(common.RightPadBytes([]byte(stringOut), 32)) 356 357 var inter []interface{} 358 err = abi.Unpack(&inter, "multi", buff.Bytes()) 359 if err != nil { 360 t.Error(err) 361 } 362 363 if len(inter) != 2 { 364 t.Fatal("expected 2 results got", len(inter)) 365 } 366 367 if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 { 368 t.Error("expected index 0 to be 1 got", num) 369 } 370 371 if str, ok := inter[1].(string); !ok || str != stringOut { 372 t.Error("expected index 1 to be", stringOut, "got", str) 373 } 374 } 375 376 func TestMarshalArrays(t *testing.T) { 377 const definition = `[ 378 { "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 379 { "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] } 380 ]` 381 382 abi, err := JSON(strings.NewReader(definition)) 383 if err != nil { 384 t.Fatal(err) 385 } 386 387 output := common.LeftPadBytes([]byte{1}, 32) 388 389 var bytes10 [10]byte 390 err = abi.Unpack(&bytes10, "bytes32", output) 391 if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { 392 t.Error("expected error or bytes32 not be assignable to bytes10:", err) 393 } 394 395 var bytes32 [32]byte 396 err = abi.Unpack(&bytes32, "bytes32", output) 397 if err != nil { 398 t.Error("didn't expect error:", err) 399 } 400 if !bytes.Equal(bytes32[:], output) { 401 t.Error("expected bytes32[31] to be 1 got", bytes32[31]) 402 } 403 404 type ( 405 B10 [10]byte 406 B32 [32]byte 407 ) 408 409 var b10 B10 410 err = abi.Unpack(&b10, "bytes32", output) 411 if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { 412 t.Error("expected error or bytes32 not be assignable to bytes10:", err) 413 } 414 415 var b32 B32 416 err = abi.Unpack(&b32, "bytes32", output) 417 if err != nil { 418 t.Error("didn't expect error:", err) 419 } 420 if !bytes.Equal(b32[:], output) { 421 t.Error("expected bytes32[31] to be 1 got", bytes32[31]) 422 } 423 424 output[10] = 1 425 var shortAssignLong [32]byte 426 err = abi.Unpack(&shortAssignLong, "bytes10", output) 427 if err != nil { 428 t.Error("didn't expect error:", err) 429 } 430 if !bytes.Equal(output, shortAssignLong[:]) { 431 t.Errorf("expected %x to be %x", shortAssignLong, output) 432 } 433 } 434 435 func TestUnmarshal(t *testing.T) { 436 const definition = `[ 437 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 438 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 439 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 440 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 441 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 442 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 443 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 444 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 445 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 446 447 abi, err := JSON(strings.NewReader(definition)) 448 if err != nil { 449 t.Fatal(err) 450 } 451 buff := new(bytes.Buffer) 452 453 // marshal int 454 var Int *big.Int 455 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 456 if err != nil { 457 t.Error(err) 458 } 459 460 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 461 t.Error("expected Int to be 1 got", Int) 462 } 463 464 // marshal bool 465 var Bool bool 466 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 467 if err != nil { 468 t.Error(err) 469 } 470 471 if !Bool { 472 t.Error("expected Bool to be true") 473 } 474 475 // marshal dynamic bytes max length 32 476 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 477 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 478 bytesOut := common.RightPadBytes([]byte("hello"), 32) 479 buff.Write(bytesOut) 480 481 var Bytes []byte 482 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 483 if err != nil { 484 t.Error(err) 485 } 486 487 if !bytes.Equal(Bytes, bytesOut) { 488 t.Errorf("expected %x got %x", bytesOut, Bytes) 489 } 490 491 // marshall dynamic bytes max length 64 492 buff.Reset() 493 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 494 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 495 bytesOut = common.RightPadBytes([]byte("hello"), 64) 496 buff.Write(bytesOut) 497 498 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 499 if err != nil { 500 t.Error(err) 501 } 502 503 if !bytes.Equal(Bytes, bytesOut) { 504 t.Errorf("expected %x got %x", bytesOut, Bytes) 505 } 506 507 // marshall dynamic bytes max length 63 508 buff.Reset() 509 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 510 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 511 bytesOut = common.RightPadBytes([]byte("hello"), 63) 512 buff.Write(bytesOut) 513 514 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 515 if err != nil { 516 t.Error(err) 517 } 518 519 if !bytes.Equal(Bytes, bytesOut) { 520 t.Errorf("expected %x got %x", bytesOut, Bytes) 521 } 522 523 // marshal dynamic bytes output empty 524 err = abi.Unpack(&Bytes, "bytes", nil) 525 if err == nil { 526 t.Error("expected error") 527 } 528 529 // marshal dynamic bytes length 5 530 buff.Reset() 531 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 532 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 533 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 534 535 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 536 if err != nil { 537 t.Error(err) 538 } 539 540 if !bytes.Equal(Bytes, []byte("hello")) { 541 t.Errorf("expected %x got %x", bytesOut, Bytes) 542 } 543 544 // marshal dynamic bytes length 5 545 buff.Reset() 546 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 547 548 var hash common.Hash 549 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 550 if err != nil { 551 t.Error(err) 552 } 553 554 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 555 if hash != helloHash { 556 t.Errorf("Expected %x to equal %x", hash, helloHash) 557 } 558 559 // marshal error 560 buff.Reset() 561 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 562 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 563 if err == nil { 564 t.Error("expected error") 565 } 566 567 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 568 if err == nil { 569 t.Error("expected error") 570 } 571 572 // marshal mixed bytes 573 buff.Reset() 574 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 575 fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001") 576 buff.Write(fixed) 577 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 578 bytesOut = common.RightPadBytes([]byte("hello"), 32) 579 buff.Write(bytesOut) 580 581 var out []interface{} 582 err = abi.Unpack(&out, "mixedBytes", buff.Bytes()) 583 if err != nil { 584 t.Fatal("didn't expect error:", err) 585 } 586 587 if !bytes.Equal(bytesOut, out[0].([]byte)) { 588 t.Errorf("expected %x, got %x", bytesOut, out[0]) 589 } 590 591 if !bytes.Equal(fixed, out[1].([]byte)) { 592 t.Errorf("expected %x, got %x", fixed, out[1]) 593 } 594 595 buff.Reset() 596 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 597 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 598 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 599 // marshal int array 600 var intArray [3]*big.Int 601 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 602 if err != nil { 603 t.Error(err) 604 } 605 var testAgainstIntArray [3]*big.Int 606 testAgainstIntArray[0] = big.NewInt(1) 607 testAgainstIntArray[1] = big.NewInt(2) 608 testAgainstIntArray[2] = big.NewInt(3) 609 610 for i, Int := range intArray { 611 if Int.Cmp(testAgainstIntArray[i]) != 0 { 612 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 613 } 614 } 615 // marshal address slice 616 buff.Reset() 617 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset 618 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 619 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 620 621 var outAddr []common.Address 622 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 623 if err != nil { 624 t.Fatal("didn't expect error:", err) 625 } 626 627 if len(outAddr) != 1 { 628 t.Fatal("expected 1 item, got", len(outAddr)) 629 } 630 631 if outAddr[0] != (common.Address{1}) { 632 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 633 } 634 635 // marshal multiple address slice 636 buff.Reset() 637 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset 638 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset 639 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 640 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 641 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size 642 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 643 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 644 645 var outAddrStruct struct { 646 A []common.Address 647 B []common.Address 648 } 649 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 650 if err != nil { 651 t.Fatal("didn't expect error:", err) 652 } 653 654 if len(outAddrStruct.A) != 1 { 655 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 656 } 657 658 if outAddrStruct.A[0] != (common.Address{1}) { 659 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 660 } 661 662 if len(outAddrStruct.B) != 2 { 663 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 664 } 665 666 if outAddrStruct.B[0] != (common.Address{2}) { 667 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 668 } 669 if outAddrStruct.B[1] != (common.Address{3}) { 670 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 671 } 672 673 // marshal invalid address slice 674 buff.Reset() 675 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 676 677 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 678 if err == nil { 679 t.Fatal("expected error:", err) 680 } 681 }