github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/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/vantum/vantum/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 "address[]", 304 []common.Address{{1}, {2}}, 305 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), 306 }, 307 { 308 "bytes32[]", 309 []common.Hash{{1}, {2}}, 310 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), 311 }, 312 { 313 "function", 314 [24]byte{1}, 315 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 316 }, 317 { 318 "string", 319 "foobar", 320 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), 321 }, 322 } { 323 typ, err := NewType(test.typ) 324 if err != nil { 325 t.Fatalf("%v failed. Unexpected parse error: %v", i, err) 326 } 327 328 output, err := typ.pack(reflect.ValueOf(test.input)) 329 if err != nil { 330 t.Fatalf("%v failed. Unexpected pack error: %v", i, err) 331 } 332 333 if !bytes.Equal(output, test.output) { 334 t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output) 335 } 336 } 337 } 338 339 func TestMethodPack(t *testing.T) { 340 abi, err := JSON(strings.NewReader(jsondata2)) 341 if err != nil { 342 t.Fatal(err) 343 } 344 345 sig := abi.Methods["slice"].Id() 346 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 347 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 348 349 packed, err := abi.Pack("slice", []uint32{1, 2}) 350 if err != nil { 351 t.Error(err) 352 } 353 354 if !bytes.Equal(packed, sig) { 355 t.Errorf("expected %x got %x", sig, packed) 356 } 357 358 var addrA, addrB = common.Address{1}, common.Address{2} 359 sig = abi.Methods["sliceAddress"].Id() 360 sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) 361 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 362 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 363 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 364 365 packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) 366 if err != nil { 367 t.Fatal(err) 368 } 369 if !bytes.Equal(packed, sig) { 370 t.Errorf("expected %x got %x", sig, packed) 371 } 372 373 var addrC, addrD = common.Address{3}, common.Address{4} 374 sig = abi.Methods["sliceMultiAddress"].Id() 375 sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) 376 sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) 377 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 378 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 379 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 380 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 381 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 382 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 383 384 packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) 385 if err != nil { 386 t.Fatal(err) 387 } 388 if !bytes.Equal(packed, sig) { 389 t.Errorf("expected %x got %x", sig, packed) 390 } 391 392 sig = abi.Methods["slice256"].Id() 393 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 394 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 395 396 packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) 397 if err != nil { 398 t.Error(err) 399 } 400 401 if !bytes.Equal(packed, sig) { 402 t.Errorf("expected %x got %x", sig, packed) 403 } 404 } 405 406 func TestPackNumber(t *testing.T) { 407 tests := []struct { 408 value reflect.Value 409 packed []byte 410 }{ 411 // Protocol limits 412 {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, 413 {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, 414 {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, 415 416 // Type corner cases 417 {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, 418 {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, 419 {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, 420 {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, 421 422 {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, 423 {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, 424 {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, 425 {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, 426 427 {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, 428 {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, 429 {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, 430 {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, 431 } 432 for i, tt := range tests { 433 packed := packNum(tt.value) 434 if !bytes.Equal(packed, tt.packed) { 435 t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) 436 } 437 } 438 }