github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/file/cataloger/secrets/generate_search_patterns_test.go (about)

     1  package secrets
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGenerateSearchPatterns(t *testing.T) {
    10  	tests := []struct {
    11  		name       string
    12  		base       map[string]string
    13  		additional map[string]string
    14  		exclude    []string
    15  		expected   map[string]string
    16  	}{
    17  		{
    18  			name: "use-base-set",
    19  			base: map[string]string{
    20  				"in-default": `^secret_key=.*`,
    21  			},
    22  			expected: map[string]string{
    23  				"in-default": `(?m)^secret_key=.*`,
    24  			},
    25  		},
    26  		{
    27  			name: "exclude-from-base-set",
    28  			base: map[string]string{
    29  				"in-default":      `^secret_key=.*`,
    30  				"also-in-default": `^also-in-default=.*`,
    31  			},
    32  			exclude: []string{"also-in-default"},
    33  			expected: map[string]string{
    34  				"in-default": `(?m)^secret_key=.*`,
    35  			},
    36  		},
    37  		{
    38  			name: "exclude-multiple-from-base-set",
    39  			base: map[string]string{
    40  				"in-default":             `^secret_key=.*`,
    41  				"also-in-default":        `^also-in-default=.*`,
    42  				"furthermore-in-default": `^furthermore-in-default=.*`,
    43  			},
    44  			exclude: []string{"also-in-default", "furthermore-in-default"},
    45  			expected: map[string]string{
    46  				"in-default": `(?m)^secret_key=.*`,
    47  			},
    48  		},
    49  		{
    50  			name: "exclude-all",
    51  			base: map[string]string{
    52  				"in-default":      `^secret_key=.*`,
    53  				"also-in-default": `^also-in-default=.*`,
    54  			},
    55  			exclude:  []string{"*"},
    56  			expected: map[string]string{},
    57  		},
    58  		{
    59  			name: "exclude-some",
    60  			base: map[string]string{
    61  				"real":            `^real=.*`,
    62  				"in-default":      `^secret_key=.*`,
    63  				"also-in-default": `^also-in-default=.*`,
    64  			},
    65  			exclude: []string{"*-default"},
    66  			expected: map[string]string{
    67  				"real": `(?m)^real=.*`,
    68  			},
    69  		},
    70  		{
    71  			name: "additional-pattern-unison",
    72  			base: map[string]string{
    73  				"in-default": `^secret_key=.*`,
    74  			},
    75  			additional: map[string]string{
    76  				"additional": `^additional=.*`,
    77  			},
    78  			expected: map[string]string{
    79  				"in-default": `(?m)^secret_key=.*`,
    80  				"additional": `(?m)^additional=.*`,
    81  			},
    82  		},
    83  		{
    84  			name: "override",
    85  			base: map[string]string{
    86  				"in-default": `^secret_key=.*`,
    87  			},
    88  			additional: map[string]string{
    89  				"in-default": `^additional=.*`,
    90  			},
    91  			expected: map[string]string{
    92  				"in-default": `(?m)^additional=.*`,
    93  			},
    94  		},
    95  		{
    96  			name: "exclude-and-override",
    97  			base: map[string]string{
    98  				"in-default": `^secret_key=.*`,
    99  			},
   100  			exclude: []string{"in-default"},
   101  			additional: map[string]string{
   102  				"in-default": `^additional=.*`,
   103  			},
   104  			expected: map[string]string{
   105  				"in-default": `(?m)^additional=.*`,
   106  			},
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			actualObj, err := GenerateSearchPatterns(test.base, test.additional, test.exclude)
   113  			if err != nil {
   114  				t.Fatalf("unable to combine: %+v", err)
   115  			}
   116  
   117  			actual := make(map[string]string)
   118  			for n, v := range actualObj {
   119  				actual[n] = v.String()
   120  			}
   121  
   122  			assert.Equal(t, test.expected, actual, "mismatched combination")
   123  		})
   124  	}
   125  }