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