github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/oo_glob_cmp_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 TestOPOverrideGlobEquals(t *testing.T) {
    16  	t.Run("no-match", func(t *testing.T) {
    17  		a := &StringEvaluator{
    18  			Value:     "/abc/",
    19  			ValueType: PatternValueType,
    20  		}
    21  
    22  		b := &StringEvaluator{
    23  			Field: "field",
    24  			EvalFnc: func(ctx *Context) string {
    25  				return "/2/abc/3"
    26  			},
    27  		}
    28  
    29  		var ctx Context
    30  		state := NewState(&testModel{}, "", nil)
    31  
    32  		e, err := GlobCmp.StringEquals(a, b, state)
    33  		assert.Empty(t, err)
    34  		assert.False(t, e.Eval(&ctx).(bool))
    35  
    36  		e, err = GlobCmp.StringEquals(b, a, state)
    37  		assert.Empty(t, err)
    38  		assert.False(t, e.Eval(&ctx).(bool))
    39  	})
    40  
    41  	t.Run("match", func(t *testing.T) {
    42  		a := &StringEvaluator{
    43  			Value:     "*/abc/*",
    44  			ValueType: PatternValueType,
    45  		}
    46  
    47  		b := &StringEvaluator{
    48  			Field: "field",
    49  			EvalFnc: func(ctx *Context) string {
    50  				return "/2/abc/3"
    51  			},
    52  		}
    53  
    54  		var ctx Context
    55  		state := NewState(&testModel{}, "", nil)
    56  
    57  		e, err := GlobCmp.StringEquals(a, b, state)
    58  		assert.Empty(t, err)
    59  		assert.True(t, e.Eval(&ctx).(bool))
    60  
    61  		e, err = GlobCmp.StringEquals(b, a, state)
    62  		assert.Empty(t, err)
    63  		assert.True(t, e.Eval(&ctx).(bool))
    64  	})
    65  }
    66  
    67  func TestOPOverrideGlobContains(t *testing.T) {
    68  	t.Run("no-match", func(t *testing.T) {
    69  		a := &StringEvaluator{
    70  			Field: "field",
    71  			EvalFnc: func(ctx *Context) string {
    72  				return "/2/abc/3"
    73  			},
    74  		}
    75  
    76  		var values StringValues
    77  		values.AppendFieldValue(FieldValue{Value: "/abc/", Type: PatternValueType})
    78  		values.AppendFieldValue(FieldValue{Value: "abc/*", Type: PatternValueType})
    79  		values.AppendFieldValue(FieldValue{Value: "*/abc", Type: PatternValueType})
    80  
    81  		b := &StringValuesEvaluator{
    82  			Values: values,
    83  		}
    84  
    85  		var ctx Context
    86  		state := NewState(&testModel{}, "", nil)
    87  
    88  		e, err := GlobCmp.StringValuesContains(a, b, state)
    89  		assert.Empty(t, err)
    90  		assert.False(t, e.Eval(&ctx).(bool))
    91  	})
    92  
    93  	t.Run("match", func(t *testing.T) {
    94  		a := &StringEvaluator{
    95  			Field: "field",
    96  			EvalFnc: func(ctx *Context) string {
    97  				return "/2/abc/3"
    98  			},
    99  		}
   100  
   101  		var values StringValues
   102  		values.AppendFieldValue(FieldValue{Value: "*/abc/*", Type: PatternValueType})
   103  		values.AppendFieldValue(FieldValue{Value: "abc", Type: PatternValueType})
   104  
   105  		b := &StringValuesEvaluator{
   106  			Values: values,
   107  		}
   108  
   109  		var ctx Context
   110  		state := NewState(&testModel{}, "", nil)
   111  
   112  		e, err := GlobCmp.StringValuesContains(a, b, state)
   113  		assert.Empty(t, err)
   114  		assert.True(t, e.Eval(&ctx).(bool))
   115  	})
   116  }
   117  
   118  func TestOPOverrideGlobArrayMatches(t *testing.T) {
   119  	t.Run("no-match", func(t *testing.T) {
   120  		a := &StringArrayEvaluator{
   121  			Values: []string{"/2/abc/3"},
   122  		}
   123  
   124  		var values StringValues
   125  		values.AppendFieldValue(FieldValue{Value: "abc", Type: PatternValueType})
   126  		values.AppendFieldValue(FieldValue{Value: "abc/*", Type: PatternValueType})
   127  		values.AppendFieldValue(FieldValue{Value: "*/abc", Type: PatternValueType})
   128  
   129  		b := &StringValuesEvaluator{
   130  			Values: values,
   131  		}
   132  		state := NewState(&testModel{}, "", nil)
   133  
   134  		e, err := GlobCmp.StringArrayMatches(a, b, state)
   135  		assert.Empty(t, err)
   136  		assert.False(t, e.Value)
   137  	})
   138  
   139  	t.Run("match", func(t *testing.T) {
   140  		a := &StringArrayEvaluator{
   141  			Values: []string{"/2/abc/3"},
   142  		}
   143  
   144  		var values StringValues
   145  		values.AppendFieldValue(FieldValue{Value: "*/abc/*", Type: PatternValueType})
   146  		values.AppendFieldValue(FieldValue{Value: "abc", Type: PatternValueType})
   147  
   148  		b := &StringValuesEvaluator{
   149  			Values: values,
   150  		}
   151  		state := NewState(&testModel{}, "", nil)
   152  
   153  		e, err := GlobCmp.StringArrayMatches(a, b, state)
   154  		assert.Empty(t, err)
   155  		assert.True(t, e.Value)
   156  	})
   157  }
   158  
   159  func TestOPOverrideGlobArrayContains(t *testing.T) {
   160  	t.Run("no-match", func(t *testing.T) {
   161  		a := &StringEvaluator{
   162  			Value:     "/abc/",
   163  			ValueType: PatternValueType,
   164  		}
   165  
   166  		b := &StringArrayEvaluator{
   167  			Values: []string{"/2/abc/3"},
   168  		}
   169  		state := NewState(&testModel{}, "", nil)
   170  
   171  		e, err := GlobCmp.StringArrayContains(a, b, state)
   172  		assert.Empty(t, err)
   173  		assert.False(t, e.Value)
   174  	})
   175  
   176  	t.Run("match", func(t *testing.T) {
   177  		a := &StringEvaluator{
   178  			Value:     "*/abc/*",
   179  			ValueType: PatternValueType,
   180  		}
   181  
   182  		b := &StringArrayEvaluator{
   183  			Field:  "dont_forget_me_or_it_wont_compile_a",
   184  			Values: []string{"/2/abc/3"},
   185  		}
   186  		state := NewState(&testModel{}, "", nil)
   187  
   188  		e, err := GlobCmp.StringArrayContains(a, b, state)
   189  		assert.Empty(t, err)
   190  		assert.True(t, e.Value)
   191  	})
   192  }