github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/binary/classifier_test.go (about)

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