Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nil check to CycloneDX enumeration (#320)
Fix #319
- Loading branch information
1 parent
cfe6d75
commit 3812a9b
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package sbom_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/osv-scanner/internal/sbom" | ||
) | ||
|
||
func runCycloneGetPackages(t *testing.T, bomFile string, want []sbom.Identifier) { | ||
t.Helper() | ||
|
||
f, err := os.Open(filepath.Join("fixtures", bomFile)) | ||
if err != nil { | ||
t.Fatalf("Failed to read fixture file: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
got := []sbom.Identifier{} | ||
callback := func(id sbom.Identifier) error { | ||
got = append(got, id) | ||
return nil | ||
} | ||
|
||
cdx := &sbom.CycloneDX{} | ||
err = cdx.GetPackages(f, callback) | ||
if err != nil { | ||
t.Errorf("GetPackages returned an error: %v", err) | ||
} | ||
|
||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("GetPackages() returned an unexpected result (-want, +got):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCycloneDXGetPackages(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
bomFile string | ||
identifiers []sbom.Identifier | ||
}{ | ||
{ | ||
bomFile: "cyclonedx.json", | ||
identifiers: []sbom.Identifier{ | ||
{PURL: "pkg:maven/org.hdrhistogram/HdrHistogram@2.1.12"}, | ||
{PURL: "pkg:maven/org.apache.logging.log4j/log4j-core@2.16.0"}, | ||
}, | ||
}, | ||
{ | ||
bomFile: "cyclonedx-empty.json", | ||
identifiers: []sbom.Identifier{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
runCycloneGetPackages(t, tt.bomFile, tt.identifiers) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"bomFormat": "CycloneDX", | ||
"specVersion": "1.4", | ||
"version": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"bomFormat": "CycloneDX", | ||
"specVersion": "1.4", | ||
"version": 1, | ||
"components": [ | ||
{ | ||
"type": "container", | ||
"name": "/target.tar", | ||
"components": [ | ||
{ | ||
"type": "library", | ||
"name": "HdrHistogram", | ||
"purl": "pkg:maven/org.hdrhistogram/HdrHistogram@2.1.12" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "library", | ||
"name": "Apache Log4j Core", | ||
"purl": "pkg:maven/org.apache.logging.log4j/log4j-core@2.16.0" | ||
} | ||
] | ||
} |