MCPcopy Index your code
hub / github.com/crowdsecurity/crowdsec / TestRegexpInFile

Function TestRegexpInFile

pkg/exprhelpers/exprlib_test.go:302–359  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

300}
301
302func TestRegexpInFile(t *testing.T) {
303 if err := Init(nil); err != nil {
304 t.Fatal(err)
305 }
306
307 err := FileInit("testdata", "test_data_re.txt", "regex")
308 if err != nil {
309 t.Fatal(err)
310 }
311
312 tests := []struct {
313 name string
314 filter string
315 result bool
316 err error
317 }{
318 {
319 name: "RegexpInFile() test: lower case word in data file",
320 filter: "RegexpInFile('crowdsec', 'test_data_re.txt')",
321 result: false,
322 err: nil,
323 },
324 {
325 name: "RegexpInFile() test: Match exactly",
326 filter: "RegexpInFile('Crowdsec', 'test_data_re.txt')",
327 result: true,
328 err: nil,
329 },
330 {
331 name: "RegexpInFile() test: match with word before",
332 filter: "RegexpInFile('test Crowdsec', 'test_data_re.txt')",
333 result: true,
334 err: nil,
335 },
336 {
337 name: "RegexpInFile() test: match with word before and other case",
338 filter: "RegexpInFile('test CrowdSec', 'test_data_re.txt')",
339 result: true,
340 err: nil,
341 },
342 }
343
344 for _, test := range tests {
345 compiledFilter, err := expr.Compile(test.filter, GetExprOptions(map[string]any{})...)
346 if err != nil {
347 t.Fatal(err)
348 }
349
350 result, err := expr.Run(compiledFilter, map[string]any{})
351 if err != nil {
352 t.Fatal(err)
353 }
354
355 if isOk := assert.Equal(t, test.result, result); !isOk {
356 t.Fatalf("test '%s': NOK", test.name)
357 }
358 }
359}

Callers

nothing calls this directly

Calls 5

InitFunction · 0.85
FileInitFunction · 0.85
GetExprOptionsFunction · 0.85
RunMethod · 0.65
CompileMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…