Skip to content

Commit

Permalink
Add unit tests for individual addEntry funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Luhring <[email protected]>
  • Loading branch information
luhring committed Jul 18, 2023
1 parent d7ab39d commit 1a2b4ae
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,38 @@ func indexCPEList(list CpeList) *dictionary.Indexed {
}

for _, cpeItem := range list.CpeItems {
cpeItemName := cpeItem.Cpe23Item.Name

for _, reference := range cpeItem.References {
ref := reference.Reference.Href

switch {
case strings.HasPrefix(ref, prefixForNPMPackages):
addEntryForNPMPackage(indexed, ref, cpeItem)
addEntryForNPMPackage(indexed, ref, cpeItemName)

case strings.HasPrefix(ref, prefixForRubyGems), strings.HasPrefix(ref, prefixForRubyGemsHTTP):
addEntryForRubyGem(indexed, ref, cpeItem)
addEntryForRubyGem(indexed, ref, cpeItemName)

case strings.HasPrefix(ref, prefixForNativeRubyGems):
addEntryForNativeRubyGem(indexed, ref, cpeItem)
addEntryForNativeRubyGem(indexed, ref, cpeItemName)

case strings.HasPrefix(ref, prefixForPyPIPackages):
addEntryForPyPIPackage(indexed, ref, cpeItem)
addEntryForPyPIPackage(indexed, ref, cpeItemName)

case strings.HasPrefix(ref, prefixForJenkinsPlugins):
// It _might_ be a jenkins plugin!
addEntryForJenkinsPlugin(indexed, ref, cpeItem)
addEntryForJenkinsPlugin(indexed, ref, cpeItemName)

case strings.HasPrefix(ref, prefixForRustCrates):
addEntryForRustCrate(indexed, ref, cpeItem)
addEntryForRustCrate(indexed, ref, cpeItemName)
}
}
}

return indexed
}

func addEntryForRustCrate(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForRustCrate(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.TrimPrefix(ref, prefixForRustCrates)
ref = strings.Split(ref, "/")[0]
Expand All @@ -155,10 +157,10 @@ func addEntryForRustCrate(indexed *dictionary.Indexed, ref string, cpeItem CpeIt
indexed.EcosystemPackages[dictionary.EcosystemRustCrates] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemRustCrates][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemRustCrates][ref] = cpeItemName
}

func addEntryForJenkinsPlugin(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForJenkinsPlugin(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.TrimPrefix(ref, prefixForJenkinsPlugins)
ref = strings.Split(ref, "/")[0]
Expand All @@ -174,10 +176,10 @@ func addEntryForJenkinsPlugin(indexed *dictionary.Indexed, ref string, cpeItem C
indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins][ref] = cpeItemName
}

func addEntryForPyPIPackage(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForPyPIPackage(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.TrimPrefix(ref, prefixForPyPIPackages)
ref = strings.Split(ref, "/")[0]
Expand All @@ -186,10 +188,10 @@ func addEntryForPyPIPackage(indexed *dictionary.Indexed, ref string, cpeItem Cpe
indexed.EcosystemPackages[dictionary.EcosystemPyPI] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemPyPI][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemPyPI][ref] = cpeItemName
}

func addEntryForNativeRubyGem(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForNativeRubyGem(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.TrimPrefix(ref, prefixForNativeRubyGems)
ref = strings.Split(ref, "/")[0]
Expand All @@ -198,10 +200,10 @@ func addEntryForNativeRubyGem(indexed *dictionary.Indexed, ref string, cpeItem C
indexed.EcosystemPackages[dictionary.EcosystemRubyGems] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItemName
}

func addEntryForRubyGem(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForRubyGem(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.TrimPrefix(ref, prefixForRubyGems)
ref = strings.TrimPrefix(ref, prefixForRubyGemsHTTP)
Expand All @@ -211,10 +213,10 @@ func addEntryForRubyGem(indexed *dictionary.Indexed, ref string, cpeItem CpeItem
indexed.EcosystemPackages[dictionary.EcosystemRubyGems] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItemName
}

func addEntryForNPMPackage(indexed *dictionary.Indexed, ref string, cpeItem CpeItem) {
func addEntryForNPMPackage(indexed *dictionary.Indexed, ref string, cpeItemName string) {
// Prune off the non-package-name parts of the URL
ref = strings.Split(ref, "/v/")[0]
ref = strings.Split(ref, "?")[0]
Expand All @@ -224,5 +226,5 @@ func addEntryForNPMPackage(indexed *dictionary.Indexed, ref string, cpeItem CpeI
indexed.EcosystemPackages[dictionary.EcosystemNPM] = make(dictionary.Packages)
}

indexed.EcosystemPackages[dictionary.EcosystemNPM][ref] = cpeItem.Cpe23Item.Name
indexed.EcosystemPackages[dictionary.EcosystemNPM][ref] = cpeItemName
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/anchore/syft/syft/pkg/cataloger/common/cpe/dictionary"
)

func Test_generateIndexedDictionaryJSON(t *testing.T) {
Expand Down Expand Up @@ -40,3 +42,128 @@ func Test_generateIndexedDictionaryJSON(t *testing.T) {
t.Errorf("generateIndexedDictionaryJSON() mismatch (-want +got):\n%s", diff)
}
}

func Test_addEntryFuncs(t *testing.T) {
tests := []struct {
name string
addEntryFunc func(indexed *dictionary.Indexed, ref string, cpeItemName string)
inputRef string
inputCpeItemName string
expectedIndexed dictionary.Indexed
}{
{
name: "addEntryForRustCrate",
addEntryFunc: addEntryForRustCrate,
inputRef: "https://crates.io/crates/unicycle/versions",
inputCpeItemName: "cpe:2.3:a:unicycle_project:unicycle:*:*:*:*:*:rust:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemRustCrates: {
"unicycle": "cpe:2.3:a:unicycle_project:unicycle:*:*:*:*:*:rust:*:*",
},
},
},
},
{
name: "addEntryForJenkinsPlugin",
addEntryFunc: addEntryForJenkinsPlugin,
inputRef: "https://github.com/jenkinsci/sonarqube-plugin",
inputCpeItemName: "cpe:2.3:a:sonarsource:sonarqube_scanner:2.7:*:*:*:*:jenkins:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemJenkinsPlugins: {
"sonarqube": "cpe:2.3:a:sonarsource:sonarqube_scanner:2.7:*:*:*:*:jenkins:*:*",
},
},
},
},
{
name: "addEntryForJenkinsPlugin: not actually a plugin",
addEntryFunc: addEntryForJenkinsPlugin,
inputRef: "https://github.com/jenkinsci/jenkins",
inputCpeItemName: "cpe:2.3:a:jenkins:jenkinsci:2.7:*:*:*:*:*:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{},
},
},
{
name: "addEntryForPyPIPackage",
addEntryFunc: addEntryForPyPIPackage,
inputRef: "https://pypi.org/project/vault-cli/#history",
inputCpeItemName: "cpe:2.3:a:vault-cli_project:vault-cli:*:*:*:*:*:python:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemPyPI: {
"vault-cli": "cpe:2.3:a:vault-cli_project:vault-cli:*:*:*:*:*:python:*:*",
},
},
},
},
{
name: "addEntryForNativeRubyGem",
addEntryFunc: addEntryForNativeRubyGem,
inputRef: "https://github.com/ruby/openssl/releases",
inputCpeItemName: "cpe:2.3:a:ruby-lang:openssl:-:*:*:*:*:ruby:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemRubyGems: {
"openssl": "cpe:2.3:a:ruby-lang:openssl:-:*:*:*:*:ruby:*:*",
},
},
},
},
{
name: "addEntryForRubyGem: https",
addEntryFunc: addEntryForRubyGem,
inputRef: "https://rubygems.org/gems/actionview/versions",
inputCpeItemName: "cpe:2.3:a:action_view_project:action_view:*:*:*:*:*:ruby:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemRubyGems: {
"actionview": "cpe:2.3:a:action_view_project:action_view:*:*:*:*:*:ruby:*:*",
},
},
},
},
{
name: "addEntryForRubyGem: http",
addEntryFunc: addEntryForRubyGem,
inputRef: "http://rubygems.org/gems/rbovirt",
inputCpeItemName: "cpe:2.3:a:amos_benari:rbovirt:*:*:*:*:*:ruby:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemRubyGems: {
"rbovirt": "cpe:2.3:a:amos_benari:rbovirt:*:*:*:*:*:ruby:*:*",
},
},
},
},
{
name: "addEntryForNPMPackage",
addEntryFunc: addEntryForNPMPackage,
inputRef: "https://www.npmjs.com/package/@nubosoftware/node-static",
inputCpeItemName: "cpe:2.3:a:\\@nubosoftware\\/node-static_project:\\@nubosoftware\\/node-static:-:*:*:*:*:node.js:*:*",
expectedIndexed: dictionary.Indexed{
EcosystemPackages: map[string]dictionary.Packages{
dictionary.EcosystemNPM: {
"@nubosoftware/node-static": "cpe:2.3:a:\\@nubosoftware\\/node-static_project:\\@nubosoftware\\/node-static:-:*:*:*:*:node.js:*:*",
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
indexed := &dictionary.Indexed{
EcosystemPackages: make(map[string]dictionary.Packages),
}

tt.addEntryFunc(indexed, tt.inputRef, tt.inputCpeItemName)

if diff := cmp.Diff(tt.expectedIndexed, *indexed); diff != "" {
t.Errorf("addEntry* mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 1a2b4ae

Please sign in to comment.