2022-03-29 12:20:26 +00:00
|
|
|
package csplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2022-05-17 10:14:59 +00:00
|
|
|
"runtime"
|
2022-03-29 12:20:26 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-06-22 09:31:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-29 12:20:26 +00:00
|
|
|
"gopkg.in/tomb.v2"
|
2023-06-22 09:31:41 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2023-06-22 09:31:41 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2022-03-29 12:20:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
2022-08-30 13:45:52 +00:00
|
|
|
func resetTestTomb(testTomb *tomb.Tomb, pw *PluginWatcher) {
|
2022-03-29 12:20:26 +00:00
|
|
|
testTomb.Kill(nil)
|
2022-08-30 13:45:52 +00:00
|
|
|
<-pw.PluginEvents
|
2022-03-29 12:20:26 +00:00
|
|
|
if err := testTomb.Wait(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetWatcherAlertCounter(pw *PluginWatcher) {
|
2022-04-19 17:12:23 +00:00
|
|
|
pw.AlertCountByPluginName.Lock()
|
|
|
|
for k := range pw.AlertCountByPluginName.data {
|
|
|
|
pw.AlertCountByPluginName.data[k] = 0
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
2022-04-19 17:12:23 +00:00
|
|
|
pw.AlertCountByPluginName.Unlock()
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertNAlertsToPlugin(pw *PluginWatcher, n int, pluginName string) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
pw.Inserts <- pluginName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenChannelWithTimeout(ctx context.Context, channel chan string) error {
|
|
|
|
select {
|
2022-04-19 17:12:23 +00:00
|
|
|
case x := <-channel:
|
|
|
|
log.Printf("received -> %v", x)
|
2022-03-29 12:20:26 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginWatcherInterval(t *testing.T) {
|
2022-05-17 10:14:59 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Skipping test on windows because timing is not reliable")
|
|
|
|
}
|
2022-03-29 12:20:26 +00:00
|
|
|
pw := PluginWatcher{}
|
|
|
|
alertsByPluginName := make(map[string][]*models.Alert)
|
|
|
|
testTomb := tomb.Tomb{}
|
|
|
|
configs := map[string]PluginConfig{
|
|
|
|
"testPlugin": {
|
|
|
|
GroupWait: time.Millisecond,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pw.Init(configs, alertsByPluginName)
|
|
|
|
pw.Start(&testTomb)
|
|
|
|
|
|
|
|
ct, cancel := context.WithTimeout(ctx, time.Microsecond)
|
|
|
|
defer cancel()
|
|
|
|
err := listenChannelWithTimeout(ct, pw.PluginEvents)
|
2023-06-22 09:31:41 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "context deadline exceeded")
|
2022-08-30 13:45:52 +00:00
|
|
|
resetTestTomb(&testTomb, &pw)
|
2022-03-29 12:20:26 +00:00
|
|
|
testTomb = tomb.Tomb{}
|
|
|
|
pw.Start(&testTomb)
|
|
|
|
|
|
|
|
ct, cancel = context.WithTimeout(ctx, time.Millisecond*5)
|
|
|
|
defer cancel()
|
|
|
|
err = listenChannelWithTimeout(ct, pw.PluginEvents)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 13:45:52 +00:00
|
|
|
resetTestTomb(&testTomb, &pw)
|
2022-03-29 12:20:26 +00:00
|
|
|
// This is to avoid the int complaining
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginAlertCountWatcher(t *testing.T) {
|
2022-05-17 10:14:59 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Skipping test on windows because timing is not reliable")
|
|
|
|
}
|
2022-03-29 12:20:26 +00:00
|
|
|
pw := PluginWatcher{}
|
|
|
|
alertsByPluginName := make(map[string][]*models.Alert)
|
|
|
|
configs := map[string]PluginConfig{
|
|
|
|
"testPlugin": {
|
|
|
|
GroupThreshold: 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testTomb := tomb.Tomb{}
|
|
|
|
pw.Init(configs, alertsByPluginName)
|
|
|
|
pw.Start(&testTomb)
|
|
|
|
|
|
|
|
// Channel won't contain any events since threshold is not crossed.
|
|
|
|
ct, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := listenChannelWithTimeout(ct, pw.PluginEvents)
|
2023-06-22 09:31:41 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "context deadline exceeded")
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// Channel won't contain any events since threshold is not crossed.
|
|
|
|
resetWatcherAlertCounter(&pw)
|
|
|
|
insertNAlertsToPlugin(&pw, 4, "testPlugin")
|
|
|
|
ct, cancel = context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = listenChannelWithTimeout(ct, pw.PluginEvents)
|
2023-06-22 09:31:41 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "context deadline exceeded")
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
// Channel will contain an event since threshold is crossed.
|
|
|
|
resetWatcherAlertCounter(&pw)
|
|
|
|
insertNAlertsToPlugin(&pw, 5, "testPlugin")
|
|
|
|
ct, cancel = context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = listenChannelWithTimeout(ct, pw.PluginEvents)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 13:45:52 +00:00
|
|
|
resetTestTomb(&testTomb, &pw)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|