2020-04-04 00:32:03 +05:30
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestANDCondition(t *testing.T) {
|
|
|
|
|
m := &Matcher{condition: ANDCondition, Words: []string{"a", "b"}}
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("a b", nil)
|
|
|
|
|
require.True(t, isMatched, "Could not match valid AND condition")
|
|
|
|
|
require.Equal(t, m.Words, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("b", nil)
|
|
|
|
|
require.False(t, isMatched, "Could match invalid AND condition")
|
|
|
|
|
require.Equal(t, []string{}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestORCondition(t *testing.T) {
|
|
|
|
|
m := &Matcher{condition: ORCondition, Words: []string{"a", "b"}}
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("a b", nil)
|
|
|
|
|
require.True(t, isMatched, "Could not match valid OR condition")
|
|
|
|
|
require.Equal(t, []string{"a"}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("b", nil)
|
|
|
|
|
require.True(t, isMatched, "Could not match valid OR condition")
|
|
|
|
|
require.Equal(t, []string{"b"}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("c", nil)
|
|
|
|
|
require.False(t, isMatched, "Could match invalid OR condition")
|
|
|
|
|
require.Equal(t, []string{}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
2021-02-24 11:23:22 +05:30
|
|
|
|
|
|
|
|
func TestHexEncoding(t *testing.T) {
|
|
|
|
|
m := &Matcher{Encoding: "hex", Type: "word", Part: "body", Words: []string{"50494e47"}}
|
|
|
|
|
err := m.CompileMatchers()
|
|
|
|
|
require.Nil(t, err, "could not compile matcher")
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("PING", nil)
|
|
|
|
|
require.True(t, isMatched, "Could not match valid Hex condition")
|
|
|
|
|
require.Equal(t, m.Words, matched)
|
2021-02-24 11:23:22 +05:30
|
|
|
}
|