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