2022-05-17 10:14:59 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package csplugin
|
|
|
|
|
|
|
|
import (
|
2023-06-05 20:58:13 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-05-17 10:14:59 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-01 08:33:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-17 12:17:23 +00:00
|
|
|
"gopkg.in/tomb.v2"
|
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2023-05-25 13:37:44 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2022-08-26 11:31:49 +00:00
|
|
|
Due to the complexity of file permission modification with go on windows, we only test the basic behavior the broker,
|
2022-05-17 10:14:59 +00:00
|
|
|
not if it will actually reject plugins with invalid permissions
|
|
|
|
*/
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerInit() {
|
2022-05-17 10:14:59 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-06-01 08:33:08 +00:00
|
|
|
action func(*testing.T)
|
2022-05-17 10:14:59 +00:00
|
|
|
procCfg csconfig.PluginCfg
|
2023-06-01 08:33:08 +00:00
|
|
|
expectedErr string
|
2022-05-17 10:14:59 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-06-22 09:31:41 +00:00
|
|
|
name: "valid config",
|
2022-05-17 10:14:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no plugin dir",
|
2023-06-01 08:33:08 +00:00
|
|
|
expectedErr: cstest.PathNotFoundMessage,
|
|
|
|
action: func(t *testing.T) {
|
|
|
|
err := os.RemoveAll(s.runDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-05-17 10:14:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no plugin binary",
|
2023-06-01 08:33:08 +00:00
|
|
|
expectedErr: "binary for plugin dummy_default not found",
|
|
|
|
action: func(t *testing.T) {
|
|
|
|
err := os.Remove(s.pluginBinary)
|
|
|
|
require.NoError(t, err)
|
2022-05-17 10:14:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
t := s.T()
|
|
|
|
if tc.action != nil {
|
|
|
|
tc.action(t)
|
2022-05-17 10:14:59 +00:00
|
|
|
}
|
2023-06-01 08:33:08 +00:00
|
|
|
_, err := s.InitBroker(&tc.procCfg)
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2022-05-17 10:14:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRun() {
|
|
|
|
t := s.T()
|
|
|
|
|
|
|
|
pb, err := s.InitBroker(nil)
|
2022-05-17 10:14:59 +00:00
|
|
|
assert.NoError(t, err)
|
2023-06-01 08:33:08 +00:00
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
tomb := tomb.Tomb{}
|
|
|
|
go pb.Run(&tomb)
|
|
|
|
|
|
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
defer os.Remove("./out")
|
|
|
|
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
|
|
|
|
assert.FileExists(t, ".\\out")
|
2023-06-05 20:58:13 +00:00
|
|
|
|
|
|
|
content, err := os.ReadFile("./out")
|
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
|
|
|
|
|
|
var alerts []models.Alert
|
|
|
|
|
|
|
|
// two notifications, one alert each
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.Equal(t, err, io.EOF)
|
2022-05-17 10:14:59 +00:00
|
|
|
}
|