topics_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 bind
  17. import (
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/accounts/abi"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. func TestMakeTopics(t *testing.T) {
  25. type args struct {
  26. query [][]interface{}
  27. }
  28. tests := []struct {
  29. name string
  30. args args
  31. want [][]common.Hash
  32. wantErr bool
  33. }{
  34. {
  35. "support fixed byte types, right padded to 32 bytes",
  36. args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}},
  37. [][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
  38. false,
  39. },
  40. }
  41. for _, tt := range tests {
  42. t.Run(tt.name, func(t *testing.T) {
  43. got, err := makeTopics(tt.args.query...)
  44. if (err != nil) != tt.wantErr {
  45. t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr)
  46. return
  47. }
  48. if !reflect.DeepEqual(got, tt.want) {
  49. t.Errorf("makeTopics() = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }
  54. type args struct {
  55. createObj func() interface{}
  56. resultObj func() interface{}
  57. resultMap func() map[string]interface{}
  58. fields abi.Arguments
  59. topics []common.Hash
  60. }
  61. type bytesStruct struct {
  62. StaticBytes [5]byte
  63. }
  64. type int8Struct struct {
  65. Int8Value int8
  66. }
  67. type int256Struct struct {
  68. Int256Value *big.Int
  69. }
  70. type topicTest struct {
  71. name string
  72. args args
  73. wantErr bool
  74. }
  75. func setupTopicsTests() []topicTest {
  76. bytesType, _ := abi.NewType("bytes5", "", nil)
  77. int8Type, _ := abi.NewType("int8", "", nil)
  78. int256Type, _ := abi.NewType("int256", "", nil)
  79. tests := []topicTest{
  80. {
  81. name: "support fixed byte types, right padded to 32 bytes",
  82. args: args{
  83. createObj: func() interface{} { return &bytesStruct{} },
  84. resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
  85. resultMap: func() map[string]interface{} {
  86. return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}}
  87. },
  88. fields: abi.Arguments{abi.Argument{
  89. Name: "staticBytes",
  90. Type: bytesType,
  91. Indexed: true,
  92. }},
  93. topics: []common.Hash{
  94. {1, 2, 3, 4, 5},
  95. },
  96. },
  97. wantErr: false,
  98. },
  99. {
  100. name: "int8 with negative value",
  101. args: args{
  102. createObj: func() interface{} { return &int8Struct{} },
  103. resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
  104. resultMap: func() map[string]interface{} {
  105. return map[string]interface{}{"int8Value": int8(-1)}
  106. },
  107. fields: abi.Arguments{abi.Argument{
  108. Name: "int8Value",
  109. Type: int8Type,
  110. Indexed: true,
  111. }},
  112. topics: []common.Hash{
  113. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  114. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  115. },
  116. },
  117. wantErr: false,
  118. },
  119. {
  120. name: "int256 with negative value",
  121. args: args{
  122. createObj: func() interface{} { return &int256Struct{} },
  123. resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} },
  124. resultMap: func() map[string]interface{} {
  125. return map[string]interface{}{"int256Value": big.NewInt(-1)}
  126. },
  127. fields: abi.Arguments{abi.Argument{
  128. Name: "int256Value",
  129. Type: int256Type,
  130. Indexed: true,
  131. }},
  132. topics: []common.Hash{
  133. {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  134. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
  135. },
  136. },
  137. wantErr: false,
  138. },
  139. }
  140. return tests
  141. }
  142. func TestParseTopics(t *testing.T) {
  143. tests := setupTopicsTests()
  144. for _, tt := range tests {
  145. t.Run(tt.name, func(t *testing.T) {
  146. createObj := tt.args.createObj()
  147. if err := parseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
  148. t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr)
  149. }
  150. resultObj := tt.args.resultObj()
  151. if !reflect.DeepEqual(createObj, resultObj) {
  152. t.Errorf("parseTopics() = %v, want %v", createObj, resultObj)
  153. }
  154. })
  155. }
  156. }
  157. func TestParseTopicsIntoMap(t *testing.T) {
  158. tests := setupTopicsTests()
  159. for _, tt := range tests {
  160. t.Run(tt.name, func(t *testing.T) {
  161. outMap := make(map[string]interface{})
  162. if err := parseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
  163. t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr)
  164. }
  165. resultMap := tt.args.resultMap()
  166. if !reflect.DeepEqual(outMap, resultMap) {
  167. t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap)
  168. }
  169. })
  170. }
  171. }