Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SetMeta and SetParsed helpers #2845

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
Meta map[string]string `yaml:"Meta,omitempty" json:"Meta,omitempty"`
}

func (e *Event) SetMeta(key string, value string) bool {
if e.Meta == nil {
e.Meta = make(map[string]string)
}
e.Meta[key] = value
return true

Check warning on line 54 in pkg/types/event.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/event.go#L49-L54

Added lines #L49 - L54 were not covered by tests
}

func (e *Event) SetParsed(key string, value string) bool {
if e.Parsed == nil {
e.Parsed = make(map[string]string)
}
e.Parsed[key] = value
return true

Check warning on line 62 in pkg/types/event.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/event.go#L57-L62

Added lines #L57 - L62 were not covered by tests
}

func (e *Event) GetType() string {
if e.Type == OVFLW {
return "overflow"
Expand Down
82 changes: 82 additions & 0 deletions pkg/types/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,88 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models"
)

func TestSetParsed(t *testing.T) {
tests := []struct {
name string
evt *Event
key string
value string
expected bool
}{
{
name: "SetParsed: Valid",
evt: &Event{},
key: "test",
value: "test",
expected: true,
},
{
name: "SetParsed: Existing map",
evt: &Event{Parsed: map[string]string{}},
key: "test",
value: "test",
expected: true,
},
{
name: "SetParsed: Existing map+key",
evt: &Event{Parsed: map[string]string{"test": "foobar"}},
key: "test",
value: "test",
expected: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.evt.SetParsed(tt.key, tt.value)
assert.Equal(t, tt.value, tt.evt.Parsed[tt.key])
})
}

}

func TestSetMeta(t *testing.T) {
tests := []struct {
name string
evt *Event
key string
value string
expected bool
}{
{
name: "SetMeta: Valid",
evt: &Event{},
key: "test",
value: "test",
expected: true,
},
{
name: "SetMeta: Existing map",
evt: &Event{Meta: map[string]string{}},
key: "test",
value: "test",
expected: true,
},
{
name: "SetMeta: Existing map+key",
evt: &Event{Meta: map[string]string{"test": "foobar"}},
key: "test",
value: "test",
expected: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.evt.SetMeta(tt.key, tt.value)
assert.Equal(t, tt.value, tt.evt.GetMeta(tt.key))
})
}

}

func TestParseIPSources(t *testing.T) {
tests := []struct {
name string
Expand Down