github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/oo_case_insensitive_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the Apache License Version 2.0. 3 // This product includes software developed at Datadog (https://www.datadoghq.com/). 4 // Copyright 2016-present Datadog, Inc. 5 6 // Package eval holds eval related files 7 package eval 8 9 import ( 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestLowerCaseEquals(t *testing.T) { 16 t.Run("no-match", func(t *testing.T) { 17 a := &StringEvaluator{ 18 Value: "BAR", 19 ValueType: ScalarValueType, 20 } 21 22 b := &StringEvaluator{ 23 Field: "field", 24 EvalFnc: func(ctx *Context) string { 25 return "foo" 26 }, 27 } 28 29 var ctx Context 30 state := NewState(&testModel{}, "", nil) 31 32 e, err := CaseInsensitiveCmp.StringEquals(a, b, state) 33 assert.Empty(t, err) 34 assert.False(t, e.Eval(&ctx).(bool)) 35 36 e, err = CaseInsensitiveCmp.StringEquals(b, a, state) 37 assert.Empty(t, err) 38 assert.False(t, e.Eval(&ctx).(bool)) 39 }) 40 41 t.Run("scalar", func(t *testing.T) { 42 a := &StringEvaluator{ 43 Value: "FOO", 44 ValueType: ScalarValueType, 45 } 46 47 b := &StringEvaluator{ 48 Field: "field", 49 EvalFnc: func(ctx *Context) string { 50 return "foo" 51 }, 52 } 53 54 var ctx Context 55 state := NewState(&testModel{}, "", nil) 56 57 e, err := CaseInsensitiveCmp.StringEquals(a, b, state) 58 assert.Empty(t, err) 59 assert.True(t, e.Eval(&ctx).(bool)) 60 61 e, err = CaseInsensitiveCmp.StringEquals(b, a, state) 62 assert.Empty(t, err) 63 assert.True(t, e.Eval(&ctx).(bool)) 64 }) 65 66 t.Run("glob", func(t *testing.T) { 67 a := &StringEvaluator{ 68 Value: "FO*", 69 ValueType: PatternValueType, 70 } 71 72 b := &StringEvaluator{ 73 Field: "field", 74 EvalFnc: func(ctx *Context) string { 75 return "foo" 76 }, 77 } 78 79 var ctx Context 80 state := NewState(&testModel{}, "", nil) 81 82 e, err := CaseInsensitiveCmp.StringEquals(a, b, state) 83 assert.Empty(t, err) 84 assert.True(t, e.Eval(&ctx).(bool)) 85 86 e, err = CaseInsensitiveCmp.StringEquals(b, a, state) 87 assert.Empty(t, err) 88 assert.True(t, e.Eval(&ctx).(bool)) 89 }) 90 91 t.Run("regex", func(t *testing.T) { 92 a := &StringEvaluator{ 93 Value: "FO.*", 94 ValueType: RegexpValueType, 95 } 96 97 b := &StringEvaluator{ 98 Field: "field", 99 EvalFnc: func(ctx *Context) string { 100 return "foo" 101 }, 102 } 103 104 var ctx Context 105 state := NewState(&testModel{}, "", nil) 106 107 e, err := CaseInsensitiveCmp.StringEquals(a, b, state) 108 assert.Empty(t, err) 109 assert.True(t, e.Eval(&ctx).(bool)) 110 111 e, err = CaseInsensitiveCmp.StringEquals(b, a, state) 112 assert.Empty(t, err) 113 assert.True(t, e.Eval(&ctx).(bool)) 114 }) 115 } 116 117 func TestLowerCaseContains(t *testing.T) { 118 t.Run("no-match", func(t *testing.T) { 119 a := &StringEvaluator{ 120 Field: "field", 121 EvalFnc: func(ctx *Context) string { 122 return "BAR" 123 }, 124 } 125 126 var values StringValues 127 values.AppendFieldValue(FieldValue{Value: "aaa", Type: ScalarValueType}) 128 values.AppendFieldValue(FieldValue{Value: "foo", Type: ScalarValueType}) 129 130 b := &StringValuesEvaluator{ 131 Values: values, 132 } 133 134 var ctx Context 135 state := NewState(&testModel{}, "", nil) 136 137 e, err := CaseInsensitiveCmp.StringValuesContains(a, b, state) 138 assert.Empty(t, err) 139 assert.False(t, e.Eval(&ctx).(bool)) 140 }) 141 142 t.Run("scalar", func(t *testing.T) { 143 a := &StringEvaluator{ 144 Field: "field", 145 EvalFnc: func(ctx *Context) string { 146 return "FOO" 147 }, 148 } 149 150 var values StringValues 151 values.AppendFieldValue(FieldValue{Value: "aaa", Type: ScalarValueType}) 152 values.AppendFieldValue(FieldValue{Value: "foo", Type: ScalarValueType}) 153 154 b := &StringValuesEvaluator{ 155 Values: values, 156 } 157 158 var ctx Context 159 state := NewState(&testModel{}, "", nil) 160 161 e, err := CaseInsensitiveCmp.StringValuesContains(a, b, state) 162 assert.Empty(t, err) 163 assert.True(t, e.Eval(&ctx).(bool)) 164 }) 165 166 t.Run("glob", func(t *testing.T) { 167 a := &StringEvaluator{ 168 Field: "field", 169 EvalFnc: func(ctx *Context) string { 170 return "foo" 171 }, 172 } 173 174 var values StringValues 175 values.AppendFieldValue(FieldValue{Value: "aaa", Type: ScalarValueType}) 176 values.AppendFieldValue(FieldValue{Value: "FOO*", Type: PatternValueType}) 177 178 b := &StringValuesEvaluator{ 179 Values: values, 180 } 181 182 var ctx Context 183 state := NewState(&testModel{}, "", nil) 184 185 e, err := CaseInsensitiveCmp.StringValuesContains(a, b, state) 186 assert.Empty(t, err) 187 assert.True(t, e.Eval(&ctx).(bool)) 188 }) 189 190 t.Run("regex", func(t *testing.T) { 191 a := &StringEvaluator{ 192 Field: "field", 193 EvalFnc: func(ctx *Context) string { 194 return "foo" 195 }, 196 } 197 198 var values StringValues 199 values.AppendFieldValue(FieldValue{Value: "aaa", Type: ScalarValueType}) 200 values.AppendFieldValue(FieldValue{Value: "FO.*", Type: RegexpValueType}) 201 202 b := &StringValuesEvaluator{ 203 Values: values, 204 } 205 206 var ctx Context 207 state := NewState(&testModel{}, "", nil) 208 209 e, err := CaseInsensitiveCmp.StringValuesContains(a, b, state) 210 assert.Empty(t, err) 211 assert.True(t, e.Eval(&ctx).(bool)) 212 213 values.AppendFieldValue(FieldValue{Value: "[Ff][Oo].*", Type: RegexpValueType}) 214 215 b = &StringValuesEvaluator{ 216 Values: values, 217 } 218 219 e, err = CaseInsensitiveCmp.StringValuesContains(a, b, state) 220 assert.Empty(t, err) 221 assert.True(t, e.Eval(&ctx).(bool)) 222 }) 223 224 t.Run("eval", func(t *testing.T) { 225 a := &StringEvaluator{ 226 Field: "field", 227 EvalFnc: func(ctx *Context) string { 228 return "FOO" 229 }, 230 } 231 232 var values StringValues 233 values.AppendFieldValue(FieldValue{Value: "aaa", Type: ScalarValueType}) 234 values.AppendFieldValue(FieldValue{Value: "fo*", Type: PatternValueType}) 235 236 opts := StringCmpOpts{ 237 CaseInsensitive: true, 238 } 239 240 if err := values.Compile(opts); err != nil { 241 t.Error(err) 242 } 243 244 b := &StringValuesEvaluator{ 245 EvalFnc: func(ctx *Context) *StringValues { 246 return &values 247 }, 248 } 249 250 var ctx Context 251 state := NewState(&testModel{}, "", nil) 252 253 e, err := CaseInsensitiveCmp.StringValuesContains(a, b, state) 254 assert.Empty(t, err) 255 assert.True(t, e.Eval(&ctx).(bool)) 256 }) 257 } 258 259 func TestLowerCaseArrayContains(t *testing.T) { 260 t.Run("no-match", func(t *testing.T) { 261 a := &StringEvaluator{ 262 Field: "field", 263 EvalFnc: func(ctx *Context) string { 264 return "BAR" 265 }, 266 } 267 268 b := &StringArrayEvaluator{ 269 Values: []string{"aaa", "bbb"}, 270 } 271 272 var ctx Context 273 state := NewState(&testModel{}, "", nil) 274 275 e, err := CaseInsensitiveCmp.StringArrayContains(a, b, state) 276 assert.Empty(t, err) 277 assert.False(t, e.Eval(&ctx).(bool)) 278 }) 279 280 t.Run("scalar", func(t *testing.T) { 281 a := &StringEvaluator{ 282 Field: "field", 283 EvalFnc: func(ctx *Context) string { 284 return "FOO" 285 }, 286 } 287 288 b := &StringArrayEvaluator{ 289 Values: []string{"aaa", "foo"}, 290 } 291 292 var ctx Context 293 state := NewState(&testModel{}, "", nil) 294 295 e, err := CaseInsensitiveCmp.StringArrayContains(a, b, state) 296 assert.Empty(t, err) 297 assert.True(t, e.Eval(&ctx).(bool)) 298 }) 299 300 t.Run("eval", func(t *testing.T) { 301 a := &StringEvaluator{ 302 Field: "field", 303 EvalFnc: func(ctx *Context) string { 304 return "foo" 305 }, 306 } 307 b := &StringArrayEvaluator{ 308 Field: "array", 309 EvalFnc: func(ctx *Context) []string { 310 return []string{"aaa", "foo"} 311 }, 312 } 313 314 var ctx Context 315 state := NewState(&testModel{}, "", nil) 316 317 e, err := CaseInsensitiveCmp.StringArrayContains(a, b, state) 318 assert.Empty(t, err) 319 assert.True(t, e.Eval(&ctx).(bool)) 320 }) 321 }