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