aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--whisper/whisperv5/api.go2
-rw-r--r--whisper/whisperv5/filter.go8
-rw-r--r--whisper/whisperv5/filter_test.go44
-rw-r--r--whisper/whisperv6/api.go2
-rw-r--r--whisper/whisperv6/filter.go8
-rw-r--r--whisper/whisperv6/filter_test.go44
6 files changed, 92 insertions, 16 deletions
diff --git a/whisper/whisperv5/api.go b/whisper/whisperv5/api.go
index 96c4b0e6c..b4494d0d6 100644
--- a/whisper/whisperv5/api.go
+++ b/whisper/whisperv5/api.go
@@ -562,7 +562,7 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
}
if len(req.Topics) > 0 {
- topics = make([][]byte, 1)
+ topics = make([][]byte, 0, len(req.Topics))
for _, topic := range req.Topics {
topics = append(topics, topic[:])
}
diff --git a/whisper/whisperv5/filter.go b/whisper/whisperv5/filter.go
index b5e893e0f..3190334eb 100644
--- a/whisper/whisperv5/filter.go
+++ b/whisper/whisperv5/filter.go
@@ -216,8 +216,12 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
}
func matchSingleTopic(topic TopicType, bt []byte) bool {
- if len(bt) > 4 {
- bt = bt[:4]
+ if len(bt) > TopicLength {
+ bt = bt[:TopicLength]
+ }
+
+ if len(bt) < TopicLength {
+ return false
}
for j, b := range bt {
diff --git a/whisper/whisperv5/filter_test.go b/whisper/whisperv5/filter_test.go
index 72568b94e..01034a351 100644
--- a/whisper/whisperv5/filter_test.go
+++ b/whisper/whisperv5/filter_test.go
@@ -776,6 +776,7 @@ func TestWatchers(t *testing.T) {
func TestVariableTopics(t *testing.T) {
InitSingleTest()
+ const lastTopicByte = 3
var match bool
params, err := generateMessageParams()
if err != nil {
@@ -796,19 +797,52 @@ func TestVariableTopics(t *testing.T) {
}
for i := 0; i < 4; i++ {
- arr := make([]byte, i+1, 4)
- copy(arr, env.Topic[:i+1])
-
- f.Topics[4] = arr
+ env.Topic = BytesToTopic(f.Topics[i])
match = f.MatchEnvelope(env)
if !match {
t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i)
}
- f.Topics[4][i]++
+ f.Topics[i][lastTopicByte]++
match = f.MatchEnvelope(env)
if match {
t.Fatalf("MatchEnvelope symmetric with seed %d, step %d: false positive.", seed, i)
}
}
}
+
+func TestMatchSingleTopic_ReturnTrue(t *testing.T) {
+ bt := []byte("test")
+ topic := BytesToTopic(bt)
+
+ if !matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_WithTail_ReturnTrue(t *testing.T) {
+ bt := []byte("test with tail")
+ topic := BytesToTopic([]byte("test"))
+
+ if !matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_NotEquals_ReturnFalse(t *testing.T) {
+ bt := []byte("tes")
+ topic := BytesToTopic(bt)
+
+ if matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_InsufficientLength_ReturnFalse(t *testing.T) {
+ bt := []byte("test")
+ topic := BytesToTopic([]byte("not_equal"))
+
+ if matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
diff --git a/whisper/whisperv6/api.go b/whisper/whisperv6/api.go
index 0e8490b41..2f6f671e9 100644
--- a/whisper/whisperv6/api.go
+++ b/whisper/whisperv6/api.go
@@ -567,7 +567,7 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
}
if len(req.Topics) > 0 {
- topics = make([][]byte, 1)
+ topics = make([][]byte, 0, len(req.Topics))
for _, topic := range req.Topics {
topics = append(topics, topic[:])
}
diff --git a/whisper/whisperv6/filter.go b/whisper/whisperv6/filter.go
index 2f52dd6b9..a752c7ac9 100644
--- a/whisper/whisperv6/filter.go
+++ b/whisper/whisperv6/filter.go
@@ -221,8 +221,12 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
}
func matchSingleTopic(topic TopicType, bt []byte) bool {
- if len(bt) > 4 {
- bt = bt[:4]
+ if len(bt) > TopicLength {
+ bt = bt[:TopicLength]
+ }
+
+ if len(bt) < TopicLength {
+ return false
}
for j, b := range bt {
diff --git a/whisper/whisperv6/filter_test.go b/whisper/whisperv6/filter_test.go
index e2877b233..9c7fdad95 100644
--- a/whisper/whisperv6/filter_test.go
+++ b/whisper/whisperv6/filter_test.go
@@ -800,6 +800,7 @@ func TestWatchers(t *testing.T) {
func TestVariableTopics(t *testing.T) {
InitSingleTest()
+ const lastTopicByte = 3
var match bool
params, err := generateMessageParams()
if err != nil {
@@ -820,19 +821,52 @@ func TestVariableTopics(t *testing.T) {
}
for i := 0; i < 4; i++ {
- arr := make([]byte, i+1, 4)
- copy(arr, env.Topic[:i+1])
-
- f.Topics[4] = arr
+ env.Topic = BytesToTopic(f.Topics[i])
match = f.MatchEnvelope(env)
if !match {
t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i)
}
- f.Topics[4][i]++
+ f.Topics[i][lastTopicByte]++
match = f.MatchEnvelope(env)
if match {
t.Fatalf("MatchEnvelope symmetric with seed %d, step %d: false positive.", seed, i)
}
}
}
+
+func TestMatchSingleTopic_ReturnTrue(t *testing.T) {
+ bt := []byte("test")
+ topic := BytesToTopic(bt)
+
+ if !matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_WithTail_ReturnTrue(t *testing.T) {
+ bt := []byte("test with tail")
+ topic := BytesToTopic([]byte("test"))
+
+ if !matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_NotEquals_ReturnFalse(t *testing.T) {
+ bt := []byte("tes")
+ topic := BytesToTopic(bt)
+
+ if matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}
+
+func TestMatchSingleTopic_InsufficientLength_ReturnFalse(t *testing.T) {
+ bt := []byte("test")
+ topic := BytesToTopic([]byte("not_equal"))
+
+ if matchSingleTopic(topic, bt) {
+ t.FailNow()
+ }
+}