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