123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package alertcontext
- import (
- "fmt"
- "testing"
- "github.com/crowdsecurity/crowdsec/pkg/models"
- "github.com/crowdsecurity/crowdsec/pkg/types"
- "github.com/stretchr/testify/assert"
- )
- func TestNewAlertContext(t *testing.T) {
- tests := []struct {
- name string
- contextToSend map[string][]string
- valueLength int
- expectedErr error
- }{
- {
- name: "basic config test",
- contextToSend: map[string][]string{
- "test": {"evt.Parsed.source_ip"},
- },
- valueLength: 100,
- expectedErr: nil,
- },
- }
- for _, test := range tests {
- fmt.Printf("Running test '%s'\n", test.name)
- err := NewAlertContext(test.contextToSend, test.valueLength)
- assert.ErrorIs(t, err, test.expectedErr)
- }
- }
- func TestEventToContext(t *testing.T) {
- tests := []struct {
- name string
- contextToSend map[string][]string
- valueLength int
- events []types.Event
- expectedResult models.Meta
- }{
- {
- name: "basic test",
- contextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- "nonexistent_field": {"evt.Parsed.nonexist"},
- },
- valueLength: 100,
- events: []types.Event{
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- },
- },
- },
- expectedResult: []*models.MetaItems0{
- {
- Key: "source_ip",
- Value: "[\"1.2.3.4\"]",
- },
- },
- },
- {
- name: "test many events",
- contextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- "source_machine": {"evt.Parsed.source_machine"},
- "cve": {"evt.Parsed.cve"},
- },
- valueLength: 100,
- events: []types.Event{
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "cve": "CVE-2022-1234",
- },
- },
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "cve": "CVE-2022-1235",
- },
- },
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "cve": "CVE-2022-125",
- },
- },
- },
- expectedResult: []*models.MetaItems0{
- {
- Key: "source_ip",
- Value: "[\"1.2.3.4\"]",
- },
- {
- Key: "source_machine",
- Value: "[\"mymachine\"]",
- },
- {
- Key: "cve",
- Value: "[\"CVE-2022-1234\",\"CVE-2022-1235\",\"CVE-2022-125\"]",
- },
- },
- },
- {
- name: "test many events with result above max length (need truncate, keep only 2 on 3 elements)",
- contextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- "source_machine": {"evt.Parsed.source_machine"},
- "uri": {"evt.Parsed.uri"},
- },
- valueLength: 100,
- events: []types.Event{
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "uri": "/test/test/test/../../../../../../../../",
- },
- },
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "uri": "/admin/admin/admin/../../../../../../../../",
- },
- },
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "uri": "/login/login/login/../../../../../../../../../../../",
- },
- },
- },
- expectedResult: []*models.MetaItems0{
- {
- Key: "source_ip",
- Value: "[\"1.2.3.4\"]",
- },
- {
- Key: "source_machine",
- Value: "[\"mymachine\"]",
- },
- {
- Key: "uri",
- Value: "[\"/test/test/test/../../../../../../../../\",\"/admin/admin/admin/../../../../../../../../\"]",
- },
- },
- },
- {
- name: "test one events with result above max length (need truncate on one element)",
- contextToSend: map[string][]string{
- "source_ip": {"evt.Parsed.source_ip"},
- "source_machine": {"evt.Parsed.source_machine"},
- "uri": {"evt.Parsed.uri"},
- },
- valueLength: 100,
- events: []types.Event{
- {
- Parsed: map[string]string{
- "source_ip": "1.2.3.4",
- "source_machine": "mymachine",
- "uri": "/test/test/test/../../../../.should_truncate_just_after_this/../../../..../../../../../../../../../../../../../../../end",
- },
- },
- },
- expectedResult: []*models.MetaItems0{
- {
- Key: "source_machine",
- Value: "[\"mymachine\"]",
- },
- {
- Key: "uri",
- Value: "[\"/test/test/test/../../../../.should_truncate_just_after_this...\"]",
- },
- {
- Key: "source_ip",
- Value: "[\"1.2.3.4\"]",
- },
- },
- },
- }
- for _, test := range tests {
- fmt.Printf("Running test '%s'\n", test.name)
- err := NewAlertContext(test.contextToSend, test.valueLength)
- assert.ErrorIs(t, err, nil)
- metas, _ := EventToContext(test.events)
- assert.ElementsMatch(t, test.expectedResult, metas)
- }
- }
|