whisper: add tests

This commit is contained in:
b00ris 2018-01-10 21:45:15 +03:00
parent 40f686b21d
commit faa510618e
No known key found for this signature in database
GPG key ID: 2F73E159EB73232C
6 changed files with 76 additions and 18 deletions

View file

@ -562,7 +562,7 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
} }
if len(req.Topics) > 0 { if len(req.Topics) > 0 {
topics = make([][]byte, 0, 1) topics = make([][]byte, 0, len(req.Topics))
for _, topic := range req.Topics { for _, topic := range req.Topics {
topics = append(topics, topic[:]) topics = append(topics, topic[:])
} }

View file

@ -216,11 +216,11 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
} }
func matchSingleTopic(topic TopicType, bt []byte) bool { func matchSingleTopic(topic TopicType, bt []byte) bool {
if len(bt) > 4 { if len(bt) > TopicLength {
bt = bt[:4] bt = bt[:TopicLength]
} }
if len(bt) < 4 { if len(bt) < TopicLength {
return false return false
} }

View file

@ -796,19 +796,48 @@ func TestVariableTopics(t *testing.T) {
} }
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
arr := make([]byte, i+1, 4) env.Topic = BytesToTopic(f.Topics[i])
copy(arr, env.Topic[:i+1])
f.Topics[4] = arr
match = f.MatchEnvelope(env) match = f.MatchEnvelope(env)
if !match { if !match {
t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i) t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i)
} }
f.Topics[4][i]++ f.Topics[i][1]++
match = f.MatchEnvelope(env) match = f.MatchEnvelope(env)
if match { if match {
t.Fatalf("MatchEnvelope symmetric with seed %d, step %d: false positive.", seed, i) 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) == false {
t.FailNow()
}
}
func TestMatchSingleTopic_WithTail_ReturnTrue(t *testing.T) {
bt := []byte("test with tail")
topic := BytesToTopic([]byte("test"))
if matchSingleTopic(topic, bt) == false {
t.FailNow()
}
}
func TestMatchSingleTopic_NotEquals_ReturnFalse(t *testing.T) {
bt := []byte("tes")
topic := BytesToTopic(bt)
if matchSingleTopic(topic, bt) == true {
t.FailNow()
}
}
func TestMatchSingleTopic_InsufficientLength_ReturnFalse(t *testing.T) {
bt := []byte("test")
topic := BytesToTopic([]byte("not_equal"))
if matchSingleTopic(topic, bt) == true {
t.FailNow()
}
}

View file

@ -562,7 +562,7 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
} }
if len(req.Topics) > 0 { if len(req.Topics) > 0 {
topics = make([][]byte, 0, 1) topics = make([][]byte, 0, len(req.Topics))
for _, topic := range req.Topics { for _, topic := range req.Topics {
topics = append(topics, topic[:]) topics = append(topics, topic[:])
} }

View file

@ -221,11 +221,11 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
} }
func matchSingleTopic(topic TopicType, bt []byte) bool { func matchSingleTopic(topic TopicType, bt []byte) bool {
if len(bt) > 4 { if len(bt) > TopicLength {
bt = bt[:4] bt = bt[:TopicLength]
} }
if len(bt) < 4 { if len(bt) < TopicLength {
return false return false
} }

View file

@ -820,19 +820,48 @@ func TestVariableTopics(t *testing.T) {
} }
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
arr := make([]byte, i+1, 4) env.Topic = BytesToTopic(f.Topics[i])
copy(arr, env.Topic[:i+1])
f.Topics[4] = arr
match = f.MatchEnvelope(env) match = f.MatchEnvelope(env)
if !match { if !match {
t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i) t.Fatalf("failed MatchEnvelope symmetric with seed %d, step %d.", seed, i)
} }
f.Topics[4][i]++ f.Topics[i][1]++
match = f.MatchEnvelope(env) match = f.MatchEnvelope(env)
if match { if match {
t.Fatalf("MatchEnvelope symmetric with seed %d, step %d: false positive.", seed, i) 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) == false {
t.FailNow()
}
}
func TestMatchSingleTopic_WithTail_ReturnTrue(t *testing.T) {
bt := []byte("test with tail")
topic := BytesToTopic([]byte("test"))
if matchSingleTopic(topic, bt) == false {
t.FailNow()
}
}
func TestMatchSingleTopic_NotEquals_ReturnFalse(t *testing.T) {
bt := []byte("tes")
topic := BytesToTopic(bt)
if matchSingleTopic(topic, bt) == true {
t.FailNow()
}
}
func TestMatchSingleTopic_InsufficientLength_ReturnFalse(t *testing.T) {
bt := []byte("test")
topic := BytesToTopic([]byte("not_equal"))
if matchSingleTopic(topic, bt) == true {
t.FailNow()
}
}