topic_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package whisper
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. var topicCreationTests = []struct {
  7. data []byte
  8. hash [4]byte
  9. }{
  10. {hash: [4]byte{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte("test name")},
  11. {hash: [4]byte{0xf2, 0x6e, 0x77, 0x79}, data: []byte("some other test")},
  12. }
  13. func TestTopicCreation(t *testing.T) {
  14. // Create the topics individually
  15. for i, tt := range topicCreationTests {
  16. topic := NewTopic(tt.data)
  17. if bytes.Compare(topic[:], tt.hash[:]) != 0 {
  18. t.Errorf("binary test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
  19. }
  20. }
  21. for i, tt := range topicCreationTests {
  22. topic := NewTopicFromString(string(tt.data))
  23. if bytes.Compare(topic[:], tt.hash[:]) != 0 {
  24. t.Errorf("textual test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
  25. }
  26. }
  27. // Create the topics in batches
  28. binaryData := make([][]byte, len(topicCreationTests))
  29. for i, tt := range topicCreationTests {
  30. binaryData[i] = tt.data
  31. }
  32. textualData := make([]string, len(topicCreationTests))
  33. for i, tt := range topicCreationTests {
  34. textualData[i] = string(tt.data)
  35. }
  36. topics := NewTopics(binaryData...)
  37. for i, tt := range topicCreationTests {
  38. if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
  39. t.Errorf("binary batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
  40. }
  41. }
  42. topics = NewTopicsFromStrings(textualData...)
  43. for i, tt := range topicCreationTests {
  44. if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
  45. t.Errorf("textual batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
  46. }
  47. }
  48. }
  49. var topicMatcherCreationTest = struct {
  50. binary [][][]byte
  51. textual [][]string
  52. matcher []map[[4]byte]struct{}
  53. }{
  54. binary: [][][]byte{
  55. [][]byte{},
  56. [][]byte{
  57. []byte("Topic A"),
  58. },
  59. [][]byte{
  60. []byte("Topic B1"),
  61. []byte("Topic B2"),
  62. []byte("Topic B3"),
  63. },
  64. },
  65. textual: [][]string{
  66. []string{},
  67. []string{"Topic A"},
  68. []string{"Topic B1", "Topic B2", "Topic B3"},
  69. },
  70. matcher: []map[[4]byte]struct{}{
  71. map[[4]byte]struct{}{},
  72. map[[4]byte]struct{}{
  73. [4]byte{0x25, 0xfc, 0x95, 0x66}: struct{}{},
  74. },
  75. map[[4]byte]struct{}{
  76. [4]byte{0x93, 0x6d, 0xec, 0x09}: struct{}{},
  77. [4]byte{0x25, 0x23, 0x34, 0xd3}: struct{}{},
  78. [4]byte{0x6b, 0xc2, 0x73, 0xd1}: struct{}{},
  79. },
  80. },
  81. }
  82. func TestTopicMatcherCreation(t *testing.T) {
  83. test := topicMatcherCreationTest
  84. matcher := newTopicMatcherFromBinary(test.binary...)
  85. for i, cond := range matcher.conditions {
  86. for topic, _ := range cond {
  87. if _, ok := test.matcher[i][topic]; !ok {
  88. t.Errorf("condition %d; extra topic found: 0x%x", i, topic[:])
  89. }
  90. }
  91. }
  92. for i, cond := range test.matcher {
  93. for topic, _ := range cond {
  94. if _, ok := matcher.conditions[i][topic]; !ok {
  95. t.Errorf("condition %d; topic not found: 0x%x", i, topic[:])
  96. }
  97. }
  98. }
  99. matcher = newTopicMatcherFromStrings(test.textual...)
  100. for i, cond := range matcher.conditions {
  101. for topic, _ := range cond {
  102. if _, ok := test.matcher[i][topic]; !ok {
  103. t.Errorf("condition %d; extra topic found: 0x%x", i, topic[:])
  104. }
  105. }
  106. }
  107. for i, cond := range test.matcher {
  108. for topic, _ := range cond {
  109. if _, ok := matcher.conditions[i][topic]; !ok {
  110. t.Errorf("condition %d; topic not found: 0x%x", i, topic[:])
  111. }
  112. }
  113. }
  114. }
  115. var topicMatcherTests = []struct {
  116. filter [][]string
  117. topics []string
  118. match bool
  119. }{
  120. // Empty topic matcher should match everything
  121. {
  122. filter: [][]string{},
  123. topics: []string{},
  124. match: true,
  125. },
  126. {
  127. filter: [][]string{},
  128. topics: []string{"a", "b", "c"},
  129. match: true,
  130. },
  131. // Fixed topic matcher should match strictly, but only prefix
  132. {
  133. filter: [][]string{[]string{"a"}, []string{"b"}},
  134. topics: []string{"a"},
  135. match: false,
  136. },
  137. {
  138. filter: [][]string{[]string{"a"}, []string{"b"}},
  139. topics: []string{"a", "b"},
  140. match: true,
  141. },
  142. {
  143. filter: [][]string{[]string{"a"}, []string{"b"}},
  144. topics: []string{"a", "b", "c"},
  145. match: true,
  146. },
  147. // Multi-matcher should match any from a sub-group
  148. {
  149. filter: [][]string{[]string{"a1", "a2"}},
  150. topics: []string{"a"},
  151. match: false,
  152. },
  153. {
  154. filter: [][]string{[]string{"a1", "a2"}},
  155. topics: []string{"a1"},
  156. match: true,
  157. },
  158. {
  159. filter: [][]string{[]string{"a1", "a2"}},
  160. topics: []string{"a2"},
  161. match: true,
  162. },
  163. // Wild-card condition should match anything
  164. {
  165. filter: [][]string{[]string{}, []string{"b"}},
  166. topics: []string{"a"},
  167. match: false,
  168. },
  169. {
  170. filter: [][]string{[]string{}, []string{"b"}},
  171. topics: []string{"a", "b"},
  172. match: true,
  173. },
  174. {
  175. filter: [][]string{[]string{}, []string{"b"}},
  176. topics: []string{"b", "b"},
  177. match: true,
  178. },
  179. }
  180. func TestTopicMatcher(t *testing.T) {
  181. for i, tt := range topicMatcherTests {
  182. topics := NewTopicsFromStrings(tt.topics...)
  183. matcher := newTopicMatcherFromStrings(tt.filter...)
  184. if match := matcher.Matches(topics); match != tt.match {
  185. t.Errorf("test %d: match mismatch: have %v, want %v", i, match, tt.match)
  186. }
  187. }
  188. }