pack_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if reflect.TypeOf(test.unpacked).Kind() != reflect.Struct {
  44. packed, err = inAbi.Pack("method", test.unpacked)
  45. } else {
  46. // if want is a struct we need to use the components.
  47. elem := reflect.ValueOf(test.unpacked)
  48. var values []interface{}
  49. for i := 0; i < elem.NumField(); i++ {
  50. field := elem.Field(i)
  51. values = append(values, field.Interface())
  52. }
  53. packed, err = inAbi.Pack("method", values...)
  54. }
  55. if err != nil {
  56. t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
  57. }
  58. if !reflect.DeepEqual(packed[4:], encb) {
  59. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
  60. }
  61. })
  62. }
  63. }
  64. func TestMethodPack(t *testing.T) {
  65. abi, err := JSON(strings.NewReader(jsondata))
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. sig := abi.Methods["slice"].ID
  70. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  71. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  72. packed, err := abi.Pack("slice", []uint32{1, 2})
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. if !bytes.Equal(packed, sig) {
  77. t.Errorf("expected %x got %x", sig, packed)
  78. }
  79. var addrA, addrB = common.Address{1}, common.Address{2}
  80. sig = abi.Methods["sliceAddress"].ID
  81. sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
  82. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  83. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  84. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  85. packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if !bytes.Equal(packed, sig) {
  90. t.Errorf("expected %x got %x", sig, packed)
  91. }
  92. var addrC, addrD = common.Address{3}, common.Address{4}
  93. sig = abi.Methods["sliceMultiAddress"].ID
  94. sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
  95. sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
  96. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  97. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  98. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  99. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  100. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  101. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  102. packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if !bytes.Equal(packed, sig) {
  107. t.Errorf("expected %x got %x", sig, packed)
  108. }
  109. sig = abi.Methods["slice256"].ID
  110. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  111. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  112. packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  113. if err != nil {
  114. t.Error(err)
  115. }
  116. if !bytes.Equal(packed, sig) {
  117. t.Errorf("expected %x got %x", sig, packed)
  118. }
  119. a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
  120. sig = abi.Methods["nestedArray"].ID
  121. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  122. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  123. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  124. sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
  125. sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
  126. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  127. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  128. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  129. packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if !bytes.Equal(packed, sig) {
  134. t.Errorf("expected %x got %x", sig, packed)
  135. }
  136. sig = abi.Methods["nestedArray2"].ID
  137. sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
  138. sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
  139. sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
  140. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  141. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  142. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  143. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  144. packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. if !bytes.Equal(packed, sig) {
  149. t.Errorf("expected %x got %x", sig, packed)
  150. }
  151. sig = abi.Methods["nestedSlice"].ID
  152. sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
  153. sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
  154. sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
  155. sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
  156. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  157. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  158. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  159. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  160. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  161. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  162. packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. if !bytes.Equal(packed, sig) {
  167. t.Errorf("expected %x got %x", sig, packed)
  168. }
  169. }
  170. func TestPackNumber(t *testing.T) {
  171. tests := []struct {
  172. value reflect.Value
  173. packed []byte
  174. }{
  175. // Protocol limits
  176. {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
  177. {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
  178. {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
  179. // Type corner cases
  180. {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
  181. {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
  182. {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
  183. {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
  184. {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
  185. {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
  186. {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
  187. {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
  188. {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
  189. {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
  190. {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
  191. {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
  192. }
  193. for i, tt := range tests {
  194. packed := packNum(tt.value)
  195. if !bytes.Equal(packed, tt.packed) {
  196. t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
  197. }
  198. }
  199. }