github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/accounts/abi/pack_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 "math" 22 "math/big" 23 "reflect" 24 "strings" 25 "testing" 26 27 "github.com/insight-chain/inb-go/common" 28 ) 29 30 func TestPack(t *testing.T) { 31 for i, test := range []struct { 32 typ string 33 34 input interface{} 35 output []byte 36 }{ 37 { 38 "uint8", 39 uint8(2), 40 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 41 }, 42 { 43 "uint8[]", 44 []uint8{1, 2}, 45 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 46 }, 47 { 48 "uint16", 49 uint16(2), 50 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 51 }, 52 { 53 "uint16[]", 54 []uint16{1, 2}, 55 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 56 }, 57 { 58 "uint32", 59 uint32(2), 60 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 61 }, 62 { 63 "uint32[]", 64 []uint32{1, 2}, 65 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 66 }, 67 { 68 "uint64", 69 uint64(2), 70 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 71 }, 72 { 73 "uint64[]", 74 []uint64{1, 2}, 75 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 76 }, 77 { 78 "uint256", 79 big.NewInt(2), 80 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 81 }, 82 { 83 "uint256[]", 84 []*big.Int{big.NewInt(1), big.NewInt(2)}, 85 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 86 }, 87 { 88 "int8", 89 int8(2), 90 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 91 }, 92 { 93 "int8[]", 94 []int8{1, 2}, 95 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 96 }, 97 { 98 "int16", 99 int16(2), 100 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 101 }, 102 { 103 "int16[]", 104 []int16{1, 2}, 105 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 106 }, 107 { 108 "int32", 109 int32(2), 110 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 111 }, 112 { 113 "int32[]", 114 []int32{1, 2}, 115 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 116 }, 117 { 118 "int64", 119 int64(2), 120 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 121 }, 122 { 123 "int64[]", 124 []int64{1, 2}, 125 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 126 }, 127 { 128 "int256", 129 big.NewInt(2), 130 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 131 }, 132 { 133 "int256[]", 134 []*big.Int{big.NewInt(1), big.NewInt(2)}, 135 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 136 }, 137 { 138 "bytes1", 139 [1]byte{1}, 140 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 141 }, 142 { 143 "bytes2", 144 [2]byte{1}, 145 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 146 }, 147 { 148 "bytes3", 149 [3]byte{1}, 150 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 151 }, 152 { 153 "bytes4", 154 [4]byte{1}, 155 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 156 }, 157 { 158 "bytes5", 159 [5]byte{1}, 160 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 161 }, 162 { 163 "bytes6", 164 [6]byte{1}, 165 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 166 }, 167 { 168 "bytes7", 169 [7]byte{1}, 170 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 171 }, 172 { 173 "bytes8", 174 [8]byte{1}, 175 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 176 }, 177 { 178 "bytes9", 179 [9]byte{1}, 180 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 181 }, 182 { 183 "bytes10", 184 [10]byte{1}, 185 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 186 }, 187 { 188 "bytes11", 189 [11]byte{1}, 190 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 191 }, 192 { 193 "bytes12", 194 [12]byte{1}, 195 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 196 }, 197 { 198 "bytes13", 199 [13]byte{1}, 200 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 201 }, 202 { 203 "bytes14", 204 [14]byte{1}, 205 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 206 }, 207 { 208 "bytes15", 209 [15]byte{1}, 210 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 211 }, 212 { 213 "bytes16", 214 [16]byte{1}, 215 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 216 }, 217 { 218 "bytes17", 219 [17]byte{1}, 220 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 221 }, 222 { 223 "bytes18", 224 [18]byte{1}, 225 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 226 }, 227 { 228 "bytes19", 229 [19]byte{1}, 230 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 231 }, 232 { 233 "bytes20", 234 [20]byte{1}, 235 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 236 }, 237 { 238 "bytes21", 239 [21]byte{1}, 240 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 241 }, 242 { 243 "bytes22", 244 [22]byte{1}, 245 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 246 }, 247 { 248 "bytes23", 249 [23]byte{1}, 250 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 251 }, 252 { 253 "bytes24", 254 [24]byte{1}, 255 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 256 }, 257 { 258 "bytes24", 259 [24]byte{1}, 260 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 261 }, 262 { 263 "bytes25", 264 [25]byte{1}, 265 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 266 }, 267 { 268 "bytes26", 269 [26]byte{1}, 270 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 271 }, 272 { 273 "bytes27", 274 [27]byte{1}, 275 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 276 }, 277 { 278 "bytes28", 279 [28]byte{1}, 280 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 281 }, 282 { 283 "bytes29", 284 [29]byte{1}, 285 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 286 }, 287 { 288 "bytes30", 289 [30]byte{1}, 290 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 291 }, 292 { 293 "bytes31", 294 [31]byte{1}, 295 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 296 }, 297 { 298 "bytes32", 299 [32]byte{1}, 300 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 301 }, 302 { 303 "uint32[2][3][4]", 304 [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, 305 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"), 306 }, 307 { 308 "address[]", 309 []common.Address{{1}, {2}}, 310 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), 311 }, 312 { 313 "bytes32[]", 314 []common.Hash{{1}, {2}}, 315 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), 316 }, 317 { 318 "function", 319 [24]byte{1}, 320 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 321 }, 322 { 323 "string", 324 "foobar", 325 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), 326 }, 327 { 328 "string[]", 329 []string{"hello", "foobar"}, 330 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 331 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 332 "0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1 333 "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5 334 "68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0] 335 "0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6 336 "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] 337 }, 338 { 339 "string[2]", 340 []string{"hello", "foobar"}, 341 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0 342 "0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1 343 "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5 344 "68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0] 345 "0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6 346 "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] 347 }, 348 { 349 "bytes32[][]", 350 [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, 351 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 352 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 353 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 354 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 355 "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] 356 "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] 357 "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 358 "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] 359 "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] 360 "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] 361 }, 362 363 { 364 "bytes32[][2]", 365 [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, 366 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 367 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 368 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 369 "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] 370 "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] 371 "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 372 "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] 373 "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] 374 "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] 375 }, 376 377 { 378 "bytes32[3][2]", 379 [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, 380 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] 381 "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] 382 "0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2] 383 "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] 384 "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] 385 "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] 386 }, 387 } { 388 typ, err := NewType(test.typ) 389 if err != nil { 390 t.Fatalf("%v failed. Unexpected parse error: %v", i, err) 391 } 392 393 output, err := typ.pack(reflect.ValueOf(test.input)) 394 if err != nil { 395 t.Fatalf("%v failed. Unexpected pack error: %v", i, err) 396 } 397 398 if !bytes.Equal(output, test.output) { 399 t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i, typ.String(), test.output, output) 400 } 401 } 402 } 403 404 func TestMethodPack(t *testing.T) { 405 abi, err := JSON(strings.NewReader(jsondata2)) 406 if err != nil { 407 t.Fatal(err) 408 } 409 410 sig := abi.Methods["slice"].Id() 411 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 412 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 413 414 packed, err := abi.Pack("slice", []uint32{1, 2}) 415 if err != nil { 416 t.Error(err) 417 } 418 419 if !bytes.Equal(packed, sig) { 420 t.Errorf("expected %x got %x", sig, packed) 421 } 422 423 var addrA, addrB = common.Address{1}, common.Address{2} 424 sig = abi.Methods["sliceAddress"].Id() 425 sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) 426 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 427 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 428 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 429 430 packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) 431 if err != nil { 432 t.Fatal(err) 433 } 434 if !bytes.Equal(packed, sig) { 435 t.Errorf("expected %x got %x", sig, packed) 436 } 437 438 var addrC, addrD = common.Address{3}, common.Address{4} 439 sig = abi.Methods["sliceMultiAddress"].Id() 440 sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) 441 sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) 442 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 443 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 444 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 445 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 446 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 447 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 448 449 packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) 450 if err != nil { 451 t.Fatal(err) 452 } 453 if !bytes.Equal(packed, sig) { 454 t.Errorf("expected %x got %x", sig, packed) 455 } 456 457 sig = abi.Methods["slice256"].Id() 458 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 459 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 460 461 packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) 462 if err != nil { 463 t.Error(err) 464 } 465 466 if !bytes.Equal(packed, sig) { 467 t.Errorf("expected %x got %x", sig, packed) 468 } 469 } 470 471 func TestPackNumber(t *testing.T) { 472 tests := []struct { 473 value reflect.Value 474 packed []byte 475 }{ 476 // Protocol limits 477 {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, 478 {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, 479 {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, 480 481 // Type corner cases 482 {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, 483 {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, 484 {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, 485 {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, 486 487 {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, 488 {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, 489 {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, 490 {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, 491 492 {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, 493 {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, 494 {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, 495 {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, 496 } 497 for i, tt := range tests { 498 packed := packNum(tt.value) 499 if !bytes.Equal(packed, tt.packed) { 500 t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) 501 } 502 } 503 }