Skip to content

Commit

Permalink
feat(1.6): extend EvidenceOccurrence
Browse files Browse the repository at this point in the history
Closes CycloneDX#156.

Signed-off-by: Maximilian Combüchen <max.combuchen@snyk.io>
  • Loading branch information
mcombuechen committed May 12, 2024
1 parent 3a84845 commit 501c5ef
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
39 changes: 32 additions & 7 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func componentConverter(specVersion SpecVersion) func(*Component) {
}

if specVersion < SpecVersion1_3 {
c.Evidence = nil
c.Properties = nil
}

Expand All @@ -137,26 +136,52 @@ func componentConverter(specVersion SpecVersion) func(*Component) {
if specVersion < SpecVersion1_5 {
c.ModelCard = nil
c.Data = nil

if c.Evidence != nil {
c.Evidence.Identity = nil
c.Evidence.Occurrences = nil
c.Evidence.Callstack = nil
}
}

if !specVersion.supportsComponentType(c.Type) {
c.Type = ComponentTypeApplication
}

convertExternalReferences(c.ExternalReferences, specVersion)
convertHashes(c.Hashes, specVersion)
convertLicenses(c.Licenses, specVersion)
convertEvidence(c, specVersion)

if !specVersion.supportsScope(c.Scope) {
c.Scope = ""
}
}
}

func convertEvidence(c *Component, specVersion SpecVersion) {
if c.Evidence == nil {
return
}

if specVersion < SpecVersion1_3 {
c.Evidence = nil
return
}

if specVersion < SpecVersion1_5 {
c.Evidence.Identity = nil
c.Evidence.Occurrences = nil
c.Evidence.Callstack = nil
return
}

for i := range *c.Evidence.Occurrences {
occ := &(*c.Evidence.Occurrences)[i]

if specVersion < SpecVersion1_6 {
occ.LineNumber = 0
occ.Offset = 0
occ.Symbol = ""
occ.AdditionalContext = ""
}
}
}

func convertCompositions(comps *[]Composition, specVersion SpecVersion) {
if comps == nil {
return
Expand Down
70 changes: 70 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cyclonedx

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_componentConverter_convertEvidence(t *testing.T) {
t.Run("spec 1.2 and lower", func(t *testing.T) {
convert := componentConverter(SpecVersion1_2)

comp := Component{
Evidence: &Evidence{},
}

convert(&comp)

assert.Nil(t, comp.Evidence)
})

t.Run("spec 1.4 and lower", func(t *testing.T) {
convert := componentConverter(SpecVersion1_4)

comp := Component{
Evidence: &Evidence{
Identity: &EvidenceIdentity{},
Occurrences: &[]EvidenceOccurrence{},
Callstack: &Callstack{},
Copyright: &[]Copyright{{Text: "foo"}},
},
}

convert(&comp)

assert.Nil(t, comp.Evidence.Identity)
assert.Nil(t, comp.Evidence.Occurrences)
assert.Nil(t, comp.Evidence.Callstack)
assert.NotNil(t, comp.Evidence.Copyright)
})

t.Run("spec 1.5 and lower", func(t *testing.T) {
convert := componentConverter(SpecVersion1_5)

comp := Component{
Evidence: &Evidence{
Occurrences: &[]EvidenceOccurrence{
{
BOMRef: "foo",
Location: "bar",
LineNumber: 42,
Offset: 42,
Symbol: "asdf",
AdditionalContext: "quux",
},
},
},
}

convert(&comp)

require.Len(t, *comp.Evidence.Occurrences, 1)
occ := (*comp.Evidence.Occurrences)[0]
assert.Zero(t, occ.LineNumber)
assert.Zero(t, occ.Offset)
assert.Zero(t, occ.Symbol)
assert.Zero(t, occ.AdditionalContext)
})
}
8 changes: 6 additions & 2 deletions cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,12 @@ const (
)

type EvidenceOccurrence struct {
BOMRef string `json:"bom-ref,omitempty" xml:"bom-ref,attr,omitempty"`
Location string `json:"location,omitempty" xml:"location,omitempty"`
BOMRef string `json:"bom-ref,omitempty" xml:"bom-ref,attr,omitempty"`
Location string `json:"location,omitempty" xml:"location,omitempty"`
LineNumber int `json:"lineNumber,omitempty" xml:"line,attr,omitempty"`
Offset int `json:"offset,omitempty" xml:"offset,attr,omitempty"`
Symbol string `json:"symbol,omitempty" xml:"symbol,attr,omitempty"`
AdditionalContext string `json:"additionalContext,omitempty" xml:"additionalContext,attr,omitempty"`
}

type ExternalReference struct {
Expand Down

0 comments on commit 501c5ef

Please sign in to comment.