Skip to content

Commit

Permalink
whisper: fix empty topic (ethereum#15811)
Browse files Browse the repository at this point in the history
* whisper: fix empty topic

* whisper: add check to matchSingleTopic

* whisper: add tests

* whisper: fix gosimple

* whisper: added lastTopicByte const
  • Loading branch information
b00ris authored and karalabe committed Jan 26, 2018
1 parent 4dd0727 commit 2ef3815
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 16 deletions.
2 changes: 1 addition & 1 deletion whisper/whisperv5/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
}
Expand Down
8 changes: 6 additions & 2 deletions whisper/whisperv5/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 39 additions & 5 deletions whisper/whisperv5/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
2 changes: 1 addition & 1 deletion whisper/whisperv6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
}
Expand Down
8 changes: 6 additions & 2 deletions whisper/whisperv6/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 39 additions & 5 deletions whisper/whisperv6/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}

0 comments on commit 2ef3815

Please sign in to comment.