github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/common/cyclonedxhelpers/cpe_test.go (about)

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