Add unit tests for case-insensitive flag in protocols

This commit is contained in:
Alexey Zhuchkov 2021-10-29 19:11:09 +03:00
parent bfb69b2ff2
commit 897f11213c
4 changed files with 94 additions and 0 deletions

View File

@ -134,6 +134,30 @@ func TestDNSOperatorMatch(t *testing.T) {
require.False(t, isMatched, "could match invalid response matcher")
require.Equal(t, []string{}, matched)
})
t.Run("caseInsensitive", func(t *testing.T) {
req := new(dns.Msg)
req.Question = append(req.Question, dns.Question{Name: "ONE.ONE.ONE.ONE.", Qtype: dns.TypeA, Qclass: dns.ClassINET})
resp := new(dns.Msg)
resp.Rcode = dns.RcodeSuccess
resp.Answer = append(resp.Answer, &dns.A{A: net.ParseIP("1.1.1.1"), Hdr: dns.RR_Header{Name: "ONE.ONE.ONE.ONE."}})
event := request.responseToDSLMap(req, resp, "ONE.ONE.ONE.ONE", "ONE.ONE.ONE.ONE")
matcher := &matchers.Matcher{
Part: "raw",
Type: "word",
Words: []string{"one.ONE.one.ONE"},
CaseInsensitive: true,
}
err = matcher.CompileMatchers()
require.Nil(t, err, "could not compile matcher")
isMatch, matched := request.Match(event, matcher)
require.True(t, isMatch, "could not match valid response")
require.Equal(t, []string{"one.one.one.one"}, matched)
})
}
func TestDNSOperatorExtract(t *testing.T) {

View File

@ -105,6 +105,26 @@ func TestFileOperatorMatch(t *testing.T) {
require.False(t, isMatched, "could match invalid response matcher")
require.Equal(t, []string{}, matched)
})
t.Run("caseInsensitive", func(t *testing.T) {
resp := "TEST-DATA\r\n1.1.1.1\r\n"
event := request.responseToDSLMap(resp, "one.one.one.one", "one.one.one.one")
require.Len(t, event, 6, "could not get correct number of items in dsl map")
require.Equal(t, resp, event["raw"], "could not get correct resp")
matcher := &matchers.Matcher{
Part: "raw",
Type: "word",
Words: []string{"TeSt-DaTA"},
CaseInsensitive: true,
}
err = matcher.CompileMatchers()
require.Nil(t, err, "could not compile matcher")
isMatched, matched := request.Match(event, matcher)
require.True(t, isMatched, "could not match valid response")
require.Equal(t, []string{"test-data"}, matched)
})
}
func TestFileOperatorExtract(t *testing.T) {

View File

@ -117,6 +117,21 @@ func TestHTTPOperatorMatch(t *testing.T) {
require.False(t, isMatched, "could match invalid response matcher")
require.Equal(t, []string{}, matched)
})
t.Run("caseInsensitive", func(t *testing.T) {
matcher := &matchers.Matcher{
Part: "body",
Type: "word", // only applies to word
Words: []string{"EXAMPLE DOMAIN"},
CaseInsensitive: true,
}
err = matcher.CompileMatchers()
require.Nil(t, err, "could not compile matcher")
isMatched, matched := request.Match(event, matcher)
require.True(t, isMatched, "could not match valid response")
require.Equal(t, []string{"example domain"}, matched)
})
}
func TestHTTPOperatorExtract(t *testing.T) {
@ -215,6 +230,22 @@ func TestHTTPOperatorExtract(t *testing.T) {
require.Equal(t, map[string]struct{}{"{\"batter\":[{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\",\"type\":\"Chocolate\"},{\"id\":\"1003\",\"type\":\"Blueberry\"},{\"id\":\"1004\",\"type\":\"Devil's Food\"}]}": {}}, data, "could not extract correct json data")
})
})
t.Run("caseInsensitive", func(t *testing.T) {
event["body"] = exampleResponseBody
extractor := &extractors.Extractor{
Type: "kval",
KVal: []string{"TEST_HEADER"}, // only applies to KVal
CaseInsensitive: true,
}
err = extractor.CompileExtractors()
require.Nil(t, err, "could not compile kval extractor")
data := request.Extract(event, extractor)
require.Greater(t, len(data), 0, "could not extractor kval valid response")
require.Equal(t, map[string]struct{}{"test-response": {}}, data, "could not extract correct kval data")
})
}
func TestHTTPMakeResult(t *testing.T) {

View File

@ -103,6 +103,25 @@ func TestNetworkOperatorMatch(t *testing.T) {
require.False(t, isMatched, "could match invalid response matcher")
require.Equal(t, []string{}, matched)
})
t.Run("caseInsensitive", func(t *testing.T) {
matcher := &matchers.Matcher{
Part: "body",
Type: "word",
Words: []string{"rESp-DAta"},
CaseInsensitive: true,
}
err = matcher.CompileMatchers()
require.Nil(t, err, "could not compile matcher")
req := "TEST-DATA\r\n"
resp := "RESP-DATA\r\nSTAT \r\n"
event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", "TEST")
isMatched, matched := request.Match(event, matcher)
require.True(t, isMatched, "could not match valid response")
require.Equal(t, []string{"resp-data"}, matched)
})
}
func TestNetworkOperatorExtract(t *testing.T) {