Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/sbom-scorecard/cmd/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"errors"
Expand Down Expand Up @@ -34,7 +33,7 @@ func init() {
}

func determineSbomType(filepath string) string {
content, err := ioutil.ReadFile(filepath)
content, err := os.ReadFile(filepath)
if err != nil {
panic(fmt.Sprintf("Error! %v", err))
}
Expand Down
3 changes: 3 additions & 0 deletions examples/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makeJSON": "happy"
}This is intentionally invalid
14 changes: 11 additions & 3 deletions pkg/cdx/cdx_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cdx
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"

cdx "github.com/CycloneDX/cyclonedx-go"
Expand All @@ -25,6 +25,11 @@ type CycloneDXReport struct {
hasCPE int
}

var missingPackages = scorecard.ReportValue{
Ratio: 0,
Reasoning: "No packages",
}

func (r *CycloneDXReport) Report() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%d total packages\n", r.totalPackages))
Expand Down Expand Up @@ -57,6 +62,9 @@ func (r *CycloneDXReport) IsSpecCompliant() scorecard.ReportValue {
}

func (r *CycloneDXReport) PackageIdentification() scorecard.ReportValue {
if r.totalPackages == 0 {
return missingPackages
}
purlPercent := scorecard.PrettyPercent(r.hasPurl, r.totalPackages)
cpePercent := scorecard.PrettyPercent(r.hasCPE, r.totalPackages)
return scorecard.ReportValue{
Expand Down Expand Up @@ -90,7 +98,7 @@ func (r *CycloneDXReport) CreationInfo() scorecard.ReportValue {
}

func GetCycloneDXReport(filename string) scorecard.SbomReport {
contents, err := ioutil.ReadFile(filename)
contents, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("Error while opening %v for reading: %v", filename, err)
return nil
Expand All @@ -116,7 +124,7 @@ func GetCycloneDXReport(filename string) scorecard.SbomReport {
return &r
}

if bom.Metadata.Tools != nil {
if bom.Metadata != nil && bom.Metadata.Tools != nil {
for _, t := range *bom.Metadata.Tools {
if t.Name != "" {
r.creationToolName += 1
Expand Down
14 changes: 14 additions & 0 deletions pkg/cdx/cdx_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ Package Licenses: 18/20
Creation Info: 15/15
Total points: 88/100 or 88%`)
}

func TestCycloneInvalid(t *testing.T) {
r := GetCycloneDXReport("../../examples/invalid.json")

report_text := scorecard.Grade(r)
assertTextEqual(t,
report_text,
`Spec Compliance: 0/25
Package ID: 0/20 (0% have purls and 0% have CPEs)
Package Versions: 0/20
Package Licenses: 0/20
Creation Info: 0/15
Total points: 0/100 or 0%`)
}
8 changes: 7 additions & 1 deletion pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ type ScoreValue struct {
MaxPoints float32
}

func isNaN(f float32) bool { return f != f }

func (sv *ScoreValue) Score() float32 {
return sv.Ratio * sv.MaxPoints
if isNaN(sv.Ratio) {
return 0
} else {
return sv.Ratio * sv.MaxPoints
}
}

type ReportResult struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/spdx/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"

spdx_json "github.com/spdx/tools-golang/json"
spdx_rdf "github.com/spdx/tools-golang/rdfloader"
Expand Down Expand Up @@ -37,7 +37,7 @@ type File struct {
}

func LoadDocument(path string) (Document, error) {
f, err := ioutil.ReadFile(path)
f, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("opening SPDX document: %w", err)
}
Expand Down