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