github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/common/cyclonedxhelpers/cpe_test.go (about)

     1  package cyclonedxhelpers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/cpe"
     9  	"github.com/anchore/syft/syft/pkg"
    10  )
    11  
    12  func Test_encodeCPE(t *testing.T) {
    13  	testCPE := cpe.Must("cpe:2.3:a:name:name:3.2:*:*:*:*:*:*:*")
    14  	testCPE2 := cpe.Must("cpe:2.3:a:name:name2:3.2:*:*:*:*:*:*:*")
    15  	tests := []struct {
    16  		name     string
    17  		input    pkg.Package
    18  		expected string
    19  	}{
    20  		{
    21  			// note: since this is an optional field, no value is preferred over NONE or NOASSERTION
    22  			name: "no metadata",
    23  			input: pkg.Package{
    24  				CPEs: []cpe.CPE{},
    25  			},
    26  			expected: "",
    27  		},
    28  		{
    29  			name: "single CPE",
    30  			input: pkg.Package{
    31  				CPEs: []cpe.CPE{
    32  					testCPE,
    33  				},
    34  			},
    35  			expected: "cpe:2.3:a:name:name:3.2:*:*:*:*:*:*:*",
    36  		},
    37  		{
    38  			name: "multiple CPEs",
    39  			input: pkg.Package{
    40  				CPEs: []cpe.CPE{
    41  					testCPE2,
    42  					testCPE,
    43  				},
    44  			},
    45  			expected: "cpe:2.3:a:name:name2:3.2:*:*:*:*:*:*:*",
    46  		},
    47  		{
    48  			// note: since this is an optional field, no value is preferred over NONE or NOASSERTION
    49  			name:     "empty",
    50  			input:    pkg.Package{},
    51  			expected: "",
    52  		},
    53  	}
    54  	for _, test := range tests {
    55  		t.Run(test.name, func(t *testing.T) {
    56  			assert.Equal(t, test.expected, encodeSingleCPE(test.input))
    57  		})
    58  	}
    59  }