github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/secrets/matchers_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package secrets_test
     5  
     6  import (
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/instana/go-sensor/secrets"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestEqualsMatcher(t *testing.T) {
    16  	m := secrets.NewEqualsMatcher("one", "two")
    17  	examples := map[string]bool{
    18  		"":          false,
    19  		"one":       true,
    20  		"two":       true,
    21  		"ONE":       false,
    22  		"tWo":       false,
    23  		"onetwo":    false,
    24  		"two ":      false,
    25  		" one":      false,
    26  		"forty-two": false,
    27  		"hello!":    false,
    28  	}
    29  
    30  	for s, expected := range examples {
    31  		t.Run(s, func(t *testing.T) {
    32  			assert.Equal(t, expected, m.Match(s))
    33  		})
    34  	}
    35  }
    36  
    37  func TestEqualsIgnoreCaseMatcher(t *testing.T) {
    38  	m := secrets.NewEqualsIgnoreCaseMatcher("One", "TWO")
    39  	examples := map[string]bool{
    40  		"":          false,
    41  		"one":       true,
    42  		"two":       true,
    43  		"ONE":       true,
    44  		"tWo":       true,
    45  		"onetwo":    false,
    46  		"two ":      false,
    47  		" one":      false,
    48  		"forty-two": false,
    49  		"hello!":    false,
    50  	}
    51  
    52  	for s, expected := range examples {
    53  		t.Run(s, func(t *testing.T) {
    54  			assert.Equal(t, expected, m.Match(s))
    55  		})
    56  	}
    57  }
    58  
    59  func TestContainsMatcher(t *testing.T) {
    60  	m := secrets.NewContainsMatcher("one", "two")
    61  	examples := map[string]bool{
    62  		"":          false,
    63  		"one":       true,
    64  		"two":       true,
    65  		"ONE":       false,
    66  		"tWo":       false,
    67  		"onetwo":    true,
    68  		"two ":      true,
    69  		" one":      true,
    70  		"forty-two": true,
    71  		"hello!":    false,
    72  	}
    73  
    74  	for s, expected := range examples {
    75  		t.Run(s, func(t *testing.T) {
    76  			assert.Equal(t, expected, m.Match(s))
    77  		})
    78  	}
    79  }
    80  
    81  func TestContainsIgnoreCaseMatcher(t *testing.T) {
    82  	m := secrets.NewContainsIgnoreCaseMatcher("one", "two")
    83  	examples := map[string]bool{
    84  		"":          false,
    85  		"one":       true,
    86  		"two":       true,
    87  		"ONE":       true,
    88  		"tWo":       true,
    89  		"onetwo":    true,
    90  		"two ":      true,
    91  		" one":      true,
    92  		"forty-TWO": true,
    93  		"hello!":    false,
    94  	}
    95  
    96  	for s, expected := range examples {
    97  		t.Run(s, func(t *testing.T) {
    98  			assert.Equal(t, expected, m.Match(s))
    99  		})
   100  	}
   101  }
   102  
   103  func TestRegexpMatcher(t *testing.T) {
   104  	m, err := secrets.NewRegexpMatcher(regexp.MustCompile(`(?i)\Aone\z`), regexp.MustCompile(`^two$`))
   105  	require.NoError(t, err)
   106  
   107  	examples := map[string]bool{
   108  		"":          false,
   109  		"one":       true,
   110  		"two":       true,
   111  		"ONE":       true,
   112  		"tWo":       false,
   113  		"onetwo":    false,
   114  		"two ":      false,
   115  		" one":      false,
   116  		"forty-TWO": false,
   117  		"hello!":    false,
   118  	}
   119  
   120  	for s, expected := range examples {
   121  		t.Run(s, func(t *testing.T) {
   122  			assert.Equal(t, expected, m.Match(s))
   123  		})
   124  	}
   125  }