type_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. kind Type
  32. }{
  33. {"bool", Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}},
  34. {"bool[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool(nil)), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}},
  35. {"bool[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}},
  36. {"bool[2][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}},
  37. {"bool[][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}},
  38. {"bool[][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}},
  39. {"bool[2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}},
  40. {"bool[2][][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][][2]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}},
  41. {"bool[2][2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}},
  42. {"bool[][][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}},
  43. {"bool[][2][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][2][]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}},
  44. {"int8", Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}},
  45. {"int16", Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}},
  46. {"int32", Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}},
  47. {"int64", Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}},
  48. {"int256", Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}},
  49. {"int8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}},
  50. {"int8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}},
  51. {"int16[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}},
  52. {"int16[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}},
  53. {"int32[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}},
  54. {"int32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}},
  55. {"int64[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}},
  56. {"int64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}},
  57. {"int256[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}},
  58. {"int256[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}},
  59. {"uint8", Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}},
  60. {"uint16", Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}},
  61. {"uint32", Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}},
  62. {"uint64", Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}},
  63. {"uint256", Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}},
  64. {"uint8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}},
  65. {"uint8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}},
  66. {"uint16[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}},
  67. {"uint16[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}},
  68. {"uint32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}},
  69. {"uint32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}},
  70. {"uint64[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}},
  71. {"uint64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}},
  72. {"uint256[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}},
  73. {"uint256[2]", Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}},
  74. {"bytes32", Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}},
  75. {"bytes[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]byte{}), Elem: &Type{Kind: reflect.Slice, Type: reflect.TypeOf([]byte{}), T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}},
  76. {"bytes[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]byte{}), Elem: &Type{T: BytesTy, Type: reflect.TypeOf([]byte{}), Kind: reflect.Slice, stringKind: "bytes"}, stringKind: "bytes[2]"}},
  77. {"bytes32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][32]byte{}), Elem: &Type{Kind: reflect.Array, Type: reflect.TypeOf([32]byte{}), T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}},
  78. {"bytes32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][32]byte{}), Elem: &Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}, stringKind: "bytes32[2]"}},
  79. {"string", Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}},
  80. {"string[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}},
  81. {"string[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]string{}), Elem: &Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}, stringKind: "string[2]"}},
  82. {"address", Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}},
  83. {"address[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}},
  84. {"address[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}},
  85. // TODO when fixed types are implemented properly
  86. // {"fixed", Type{}},
  87. // {"fixed128x128", Type{}},
  88. // {"fixed[]", Type{}},
  89. // {"fixed[2]", Type{}},
  90. // {"fixed128x128[]", Type{}},
  91. // {"fixed128x128[2]", Type{}},
  92. }
  93. for _, tt := range tests {
  94. typ, err := NewType(tt.blob)
  95. if err != nil {
  96. t.Errorf("type %q: failed to parse type string: %v", tt.blob, err)
  97. }
  98. if !reflect.DeepEqual(typ, tt.kind) {
  99. t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind)))
  100. }
  101. }
  102. }
  103. func TestTypeCheck(t *testing.T) {
  104. for i, test := range []struct {
  105. typ string
  106. input interface{}
  107. err string
  108. }{
  109. {"uint", big.NewInt(1), "unsupported arg type: uint"},
  110. {"int", big.NewInt(1), "unsupported arg type: int"},
  111. {"uint256", big.NewInt(1), ""},
  112. {"uint256[][3][]", [][3][]*big.Int{{{}}}, ""},
  113. {"uint256[][][3]", [3][][]*big.Int{{{}}}, ""},
  114. {"uint256[3][][]", [][][3]*big.Int{{{}}}, ""},
  115. {"uint256[3][3][3]", [3][3][3]*big.Int{{{}}}, ""},
  116. {"uint8[][]", [][]uint8{}, ""},
  117. {"int256", big.NewInt(1), ""},
  118. {"uint8", uint8(1), ""},
  119. {"uint16", uint16(1), ""},
  120. {"uint32", uint32(1), ""},
  121. {"uint64", uint64(1), ""},
  122. {"int8", int8(1), ""},
  123. {"int16", int16(1), ""},
  124. {"int32", int32(1), ""},
  125. {"int64", int64(1), ""},
  126. {"uint24", big.NewInt(1), ""},
  127. {"uint40", big.NewInt(1), ""},
  128. {"uint48", big.NewInt(1), ""},
  129. {"uint56", big.NewInt(1), ""},
  130. {"uint72", big.NewInt(1), ""},
  131. {"uint80", big.NewInt(1), ""},
  132. {"uint88", big.NewInt(1), ""},
  133. {"uint96", big.NewInt(1), ""},
  134. {"uint104", big.NewInt(1), ""},
  135. {"uint112", big.NewInt(1), ""},
  136. {"uint120", big.NewInt(1), ""},
  137. {"uint128", big.NewInt(1), ""},
  138. {"uint136", big.NewInt(1), ""},
  139. {"uint144", big.NewInt(1), ""},
  140. {"uint152", big.NewInt(1), ""},
  141. {"uint160", big.NewInt(1), ""},
  142. {"uint168", big.NewInt(1), ""},
  143. {"uint176", big.NewInt(1), ""},
  144. {"uint184", big.NewInt(1), ""},
  145. {"uint192", big.NewInt(1), ""},
  146. {"uint200", big.NewInt(1), ""},
  147. {"uint208", big.NewInt(1), ""},
  148. {"uint216", big.NewInt(1), ""},
  149. {"uint224", big.NewInt(1), ""},
  150. {"uint232", big.NewInt(1), ""},
  151. {"uint240", big.NewInt(1), ""},
  152. {"uint248", big.NewInt(1), ""},
  153. {"int24", big.NewInt(1), ""},
  154. {"int40", big.NewInt(1), ""},
  155. {"int48", big.NewInt(1), ""},
  156. {"int56", big.NewInt(1), ""},
  157. {"int72", big.NewInt(1), ""},
  158. {"int80", big.NewInt(1), ""},
  159. {"int88", big.NewInt(1), ""},
  160. {"int96", big.NewInt(1), ""},
  161. {"int104", big.NewInt(1), ""},
  162. {"int112", big.NewInt(1), ""},
  163. {"int120", big.NewInt(1), ""},
  164. {"int128", big.NewInt(1), ""},
  165. {"int136", big.NewInt(1), ""},
  166. {"int144", big.NewInt(1), ""},
  167. {"int152", big.NewInt(1), ""},
  168. {"int160", big.NewInt(1), ""},
  169. {"int168", big.NewInt(1), ""},
  170. {"int176", big.NewInt(1), ""},
  171. {"int184", big.NewInt(1), ""},
  172. {"int192", big.NewInt(1), ""},
  173. {"int200", big.NewInt(1), ""},
  174. {"int208", big.NewInt(1), ""},
  175. {"int216", big.NewInt(1), ""},
  176. {"int224", big.NewInt(1), ""},
  177. {"int232", big.NewInt(1), ""},
  178. {"int240", big.NewInt(1), ""},
  179. {"int248", big.NewInt(1), ""},
  180. {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"},
  181. {"uint8", uint16(1), "abi: cannot use uint16 as type uint8 as argument"},
  182. {"uint8", uint32(1), "abi: cannot use uint32 as type uint8 as argument"},
  183. {"uint8", uint64(1), "abi: cannot use uint64 as type uint8 as argument"},
  184. {"uint8", int8(1), "abi: cannot use int8 as type uint8 as argument"},
  185. {"uint8", int16(1), "abi: cannot use int16 as type uint8 as argument"},
  186. {"uint8", int32(1), "abi: cannot use int32 as type uint8 as argument"},
  187. {"uint8", int64(1), "abi: cannot use int64 as type uint8 as argument"},
  188. {"uint16", uint16(1), ""},
  189. {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
  190. {"uint16[]", []uint16{1, 2, 3}, ""},
  191. {"uint16[]", [3]uint16{1, 2, 3}, ""},
  192. {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"},
  193. {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
  194. {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  195. {"uint16[3]", []uint16{1, 2, 3}, ""},
  196. {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  197. {"address[]", []common.Address{{1}}, ""},
  198. {"address[1]", []common.Address{{1}}, ""},
  199. {"address[1]", [1]common.Address{{1}}, ""},
  200. {"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"},
  201. {"bytes32", [32]byte{}, ""},
  202. {"bytes31", [31]byte{}, ""},
  203. {"bytes30", [30]byte{}, ""},
  204. {"bytes29", [29]byte{}, ""},
  205. {"bytes28", [28]byte{}, ""},
  206. {"bytes27", [27]byte{}, ""},
  207. {"bytes26", [26]byte{}, ""},
  208. {"bytes25", [25]byte{}, ""},
  209. {"bytes24", [24]byte{}, ""},
  210. {"bytes23", [23]byte{}, ""},
  211. {"bytes22", [22]byte{}, ""},
  212. {"bytes21", [21]byte{}, ""},
  213. {"bytes20", [20]byte{}, ""},
  214. {"bytes19", [19]byte{}, ""},
  215. {"bytes18", [18]byte{}, ""},
  216. {"bytes17", [17]byte{}, ""},
  217. {"bytes16", [16]byte{}, ""},
  218. {"bytes15", [15]byte{}, ""},
  219. {"bytes14", [14]byte{}, ""},
  220. {"bytes13", [13]byte{}, ""},
  221. {"bytes12", [12]byte{}, ""},
  222. {"bytes11", [11]byte{}, ""},
  223. {"bytes10", [10]byte{}, ""},
  224. {"bytes9", [9]byte{}, ""},
  225. {"bytes8", [8]byte{}, ""},
  226. {"bytes7", [7]byte{}, ""},
  227. {"bytes6", [6]byte{}, ""},
  228. {"bytes5", [5]byte{}, ""},
  229. {"bytes4", [4]byte{}, ""},
  230. {"bytes3", [3]byte{}, ""},
  231. {"bytes2", [2]byte{}, ""},
  232. {"bytes1", [1]byte{}, ""},
  233. {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
  234. {"bytes32", common.Hash{1}, ""},
  235. {"bytes31", common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"},
  236. {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
  237. {"bytes", []byte{0, 1}, ""},
  238. {"bytes", [2]byte{0, 1}, "abi: cannot use array as type slice as argument"},
  239. {"bytes", common.Hash{1}, "abi: cannot use array as type slice as argument"},
  240. {"string", "hello world", ""},
  241. {"string", string(""), ""},
  242. {"string", []byte{}, "abi: cannot use slice as type string as argument"},
  243. {"bytes32[]", [][32]byte{{}}, ""},
  244. {"function", [24]byte{}, ""},
  245. {"bytes20", common.Address{}, ""},
  246. {"address", [20]byte{}, ""},
  247. {"address", common.Address{}, ""},
  248. {"bytes32[]]", "", "invalid arg type in abi"},
  249. {"invalidType", "", "unsupported arg type: invalidType"},
  250. {"invalidSlice[]", "", "unsupported arg type: invalidSlice"},
  251. } {
  252. typ, err := NewType(test.typ)
  253. if err != nil && len(test.err) == 0 {
  254. t.Fatal("unexpected parse error:", err)
  255. } else if err != nil && len(test.err) != 0 {
  256. if err.Error() != test.err {
  257. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  258. }
  259. continue
  260. }
  261. err = typeCheck(typ, reflect.ValueOf(test.input))
  262. if err != nil && len(test.err) == 0 {
  263. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  264. continue
  265. }
  266. if err == nil && len(test.err) != 0 {
  267. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  268. continue
  269. }
  270. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  271. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  272. }
  273. }
  274. }