reflect_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2019 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. )
  22. type reflectTest struct {
  23. name string
  24. args []string
  25. struc interface{}
  26. want map[string]string
  27. err string
  28. }
  29. var reflectTests = []reflectTest{
  30. {
  31. name: "OneToOneCorrespondance",
  32. args: []string{"fieldA"},
  33. struc: struct {
  34. FieldA int `abi:"fieldA"`
  35. }{},
  36. want: map[string]string{
  37. "fieldA": "FieldA",
  38. },
  39. },
  40. {
  41. name: "MissingFieldsInStruct",
  42. args: []string{"fieldA", "fieldB"},
  43. struc: struct {
  44. FieldA int `abi:"fieldA"`
  45. }{},
  46. want: map[string]string{
  47. "fieldA": "FieldA",
  48. },
  49. },
  50. {
  51. name: "MoreFieldsInStructThanArgs",
  52. args: []string{"fieldA"},
  53. struc: struct {
  54. FieldA int `abi:"fieldA"`
  55. FieldB int
  56. }{},
  57. want: map[string]string{
  58. "fieldA": "FieldA",
  59. },
  60. },
  61. {
  62. name: "MissingFieldInArgs",
  63. args: []string{"fieldA"},
  64. struc: struct {
  65. FieldA int `abi:"fieldA"`
  66. FieldB int `abi:"fieldB"`
  67. }{},
  68. err: "struct: abi tag 'fieldB' defined but not found in abi",
  69. },
  70. {
  71. name: "NoAbiDescriptor",
  72. args: []string{"fieldA"},
  73. struc: struct {
  74. FieldA int
  75. }{},
  76. want: map[string]string{
  77. "fieldA": "FieldA",
  78. },
  79. },
  80. {
  81. name: "NoArgs",
  82. args: []string{},
  83. struc: struct {
  84. FieldA int `abi:"fieldA"`
  85. }{},
  86. err: "struct: abi tag 'fieldA' defined but not found in abi",
  87. },
  88. {
  89. name: "DifferentName",
  90. args: []string{"fieldB"},
  91. struc: struct {
  92. FieldA int `abi:"fieldB"`
  93. }{},
  94. want: map[string]string{
  95. "fieldB": "FieldA",
  96. },
  97. },
  98. {
  99. name: "DifferentName",
  100. args: []string{"fieldB"},
  101. struc: struct {
  102. FieldA int `abi:"fieldB"`
  103. }{},
  104. want: map[string]string{
  105. "fieldB": "FieldA",
  106. },
  107. },
  108. {
  109. name: "MultipleFields",
  110. args: []string{"fieldA", "fieldB"},
  111. struc: struct {
  112. FieldA int `abi:"fieldA"`
  113. FieldB int `abi:"fieldB"`
  114. }{},
  115. want: map[string]string{
  116. "fieldA": "FieldA",
  117. "fieldB": "FieldB",
  118. },
  119. },
  120. {
  121. name: "MultipleFieldsABIMissing",
  122. args: []string{"fieldA", "fieldB"},
  123. struc: struct {
  124. FieldA int `abi:"fieldA"`
  125. FieldB int
  126. }{},
  127. want: map[string]string{
  128. "fieldA": "FieldA",
  129. "fieldB": "FieldB",
  130. },
  131. },
  132. {
  133. name: "NameConflict",
  134. args: []string{"fieldB"},
  135. struc: struct {
  136. FieldA int `abi:"fieldB"`
  137. FieldB int
  138. }{},
  139. err: "abi: multiple variables maps to the same abi field 'fieldB'",
  140. },
  141. {
  142. name: "Underscored",
  143. args: []string{"_"},
  144. struc: struct {
  145. FieldA int
  146. }{},
  147. err: "abi: purely underscored output cannot unpack to struct",
  148. },
  149. {
  150. name: "DoubleMapping",
  151. args: []string{"fieldB", "fieldC", "fieldA"},
  152. struc: struct {
  153. FieldA int `abi:"fieldC"`
  154. FieldB int
  155. }{},
  156. err: "abi: multiple outputs mapping to the same struct field 'FieldA'",
  157. },
  158. {
  159. name: "AlreadyMapped",
  160. args: []string{"fieldB", "fieldB"},
  161. struc: struct {
  162. FieldB int `abi:"fieldB"`
  163. }{},
  164. err: "struct: abi tag in 'FieldB' already mapped",
  165. },
  166. }
  167. func TestReflectNameToStruct(t *testing.T) {
  168. for _, test := range reflectTests {
  169. t.Run(test.name, func(t *testing.T) {
  170. m, err := mapArgNamesToStructFields(test.args, reflect.ValueOf(test.struc))
  171. if len(test.err) > 0 {
  172. if err == nil || err.Error() != test.err {
  173. t.Fatalf("Invalid error: expected %v, got %v", test.err, err)
  174. }
  175. } else {
  176. if err != nil {
  177. t.Fatalf("Unexpected error: %v", err)
  178. }
  179. for fname := range test.want {
  180. if m[fname] != test.want[fname] {
  181. t.Fatalf("Incorrect value for field %s: expected %v, got %v", fname, test.want[fname], m[fname])
  182. }
  183. }
  184. }
  185. })
  186. }
  187. }
  188. func TestConvertType(t *testing.T) {
  189. // Test Basic Struct
  190. type T struct {
  191. X *big.Int
  192. Y *big.Int
  193. }
  194. // Create on-the-fly structure
  195. var fields []reflect.StructField
  196. fields = append(fields, reflect.StructField{
  197. Name: "X",
  198. Type: reflect.TypeOf(new(big.Int)),
  199. Tag: "json:\"" + "x" + "\"",
  200. })
  201. fields = append(fields, reflect.StructField{
  202. Name: "Y",
  203. Type: reflect.TypeOf(new(big.Int)),
  204. Tag: "json:\"" + "y" + "\"",
  205. })
  206. val := reflect.New(reflect.StructOf(fields))
  207. val.Elem().Field(0).Set(reflect.ValueOf(big.NewInt(1)))
  208. val.Elem().Field(1).Set(reflect.ValueOf(big.NewInt(2)))
  209. // ConvertType
  210. out := *ConvertType(val.Interface(), new(T)).(*T)
  211. if out.X.Cmp(big.NewInt(1)) != 0 {
  212. t.Errorf("ConvertType failed, got %v want %v", out.X, big.NewInt(1))
  213. }
  214. if out.Y.Cmp(big.NewInt(2)) != 0 {
  215. t.Errorf("ConvertType failed, got %v want %v", out.Y, big.NewInt(2))
  216. }
  217. // Slice Type
  218. val2 := reflect.MakeSlice(reflect.SliceOf(reflect.StructOf(fields)), 2, 2)
  219. val2.Index(0).Field(0).Set(reflect.ValueOf(big.NewInt(1)))
  220. val2.Index(0).Field(1).Set(reflect.ValueOf(big.NewInt(2)))
  221. val2.Index(1).Field(0).Set(reflect.ValueOf(big.NewInt(3)))
  222. val2.Index(1).Field(1).Set(reflect.ValueOf(big.NewInt(4)))
  223. out2 := *ConvertType(val2.Interface(), new([]T)).(*[]T)
  224. if out2[0].X.Cmp(big.NewInt(1)) != 0 {
  225. t.Errorf("ConvertType failed, got %v want %v", out2[0].X, big.NewInt(1))
  226. }
  227. if out2[0].Y.Cmp(big.NewInt(2)) != 0 {
  228. t.Errorf("ConvertType failed, got %v want %v", out2[1].Y, big.NewInt(2))
  229. }
  230. if out2[1].X.Cmp(big.NewInt(3)) != 0 {
  231. t.Errorf("ConvertType failed, got %v want %v", out2[0].X, big.NewInt(1))
  232. }
  233. if out2[1].Y.Cmp(big.NewInt(4)) != 0 {
  234. t.Errorf("ConvertType failed, got %v want %v", out2[1].Y, big.NewInt(2))
  235. }
  236. // Array Type
  237. val3 := reflect.New(reflect.ArrayOf(2, reflect.StructOf(fields)))
  238. val3.Elem().Index(0).Field(0).Set(reflect.ValueOf(big.NewInt(1)))
  239. val3.Elem().Index(0).Field(1).Set(reflect.ValueOf(big.NewInt(2)))
  240. val3.Elem().Index(1).Field(0).Set(reflect.ValueOf(big.NewInt(3)))
  241. val3.Elem().Index(1).Field(1).Set(reflect.ValueOf(big.NewInt(4)))
  242. out3 := *ConvertType(val3.Interface(), new([2]T)).(*[2]T)
  243. if out3[0].X.Cmp(big.NewInt(1)) != 0 {
  244. t.Errorf("ConvertType failed, got %v want %v", out3[0].X, big.NewInt(1))
  245. }
  246. if out3[0].Y.Cmp(big.NewInt(2)) != 0 {
  247. t.Errorf("ConvertType failed, got %v want %v", out3[1].Y, big.NewInt(2))
  248. }
  249. if out3[1].X.Cmp(big.NewInt(3)) != 0 {
  250. t.Errorf("ConvertType failed, got %v want %v", out3[0].X, big.NewInt(1))
  251. }
  252. if out3[1].Y.Cmp(big.NewInt(4)) != 0 {
  253. t.Errorf("ConvertType failed, got %v want %v", out3[1].Y, big.NewInt(2))
  254. }
  255. }