github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/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-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "math/big" 24 "reflect" 25 "strconv" 26 "strings" 27 "testing" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/stretchr/testify/require" 31 ) 32 33 type unpackTest struct { 34 def string // ABI definition JSON 35 enc string // evm return data 36 want interface{} // the expected output 37 err string // empty or error if expected 38 } 39 40 func (test unpackTest) checkError(err error) error { 41 if err != nil { 42 if len(test.err) == 0 { 43 return fmt.Errorf("expected no err but got: %v", err) 44 } else if err.Error() != test.err { 45 return fmt.Errorf("expected err: '%v' got err: %q", test.err, err) 46 } 47 } else if len(test.err) > 0 { 48 return fmt.Errorf("expected err: %v but got none", test.err) 49 } 50 return nil 51 } 52 53 var unpackTests = []unpackTest{ 54 { 55 def: `[{ "type": "bool" }]`, 56 enc: "0000000000000000000000000000000000000000000000000000000000000001", 57 want: true, 58 }, 59 { 60 def: `[{"type": "uint32"}]`, 61 enc: "0000000000000000000000000000000000000000000000000000000000000001", 62 want: uint32(1), 63 }, 64 { 65 def: `[{"type": "uint32"}]`, 66 enc: "0000000000000000000000000000000000000000000000000000000000000001", 67 want: uint16(0), 68 err: "abi: cannot unmarshal uint32 in to uint16", 69 }, 70 { 71 def: `[{"type": "uint17"}]`, 72 enc: "0000000000000000000000000000000000000000000000000000000000000001", 73 want: uint16(0), 74 err: "abi: cannot unmarshal *big.Int in to uint16", 75 }, 76 { 77 def: `[{"type": "uint17"}]`, 78 enc: "0000000000000000000000000000000000000000000000000000000000000001", 79 want: big.NewInt(1), 80 }, 81 { 82 def: `[{"type": "int32"}]`, 83 enc: "0000000000000000000000000000000000000000000000000000000000000001", 84 want: int32(1), 85 }, 86 { 87 def: `[{"type": "int32"}]`, 88 enc: "0000000000000000000000000000000000000000000000000000000000000001", 89 want: int16(0), 90 err: "abi: cannot unmarshal int32 in to int16", 91 }, 92 { 93 def: `[{"type": "int17"}]`, 94 enc: "0000000000000000000000000000000000000000000000000000000000000001", 95 want: int16(0), 96 err: "abi: cannot unmarshal *big.Int in to int16", 97 }, 98 { 99 def: `[{"type": "int17"}]`, 100 enc: "0000000000000000000000000000000000000000000000000000000000000001", 101 want: big.NewInt(1), 102 }, 103 { 104 def: `[{"type": "address"}]`, 105 enc: "0000000000000000000000000100000000000000000000000000000000000000", 106 want: common.Address{1}, 107 }, 108 { 109 def: `[{"type": "bytes32"}]`, 110 enc: "0100000000000000000000000000000000000000000000000000000000000000", 111 want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 112 }, 113 { 114 def: `[{"type": "bytes"}]`, 115 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 116 want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 117 }, 118 { 119 def: `[{"type": "bytes"}]`, 120 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 121 want: [32]byte{}, 122 err: "abi: cannot unmarshal []uint8 in to [32]uint8", 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: `[{"type": "bytes32"}]`, 132 enc: "0100000000000000000000000000000000000000000000000000000000000000", 133 want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"), 134 }, 135 { 136 def: `[{"type": "function"}]`, 137 enc: "0100000000000000000000000000000000000000000000000000000000000000", 138 want: [24]byte{1}, 139 }, 140 // slices 141 { 142 def: `[{"type": "uint8[]"}]`, 143 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 144 want: []uint8{1, 2}, 145 }, 146 { 147 def: `[{"type": "uint8[2]"}]`, 148 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 149 want: [2]uint8{1, 2}, 150 }, 151 // multi dimensional, if these pass, all types that don't require length prefix should pass 152 { 153 def: `[{"type": "uint8[][]"}]`, 154 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 155 want: [][]uint8{{1, 2}, {1, 2}}, 156 }, 157 { 158 def: `[{"type": "uint8[2][2]"}]`, 159 enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 160 want: [2][2]uint8{{1, 2}, {1, 2}}, 161 }, 162 { 163 def: `[{"type": "uint8[][2]"}]`, 164 enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", 165 want: [2][]uint8{{1}, {1}}, 166 }, 167 { 168 def: `[{"type": "uint8[2][]"}]`, 169 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 170 want: [][2]uint8{{1, 2}}, 171 }, 172 { 173 def: `[{"type": "uint16[]"}]`, 174 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 175 want: []uint16{1, 2}, 176 }, 177 { 178 def: `[{"type": "uint16[2]"}]`, 179 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 180 want: [2]uint16{1, 2}, 181 }, 182 { 183 def: `[{"type": "uint32[]"}]`, 184 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 185 want: []uint32{1, 2}, 186 }, 187 { 188 def: `[{"type": "uint32[2]"}]`, 189 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 190 want: [2]uint32{1, 2}, 191 }, 192 { 193 def: `[{"type": "uint64[]"}]`, 194 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 195 want: []uint64{1, 2}, 196 }, 197 { 198 def: `[{"type": "uint64[2]"}]`, 199 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 200 want: [2]uint64{1, 2}, 201 }, 202 { 203 def: `[{"type": "uint256[]"}]`, 204 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 205 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 206 }, 207 { 208 def: `[{"type": "uint256[3]"}]`, 209 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 210 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 211 }, 212 { 213 def: `[{"type": "int8[]"}]`, 214 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 215 want: []int8{1, 2}, 216 }, 217 { 218 def: `[{"type": "int8[2]"}]`, 219 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 220 want: [2]int8{1, 2}, 221 }, 222 { 223 def: `[{"type": "int16[]"}]`, 224 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 225 want: []int16{1, 2}, 226 }, 227 { 228 def: `[{"type": "int16[2]"}]`, 229 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 230 want: [2]int16{1, 2}, 231 }, 232 { 233 def: `[{"type": "int32[]"}]`, 234 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 235 want: []int32{1, 2}, 236 }, 237 { 238 def: `[{"type": "int32[2]"}]`, 239 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 240 want: [2]int32{1, 2}, 241 }, 242 { 243 def: `[{"type": "int64[]"}]`, 244 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 245 want: []int64{1, 2}, 246 }, 247 { 248 def: `[{"type": "int64[2]"}]`, 249 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 250 want: [2]int64{1, 2}, 251 }, 252 { 253 def: `[{"type": "int256[]"}]`, 254 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 255 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 256 }, 257 { 258 def: `[{"type": "int256[3]"}]`, 259 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 260 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 261 }, 262 } 263 264 func TestUnpack(t *testing.T) { 265 for i, test := range unpackTests { 266 t.Run(strconv.Itoa(i), func(t *testing.T) { 267 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 268 abi, err := JSON(strings.NewReader(def)) 269 if err != nil { 270 t.Fatalf("invalid ABI definition %s: %v", def, err) 271 } 272 encb, err := hex.DecodeString(test.enc) 273 if err != nil { 274 t.Fatalf("invalid hex: %s" + test.enc) 275 } 276 outptr := reflect.New(reflect.TypeOf(test.want)) 277 err = abi.Unpack(outptr.Interface(), "method", encb) 278 if err := test.checkError(err); err != nil { 279 t.Errorf("test %d (%v) failed: %v", i, test.def, err) 280 return 281 } 282 out := outptr.Elem().Interface() 283 if !reflect.DeepEqual(test.want, out) { 284 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) 285 } 286 }) 287 } 288 } 289 290 type methodMultiOutput struct { 291 Int *big.Int 292 String string 293 } 294 295 func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { 296 const definition = `[ 297 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 298 var expected = methodMultiOutput{big.NewInt(1), "hello"} 299 300 abi, err := JSON(strings.NewReader(definition)) 301 require.NoError(err) 302 // using buff to make the code readable 303 buff := new(bytes.Buffer) 304 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 305 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 306 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 307 buff.Write(common.RightPadBytes([]byte(expected.String), 32)) 308 return abi, buff.Bytes(), expected 309 } 310 311 func TestMethodMultiReturn(t *testing.T) { 312 type reversed struct { 313 String string 314 Int *big.Int 315 } 316 317 abi, data, expected := methodMultiReturn(require.New(t)) 318 bigint := new(big.Int) 319 var testCases = []struct { 320 dest interface{} 321 expected interface{} 322 error string 323 name string 324 }{{ 325 &methodMultiOutput{}, 326 &expected, 327 "", 328 "Can unpack into structure", 329 }, { 330 &reversed{}, 331 &reversed{expected.String, expected.Int}, 332 "", 333 "Can unpack into reversed structure", 334 }, { 335 &[]interface{}{&bigint, new(string)}, 336 &[]interface{}{&expected.Int, &expected.String}, 337 "", 338 "Can unpack into a slice", 339 }, { 340 &[2]interface{}{&bigint, new(string)}, 341 &[2]interface{}{&expected.Int, &expected.String}, 342 "", 343 "Can unpack into an array", 344 }, { 345 &[]interface{}{new(int), new(int)}, 346 &[]interface{}{&expected.Int, &expected.String}, 347 "abi: cannot unmarshal *big.Int in to int", 348 "Can not unpack into a slice with wrong types", 349 }, { 350 &[]interface{}{new(int)}, 351 &[]interface{}{}, 352 "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", 353 "Can not unpack into a slice with wrong types", 354 }} 355 for _, tc := range testCases { 356 tc := tc 357 t.Run(tc.name, func(t *testing.T) { 358 require := require.New(t) 359 err := abi.Unpack(tc.dest, "multi", data) 360 if tc.error == "" { 361 require.Nil(err, "Should be able to unpack method outputs.") 362 require.Equal(tc.expected, tc.dest) 363 } else { 364 require.EqualError(err, tc.error) 365 } 366 }) 367 } 368 } 369 370 func TestMultiReturnWithArray(t *testing.T) { 371 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` 372 abi, err := JSON(strings.NewReader(definition)) 373 if err != nil { 374 t.Fatal(err) 375 } 376 buff := new(bytes.Buffer) 377 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) 378 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 379 380 ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} 381 ret2, ret2Exp := new(uint64), uint64(8) 382 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 383 t.Fatal(err) 384 } 385 if !reflect.DeepEqual(*ret1, ret1Exp) { 386 t.Error("array result", *ret1, "!= Expected", ret1Exp) 387 } 388 if *ret2 != ret2Exp { 389 t.Error("int result", *ret2, "!= Expected", ret2Exp) 390 } 391 } 392 393 func TestUnmarshal(t *testing.T) { 394 const definition = `[ 395 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 396 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 397 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 398 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 399 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 400 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 401 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 402 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 403 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 404 405 abi, err := JSON(strings.NewReader(definition)) 406 if err != nil { 407 t.Fatal(err) 408 } 409 buff := new(bytes.Buffer) 410 411 // marshall mixed bytes (mixedBytes) 412 p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") 413 p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") 414 mixedBytes := []interface{}{&p0, &p1} 415 416 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 417 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) 418 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) 419 buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) 420 421 err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes()) 422 if err != nil { 423 t.Error(err) 424 } else { 425 if !bytes.Equal(p0, p0Exp) { 426 t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) 427 } 428 429 if !bytes.Equal(p1[:], p1Exp) { 430 t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) 431 } 432 } 433 434 // marshal int 435 var Int *big.Int 436 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 437 if err != nil { 438 t.Error(err) 439 } 440 441 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 442 t.Error("expected Int to be 1 got", Int) 443 } 444 445 // marshal bool 446 var Bool bool 447 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 448 if err != nil { 449 t.Error(err) 450 } 451 452 if !Bool { 453 t.Error("expected Bool to be true") 454 } 455 456 // marshal dynamic bytes max length 32 457 buff.Reset() 458 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 459 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 460 bytesOut := common.RightPadBytes([]byte("hello"), 32) 461 buff.Write(bytesOut) 462 463 var Bytes []byte 464 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 465 if err != nil { 466 t.Error(err) 467 } 468 469 if !bytes.Equal(Bytes, bytesOut) { 470 t.Errorf("expected %x got %x", bytesOut, Bytes) 471 } 472 473 // marshall dynamic bytes max length 64 474 buff.Reset() 475 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 476 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 477 bytesOut = common.RightPadBytes([]byte("hello"), 64) 478 buff.Write(bytesOut) 479 480 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 481 if err != nil { 482 t.Error(err) 483 } 484 485 if !bytes.Equal(Bytes, bytesOut) { 486 t.Errorf("expected %x got %x", bytesOut, Bytes) 487 } 488 489 // marshall dynamic bytes max length 64 490 buff.Reset() 491 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 492 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 493 bytesOut = common.RightPadBytes([]byte("hello"), 64) 494 buff.Write(bytesOut) 495 496 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 497 if err != nil { 498 t.Error(err) 499 } 500 501 if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { 502 t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) 503 } 504 505 // marshal dynamic bytes output empty 506 err = abi.Unpack(&Bytes, "bytes", nil) 507 if err == nil { 508 t.Error("expected error") 509 } 510 511 // marshal dynamic bytes length 5 512 buff.Reset() 513 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 514 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 515 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 516 517 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 518 if err != nil { 519 t.Error(err) 520 } 521 522 if !bytes.Equal(Bytes, []byte("hello")) { 523 t.Errorf("expected %x got %x", bytesOut, Bytes) 524 } 525 526 // marshal dynamic bytes length 5 527 buff.Reset() 528 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 529 530 var hash common.Hash 531 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 532 if err != nil { 533 t.Error(err) 534 } 535 536 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 537 if hash != helloHash { 538 t.Errorf("Expected %x to equal %x", hash, helloHash) 539 } 540 541 // marshal error 542 buff.Reset() 543 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 544 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 545 if err == nil { 546 t.Error("expected error") 547 } 548 549 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 550 if err == nil { 551 t.Error("expected error") 552 } 553 554 buff.Reset() 555 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 556 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 557 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 558 // marshal int array 559 var intArray [3]*big.Int 560 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 561 if err != nil { 562 t.Error(err) 563 } 564 var testAgainstIntArray [3]*big.Int 565 testAgainstIntArray[0] = big.NewInt(1) 566 testAgainstIntArray[1] = big.NewInt(2) 567 testAgainstIntArray[2] = big.NewInt(3) 568 569 for i, Int := range intArray { 570 if Int.Cmp(testAgainstIntArray[i]) != 0 { 571 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 572 } 573 } 574 // marshal address slice 575 buff.Reset() 576 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset 577 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 578 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 579 580 var outAddr []common.Address 581 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 582 if err != nil { 583 t.Fatal("didn't expect error:", err) 584 } 585 586 if len(outAddr) != 1 { 587 t.Fatal("expected 1 item, got", len(outAddr)) 588 } 589 590 if outAddr[0] != (common.Address{1}) { 591 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 592 } 593 594 // marshal multiple address slice 595 buff.Reset() 596 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset 597 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset 598 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 599 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 600 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size 601 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 602 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 603 604 var outAddrStruct struct { 605 A []common.Address 606 B []common.Address 607 } 608 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 609 if err != nil { 610 t.Fatal("didn't expect error:", err) 611 } 612 613 if len(outAddrStruct.A) != 1 { 614 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 615 } 616 617 if outAddrStruct.A[0] != (common.Address{1}) { 618 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 619 } 620 621 if len(outAddrStruct.B) != 2 { 622 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 623 } 624 625 if outAddrStruct.B[0] != (common.Address{2}) { 626 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 627 } 628 if outAddrStruct.B[1] != (common.Address{3}) { 629 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 630 } 631 632 // marshal invalid address slice 633 buff.Reset() 634 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 635 636 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 637 if err == nil { 638 t.Fatal("expected error:", err) 639 } 640 }