github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/binary/classifier_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/anchore/syft/syft/cpe"
     9  	"github.com/anchore/syft/syft/file"
    10  )
    11  
    12  func Test_ClassifierCPEs(t *testing.T) {
    13  	tests := []struct {
    14  		name       string
    15  		fixture    string
    16  		classifier classifier
    17  		cpes       []string
    18  	}{
    19  		{
    20  			name:    "no CPEs",
    21  			fixture: "test-fixtures/version.txt",
    22  			classifier: classifier{
    23  				Package:         "some-app",
    24  				FileGlob:        "**/version.txt",
    25  				EvidenceMatcher: fileContentsVersionMatcher(`(?m)my-verison:(?P<version>[0-9.]+)`),
    26  				CPEs:            []cpe.CPE{},
    27  			},
    28  			cpes: nil,
    29  		},
    30  		{
    31  			name:    "one CPE",
    32  			fixture: "test-fixtures/version.txt",
    33  			classifier: classifier{
    34  				Package:         "some-app",
    35  				FileGlob:        "**/version.txt",
    36  				EvidenceMatcher: fileContentsVersionMatcher(`(?m)my-verison:(?P<version>[0-9.]+)`),
    37  				CPEs: []cpe.CPE{
    38  					cpe.Must("cpe:2.3:a:some:app:*:*:*:*:*:*:*:*"),
    39  				},
    40  			},
    41  			cpes: []string{
    42  				"cpe:2.3:a:some:app:1.8:*:*:*:*:*:*:*",
    43  			},
    44  		},
    45  		{
    46  			name:    "multiple CPEs",
    47  			fixture: "test-fixtures/version.txt",
    48  			classifier: classifier{
    49  				Package:         "some-app",
    50  				FileGlob:        "**/version.txt",
    51  				EvidenceMatcher: fileContentsVersionMatcher(`(?m)my-verison:(?P<version>[0-9.]+)`),
    52  				CPEs: []cpe.CPE{
    53  					cpe.Must("cpe:2.3:a:some:app:*:*:*:*:*:*:*:*"),
    54  					cpe.Must("cpe:2.3:a:some:apps:*:*:*:*:*:*:*:*"),
    55  				},
    56  			},
    57  			cpes: []string{
    58  				"cpe:2.3:a:some:app:1.8:*:*:*:*:*:*:*",
    59  				"cpe:2.3:a:some:apps:1.8:*:*:*:*:*:*:*",
    60  			},
    61  		},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		t.Run(test.name, func(t *testing.T) {
    66  			resolver := file.NewMockResolverForPaths(test.fixture)
    67  			ls, err := resolver.FilesByPath(test.fixture)
    68  			require.NoError(t, err)
    69  			require.Len(t, ls, 1)
    70  
    71  			pkgs, err := test.classifier.EvidenceMatcher(resolver, test.classifier, ls[0])
    72  			require.NoError(t, err)
    73  
    74  			require.Len(t, pkgs, 1)
    75  
    76  			p := pkgs[0]
    77  
    78  			var cpes []string
    79  			for _, c := range p.CPEs {
    80  				cpes = append(cpes, cpe.String(c))
    81  			}
    82  			require.Equal(t, test.cpes, cpes)
    83  		})
    84  	}
    85  }