type_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 2016 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. "math/big"
  19. "reflect"
  20. "testing"
  21. "github.com/davecgh/go-spew/spew"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. // typeWithoutStringer is a alias for the Type type which simply doesn't implement
  25. // the stringer interface to allow printing type details in the tests below.
  26. type typeWithoutStringer Type
  27. // Tests that all allowed types get recognized by the type parser.
  28. func TestTypeRegexp(t *testing.T) {
  29. tests := []struct {
  30. blob string
  31. components []ArgumentMarshaling
  32. kind Type
  33. }{
  34. {"bool", nil, Type{T: BoolTy, stringKind: "bool"}},
  35. {"bool[]", nil, Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}},
  36. {"bool[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}},
  37. {"bool[2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}},
  38. {"bool[][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}},
  39. {"bool[][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}},
  40. {"bool[2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}},
  41. {"bool[2][][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}},
  42. {"bool[2][2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}},
  43. {"bool[][][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}},
  44. {"bool[][2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}},
  45. {"int8", nil, Type{Size: 8, T: IntTy, stringKind: "int8"}},
  46. {"int16", nil, Type{Size: 16, T: IntTy, stringKind: "int16"}},
  47. {"int32", nil, Type{Size: 32, T: IntTy, stringKind: "int32"}},
  48. {"int64", nil, Type{Size: 64, T: IntTy, stringKind: "int64"}},
  49. {"int256", nil, Type{Size: 256, T: IntTy, stringKind: "int256"}},
  50. {"int8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}},
  51. {"int8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}},
  52. {"int16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}},
  53. {"int16[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}},
  54. {"int32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}},
  55. {"int32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}},
  56. {"int64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}},
  57. {"int64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}},
  58. {"int256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}},
  59. {"int256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}},
  60. {"uint8", nil, Type{Size: 8, T: UintTy, stringKind: "uint8"}},
  61. {"uint16", nil, Type{Size: 16, T: UintTy, stringKind: "uint16"}},
  62. {"uint32", nil, Type{Size: 32, T: UintTy, stringKind: "uint32"}},
  63. {"uint64", nil, Type{Size: 64, T: UintTy, stringKind: "uint64"}},
  64. {"uint256", nil, Type{Size: 256, T: UintTy, stringKind: "uint256"}},
  65. {"uint8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}},
  66. {"uint8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}},
  67. {"uint16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}},
  68. {"uint16[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}},
  69. {"uint32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}},
  70. {"uint32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}},
  71. {"uint64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}},
  72. {"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}},
  73. {"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}},
  74. {"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}},
  75. {"bytes32", nil, Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}},
  76. {"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}},
  77. {"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}},
  78. {"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}},
  79. {"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}},
  80. {"string", nil, Type{T: StringTy, stringKind: "string"}},
  81. {"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}},
  82. {"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}},
  83. {"address", nil, Type{Size: 20, T: AddressTy, stringKind: "address"}},
  84. {"address[]", nil, Type{T: SliceTy, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}},
  85. {"address[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}},
  86. // TODO when fixed types are implemented properly
  87. // {"fixed", nil, Type{}},
  88. // {"fixed128x128", nil, Type{}},
  89. // {"fixed[]", nil, Type{}},
  90. // {"fixed[2]", nil, Type{}},
  91. // {"fixed128x128[]", nil, Type{}},
  92. // {"fixed128x128[2]", nil, Type{}},
  93. {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct {
  94. A int64 `json:"a"`
  95. }{}), stringKind: "(int64)",
  96. TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}},
  97. {"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct {
  98. ATypicalParamName int64 `json:"aTypicalParamName"`
  99. }{}), stringKind: "(int64)",
  100. TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}}},
  101. }
  102. for _, tt := range tests {
  103. typ, err := NewType(tt.blob, "", tt.components)
  104. if err != nil {
  105. t.Errorf("type %q: failed to parse type string: %v", tt.blob, err)
  106. }
  107. if !reflect.DeepEqual(typ, tt.kind) {
  108. t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind)))
  109. }
  110. }
  111. }
  112. func TestTypeCheck(t *testing.T) {
  113. for i, test := range []struct {
  114. typ string
  115. components []ArgumentMarshaling
  116. input interface{}
  117. err string
  118. }{
  119. {"uint", nil, big.NewInt(1), "unsupported arg type: uint"},
  120. {"int", nil, big.NewInt(1), "unsupported arg type: int"},
  121. {"uint256", nil, big.NewInt(1), ""},
  122. {"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""},
  123. {"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""},
  124. {"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""},
  125. {"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""},
  126. {"uint8[][]", nil, [][]uint8{}, ""},
  127. {"int256", nil, big.NewInt(1), ""},
  128. {"uint8", nil, uint8(1), ""},
  129. {"uint16", nil, uint16(1), ""},
  130. {"uint32", nil, uint32(1), ""},
  131. {"uint64", nil, uint64(1), ""},
  132. {"int8", nil, int8(1), ""},
  133. {"int16", nil, int16(1), ""},
  134. {"int32", nil, int32(1), ""},
  135. {"int64", nil, int64(1), ""},
  136. {"uint24", nil, big.NewInt(1), ""},
  137. {"uint40", nil, big.NewInt(1), ""},
  138. {"uint48", nil, big.NewInt(1), ""},
  139. {"uint56", nil, big.NewInt(1), ""},
  140. {"uint72", nil, big.NewInt(1), ""},
  141. {"uint80", nil, big.NewInt(1), ""},
  142. {"uint88", nil, big.NewInt(1), ""},
  143. {"uint96", nil, big.NewInt(1), ""},
  144. {"uint104", nil, big.NewInt(1), ""},
  145. {"uint112", nil, big.NewInt(1), ""},
  146. {"uint120", nil, big.NewInt(1), ""},
  147. {"uint128", nil, big.NewInt(1), ""},
  148. {"uint136", nil, big.NewInt(1), ""},
  149. {"uint144", nil, big.NewInt(1), ""},
  150. {"uint152", nil, big.NewInt(1), ""},
  151. {"uint160", nil, big.NewInt(1), ""},
  152. {"uint168", nil, big.NewInt(1), ""},
  153. {"uint176", nil, big.NewInt(1), ""},
  154. {"uint184", nil, big.NewInt(1), ""},
  155. {"uint192", nil, big.NewInt(1), ""},
  156. {"uint200", nil, big.NewInt(1), ""},
  157. {"uint208", nil, big.NewInt(1), ""},
  158. {"uint216", nil, big.NewInt(1), ""},
  159. {"uint224", nil, big.NewInt(1), ""},
  160. {"uint232", nil, big.NewInt(1), ""},
  161. {"uint240", nil, big.NewInt(1), ""},
  162. {"uint248", nil, big.NewInt(1), ""},
  163. {"int24", nil, big.NewInt(1), ""},
  164. {"int40", nil, big.NewInt(1), ""},
  165. {"int48", nil, big.NewInt(1), ""},
  166. {"int56", nil, big.NewInt(1), ""},
  167. {"int72", nil, big.NewInt(1), ""},
  168. {"int80", nil, big.NewInt(1), ""},
  169. {"int88", nil, big.NewInt(1), ""},
  170. {"int96", nil, big.NewInt(1), ""},
  171. {"int104", nil, big.NewInt(1), ""},
  172. {"int112", nil, big.NewInt(1), ""},
  173. {"int120", nil, big.NewInt(1), ""},
  174. {"int128", nil, big.NewInt(1), ""},
  175. {"int136", nil, big.NewInt(1), ""},
  176. {"int144", nil, big.NewInt(1), ""},
  177. {"int152", nil, big.NewInt(1), ""},
  178. {"int160", nil, big.NewInt(1), ""},
  179. {"int168", nil, big.NewInt(1), ""},
  180. {"int176", nil, big.NewInt(1), ""},
  181. {"int184", nil, big.NewInt(1), ""},
  182. {"int192", nil, big.NewInt(1), ""},
  183. {"int200", nil, big.NewInt(1), ""},
  184. {"int208", nil, big.NewInt(1), ""},
  185. {"int216", nil, big.NewInt(1), ""},
  186. {"int224", nil, big.NewInt(1), ""},
  187. {"int232", nil, big.NewInt(1), ""},
  188. {"int240", nil, big.NewInt(1), ""},
  189. {"int248", nil, big.NewInt(1), ""},
  190. {"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"},
  191. {"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"},
  192. {"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"},
  193. {"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"},
  194. {"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"},
  195. {"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"},
  196. {"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"},
  197. {"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"},
  198. {"uint16", nil, uint16(1), ""},
  199. {"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
  200. {"uint16[]", nil, []uint16{1, 2, 3}, ""},
  201. {"uint16[]", nil, [3]uint16{1, 2, 3}, ""},
  202. {"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"},
  203. {"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
  204. {"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  205. {"uint16[3]", nil, []uint16{1, 2, 3}, ""},
  206. {"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  207. {"address[]", nil, []common.Address{{1}}, ""},
  208. {"address[1]", nil, []common.Address{{1}}, ""},
  209. {"address[1]", nil, [1]common.Address{{1}}, ""},
  210. {"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"},
  211. {"bytes32", nil, [32]byte{}, ""},
  212. {"bytes31", nil, [31]byte{}, ""},
  213. {"bytes30", nil, [30]byte{}, ""},
  214. {"bytes29", nil, [29]byte{}, ""},
  215. {"bytes28", nil, [28]byte{}, ""},
  216. {"bytes27", nil, [27]byte{}, ""},
  217. {"bytes26", nil, [26]byte{}, ""},
  218. {"bytes25", nil, [25]byte{}, ""},
  219. {"bytes24", nil, [24]byte{}, ""},
  220. {"bytes23", nil, [23]byte{}, ""},
  221. {"bytes22", nil, [22]byte{}, ""},
  222. {"bytes21", nil, [21]byte{}, ""},
  223. {"bytes20", nil, [20]byte{}, ""},
  224. {"bytes19", nil, [19]byte{}, ""},
  225. {"bytes18", nil, [18]byte{}, ""},
  226. {"bytes17", nil, [17]byte{}, ""},
  227. {"bytes16", nil, [16]byte{}, ""},
  228. {"bytes15", nil, [15]byte{}, ""},
  229. {"bytes14", nil, [14]byte{}, ""},
  230. {"bytes13", nil, [13]byte{}, ""},
  231. {"bytes12", nil, [12]byte{}, ""},
  232. {"bytes11", nil, [11]byte{}, ""},
  233. {"bytes10", nil, [10]byte{}, ""},
  234. {"bytes9", nil, [9]byte{}, ""},
  235. {"bytes8", nil, [8]byte{}, ""},
  236. {"bytes7", nil, [7]byte{}, ""},
  237. {"bytes6", nil, [6]byte{}, ""},
  238. {"bytes5", nil, [5]byte{}, ""},
  239. {"bytes4", nil, [4]byte{}, ""},
  240. {"bytes3", nil, [3]byte{}, ""},
  241. {"bytes2", nil, [2]byte{}, ""},
  242. {"bytes1", nil, [1]byte{}, ""},
  243. {"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
  244. {"bytes32", nil, common.Hash{1}, ""},
  245. {"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"},
  246. {"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
  247. {"bytes", nil, []byte{0, 1}, ""},
  248. {"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"},
  249. {"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"},
  250. {"string", nil, "hello world", ""},
  251. {"string", nil, "", ""},
  252. {"string", nil, []byte{}, "abi: cannot use slice as type string as argument"},
  253. {"bytes32[]", nil, [][32]byte{{}}, ""},
  254. {"function", nil, [24]byte{}, ""},
  255. {"bytes20", nil, common.Address{}, ""},
  256. {"address", nil, [20]byte{}, ""},
  257. {"address", nil, common.Address{}, ""},
  258. {"bytes32[]]", nil, "", "invalid arg type in abi"},
  259. {"invalidType", nil, "", "unsupported arg type: invalidType"},
  260. {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"},
  261. // simple tuple
  262. {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct {
  263. A *big.Int
  264. B *big.Int
  265. }{}, ""},
  266. // tuple slice
  267. {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
  268. A *big.Int
  269. B *big.Int
  270. }{}, ""},
  271. // tuple array
  272. {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
  273. A *big.Int
  274. B *big.Int
  275. }{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""},
  276. } {
  277. typ, err := NewType(test.typ, "", test.components)
  278. if err != nil && len(test.err) == 0 {
  279. t.Fatal("unexpected parse error:", err)
  280. } else if err != nil && len(test.err) != 0 {
  281. if err.Error() != test.err {
  282. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  283. }
  284. continue
  285. }
  286. err = typeCheck(typ, reflect.ValueOf(test.input))
  287. if err != nil && len(test.err) == 0 {
  288. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  289. continue
  290. }
  291. if err == nil && len(test.err) != 0 {
  292. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  293. continue
  294. }
  295. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  296. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  297. }
  298. }
  299. }
  300. func TestInternalType(t *testing.T) {
  301. components := []ArgumentMarshaling{{Name: "a", Type: "int64"}}
  302. internalType := "struct a.b[]"
  303. kind := Type{
  304. T: TupleTy,
  305. TupleType: reflect.TypeOf(struct {
  306. A int64 `json:"a"`
  307. }{}),
  308. stringKind: "(int64)",
  309. TupleRawName: "ab[]",
  310. TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}},
  311. TupleRawNames: []string{"a"},
  312. }
  313. blob := "tuple"
  314. typ, err := NewType(blob, internalType, components)
  315. if err != nil {
  316. t.Errorf("type %q: failed to parse type string: %v", blob, err)
  317. }
  318. if !reflect.DeepEqual(typ, kind) {
  319. t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(kind)))
  320. }
  321. }
  322. func TestGetTypeSize(t *testing.T) {
  323. var testCases = []struct {
  324. typ string
  325. components []ArgumentMarshaling
  326. typSize int
  327. }{
  328. // simple array
  329. {"uint256[2]", nil, 32 * 2},
  330. {"address[3]", nil, 32 * 3},
  331. {"bytes32[4]", nil, 32 * 4},
  332. // array array
  333. {"uint256[2][3][4]", nil, 32 * (2 * 3 * 4)},
  334. // array tuple
  335. {"tuple[2]", []ArgumentMarshaling{{Name: "x", Type: "bytes32"}, {Name: "y", Type: "bytes32"}}, (32 * 2) * 2},
  336. // simple tuple
  337. {"tuple", []ArgumentMarshaling{{Name: "x", Type: "uint256"}, {Name: "y", Type: "uint256"}}, 32 * 2},
  338. // tuple array
  339. {"tuple", []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}}, 32 * 2},
  340. // tuple tuple
  341. {"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32"}}}}, 32},
  342. {"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}, {Name: "y", Type: "uint256"}}}}, 32 * (2 + 1)},
  343. }
  344. for i, data := range testCases {
  345. typ, err := NewType(data.typ, "", data.components)
  346. if err != nil {
  347. t.Errorf("type %q: failed to parse type string: %v", data.typ, err)
  348. }
  349. result := getTypeSize(typ)
  350. if result != data.typSize {
  351. t.Errorf("case %d type %q: get type size error: actual: %d expected: %d", i, data.typ, result, data.typSize)
  352. }
  353. }
  354. }