|
@@ -4,22 +4,20 @@ import (
|
|
"context"
|
|
"context"
|
|
"fmt"
|
|
"fmt"
|
|
"os"
|
|
"os"
|
|
|
|
+ "testing"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
+ "github.com/antonmedv/expr"
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
|
|
+ log "github.com/sirupsen/logrus"
|
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
|
+ "github.com/stretchr/testify/require"
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
- log "github.com/sirupsen/logrus"
|
|
|
|
-
|
|
|
|
- "testing"
|
|
|
|
-
|
|
|
|
- "github.com/antonmedv/expr"
|
|
|
|
- "github.com/stretchr/testify/assert"
|
|
|
|
- "github.com/stretchr/testify/require"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -29,24 +27,21 @@ var (
|
|
func getDBClient(t *testing.T) *database.Client {
|
|
func getDBClient(t *testing.T) *database.Client {
|
|
t.Helper()
|
|
t.Helper()
|
|
dbPath, err := os.CreateTemp("", "*sqlite")
|
|
dbPath, err := os.CreateTemp("", "*sqlite")
|
|
- if err != nil {
|
|
|
|
- t.Fatal(err)
|
|
|
|
- }
|
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
testDbClient, err := database.NewClient(&csconfig.DatabaseCfg{
|
|
testDbClient, err := database.NewClient(&csconfig.DatabaseCfg{
|
|
Type: "sqlite",
|
|
Type: "sqlite",
|
|
DbName: "crowdsec",
|
|
DbName: "crowdsec",
|
|
DbPath: dbPath.Name(),
|
|
DbPath: dbPath.Name(),
|
|
})
|
|
})
|
|
- if err != nil {
|
|
|
|
- t.Fatal(err)
|
|
|
|
- }
|
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
return testDbClient
|
|
return testDbClient
|
|
}
|
|
}
|
|
|
|
|
|
func TestVisitor(t *testing.T) {
|
|
func TestVisitor(t *testing.T) {
|
|
- if err := Init(nil); err != nil {
|
|
|
|
- log.Fatal(err)
|
|
|
|
- }
|
|
|
|
|
|
+ err := Init(nil)
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
tests := []struct {
|
|
tests := []struct {
|
|
name string
|
|
name string
|
|
@@ -130,6 +125,39 @@ func TestVisitor(t *testing.T) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func TestRegexpCacheBehavior(t *testing.T) {
|
|
|
|
+ err := Init(nil)
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ filename := "test_data_re.txt"
|
|
|
|
+ err = FileInit(TestFolder, filename, "regex")
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ //cache with no TTL
|
|
|
|
+ err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: types.IntPtr(1)})
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ ret := RegexpInFile("crowdsec", filename)
|
|
|
|
+ assert.False(t, ret)
|
|
|
|
+ assert.Equal(t, 1, dataFileRegexCache[filename].Len(false))
|
|
|
|
+
|
|
|
|
+ ret = RegexpInFile("Crowdsec", filename)
|
|
|
|
+ assert.True(t, ret)
|
|
|
|
+ assert.Equal(t, 1, dataFileRegexCache[filename].Len(false))
|
|
|
|
+
|
|
|
|
+ //cache with TTL
|
|
|
|
+ ttl := 500 * time.Millisecond
|
|
|
|
+ err = RegexpCacheInit(filename, types.DataSource{Type: "regex", Size: types.IntPtr(2), TTL: &ttl})
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+
|
|
|
|
+ ret = RegexpInFile("crowdsec", filename)
|
|
|
|
+ assert.False(t, ret)
|
|
|
|
+ assert.Equal(t, 1, dataFileRegexCache[filename].Len(true))
|
|
|
|
+
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
+ assert.Equal(t, 0, dataFileRegexCache[filename].Len(true))
|
|
|
|
+}
|
|
|
|
+
|
|
func TestRegexpInFile(t *testing.T) {
|
|
func TestRegexpInFile(t *testing.T) {
|
|
if err := Init(nil); err != nil {
|
|
if err := Init(nil); err != nil {
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
@@ -449,7 +477,7 @@ func TestAtof(t *testing.T) {
|
|
expectedFloat := 1.5
|
|
expectedFloat := 1.5
|
|
|
|
|
|
if Atof(testFloat) != expectedFloat {
|
|
if Atof(testFloat) != expectedFloat {
|
|
- t.Fatalf("Atof should returned 1.5 as a float")
|
|
|
|
|
|
+ t.Fatalf("Atof should return 1.5 as a float")
|
|
}
|
|
}
|
|
|
|
|
|
log.Printf("test 'Atof()' : OK")
|
|
log.Printf("test 'Atof()' : OK")
|
|
@@ -459,7 +487,7 @@ func TestAtof(t *testing.T) {
|
|
expectedFloat = 0.0
|
|
expectedFloat = 0.0
|
|
|
|
|
|
if Atof(testFloat) != expectedFloat {
|
|
if Atof(testFloat) != expectedFloat {
|
|
- t.Fatalf("Atof should returned a negative value (error) as a float got")
|
|
|
|
|
|
+ t.Fatalf("Atof should return a negative value (error) as a float got")
|
|
}
|
|
}
|
|
|
|
|
|
log.Printf("test 'Atof()' : OK")
|
|
log.Printf("test 'Atof()' : OK")
|
|
@@ -470,7 +498,7 @@ func TestUpper(t *testing.T) {
|
|
expectedStr := "TEST"
|
|
expectedStr := "TEST"
|
|
|
|
|
|
if Upper(testStr) != expectedStr {
|
|
if Upper(testStr) != expectedStr {
|
|
- t.Fatalf("Upper() should returned test in upper case")
|
|
|
|
|
|
+ t.Fatalf("Upper() should return test in upper case")
|
|
}
|
|
}
|
|
|
|
|
|
log.Printf("test 'Upper()' : OK")
|
|
log.Printf("test 'Upper()' : OK")
|
|
@@ -503,7 +531,7 @@ func TestParseUri(t *testing.T) {
|
|
"ParseUri": ParseUri,
|
|
"ParseUri": ParseUri,
|
|
},
|
|
},
|
|
code: "ParseUri(uri)",
|
|
code: "ParseUri(uri)",
|
|
- result: map[string][]string{"a": []string{"1"}, "b": []string{"2"}},
|
|
|
|
|
|
+ result: map[string][]string{"a": {"1"}, "b": {"2"}},
|
|
err: "",
|
|
err: "",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -523,7 +551,7 @@ func TestParseUri(t *testing.T) {
|
|
"ParseUri": ParseUri,
|
|
"ParseUri": ParseUri,
|
|
},
|
|
},
|
|
code: "ParseUri(uri)",
|
|
code: "ParseUri(uri)",
|
|
- result: map[string][]string{"a": []string{"1"}, "b": []string{"2?"}},
|
|
|
|
|
|
+ result: map[string][]string{"a": {"1"}, "b": {"2?"}},
|
|
err: "",
|
|
err: "",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -533,7 +561,7 @@ func TestParseUri(t *testing.T) {
|
|
"ParseUri": ParseUri,
|
|
"ParseUri": ParseUri,
|
|
},
|
|
},
|
|
code: "ParseUri(uri)",
|
|
code: "ParseUri(uri)",
|
|
- result: map[string][]string{"?": []string{"", "123"}},
|
|
|
|
|
|
+ result: map[string][]string{"?": {"", "123"}},
|
|
err: "",
|
|
err: "",
|
|
},
|
|
},
|
|
{
|
|
{
|