github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/pattern_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 TestPatternNextSegment(t *testing.T) {
    14  	star, segment, _ := nextSegment("*test123")
    15  	if !star || segment != "test123" {
    16  		t.Errorf("expected segment not found: %v, %v", star, segment)
    17  	}
    18  
    19  	star, segment, _ = nextSegment("test123*")
    20  	if star || segment != "test123" {
    21  		t.Errorf("expected segment not found: %v, %v", star, segment)
    22  	}
    23  
    24  	star, segment, _ = nextSegment("*test123*")
    25  	if !star || segment != "test123" {
    26  		t.Errorf("expected segment not found: %v, %v", star, segment)
    27  	}
    28  
    29  	star, segment, _ = nextSegment("*test*123*")
    30  	if !star || segment != "test" {
    31  		t.Errorf("expected segment not found: %v, %v", star, segment)
    32  	}
    33  
    34  	star, segment, _ = nextSegment("test")
    35  	if star || segment != "test" {
    36  		t.Errorf("expected segment not found: %v, %v", star, segment)
    37  	}
    38  
    39  	star, segment, _ = nextSegment("**")
    40  	if !star || segment != "" {
    41  		t.Errorf("expected segment not found: %v, %v", star, segment)
    42  	}
    43  }
    44  
    45  func TestPatternMatches(t *testing.T) {
    46  	t.Run("sensitive-case", func(t *testing.T) {
    47  		if !PatternMatches("*abc*", "/abc/", false) {
    48  			t.Error("should match")
    49  		}
    50  
    51  		if !PatternMatches("*test123", "aaatest123", false) {
    52  			t.Error("should match")
    53  		}
    54  
    55  		if PatternMatches("*test456", "aaatest123", false) {
    56  			t.Error("shouldn't match")
    57  		}
    58  
    59  		if !PatternMatches("*", "test123", false) {
    60  			t.Error("should match")
    61  		}
    62  
    63  		if !PatternMatches("test*", "test123", false) {
    64  			t.Error("should match")
    65  		}
    66  
    67  		if !PatternMatches("t*123", "test123", false) {
    68  			t.Error("should match")
    69  		}
    70  
    71  		if !PatternMatches("t*1*3", "test123", false) {
    72  			t.Error("should match")
    73  		}
    74  
    75  		if !PatternMatches("*t*1*3", "atest123", false) {
    76  			t.Error("should match")
    77  		}
    78  
    79  		if PatternMatches("*t*9*3", "atest123", false) {
    80  			t.Error("shouldn't match")
    81  		}
    82  
    83  		if PatternMatches("*.c", "test.ct", false) {
    84  			t.Error("shouldn't match")
    85  		}
    86  	})
    87  
    88  	t.Run("insensitive-case", func(t *testing.T) {
    89  		if !PatternMatches("*TEST123", "aaatest123", true) {
    90  			t.Error("should match")
    91  		}
    92  
    93  		if PatternMatches("*TEST456", "aaatest123", true) {
    94  			t.Error("shouldn't match")
    95  		}
    96  
    97  		if !PatternMatches("test*", "TEST123", true) {
    98  			t.Error("should match")
    99  		}
   100  
   101  		if !PatternMatches("T*123", "test123", true) {
   102  			t.Error("should match")
   103  		}
   104  
   105  		if !PatternMatches("T*t123", "tEsT123", true) {
   106  			t.Error("should match")
   107  		}
   108  	})
   109  }
   110  
   111  func BenchmarkNextSegment(b *testing.B) {
   112  	for i := 0; i < b.N; i++ {
   113  		star, segment, _ := nextSegment("*test*123*")
   114  		if !star || segment != "test" {
   115  			b.Fatalf("expected segment not found: %v, %v", star, segment)
   116  		}
   117  	}
   118  }