github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/glob_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  
    13  func TestGlobPattern(t *testing.T) {
    14  	if _, err := NewGlob("**/conf.d", false, false); err == nil {
    15  		t.Error("should return an error")
    16  	}
    17  
    18  	if _, err := NewGlob("/etc/conf.d/**", false, false); err != nil {
    19  		t.Error("shouldn't return an error")
    20  	}
    21  
    22  	if _, err := NewGlob("/etc/**/*.conf", false, false); err == nil {
    23  		t.Error("should return an error")
    24  	}
    25  
    26  	if _, err := NewGlob("/etc/**.conf", false, false); err == nil {
    27  		t.Error("should return an error")
    28  	}
    29  }
    30  
    31  func TestGlobContains(t *testing.T) {
    32  	if glob, _ := NewGlob("/", false, false); !glob.Contains("/var/log") {
    33  		t.Error("should contain the filename")
    34  	}
    35  
    36  	if glob, _ := NewGlob("/var/log/*", false, false); !glob.Contains("/var/log") {
    37  		t.Error("should contain the filename")
    38  	}
    39  
    40  	if glob, _ := NewGlob("/var/log/*", false, false); !glob.Contains("/var/log/httpd") {
    41  		t.Error("should contain the filename")
    42  	}
    43  
    44  	if glob, _ := NewGlob("*/log/httpd", false, false); !glob.Contains("/var/log/httpd") {
    45  		t.Error("should contain the filename")
    46  	}
    47  
    48  	if glob, _ := NewGlob("*/log", false, false); !glob.Contains("/var/log/httpd") {
    49  		t.Error("should contain the filename")
    50  	}
    51  
    52  	if glob, _ := NewGlob("*/http*", false, false); glob.Contains("/var/log/httpd") {
    53  		t.Error("shouldn't contain the filename")
    54  	}
    55  
    56  	if glob, _ := NewGlob("*/*/http*", false, false); !glob.Contains("/var/log/httpd") {
    57  		t.Error("should contain the filename")
    58  	}
    59  
    60  	if glob, _ := NewGlob("/var/*/httpd", false, false); !glob.Contains("/var/log/httpd") {
    61  		t.Error("should contain the filename")
    62  	}
    63  
    64  	if glob, _ := NewGlob("/var/*/httpd", false, false); glob.Contains("/var/log/nginx") {
    65  		t.Error("shouldn't contain the filename")
    66  	}
    67  
    68  	if glob, _ := NewGlob("/var/**", false, false); !glob.Contains("/var/log/nginx") {
    69  		t.Error("should contain the filename")
    70  	}
    71  
    72  	if glob, _ := NewGlob("/var/*", false, false); !glob.Contains("/var/log/nginx") {
    73  		t.Error("should contain the filename")
    74  	}
    75  
    76  	if glob, _ := NewGlob("/var/log/ng*", false, false); !glob.Contains("/var/log/nginx") {
    77  		t.Error("should contain the filename")
    78  	}
    79  
    80  	if glob, _ := NewGlob("/var/*o*/nginx", false, false); !glob.Contains("/var/log/nginx") {
    81  		t.Error("should contain the filename")
    82  	}
    83  
    84  	if glob, _ := NewGlob("/var/**", false, false); !glob.Contains("/var/log/nginx") {
    85  		t.Error("should contain the filename")
    86  	}
    87  
    88  	if glob, _ := NewGlob("/var/log/**", false, false); !glob.Contains("/var/log/nginx") {
    89  		t.Error("should contain the filename")
    90  	}
    91  
    92  	if glob, _ := NewGlob("/etc/conf.d/ab*", false, false); !glob.Contains("/etc/conf.d/") {
    93  		t.Error("should contain the filename")
    94  	}
    95  
    96  	if glob, _ := NewGlob("/var/*", false, false); !glob.Contains("/var/log/nginx") {
    97  		t.Error("should contain the filename")
    98  	}
    99  
   100  	if glob, _ := NewGlob("/var/log", false, false); !glob.Contains("/var/log") {
   101  		t.Error("should contain the filename")
   102  	}
   103  
   104  	if glob, _ := NewGlob("/etc/conf.d/*", false, false); glob.Contains("/etc/sys.d/nginx.conf") {
   105  		t.Error("shouldn't contain the filename")
   106  	}
   107  
   108  	if glob, _ := NewGlob("/var/log", false, false); !glob.Contains("/var/log/httpd") {
   109  		t.Error("should contain the filename")
   110  	}
   111  }
   112  
   113  func TestGlobMatches(t *testing.T) {
   114  	if glob, _ := NewGlob("/tmp/test/test789", false, false); !glob.Matches("/tmp/test/test789") {
   115  		t.Error("should contain the filename")
   116  	}
   117  
   118  	if glob, _ := NewGlob("*/abc/*", false, false); !glob.Matches("/1/abc/2") {
   119  		t.Error("should contain the filename")
   120  	}
   121  
   122  	if glob, _ := NewGlob("/tmp/test/test789*", false, false); !glob.Matches("/tmp/test/test7890") {
   123  		t.Error("should contain the filename")
   124  	}
   125  
   126  	if glob, _ := NewGlob("/tmp/test/test789*", false, false); glob.Matches("/tmp/test") {
   127  		t.Error("shouldn't contain the filename")
   128  	}
   129  
   130  	if glob, _ := NewGlob("/tmp/test/*st*", false, false); glob.Matches("/tmp/test") {
   131  		t.Error("shouldn't contain the filename")
   132  	}
   133  
   134  	if glob, _ := NewGlob("/tmp/test/*st*", false, false); !glob.Matches("/tmp/test/ast") {
   135  		t.Error("should contain the filename")
   136  	}
   137  
   138  	if glob, _ := NewGlob("/tmp/*/test789", false, false); !glob.Matches("/tmp/test/test789") {
   139  		t.Error("should contain the filename")
   140  	}
   141  
   142  	if glob, _ := NewGlob("/tmp/*/test789", false, false); glob.Matches("/tmp/test/test") {
   143  		t.Error("shouldn't contain the filename")
   144  	}
   145  
   146  	if glob, _ := NewGlob("/tmp/**", false, false); !glob.Matches("/tmp/test/ast") {
   147  		t.Error("should the filename")
   148  	}
   149  
   150  	if glob, _ := NewGlob("/tmp/*", false, false); glob.Matches("/tmp/test/ast") {
   151  		t.Error("shouldn't the filename")
   152  	}
   153  
   154  	if glob, _ := NewGlob("*", false, false); glob.Matches("/tmp/test/ast") {
   155  		t.Error("shouldn't the filename")
   156  	}
   157  
   158  	if glob, _ := NewGlob("**", false, false); !glob.Matches("/tmp/test/ast") {
   159  		t.Error("should the filename")
   160  	}
   161  
   162  	if glob, _ := NewGlob("/var/log/*", false, false); !glob.Matches("/var/log/httpd") {
   163  		t.Error("should match the filename")
   164  	}
   165  
   166  	if glob, _ := NewGlob("/var/**", false, false); !glob.Matches("/var/log/nginx") {
   167  		t.Error("should match the filename")
   168  	}
   169  
   170  	if glob, _ := NewGlob("/var/*", false, false); glob.Matches("/var/log/nginx") {
   171  		t.Error("shouldn't match the filename")
   172  	}
   173  
   174  	if glob, _ := NewGlob("/var/log", false, false); !glob.Matches("/var/log") {
   175  		t.Error("should match the filename")
   176  	}
   177  
   178  	if glob, _ := NewGlob("/var/run", false, false); glob.Matches("/var/log") {
   179  		t.Error("shouldn't match the filename")
   180  	}
   181  
   182  	if glob, _ := NewGlob("/var/run", false, false); glob.Matches("/var/run/httpd") {
   183  		t.Error("shouldn't match the filename")
   184  	}
   185  
   186  	if glob, _ := NewGlob("/var/run/*", false, false); glob.Matches("abc") {
   187  		t.Error("shouldn't match the filename")
   188  	}
   189  
   190  	if glob, _ := NewGlob("ab*", false, false); !glob.Matches("abc") {
   191  		t.Error("should match the filename")
   192  	}
   193  
   194  	if glob, _ := NewGlob("*b*", false, false); !glob.Matches("abc") {
   195  		t.Error("should match the filename")
   196  	}
   197  
   198  	if glob, _ := NewGlob("*d*", false, false); glob.Matches("abc") {
   199  		t.Error("shouldn't match the filename")
   200  	}
   201  
   202  	if glob, _ := NewGlob("*/*/httpd", false, false); !glob.Matches("/var/log/httpd") {
   203  		t.Error("should contain the filename")
   204  	}
   205  
   206  	if glob, _ := NewGlob("*/*/http*", false, false); !glob.Matches("/var/log/httpd") {
   207  		t.Error("should contain the filename")
   208  	}
   209  
   210  	if glob, _ := NewGlob("httpd", false, false); !glob.Matches("httpd") {
   211  		t.Error("should contain the filename")
   212  	}
   213  
   214  	if glob, _ := NewGlob("mysqld", false, false); glob.Matches("httpd") {
   215  		t.Error("shouldn't contain the filename")
   216  	}
   217  
   218  	if glob, _ := NewGlob("/", false, false); glob.Matches("/httpd") {
   219  		t.Error("shouldn't contain the filename")
   220  	}
   221  
   222  	if glob, _ := NewGlob("/*", false, false); !glob.Matches("/httpd") {
   223  		t.Error("should contain the filename")
   224  	}
   225  
   226  	if glob, _ := NewGlob("/sys/fs/cgroup/*", false, false); !glob.Matches("/sys/fs/cgroup/") {
   227  		t.Error("should contain the filename")
   228  	}
   229  }
   230  
   231  func BenchmarkGlob(b *testing.B) {
   232  	glob, err := NewGlob("*/*/http*/*b*/test/**", false, false)
   233  	if err != nil {
   234  		b.Fatal(err)
   235  	}
   236  	b.ResetTimer()
   237  
   238  	for i := 0; i < b.N; i++ {
   239  		if matches := glob.Matches("/var/log/httpd/abc/test/123"); !matches {
   240  			b.Fatalf("glob should match")
   241  		}
   242  	}
   243  }