pack_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. package abi
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "math"
  22. "math/big"
  23. "reflect"
  24. "strconv"
  25. "strings"
  26. "testing"
  27. "github.com/ethereum/go-ethereum/common"
  28. )
  29. // TestPack tests the general pack/unpack tests in packing_test.go
  30. func TestPack(t *testing.T) {
  31. for i, test := range packUnpackTests {
  32. t.Run(strconv.Itoa(i), func(t *testing.T) {
  33. encb, err := hex.DecodeString(test.packed)
  34. if err != nil {
  35. t.Fatalf("invalid hex %s: %v", test.packed, err)
  36. }
  37. inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def)
  38. inAbi, err := JSON(strings.NewReader(inDef))
  39. if err != nil {
  40. t.Fatalf("invalid ABI definition %s, %v", inDef, err)
  41. }
  42. var packed []byte
  43. packed, err = inAbi.Pack("method", test.unpacked)
  44. if err != nil {
  45. t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
  46. }
  47. if !reflect.DeepEqual(packed[4:], encb) {
  48. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
  49. }
  50. })
  51. }
  52. }
  53. func TestMethodPack(t *testing.T) {
  54. abi, err := JSON(strings.NewReader(jsondata))
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. sig := abi.Methods["slice"].ID
  59. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  60. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  61. packed, err := abi.Pack("slice", []uint32{1, 2})
  62. if err != nil {
  63. t.Error(err)
  64. }
  65. if !bytes.Equal(packed, sig) {
  66. t.Errorf("expected %x got %x", sig, packed)
  67. }
  68. var addrA, addrB = common.Address{1}, common.Address{2}
  69. sig = abi.Methods["sliceAddress"].ID
  70. sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
  71. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  72. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  73. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  74. packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if !bytes.Equal(packed, sig) {
  79. t.Errorf("expected %x got %x", sig, packed)
  80. }
  81. var addrC, addrD = common.Address{3}, common.Address{4}
  82. sig = abi.Methods["sliceMultiAddress"].ID
  83. sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
  84. sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
  85. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  86. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  87. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  88. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  89. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  90. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  91. packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if !bytes.Equal(packed, sig) {
  96. t.Errorf("expected %x got %x", sig, packed)
  97. }
  98. sig = abi.Methods["slice256"].ID
  99. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  100. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  101. packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  102. if err != nil {
  103. t.Error(err)
  104. }
  105. if !bytes.Equal(packed, sig) {
  106. t.Errorf("expected %x got %x", sig, packed)
  107. }
  108. a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
  109. sig = abi.Methods["nestedArray"].ID
  110. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  111. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  112. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  113. sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
  114. sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
  115. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  116. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  117. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  118. packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if !bytes.Equal(packed, sig) {
  123. t.Errorf("expected %x got %x", sig, packed)
  124. }
  125. sig = abi.Methods["nestedArray2"].ID
  126. sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
  127. sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
  128. sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
  129. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  130. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  131. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  132. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  133. packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if !bytes.Equal(packed, sig) {
  138. t.Errorf("expected %x got %x", sig, packed)
  139. }
  140. sig = abi.Methods["nestedSlice"].ID
  141. sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
  142. sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
  143. sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
  144. sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
  145. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  146. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  147. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  148. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  149. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  150. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  151. packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. if !bytes.Equal(packed, sig) {
  156. t.Errorf("expected %x got %x", sig, packed)
  157. }
  158. }
  159. func TestPackNumber(t *testing.T) {
  160. tests := []struct {
  161. value reflect.Value
  162. packed []byte
  163. }{
  164. // Protocol limits
  165. {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
  166. {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
  167. {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
  168. // Type corner cases
  169. {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
  170. {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
  171. {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
  172. {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
  173. {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
  174. {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
  175. {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
  176. {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
  177. {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
  178. {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
  179. {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
  180. {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
  181. }
  182. for i, tt := range tests {
  183. packed := packNum(tt.value)
  184. if !bytes.Equal(packed, tt.packed) {
  185. t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
  186. }
  187. }
  188. }