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

Function TestMatch

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

Source from the content-addressed store, hash-verified

112}
113
114func TestMatch(t *testing.T) {
115 err := Init(nil)
116 require.NoError(t, err)
117
118 tests := []struct {
119 glob string
120 val string
121 ret bool
122 expr string
123 }{
124 {"foo", "foo", true, `Match(pattern, name)`},
125 {"foo", "bar", false, `Match(pattern, name)`},
126 {"foo*", "foo", true, `Match(pattern, name)`},
127 {"foo*", "foobar", true, `Match(pattern, name)`},
128 {"foo*", "barfoo", false, `Match(pattern, name)`},
129 {"foo*", "bar", false, `Match(pattern, name)`},
130 {"*foo", "foo", true, `Match(pattern, name)`},
131 {"*foo", "barfoo", true, `Match(pattern, name)`},
132 {"foo*r", "foobar", true, `Match(pattern, name)`},
133 {"foo*r", "foobazr", true, `Match(pattern, name)`},
134 {"foo?ar", "foobar", true, `Match(pattern, name)`},
135 {"foo?ar", "foobazr", false, `Match(pattern, name)`},
136 {"foo?ar", "foobaz", false, `Match(pattern, name)`},
137 {"*foo?ar?", "foobar", false, `Match(pattern, name)`},
138 {"*foo?ar?", "foobare", true, `Match(pattern, name)`},
139 {"*foo?ar?", "rafoobar", false, `Match(pattern, name)`},
140 {"*foo?ar?", "rafoobare", true, `Match(pattern, name)`},
141 }
142 for _, test := range tests {
143 env := map[string]any{
144 "pattern": test.glob,
145 "name": test.val,
146 }
147
148 vm, err := expr.Compile(test.expr, GetExprOptions(env)...)
149 if err != nil {
150 t.Fatalf("pattern:%s val:%s NOK %s", test.glob, test.val, err)
151 }
152
153 ret, err := expr.Run(vm, env)
154 require.NoError(t, err)
155
156 if isOk := assert.Equal(t, test.ret, ret); !isOk {
157 t.Fatalf("pattern:%s val:%s NOK %t != %t", test.glob, test.val, ret, test.ret)
158 }
159 }
160}
161
162// just to verify that the function is available, real tests are in TestExtractQueryParam
163func TestExtractQueryParamExpr(t *testing.T) {

Callers

nothing calls this directly

Calls 4

InitFunction · 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…