github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/accounts/abi/unpack_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "math/big" 24 "reflect" 25 "strconv" 26 "strings" 27 "testing" 28 29 "github.com/vantum/vantum/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 // struct outputs 263 { 264 def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`, 265 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 266 want: struct { 267 Int1 *big.Int 268 Int2 *big.Int 269 }{big.NewInt(1), big.NewInt(2)}, 270 }, 271 { 272 def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, 273 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 274 want: struct { 275 Int1 *big.Int 276 Int2 *big.Int 277 }{}, 278 err: "abi: multiple outputs mapping to the same struct field 'Int'", 279 }, 280 { 281 def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`, 282 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 283 want: struct { 284 Int1 *big.Int 285 Int2 *big.Int 286 }{}, 287 err: "abi: multiple outputs mapping to the same struct field 'Int'", 288 }, 289 { 290 def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`, 291 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 292 want: struct { 293 Int1 *big.Int 294 Int2 *big.Int 295 }{}, 296 err: "abi: multiple outputs mapping to the same struct field 'Int'", 297 }, 298 { 299 def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`, 300 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 301 want: struct { 302 Int1 *big.Int 303 Int2 *big.Int 304 }{}, 305 err: "abi: purely underscored output cannot unpack to struct", 306 }, 307 } 308 309 func TestUnpack(t *testing.T) { 310 for i, test := range unpackTests { 311 t.Run(strconv.Itoa(i), func(t *testing.T) { 312 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 313 abi, err := JSON(strings.NewReader(def)) 314 if err != nil { 315 t.Fatalf("invalid ABI definition %s: %v", def, err) 316 } 317 encb, err := hex.DecodeString(test.enc) 318 if err != nil { 319 t.Fatalf("invalid hex: %s" + test.enc) 320 } 321 outptr := reflect.New(reflect.TypeOf(test.want)) 322 err = abi.Unpack(outptr.Interface(), "method", encb) 323 if err := test.checkError(err); err != nil { 324 t.Errorf("test %d (%v) failed: %v", i, test.def, err) 325 return 326 } 327 out := outptr.Elem().Interface() 328 if !reflect.DeepEqual(test.want, out) { 329 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) 330 } 331 }) 332 } 333 } 334 335 type methodMultiOutput struct { 336 Int *big.Int 337 String string 338 } 339 340 func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { 341 const definition = `[ 342 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 343 var expected = methodMultiOutput{big.NewInt(1), "hello"} 344 345 abi, err := JSON(strings.NewReader(definition)) 346 require.NoError(err) 347 // using buff to make the code readable 348 buff := new(bytes.Buffer) 349 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 350 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 351 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 352 buff.Write(common.RightPadBytes([]byte(expected.String), 32)) 353 return abi, buff.Bytes(), expected 354 } 355 356 func TestMethodMultiReturn(t *testing.T) { 357 type reversed struct { 358 String string 359 Int *big.Int 360 } 361 362 abi, data, expected := methodMultiReturn(require.New(t)) 363 bigint := new(big.Int) 364 var testCases = []struct { 365 dest interface{} 366 expected interface{} 367 error string 368 name string 369 }{{ 370 &methodMultiOutput{}, 371 &expected, 372 "", 373 "Can unpack into structure", 374 }, { 375 &reversed{}, 376 &reversed{expected.String, expected.Int}, 377 "", 378 "Can unpack into reversed structure", 379 }, { 380 &[]interface{}{&bigint, new(string)}, 381 &[]interface{}{&expected.Int, &expected.String}, 382 "", 383 "Can unpack into a slice", 384 }, { 385 &[2]interface{}{&bigint, new(string)}, 386 &[2]interface{}{&expected.Int, &expected.String}, 387 "", 388 "Can unpack into an array", 389 }, { 390 &[]interface{}{new(int), new(int)}, 391 &[]interface{}{&expected.Int, &expected.String}, 392 "abi: cannot unmarshal *big.Int in to int", 393 "Can not unpack into a slice with wrong types", 394 }, { 395 &[]interface{}{new(int)}, 396 &[]interface{}{}, 397 "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", 398 "Can not unpack into a slice with wrong types", 399 }} 400 for _, tc := range testCases { 401 tc := tc 402 t.Run(tc.name, func(t *testing.T) { 403 require := require.New(t) 404 err := abi.Unpack(tc.dest, "multi", data) 405 if tc.error == "" { 406 require.Nil(err, "Should be able to unpack method outputs.") 407 require.Equal(tc.expected, tc.dest) 408 } else { 409 require.EqualError(err, tc.error) 410 } 411 }) 412 } 413 } 414 415 func TestMultiReturnWithArray(t *testing.T) { 416 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` 417 abi, err := JSON(strings.NewReader(definition)) 418 if err != nil { 419 t.Fatal(err) 420 } 421 buff := new(bytes.Buffer) 422 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) 423 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 424 425 ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} 426 ret2, ret2Exp := new(uint64), uint64(8) 427 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 428 t.Fatal(err) 429 } 430 if !reflect.DeepEqual(*ret1, ret1Exp) { 431 t.Error("array result", *ret1, "!= Expected", ret1Exp) 432 } 433 if *ret2 != ret2Exp { 434 t.Error("int result", *ret2, "!= Expected", ret2Exp) 435 } 436 } 437 438 func TestUnmarshal(t *testing.T) { 439 const definition = `[ 440 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 441 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 442 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 443 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 444 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 445 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 446 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 447 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 448 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 449 450 abi, err := JSON(strings.NewReader(definition)) 451 if err != nil { 452 t.Fatal(err) 453 } 454 buff := new(bytes.Buffer) 455 456 // marshall mixed bytes (mixedBytes) 457 p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") 458 p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") 459 mixedBytes := []interface{}{&p0, &p1} 460 461 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 462 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) 463 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) 464 buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) 465 466 err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes()) 467 if err != nil { 468 t.Error(err) 469 } else { 470 if !bytes.Equal(p0, p0Exp) { 471 t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) 472 } 473 474 if !bytes.Equal(p1[:], p1Exp) { 475 t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) 476 } 477 } 478 479 // marshal int 480 var Int *big.Int 481 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 482 if err != nil { 483 t.Error(err) 484 } 485 486 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 487 t.Error("expected Int to be 1 got", Int) 488 } 489 490 // marshal bool 491 var Bool bool 492 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 493 if err != nil { 494 t.Error(err) 495 } 496 497 if !Bool { 498 t.Error("expected Bool to be true") 499 } 500 501 // marshal dynamic bytes max length 32 502 buff.Reset() 503 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 504 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 505 bytesOut := common.RightPadBytes([]byte("hello"), 32) 506 buff.Write(bytesOut) 507 508 var Bytes []byte 509 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 510 if err != nil { 511 t.Error(err) 512 } 513 514 if !bytes.Equal(Bytes, bytesOut) { 515 t.Errorf("expected %x got %x", bytesOut, Bytes) 516 } 517 518 // marshall dynamic bytes max length 64 519 buff.Reset() 520 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 521 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 522 bytesOut = common.RightPadBytes([]byte("hello"), 64) 523 buff.Write(bytesOut) 524 525 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 526 if err != nil { 527 t.Error(err) 528 } 529 530 if !bytes.Equal(Bytes, bytesOut) { 531 t.Errorf("expected %x got %x", bytesOut, Bytes) 532 } 533 534 // marshall dynamic bytes max length 64 535 buff.Reset() 536 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 537 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 538 bytesOut = common.RightPadBytes([]byte("hello"), 64) 539 buff.Write(bytesOut) 540 541 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 542 if err != nil { 543 t.Error(err) 544 } 545 546 if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { 547 t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) 548 } 549 550 // marshal dynamic bytes output empty 551 err = abi.Unpack(&Bytes, "bytes", nil) 552 if err == nil { 553 t.Error("expected error") 554 } 555 556 // marshal dynamic bytes length 5 557 buff.Reset() 558 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 559 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 560 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 561 562 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 563 if err != nil { 564 t.Error(err) 565 } 566 567 if !bytes.Equal(Bytes, []byte("hello")) { 568 t.Errorf("expected %x got %x", bytesOut, Bytes) 569 } 570 571 // marshal dynamic bytes length 5 572 buff.Reset() 573 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 574 575 var hash common.Hash 576 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 577 if err != nil { 578 t.Error(err) 579 } 580 581 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 582 if hash != helloHash { 583 t.Errorf("Expected %x to equal %x", hash, helloHash) 584 } 585 586 // marshal error 587 buff.Reset() 588 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 589 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 590 if err == nil { 591 t.Error("expected error") 592 } 593 594 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 595 if err == nil { 596 t.Error("expected error") 597 } 598 599 buff.Reset() 600 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 601 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 602 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 603 // marshal int array 604 var intArray [3]*big.Int 605 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 606 if err != nil { 607 t.Error(err) 608 } 609 var testAgainstIntArray [3]*big.Int 610 testAgainstIntArray[0] = big.NewInt(1) 611 testAgainstIntArray[1] = big.NewInt(2) 612 testAgainstIntArray[2] = big.NewInt(3) 613 614 for i, Int := range intArray { 615 if Int.Cmp(testAgainstIntArray[i]) != 0 { 616 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 617 } 618 } 619 // marshal address slice 620 buff.Reset() 621 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset 622 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 623 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 624 625 var outAddr []common.Address 626 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 627 if err != nil { 628 t.Fatal("didn't expect error:", err) 629 } 630 631 if len(outAddr) != 1 { 632 t.Fatal("expected 1 item, got", len(outAddr)) 633 } 634 635 if outAddr[0] != (common.Address{1}) { 636 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 637 } 638 639 // marshal multiple address slice 640 buff.Reset() 641 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset 642 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset 643 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size 644 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 645 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size 646 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 647 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 648 649 var outAddrStruct struct { 650 A []common.Address 651 B []common.Address 652 } 653 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 654 if err != nil { 655 t.Fatal("didn't expect error:", err) 656 } 657 658 if len(outAddrStruct.A) != 1 { 659 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 660 } 661 662 if outAddrStruct.A[0] != (common.Address{1}) { 663 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 664 } 665 666 if len(outAddrStruct.B) != 2 { 667 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 668 } 669 670 if outAddrStruct.B[0] != (common.Address{2}) { 671 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 672 } 673 if outAddrStruct.B[1] != (common.Address{3}) { 674 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 675 } 676 677 // marshal invalid address slice 678 buff.Reset() 679 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 680 681 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 682 if err == nil { 683 t.Fatal("expected error:", err) 684 } 685 }